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,285 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
var _exportNames = {
|
|
7
|
+
prepareSelector: true,
|
|
8
|
+
isMerkleTreeType: true,
|
|
9
|
+
getDependencies: true,
|
|
10
|
+
encodeType: true,
|
|
11
|
+
getTypeHash: true,
|
|
12
|
+
encodeValue: true,
|
|
13
|
+
encodeData: true,
|
|
14
|
+
getStructHash: true,
|
|
15
|
+
getMessageHash: true
|
|
16
|
+
};
|
|
17
|
+
exports.encodeData = encodeData;
|
|
18
|
+
exports.encodeType = encodeType;
|
|
19
|
+
exports.encodeValue = encodeValue;
|
|
20
|
+
exports.getDependencies = getDependencies;
|
|
21
|
+
exports.getMessageHash = getMessageHash;
|
|
22
|
+
exports.getStructHash = getStructHash;
|
|
23
|
+
exports.getTypeHash = getTypeHash;
|
|
24
|
+
exports.isMerkleTreeType = isMerkleTreeType;
|
|
25
|
+
exports.prepareSelector = prepareSelector;
|
|
26
|
+
var _constants = require("../constants.cjs");
|
|
27
|
+
var _types = require("../types/index.cjs");
|
|
28
|
+
var _assert = _interopRequireDefault(require("./assert.cjs"));
|
|
29
|
+
var _byteArray = require("./calldata/byteArray.cjs");
|
|
30
|
+
var _hash = require("./hash/index.cjs");
|
|
31
|
+
var _merkle = require("./merkle.cjs");
|
|
32
|
+
var _num = require("./num.cjs");
|
|
33
|
+
var _shortString = require("./shortString.cjs");
|
|
34
|
+
var _typedData = require("../types/typedData.cjs");
|
|
35
|
+
Object.keys(_typedData).forEach(function (key) {
|
|
36
|
+
if (key === "default" || key === "__esModule") return;
|
|
37
|
+
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
|
|
38
|
+
if (key in exports && exports[key] === _typedData[key]) return;
|
|
39
|
+
Object.defineProperty(exports, key, {
|
|
40
|
+
enumerable: true,
|
|
41
|
+
get: function () {
|
|
42
|
+
return _typedData[key];
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
});
|
|
46
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
47
|
+
const presetTypes = {
|
|
48
|
+
u256: JSON.parse('[{ "name": "low", "type": "u128" }, { "name": "high", "type": "u128" }]'),
|
|
49
|
+
TokenAmount: JSON.parse('[{ "name": "token_address", "type": "ContractAddress" }, { "name": "amount", "type": "u256" }]'),
|
|
50
|
+
NftId: JSON.parse('[{ "name": "collection_address", "type": "ContractAddress" }, { "name": "token_id", "type": "u256" }]')
|
|
51
|
+
};
|
|
52
|
+
const revisionConfiguration = {
|
|
53
|
+
[_types.TypedDataRevision.ACTIVE]: {
|
|
54
|
+
domain: "StarknetDomain",
|
|
55
|
+
hashMethod: _hash.computePoseidonHashOnElements,
|
|
56
|
+
hashMerkleMethod: _hash.computePoseidonHash,
|
|
57
|
+
escapeTypeString: s => `"${s}"`,
|
|
58
|
+
presetTypes
|
|
59
|
+
},
|
|
60
|
+
[_types.TypedDataRevision.LEGACY]: {
|
|
61
|
+
domain: "StarkNetDomain",
|
|
62
|
+
hashMethod: _hash.computePedersenHashOnElements,
|
|
63
|
+
hashMerkleMethod: _hash.computePedersenHash,
|
|
64
|
+
escapeTypeString: s => s,
|
|
65
|
+
presetTypes: {}
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
function assertRange(data, type, {
|
|
69
|
+
min,
|
|
70
|
+
max
|
|
71
|
+
}) {
|
|
72
|
+
const value = BigInt(data);
|
|
73
|
+
(0, _assert.default)(value >= min && value <= max, `${value} (${type}) is out of bounds [${min}, ${max}]`);
|
|
74
|
+
}
|
|
75
|
+
function identifyRevision({
|
|
76
|
+
types,
|
|
77
|
+
domain
|
|
78
|
+
}) {
|
|
79
|
+
if (revisionConfiguration[_types.TypedDataRevision.ACTIVE].domain in types && domain.revision === _types.TypedDataRevision.ACTIVE) return _types.TypedDataRevision.ACTIVE;
|
|
80
|
+
if (revisionConfiguration[_types.TypedDataRevision.LEGACY].domain in types && (domain.revision ?? _types.TypedDataRevision.LEGACY) === _types.TypedDataRevision.LEGACY) return _types.TypedDataRevision.LEGACY;
|
|
81
|
+
return void 0;
|
|
82
|
+
}
|
|
83
|
+
function getHex(value) {
|
|
84
|
+
try {
|
|
85
|
+
return (0, _num.toHex)(value);
|
|
86
|
+
} catch (e) {
|
|
87
|
+
if ((0, _shortString.isString)(value)) {
|
|
88
|
+
return (0, _num.toHex)((0, _shortString.encodeShortString)(value));
|
|
89
|
+
}
|
|
90
|
+
throw new Error(`Invalid BigNumberish: ${value}`);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
function validateTypedData(data) {
|
|
94
|
+
const typedData = data;
|
|
95
|
+
return Boolean(typedData.message && typedData.primaryType && typedData.types && identifyRevision(typedData));
|
|
96
|
+
}
|
|
97
|
+
function prepareSelector(selector) {
|
|
98
|
+
return (0, _num.isHex)(selector) ? selector : (0, _hash.getSelectorFromName)(selector);
|
|
99
|
+
}
|
|
100
|
+
function isMerkleTreeType(type) {
|
|
101
|
+
return type.type === "merkletree";
|
|
102
|
+
}
|
|
103
|
+
function getDependencies(types, type, dependencies = [], contains = "", revision = _types.TypedDataRevision.LEGACY) {
|
|
104
|
+
if (type[type.length - 1] === "*") {
|
|
105
|
+
type = type.slice(0, -1);
|
|
106
|
+
} else if (revision === _types.TypedDataRevision.ACTIVE) {
|
|
107
|
+
if (type === "enum") {
|
|
108
|
+
type = contains;
|
|
109
|
+
} else if (type.match(/^\(.*\)$/)) {
|
|
110
|
+
type = type.slice(1, -1);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
if (dependencies.includes(type) || !types[type]) {
|
|
114
|
+
return dependencies;
|
|
115
|
+
}
|
|
116
|
+
return [type, ...types[type].reduce((previous, t) => [...previous, ...getDependencies(types, t.type, previous, t.contains, revision).filter(dependency => !previous.includes(dependency))], [])];
|
|
117
|
+
}
|
|
118
|
+
function getMerkleTreeType(types, ctx) {
|
|
119
|
+
if (ctx.parent && ctx.key) {
|
|
120
|
+
const parentType = types[ctx.parent];
|
|
121
|
+
const merkleType = parentType.find(t => t.name === ctx.key);
|
|
122
|
+
const isMerkleTree = isMerkleTreeType(merkleType);
|
|
123
|
+
if (!isMerkleTree) {
|
|
124
|
+
throw new Error(`${ctx.key} is not a merkle tree`);
|
|
125
|
+
}
|
|
126
|
+
if (merkleType.contains.endsWith("*")) {
|
|
127
|
+
throw new Error(`Merkle tree contain property must not be an array but was given ${ctx.key}`);
|
|
128
|
+
}
|
|
129
|
+
return merkleType.contains;
|
|
130
|
+
}
|
|
131
|
+
return "raw";
|
|
132
|
+
}
|
|
133
|
+
function encodeType(types, type, revision = _types.TypedDataRevision.LEGACY) {
|
|
134
|
+
const allTypes = revision === _types.TypedDataRevision.ACTIVE ? {
|
|
135
|
+
...types,
|
|
136
|
+
...revisionConfiguration[revision].presetTypes
|
|
137
|
+
} : types;
|
|
138
|
+
const [primary, ...dependencies] = getDependencies(allTypes, type, void 0, void 0, revision);
|
|
139
|
+
const newTypes = !primary ? [] : [primary, ...dependencies.sort()];
|
|
140
|
+
const esc = revisionConfiguration[revision].escapeTypeString;
|
|
141
|
+
return newTypes.map(dependency => {
|
|
142
|
+
const dependencyElements = allTypes[dependency].map(t => {
|
|
143
|
+
const targetType = t.type === "enum" && revision === _types.TypedDataRevision.ACTIVE ? t.contains : t.type;
|
|
144
|
+
const typeString = targetType.match(/^\(.*\)$/) ? `(${targetType.slice(1, -1).split(",").map(e => e ? esc(e) : e).join(",")})` : esc(targetType);
|
|
145
|
+
return `${esc(t.name)}:${typeString}`;
|
|
146
|
+
});
|
|
147
|
+
return `${esc(dependency)}(${dependencyElements})`;
|
|
148
|
+
}).join("");
|
|
149
|
+
}
|
|
150
|
+
function getTypeHash(types, type, revision = _types.TypedDataRevision.LEGACY) {
|
|
151
|
+
return (0, _hash.getSelectorFromName)(encodeType(types, type, revision));
|
|
152
|
+
}
|
|
153
|
+
function encodeValue(types, type, data, ctx = {}, revision = _types.TypedDataRevision.LEGACY) {
|
|
154
|
+
if (types[type]) {
|
|
155
|
+
return [type, getStructHash(types, type, data, revision)];
|
|
156
|
+
}
|
|
157
|
+
if (revisionConfiguration[revision].presetTypes[type]) {
|
|
158
|
+
return [type, getStructHash(revisionConfiguration[revision].presetTypes, type, data, revision)];
|
|
159
|
+
}
|
|
160
|
+
if (type.endsWith("*")) {
|
|
161
|
+
const hashes = data.map(entry => encodeValue(types, type.slice(0, -1), entry, void 0, revision)[1]);
|
|
162
|
+
return [type, revisionConfiguration[revision].hashMethod(hashes)];
|
|
163
|
+
}
|
|
164
|
+
switch (type) {
|
|
165
|
+
case "enum":
|
|
166
|
+
{
|
|
167
|
+
if (revision === _types.TypedDataRevision.ACTIVE) {
|
|
168
|
+
const [variantKey, variantData] = Object.entries(data)[0];
|
|
169
|
+
const parentType = types[ctx.parent][0];
|
|
170
|
+
const enumType = types[parentType.contains];
|
|
171
|
+
const variantType = enumType.find(t => t.name === variantKey);
|
|
172
|
+
const variantIndex = enumType.indexOf(variantType);
|
|
173
|
+
const encodedSubtypes = variantType.type.slice(1, -1).split(",").map((subtype, index) => {
|
|
174
|
+
if (!subtype) return subtype;
|
|
175
|
+
const subtypeData = variantData[index];
|
|
176
|
+
return encodeValue(types, subtype, subtypeData, void 0, revision)[1];
|
|
177
|
+
});
|
|
178
|
+
return [type, revisionConfiguration[revision].hashMethod([variantIndex, ...encodedSubtypes])];
|
|
179
|
+
}
|
|
180
|
+
return [type, getHex(data)];
|
|
181
|
+
}
|
|
182
|
+
case "merkletree":
|
|
183
|
+
{
|
|
184
|
+
const merkleTreeType = getMerkleTreeType(types, ctx);
|
|
185
|
+
const structHashes = data.map(struct => {
|
|
186
|
+
return encodeValue(types, merkleTreeType, struct, void 0, revision)[1];
|
|
187
|
+
});
|
|
188
|
+
const {
|
|
189
|
+
root
|
|
190
|
+
} = new _merkle.MerkleTree(structHashes, revisionConfiguration[revision].hashMerkleMethod);
|
|
191
|
+
return ["felt", root];
|
|
192
|
+
}
|
|
193
|
+
case "selector":
|
|
194
|
+
{
|
|
195
|
+
return ["felt", prepareSelector(data)];
|
|
196
|
+
}
|
|
197
|
+
case "string":
|
|
198
|
+
{
|
|
199
|
+
if (revision === _types.TypedDataRevision.ACTIVE) {
|
|
200
|
+
const byteArray = (0, _byteArray.byteArrayFromString)(data);
|
|
201
|
+
const elements = [byteArray.data.length, ...byteArray.data, byteArray.pending_word, byteArray.pending_word_len];
|
|
202
|
+
return [type, revisionConfiguration[revision].hashMethod(elements)];
|
|
203
|
+
}
|
|
204
|
+
return [type, getHex(data)];
|
|
205
|
+
}
|
|
206
|
+
case "i128":
|
|
207
|
+
{
|
|
208
|
+
if (revision === _types.TypedDataRevision.ACTIVE) {
|
|
209
|
+
const value = BigInt(data);
|
|
210
|
+
assertRange(value, type, _constants.RANGE_I128);
|
|
211
|
+
return [type, getHex(value < 0n ? _constants.PRIME + value : value)];
|
|
212
|
+
}
|
|
213
|
+
return [type, getHex(data)];
|
|
214
|
+
}
|
|
215
|
+
case "timestamp":
|
|
216
|
+
case "u128":
|
|
217
|
+
{
|
|
218
|
+
if (revision === _types.TypedDataRevision.ACTIVE) {
|
|
219
|
+
assertRange(data, type, _constants.RANGE_U128);
|
|
220
|
+
}
|
|
221
|
+
return [type, getHex(data)];
|
|
222
|
+
}
|
|
223
|
+
case "felt":
|
|
224
|
+
case "shortstring":
|
|
225
|
+
{
|
|
226
|
+
if (revision === _types.TypedDataRevision.ACTIVE) {
|
|
227
|
+
assertRange(getHex(data), type, _constants.RANGE_FELT);
|
|
228
|
+
}
|
|
229
|
+
return [type, getHex(data)];
|
|
230
|
+
}
|
|
231
|
+
case "ClassHash":
|
|
232
|
+
case "ContractAddress":
|
|
233
|
+
{
|
|
234
|
+
if (revision === _types.TypedDataRevision.ACTIVE) {
|
|
235
|
+
assertRange(data, type, _constants.RANGE_FELT);
|
|
236
|
+
}
|
|
237
|
+
return [type, getHex(data)];
|
|
238
|
+
}
|
|
239
|
+
case "bool":
|
|
240
|
+
{
|
|
241
|
+
if (revision === _types.TypedDataRevision.ACTIVE) {
|
|
242
|
+
(0, _assert.default)(typeof data === "boolean", `Type mismatch for ${type} ${data}`);
|
|
243
|
+
}
|
|
244
|
+
return [type, getHex(data)];
|
|
245
|
+
}
|
|
246
|
+
default:
|
|
247
|
+
{
|
|
248
|
+
if (revision === _types.TypedDataRevision.ACTIVE) {
|
|
249
|
+
throw new Error(`Unsupported type: ${type}`);
|
|
250
|
+
}
|
|
251
|
+
return [type, getHex(data)];
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
function encodeData(types, type, data, revision = _types.TypedDataRevision.LEGACY) {
|
|
256
|
+
const targetType = types[type] ?? revisionConfiguration[revision].presetTypes[type];
|
|
257
|
+
const [returnTypes, values] = targetType.reduce(([ts, vs], field) => {
|
|
258
|
+
if (data[field.name] === void 0 || data[field.name] === null && field.type !== "enum") {
|
|
259
|
+
throw new Error(`Cannot encode data: missing data for '${field.name}'`);
|
|
260
|
+
}
|
|
261
|
+
const value = data[field.name];
|
|
262
|
+
const ctx = {
|
|
263
|
+
parent: type,
|
|
264
|
+
key: field.name
|
|
265
|
+
};
|
|
266
|
+
const [t, encodedValue] = encodeValue(types, field.type, value, ctx, revision);
|
|
267
|
+
return [[...ts, t], [...vs, encodedValue]];
|
|
268
|
+
}, [["felt"], [getTypeHash(types, type, revision)]]);
|
|
269
|
+
return [returnTypes, values];
|
|
270
|
+
}
|
|
271
|
+
function getStructHash(types, type, data, revision = _types.TypedDataRevision.LEGACY) {
|
|
272
|
+
return revisionConfiguration[revision].hashMethod(encodeData(types, type, data, revision)[1]);
|
|
273
|
+
}
|
|
274
|
+
function getMessageHash(typedData, account) {
|
|
275
|
+
if (!validateTypedData(typedData)) {
|
|
276
|
+
throw new Error("Typed data does not match JSON schema");
|
|
277
|
+
}
|
|
278
|
+
const revision = identifyRevision(typedData);
|
|
279
|
+
const {
|
|
280
|
+
domain,
|
|
281
|
+
hashMethod
|
|
282
|
+
} = revisionConfiguration[revision];
|
|
283
|
+
const message = [(0, _shortString.encodeShortString)("StarkNet Message"), getStructHash(typedData.types, domain, typedData.domain, revision), account, getStructHash(typedData.types, typedData.primaryType, typedData.message, revision)];
|
|
284
|
+
return hashMethod(message);
|
|
285
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { BigNumberish, TypedDataRevision as Revision, StarknetMerkleType, StarknetType, TypedData } from '../types';
|
|
2
|
+
/** @deprecated prefer importing from 'types' over 'typedData' */
|
|
3
|
+
export * from '../types/typedData';
|
|
4
|
+
interface Context {
|
|
5
|
+
parent?: string;
|
|
6
|
+
key?: string;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Prepares the selector for use.
|
|
10
|
+
*
|
|
11
|
+
* @param {string} selector - The selector to be prepared.
|
|
12
|
+
* @returns {string} The prepared selector.
|
|
13
|
+
*/
|
|
14
|
+
export declare function prepareSelector(selector: string): string;
|
|
15
|
+
/**
|
|
16
|
+
* Checks if the given Starknet type is a Merkle tree type.
|
|
17
|
+
*
|
|
18
|
+
* @param {StarknetType} type - The StarkNet type to check.
|
|
19
|
+
*
|
|
20
|
+
* @returns {boolean} - True if the type is a Merkle tree type, false otherwise.
|
|
21
|
+
*/
|
|
22
|
+
export declare function isMerkleTreeType(type: StarknetType): type is StarknetMerkleType;
|
|
23
|
+
/**
|
|
24
|
+
* Get the dependencies of a struct type. If a struct has the same dependency multiple times, it's only included once
|
|
25
|
+
* in the resulting array.
|
|
26
|
+
*/
|
|
27
|
+
export declare function getDependencies(types: TypedData['types'], type: string, dependencies?: string[], contains?: string, revision?: Revision): string[];
|
|
28
|
+
/**
|
|
29
|
+
* Encode a type to a string. All dependent types are alphabetically sorted.
|
|
30
|
+
*/
|
|
31
|
+
export declare function encodeType(types: TypedData['types'], type: string, revision?: Revision): string;
|
|
32
|
+
/**
|
|
33
|
+
* Get a type string as hash.
|
|
34
|
+
*/
|
|
35
|
+
export declare function getTypeHash(types: TypedData['types'], type: string, revision?: Revision): string;
|
|
36
|
+
/**
|
|
37
|
+
* Encodes a single value to an ABI serialisable string, number or Buffer. Returns the data as tuple, which consists of
|
|
38
|
+
* an array of ABI compatible types, and an array of corresponding values.
|
|
39
|
+
*/
|
|
40
|
+
export declare function encodeValue(types: TypedData['types'], type: string, data: unknown, ctx?: Context, revision?: Revision): [string, string];
|
|
41
|
+
/**
|
|
42
|
+
* Encode the data to an ABI encoded Buffer. The data should be a key -> value object with all the required values.
|
|
43
|
+
* All dependent types are automatically encoded.
|
|
44
|
+
*/
|
|
45
|
+
export declare function encodeData<T extends TypedData>(types: T['types'], type: string, data: T['message'], revision?: Revision): string[][];
|
|
46
|
+
/**
|
|
47
|
+
* Get encoded data as a hash. The data should be a key -> value object with all the required values.
|
|
48
|
+
* All dependent types are automatically encoded.
|
|
49
|
+
*/
|
|
50
|
+
export declare function getStructHash<T extends TypedData>(types: T['types'], type: string, data: T['message'], revision?: Revision): string;
|
|
51
|
+
/**
|
|
52
|
+
* Get the SNIP-12 encoded message to sign, from the typedData object.
|
|
53
|
+
*/
|
|
54
|
+
export declare function getMessageHash(typedData: TypedData, account: BigNumberish): string;
|
|
@@ -0,0 +1,286 @@
|
|
|
1
|
+
import { PRIME, RANGE_FELT, RANGE_I128, RANGE_U128 } from "../constants.mjs";
|
|
2
|
+
import {
|
|
3
|
+
TypedDataRevision as Revision
|
|
4
|
+
} from "../types/index.mjs";
|
|
5
|
+
import assert from "./assert.mjs";
|
|
6
|
+
import { byteArrayFromString } from "./calldata/byteArray.mjs";
|
|
7
|
+
import {
|
|
8
|
+
computePedersenHash,
|
|
9
|
+
computePedersenHashOnElements,
|
|
10
|
+
computePoseidonHash,
|
|
11
|
+
computePoseidonHashOnElements,
|
|
12
|
+
getSelectorFromName
|
|
13
|
+
} from "./hash/index.mjs";
|
|
14
|
+
import { MerkleTree } from "./merkle.mjs";
|
|
15
|
+
import { isHex, toHex } from "./num.mjs";
|
|
16
|
+
import { encodeShortString, isString } from "./shortString.mjs";
|
|
17
|
+
export * from "../types/typedData.mjs";
|
|
18
|
+
const presetTypes = {
|
|
19
|
+
u256: JSON.parse('[{ "name": "low", "type": "u128" }, { "name": "high", "type": "u128" }]'),
|
|
20
|
+
TokenAmount: JSON.parse(
|
|
21
|
+
'[{ "name": "token_address", "type": "ContractAddress" }, { "name": "amount", "type": "u256" }]'
|
|
22
|
+
),
|
|
23
|
+
NftId: JSON.parse(
|
|
24
|
+
'[{ "name": "collection_address", "type": "ContractAddress" }, { "name": "token_id", "type": "u256" }]'
|
|
25
|
+
)
|
|
26
|
+
};
|
|
27
|
+
const revisionConfiguration = {
|
|
28
|
+
[Revision.ACTIVE]: {
|
|
29
|
+
domain: "StarknetDomain",
|
|
30
|
+
hashMethod: computePoseidonHashOnElements,
|
|
31
|
+
hashMerkleMethod: computePoseidonHash,
|
|
32
|
+
escapeTypeString: (s) => `"${s}"`,
|
|
33
|
+
presetTypes
|
|
34
|
+
},
|
|
35
|
+
[Revision.LEGACY]: {
|
|
36
|
+
domain: "StarkNetDomain",
|
|
37
|
+
hashMethod: computePedersenHashOnElements,
|
|
38
|
+
hashMerkleMethod: computePedersenHash,
|
|
39
|
+
escapeTypeString: (s) => s,
|
|
40
|
+
presetTypes: {}
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
function assertRange(data, type, { min, max }) {
|
|
44
|
+
const value = BigInt(data);
|
|
45
|
+
assert(value >= min && value <= max, `${value} (${type}) is out of bounds [${min}, ${max}]`);
|
|
46
|
+
}
|
|
47
|
+
function identifyRevision({ types, domain }) {
|
|
48
|
+
if (revisionConfiguration[Revision.ACTIVE].domain in types && domain.revision === Revision.ACTIVE)
|
|
49
|
+
return Revision.ACTIVE;
|
|
50
|
+
if (revisionConfiguration[Revision.LEGACY].domain in types && (domain.revision ?? Revision.LEGACY) === Revision.LEGACY)
|
|
51
|
+
return Revision.LEGACY;
|
|
52
|
+
return void 0;
|
|
53
|
+
}
|
|
54
|
+
function getHex(value) {
|
|
55
|
+
try {
|
|
56
|
+
return toHex(value);
|
|
57
|
+
} catch (e) {
|
|
58
|
+
if (isString(value)) {
|
|
59
|
+
return toHex(encodeShortString(value));
|
|
60
|
+
}
|
|
61
|
+
throw new Error(`Invalid BigNumberish: ${value}`);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
function validateTypedData(data) {
|
|
65
|
+
const typedData = data;
|
|
66
|
+
return Boolean(
|
|
67
|
+
typedData.message && typedData.primaryType && typedData.types && identifyRevision(typedData)
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
export function prepareSelector(selector) {
|
|
71
|
+
return isHex(selector) ? selector : getSelectorFromName(selector);
|
|
72
|
+
}
|
|
73
|
+
export function isMerkleTreeType(type) {
|
|
74
|
+
return type.type === "merkletree";
|
|
75
|
+
}
|
|
76
|
+
export function getDependencies(types, type, dependencies = [], contains = "", revision = Revision.LEGACY) {
|
|
77
|
+
if (type[type.length - 1] === "*") {
|
|
78
|
+
type = type.slice(0, -1);
|
|
79
|
+
} else if (revision === Revision.ACTIVE) {
|
|
80
|
+
if (type === "enum") {
|
|
81
|
+
type = contains;
|
|
82
|
+
} else if (type.match(/^\(.*\)$/)) {
|
|
83
|
+
type = type.slice(1, -1);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
if (dependencies.includes(type) || !types[type]) {
|
|
87
|
+
return dependencies;
|
|
88
|
+
}
|
|
89
|
+
return [
|
|
90
|
+
type,
|
|
91
|
+
...types[type].reduce(
|
|
92
|
+
(previous, t) => [
|
|
93
|
+
...previous,
|
|
94
|
+
...getDependencies(types, t.type, previous, t.contains, revision).filter(
|
|
95
|
+
(dependency) => !previous.includes(dependency)
|
|
96
|
+
)
|
|
97
|
+
],
|
|
98
|
+
[]
|
|
99
|
+
)
|
|
100
|
+
];
|
|
101
|
+
}
|
|
102
|
+
function getMerkleTreeType(types, ctx) {
|
|
103
|
+
if (ctx.parent && ctx.key) {
|
|
104
|
+
const parentType = types[ctx.parent];
|
|
105
|
+
const merkleType = parentType.find((t) => t.name === ctx.key);
|
|
106
|
+
const isMerkleTree = isMerkleTreeType(merkleType);
|
|
107
|
+
if (!isMerkleTree) {
|
|
108
|
+
throw new Error(`${ctx.key} is not a merkle tree`);
|
|
109
|
+
}
|
|
110
|
+
if (merkleType.contains.endsWith("*")) {
|
|
111
|
+
throw new Error(`Merkle tree contain property must not be an array but was given ${ctx.key}`);
|
|
112
|
+
}
|
|
113
|
+
return merkleType.contains;
|
|
114
|
+
}
|
|
115
|
+
return "raw";
|
|
116
|
+
}
|
|
117
|
+
export function encodeType(types, type, revision = Revision.LEGACY) {
|
|
118
|
+
const allTypes = revision === Revision.ACTIVE ? { ...types, ...revisionConfiguration[revision].presetTypes } : types;
|
|
119
|
+
const [primary, ...dependencies] = getDependencies(
|
|
120
|
+
allTypes,
|
|
121
|
+
type,
|
|
122
|
+
void 0,
|
|
123
|
+
void 0,
|
|
124
|
+
revision
|
|
125
|
+
);
|
|
126
|
+
const newTypes = !primary ? [] : [primary, ...dependencies.sort()];
|
|
127
|
+
const esc = revisionConfiguration[revision].escapeTypeString;
|
|
128
|
+
return newTypes.map((dependency) => {
|
|
129
|
+
const dependencyElements = allTypes[dependency].map((t) => {
|
|
130
|
+
const targetType = t.type === "enum" && revision === Revision.ACTIVE ? t.contains : t.type;
|
|
131
|
+
const typeString = targetType.match(/^\(.*\)$/) ? `(${targetType.slice(1, -1).split(",").map((e) => e ? esc(e) : e).join(",")})` : esc(targetType);
|
|
132
|
+
return `${esc(t.name)}:${typeString}`;
|
|
133
|
+
});
|
|
134
|
+
return `${esc(dependency)}(${dependencyElements})`;
|
|
135
|
+
}).join("");
|
|
136
|
+
}
|
|
137
|
+
export function getTypeHash(types, type, revision = Revision.LEGACY) {
|
|
138
|
+
return getSelectorFromName(encodeType(types, type, revision));
|
|
139
|
+
}
|
|
140
|
+
export function encodeValue(types, type, data, ctx = {}, revision = Revision.LEGACY) {
|
|
141
|
+
if (types[type]) {
|
|
142
|
+
return [type, getStructHash(types, type, data, revision)];
|
|
143
|
+
}
|
|
144
|
+
if (revisionConfiguration[revision].presetTypes[type]) {
|
|
145
|
+
return [
|
|
146
|
+
type,
|
|
147
|
+
getStructHash(
|
|
148
|
+
revisionConfiguration[revision].presetTypes,
|
|
149
|
+
type,
|
|
150
|
+
data,
|
|
151
|
+
revision
|
|
152
|
+
)
|
|
153
|
+
];
|
|
154
|
+
}
|
|
155
|
+
if (type.endsWith("*")) {
|
|
156
|
+
const hashes = data.map(
|
|
157
|
+
(entry) => encodeValue(types, type.slice(0, -1), entry, void 0, revision)[1]
|
|
158
|
+
);
|
|
159
|
+
return [type, revisionConfiguration[revision].hashMethod(hashes)];
|
|
160
|
+
}
|
|
161
|
+
switch (type) {
|
|
162
|
+
case "enum": {
|
|
163
|
+
if (revision === Revision.ACTIVE) {
|
|
164
|
+
const [variantKey, variantData] = Object.entries(data)[0];
|
|
165
|
+
const parentType = types[ctx.parent][0];
|
|
166
|
+
const enumType = types[parentType.contains];
|
|
167
|
+
const variantType = enumType.find((t) => t.name === variantKey);
|
|
168
|
+
const variantIndex = enumType.indexOf(variantType);
|
|
169
|
+
const encodedSubtypes = variantType.type.slice(1, -1).split(",").map((subtype, index) => {
|
|
170
|
+
if (!subtype) return subtype;
|
|
171
|
+
const subtypeData = variantData[index];
|
|
172
|
+
return encodeValue(types, subtype, subtypeData, void 0, revision)[1];
|
|
173
|
+
});
|
|
174
|
+
return [
|
|
175
|
+
type,
|
|
176
|
+
revisionConfiguration[revision].hashMethod([variantIndex, ...encodedSubtypes])
|
|
177
|
+
];
|
|
178
|
+
}
|
|
179
|
+
return [type, getHex(data)];
|
|
180
|
+
}
|
|
181
|
+
case "merkletree": {
|
|
182
|
+
const merkleTreeType = getMerkleTreeType(types, ctx);
|
|
183
|
+
const structHashes = data.map((struct) => {
|
|
184
|
+
return encodeValue(types, merkleTreeType, struct, void 0, revision)[1];
|
|
185
|
+
});
|
|
186
|
+
const { root } = new MerkleTree(
|
|
187
|
+
structHashes,
|
|
188
|
+
revisionConfiguration[revision].hashMerkleMethod
|
|
189
|
+
);
|
|
190
|
+
return ["felt", root];
|
|
191
|
+
}
|
|
192
|
+
case "selector": {
|
|
193
|
+
return ["felt", prepareSelector(data)];
|
|
194
|
+
}
|
|
195
|
+
case "string": {
|
|
196
|
+
if (revision === Revision.ACTIVE) {
|
|
197
|
+
const byteArray = byteArrayFromString(data);
|
|
198
|
+
const elements = [
|
|
199
|
+
byteArray.data.length,
|
|
200
|
+
...byteArray.data,
|
|
201
|
+
byteArray.pending_word,
|
|
202
|
+
byteArray.pending_word_len
|
|
203
|
+
];
|
|
204
|
+
return [type, revisionConfiguration[revision].hashMethod(elements)];
|
|
205
|
+
}
|
|
206
|
+
return [type, getHex(data)];
|
|
207
|
+
}
|
|
208
|
+
case "i128": {
|
|
209
|
+
if (revision === Revision.ACTIVE) {
|
|
210
|
+
const value = BigInt(data);
|
|
211
|
+
assertRange(value, type, RANGE_I128);
|
|
212
|
+
return [type, getHex(value < 0n ? PRIME + value : value)];
|
|
213
|
+
}
|
|
214
|
+
return [type, getHex(data)];
|
|
215
|
+
}
|
|
216
|
+
case "timestamp":
|
|
217
|
+
case "u128": {
|
|
218
|
+
if (revision === Revision.ACTIVE) {
|
|
219
|
+
assertRange(data, type, RANGE_U128);
|
|
220
|
+
}
|
|
221
|
+
return [type, getHex(data)];
|
|
222
|
+
}
|
|
223
|
+
case "felt":
|
|
224
|
+
case "shortstring": {
|
|
225
|
+
if (revision === Revision.ACTIVE) {
|
|
226
|
+
assertRange(getHex(data), type, RANGE_FELT);
|
|
227
|
+
}
|
|
228
|
+
return [type, getHex(data)];
|
|
229
|
+
}
|
|
230
|
+
case "ClassHash":
|
|
231
|
+
case "ContractAddress": {
|
|
232
|
+
if (revision === Revision.ACTIVE) {
|
|
233
|
+
assertRange(data, type, RANGE_FELT);
|
|
234
|
+
}
|
|
235
|
+
return [type, getHex(data)];
|
|
236
|
+
}
|
|
237
|
+
case "bool": {
|
|
238
|
+
if (revision === Revision.ACTIVE) {
|
|
239
|
+
assert(typeof data === "boolean", `Type mismatch for ${type} ${data}`);
|
|
240
|
+
}
|
|
241
|
+
return [type, getHex(data)];
|
|
242
|
+
}
|
|
243
|
+
default: {
|
|
244
|
+
if (revision === Revision.ACTIVE) {
|
|
245
|
+
throw new Error(`Unsupported type: ${type}`);
|
|
246
|
+
}
|
|
247
|
+
return [type, getHex(data)];
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
export function encodeData(types, type, data, revision = Revision.LEGACY) {
|
|
252
|
+
const targetType = types[type] ?? revisionConfiguration[revision].presetTypes[type];
|
|
253
|
+
const [returnTypes, values] = targetType.reduce(
|
|
254
|
+
([ts, vs], field) => {
|
|
255
|
+
if (data[field.name] === void 0 || data[field.name] === null && field.type !== "enum") {
|
|
256
|
+
throw new Error(`Cannot encode data: missing data for '${field.name}'`);
|
|
257
|
+
}
|
|
258
|
+
const value = data[field.name];
|
|
259
|
+
const ctx = { parent: type, key: field.name };
|
|
260
|
+
const [t, encodedValue] = encodeValue(types, field.type, value, ctx, revision);
|
|
261
|
+
return [
|
|
262
|
+
[...ts, t],
|
|
263
|
+
[...vs, encodedValue]
|
|
264
|
+
];
|
|
265
|
+
},
|
|
266
|
+
[["felt"], [getTypeHash(types, type, revision)]]
|
|
267
|
+
);
|
|
268
|
+
return [returnTypes, values];
|
|
269
|
+
}
|
|
270
|
+
export function getStructHash(types, type, data, revision = Revision.LEGACY) {
|
|
271
|
+
return revisionConfiguration[revision].hashMethod(encodeData(types, type, data, revision)[1]);
|
|
272
|
+
}
|
|
273
|
+
export function getMessageHash(typedData, account) {
|
|
274
|
+
if (!validateTypedData(typedData)) {
|
|
275
|
+
throw new Error("Typed data does not match JSON schema");
|
|
276
|
+
}
|
|
277
|
+
const revision = identifyRevision(typedData);
|
|
278
|
+
const { domain, hashMethod } = revisionConfiguration[revision];
|
|
279
|
+
const message = [
|
|
280
|
+
encodeShortString("StarkNet Message"),
|
|
281
|
+
getStructHash(typedData.types, domain, typedData.domain, revision),
|
|
282
|
+
account,
|
|
283
|
+
getStructHash(typedData.types, typedData.primaryType, typedData.message, revision)
|
|
284
|
+
];
|
|
285
|
+
return hashMethod(message);
|
|
286
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
Object.defineProperty(exports, "UINT_128_MAX", {
|
|
7
|
+
enumerable: true,
|
|
8
|
+
get: function () {
|
|
9
|
+
return _uint.UINT_128_MAX;
|
|
10
|
+
}
|
|
11
|
+
});
|
|
12
|
+
Object.defineProperty(exports, "UINT_256_MAX", {
|
|
13
|
+
enumerable: true,
|
|
14
|
+
get: function () {
|
|
15
|
+
return _uint.UINT_256_MAX;
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
exports.bnToUint256 = bnToUint256;
|
|
19
|
+
exports.isUint256 = isUint256;
|
|
20
|
+
exports.uint256ToBN = uint256ToBN;
|
|
21
|
+
var _uint = require("./cairoDataTypes/uint256.cjs");
|
|
22
|
+
function uint256ToBN(uint256) {
|
|
23
|
+
return new _uint.CairoUint256(uint256).toBigInt();
|
|
24
|
+
}
|
|
25
|
+
function isUint256(bn) {
|
|
26
|
+
return _uint.CairoUint256.is(bn);
|
|
27
|
+
}
|
|
28
|
+
function bnToUint256(bn) {
|
|
29
|
+
return new _uint.CairoUint256(bn).toUint256HexString();
|
|
30
|
+
}
|