@hashgraph/hedera-wallet-connect 1.3.2-0 → 1.3.2-1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (99) hide show
  1. package/README.md +4 -4
  2. package/dist/src/index.d.ts +1 -0
  3. package/dist/src/index.js +20 -0
  4. package/dist/{types/src → src}/lib/dapp/DAppSigner.d.ts +0 -1
  5. package/dist/src/lib/dapp/DAppSigner.js +214 -0
  6. package/dist/{types/src → src}/lib/dapp/index.d.ts +0 -1
  7. package/dist/src/lib/dapp/index.js +431 -0
  8. package/dist/{types/src → src}/lib/index.d.ts +0 -1
  9. package/dist/src/lib/index.js +22 -0
  10. package/dist/{types/src → src}/lib/shared/chainIds.d.ts +0 -1
  11. package/dist/src/lib/shared/chainIds.js +31 -0
  12. package/dist/{types/src → src}/lib/shared/errors.d.ts +0 -1
  13. package/dist/src/lib/shared/errors.js +51 -0
  14. package/dist/{types/src → src}/lib/shared/events.d.ts +0 -1
  15. package/dist/src/lib/shared/events.js +27 -0
  16. package/dist/{types/src → src}/lib/shared/extensionController.d.ts +0 -1
  17. package/dist/src/lib/shared/extensionController.js +42 -0
  18. package/dist/{types/src → src}/lib/shared/index.d.ts +0 -1
  19. package/dist/src/lib/shared/index.js +26 -0
  20. package/dist/{types/src → src}/lib/shared/methods.d.ts +0 -1
  21. package/dist/src/lib/shared/methods.js +32 -0
  22. package/dist/{types/src → src}/lib/shared/payloads.d.ts +0 -1
  23. package/dist/src/lib/shared/payloads.js +20 -0
  24. package/dist/{types/src → src}/lib/shared/utils.d.ts +0 -1
  25. package/dist/src/lib/shared/utils.js +424 -0
  26. package/dist/{types/src → src}/lib/wallet/index.d.ts +0 -1
  27. package/dist/src/lib/wallet/index.js +301 -0
  28. package/dist/{types/src → src}/lib/wallet/provider.d.ts +0 -1
  29. package/dist/src/lib/wallet/provider.js +58 -0
  30. package/dist/{types/src → src}/lib/wallet/types.d.ts +0 -1
  31. package/dist/src/lib/wallet/types.js +20 -0
  32. package/dist/{types/test → test}/_helpers.d.ts +0 -1
  33. package/dist/test/_helpers.js +95 -0
  34. package/dist/test/dapp/DAppConnector.test.d.ts +1 -0
  35. package/dist/test/dapp/DAppConnector.test.js +236 -0
  36. package/dist/test/dapp/DAppSigner.test.d.ts +1 -0
  37. package/dist/test/dapp/DAppSigner.test.js +155 -0
  38. package/dist/test/utils.test.d.ts +1 -0
  39. package/dist/test/utils.test.js +202 -0
  40. package/dist/test/wallet/methods/wallet-executeTransaction.test.d.ts +1 -0
  41. package/dist/test/wallet/methods/wallet-executeTransaction.test.js +43 -0
  42. package/dist/test/wallet/methods/wallet-getNodeAddresses.test.d.ts +1 -0
  43. package/dist/test/wallet/methods/wallet-getNodeAddresses.test.js +40 -0
  44. package/dist/test/wallet/methods/wallet-signAndExecuteQuery.test.d.ts +1 -0
  45. package/dist/test/wallet/methods/wallet-signAndExecuteQuery.test.js +41 -0
  46. package/dist/test/wallet/methods/wallet-signAndExecuteTransaction.test.d.ts +1 -0
  47. package/dist/test/wallet/methods/wallet-signAndExecuteTransaction.test.js +48 -0
  48. package/dist/test/wallet/methods/wallet-signMessage.test.d.ts +1 -0
  49. package/dist/test/wallet/methods/wallet-signMessage.test.js +60 -0
  50. package/dist/test/wallet/methods/wallet-signTransaction.test.d.ts +1 -0
  51. package/dist/test/wallet/methods/wallet-signTransaction.test.js +45 -0
  52. package/dist/test/wallet/wallet-init.test.d.ts +1 -0
  53. package/dist/test/wallet/wallet-init.test.js +53 -0
  54. package/package.json +11 -19
  55. package/dist/browser-cjs-metafile.json +0 -1
  56. package/dist/browser-cjs.js +0 -250
  57. package/dist/browser-esm-metafile.json +0 -1
  58. package/dist/browser-esm.js +0 -250
  59. package/dist/node-cjs-metafile.json +0 -1
  60. package/dist/node-cjs.js +0 -11404
  61. package/dist/node-esm-metafile.json +0 -1
  62. package/dist/node-esm.js +0 -11404
  63. package/dist/types/src/index.d.ts +0 -2
  64. package/dist/types/src/index.d.ts.map +0 -1
  65. package/dist/types/src/lib/dapp/DAppSigner.d.ts.map +0 -1
  66. package/dist/types/src/lib/dapp/index.d.ts.map +0 -1
  67. package/dist/types/src/lib/index.d.ts.map +0 -1
  68. package/dist/types/src/lib/shared/chainIds.d.ts.map +0 -1
  69. package/dist/types/src/lib/shared/errors.d.ts.map +0 -1
  70. package/dist/types/src/lib/shared/events.d.ts.map +0 -1
  71. package/dist/types/src/lib/shared/extensionController.d.ts.map +0 -1
  72. package/dist/types/src/lib/shared/index.d.ts.map +0 -1
  73. package/dist/types/src/lib/shared/methods.d.ts.map +0 -1
  74. package/dist/types/src/lib/shared/payloads.d.ts.map +0 -1
  75. package/dist/types/src/lib/shared/utils.d.ts.map +0 -1
  76. package/dist/types/src/lib/wallet/index.d.ts.map +0 -1
  77. package/dist/types/src/lib/wallet/provider.d.ts.map +0 -1
  78. package/dist/types/src/lib/wallet/types.d.ts.map +0 -1
  79. package/dist/types/test/_helpers.d.ts.map +0 -1
  80. package/dist/types/test/dapp/DAppConnector.test.d.ts +0 -2
  81. package/dist/types/test/dapp/DAppConnector.test.d.ts.map +0 -1
  82. package/dist/types/test/dapp/DAppSigner.test.d.ts +0 -2
  83. package/dist/types/test/dapp/DAppSigner.test.d.ts.map +0 -1
  84. package/dist/types/test/utils.test.d.ts +0 -2
  85. package/dist/types/test/utils.test.d.ts.map +0 -1
  86. package/dist/types/test/wallet/methods/wallet-executeTransaction.test.d.ts +0 -2
  87. package/dist/types/test/wallet/methods/wallet-executeTransaction.test.d.ts.map +0 -1
  88. package/dist/types/test/wallet/methods/wallet-getNodeAddresses.test.d.ts +0 -2
  89. package/dist/types/test/wallet/methods/wallet-getNodeAddresses.test.d.ts.map +0 -1
  90. package/dist/types/test/wallet/methods/wallet-signAndExecuteQuery.test.d.ts +0 -2
  91. package/dist/types/test/wallet/methods/wallet-signAndExecuteQuery.test.d.ts.map +0 -1
  92. package/dist/types/test/wallet/methods/wallet-signAndExecuteTransaction.test.d.ts +0 -2
  93. package/dist/types/test/wallet/methods/wallet-signAndExecuteTransaction.test.d.ts.map +0 -1
  94. package/dist/types/test/wallet/methods/wallet-signMessage.test.d.ts +0 -2
  95. package/dist/types/test/wallet/methods/wallet-signMessage.test.d.ts.map +0 -1
  96. package/dist/types/test/wallet/methods/wallet-signTransaction.test.d.ts +0 -2
  97. package/dist/types/test/wallet/methods/wallet-signTransaction.test.d.ts.map +0 -1
  98. package/dist/types/test/wallet/wallet-init.test.d.ts +0 -2
  99. package/dist/types/test/wallet/wallet-init.test.d.ts.map +0 -1
package/README.md CHANGED
@@ -67,14 +67,14 @@ use this library’s functions to easily create and handle requests for the Hede
67
67
 
68
68
  ### Installation
69
69
 
70
- `npm i --save @hashgraph/hedera-wallet-connect @hashgraph/sdk`
70
+ `npm i --save @hashgraph/hedera-wallet-connect`
71
71
 
72
72
  ### Example code
73
73
 
74
- There is a working example written in vanilla html css and typescript.
74
+ - [Typescript dApp example code](src/examples/typescript/dapp/main.ts)
75
+ - [Typescript Wallet example code](src/examples/typescript/wallet/main.ts)
76
+ - [React dApp example code](demos/react-dapp)
75
77
 
76
- - [dApp example code](src/examples/typescript/dapp/main.ts)
77
- - [Wallet example code](src/examples/typescript/wallet/main.ts)
78
78
 
79
79
  ### DApps
80
80
 
@@ -0,0 +1 @@
1
+ export * from './lib';
@@ -0,0 +1,20 @@
1
+ /*
2
+ *
3
+ * Hedera Wallet Connect
4
+ *
5
+ * Copyright (C) 2023 Hedera Hashgraph, LLC
6
+ *
7
+ * Licensed under the Apache License, Version 2.0 (the "License");
8
+ * you may not use this file except in compliance with the License.
9
+ * You may obtain a copy of the License at
10
+ *
11
+ * http://www.apache.org/licenses/LICENSE-2.0
12
+ *
13
+ * Unless required by applicable law or agreed to in writing, software
14
+ * distributed under the License is distributed on an "AS IS" BASIS,
15
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ * See the License for the specific language governing permissions and
17
+ * limitations under the License.
18
+ *
19
+ */
20
+ export * from './lib';
@@ -41,4 +41,3 @@ export declare class DAppSigner implements Signer {
41
41
  private _tryExecuteQueryRequest;
42
42
  call<RequestT, ResponseT, OutputT>(request: Executable<RequestT, ResponseT, OutputT>): Promise<OutputT>;
43
43
  }
44
- //# sourceMappingURL=DAppSigner.d.ts.map
@@ -0,0 +1,214 @@
1
+ /*
2
+ *
3
+ * Hedera Wallet Connect
4
+ *
5
+ * Copyright (C) 2023 Hedera Hashgraph, LLC
6
+ *
7
+ * Licensed under the Apache License, Version 2.0 (the "License");
8
+ * you may not use this file except in compliance with the License.
9
+ * You may obtain a copy of the License at
10
+ *
11
+ * http://www.apache.org/licenses/LICENSE-2.0
12
+ *
13
+ * Unless required by applicable law or agreed to in writing, software
14
+ * distributed under the License is distributed on an "AS IS" BASIS,
15
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ * See the License for the specific language governing permissions and
17
+ * limitations under the License.
18
+ *
19
+ */
20
+ import { AccountBalance, AccountId, AccountInfo, LedgerId, SignerSignature, Transaction, TransactionRecord, Client, PublicKey, TransactionId, TransactionResponse, Query, AccountRecordsQuery, AccountInfoQuery, AccountBalanceQuery, TransactionReceiptQuery, TransactionReceipt, } from '@hashgraph/sdk';
21
+ import { proto } from '@hashgraph/proto';
22
+ import { HederaJsonRpcMethod, Uint8ArrayToBase64String, base64StringToSignatureMap, base64StringToUint8Array, ledgerIdToCAIPChainId, queryToBase64String, transactionBodyToBase64String, transactionToBase64String, transactionToTransactionBody, extensionOpen, } from '../shared';
23
+ const clients = {};
24
+ export class DAppSigner {
25
+ constructor(accountId, signClient, topic, ledgerId = LedgerId.MAINNET, extensionId) {
26
+ this.accountId = accountId;
27
+ this.signClient = signClient;
28
+ this.topic = topic;
29
+ this.ledgerId = ledgerId;
30
+ this.extensionId = extensionId;
31
+ }
32
+ _getHederaClient() {
33
+ const ledgerIdString = this.ledgerId.toString();
34
+ if (!clients[ledgerIdString]) {
35
+ clients[ledgerIdString] = Client.forName(ledgerIdString);
36
+ }
37
+ return clients[ledgerIdString];
38
+ }
39
+ get _signerAccountId() {
40
+ return `${ledgerIdToCAIPChainId(this.ledgerId)}:${this.accountId.toString()}`;
41
+ }
42
+ _getRandomNodes(numberOfNodes) {
43
+ const allNodes = Object.values(this._getHederaClient().network).map((o) => typeof o === 'string' ? AccountId.fromString(o) : o);
44
+ // shuffle nodes
45
+ for (let i = allNodes.length - 1; i > 0; i--) {
46
+ const j = Math.floor(Math.random() * (i + 1));
47
+ [allNodes[i], allNodes[j]] = [allNodes[j], allNodes[i]];
48
+ }
49
+ return allNodes.slice(0, numberOfNodes);
50
+ }
51
+ request(request) {
52
+ if (this.extensionId)
53
+ extensionOpen(this.extensionId);
54
+ return this.signClient.request({
55
+ topic: this.topic,
56
+ request,
57
+ chainId: ledgerIdToCAIPChainId(this.ledgerId),
58
+ });
59
+ }
60
+ getAccountId() {
61
+ return this.accountId;
62
+ }
63
+ getAccountKey() {
64
+ throw new Error('Method not implemented.');
65
+ }
66
+ getLedgerId() {
67
+ return this.ledgerId;
68
+ }
69
+ getNetwork() {
70
+ return this._getHederaClient().network;
71
+ }
72
+ getMirrorNetwork() {
73
+ return this._getHederaClient().mirrorNetwork;
74
+ }
75
+ getAccountBalance() {
76
+ return this.call(new AccountBalanceQuery().setAccountId(this.accountId));
77
+ }
78
+ getAccountInfo() {
79
+ return this.call(new AccountInfoQuery().setAccountId(this.accountId));
80
+ }
81
+ getAccountRecords() {
82
+ return this.call(new AccountRecordsQuery().setAccountId(this.accountId));
83
+ }
84
+ async sign(data, signOptions) {
85
+ const { signatureMap } = await this.request({
86
+ method: HederaJsonRpcMethod.SignMessage,
87
+ params: {
88
+ signerAccountId: this._signerAccountId,
89
+ message: Uint8ArrayToBase64String(data[0]),
90
+ },
91
+ });
92
+ const sigmap = base64StringToSignatureMap(signatureMap);
93
+ const signerSignature = new SignerSignature({
94
+ accountId: this.getAccountId(),
95
+ publicKey: PublicKey.fromBytes(sigmap.sigPair[0].pubKeyPrefix),
96
+ signature: sigmap.sigPair[0].ed25519 ||
97
+ sigmap.sigPair[0].ECDSASecp256k1,
98
+ });
99
+ return [signerSignature];
100
+ }
101
+ async checkTransaction(transaction) {
102
+ throw new Error('Method not implemented.');
103
+ }
104
+ async populateTransaction(transaction) {
105
+ return transaction
106
+ .setNodeAccountIds(this._getRandomNodes(10)) // allow retrying on up to 10 nodes
107
+ .setTransactionId(TransactionId.generate(this.getAccountId()));
108
+ }
109
+ /**
110
+ * Prepares a transaction object for signing using a single node account id.
111
+ * If the transaction object does not already have a node account id,
112
+ * generate a random node account id using the Hedera SDK client
113
+ *
114
+ * @param transaction - Any instance of a class that extends `Transaction`
115
+ * @returns transaction - `Transaction` object with signature
116
+ */
117
+ async signTransaction(transaction) {
118
+ let nodeAccountId;
119
+ if (!transaction.nodeAccountIds || transaction.nodeAccountIds.length === 0)
120
+ nodeAccountId = this._getRandomNodes(1)[0];
121
+ else
122
+ nodeAccountId = transaction.nodeAccountIds[0];
123
+ const transactionBody = transactionToTransactionBody(transaction, nodeAccountId);
124
+ const transactionBodyBase64 = transactionBodyToBase64String(transactionBody);
125
+ const { signatureMap } = await this.request({
126
+ method: HederaJsonRpcMethod.SignTransaction,
127
+ params: {
128
+ signerAccountId: this._signerAccountId,
129
+ transactionBody: transactionBodyBase64,
130
+ },
131
+ });
132
+ const sigMap = base64StringToSignatureMap(signatureMap);
133
+ const bodyBytes = base64StringToUint8Array(transactionBodyBase64);
134
+ const bytes = proto.Transaction.encode({ bodyBytes, sigMap }).finish();
135
+ return Transaction.fromBytes(bytes);
136
+ }
137
+ async _tryExecuteTransactionRequest(request) {
138
+ try {
139
+ const transaction = Transaction.fromBytes(request.toBytes());
140
+ const result = await this.request({
141
+ method: HederaJsonRpcMethod.SignAndExecuteTransaction,
142
+ params: {
143
+ signerAccountId: this._signerAccountId,
144
+ transactionList: transactionToBase64String(transaction),
145
+ },
146
+ });
147
+ return { result: TransactionResponse.fromJSON(result) };
148
+ }
149
+ catch (error) {
150
+ return { error };
151
+ }
152
+ }
153
+ async _parseQueryResponse(query, base64EncodedQueryResponse) {
154
+ if (query instanceof AccountRecordsQuery) {
155
+ const base64EncodedQueryResponseSplit = base64EncodedQueryResponse.split(',');
156
+ const data = base64EncodedQueryResponseSplit.map((o) => base64StringToUint8Array(o));
157
+ return data.map((o) => TransactionRecord.fromBytes(o));
158
+ }
159
+ const data = base64StringToUint8Array(base64EncodedQueryResponse);
160
+ if (query instanceof AccountBalanceQuery) {
161
+ return AccountBalance.fromBytes(data);
162
+ }
163
+ else if (query instanceof AccountInfoQuery) {
164
+ return AccountInfo.fromBytes(data);
165
+ }
166
+ else if (query instanceof TransactionReceiptQuery) {
167
+ return TransactionReceipt.fromBytes(data);
168
+ }
169
+ else {
170
+ throw new Error('Unsupported query type');
171
+ }
172
+ }
173
+ async _tryExecuteQueryRequest(request) {
174
+ try {
175
+ const query = Query.fromBytes(request.toBytes());
176
+ const result = await this.request({
177
+ method: HederaJsonRpcMethod.SignAndExecuteQuery,
178
+ params: {
179
+ signerAccountId: this._signerAccountId,
180
+ query: queryToBase64String(query),
181
+ },
182
+ });
183
+ return { result: this._parseQueryResponse(query, result.response) };
184
+ }
185
+ catch (error) {
186
+ return { error };
187
+ }
188
+ }
189
+ async call(request) {
190
+ var _a, _b, _c, _d, _e, _f;
191
+ const txResult = await this._tryExecuteTransactionRequest(request);
192
+ if (txResult.result) {
193
+ return txResult.result;
194
+ }
195
+ const queryResult = await this._tryExecuteQueryRequest(request);
196
+ if (queryResult.result) {
197
+ return queryResult.result;
198
+ }
199
+ // TODO: make this error more usable
200
+ throw new Error('Error executing transaction or query: \n' +
201
+ JSON.stringify({
202
+ txError: {
203
+ name: (_a = txResult.error) === null || _a === void 0 ? void 0 : _a.name,
204
+ message: (_b = txResult.error) === null || _b === void 0 ? void 0 : _b.message,
205
+ stack: (_c = txResult.error) === null || _c === void 0 ? void 0 : _c.stack,
206
+ },
207
+ queryError: {
208
+ name: (_d = queryResult.error) === null || _d === void 0 ? void 0 : _d.name,
209
+ message: (_e = queryResult.error) === null || _e === void 0 ? void 0 : _e.message,
210
+ stack: (_f = queryResult.error) === null || _f === void 0 ? void 0 : _f.stack,
211
+ },
212
+ }, null, 2));
213
+ }
214
+ }
@@ -188,4 +188,3 @@ export declare class DAppConnector {
188
188
  signTransaction(params: SignTransactionParams): Promise<SignTransactionResult>;
189
189
  }
190
190
  export default DAppConnector;
191
- //# sourceMappingURL=index.d.ts.map