@hashgraph/hedera-wallet-connect 0.1.0 → 1.0.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (58) hide show
  1. package/.github/ISSUE_TEMPLATE/bug_report.md +35 -0
  2. package/.github/ISSUE_TEMPLATE/feature_request.md +17 -0
  3. package/.github/ISSUE_TEMPLATE/hip_820.md +16 -0
  4. package/.github/dependabot.yml +17 -0
  5. package/.github/workflows/prettier.yml +28 -0
  6. package/.github/workflows/publish.yml +35 -0
  7. package/.github/workflows/test.yml +32 -0
  8. package/.husky/pre-commit +4 -0
  9. package/.lintstagedrc.json +5 -0
  10. package/.prettierignore +3 -0
  11. package/LICENSE +201 -21
  12. package/README.md +132 -107
  13. package/dist/browser-cjs-metafile.json +1 -0
  14. package/dist/browser-cjs.js +240 -0
  15. package/dist/browser-esm-metafile.json +1 -0
  16. package/dist/browser-esm.js +240 -0
  17. package/dist/node-cjs-metafile.json +1 -0
  18. package/dist/node-cjs.js +11391 -0
  19. package/dist/node-esm-metafile.json +1 -0
  20. package/dist/node-esm.js +11391 -0
  21. package/jest.config.ts +33 -0
  22. package/package.json +59 -31
  23. package/scripts/examples/build.mjs +54 -0
  24. package/scripts/examples/dev.mjs +54 -0
  25. package/scripts/lib/build.mjs +40 -0
  26. package/scripts/lib/context.mjs +71 -0
  27. package/scripts/lib/watch.mjs +28 -0
  28. package/typedoc.json +10 -0
  29. package/.editorconfig +0 -19
  30. package/.github/workflows/npm-publish.yml +0 -18
  31. package/lib/esm/Connector.js +0 -81
  32. package/lib/esm/Connector.js.map +0 -1
  33. package/lib/esm/DAppConnector.js +0 -118
  34. package/lib/esm/DAppConnector.js.map +0 -1
  35. package/lib/esm/ErrorHelper.js +0 -97
  36. package/lib/esm/ErrorHelper.js.map +0 -1
  37. package/lib/esm/Utils.js +0 -99
  38. package/lib/esm/Utils.js.map +0 -1
  39. package/lib/esm/WCSigner.js +0 -241
  40. package/lib/esm/WCSigner.js.map +0 -1
  41. package/lib/esm/WalletConnector.js +0 -203
  42. package/lib/esm/WalletConnector.js.map +0 -1
  43. package/lib/esm/index.js +0 -4
  44. package/lib/esm/index.js.map +0 -1
  45. package/lib/esm/types/Connector.d.ts +0 -18
  46. package/lib/esm/types/Connector.d.ts.map +0 -1
  47. package/lib/esm/types/DAppConnector.d.ts +0 -27
  48. package/lib/esm/types/DAppConnector.d.ts.map +0 -1
  49. package/lib/esm/types/ErrorHelper.d.ts +0 -15
  50. package/lib/esm/types/ErrorHelper.d.ts.map +0 -1
  51. package/lib/esm/types/Utils.d.ts +0 -39
  52. package/lib/esm/types/Utils.d.ts.map +0 -1
  53. package/lib/esm/types/WCSigner.d.ts +0 -33
  54. package/lib/esm/types/WCSigner.d.ts.map +0 -1
  55. package/lib/esm/types/WalletConnector.d.ts +0 -21
  56. package/lib/esm/types/WalletConnector.d.ts.map +0 -1
  57. package/lib/esm/types/index.d.ts +0 -4
  58. package/lib/esm/types/index.d.ts.map +0 -1
package/README.md CHANGED
@@ -1,119 +1,144 @@
1
- # **Hedera Wallet Connect**
1
+ # Overview
2
2
 
3
- This package is a messaging relay between decentralized applications and wallets in Hedera network based on Wallet Connect relays.
3
+ This library is the result of Hedera community collaboration to bring Hedera into the
4
+ WalletConnect ecosystem and vice versa.
4
5
 
5
- ## Getting started
6
- ### Installation
7
- 1. Install npm package `hedera-wallet-connect`;
8
- ```bash
9
- npm install hedera-wallet-connect
10
- ```
6
+ The goal of this repository is to be a reference for wallets and dApps integrating the
7
+ WalletConnect <> Hedera JSON-RPC reference. Additionally, this library is meant to be included
8
+ in projects supporting WalletConnect and Hedera, providing utility functions useful to
9
+ validating requests and resposes in both the WalletConnect JSON-RPC context as well as the
10
+ Hedera context.
11
11
 
12
- ### DApp section
13
- 1. Import `DAppConnector` from `hedera-wallet-connect` package:
14
- ```typescript
15
- import {DAppConnector} from "hedera-wallet-connect";
16
- ```
17
- 2. Create an instance of DAppConnector: `this.dAppConnector = new DAppConnector();`.
18
- You can optionally pass application metadata to the constructor.
19
- Metadata is a general WalletConnect metadata with provided structure:
20
- ```typescript
21
- type Metadata = {
22
- name: string; // Name of your DApp
23
- description: string; // Description for your DApp
24
- url: string; // URL adress of your DApp
25
- icons: string[]; // Icons for displaying in connector
26
- }
27
- ```
28
- If not specified metadata will be automatically composed using website meta content.
29
- 3. Execute `init` method on instance of DAppConnector. You can pass array of custom event names you would like to receive from Wallet (They will be sent by wallet only if supported).
30
- ```typescript
31
- await this.dAppConnector.init(["someEventName"])
32
- ```
33
- 4. In order to request connection to the Wallet run `connect` method. You can pass LedgerId to this method in order to select other than MAINNET Ledger.
34
- ```typescript
35
- await this.dAppConnector.connect(LedgerId.TESTNET)
36
- ```
37
- If app is reloading calling `connect` method will try to resume existing session instead of opening new pairing window.
38
- 5. When connection is established you should request signers array by calling `getSigners` method.
39
- ```typescript
40
- await this.dAppConnector.getSigners()
41
- ```
42
- This will return array of Signer (HIP-338) interfaces for all allowed by Wallet accounts.
43
- Use them to execute methods on behalf of wallet account.
44
- You can also subscribe to the events sent by the connected Wallet. Use `events$` subscription provided by DAppConnector.
45
- ```typescript
46
- this.dAppConnector.$events.subscribe(
47
- ({name, data}: {name: string, data: any}) => {
48
- console.log(`Event ${name}: ${JSON.stringify(data)}`);
49
- })
50
- ```
12
+ A few useful resources include:
51
13
 
52
- ### Wallet section
53
- 1. Import `WalletConnector` from `hedera-wallet-connect` package:
54
- ```typescript
55
- import {WalletConnector} from "hedera-wallet-connect";
56
- ```
57
- 2. Create an instance of WalletConnector:
58
- ```typescript
59
- this.walletConnector = new WalletConnector();
60
- ```
61
- You can optionally pass application metadata to the constructor.
62
- Metadata is a general WalletConnect metadata with provided structure:
63
- ```typescript
64
- type Metadata = {
65
- name: string; // Name of your DApp
66
- description: string; // Description for your DApp
67
- url: string; // URL adress of your DApp
68
- icons: string[]; // Icons for displaying in connector
69
- }
70
- ```
71
- If not specified metadata will be automatically composed using website meta content.
72
- It's advisable to specify metadata in case of extension or hybrid app wallet.
73
- 3. Execute `init` method on instance of DAppConnector. You should pass callback method of your wallet as an argument.
74
- This callback will be executed as soon as proposal for connection will arrive from the DApp. As an argument for this callback you will get WalletConnect session proposal.
75
- ```typescript
76
- type ProposalCallback = (proposal: SignClientTypes.EventArguments["session_proposal"]) => Promise<void>;
77
- ```
78
- ```typescript
79
- await this.walletConnector.init(proposalCallback)
80
- ```
81
- 4. In order to connect to DApp just pass URI string to `pair` method of connector
82
- ```typescript
83
- this.walletConnector.pair(uri)
84
- ```
85
- 5. After reviewing of current proposal you should approve or reject it by calling appropriate methods:
86
- ```typescript
87
- rejectSessionProposal(data: RejectParams)
88
- ```
89
- or
90
- ```typescript
91
- approveSessionProposal<T extends Signer>(data: ApproveParams, signers: T[])
92
- ```
93
- When approving session except ApproveParams you should also provide Signer (HIP-338) implementations for all approved accounts.
94
- 6. Wallet can also send events to the dApp using method `sendEvent`. With name and payload:
95
- ```typescript
96
- await this.walletConnector.sendEvent(name, data);
97
- ```
14
+ - [HIP-820](https://hips.hedera.com/hip/hip-820)
15
+ - [WalletConnect <> Hedera JSON-RPC spec](https://specs.walletconnect.com/2.0/blockchain-rpc/hedera-rpc).
16
+
17
+ > WalletConnect brings the ecosystem together by enabling wallets and apps to securely connect
18
+ > and interact.
19
+ >
20
+ > -- <cite> https://walletconnect.com
21
+
22
+ Hedera aims to be:
23
+
24
+ > The open source public ledger for everyone
25
+ >
26
+ > -- <cite> https://hedera.com
27
+
28
+ ---
29
+
30
+ This package managed by the Hedera community and is intended to be a standard for ecosystem
31
+ wallets and dApp providers utilizing [WalletConnect](https://walletconnect.com) as a their
32
+ communications protocol. It utilizes the
33
+ [`@hashgraph/sdk`](https://www.npmjs.com/package/@hashgraph/sdk) and provides functions to
34
+ facilitate implementing the
35
+ [WalletConnect <> Hedera JSON-RPC spec](https://specs.walletconnect.com/2.0/blockchain-rpc/hedera-rpc)
36
+ which has been defined through the collaborative HIP process in
37
+ [HIP-820](https://hips.hedera.com/hip/hip-820).
38
+
39
+ This library facilitates the implementation of the **WalletConnect <> Hedera Spec** which allows
40
+ wallets and dApps to natively integrate with Hedera. It provides additional, out of network
41
+ functionality with the `hedera_signMessage` function.
42
+
43
+ In short, it uses the Hedera javascript SDK to build transactions, serialize them, send to
44
+ wallets for processing and return responses back to dApps.
45
+
46
+ _Please note, this is distinct from the
47
+ [Implementation of Ethereum JSON-RPC APIs for Hedera](https://github.com/hashgraph/hedera-json-rpc-relay).
48
+ At the time of this writing, "the Hedera JSON-RPC relay implementation is in beta, offers
49
+ limited functionality today, and is only available to developers."_
50
+
51
+ _The relay and this library have different intentions and serve different purposes - namely
52
+ native Hedera integration vs. Ethereum compatability layers to ease developer onboarding for
53
+ those more familiar with the Ethereum ecosystem._
54
+
55
+ ## Set up
98
56
 
99
- ### Common section
100
- 1. You can check whether connector is initialized by checking initialized field:
101
- ```typescript
102
- this.connector.initialized === true
57
+ To start using WalletConnect, sign up for an account at <https://cloud.walletconnect.com>. You
58
+ will use your project id when initializing client libraries.
59
+
60
+ It is important to understand core WalletConnect concepts when integrating this library. Please
61
+ reference the [WalletConnect documentation](https://docs.walletconnect.com/2.0/).
62
+
63
+ ## Usage
64
+
65
+ Upon successfully configuring your dApp and/or wallet to manage WalletConnect sessions, you can
66
+ use this library’s functions to easily create and handle requests for the Hedera network.
67
+
68
+ ### Wallet
69
+
70
+ This library provides a Wallet class that extends the
71
+ [ Web3Wallet ](https://github.com/WalletConnect/walletconnect-monorepo/tree/v2.0/packages/web3wallet)
72
+ class provided by WalletConnect class
73
+
74
+ #### Event Listeners
75
+
76
+ WalletConnect emits various events during a session. Listen to these events to synchronize the
77
+ state of your application:
78
+
79
+ ```javascript
80
+ // Handle pairing proposals
81
+ signClient.on('session_proposal', (event) => {
82
+ // Display session proposal to the user and decide to approve or reject
83
+ })
84
+
85
+ // Handle session requests, like signing transactions or messages
86
+ signClient.on('session_request', (event) => {
87
+ // Process the session request
88
+ })
89
+
90
+ // Handle session deletions
91
+ signClient.on('session_delete', (event) => {
92
+ // React to session termination
93
+ })
103
94
  ```
104
- 2. In order to disconnect call `disconnect` method.
105
- ```typescript
106
- this.connector.disconnect()
95
+
96
+ #### Pairing with dApps
97
+
98
+ Pairing establishes a connection between the wallet and a dApp. Once paired, the dApp can send
99
+ session requests to the wallet.
100
+
101
+ ##### a. Pairing via URI
102
+
103
+ If a dApp shares a URI for pairing:
104
+
105
+ ```javascript
106
+ await signClient.core.pairing.pair({ uri: 'RECEIVED_URI' })
107
107
  ```
108
108
 
109
+ Upon successful pairing, the `session_proposal` event will be triggered.
110
+
111
+ ##### b. Pairing via QR Codes
109
112
 
110
- ## Utils
113
+ For a better user experience, dApps often share QR codes that wallets can scan to establish a
114
+ pairing. Use a QR code scanning library to scan and obtain the URI, then proceed with pairing:
111
115
 
112
- Conversion between WC chainId and Hedera LedgerId is possible using methods:
113
- ```typescript
114
- getChainByLedgerId: (ledgerId: LedgerId) => string;
115
- getLedgerIdByChainId: (chainId: string) => string;
116
+ ```javascript
117
+ const scannedUri = '...' // URI obtained from scanning the QR code
118
+ await signClient.core.pairing.pair({ uri: scannedUri })
116
119
  ```
117
120
 
118
- # License
119
- This repository is distributed under the terms of the MIT License. See [LICENSE](LICENSE) for details.
121
+ #### Handling Session Proposals
122
+
123
+ Upon receiving a `session_proposal` event, display the proposal details to the user. Allow them
124
+ to approve or reject the session:
125
+
126
+ ##### Handling Session Requests
127
+
128
+ Upon receiving a `session_request` event, process the request. For instance, if the dApp
129
+ requests a transaction to be signed:
130
+
131
+ ## Demo & docs
132
+
133
+ This repository includes a vanilla html/css/javascript implementation with a dApp and wallet
134
+ example useful for testing and development while integrating WalletConnect and Hedera.
135
+
136
+ The docs site utilizes [Typedoc](https://typedoc.org) to generate a library documentation site
137
+ at <https://wc.hgraph.app/docs/>
138
+
139
+ The demo source code lives in `./src/examples/typescript` and is available at
140
+ <https://wc.hgraph.app>
141
+
142
+ ## Passing tests
143
+
144
+ - `git commit -Ss "the commit message"`