Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial Mobile support (ussing cip45) #179

Merged
merged 4 commits into from
Jan 29, 2024
Merged

Initial Mobile support (ussing cip45) #179

merged 4 commits into from
Jan 29, 2024

Conversation

hrajchert
Copy link
Collaborator

@hrajchert hrajchert commented Jan 22, 2024

Summary

This PR adds initial Mobile support to the Marlowe TS-SDK by providing an adaptor module to the cardano-peer-connect library. You can follow the library instructions to configure a Dapp and use the new @marlowe.io/wallet/peer-connect adaptor to provide the onApiInject and onApiEject methods.

How to test

An example is provided in the examples/cip45-flow folder

  1. Build the project
$ npm i
$ npm run build
  1. Serve the examples folder
$ npm run serve-dev
  1. Open the browser at http://localhost:1337/examples/cip45-flow/
Screenshot 2024-01-22 at 13 39 22
  1. Use a cip45 wallet like eternl and scan the QR code to connect to the wallet
  2. After the prompt to connect, you can click the create contract button.
Screenshot 2024-01-22 at 13 42 53 Screenshot 2024-01-22 at 13 44 01 Screenshot 2024-01-22 at 13 44 47

Current limitations found:

  1. The mobile phone can connect to a dapp running on a Desktop browser, but not to a dapp running on the mobile browser. I would assume this has to do with the inject mechanism, as far as I know mobile wallets don't allow injection of code in the global window object and that is why CIP-30 cannot be implemented directly. This does pose a limitation on users, as the interactive part needs to happen from the Desktop browser and the wallet can only sign.
  2. The Desktop browser dapp can only handle a connection from one mobile wallet at a time. If a wallet is connected to the dapp and you try to connect from another wallet, you get an error. I was expecting to be able to access both wallets and coordinate a contract between them. If each mobile phone could use their own dapp from the mobile browser (limitation 1) then this wouldn't be that much of an issue.
  3. The cardano-peer-connect library doesn't seem to offer a disconnect method from the DAppPeerConnect class. So the wallet can disconnect from the dapp but the dapp can't easily disconnect from the wallet. See Issue 57

Summary by CodeRabbit

Summary by CodeRabbit

  • New Features

    • Introduced a web interface for testing CIP45 flow including wallet connection and contract interaction.
    • Added a new example with documentation for using the Marlowe Runtime REST client.
  • Documentation

    • Started a work-in-progress guide for the CIP45 flow example.
  • Refactor

    • Improved module import handling with updated CDN links for necessary JavaScript files.
  • Style

    • Set up a new web page layout for CIP45 flow testing with input fields, wallet information, and a console display.

Copy link
Contributor

coderabbitai bot commented Jan 22, 2024

Walkthrough

The updates revolve around the integration and testing of the @marlowe.io/wallet/peer-connect module for CIP45 flow in a web environment. A new example with a README, HTML interface, and JavaScript logic has been introduced to facilitate wallet connections and Marlowe contract interactions. Adjustments to the wallet package improve code visibility and maintainability, while a new peer connection module enhances wallet API management.

Changes

Files Change Summary
examples/cip45-flow/Readme.md
examples/cip45-flow/index.html
examples/cip45-flow/index.js
Introduced a work-in-progress readme, a web page, and script to test CIP45 flow, including wallet connections and contract interactions.
jsdelivr-npm-importmap.js Added a CDN entry for @marlowe.io/wallet/peer-connect module.
packages/wallet/src/browser/index.ts
packages/wallet/src/peer-connect/index.ts
Refactored named exports and added a new module for peer wallet connection management.

🐇🎉
To code anew, under the moon,
Wallets connect, contracts bloom.
Peer through the web, a handshake tight,
Marlowe flows, in bytes take flight. 🚀🌕

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share

Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit-tests for this file.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit tests for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository from git and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit tests.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/coderabbit-overrides.v2.json

CodeRabbit Discord Community

Join our Discord Community to get help, request features, and share feedback.

Co-authored-by: Brian W Bush <[email protected]>
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 76b2336 and 9135b5e.
Files ignored due to path filters (1)
  • packages/wallet/package.json is excluded by: !**/*.json
Files selected for processing (8)
  • changelog.d/20240112_130130_hrajchert_plt_9089_createContract_bundle_support.md (1 hunks)
  • changelog.d/20240122_133315_hrajchert_cip45.md (1 hunks)
  • examples/cip45-flow/Readme.md (1 hunks)
  • examples/cip45-flow/index.html (1 hunks)
  • examples/cip45-flow/index.js (1 hunks)
  • jsdelivr-npm-importmap.js (1 hunks)
  • packages/wallet/src/browser/index.ts (2 hunks)
  • packages/wallet/src/peer-connect/index.ts (1 hunks)
Files skipped from review due to trivial changes (1)
  • changelog.d/20240112_130130_hrajchert_plt_9089_createContract_bundle_support.md
Files skipped from review as they are similar to previous changes (6)
  • examples/cip45-flow/Readme.md
  • examples/cip45-flow/index.html
  • examples/cip45-flow/index.js
  • jsdelivr-npm-importmap.js
  • packages/wallet/src/browser/index.ts
  • packages/wallet/src/peer-connect/index.ts
Additional comments: 1
changelog.d/20240122_133315_hrajchert_cip45.md (1)
  • 4-4: The changelog entry is clear and informative, providing necessary details about the new feature and its associated PR.

import * as Browser from "../browser/index.js";

type WalletHandler = (walletId: string, wallet: WalletAPI) => void;
export const mkPeerConnectAdapter = () => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is hard to understand

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can add some documentation around it. Have you seen the example to clarify on use?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or is there anything in particular that you would need clarification?

Copy link
Collaborator

@nhenin nhenin Jan 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would have wrapped this function directly in that file :

const dAppConnect = new CardanoPeerConnect.DAppPeerConnect({
  dAppInfo: dAppInfo,
  verifyConnection: verifyConnection,
  onApiInject: adapter.adaptApiInject,
  onApiEject: adapter.adaptApiEject,
});

And provide explanation about how to create and use this DAppPeerConnect function. I would have kept adapter.adaptApiInject and adapter.adaptApiEject private into this module also

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nevertheless, it is good enough like that imo... I would focus on WalletConnect and merge this as it is...

getTokens: Browser.getTokens(di),
getLovelaces: Browser.getLovelaces(di),
};
wallet = peerWallet;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not using this function instead ?

/**
 * Returns an instance of the browser wallet API for the specified wallet.
 * @param walletName - The name of the wallet to get an instance of.
 * @returns An instance of the BrowserWalletAPI class.
 */
export async function mkBrowserWallet(
  walletName: SupportedWalletName
): Promise<WalletAPI> {
  if (
    getInstalledWalletExtensions()
      .map((extension) => extension.name.toLowerCase())
      .includes(walletName.toLowerCase())
  ) {
    const extension = await window.cardano[walletName.toLowerCase()].enable();
    const di = { extension };
    return {
      waitConfirmation: waitConfirmation(di),
      signTx: signTx(di),
      getChangeAddress: getChangeAddress(di),
      getUsedAddresses: getUsedAddresses(di),
      getCollaterals: getCollaterals(di),
      getUTxOs: getUTxOs(di),
      isMainnet: isMainnet(di),
      getTokens: getTokens(di),
      getLovelaces: getLovelaces(di),
    };
  } else {
    throw new Error(`Wallet ${walletName} is not available in the browser`);
  }
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and why exposing all the internal functions ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, after you do the handshake, cardano peer connect offers a CIP-30 compatible API and I needed an adaptor from CIP-30 to Marlowe's WalletAPI.

This is not a browser wallet, so using that function would be confusing. I also did not wanted to change getInstalledWalletExtensions for the same reason.

Note that the actor in charge of creating the wallet is inverted from normal uses. We get a callback when somebody connects rather than the dapp being able to create a wallet connection at will.

Instead of exposing the inner functions we could create an adaptCIP30Wallet and use that instead.

If we want to add support for the walletConnect standard through adalib we can also use the same adaptor

@hrajchert hrajchert merged commit ffaa183 into main Jan 29, 2024
2 checks passed
@hrajchert hrajchert deleted the hrajchert/cip45 branch January 29, 2024 15:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants