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

Add WalletConnect signing manager #50

Merged
merged 1 commit into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions commitlint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ module.exports = {
'hashicorp-vault-signing-manager',
'approval-signing-manager',
'fireblocks-signing-manager',
'walletconnect-signing-manager',
],
],
},
Expand Down
3 changes: 3 additions & 0 deletions packages/walletconnect-signing-manager/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": [["@nrwl/web/babel", { "useBuiltIns": "usage" }]]
}
18 changes: 18 additions & 0 deletions packages/walletconnect-signing-manager/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"extends": ["../../.eslintrc.json"],
"ignorePatterns": ["!**/*"],
"overrides": [
{
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
"rules": {}
},
{
"files": ["*.ts", "*.tsx"],
"rules": {}
},
{
"files": ["*.js", "*.jsx"],
"rules": {}
}
]
}
Empty file.
142 changes: 142 additions & 0 deletions packages/walletconnect-signing-manager/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
# WalletConnect Signing Manager

This library provides a Polymesh SDK-compatible signing manager that enables interaction with a WalletConnect-compatible wallet, such as a remote mobile wallet.

The WalletConnect signing manager is designed to have a similar API to the Browser Extension Signing Manager to simplify the effort for an integration to support both options.

## Creating a Signing Manager

To use the `WalletConnectSigningManager`, follow these steps:

### 1. Import the Necessary Modules

- Import the `WalletConnectSigningManager` class from the `@polymeshassociation/walletconnect-signing-manager` package.
- Import the `Polymesh` class from the `@polymeshassociation/polymesh-sdk` package.

```typescript
import { WalletConnectSigningManager } from '@polymeshassociation/walletconnect-signing-manager';
import { Polymesh } from '@polymeshassociation/polymesh-sdk';
```

### 2. Define WalletConnect Configuration

Define the `walletConnectConfiguration` object with the necessary parameters.

Provide details for each configuration parameter:

- `projectId`: Obtain from [WalletConnect](https://cloud.walletconnect.com/).
- `relayUrl` (optional): Override the default relay endpoint.
- `metadata` (optional): Metadata displayed in the wallet connecting to your app.
- `chainIds`: CASA CAIP-2 representation of the chain. Polymesh instances begin with 'polkadot:' + first 32 bytes of the genesis hash. The connected wallet must support the provided chain ID.
- `optionalChainIds` (optional): Additional chain IDs. Not mandatory for wallet support.
- `modalOptions` (optional): WalletConnect modal configuration parameters. Refer to [WalletConnect documentation](https://docs.walletconnect.com/advanced/walletconnectmodal/options) for options.
- `handleConnectUri` (optional): Callback to handle the WalletConnect URI. Runs only once when the URI is generated. If provided, the WalletConnect modal will not be displayed.
- `onSessionDelete` (optional): Callback function to run when a WalletConnect session is deleted.

```typescript
const walletConnectConfiguration = {
projectId: '427...',
relayUrl: 'wss://relay.walletconnect.org',
metadata: {
name: 'My App',
description: 'App for interacting with the Polymesh Blockchain',
url: 'https://example.com',
icons: ['https://walletconnect.com/walletconnect-logo.png'],
},
chainIds: ['polkadot:6fbd74e5e1d0a61d52ccfe9d4adaed16'],
optionalChainIds: ['polkadot:2ace05e703aa50b48c0ccccfc8b424f7'],
modalOptions: {
// See WalletConnect documentation for options
},
handleConnectUri: uri => {
// Code to handle the WalletConnect URI.
// Note: If provided, the WalletConnect modal will not be displayed.
},
onSessionDelete: () => {
// Code to run on session delete.
},
};
```

### 3. Create a WalletConnect Connection

- Use the `create()` method of the `WalletConnectSigningManager` class to create a connection.
- Pass the `walletConnectConfiguration` object and other optional parameters:
- `appName`: Name of the dApp attempting to connect to the extension.
- `ss58Format` (optional): SS58 prefix for encoding addresses. When the SDK connects to a node this gets set to the chain specific.
- `genesisHash` (optional): Genesis hash of the target chain. This is required only when signing raw data to configure the chainId for. When signing transactions prepared by the SDK the chainId will be derived from the genesisHash contained in the transaction payload.

When called, the generated URI or QR code must be used to make the WalletConnect connection before the signing manager is returned.

```typescript
const signingManager = await WalletConnectSigningManager.create({
config: walletConnectConfiguration,
appName: 'My App',
ss58Format: 12, // (optional)
genesisHash: '0x6fbd74e5e1d0a61d52ccfe9d4adaed16dd3a7caa37c6bc4d0c2fa12e8b2f4063', // (optional)
});
```

### 4. Attach the Signing Manager to the Polymesh SDK

- Connect the `signingManager` instance to the Polymesh SDK using the `Polymesh.connect()` method.

```typescript
const polymesh = await Polymesh.connect({
nodeUrl,
signingManager,
});
```

Now you have successfully created a `WalletConnectSigningManager` instance and attached it to the Polymesh SDK, allowing interaction with a WalletConnect-compatible wallet.

## Additional Methods

### Setting SS58 Format and Genesis Hash

Use the `setSs58Format()` method and `setGenesisHash()` method to set the SS58 prefix and genesis hash, respectively.

```typescript
signingManager.setSs58Format(42);
signingManager.setGenesisHash('0x123456789abcdef');
```

### Getting Accounts

To retrieve accounts from the connected wallet, use the `getAccounts()` or `getAccountsWithMeta()` methods.

```typescript
const accounts = await signingManager.getAccounts();
const accountsWithMeta = await signingManager.getAccountsWithMeta();
```

### Getting External Signer

Use the `getExternalSigner()` method to get a `WalletConnectSigner` object that uses connected walletConnect accounts for signing.

```typescript
const externalSigner = signingManager.getExternalSigner();
```

### Subscribing to Account Changes

Subscribe to changes in the connected wallet's accounts using the `onAccountChange()` method.

```typescript
const unsubscribe = signingManager.onAccountChange(accounts => {
// Handle account change event
});
```

### Disconnecting and Checking Connection Status

Use the `disconnect()` method to disconnect the connected WalletConnect session, and `isConnected()` method to check the connection status.

```typescript
await signingManager.disconnect();
const isConnected = signingManager.isConnected();
```

## Info

This library was generated with [Nx](https://nx.dev).
13 changes: 13 additions & 0 deletions packages/walletconnect-signing-manager/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module.exports = {
displayName: 'walletconnect-signing-manager',
preset: '../../jest.preset.js',
globals: {
'ts-jest': {
tsconfig: '<rootDir>/tsconfig.spec.json',
},
},
testEnvironment: 'node',
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'],
coverageDirectory: '../../coverage/packages/walletconnect-signing-manager',
restoreMocks: true,
};
24 changes: 24 additions & 0 deletions packages/walletconnect-signing-manager/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "@polymeshassociation/walletconnect-signing-manager",
"version": "0.1.0",
"main": "./index.js",
"typings": "./index.d.ts",
"repository": {
"type": "git",
"url": "https://github.com/PolymeshAssociation/signing-managers.git"
},
"publishConfig": {
"access": "public"
},
"dependencies": {
"@polymeshassociation/signing-manager-types": "^3.3.0",
"@walletconnect/modal": "^2.6.2",
"@walletconnect/sign-client": "2.13.0"
},
"devDependencies": {
"@walletconnect/types": "2.13.0"
},
"peerDependencies": {
"@polymeshassociation/polymesh-sdk": ">=15.0.0"
}
}
51 changes: 51 additions & 0 deletions packages/walletconnect-signing-manager/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{
"root": "packages/walletconnect-signing-manager",
"sourceRoot": "packages/walletconnect-signing-manager/src",
"projectType": "library",
"targets": {
"build": {
"executor": "@nrwl/node:package",
"outputs": ["{options.outputPath}"],
"options": {
"outputPath": "dist/packages/walletconnect-signing-manager",
"tsConfig": "packages/walletconnect-signing-manager/tsconfig.lib.json",
"packageJson": "packages/walletconnect-signing-manager/package.json",
"main": "packages/walletconnect-signing-manager/src/index.ts",
"assets": ["packages/walletconnect-signing-manager/*.md"],
"srcRootForCompilationRoot": "packages/walletconnect-signing-manager/src"
}
},
"lint": {
"executor": "@nrwl/linter:eslint",
"outputs": ["{options.outputFile}"],
"options": {
"lintFilePatterns": ["packages/walletconnect-signing-manager/**/*.ts"]
}
},
"test": {
"executor": "@nrwl/jest:jest",
"outputs": ["coverage/packages/walletconnect-signing-manager"],
"options": {
"jestConfig": "packages/walletconnect-signing-manager/jest.config.js",
"passWithNoTests": true
}
},
"release": {
"executor": "@ng-easy/builders:semantic-release",
"configurations": {
"local": {
"force": true
}
}
},
"run-local": {
"executor": "./tools/executors/run-local:run-local",
"options": {
"runInBrowser": false,
"path": "packages/walletconnect-signing-manager/sandbox/index.ts",
"port": 9000
}
}
},
"tags": []
}
3 changes: 3 additions & 0 deletions packages/walletconnect-signing-manager/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/* istanbul ignore file */

export { WalletConnectSigningManager } from './lib/walletconnect-signing-manager';
Loading
Loading