starknet 4.3.0 → 4.4.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.
Files changed (99) hide show
  1. package/CHANGELOG.md +40 -1
  2. package/__mocks__/typedDataSessionExample.json +42 -0
  3. package/__tests__/defaultProvider.test.ts +11 -24
  4. package/__tests__/rpcProvider.test.ts +32 -5
  5. package/__tests__/sequencerProvider.test.ts +9 -1
  6. package/__tests__/utils/__snapshots__/ellipticalCurve.test.ts.snap +2 -0
  7. package/__tests__/utils/ellipticalCurve.test.ts +5 -0
  8. package/__tests__/utils/merkle.test.ts +146 -0
  9. package/__tests__/utils/typedData.test.ts +107 -9
  10. package/dist/index.d.ts +1 -0
  11. package/dist/index.js +2 -1
  12. package/dist/provider/default.d.ts +2 -2
  13. package/dist/provider/default.js +3 -3
  14. package/dist/provider/interface.d.ts +6 -3
  15. package/dist/provider/rpc.d.ts +9 -4
  16. package/dist/provider/rpc.js +70 -28
  17. package/dist/provider/sequencer.d.ts +2 -2
  18. package/dist/provider/sequencer.js +4 -4
  19. package/dist/provider/utils.d.ts +12 -0
  20. package/dist/provider/utils.js +17 -1
  21. package/dist/signer/default.d.ts +1 -1
  22. package/dist/signer/default.js +1 -0
  23. package/dist/types/api/openrpc.d.ts +151 -0
  24. package/dist/types/api/openrpc.js +9 -0
  25. package/dist/types/api/rpc.d.ts +22 -43
  26. package/dist/types/provider.d.ts +5 -5
  27. package/dist/utils/ellipticCurve.d.ts +13 -0
  28. package/dist/utils/ellipticCurve.js +20 -16
  29. package/dist/utils/hash.js +8 -6
  30. package/dist/utils/merkle.d.ts +10 -0
  31. package/dist/utils/merkle.js +90 -0
  32. package/dist/utils/number.d.ts +1 -0
  33. package/dist/utils/number.js +6 -2
  34. package/dist/utils/responseParser/rpc.d.ts +13 -3
  35. package/dist/utils/responseParser/rpc.js +2 -10
  36. package/dist/utils/responseParser/sequencer.d.ts +4 -1
  37. package/dist/utils/responseParser/sequencer.js +1 -7
  38. package/dist/utils/typedData/index.d.ts +23 -8
  39. package/dist/utils/typedData/index.js +70 -31
  40. package/dist/utils/typedData/types.d.ts +8 -3
  41. package/index.d.ts +1 -0
  42. package/index.js +2 -1
  43. package/package.json +1 -1
  44. package/provider/default.d.ts +2 -2
  45. package/provider/default.js +3 -3
  46. package/provider/interface.d.ts +6 -3
  47. package/provider/rpc.d.ts +9 -4
  48. package/provider/rpc.js +70 -28
  49. package/provider/sequencer.d.ts +2 -2
  50. package/provider/sequencer.js +4 -4
  51. package/provider/utils.d.ts +12 -0
  52. package/provider/utils.js +17 -1
  53. package/signer/default.d.ts +1 -1
  54. package/signer/default.js +1 -0
  55. package/src/index.ts +1 -0
  56. package/src/provider/default.ts +2 -3
  57. package/src/provider/interface.ts +5 -3
  58. package/src/provider/rpc.ts +59 -32
  59. package/src/provider/sequencer.ts +3 -6
  60. package/src/provider/utils.ts +22 -1
  61. package/src/signer/default.ts +2 -2
  62. package/src/types/api/openrpc.ts +168 -0
  63. package/src/types/api/rpc.ts +22 -45
  64. package/src/types/provider.ts +5 -5
  65. package/src/utils/ellipticCurve.ts +20 -16
  66. package/src/utils/hash.ts +8 -6
  67. package/src/utils/merkle.ts +70 -0
  68. package/src/utils/number.ts +5 -1
  69. package/src/utils/responseParser/rpc.ts +16 -13
  70. package/src/utils/responseParser/sequencer.ts +5 -8
  71. package/src/utils/typedData/index.ts +88 -34
  72. package/src/utils/typedData/types.ts +12 -4
  73. package/types/api/openrpc.d.ts +151 -0
  74. package/types/api/openrpc.js +9 -0
  75. package/types/api/rpc.d.ts +22 -43
  76. package/types/provider.d.ts +5 -5
  77. package/utils/ellipticCurve.d.ts +13 -0
  78. package/utils/ellipticCurve.js +20 -16
  79. package/utils/hash.js +8 -6
  80. package/utils/merkle.d.ts +10 -0
  81. package/utils/merkle.js +90 -0
  82. package/utils/number.d.ts +1 -0
  83. package/utils/number.js +6 -2
  84. package/utils/responseParser/rpc.d.ts +13 -3
  85. package/utils/responseParser/rpc.js +2 -10
  86. package/utils/responseParser/sequencer.d.ts +4 -1
  87. package/utils/responseParser/sequencer.js +1 -7
  88. package/utils/typedData/index.d.ts +23 -8
  89. package/utils/typedData/index.js +70 -31
  90. package/utils/typedData/types.d.ts +8 -3
  91. package/www/docs/API/account.md +20 -18
  92. package/www/docs/API/contract.md +10 -10
  93. package/www/docs/API/contractFactory.md +14 -11
  94. package/www/docs/API/provider.md +29 -63
  95. package/www/docs/API/signer.md +8 -10
  96. package/www/docs/API/utils.md +157 -74
  97. package/www/guides/account.md +12 -12
  98. package/www/guides/erc20.md +19 -4
  99. package/www/guides/intro.md +3 -1
@@ -39,8 +39,9 @@ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
39
39
  return to.concat(ar || Array.prototype.slice.call(from));
40
40
  };
41
41
  Object.defineProperty(exports, "__esModule", { value: true });
42
- exports.getMessageHash = exports.getStructHash = exports.encodeData = exports.getTypeHash = exports.encodeType = exports.getDependencies = void 0;
42
+ exports.getMessageHash = exports.getStructHash = exports.encodeData = exports.encodeValue = exports.getTypeHash = exports.encodeType = exports.getDependencies = exports.isMerkleTreeType = exports.prepareSelector = void 0;
43
43
  var hash_1 = require("../hash");
44
+ var merkle_1 = require("../merkle");
44
45
  var number_1 = require("../number");
45
46
  var shortString_1 = require("../shortString");
46
47
  var utils_1 = require("./utils");
@@ -56,6 +57,14 @@ function getHex(value) {
56
57
  throw new Error("Invalid BigNumberish: ".concat(value));
57
58
  }
58
59
  }
60
+ function prepareSelector(selector) {
61
+ return (0, number_1.isHex)(selector) ? selector : (0, hash_1.getSelectorFromName)(selector);
62
+ }
63
+ exports.prepareSelector = prepareSelector;
64
+ function isMerkleTreeType(type) {
65
+ return type.type === 'merkletree';
66
+ }
67
+ exports.isMerkleTreeType = isMerkleTreeType;
59
68
  /**
60
69
  * Get the dependencies of a struct type. If a struct has the same dependency multiple times, it's only included once
61
70
  * in the resulting array.
@@ -65,12 +74,8 @@ function getHex(value) {
65
74
  * @param {string[]} [dependencies]
66
75
  * @return {string[]}
67
76
  */
68
- var getDependencies = function (typedData, type, dependencies) {
77
+ var getDependencies = function (types, type, dependencies) {
69
78
  if (dependencies === void 0) { dependencies = []; }
70
- // `getDependencies` is called by most other functions, so we validate the JSON schema here
71
- if (!(0, utils_1.validateTypedData)(typedData)) {
72
- throw new Error('Typed data does not match JSON schema');
73
- }
74
79
  // Include pointers (struct arrays)
75
80
  if (type[type.length - 1] === '*') {
76
81
  // eslint-disable-next-line no-param-reassign
@@ -79,14 +84,29 @@ var getDependencies = function (typedData, type, dependencies) {
79
84
  if (dependencies.includes(type)) {
80
85
  return dependencies;
81
86
  }
82
- if (!typedData.types[type]) {
87
+ if (!types[type]) {
83
88
  return dependencies;
84
89
  }
85
90
  return __spreadArray([
86
91
  type
87
- ], __read(typedData.types[type].reduce(function (previous, t) { return __spreadArray(__spreadArray([], __read(previous), false), __read((0, exports.getDependencies)(typedData, t.type, previous).filter(function (dependency) { return !previous.includes(dependency); })), false); }, [])), false);
92
+ ], __read(types[type].reduce(function (previous, t) { return __spreadArray(__spreadArray([], __read(previous), false), __read((0, exports.getDependencies)(types, t.type, previous).filter(function (dependency) { return !previous.includes(dependency); })), false); }, [])), false);
88
93
  };
89
94
  exports.getDependencies = getDependencies;
95
+ function getMerkleTreeType(types, ctx) {
96
+ if (ctx.parent && ctx.key) {
97
+ var parentType = types[ctx.parent];
98
+ var merkleType = parentType.find(function (t) { return t.name === ctx.key; });
99
+ var isMerkleTree = isMerkleTreeType(merkleType);
100
+ if (!isMerkleTree) {
101
+ throw new Error("".concat(ctx.key, " is not a merkle tree"));
102
+ }
103
+ if (merkleType.contains.endsWith('*')) {
104
+ throw new Error("Merkle tree contain property must not be an array but was given ".concat(ctx.key));
105
+ }
106
+ return merkleType.contains;
107
+ }
108
+ return 'raw';
109
+ }
90
110
  /**
91
111
  * Encode a type to a string. All dependant types are alphabetically sorted.
92
112
  *
@@ -94,12 +114,12 @@ exports.getDependencies = getDependencies;
94
114
  * @param {string} type
95
115
  * @return {string}
96
116
  */
97
- var encodeType = function (typedData, type) {
98
- var _a = __read((0, exports.getDependencies)(typedData, type)), primary = _a[0], dependencies = _a.slice(1);
99
- var types = __spreadArray([primary], __read(dependencies.sort()), false);
100
- return types
117
+ var encodeType = function (types, type) {
118
+ var _a = __read((0, exports.getDependencies)(types, type)), primary = _a[0], dependencies = _a.slice(1);
119
+ var newTypes = !primary ? [] : __spreadArray([primary], __read(dependencies.sort()), false);
120
+ return newTypes
101
121
  .map(function (dependency) {
102
- return "".concat(dependency, "(").concat(typedData.types[dependency].map(function (t) { return "".concat(t.name, ":").concat(t.type); }), ")");
122
+ return "".concat(dependency, "(").concat(types[dependency].map(function (t) { return "".concat(t.name, ":").concat(t.type); }), ")");
103
123
  })
104
124
  .join('');
105
125
  };
@@ -111,8 +131,8 @@ exports.encodeType = encodeType;
111
131
  * @param {string} type
112
132
  * @return {string}
113
133
  */
114
- var getTypeHash = function (typedData, type) {
115
- return (0, hash_1.getSelectorFromName)((0, exports.encodeType)(typedData, type));
134
+ var getTypeHash = function (types, type) {
135
+ return (0, hash_1.getSelectorFromName)((0, exports.encodeType)(types, type));
116
136
  };
117
137
  exports.getTypeHash = getTypeHash;
118
138
  /**
@@ -124,25 +144,39 @@ exports.getTypeHash = getTypeHash;
124
144
  * @param {any} data
125
145
  * @returns {[string, string]}
126
146
  */
127
- var encodeValue = function (typedData, type, data) {
128
- if (typedData.types[type]) {
147
+ var encodeValue = function (types, type, data, ctx) {
148
+ if (ctx === void 0) { ctx = {}; }
149
+ if (types[type]) {
129
150
  // eslint-disable-next-line @typescript-eslint/no-use-before-define
130
- return [type, (0, exports.getStructHash)(typedData, type, data)];
151
+ return [type, (0, exports.getStructHash)(types, type, data)];
131
152
  }
132
- if (Object.keys(typedData.types)
153
+ if (Object.keys(types)
133
154
  .map(function (x) { return "".concat(x, "*"); })
134
155
  .includes(type)) {
135
156
  var structHashes = data.map(function (struct) {
136
157
  // eslint-disable-next-line @typescript-eslint/no-use-before-define
137
- return (0, exports.getStructHash)(typedData, type.slice(0, -1), struct);
158
+ return (0, exports.getStructHash)(types, type.slice(0, -1), struct);
138
159
  });
139
160
  return [type, (0, hash_1.computeHashOnElements)(structHashes)];
140
161
  }
162
+ if (type === 'merkletree') {
163
+ var merkleTreeType_1 = getMerkleTreeType(types, ctx);
164
+ var structHashes = data.map(function (struct) {
165
+ // eslint-disable-next-line @typescript-eslint/no-use-before-define
166
+ return (0, exports.encodeValue)(types, merkleTreeType_1, struct)[1];
167
+ });
168
+ var root = new merkle_1.MerkleTree(structHashes).root;
169
+ return ['felt', root];
170
+ }
141
171
  if (type === 'felt*') {
142
172
  return ['felt*', (0, hash_1.computeHashOnElements)(data)];
143
173
  }
174
+ if (type === 'selector') {
175
+ return ['felt', prepareSelector(data)];
176
+ }
144
177
  return [type, getHex(data)];
145
178
  };
179
+ exports.encodeValue = encodeValue;
146
180
  /**
147
181
  * Encode the data to an ABI encoded Buffer. The data should be a key -> value object with all the required values. All
148
182
  * dependant types are automatically encoded.
@@ -151,20 +185,23 @@ var encodeValue = function (typedData, type, data) {
151
185
  * @param {string} type
152
186
  * @param {Record<string, any>} data
153
187
  */
154
- var encodeData = function (typedData, type, data) {
155
- var _a = __read(typedData.types[type].reduce(function (_a, field) {
188
+ var encodeData = function (types, type, data) {
189
+ var _a = __read(types[type].reduce(function (_a, field) {
156
190
  var _b = __read(_a, 2), ts = _b[0], vs = _b[1];
157
191
  if (data[field.name] === undefined || data[field.name] === null) {
158
192
  throw new Error("Cannot encode data: missing data for '".concat(field.name, "'"));
159
193
  }
160
194
  var value = data[field.name];
161
- var _c = __read(encodeValue(typedData, field.type, value), 2), t = _c[0], encodedValue = _c[1];
195
+ var _c = __read((0, exports.encodeValue)(types, field.type, value, {
196
+ parent: type,
197
+ key: field.name,
198
+ }), 2), t = _c[0], encodedValue = _c[1];
162
199
  return [
163
200
  __spreadArray(__spreadArray([], __read(ts), false), [t], false),
164
201
  __spreadArray(__spreadArray([], __read(vs), false), [encodedValue], false),
165
202
  ];
166
- }, [['felt'], [(0, exports.getTypeHash)(typedData, type)]]), 2), types = _a[0], values = _a[1];
167
- return [types, values];
203
+ }, [['felt'], [(0, exports.getTypeHash)(types, type)]]), 2), returnTypes = _a[0], values = _a[1];
204
+ return [returnTypes, values];
168
205
  };
169
206
  exports.encodeData = encodeData;
170
207
  /**
@@ -176,24 +213,26 @@ exports.encodeData = encodeData;
176
213
  * @param {Record<string, any>} data
177
214
  * @return {Buffer}
178
215
  */
179
- var getStructHash = function (typedData, type, data) {
180
- return (0, hash_1.computeHashOnElements)((0, exports.encodeData)(typedData, type, data)[1]);
216
+ var getStructHash = function (types, type, data) {
217
+ return (0, hash_1.computeHashOnElements)((0, exports.encodeData)(types, type, data)[1]);
181
218
  };
182
219
  exports.getStructHash = getStructHash;
183
220
  /**
184
- * Get the EIP-191 encoded message to sign, from the typedData object. If `hash` is enabled, the message will be hashed
185
- * with Keccak256.
221
+ * Get the EIP-191 encoded message to sign, from the typedData object.
186
222
  *
187
223
  * @param {TypedData} typedData
188
224
  * @param {BigNumberish} account
189
225
  * @return {string}
190
226
  */
191
227
  var getMessageHash = function (typedData, account) {
228
+ if (!(0, utils_1.validateTypedData)(typedData)) {
229
+ throw new Error('Typed data does not match JSON schema');
230
+ }
192
231
  var message = [
193
232
  (0, shortString_1.encodeShortString)('StarkNet Message'),
194
- (0, exports.getStructHash)(typedData, 'StarkNetDomain', typedData.domain),
233
+ (0, exports.getStructHash)(typedData.types, 'StarkNetDomain', typedData.domain),
195
234
  account,
196
- (0, exports.getStructHash)(typedData, typedData.primaryType, typedData.message),
235
+ (0, exports.getStructHash)(typedData.types, typedData.primaryType, typedData.message),
197
236
  ];
198
237
  return (0, hash_1.computeHashOnElements)(message);
199
238
  };
@@ -1,13 +1,18 @@
1
+ export declare type StarkNetMerkleType = {
2
+ name: string;
3
+ type: 'merkletree';
4
+ contains: string;
5
+ };
1
6
  /**
2
7
  * A single type, as part of a struct. The `type` field can be any of the EIP-712 supported types.
3
8
  *
4
9
  * Note that the `uint` and `int` aliases like in Solidity, and fixed point numbers are not supported by the EIP-712
5
10
  * standard.
6
11
  */
7
- export interface StarkNetType {
12
+ export declare type StarkNetType = {
8
13
  name: string;
9
- type: 'felt' | 'felt*' | string;
10
- }
14
+ type: string;
15
+ } | StarkNetMerkleType;
11
16
  /**
12
17
  * The EIP712 domain struct. Any of these fields are optional, but it must contain at least one field.
13
18
  */
package/index.d.ts CHANGED
@@ -16,6 +16,7 @@ export * as json from './utils/json';
16
16
  export * as number from './utils/number';
17
17
  export * as transaction from './utils/transaction';
18
18
  export * as stark from './utils/stark';
19
+ export * as merkle from './utils/merkle';
19
20
  export * as ec from './utils/ellipticCurve';
20
21
  export * as uint256 from './utils/uint256';
21
22
  export * as shortString from './utils/shortString';
package/index.js CHANGED
@@ -26,7 +26,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
26
26
  return result;
27
27
  };
28
28
  Object.defineProperty(exports, "__esModule", { value: true });
29
- exports.typedData = exports.shortString = exports.uint256 = exports.ec = exports.stark = exports.transaction = exports.number = exports.json = exports.hash = exports.encode = exports.constants = void 0;
29
+ exports.typedData = exports.shortString = exports.uint256 = exports.ec = exports.merkle = exports.stark = exports.transaction = exports.number = exports.json = exports.hash = exports.encode = exports.constants = void 0;
30
30
  /**
31
31
  * Main
32
32
  */
@@ -45,6 +45,7 @@ exports.json = __importStar(require("./utils/json"));
45
45
  exports.number = __importStar(require("./utils/number"));
46
46
  exports.transaction = __importStar(require("./utils/transaction"));
47
47
  exports.stark = __importStar(require("./utils/stark"));
48
+ exports.merkle = __importStar(require("./utils/merkle"));
48
49
  exports.ec = __importStar(require("./utils/ellipticCurve"));
49
50
  exports.uint256 = __importStar(require("./utils/uint256"));
50
51
  exports.shortString = __importStar(require("./utils/shortString"));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "starknet",
3
- "version": "4.3.0",
3
+ "version": "4.4.1",
4
4
  "description": "JavaScript library for StarkNet",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -1,5 +1,5 @@
1
1
  import { StarknetChainId } from '../constants';
2
- import { BlockTag, Call, CallContractResponse, ContractClass, DeclareContractPayload, DeclareContractResponse, DeployContractPayload, DeployContractResponse, EstimateFeeResponse, GetBlockResponse, GetCodeResponse, GetTransactionReceiptResponse, GetTransactionResponse, Invocation, InvocationsDetails, InvokeFunctionResponse } from '../types';
2
+ import { Call, CallContractResponse, ContractClass, DeclareContractPayload, DeclareContractResponse, DeployContractPayload, DeployContractResponse, EstimateFeeResponse, GetBlockResponse, GetCodeResponse, GetTransactionReceiptResponse, GetTransactionResponse, Invocation, InvocationsDetails, InvokeFunctionResponse } from '../types';
3
3
  import { BigNumberish } from '../utils/number';
4
4
  import { ProviderInterface } from './interface';
5
5
  import { RpcProviderOptions } from './rpc';
@@ -16,7 +16,7 @@ export declare class Provider implements ProviderInterface {
16
16
  getBlock(blockIdentifier?: BlockIdentifier): Promise<GetBlockResponse>;
17
17
  getClassAt(contractAddress: string, blockIdentifier?: BlockIdentifier): Promise<ContractClass>;
18
18
  getEstimateFee(invocation: Invocation, blockIdentifier?: BlockIdentifier, invocationDetails?: InvocationsDetails): Promise<EstimateFeeResponse>;
19
- getStorageAt(contractAddress: string, key: BigNumberish, blockTagOrHash?: BlockTag | BigNumberish): Promise<BigNumberish>;
19
+ getStorageAt(contractAddress: string, key: BigNumberish, blockIdentifier?: BlockIdentifier): Promise<BigNumberish>;
20
20
  getTransaction(txHash: BigNumberish): Promise<GetTransactionResponse>;
21
21
  getTransactionReceipt(txHash: BigNumberish): Promise<GetTransactionReceiptResponse>;
22
22
  callContract(request: Call, blockIdentifier?: BlockIdentifier): Promise<CallContractResponse>;
@@ -86,11 +86,11 @@ var Provider = /** @class */ (function () {
86
86
  });
87
87
  });
88
88
  };
89
- Provider.prototype.getStorageAt = function (contractAddress, key, blockTagOrHash) {
90
- if (blockTagOrHash === void 0) { blockTagOrHash = 'pending'; }
89
+ Provider.prototype.getStorageAt = function (contractAddress, key, blockIdentifier) {
90
+ if (blockIdentifier === void 0) { blockIdentifier = 'pending'; }
91
91
  return __awaiter(this, void 0, void 0, function () {
92
92
  return __generator(this, function (_a) {
93
- return [2 /*return*/, this.provider.getStorageAt(contractAddress, key, blockTagOrHash)];
93
+ return [2 /*return*/, this.provider.getStorageAt(contractAddress, key, blockIdentifier)];
94
94
  });
95
95
  });
96
96
  };
@@ -1,5 +1,5 @@
1
1
  import { StarknetChainId } from '../constants';
2
- import type { BlockTag, Call, CallContractResponse, ContractClass, DeclareContractPayload, DeclareContractResponse, DeployContractPayload, DeployContractResponse, EstimateFeeResponse, GetBlockResponse, GetCodeResponse, GetTransactionReceiptResponse, GetTransactionResponse, Invocation, InvocationsDetails, InvokeFunctionResponse } from '../types';
2
+ import type { Call, CallContractResponse, ContractClass, DeclareContractPayload, DeclareContractResponse, DeployContractPayload, DeployContractResponse, EstimateFeeResponse, GetBlockResponse, GetCodeResponse, GetTransactionReceiptResponse, GetTransactionResponse, Invocation, InvocationsDetails, InvokeFunctionResponse } from '../types';
3
3
  import type { BigNumberish } from '../utils/number';
4
4
  import { BlockIdentifier } from './utils';
5
5
  export declare abstract class ProviderInterface {
@@ -19,6 +19,9 @@ export declare abstract class ProviderInterface {
19
19
  * @returns the block object
20
20
  */
21
21
  abstract getBlock(blockIdentifier: BlockIdentifier): Promise<GetBlockResponse>;
22
+ /**
23
+ * @deprecated The method should not be used
24
+ */
22
25
  abstract getCode(contractAddress: string, blockIdentifier?: BlockIdentifier): Promise<GetCodeResponse>;
23
26
  /**
24
27
  * Gets the contract class of the deployed contract.
@@ -33,10 +36,10 @@ export declare abstract class ProviderInterface {
33
36
  *
34
37
  * @param contractAddress
35
38
  * @param key - from getStorageVarAddress('<STORAGE_VARIABLE_NAME>') (WIP)
36
- * @param blockHashOrTag - block hash or tag (pending, latest)
39
+ * @param blockIdentifier - block identifier
37
40
  * @returns the value of the storage variable
38
41
  */
39
- abstract getStorageAt(contractAddress: string, key: BigNumberish, blockHashOrTag?: BlockTag | BigNumberish): Promise<BigNumberish>;
42
+ abstract getStorageAt(contractAddress: string, key: BigNumberish, blockIdentifier: BlockIdentifier): Promise<BigNumberish>;
40
43
  /**
41
44
  * Gets the transaction information from a tx id.
42
45
  *
package/provider/rpc.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { StarknetChainId } from '../constants';
2
- import { BlockTag, Call, CallContractResponse, DeclareContractPayload, DeclareContractResponse, DeployContractPayload, DeployContractResponse, EstimateFeeResponse, GetBlockResponse, GetTransactionReceiptResponse, GetTransactionResponse, Invocation, InvocationsDetails, InvokeFunctionResponse } from '../types';
2
+ import { Call, CallContractResponse, DeclareContractPayload, DeclareContractResponse, DeployContractPayload, DeployContractResponse, EstimateFeeResponse, GetBlockResponse, GetCodeResponse, GetTransactionReceiptResponse, GetTransactionResponse, Invocation, InvocationsDetails, InvokeFunctionResponse } from '../types';
3
3
  import { RPC } from '../types/api';
4
4
  import { BigNumberish } from '../utils/number';
5
5
  import { ProviderInterface } from './interface';
@@ -15,16 +15,21 @@ export declare class RpcProvider implements ProviderInterface {
15
15
  protected fetchEndpoint<T extends keyof RPC.Methods>(method: T, request?: RPC.Methods[T]['REQUEST']): Promise<RPC.Methods[T]['RESPONSE']>;
16
16
  getChainId(): Promise<StarknetChainId>;
17
17
  getBlock(blockIdentifier?: BlockIdentifier): Promise<GetBlockResponse>;
18
- getStorageAt(contractAddress: string, key: BigNumberish, blockHashOrTag?: BlockTag | BigNumberish): Promise<BigNumberish>;
18
+ getBlockWithTxHashes(blockIdentifier?: BlockIdentifier): Promise<RPC.GetBlockWithTxHashesResponse>;
19
+ getBlockWithTxs(blockIdentifier?: BlockIdentifier): Promise<RPC.GetBlockWithTxs>;
20
+ getNonce(contractAddress: string): Promise<any>;
21
+ getStorageAt(contractAddress: string, key: BigNumberish, blockIdentifier?: BlockIdentifier): Promise<BigNumberish>;
19
22
  getTransaction(txHash: BigNumberish): Promise<GetTransactionResponse>;
23
+ getTransactionByHash(txHash: BigNumberish): Promise<RPC.GetTransactionByHashResponse>;
24
+ getTransactionByBlockIdAndIndex(blockIdentifier: BlockIdentifier, index: number): Promise<RPC.GetTransactionByBlockIdAndIndex>;
20
25
  getTransactionReceipt(txHash: BigNumberish): Promise<GetTransactionReceiptResponse>;
21
- getClassAt(contractAddress: string, _blockIdentifier?: BlockIdentifier): Promise<any>;
26
+ getClassAt(contractAddress: string, blockIdentifier: BlockIdentifier): Promise<any>;
22
27
  getEstimateFee(invocation: Invocation, blockIdentifier?: BlockIdentifier, invocationDetails?: InvocationsDetails): Promise<EstimateFeeResponse>;
23
28
  declareContract({ contract, version, }: DeclareContractPayload): Promise<DeclareContractResponse>;
24
29
  deployContract({ contract, constructorCalldata, addressSalt, }: DeployContractPayload): Promise<DeployContractResponse>;
25
30
  invokeFunction(functionInvocation: Invocation, details: InvocationsDetails): Promise<InvokeFunctionResponse>;
26
31
  callContract(call: Call, blockIdentifier?: BlockIdentifier): Promise<CallContractResponse>;
27
- getCode(contractAddress: string, _blockIdentifier?: BlockIdentifier): Promise<RPC.GetCodeResponse>;
32
+ getCode(_contractAddress: string, _blockIdentifier?: BlockIdentifier): Promise<GetCodeResponse>;
28
33
  waitForTransaction(txHash: BigNumberish, retryInterval?: number): Promise<void>;
29
34
  /**
30
35
  * Gets the transaction count from a block.
package/provider/rpc.js CHANGED
@@ -47,6 +47,7 @@ var number_1 = require("../utils/number");
47
47
  var provider_1 = require("../utils/provider");
48
48
  var rpc_1 = require("../utils/responseParser/rpc");
49
49
  var stark_1 = require("../utils/stark");
50
+ var utils_1 = require("./utils");
50
51
  var RpcProvider = /** @class */ (function () {
51
52
  function RpcProvider(optionsOrProvider) {
52
53
  var _this = this;
@@ -112,36 +113,78 @@ var RpcProvider = /** @class */ (function () {
112
113
  });
113
114
  });
114
115
  };
116
+ // Common Interface
115
117
  RpcProvider.prototype.getBlock = function (blockIdentifier) {
116
118
  if (blockIdentifier === void 0) { blockIdentifier = 'pending'; }
117
119
  return __awaiter(this, void 0, void 0, function () {
118
- var method;
119
120
  return __generator(this, function (_a) {
120
- method = typeof blockIdentifier === 'string' && (0, number_1.isHex)(blockIdentifier)
121
- ? 'starknet_getBlockByHash'
122
- : 'starknet_getBlockByNumber';
123
- return [2 /*return*/, this.fetchEndpoint(method, [blockIdentifier]).then(this.responseParser.parseGetBlockResponse)];
121
+ return [2 /*return*/, this.getBlockWithTxHashes(blockIdentifier).then(this.responseParser.parseGetBlockResponse)];
124
122
  });
125
123
  });
126
124
  };
127
- RpcProvider.prototype.getStorageAt = function (contractAddress, key, blockHashOrTag) {
128
- if (blockHashOrTag === void 0) { blockHashOrTag = 'pending'; }
125
+ RpcProvider.prototype.getBlockWithTxHashes = function (blockIdentifier) {
126
+ if (blockIdentifier === void 0) { blockIdentifier = 'pending'; }
127
+ return __awaiter(this, void 0, void 0, function () {
128
+ var blockIdentifierGetter;
129
+ return __generator(this, function (_a) {
130
+ blockIdentifierGetter = new utils_1.BlockIdentifierClass(blockIdentifier);
131
+ return [2 /*return*/, this.fetchEndpoint('starknet_getBlockWithTxHashes', [
132
+ blockIdentifierGetter.getIdentifier(),
133
+ ])];
134
+ });
135
+ });
136
+ };
137
+ RpcProvider.prototype.getBlockWithTxs = function (blockIdentifier) {
138
+ if (blockIdentifier === void 0) { blockIdentifier = 'pending'; }
139
+ return __awaiter(this, void 0, void 0, function () {
140
+ var blockIdentifierGetter;
141
+ return __generator(this, function (_a) {
142
+ blockIdentifierGetter = new utils_1.BlockIdentifierClass(blockIdentifier);
143
+ return [2 /*return*/, this.fetchEndpoint('starknet_getBlockWithTxs', [blockIdentifierGetter.getIdentifier()])];
144
+ });
145
+ });
146
+ };
147
+ RpcProvider.prototype.getNonce = function (contractAddress) {
148
+ return __awaiter(this, void 0, void 0, function () {
149
+ return __generator(this, function (_a) {
150
+ return [2 /*return*/, this.fetchEndpoint('starknet_getNonce', [contractAddress])];
151
+ });
152
+ });
153
+ };
154
+ RpcProvider.prototype.getStorageAt = function (contractAddress, key, blockIdentifier) {
155
+ if (blockIdentifier === void 0) { blockIdentifier = 'pending'; }
129
156
  return __awaiter(this, void 0, void 0, function () {
130
- var parsedKey;
157
+ var parsedKey, blockIdentifierGetter;
131
158
  return __generator(this, function (_a) {
132
159
  parsedKey = (0, number_1.toHex)((0, number_1.toBN)(key));
160
+ blockIdentifierGetter = new utils_1.BlockIdentifierClass(blockIdentifier);
133
161
  return [2 /*return*/, this.fetchEndpoint('starknet_getStorageAt', [
134
162
  contractAddress,
135
163
  parsedKey,
136
- blockHashOrTag,
164
+ blockIdentifierGetter.getIdentifier(),
137
165
  ])];
138
166
  });
139
167
  });
140
168
  };
169
+ // common interface
141
170
  RpcProvider.prototype.getTransaction = function (txHash) {
142
171
  return __awaiter(this, void 0, void 0, function () {
143
172
  return __generator(this, function (_a) {
144
- return [2 /*return*/, this.fetchEndpoint('starknet_getTransactionByHash', [txHash]).then(this.responseParser.parseGetTransactionResponse)];
173
+ return [2 /*return*/, this.getTransactionByHash(txHash).then(this.responseParser.parseGetTransactionResponse)];
174
+ });
175
+ });
176
+ };
177
+ RpcProvider.prototype.getTransactionByHash = function (txHash) {
178
+ return __awaiter(this, void 0, void 0, function () {
179
+ return __generator(this, function (_a) {
180
+ return [2 /*return*/, this.fetchEndpoint('starknet_getTransactionByHash', [txHash])];
181
+ });
182
+ });
183
+ };
184
+ RpcProvider.prototype.getTransactionByBlockIdAndIndex = function (blockIdentifier, index) {
185
+ return __awaiter(this, void 0, void 0, function () {
186
+ return __generator(this, function (_a) {
187
+ return [2 /*return*/, this.fetchEndpoint('starknet_getTransactionByHash', [blockIdentifier, index])];
145
188
  });
146
189
  });
147
190
  };
@@ -152,11 +195,15 @@ var RpcProvider = /** @class */ (function () {
152
195
  });
153
196
  });
154
197
  };
155
- RpcProvider.prototype.getClassAt = function (contractAddress, _blockIdentifier) {
156
- if (_blockIdentifier === void 0) { _blockIdentifier = 'pending'; }
198
+ RpcProvider.prototype.getClassAt = function (contractAddress, blockIdentifier) {
157
199
  return __awaiter(this, void 0, void 0, function () {
200
+ var blockIdentifierGetter;
158
201
  return __generator(this, function (_a) {
159
- return [2 /*return*/, this.fetchEndpoint('starknet_getClassAt', [contractAddress])];
202
+ blockIdentifierGetter = new utils_1.BlockIdentifierClass(blockIdentifier);
203
+ return [2 /*return*/, this.fetchEndpoint('starknet_getClassAt', [
204
+ blockIdentifierGetter.getIdentifier(),
205
+ contractAddress,
206
+ ])];
160
207
  });
161
208
  });
162
209
  };
@@ -170,7 +217,7 @@ var RpcProvider = /** @class */ (function () {
170
217
  contract_address: invocation.contractAddress,
171
218
  entry_point_selector: (0, hash_1.getSelectorFromName)(invocation.entrypoint),
172
219
  calldata: (0, provider_1.parseCalldata)(invocation.calldata),
173
- signature: (0, number_1.bigNumberishArrayToDecimalStringArray)(invocation.signature || []),
220
+ signature: (0, number_1.bigNumberishArrayToHexadecimalStringArray)(invocation.signature || []),
174
221
  version: (0, number_1.toHex)((0, number_1.toBN)((invocationDetails === null || invocationDetails === void 0 ? void 0 : invocationDetails.version) || 0)),
175
222
  },
176
223
  blockIdentifier,
@@ -202,7 +249,7 @@ var RpcProvider = /** @class */ (function () {
202
249
  contractDefinition = (0, provider_1.parseContract)(contract);
203
250
  return [2 /*return*/, this.fetchEndpoint('starknet_addDeployTransaction', [
204
251
  addressSalt !== null && addressSalt !== void 0 ? addressSalt : (0, stark_1.randomAddress)(),
205
- (0, number_1.bigNumberishArrayToDecimalStringArray)(constructorCalldata !== null && constructorCalldata !== void 0 ? constructorCalldata : []),
252
+ (0, number_1.bigNumberishArrayToHexadecimalStringArray)(constructorCalldata !== null && constructorCalldata !== void 0 ? constructorCalldata : []),
206
253
  {
207
254
  program: contractDefinition.program,
208
255
  entry_points_by_type: contractDefinition.entry_points_by_type,
@@ -220,7 +267,7 @@ var RpcProvider = /** @class */ (function () {
220
267
  entry_point_selector: (0, hash_1.getSelectorFromName)(functionInvocation.entrypoint),
221
268
  calldata: (0, provider_1.parseCalldata)(functionInvocation.calldata),
222
269
  },
223
- (0, number_1.bigNumberishArrayToDecimalStringArray)(functionInvocation.signature || []),
270
+ (0, number_1.bigNumberishArrayToHexadecimalStringArray)(functionInvocation.signature || []),
224
271
  (0, number_1.toHex)((0, number_1.toBN)(details.maxFee || 0)),
225
272
  (0, number_1.toHex)((0, number_1.toBN)(details.version || 0)),
226
273
  ]).then(this.responseParser.parseInvokeFunctionResponse)];
@@ -248,16 +295,10 @@ var RpcProvider = /** @class */ (function () {
248
295
  });
249
296
  });
250
297
  };
251
- RpcProvider.prototype.getCode = function (contractAddress, _blockIdentifier) {
298
+ RpcProvider.prototype.getCode = function (_contractAddress, _blockIdentifier) {
252
299
  return __awaiter(this, void 0, void 0, function () {
253
- var result;
254
300
  return __generator(this, function (_a) {
255
- switch (_a.label) {
256
- case 0: return [4 /*yield*/, this.fetchEndpoint('starknet_getCode', [contractAddress])];
257
- case 1:
258
- result = _a.sent();
259
- return [2 /*return*/, this.responseParser.parseGetCodeResponse(result)];
260
- }
301
+ throw new Error('RPC 0.1.0 does not implement getCode function');
261
302
  });
262
303
  });
263
304
  };
@@ -325,11 +366,12 @@ var RpcProvider = /** @class */ (function () {
325
366
  */
326
367
  RpcProvider.prototype.getTransactionCount = function (blockIdentifier) {
327
368
  return __awaiter(this, void 0, void 0, function () {
369
+ var blockIdentifierGetter;
328
370
  return __generator(this, function (_a) {
329
- if (typeof blockIdentifier === 'number') {
330
- return [2 /*return*/, this.fetchEndpoint('starknet_getBlockTransactionCountByNumber', [blockIdentifier])];
331
- }
332
- return [2 /*return*/, this.fetchEndpoint('starknet_getBlockTransactionCountByHash', [blockIdentifier])];
371
+ blockIdentifierGetter = new utils_1.BlockIdentifierClass(blockIdentifier);
372
+ return [2 /*return*/, this.fetchEndpoint('starknet_getBlockTransactionCount', [
373
+ blockIdentifierGetter.getIdentifier(),
374
+ ])];
333
375
  });
334
376
  });
335
377
  };
@@ -1,5 +1,5 @@
1
1
  import { StarknetChainId } from '../constants';
2
- import { BlockTag, Call, CallContractResponse, ContractClass, DeclareContractPayload, DeclareContractResponse, DeployContractPayload, DeployContractResponse, EstimateFeeResponse, GetBlockResponse, GetTransactionReceiptResponse, GetTransactionResponse, Invocation, InvocationsDetails, InvokeFunctionResponse } from '../types';
2
+ import { Call, CallContractResponse, ContractClass, DeclareContractPayload, DeclareContractResponse, DeployContractPayload, DeployContractResponse, EstimateFeeResponse, GetBlockResponse, GetTransactionReceiptResponse, GetTransactionResponse, Invocation, InvocationsDetails, InvokeFunctionResponse } from '../types';
3
3
  import { GetContractAddressesResponse, GetTransactionStatusResponse, GetTransactionTraceResponse, Sequencer } from '../types/api';
4
4
  import { BigNumberish } from '../utils/number';
5
5
  import { ProviderInterface } from './interface';
@@ -29,7 +29,7 @@ export declare class SequencerProvider implements ProviderInterface {
29
29
  protected fetchEndpoint<T extends keyof Sequencer.Endpoints>(endpoint: T, ...[query, request]: Sequencer.Endpoints[T]['QUERY'] extends never ? Sequencer.Endpoints[T]['REQUEST'] extends never ? [] : [undefined, Sequencer.Endpoints[T]['REQUEST']] : Sequencer.Endpoints[T]['REQUEST'] extends never ? [Sequencer.Endpoints[T]['QUERY']] : [Sequencer.Endpoints[T]['QUERY'], Sequencer.Endpoints[T]['REQUEST']]): Promise<Sequencer.Endpoints[T]['RESPONSE']>;
30
30
  callContract({ contractAddress, entrypoint: entryPointSelector, calldata }: Call, blockIdentifier?: BlockIdentifier): Promise<CallContractResponse>;
31
31
  getBlock(blockIdentifier?: BlockIdentifier): Promise<GetBlockResponse>;
32
- getStorageAt(contractAddress: string, key: BigNumberish, blockHashOrTag?: BlockTag | BigNumberish): Promise<BigNumberish>;
32
+ getStorageAt(contractAddress: string, key: BigNumberish, blockIdentifier?: BlockIdentifier): Promise<BigNumberish>;
33
33
  getTransaction(txHash: BigNumberish): Promise<GetTransactionResponse>;
34
34
  getTransactionReceipt(txHash: BigNumberish): Promise<GetTransactionReceiptResponse>;
35
35
  getClassAt(contractAddress: string, blockIdentifier?: BlockIdentifier): Promise<ContractClass>;
@@ -249,14 +249,14 @@ var SequencerProvider = /** @class */ (function () {
249
249
  });
250
250
  });
251
251
  };
252
- SequencerProvider.prototype.getStorageAt = function (contractAddress, key, blockHashOrTag) {
253
- if (blockHashOrTag === void 0) { blockHashOrTag = 'pending'; }
252
+ SequencerProvider.prototype.getStorageAt = function (contractAddress, key, blockIdentifier) {
253
+ if (blockIdentifier === void 0) { blockIdentifier = 'pending'; }
254
254
  return __awaiter(this, void 0, void 0, function () {
255
255
  var parsedKey;
256
256
  return __generator(this, function (_a) {
257
257
  parsedKey = (0, number_1.toBN)(key).toString(10);
258
258
  return [2 /*return*/, this.fetchEndpoint('get_storage_at', {
259
- blockIdentifier: blockHashOrTag,
259
+ blockIdentifier: blockIdentifier,
260
260
  contractAddress: contractAddress,
261
261
  key: parsedKey,
262
262
  })];
@@ -359,7 +359,7 @@ var SequencerProvider = /** @class */ (function () {
359
359
  if (blockIdentifier === void 0) { blockIdentifier = 'pending'; }
360
360
  return __awaiter(this, void 0, void 0, function () {
361
361
  return __generator(this, function (_a) {
362
- return [2 /*return*/, this.fetchEndpoint('get_code', { contractAddress: contractAddress, blockIdentifier: blockIdentifier }).then(this.responseParser.parseGetCodeResponse)];
362
+ return [2 /*return*/, this.fetchEndpoint('get_code', { contractAddress: contractAddress, blockIdentifier: blockIdentifier })];
363
363
  });
364
364
  });
365
365
  };
@@ -1,3 +1,4 @@
1
+ /// <reference types="bn.js" />
1
2
  import type { BlockNumber } from '../types';
2
3
  import { BigNumberish } from '../utils/number';
3
4
  /**
@@ -23,6 +24,17 @@ declare type BlockIdentifierObject = {
23
24
  type: 'BLOCK_HASH';
24
25
  data: BigNumberish;
25
26
  };
27
+ export declare class BlockIdentifierClass {
28
+ blockIdentifier: BlockIdentifier;
29
+ constructor(blockIdentifier: BlockIdentifier);
30
+ getIdentifier(): string | import("bn.js") | {
31
+ block_hash: string;
32
+ block_number?: undefined;
33
+ } | {
34
+ block_number: number;
35
+ block_hash?: undefined;
36
+ } | null;
37
+ }
26
38
  /**
27
39
  * Identifies the block to be queried.
28
40
  *
package/provider/utils.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getFormattedBlockIdentifier = exports.getBlockIdentifier = exports.txIdentifier = exports.formatHash = void 0;
3
+ exports.getFormattedBlockIdentifier = exports.getBlockIdentifier = exports.BlockIdentifierClass = exports.txIdentifier = exports.formatHash = void 0;
4
4
  var number_1 = require("../utils/number");
5
5
  /**
6
6
  *
@@ -29,6 +29,22 @@ function txIdentifier(txHash, txId) {
29
29
  return "transactionHash=".concat(hashString);
30
30
  }
31
31
  exports.txIdentifier = txIdentifier;
32
+ var BlockIdentifierClass = /** @class */ (function () {
33
+ function BlockIdentifierClass(blockIdentifier) {
34
+ this.blockIdentifier = blockIdentifier;
35
+ }
36
+ BlockIdentifierClass.prototype.getIdentifier = function () {
37
+ if (typeof this.blockIdentifier === 'string' && (0, number_1.isHex)(this.blockIdentifier)) {
38
+ return { block_hash: this.blockIdentifier };
39
+ }
40
+ if (typeof this.blockIdentifier === 'number') {
41
+ return { block_number: this.blockIdentifier };
42
+ }
43
+ return this.blockIdentifier;
44
+ };
45
+ return BlockIdentifierClass;
46
+ }());
47
+ exports.BlockIdentifierClass = BlockIdentifierClass;
32
48
  /**
33
49
  * Identifies the block to be queried.
34
50
  *