@hashgraph/hedera-wallet-connect 1.3.1 → 1.3.2-1
Sign up to get free protection for your applications and to get access to all the features.
- package/README.md +11 -0
- package/dist/src/index.d.ts +1 -0
- package/dist/src/index.js +20 -0
- package/dist/{types/src → src}/lib/dapp/DAppSigner.d.ts +0 -1
- package/dist/src/lib/dapp/DAppSigner.js +214 -0
- package/dist/{types/src → src}/lib/dapp/index.d.ts +0 -1
- package/dist/src/lib/dapp/index.js +431 -0
- package/dist/{types/src → src}/lib/index.d.ts +0 -1
- package/dist/src/lib/index.js +22 -0
- package/dist/{types/src → src}/lib/shared/chainIds.d.ts +0 -1
- package/dist/src/lib/shared/chainIds.js +31 -0
- package/dist/{types/src → src}/lib/shared/errors.d.ts +0 -1
- package/dist/src/lib/shared/errors.js +51 -0
- package/dist/{types/src → src}/lib/shared/events.d.ts +0 -1
- package/dist/src/lib/shared/events.js +27 -0
- package/dist/{types/src → src}/lib/shared/extensionController.d.ts +0 -1
- package/dist/src/lib/shared/extensionController.js +42 -0
- package/dist/{types/src → src}/lib/shared/index.d.ts +0 -1
- package/dist/src/lib/shared/index.js +26 -0
- package/dist/{types/src → src}/lib/shared/methods.d.ts +0 -1
- package/dist/src/lib/shared/methods.js +32 -0
- package/dist/{types/src → src}/lib/shared/payloads.d.ts +0 -1
- package/dist/src/lib/shared/payloads.js +20 -0
- package/dist/{types/src → src}/lib/shared/utils.d.ts +0 -1
- package/dist/src/lib/shared/utils.js +424 -0
- package/dist/{types/src → src}/lib/wallet/index.d.ts +0 -1
- package/dist/src/lib/wallet/index.js +301 -0
- package/dist/{types/src → src}/lib/wallet/provider.d.ts +0 -1
- package/dist/src/lib/wallet/provider.js +58 -0
- package/dist/{types/src → src}/lib/wallet/types.d.ts +0 -1
- package/dist/src/lib/wallet/types.js +20 -0
- package/dist/{types/test → test}/_helpers.d.ts +0 -1
- package/dist/test/_helpers.js +95 -0
- package/dist/test/dapp/DAppConnector.test.d.ts +1 -0
- package/dist/test/dapp/DAppConnector.test.js +236 -0
- package/dist/test/dapp/DAppSigner.test.d.ts +1 -0
- package/dist/test/dapp/DAppSigner.test.js +155 -0
- package/dist/test/utils.test.d.ts +1 -0
- package/dist/test/utils.test.js +202 -0
- package/dist/test/wallet/methods/wallet-executeTransaction.test.d.ts +1 -0
- package/dist/test/wallet/methods/wallet-executeTransaction.test.js +43 -0
- package/dist/test/wallet/methods/wallet-getNodeAddresses.test.d.ts +1 -0
- package/dist/test/wallet/methods/wallet-getNodeAddresses.test.js +40 -0
- package/dist/test/wallet/methods/wallet-signAndExecuteQuery.test.d.ts +1 -0
- package/dist/test/wallet/methods/wallet-signAndExecuteQuery.test.js +41 -0
- package/dist/test/wallet/methods/wallet-signAndExecuteTransaction.test.d.ts +1 -0
- package/dist/test/wallet/methods/wallet-signAndExecuteTransaction.test.js +48 -0
- package/dist/test/wallet/methods/wallet-signMessage.test.d.ts +1 -0
- package/dist/test/wallet/methods/wallet-signMessage.test.js +60 -0
- package/dist/test/wallet/methods/wallet-signTransaction.test.d.ts +1 -0
- package/dist/test/wallet/methods/wallet-signTransaction.test.js +45 -0
- package/dist/test/wallet/wallet-init.test.d.ts +1 -0
- package/dist/test/wallet/wallet-init.test.js +53 -0
- package/package.json +13 -19
- package/dist/browser-cjs-metafile.json +0 -1
- package/dist/browser-cjs.js +0 -250
- package/dist/browser-esm-metafile.json +0 -1
- package/dist/browser-esm.js +0 -250
- package/dist/node-cjs-metafile.json +0 -1
- package/dist/node-cjs.js +0 -11404
- package/dist/node-esm-metafile.json +0 -1
- package/dist/node-esm.js +0 -11404
- package/dist/types/src/index.d.ts +0 -2
- package/dist/types/src/index.d.ts.map +0 -1
- package/dist/types/src/lib/dapp/DAppSigner.d.ts.map +0 -1
- package/dist/types/src/lib/dapp/index.d.ts.map +0 -1
- package/dist/types/src/lib/index.d.ts.map +0 -1
- package/dist/types/src/lib/shared/chainIds.d.ts.map +0 -1
- package/dist/types/src/lib/shared/errors.d.ts.map +0 -1
- package/dist/types/src/lib/shared/events.d.ts.map +0 -1
- package/dist/types/src/lib/shared/extensionController.d.ts.map +0 -1
- package/dist/types/src/lib/shared/index.d.ts.map +0 -1
- package/dist/types/src/lib/shared/methods.d.ts.map +0 -1
- package/dist/types/src/lib/shared/payloads.d.ts.map +0 -1
- package/dist/types/src/lib/shared/utils.d.ts.map +0 -1
- package/dist/types/src/lib/wallet/index.d.ts.map +0 -1
- package/dist/types/src/lib/wallet/provider.d.ts.map +0 -1
- package/dist/types/src/lib/wallet/types.d.ts.map +0 -1
- package/dist/types/test/_helpers.d.ts.map +0 -1
- package/dist/types/test/dapp/DAppConnector.test.d.ts +0 -2
- package/dist/types/test/dapp/DAppConnector.test.d.ts.map +0 -1
- package/dist/types/test/dapp/DAppSigner.test.d.ts +0 -2
- package/dist/types/test/dapp/DAppSigner.test.d.ts.map +0 -1
- package/dist/types/test/utils.test.d.ts +0 -2
- package/dist/types/test/utils.test.d.ts.map +0 -1
- package/dist/types/test/wallet/methods/wallet-executeTransaction.test.d.ts +0 -2
- package/dist/types/test/wallet/methods/wallet-executeTransaction.test.d.ts.map +0 -1
- package/dist/types/test/wallet/methods/wallet-getNodeAddresses.test.d.ts +0 -2
- package/dist/types/test/wallet/methods/wallet-getNodeAddresses.test.d.ts.map +0 -1
- package/dist/types/test/wallet/methods/wallet-signAndExecuteQuery.test.d.ts +0 -2
- package/dist/types/test/wallet/methods/wallet-signAndExecuteQuery.test.d.ts.map +0 -1
- package/dist/types/test/wallet/methods/wallet-signAndExecuteTransaction.test.d.ts +0 -2
- package/dist/types/test/wallet/methods/wallet-signAndExecuteTransaction.test.d.ts.map +0 -1
- package/dist/types/test/wallet/methods/wallet-signMessage.test.d.ts +0 -2
- package/dist/types/test/wallet/methods/wallet-signMessage.test.d.ts.map +0 -1
- package/dist/types/test/wallet/methods/wallet-signTransaction.test.d.ts +0 -2
- package/dist/types/test/wallet/methods/wallet-signTransaction.test.d.ts.map +0 -1
- package/dist/types/test/wallet/wallet-init.test.d.ts +0 -2
- package/dist/types/test/wallet/wallet-init.test.d.ts.map +0 -1
@@ -0,0 +1,32 @@
|
|
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
|
+
/**
|
21
|
+
* Enum representing different JSON-RPC methods related to Hedera.
|
22
|
+
* The methods are ordered alphabetically for ease of use and readability.
|
23
|
+
*/
|
24
|
+
export var HederaJsonRpcMethod;
|
25
|
+
(function (HederaJsonRpcMethod) {
|
26
|
+
HederaJsonRpcMethod["GetNodeAddresses"] = "hedera_getNodeAddresses";
|
27
|
+
HederaJsonRpcMethod["ExecuteTransaction"] = "hedera_executeTransaction";
|
28
|
+
HederaJsonRpcMethod["SignMessage"] = "hedera_signMessage";
|
29
|
+
HederaJsonRpcMethod["SignAndExecuteQuery"] = "hedera_signAndExecuteQuery";
|
30
|
+
HederaJsonRpcMethod["SignAndExecuteTransaction"] = "hedera_signAndExecuteTransaction";
|
31
|
+
HederaJsonRpcMethod["SignTransaction"] = "hedera_signTransaction";
|
32
|
+
})(HederaJsonRpcMethod || (HederaJsonRpcMethod = {}));
|
@@ -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 {};
|
@@ -0,0 +1,424 @@
|
|
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 { Buffer } from 'buffer';
|
21
|
+
import { AccountId, Transaction, LedgerId, Query, } from '@hashgraph/sdk';
|
22
|
+
import { proto } from '@hashgraph/proto';
|
23
|
+
/**
|
24
|
+
* Freezes a transaction if it is not already frozen. Transactions must
|
25
|
+
* be frozen before they can be converted to bytes.
|
26
|
+
*
|
27
|
+
* @param transaction - Any instance of a class that extends `Transaction`
|
28
|
+
*/
|
29
|
+
export function freezeTransaction(transaction) {
|
30
|
+
if (!transaction.isFrozen())
|
31
|
+
transaction.freeze();
|
32
|
+
}
|
33
|
+
/**
|
34
|
+
* Sets default consensus nodes that a transaction will be submitted to. Node Account ID(s)
|
35
|
+
* must be set before a transaction can be frozen. If they have already been set, this
|
36
|
+
* function will not modify the transaction.
|
37
|
+
* @param transaction - any instance of a class that extends `Transaction`
|
38
|
+
*
|
39
|
+
* @see {@link https://docs.hedera.com/hedera/networks/testnet/testnet-nodes | Full list of Testnet-nodes}
|
40
|
+
* @see {@link https://docs.hedera.com/hedera/networks/mainnet/mainnet-nodes | Full list of Mainnet-nodes}
|
41
|
+
*/
|
42
|
+
export function setDefaultNodeAccountIds(transaction) {
|
43
|
+
const isNodeAccountIdNotSet = !transaction.nodeAccountIds || transaction.nodeAccountIds.length === 0;
|
44
|
+
if (!transaction.isFrozen() && isNodeAccountIdNotSet)
|
45
|
+
transaction.setNodeAccountIds([new AccountId(3), new AccountId(4), new AccountId(5)]);
|
46
|
+
}
|
47
|
+
/**
|
48
|
+
* Converts `Transaction` to a Base64-string.
|
49
|
+
*
|
50
|
+
* First converts a transaction to bytes and then encodes it as a Base64-string. Will attempt
|
51
|
+
* to set default Node Account ID and freeze the transaction before converting.
|
52
|
+
* @param transaction - Any instance of a class that extends `Transaction`
|
53
|
+
* @returns Base64 encoded representation of the input `Transaction` object
|
54
|
+
*/
|
55
|
+
export function transactionToBase64String(transaction) {
|
56
|
+
setDefaultNodeAccountIds(transaction);
|
57
|
+
freezeTransaction(transaction);
|
58
|
+
const transactionBytes = transaction.toBytes();
|
59
|
+
return Buffer.from(transactionBytes).toString('base64');
|
60
|
+
}
|
61
|
+
/**
|
62
|
+
* Recreates a `Transaction` from a base64 encoded string.
|
63
|
+
*
|
64
|
+
* Decodes the string to a buffer,
|
65
|
+
* then passes to `Transaction.fromBytes`. For greater flexibility, this function uses the base
|
66
|
+
* `Transaction` class, but takes an optional type parameter if the type of transaction is known,
|
67
|
+
* allowing stronger typeing.
|
68
|
+
* @param transactionBytes - a base64 encoded string
|
69
|
+
* @returns `Transaction`
|
70
|
+
* @example
|
71
|
+
* ```ts
|
72
|
+
* const txn1 = base64StringToTransaction(bytesString)
|
73
|
+
* const txn2 = base64StringToTransaction<TransferTransaction>(bytesString)
|
74
|
+
* // txn1 type: Transaction
|
75
|
+
* // txn2 type: TransferTransaction
|
76
|
+
* ```
|
77
|
+
*/
|
78
|
+
export function base64StringToTransaction(transactionBytes) {
|
79
|
+
const decoded = Buffer.from(transactionBytes, 'base64');
|
80
|
+
return Transaction.fromBytes(decoded);
|
81
|
+
}
|
82
|
+
/**
|
83
|
+
* @param transaction - a base64 encoded string of proto.TransactionBody.encode().finish()
|
84
|
+
* @returns `string`
|
85
|
+
* */
|
86
|
+
export function transactionToTransactionBody(transaction, nodeAccountId) {
|
87
|
+
// This is a private function, though provides the capabilities to construct a proto.TransactionBody
|
88
|
+
//@ts-ignore
|
89
|
+
return transaction._makeTransactionBody(nodeAccountId);
|
90
|
+
}
|
91
|
+
export function transactionBodyToBase64String(transactionBody) {
|
92
|
+
return Uint8ArrayToBase64String(proto.TransactionBody.encode(transactionBody).finish());
|
93
|
+
}
|
94
|
+
/**
|
95
|
+
* @param transactionList - a proto.TransactionList object
|
96
|
+
* @returns `string`
|
97
|
+
* */
|
98
|
+
export function transactionListToBase64String(transactionList) {
|
99
|
+
const encoded = proto.TransactionList.encode(transactionList).finish();
|
100
|
+
return Uint8ArrayToBase64String(encoded);
|
101
|
+
}
|
102
|
+
/**
|
103
|
+
* Decodes base64 encoded proto.TransactionBody bytes to a `proto.TransactionBody` object.
|
104
|
+
*
|
105
|
+
* @param transactionBody - a base64 encoded string of proto.TransactionBody.encode().finish()
|
106
|
+
* @returns `Transaction`
|
107
|
+
*
|
108
|
+
* */
|
109
|
+
export function base64StringToTransactionBody(transactionBody) {
|
110
|
+
const bytes = Buffer.from(transactionBody, 'base64');
|
111
|
+
return proto.TransactionBody.decode(bytes);
|
112
|
+
}
|
113
|
+
/**
|
114
|
+
* Converts a `proto.SignatureMap` to a base64 encoded string.
|
115
|
+
*
|
116
|
+
* First converts the `proto.SignatureMap` object to a JSON.
|
117
|
+
* Then encodes the JSON to a base64 encoded string.
|
118
|
+
* @param signatureMap - The `proto.SignatureMap` object to be converted
|
119
|
+
* @returns Base64-encoded string representation of the input `proto.SignatureMap`
|
120
|
+
*/
|
121
|
+
export function signatureMapToBase64String(signatureMap) {
|
122
|
+
const encoded = proto.SignatureMap.encode(signatureMap).finish();
|
123
|
+
return Uint8ArrayToBase64String(encoded);
|
124
|
+
}
|
125
|
+
/**
|
126
|
+
* Converts a Base64-encoded string to a `proto.SignatureMap`.
|
127
|
+
* @param base64string - Base64-encoded string
|
128
|
+
* @returns `proto.SignatureMap`
|
129
|
+
*/
|
130
|
+
export function base64StringToSignatureMap(base64string) {
|
131
|
+
const encoded = Buffer.from(base64string, 'base64');
|
132
|
+
return proto.SignatureMap.decode(encoded);
|
133
|
+
}
|
134
|
+
/**
|
135
|
+
* Encodes the binary data represented by the `Uint8Array` to a Base64 string.
|
136
|
+
* @param binary - The `Uint8Array` containing binary data to be converted
|
137
|
+
* @returns Base64-encoded string representation of the input `Uint8Array`
|
138
|
+
*/
|
139
|
+
export function Uint8ArrayToBase64String(binary) {
|
140
|
+
return Buffer.from(binary).toString('base64');
|
141
|
+
}
|
142
|
+
/**
|
143
|
+
* Converts a Base64-encoded string to a `Uint8Array`.
|
144
|
+
* @param base64string - Base64-encoded string to be converted
|
145
|
+
* @returns A `Uint8Array` representing the decoded binary data
|
146
|
+
*/
|
147
|
+
export function base64StringToUint8Array(base64string) {
|
148
|
+
const encoded = Buffer.from(base64string, 'base64');
|
149
|
+
return new Uint8Array(encoded);
|
150
|
+
}
|
151
|
+
/**
|
152
|
+
* Converts a `Query` object to a Base64-encoded string.
|
153
|
+
* First utilizes the `toBytes` method of the `Query` instance to obtain its binary `Uint8Array` representation.
|
154
|
+
* Then encodes the binary `Uint8Array` to a Base64 string representation.
|
155
|
+
* @param query - A `Query` object to be converted
|
156
|
+
* @returns Base64 encoded representation of the input `Query` object
|
157
|
+
*/
|
158
|
+
export function queryToBase64String(query) {
|
159
|
+
const queryBytes = query.toBytes();
|
160
|
+
return Buffer.from(queryBytes).toString('base64');
|
161
|
+
}
|
162
|
+
/**
|
163
|
+
* Recreates a `Query` from a Base64-encoded string. First decodes the string to a buffer,
|
164
|
+
* then passes to `Query.fromBytes`. For greater flexibility, this function uses the base
|
165
|
+
* `Query` class, but takes an optional type parameter if the type of query is known,
|
166
|
+
* allowing stronger typeing.
|
167
|
+
* @param bytesString - Base64-encoded string
|
168
|
+
* @returns `Query<T>`
|
169
|
+
* @example
|
170
|
+
* ```ts
|
171
|
+
* const query1 = base64StringToQuery(bytesString)
|
172
|
+
* const query2 = base64StringToQuery<AccountInfoQuery>(bytesString)
|
173
|
+
* // query1 type: Query<any>
|
174
|
+
* // query2 type: AccountInfoQuery
|
175
|
+
* ```
|
176
|
+
*/
|
177
|
+
export function base64StringToQuery(bytesString) {
|
178
|
+
const decoded = Buffer.from(bytesString, 'base64');
|
179
|
+
return Query.fromBytes(decoded);
|
180
|
+
}
|
181
|
+
export function prefixMessageToSign(message) {
|
182
|
+
return '\x19Hedera Signed Message:\n' + message.length + message;
|
183
|
+
}
|
184
|
+
/**
|
185
|
+
* Incorporates additional data (salt) into the message to alter the output signature.
|
186
|
+
* This alteration ensures that passing a transaction here for signing will yield an invalid signature,
|
187
|
+
* as the additional data modifies the signature text.
|
188
|
+
*
|
189
|
+
* @param message - A plain text string
|
190
|
+
* @returns An array of Uint8Array containing the prepared message for signing
|
191
|
+
*/
|
192
|
+
export function stringToSignerMessage(message) {
|
193
|
+
return [Buffer.from(prefixMessageToSign(message))];
|
194
|
+
}
|
195
|
+
/**
|
196
|
+
* This implementation expects a plain text string, which is prefixed and then signed by a wallet.
|
197
|
+
* Because the spec calls for 1 message to be signed and 1 signer, this function expects a single
|
198
|
+
* signature and used the first item in the sigPair array.
|
199
|
+
*
|
200
|
+
* @param message - A plain text string
|
201
|
+
* @param base64SignatureMap - A base64 encoded proto.SignatureMap object
|
202
|
+
* @param publicKey - A PublicKey object use to verify the signature
|
203
|
+
* @returns boolean - whether or not the first signature in the sigPair is valid for the message and public key
|
204
|
+
*/
|
205
|
+
export function verifyMessageSignature(message, base64SignatureMap, publicKey) {
|
206
|
+
const signatureMap = base64StringToSignatureMap(base64SignatureMap);
|
207
|
+
const signature = signatureMap.sigPair[0].ed25519 || signatureMap.sigPair[0].ECDSASecp256k1;
|
208
|
+
if (!signature)
|
209
|
+
throw new Error('Signature not found in signature map');
|
210
|
+
return publicKey.verify(Buffer.from(prefixMessageToSign(message)), signature);
|
211
|
+
}
|
212
|
+
/**
|
213
|
+
* This implementation expects a plain text string, which is prefixed and then signed by a wallet.
|
214
|
+
* Because the spec calls for 1 message to be signed and 1 signer, this function expects a single
|
215
|
+
* signature and used the first item in the sigPair array.
|
216
|
+
*
|
217
|
+
* @param message - A plain text string
|
218
|
+
* @param signerSignature - A SignerSignature object
|
219
|
+
* @param publicKey - A PublicKey object use to verify the signature
|
220
|
+
* @returns boolean - whether or not the first signature in the sigPair is valid for the message and public key
|
221
|
+
*/
|
222
|
+
export function verifySignerSignature(message, signerSignature, publicKey) {
|
223
|
+
const signature = signerSignature.signature;
|
224
|
+
if (!signature)
|
225
|
+
throw new Error('Signature not found in signature map');
|
226
|
+
return publicKey.verify(Buffer.from(prefixMessageToSign(message)), signature);
|
227
|
+
}
|
228
|
+
/**
|
229
|
+
*
|
230
|
+
* https://github.com/hashgraph/hedera-sdk-js/blob/c78512b1d43eedf1d8bf2926a5b7ed3368fc39d1/src/PublicKey.js#L258
|
231
|
+
* a signature pair is a protobuf object with a signature and a public key, it is the responsibility of a dApp to ensure the public key matches the account id
|
232
|
+
* @param signerSignatures - An array of `SignerSignature` objects
|
233
|
+
* @returns `proto.SignatureMap` object
|
234
|
+
*/
|
235
|
+
export function signerSignaturesToSignatureMap(signerSignatures) {
|
236
|
+
const signatureMap = proto.SignatureMap.create({
|
237
|
+
sigPair: signerSignatures.map((s) => s.publicKey._toProtobufSignature(s.signature)),
|
238
|
+
});
|
239
|
+
return signatureMap;
|
240
|
+
}
|
241
|
+
/**
|
242
|
+
* A mapping of `LedgerId` to EIP chain id and CAIP-2 network name.
|
243
|
+
*
|
244
|
+
* Structure: [`LedgerId`, `number` (EIP155 chain id), `string` (CAIP-2 chain id)][]
|
245
|
+
*
|
246
|
+
* @see {@link https://namespaces.chainagnostic.org/hedera/README | Hedera Namespaces}
|
247
|
+
* @see {@link https://hips.hedera.com/hip/hip-30 | CAIP Identifiers for the Hedera Network (HIP-30)}
|
248
|
+
*/
|
249
|
+
export const LEDGER_ID_MAPPINGS = [
|
250
|
+
[LedgerId.MAINNET, 295, 'hedera:mainnet'],
|
251
|
+
[LedgerId.TESTNET, 296, 'hedera:testnet'],
|
252
|
+
[LedgerId.PREVIEWNET, 297, 'hedera:previewnet'],
|
253
|
+
[LedgerId.LOCAL_NODE, 298, 'hedera:devnet'],
|
254
|
+
];
|
255
|
+
const DEFAULT_LEDGER_ID = LedgerId.LOCAL_NODE;
|
256
|
+
const DEFAULT_EIP = LEDGER_ID_MAPPINGS[3][1];
|
257
|
+
const DEFAULT_CAIP = LEDGER_ID_MAPPINGS[3][2];
|
258
|
+
/**
|
259
|
+
* Converts an EIP chain id to a LedgerId object.
|
260
|
+
*
|
261
|
+
* If no mapping is found, returns `LedgerId.LOCAL_NODE`.
|
262
|
+
*
|
263
|
+
* @param chainId - The EIP chain ID (number) to be converted
|
264
|
+
* @returns A `LedgerId` corresponding to the provided chain ID
|
265
|
+
* @example
|
266
|
+
* ```ts
|
267
|
+
* const localnodeLedgerId = EIPChainIdToLedgerId(298)
|
268
|
+
* console.log(localnodeLedgerId) // LedgerId.LOCAL_NODE
|
269
|
+
* const mainnetLedgerId = EIPChainIdToLedgerId(295)
|
270
|
+
* console.log(mainnetLedgerId) // LedgerId.MAINNET
|
271
|
+
* ```
|
272
|
+
*/
|
273
|
+
export function EIPChainIdToLedgerId(chainId) {
|
274
|
+
for (let i = 0; i < LEDGER_ID_MAPPINGS.length; i++) {
|
275
|
+
const [ledgerId, chainId_] = LEDGER_ID_MAPPINGS[i];
|
276
|
+
if (chainId === chainId_) {
|
277
|
+
return ledgerId;
|
278
|
+
}
|
279
|
+
}
|
280
|
+
return DEFAULT_LEDGER_ID;
|
281
|
+
}
|
282
|
+
/**
|
283
|
+
* Converts a LedgerId object to an EIP chain id.
|
284
|
+
*
|
285
|
+
* If no mapping is found, returns the EIP chain id for `LedgerId.LOCAL_NODE`.
|
286
|
+
*
|
287
|
+
* @param ledgerId - The `LedgerId` object to be converted
|
288
|
+
* @returns A `number` representing the EIP chain id for the provided `LedgerId`
|
289
|
+
* @example
|
290
|
+
* ```ts
|
291
|
+
* const previewnetChainId = ledgerIdToEIPChainId(LedgerId.PREVIEWNET)
|
292
|
+
* console.log(previewnetChainId) // 297
|
293
|
+
* const testnetChainId = ledgerIdToEIPChainId(LedgerId.TESTNET)
|
294
|
+
* console.log(testnetChainId) // 296
|
295
|
+
* ```
|
296
|
+
*/
|
297
|
+
export function ledgerIdToEIPChainId(ledgerId) {
|
298
|
+
for (let i = 0; i < LEDGER_ID_MAPPINGS.length; i++) {
|
299
|
+
const [ledgerId_, chainId] = LEDGER_ID_MAPPINGS[i];
|
300
|
+
if (ledgerId === ledgerId_) {
|
301
|
+
return chainId;
|
302
|
+
}
|
303
|
+
}
|
304
|
+
return DEFAULT_EIP;
|
305
|
+
}
|
306
|
+
/**
|
307
|
+
* Converts a network name to an EIP chain id.
|
308
|
+
* If no mapping is found, returns the EIP chain id for `LedgerId.LOCAL_NODE`.
|
309
|
+
*
|
310
|
+
* @param networkName - The network name (string) to be converted
|
311
|
+
* @returns A `number` representing the EIP chain id for the provided network name
|
312
|
+
* @example
|
313
|
+
* ```ts
|
314
|
+
* const mainnetChainId = networkNameToEIPChainId('mainnet')
|
315
|
+
* console.log(mainnetChainId) // 295
|
316
|
+
* const testnetChainId = networkNameToEIPChainId('testnet')
|
317
|
+
* console.log(mainnetChainId) // 296
|
318
|
+
* ```
|
319
|
+
*/
|
320
|
+
export function networkNameToEIPChainId(networkName) {
|
321
|
+
const ledgerId = LedgerId.fromString(networkName.toLowerCase());
|
322
|
+
return ledgerIdToEIPChainId(ledgerId);
|
323
|
+
}
|
324
|
+
/**
|
325
|
+
* Converts a CAIP chain id to a LedgerId object.
|
326
|
+
*
|
327
|
+
* If no mapping is found, returns `LedgerId.LOCAL_NODE`.
|
328
|
+
*
|
329
|
+
* @param chainId - The CAIP chain ID (string) to be converted
|
330
|
+
* @returns A `LedgerId` corresponding to the provided CAIP chain ID
|
331
|
+
* @example
|
332
|
+
* ```ts
|
333
|
+
* const previewnetLedgerId = CAIPChainIdToLedgerId(HederaChainId.Previewnet)
|
334
|
+
* console.log(previewnetLedgerId) // LedgerId.PREVIEWNET
|
335
|
+
* const testnetLedgerId = CAIPChainIdToLedgerId(HederaChainId.Testnet)
|
336
|
+
* console.log(testnetLedgerId) // LedgerId.TESTNET
|
337
|
+
* ```
|
338
|
+
*/
|
339
|
+
export function CAIPChainIdToLedgerId(chainId) {
|
340
|
+
for (let i = 0; i < LEDGER_ID_MAPPINGS.length; i++) {
|
341
|
+
const [ledgerId, _, chainId_] = LEDGER_ID_MAPPINGS[i];
|
342
|
+
if (chainId === chainId_) {
|
343
|
+
return ledgerId;
|
344
|
+
}
|
345
|
+
}
|
346
|
+
return DEFAULT_LEDGER_ID;
|
347
|
+
}
|
348
|
+
/**
|
349
|
+
* Converts a LedgerId object to a CAIP chain id.
|
350
|
+
*
|
351
|
+
* If no mapping is found, returns the CAIP chain id for `LedgerId.LOCAL_NODE`.
|
352
|
+
*
|
353
|
+
* @param ledgerId - The `LedgerId` object to be converted
|
354
|
+
* @returns A `string` representing the CAIP chain id for the provided `LedgerId`
|
355
|
+
* @example
|
356
|
+
* ```ts
|
357
|
+
* const mainnetChainId = ledgerIdToCAIPChainId(HederaChainId.Mainnet)
|
358
|
+
* console.log(mainnetChainId) // LedgerId.PREVIEWNET
|
359
|
+
* const testnetChainId = ledgerIdToCAIPChainId(HederaChainId.Testnet)
|
360
|
+
* console.log(testnetChainId) // LedgerId.TESTNET
|
361
|
+
* ```
|
362
|
+
*/
|
363
|
+
export function ledgerIdToCAIPChainId(ledgerId) {
|
364
|
+
for (let i = 0; i < LEDGER_ID_MAPPINGS.length; i++) {
|
365
|
+
const [ledgerId_, _, chainId] = LEDGER_ID_MAPPINGS[i];
|
366
|
+
if (ledgerId.toString() === ledgerId_.toString()) {
|
367
|
+
return chainId;
|
368
|
+
}
|
369
|
+
}
|
370
|
+
return DEFAULT_CAIP;
|
371
|
+
}
|
372
|
+
/**
|
373
|
+
* Converts a network name to a CAIP chain id.
|
374
|
+
*
|
375
|
+
* If no mapping is found, returns the CAIP chain id for `LedgerId.LOCAL_NODE`.
|
376
|
+
*
|
377
|
+
* @param networkName - The network name (string) to be converted
|
378
|
+
* @returns A `string` representing the CAIP chain id for the provided network name
|
379
|
+
* @example
|
380
|
+
* ```ts
|
381
|
+
* const previewnetChainId = networkNameToCAIPChainId('previewnet')
|
382
|
+
* console.log(previewnetChainId) // HederaChainId.Previewnet
|
383
|
+
* const devnetChainId = networkNameToCAIPChainId('devnet')
|
384
|
+
* console.log(devnetChainId) // HederaChainId.Devnet
|
385
|
+
* ```
|
386
|
+
*/
|
387
|
+
export function networkNameToCAIPChainId(networkName) {
|
388
|
+
const ledgerId = LedgerId.fromString(networkName.toLowerCase());
|
389
|
+
const chainId = ledgerIdToCAIPChainId(ledgerId);
|
390
|
+
return chainId;
|
391
|
+
}
|
392
|
+
/**
|
393
|
+
* Create a `ProposalTypes.RequiredNamespaces` object for a given ledgerId.
|
394
|
+
*
|
395
|
+
* @param ledgerId - The `LedgerId` for which the namespaces are created
|
396
|
+
* @param methods - An array of strings representing methods
|
397
|
+
* @param events - An array of strings representing events
|
398
|
+
* @returns A `ProposalTypes.RequiredNamespaces` object
|
399
|
+
*/
|
400
|
+
export const networkNamespaces = (ledgerId, methods, events) => ({
|
401
|
+
hedera: {
|
402
|
+
chains: [ledgerIdToCAIPChainId(ledgerId)],
|
403
|
+
methods,
|
404
|
+
events,
|
405
|
+
},
|
406
|
+
});
|
407
|
+
/**
|
408
|
+
* Get the account and ledger from a `SessionTypes.Struct` object.
|
409
|
+
*
|
410
|
+
* @param session - The `SessionTypes.Struct` object containing namespaces
|
411
|
+
* @returns `ProposalTypes.RequiredNamespaces` - an array of objects containing network (LedgerId) and account (AccountId)
|
412
|
+
*/
|
413
|
+
export const accountAndLedgerFromSession = (session) => {
|
414
|
+
const hederaNamespace = session.namespaces.hedera;
|
415
|
+
if (!hederaNamespace)
|
416
|
+
throw new Error('No hedera namespace found');
|
417
|
+
return hederaNamespace.accounts.map((account) => {
|
418
|
+
const [chain, network, acc] = account.split(':');
|
419
|
+
return {
|
420
|
+
network: CAIPChainIdToLedgerId(chain + ':' + network),
|
421
|
+
account: AccountId.fromString(acc),
|
422
|
+
};
|
423
|
+
});
|
424
|
+
};
|