starknet 6.9.0 → 6.10.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +12 -0
- package/LICENSE +1 -1
- package/README.md +5 -9
- package/dist/account/default.cjs +655 -0
- package/dist/account/default.d.ts +67 -0
- package/dist/account/default.mjs +652 -0
- package/dist/account/index.cjs +27 -0
- package/dist/account/index.d.ts +2 -0
- package/dist/account/index.mjs +2 -0
- package/dist/account/interface.cjs +9 -0
- package/dist/account/interface.d.ts +301 -0
- package/dist/account/interface.mjs +3 -0
- package/dist/channel/index.cjs +27 -0
- package/dist/channel/index.d.ts +3 -0
- package/dist/channel/index.mjs +3 -0
- package/dist/channel/rpc_0_6.cjs +607 -0
- package/dist/channel/rpc_0_6.d.ts +78 -0
- package/dist/channel/rpc_0_6.mjs +543 -0
- package/dist/channel/rpc_0_7.cjs +619 -0
- package/dist/channel/rpc_0_7.d.ts +81 -0
- package/dist/channel/rpc_0_7.mjs +561 -0
- package/dist/constants.cjs +76 -0
- package/dist/constants.d.ts +70 -0
- package/dist/constants.mjs +60 -0
- package/dist/contract/contractFactory.cjs +93 -0
- package/dist/contract/contractFactory.d.ts +46 -0
- package/dist/contract/contractFactory.mjs +83 -0
- package/dist/contract/default.cjs +269 -0
- package/dist/contract/default.d.ts +58 -0
- package/dist/contract/default.mjs +254 -0
- package/dist/contract/index.cjs +38 -0
- package/dist/contract/index.d.ts +3 -0
- package/dist/contract/index.mjs +3 -0
- package/dist/contract/interface.cjs +13 -0
- package/dist/contract/interface.d.ts +128 -0
- package/dist/contract/interface.mjs +6 -0
- package/dist/index.cjs +267 -0
- package/dist/index.d.ts +45 -5991
- package/dist/index.global.js +728 -609
- package/dist/index.global.js.map +1 -1
- package/dist/index.mjs +39 -7320
- package/dist/provider/errors.cjs +50 -0
- package/dist/provider/errors.d.ts +16 -0
- package/dist/provider/errors.mjs +35 -0
- package/dist/provider/extensions/default.cjs +11 -0
- package/dist/provider/extensions/default.d.ts +6 -0
- package/dist/provider/extensions/default.mjs +5 -0
- package/dist/provider/extensions/starknetId.cjs +174 -0
- package/dist/provider/extensions/starknetId.d.ts +10 -0
- package/dist/provider/extensions/starknetId.mjs +230 -0
- package/dist/provider/index.cjs +56 -0
- package/dist/provider/index.d.ts +6 -0
- package/dist/provider/index.mjs +6 -0
- package/dist/provider/interface.cjs +8 -0
- package/dist/provider/interface.d.ts +261 -0
- package/dist/provider/interface.mjs +2 -0
- package/dist/provider/rpc.cjs +301 -0
- package/dist/provider/rpc.d.ts +115 -0
- package/dist/provider/rpc.mjs +302 -0
- package/dist/service/types.cjs +1 -0
- package/dist/service/types.d.ts +5 -0
- package/dist/service/types.mjs +0 -0
- package/dist/signer/default.cjs +105 -0
- package/dist/signer/default.d.ts +12 -0
- package/dist/signer/default.mjs +102 -0
- package/dist/signer/ethSigner.cjs +123 -0
- package/dist/signer/ethSigner.d.ts +25 -0
- package/dist/signer/ethSigner.mjs +140 -0
- package/dist/signer/index.cjs +38 -0
- package/dist/signer/index.d.ts +3 -0
- package/dist/signer/index.mjs +3 -0
- package/dist/signer/interface.cjs +8 -0
- package/dist/signer/interface.d.ts +115 -0
- package/dist/signer/interface.mjs +2 -0
- package/dist/types/account.cjs +1 -0
- package/dist/types/account.d.ts +79 -0
- package/dist/types/account.mjs +0 -0
- package/dist/types/api/index.cjs +30 -0
- package/dist/types/api/index.d.ts +4 -0
- package/dist/types/api/index.mjs +4 -0
- package/dist/types/api/jsonrpc/index.cjs +1 -0
- package/dist/types/api/jsonrpc/index.d.ts +21 -0
- package/dist/types/api/jsonrpc/index.mjs +0 -0
- package/dist/types/api/rpcspec_0_6/components.cjs +1 -0
- package/dist/types/api/rpcspec_0_6/components.d.ts +541 -0
- package/dist/types/api/rpcspec_0_6/components.mjs +0 -0
- package/dist/types/api/rpcspec_0_6/contract.cjs +1 -0
- package/dist/types/api/rpcspec_0_6/contract.d.ts +85 -0
- package/dist/types/api/rpcspec_0_6/contract.mjs +0 -0
- package/dist/types/api/rpcspec_0_6/errors.cjs +1 -0
- package/dist/types/api/rpcspec_0_6/errors.d.ts +128 -0
- package/dist/types/api/rpcspec_0_6/errors.mjs +0 -0
- package/dist/types/api/rpcspec_0_6/index.cjs +45 -0
- package/dist/types/api/rpcspec_0_6/index.d.ts +8 -0
- package/dist/types/api/rpcspec_0_6/index.mjs +5 -0
- package/dist/types/api/rpcspec_0_6/methods.cjs +1 -0
- package/dist/types/api/rpcspec_0_6/methods.d.ts +207 -0
- package/dist/types/api/rpcspec_0_6/methods.mjs +0 -0
- package/dist/types/api/rpcspec_0_6/nonspec.cjs +65 -0
- package/dist/types/api/rpcspec_0_6/nonspec.d.ts +147 -0
- package/dist/types/api/rpcspec_0_6/nonspec.mjs +59 -0
- package/dist/types/cairoEnum.cjs +1 -0
- package/dist/types/cairoEnum.d.ts +2 -0
- package/dist/types/cairoEnum.mjs +0 -0
- package/dist/types/calldata.cjs +27 -0
- package/dist/types/calldata.d.ts +23 -0
- package/dist/types/calldata.mjs +21 -0
- package/dist/types/contract.cjs +1 -0
- package/dist/types/contract.d.ts +27 -0
- package/dist/types/contract.mjs +0 -0
- package/dist/types/helpers/valuesType.cjs +1 -0
- package/dist/types/helpers/valuesType.d.ts +1 -0
- package/dist/types/helpers/valuesType.mjs +0 -0
- package/dist/types/index.cjs +121 -0
- package/dist/types/index.d.ts +10 -0
- package/dist/types/index.mjs +10 -0
- package/dist/types/lib/contract/abi.cjs +1 -0
- package/dist/types/lib/contract/abi.d.ts +71 -0
- package/dist/types/lib/contract/abi.mjs +0 -0
- package/dist/types/lib/contract/index.cjs +50 -0
- package/dist/types/lib/contract/index.d.ts +26 -0
- package/dist/types/lib/contract/index.mjs +8 -0
- package/dist/types/lib/contract/legacy.cjs +1 -0
- package/dist/types/lib/contract/legacy.d.ts +33 -0
- package/dist/types/lib/contract/legacy.mjs +0 -0
- package/dist/types/lib/contract/sierra.cjs +1 -0
- package/dist/types/lib/contract/sierra.d.ts +52 -0
- package/dist/types/lib/contract/sierra.mjs +0 -0
- package/dist/types/lib/index.cjs +61 -0
- package/dist/types/lib/index.d.ts +259 -0
- package/dist/types/lib/index.mjs +36 -0
- package/dist/types/provider/configuration.cjs +1 -0
- package/dist/types/provider/configuration.d.ts +20 -0
- package/dist/types/provider/configuration.mjs +0 -0
- package/dist/types/provider/index.cjs +27 -0
- package/dist/types/provider/index.d.ts +2 -0
- package/dist/types/provider/index.mjs +2 -0
- package/dist/types/provider/response.cjs +30 -0
- package/dist/types/provider/response.d.ts +98 -0
- package/dist/types/provider/response.mjs +1 -0
- package/dist/types/provider/spec.cjs +1 -0
- package/dist/types/provider/spec.d.ts +49 -0
- package/dist/types/provider/spec.mjs +0 -0
- package/dist/types/signer.cjs +1 -0
- package/dist/types/signer.d.ts +49 -0
- package/dist/types/signer.mjs +0 -0
- package/dist/types/transactionReceipt.cjs +1 -0
- package/dist/types/transactionReceipt.d.ts +23 -0
- package/dist/types/transactionReceipt.mjs +0 -0
- package/dist/types/typedData.cjs +12 -0
- package/dist/types/typedData.d.ts +1 -0
- package/dist/types/typedData.mjs +3 -0
- package/dist/utils/address.cjs +44 -0
- package/dist/utils/address.d.ts +55 -0
- package/dist/utils/address.mjs +35 -0
- package/dist/utils/assert.cjs +11 -0
- package/dist/utils/assert.d.ts +12 -0
- package/dist/utils/assert.mjs +5 -0
- package/dist/utils/cairoDataTypes/felt.cjs +31 -0
- package/dist/utils/cairoDataTypes/felt.d.ts +6 -0
- package/dist/utils/cairoDataTypes/felt.mjs +27 -0
- package/dist/utils/cairoDataTypes/uint256.cjs +113 -0
- package/dist/utils/cairoDataTypes/uint256.d.ts +72 -0
- package/dist/utils/cairoDataTypes/uint256.mjs +103 -0
- package/dist/utils/cairoDataTypes/uint512.cjs +125 -0
- package/dist/utils/cairoDataTypes/uint512.d.ts +76 -0
- package/dist/utils/cairoDataTypes/uint512.mjs +123 -0
- package/dist/utils/calldata/byteArray.cjs +27 -0
- package/dist/utils/calldata/byteArray.d.ts +32 -0
- package/dist/utils/calldata/byteArray.mjs +20 -0
- package/dist/utils/calldata/cairo.cjs +114 -0
- package/dist/utils/calldata/cairo.d.ts +196 -0
- package/dist/utils/calldata/cairo.mjs +68 -0
- package/dist/utils/calldata/enum/CairoCustomEnum.cjs +55 -0
- package/dist/utils/calldata/enum/CairoCustomEnum.d.ts +38 -0
- package/dist/utils/calldata/enum/CairoCustomEnum.mjs +50 -0
- package/dist/utils/calldata/enum/CairoOption.cjs +55 -0
- package/dist/utils/calldata/enum/CairoOption.d.ts +37 -0
- package/dist/utils/calldata/enum/CairoOption.mjs +50 -0
- package/dist/utils/calldata/enum/CairoResult.cjs +54 -0
- package/dist/utils/calldata/enum/CairoResult.d.ts +36 -0
- package/dist/utils/calldata/enum/CairoResult.mjs +47 -0
- package/dist/utils/calldata/enum/index.cjs +38 -0
- package/dist/utils/calldata/enum/index.d.ts +3 -0
- package/dist/utils/calldata/enum/index.mjs +3 -0
- package/dist/utils/calldata/formatter.cjs +55 -0
- package/dist/utils/calldata/formatter.d.ts +9 -0
- package/dist/utils/calldata/formatter.mjs +58 -0
- package/dist/utils/calldata/index.cjs +272 -0
- package/dist/utils/calldata/index.d.ts +90 -0
- package/dist/utils/calldata/index.mjs +276 -0
- package/dist/utils/calldata/parser/index.cjs +29 -0
- package/dist/utils/calldata/parser/index.d.ts +5 -0
- package/dist/utils/calldata/parser/index.mjs +21 -0
- package/dist/utils/calldata/parser/interface.cjs +8 -0
- package/dist/utils/calldata/parser/interface.d.ts +20 -0
- package/dist/utils/calldata/parser/interface.mjs +2 -0
- package/dist/utils/calldata/parser/parser-0-1.1.0.cjs +38 -0
- package/dist/utils/calldata/parser/parser-0-1.1.0.d.ts +24 -0
- package/dist/utils/calldata/parser/parser-0-1.1.0.mjs +31 -0
- package/dist/utils/calldata/parser/parser-2.0.0.cjs +42 -0
- package/dist/utils/calldata/parser/parser-2.0.0.d.ts +23 -0
- package/dist/utils/calldata/parser/parser-2.0.0.mjs +37 -0
- package/dist/utils/calldata/propertyOrder.cjs +159 -0
- package/dist/utils/calldata/propertyOrder.d.ts +2 -0
- package/dist/utils/calldata/propertyOrder.mjs +189 -0
- package/dist/utils/calldata/requestParser.cjs +204 -0
- package/dist/utils/calldata/requestParser.d.ts +11 -0
- package/dist/utils/calldata/requestParser.mjs +231 -0
- package/dist/utils/calldata/responseParser.cjs +200 -0
- package/dist/utils/calldata/responseParser.d.ts +11 -0
- package/dist/utils/calldata/responseParser.mjs +201 -0
- package/dist/utils/calldata/tuple.cjs +106 -0
- package/dist/utils/calldata/tuple.d.ts +6 -0
- package/dist/utils/calldata/tuple.mjs +88 -0
- package/dist/utils/calldata/validate.cjs +241 -0
- package/dist/utils/calldata/validate.d.ts +6 -0
- package/dist/utils/calldata/validate.mjs +315 -0
- package/dist/utils/contract.cjs +40 -0
- package/dist/utils/contract.d.ts +37 -0
- package/dist/utils/contract.mjs +31 -0
- package/dist/utils/ec.cjs +12 -0
- package/dist/utils/ec.d.ts +2 -0
- package/dist/utils/ec.mjs +2 -0
- package/dist/utils/encode.cjs +78 -0
- package/dist/utils/encode.d.ts +207 -0
- package/dist/utils/encode.mjs +56 -0
- package/dist/utils/eth.cjs +22 -0
- package/dist/utils/eth.d.ts +22 -0
- package/dist/utils/eth.mjs +14 -0
- package/dist/utils/events/index.cjs +144 -0
- package/dist/utils/events/index.d.ts +84 -0
- package/dist/utils/events/index.mjs +136 -0
- package/dist/utils/fetchPonyfill.cjs +14 -0
- package/dist/utils/fetchPonyfill.d.ts +2 -0
- package/dist/utils/fetchPonyfill.mjs +5 -0
- package/dist/utils/hash/classHash.cjs +151 -0
- package/dist/utils/hash/classHash.d.ts +120 -0
- package/dist/utils/hash/classHash.mjs +168 -0
- package/dist/utils/hash/index.cjs +49 -0
- package/dist/utils/hash/index.d.ts +7 -0
- package/dist/utils/hash/index.mjs +4 -0
- package/dist/utils/hash/selector.cjs +37 -0
- package/dist/utils/hash/selector.d.ts +57 -0
- package/dist/utils/hash/selector.mjs +28 -0
- package/dist/utils/hash/transactionHash/index.cjs +38 -0
- package/dist/utils/hash/transactionHash/index.d.ts +81 -0
- package/dist/utils/hash/transactionHash/index.mjs +102 -0
- package/dist/utils/hash/transactionHash/v2.cjs +31 -0
- package/dist/utils/hash/transactionHash/v2.d.ts +34 -0
- package/dist/utils/hash/transactionHash/v2.mjs +57 -0
- package/dist/utils/hash/transactionHash/v3.cjs +45 -0
- package/dist/utils/hash/transactionHash/v3.d.ts +24 -0
- package/dist/utils/hash/transactionHash/v3.mjs +80 -0
- package/dist/utils/json.cjs +21 -0
- package/dist/utils/json.d.ts +50 -0
- package/dist/utils/json.mjs +10 -0
- package/dist/utils/merkle.cjs +118 -0
- package/dist/utils/merkle.d.ts +87 -0
- package/dist/utils/merkle.mjs +114 -0
- package/dist/utils/num.cjs +109 -0
- package/dist/utils/num.d.ts +283 -0
- package/dist/utils/num.mjs +86 -0
- package/dist/utils/provider.cjs +182 -0
- package/dist/utils/provider.d.ts +217 -0
- package/dist/utils/provider.mjs +158 -0
- package/dist/utils/responseParser/index.cjs +27 -0
- package/dist/utils/responseParser/index.d.ts +2 -0
- package/dist/utils/responseParser/index.mjs +2 -0
- package/dist/utils/responseParser/interface.cjs +8 -0
- package/dist/utils/responseParser/interface.d.ts +13 -0
- package/dist/utils/responseParser/interface.mjs +2 -0
- package/dist/utils/responseParser/rpc.cjs +83 -0
- package/dist/utils/responseParser/rpc.d.ts +19 -0
- package/dist/utils/responseParser/rpc.mjs +77 -0
- package/dist/utils/shortString.cjs +56 -0
- package/dist/utils/shortString.d.ts +116 -0
- package/dist/utils/shortString.mjs +39 -0
- package/dist/utils/stark.cjs +137 -0
- package/dist/utils/stark.d.ts +224 -0
- package/dist/utils/stark.mjs +109 -0
- package/dist/utils/starknetId.cjs +196 -0
- package/dist/utils/starknetId.d.ts +182 -0
- package/dist/utils/starknetId.mjs +185 -0
- package/dist/utils/transaction.cjs +115 -0
- package/dist/utils/transaction.d.ts +185 -0
- package/dist/utils/transaction.mjs +98 -0
- package/dist/utils/transactionReceipt.cjs +54 -0
- package/dist/utils/transactionReceipt.d.ts +33 -0
- package/dist/utils/transactionReceipt.mjs +49 -0
- package/dist/utils/typedData.cjs +285 -0
- package/dist/utils/typedData.d.ts +54 -0
- package/dist/utils/typedData.mjs +286 -0
- package/dist/utils/uint256.cjs +30 -0
- package/dist/utils/uint256.d.ts +45 -0
- package/dist/utils/uint256.mjs +11 -0
- package/dist/utils/url.cjs +35 -0
- package/dist/utils/url.d.ts +29 -0
- package/dist/utils/url.mjs +27 -0
- package/dist/wallet/account.cjs +115 -0
- package/dist/wallet/account.d.ts +31 -0
- package/dist/wallet/account.mjs +112 -0
- package/dist/wallet/connect.cjs +90 -0
- package/dist/wallet/connect.d.ts +82 -0
- package/dist/wallet/connect.mjs +64 -0
- package/dist/wallet/index.cjs +16 -0
- package/dist/wallet/index.d.ts +1 -0
- package/dist/wallet/index.mjs +1 -0
- package/dist/wallet/types.cjs +1 -0
- package/dist/wallet/types.d.ts +4 -0
- package/dist/wallet/types.mjs +0 -0
- package/package.json +13 -13
- package/dist/index.js +0 -7426
- package/dist/index.js.map +0 -1
- package/dist/index.mjs.map +0 -1
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.MerkleTree = void 0;
|
|
7
|
+
exports.proofMerklePath = proofMerklePath;
|
|
8
|
+
var _hash = require("./hash/index.cjs");
|
|
9
|
+
class MerkleTree {
|
|
10
|
+
leaves;
|
|
11
|
+
branches = [];
|
|
12
|
+
root;
|
|
13
|
+
hashMethod;
|
|
14
|
+
/**
|
|
15
|
+
* Create a Merkle tree
|
|
16
|
+
*
|
|
17
|
+
* @param leafHashes hex-string array
|
|
18
|
+
* @param hashMethod hash method to use, default: Pedersen
|
|
19
|
+
* @returns created Merkle tree
|
|
20
|
+
* @example
|
|
21
|
+
* ```typescript
|
|
22
|
+
* const leaves = ['0x1', '0x2', '0x3', '0x4', '0x5', '0x6', '0x7'];
|
|
23
|
+
* const tree = new MerkleTree(leaves);
|
|
24
|
+
* // tree = {
|
|
25
|
+
* // branches: [['0x5bb9440e2...', '0x262697b88...', ...], ['0x38118a340...', ...], ...],
|
|
26
|
+
* // leaves: ['0x1', '0x2', '0x3', '0x4', '0x5', '0x6', '0x7'],
|
|
27
|
+
* // root: '0x7f748c75e5bdb7ae28013f076b8ab650c4e01d3530c6e5ab665f9f1accbe7d4',
|
|
28
|
+
* // hashMethod: [Function computePedersenHash],
|
|
29
|
+
* // }
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
constructor(leafHashes, hashMethod = _hash.computePedersenHash) {
|
|
33
|
+
this.hashMethod = hashMethod;
|
|
34
|
+
this.leaves = leafHashes;
|
|
35
|
+
this.root = this.build(leafHashes);
|
|
36
|
+
}
|
|
37
|
+
/** @ignore */
|
|
38
|
+
build(leaves) {
|
|
39
|
+
if (leaves.length === 1) {
|
|
40
|
+
return leaves[0];
|
|
41
|
+
}
|
|
42
|
+
if (leaves.length !== this.leaves.length) {
|
|
43
|
+
this.branches.push(leaves);
|
|
44
|
+
}
|
|
45
|
+
const newLeaves = [];
|
|
46
|
+
for (let i = 0; i < leaves.length; i += 2) {
|
|
47
|
+
if (i + 1 === leaves.length) {
|
|
48
|
+
newLeaves.push(MerkleTree.hash(leaves[i], "0x0", this.hashMethod));
|
|
49
|
+
} else {
|
|
50
|
+
newLeaves.push(MerkleTree.hash(leaves[i], leaves[i + 1], this.hashMethod));
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return this.build(newLeaves);
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Calculate hash from ordered a and b, Pedersen hash default
|
|
57
|
+
*
|
|
58
|
+
* @param a first value
|
|
59
|
+
* @param b second value
|
|
60
|
+
* @param hashMethod hash method to use, default: Pedersen
|
|
61
|
+
* @returns result of the hash function
|
|
62
|
+
* @example
|
|
63
|
+
* ```typescript
|
|
64
|
+
* const result1 = MerkleTree.hash('0xabc', '0xdef');
|
|
65
|
+
* // result1 = '0x484f029da7914ada038b1adf67fc83632364a3ebc2cd9349b41ab61626d9e82'
|
|
66
|
+
*
|
|
67
|
+
* const customHashMethod = (a, b) => `custom_${a}_${b}`;
|
|
68
|
+
* const result2 = MerkleTree.hash('0xabc', '0xdef', customHashMethod);
|
|
69
|
+
* // result2 = 'custom_2748_3567'
|
|
70
|
+
* ```
|
|
71
|
+
*/
|
|
72
|
+
static hash(a, b, hashMethod = _hash.computePedersenHash) {
|
|
73
|
+
const [aSorted, bSorted] = [BigInt(a), BigInt(b)].sort((x, y) => x >= y ? 1 : -1);
|
|
74
|
+
return hashMethod(aSorted, bSorted);
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Calculates the merkle membership proof path
|
|
78
|
+
*
|
|
79
|
+
* @param leaf hex-string
|
|
80
|
+
* @param branch hex-string array
|
|
81
|
+
* @param hashPath hex-string array
|
|
82
|
+
* @returns collection of merkle proof hex-string hashes
|
|
83
|
+
* @example
|
|
84
|
+
* ```typescript
|
|
85
|
+
* const leaves = ['0x1', '0x2', '0x3', '0x4', '0x5', '0x6', '0x7'];
|
|
86
|
+
* const tree = new MerkleTree(leaves);
|
|
87
|
+
* const result = tree.getProof('0x3');
|
|
88
|
+
* // result = [
|
|
89
|
+
* // '0x4',
|
|
90
|
+
* // '0x5bb9440e27889a364bcb678b1f679ecd1347acdedcbf36e83494f857cc58026',
|
|
91
|
+
* // '0x8c0e46dd2df9aaf3a8ebfbc25408a582ad7fa7171f0698ddbbc5130b4b4e60',
|
|
92
|
+
* // ]
|
|
93
|
+
* ```
|
|
94
|
+
*/
|
|
95
|
+
getProof(leaf, branch = this.leaves, hashPath = []) {
|
|
96
|
+
const index = branch.indexOf(leaf);
|
|
97
|
+
if (index === -1) {
|
|
98
|
+
throw new Error("leaf not found");
|
|
99
|
+
}
|
|
100
|
+
if (branch.length === 1) {
|
|
101
|
+
return hashPath;
|
|
102
|
+
}
|
|
103
|
+
const isLeft = index % 2 === 0;
|
|
104
|
+
const neededBranch = (isLeft ? branch[index + 1] : branch[index - 1]) ?? "0x0";
|
|
105
|
+
const newHashPath = [...hashPath, neededBranch];
|
|
106
|
+
const currentBranchLevelIndex = this.leaves.length === branch.length ? -1 : this.branches.findIndex(b => b.length === branch.length);
|
|
107
|
+
const nextBranch = this.branches[currentBranchLevelIndex + 1] ?? [this.root];
|
|
108
|
+
return this.getProof(MerkleTree.hash(isLeft ? leaf : neededBranch, isLeft ? neededBranch : leaf, this.hashMethod), nextBranch, newHashPath);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
exports.MerkleTree = MerkleTree;
|
|
112
|
+
function proofMerklePath(root, leaf, path, hashMethod = _hash.computePedersenHash) {
|
|
113
|
+
if (path.length === 0) {
|
|
114
|
+
return root === leaf;
|
|
115
|
+
}
|
|
116
|
+
const [next, ...rest] = path;
|
|
117
|
+
return proofMerklePath(root, MerkleTree.hash(leaf, next, hashMethod), rest, hashMethod);
|
|
118
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { BigNumberish } from '../types';
|
|
2
|
+
export declare class MerkleTree {
|
|
3
|
+
leaves: string[];
|
|
4
|
+
branches: string[][];
|
|
5
|
+
root: string;
|
|
6
|
+
hashMethod: (a: BigNumberish, b: BigNumberish) => string;
|
|
7
|
+
/**
|
|
8
|
+
* Create a Merkle tree
|
|
9
|
+
*
|
|
10
|
+
* @param leafHashes hex-string array
|
|
11
|
+
* @param hashMethod hash method to use, default: Pedersen
|
|
12
|
+
* @returns created Merkle tree
|
|
13
|
+
* @example
|
|
14
|
+
* ```typescript
|
|
15
|
+
* const leaves = ['0x1', '0x2', '0x3', '0x4', '0x5', '0x6', '0x7'];
|
|
16
|
+
* const tree = new MerkleTree(leaves);
|
|
17
|
+
* // tree = {
|
|
18
|
+
* // branches: [['0x5bb9440e2...', '0x262697b88...', ...], ['0x38118a340...', ...], ...],
|
|
19
|
+
* // leaves: ['0x1', '0x2', '0x3', '0x4', '0x5', '0x6', '0x7'],
|
|
20
|
+
* // root: '0x7f748c75e5bdb7ae28013f076b8ab650c4e01d3530c6e5ab665f9f1accbe7d4',
|
|
21
|
+
* // hashMethod: [Function computePedersenHash],
|
|
22
|
+
* // }
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
constructor(leafHashes: string[], hashMethod?: (a: BigNumberish, b: BigNumberish) => string);
|
|
26
|
+
/** @ignore */
|
|
27
|
+
private build;
|
|
28
|
+
/**
|
|
29
|
+
* Calculate hash from ordered a and b, Pedersen hash default
|
|
30
|
+
*
|
|
31
|
+
* @param a first value
|
|
32
|
+
* @param b second value
|
|
33
|
+
* @param hashMethod hash method to use, default: Pedersen
|
|
34
|
+
* @returns result of the hash function
|
|
35
|
+
* @example
|
|
36
|
+
* ```typescript
|
|
37
|
+
* const result1 = MerkleTree.hash('0xabc', '0xdef');
|
|
38
|
+
* // result1 = '0x484f029da7914ada038b1adf67fc83632364a3ebc2cd9349b41ab61626d9e82'
|
|
39
|
+
*
|
|
40
|
+
* const customHashMethod = (a, b) => `custom_${a}_${b}`;
|
|
41
|
+
* const result2 = MerkleTree.hash('0xabc', '0xdef', customHashMethod);
|
|
42
|
+
* // result2 = 'custom_2748_3567'
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
static hash(a: BigNumberish, b: BigNumberish, hashMethod?: (a: BigNumberish, b: BigNumberish) => string): string;
|
|
46
|
+
/**
|
|
47
|
+
* Calculates the merkle membership proof path
|
|
48
|
+
*
|
|
49
|
+
* @param leaf hex-string
|
|
50
|
+
* @param branch hex-string array
|
|
51
|
+
* @param hashPath hex-string array
|
|
52
|
+
* @returns collection of merkle proof hex-string hashes
|
|
53
|
+
* @example
|
|
54
|
+
* ```typescript
|
|
55
|
+
* const leaves = ['0x1', '0x2', '0x3', '0x4', '0x5', '0x6', '0x7'];
|
|
56
|
+
* const tree = new MerkleTree(leaves);
|
|
57
|
+
* const result = tree.getProof('0x3');
|
|
58
|
+
* // result = [
|
|
59
|
+
* // '0x4',
|
|
60
|
+
* // '0x5bb9440e27889a364bcb678b1f679ecd1347acdedcbf36e83494f857cc58026',
|
|
61
|
+
* // '0x8c0e46dd2df9aaf3a8ebfbc25408a582ad7fa7171f0698ddbbc5130b4b4e60',
|
|
62
|
+
* // ]
|
|
63
|
+
* ```
|
|
64
|
+
*/
|
|
65
|
+
getProof(leaf: string, branch?: string[], hashPath?: string[]): string[];
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Tests a Merkle tree path
|
|
69
|
+
*
|
|
70
|
+
* @param root hex-string
|
|
71
|
+
* @param leaf hex-string
|
|
72
|
+
* @param path hex-string array
|
|
73
|
+
* @param hashMethod hash method to use, default: Pedersen
|
|
74
|
+
* @returns true if the path is valid, false otherwise
|
|
75
|
+
* @example
|
|
76
|
+
* ```typescript
|
|
77
|
+
* const leaves = ['0x1', '0x2', '0x3', '0x4', '0x5', '0x6', '0x7'];
|
|
78
|
+
* const tree = new MerkleTree(leaves);
|
|
79
|
+
* const result = proofMerklePath(tree.root, '0x3', [
|
|
80
|
+
* '0x4',
|
|
81
|
+
* '0x5bb9440e27889a364bcb678b1f679ecd1347acdedcbf36e83494f857cc58026',
|
|
82
|
+
* '0x8c0e46dd2df9aaf3a8ebfbc25408a582ad7fa7171f0698ddbbc5130b4b4e60',
|
|
83
|
+
* ]);
|
|
84
|
+
* // result = true
|
|
85
|
+
* ```
|
|
86
|
+
*/
|
|
87
|
+
export declare function proofMerklePath(root: string, leaf: string, path: string[], hashMethod?: (a: BigNumberish, b: BigNumberish) => string): boolean;
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { computePedersenHash } from "./hash/index.mjs";
|
|
2
|
+
export class MerkleTree {
|
|
3
|
+
leaves;
|
|
4
|
+
branches = [];
|
|
5
|
+
root;
|
|
6
|
+
hashMethod;
|
|
7
|
+
/**
|
|
8
|
+
* Create a Merkle tree
|
|
9
|
+
*
|
|
10
|
+
* @param leafHashes hex-string array
|
|
11
|
+
* @param hashMethod hash method to use, default: Pedersen
|
|
12
|
+
* @returns created Merkle tree
|
|
13
|
+
* @example
|
|
14
|
+
* ```typescript
|
|
15
|
+
* const leaves = ['0x1', '0x2', '0x3', '0x4', '0x5', '0x6', '0x7'];
|
|
16
|
+
* const tree = new MerkleTree(leaves);
|
|
17
|
+
* // tree = {
|
|
18
|
+
* // branches: [['0x5bb9440e2...', '0x262697b88...', ...], ['0x38118a340...', ...], ...],
|
|
19
|
+
* // leaves: ['0x1', '0x2', '0x3', '0x4', '0x5', '0x6', '0x7'],
|
|
20
|
+
* // root: '0x7f748c75e5bdb7ae28013f076b8ab650c4e01d3530c6e5ab665f9f1accbe7d4',
|
|
21
|
+
* // hashMethod: [Function computePedersenHash],
|
|
22
|
+
* // }
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
constructor(leafHashes, hashMethod = computePedersenHash) {
|
|
26
|
+
this.hashMethod = hashMethod;
|
|
27
|
+
this.leaves = leafHashes;
|
|
28
|
+
this.root = this.build(leafHashes);
|
|
29
|
+
}
|
|
30
|
+
/** @ignore */
|
|
31
|
+
build(leaves) {
|
|
32
|
+
if (leaves.length === 1) {
|
|
33
|
+
return leaves[0];
|
|
34
|
+
}
|
|
35
|
+
if (leaves.length !== this.leaves.length) {
|
|
36
|
+
this.branches.push(leaves);
|
|
37
|
+
}
|
|
38
|
+
const newLeaves = [];
|
|
39
|
+
for (let i = 0; i < leaves.length; i += 2) {
|
|
40
|
+
if (i + 1 === leaves.length) {
|
|
41
|
+
newLeaves.push(MerkleTree.hash(leaves[i], "0x0", this.hashMethod));
|
|
42
|
+
} else {
|
|
43
|
+
newLeaves.push(MerkleTree.hash(leaves[i], leaves[i + 1], this.hashMethod));
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return this.build(newLeaves);
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Calculate hash from ordered a and b, Pedersen hash default
|
|
50
|
+
*
|
|
51
|
+
* @param a first value
|
|
52
|
+
* @param b second value
|
|
53
|
+
* @param hashMethod hash method to use, default: Pedersen
|
|
54
|
+
* @returns result of the hash function
|
|
55
|
+
* @example
|
|
56
|
+
* ```typescript
|
|
57
|
+
* const result1 = MerkleTree.hash('0xabc', '0xdef');
|
|
58
|
+
* // result1 = '0x484f029da7914ada038b1adf67fc83632364a3ebc2cd9349b41ab61626d9e82'
|
|
59
|
+
*
|
|
60
|
+
* const customHashMethod = (a, b) => `custom_${a}_${b}`;
|
|
61
|
+
* const result2 = MerkleTree.hash('0xabc', '0xdef', customHashMethod);
|
|
62
|
+
* // result2 = 'custom_2748_3567'
|
|
63
|
+
* ```
|
|
64
|
+
*/
|
|
65
|
+
static hash(a, b, hashMethod = computePedersenHash) {
|
|
66
|
+
const [aSorted, bSorted] = [BigInt(a), BigInt(b)].sort((x, y) => x >= y ? 1 : -1);
|
|
67
|
+
return hashMethod(aSorted, bSorted);
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Calculates the merkle membership proof path
|
|
71
|
+
*
|
|
72
|
+
* @param leaf hex-string
|
|
73
|
+
* @param branch hex-string array
|
|
74
|
+
* @param hashPath hex-string array
|
|
75
|
+
* @returns collection of merkle proof hex-string hashes
|
|
76
|
+
* @example
|
|
77
|
+
* ```typescript
|
|
78
|
+
* const leaves = ['0x1', '0x2', '0x3', '0x4', '0x5', '0x6', '0x7'];
|
|
79
|
+
* const tree = new MerkleTree(leaves);
|
|
80
|
+
* const result = tree.getProof('0x3');
|
|
81
|
+
* // result = [
|
|
82
|
+
* // '0x4',
|
|
83
|
+
* // '0x5bb9440e27889a364bcb678b1f679ecd1347acdedcbf36e83494f857cc58026',
|
|
84
|
+
* // '0x8c0e46dd2df9aaf3a8ebfbc25408a582ad7fa7171f0698ddbbc5130b4b4e60',
|
|
85
|
+
* // ]
|
|
86
|
+
* ```
|
|
87
|
+
*/
|
|
88
|
+
getProof(leaf, branch = this.leaves, hashPath = []) {
|
|
89
|
+
const index = branch.indexOf(leaf);
|
|
90
|
+
if (index === -1) {
|
|
91
|
+
throw new Error("leaf not found");
|
|
92
|
+
}
|
|
93
|
+
if (branch.length === 1) {
|
|
94
|
+
return hashPath;
|
|
95
|
+
}
|
|
96
|
+
const isLeft = index % 2 === 0;
|
|
97
|
+
const neededBranch = (isLeft ? branch[index + 1] : branch[index - 1]) ?? "0x0";
|
|
98
|
+
const newHashPath = [...hashPath, neededBranch];
|
|
99
|
+
const currentBranchLevelIndex = this.leaves.length === branch.length ? -1 : this.branches.findIndex((b) => b.length === branch.length);
|
|
100
|
+
const nextBranch = this.branches[currentBranchLevelIndex + 1] ?? [this.root];
|
|
101
|
+
return this.getProof(
|
|
102
|
+
MerkleTree.hash(isLeft ? leaf : neededBranch, isLeft ? neededBranch : leaf, this.hashMethod),
|
|
103
|
+
nextBranch,
|
|
104
|
+
newHashPath
|
|
105
|
+
);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
export function proofMerklePath(root, leaf, path, hashMethod = computePedersenHash) {
|
|
109
|
+
if (path.length === 0) {
|
|
110
|
+
return root === leaf;
|
|
111
|
+
}
|
|
112
|
+
const [next, ...rest] = path;
|
|
113
|
+
return proofMerklePath(root, MerkleTree.hash(leaf, next, hashMethod), rest, hashMethod);
|
|
114
|
+
}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.addPercent = addPercent;
|
|
7
|
+
exports.assertInRange = assertInRange;
|
|
8
|
+
exports.bigNumberishArrayToDecimalStringArray = bigNumberishArrayToDecimalStringArray;
|
|
9
|
+
exports.bigNumberishArrayToHexadecimalStringArray = bigNumberishArrayToHexadecimalStringArray;
|
|
10
|
+
exports.cleanHex = cleanHex;
|
|
11
|
+
exports.getDecimalString = getDecimalString;
|
|
12
|
+
exports.getHexString = getHexString;
|
|
13
|
+
exports.getHexStringArray = getHexStringArray;
|
|
14
|
+
exports.hexToBytes = hexToBytes;
|
|
15
|
+
exports.hexToDecimalString = hexToDecimalString;
|
|
16
|
+
exports.isBigInt = isBigInt;
|
|
17
|
+
exports.isBoolean = isBoolean;
|
|
18
|
+
exports.isHex = isHex;
|
|
19
|
+
exports.isNumber = isNumber;
|
|
20
|
+
exports.isStringWholeNumber = isStringWholeNumber;
|
|
21
|
+
exports.toBigInt = toBigInt;
|
|
22
|
+
exports.toCairoBool = toCairoBool;
|
|
23
|
+
exports.toHex = toHex;
|
|
24
|
+
exports.toHexString = void 0;
|
|
25
|
+
exports.toStorageKey = toStorageKey;
|
|
26
|
+
var _utils = require("@noble/curves/abstract/utils");
|
|
27
|
+
var _assert = _interopRequireDefault(require("./assert.cjs"));
|
|
28
|
+
var _encode = require("./encode.cjs");
|
|
29
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
30
|
+
function isHex(hex) {
|
|
31
|
+
return /^0x[0-9a-f]*$/i.test(hex);
|
|
32
|
+
}
|
|
33
|
+
function toBigInt(value) {
|
|
34
|
+
return BigInt(value);
|
|
35
|
+
}
|
|
36
|
+
function isBigInt(value) {
|
|
37
|
+
return typeof value === "bigint";
|
|
38
|
+
}
|
|
39
|
+
function toHex(value) {
|
|
40
|
+
return (0, _encode.addHexPrefix)(toBigInt(value).toString(16));
|
|
41
|
+
}
|
|
42
|
+
const toHexString = exports.toHexString = toHex;
|
|
43
|
+
function toStorageKey(number) {
|
|
44
|
+
return (0, _encode.addHexPrefix)(toBigInt(number).toString(16).padStart(64, "0"));
|
|
45
|
+
}
|
|
46
|
+
function hexToDecimalString(hex) {
|
|
47
|
+
return BigInt((0, _encode.addHexPrefix)(hex)).toString(10);
|
|
48
|
+
}
|
|
49
|
+
function cleanHex(hex) {
|
|
50
|
+
return hex.toLowerCase().replace(/^(0x)0+/, "$1");
|
|
51
|
+
}
|
|
52
|
+
function assertInRange(input, lowerBound, upperBound, inputName = "") {
|
|
53
|
+
const messageSuffix = inputName === "" ? "invalid length" : `invalid ${inputName} length`;
|
|
54
|
+
const inputBigInt = BigInt(input);
|
|
55
|
+
const lowerBoundBigInt = BigInt(lowerBound);
|
|
56
|
+
const upperBoundBigInt = BigInt(upperBound);
|
|
57
|
+
(0, _assert.default)(inputBigInt >= lowerBoundBigInt && inputBigInt <= upperBoundBigInt, `Message not signable, ${messageSuffix}.`);
|
|
58
|
+
}
|
|
59
|
+
function bigNumberishArrayToDecimalStringArray(data) {
|
|
60
|
+
return data.map(x => toBigInt(x).toString(10));
|
|
61
|
+
}
|
|
62
|
+
function bigNumberishArrayToHexadecimalStringArray(data) {
|
|
63
|
+
return data.map(x => toHex(x));
|
|
64
|
+
}
|
|
65
|
+
function isStringWholeNumber(str) {
|
|
66
|
+
return /^\d+$/.test(str);
|
|
67
|
+
}
|
|
68
|
+
function getDecimalString(str) {
|
|
69
|
+
if (isHex(str)) {
|
|
70
|
+
return hexToDecimalString(str);
|
|
71
|
+
}
|
|
72
|
+
if (isStringWholeNumber(str)) {
|
|
73
|
+
return str;
|
|
74
|
+
}
|
|
75
|
+
throw new Error(`${str} needs to be a hex-string or whole-number-string`);
|
|
76
|
+
}
|
|
77
|
+
function getHexString(str) {
|
|
78
|
+
if (isHex(str)) {
|
|
79
|
+
return str;
|
|
80
|
+
}
|
|
81
|
+
if (isStringWholeNumber(str)) {
|
|
82
|
+
return toHexString(str);
|
|
83
|
+
}
|
|
84
|
+
throw new Error(`${str} needs to be a hex-string or whole-number-string`);
|
|
85
|
+
}
|
|
86
|
+
function getHexStringArray(array) {
|
|
87
|
+
return array.map(getHexString);
|
|
88
|
+
}
|
|
89
|
+
function toCairoBool(value) {
|
|
90
|
+
return (+value).toString();
|
|
91
|
+
}
|
|
92
|
+
function hexToBytes(str) {
|
|
93
|
+
if (!isHex(str)) throw new Error(`${str} needs to be a hex-string`);
|
|
94
|
+
let adaptedValue = (0, _encode.removeHexPrefix)(str);
|
|
95
|
+
if (adaptedValue.length % 2 !== 0) {
|
|
96
|
+
adaptedValue = `0${adaptedValue}`;
|
|
97
|
+
}
|
|
98
|
+
return (0, _utils.hexToBytes)(adaptedValue);
|
|
99
|
+
}
|
|
100
|
+
function addPercent(number, percent) {
|
|
101
|
+
const bigIntNum = BigInt(number);
|
|
102
|
+
return bigIntNum + bigIntNum * BigInt(percent) / 100n;
|
|
103
|
+
}
|
|
104
|
+
function isNumber(value) {
|
|
105
|
+
return typeof value === "number";
|
|
106
|
+
}
|
|
107
|
+
function isBoolean(value) {
|
|
108
|
+
return typeof value === "boolean";
|
|
109
|
+
}
|