starknet 4.7.0 → 4.8.0

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/src/utils/hash.ts CHANGED
@@ -15,7 +15,15 @@ import {
15
15
  import { RawCalldata } from '../types/lib';
16
16
  import { ec } from './ellipticCurve';
17
17
  import { addHexPrefix, buf2hex, removeHexPrefix, utf8ToArray } from './encode';
18
- import { BigNumberish, toBN, toFelt, toHex } from './number';
18
+ import {
19
+ BigNumberish,
20
+ isHex,
21
+ isStringWholeNumber,
22
+ toBN,
23
+ toFelt,
24
+ toHex,
25
+ toHexString,
26
+ } from './number';
19
27
 
20
28
  export const transactionVersion = 1;
21
29
  export const feeTransactionVersion = toBN(2).pow(toBN(128)).add(toBN(transactionVersion));
@@ -53,6 +61,21 @@ export function getSelectorFromName(funcName: string) {
53
61
  return toHex(starknetKeccak(funcName));
54
62
  }
55
63
 
64
+ /**
65
+ * Function to get hex selector from function name, decimal string or hex string
66
+ * @param value hex string | decimal string | string
67
+ * @returns Hex selector
68
+ */
69
+ export function getSelector(value: string) {
70
+ if (isHex(value)) {
71
+ return value;
72
+ }
73
+ if (isStringWholeNumber(value)) {
74
+ return toHexString(value);
75
+ }
76
+ return getSelectorFromName(value);
77
+ }
78
+
56
79
  const constantPoints = CONSTANT_POINTS.map((coords: string[]) =>
57
80
  ec.curve.point(coords[0], coords[1])
58
81
  );
@@ -1,4 +1,5 @@
1
1
  import { pedersen } from './hash';
2
+ import { toBN } from './number';
2
3
 
3
4
  export class MerkleTree {
4
5
  public leaves: string[];
@@ -31,7 +32,7 @@ export class MerkleTree {
31
32
  }
32
33
 
33
34
  static hash(a: string, b: string) {
34
- const [aSorted, bSorted] = [a, b].sort();
35
+ const [aSorted, bSorted] = [toBN(a), toBN(b)].sort((x: any, y: any) => (x.gte(y) ? 1 : -1));
35
36
  return pedersen([aSorted, bSorted]);
36
37
  }
37
38
 
@@ -61,3 +61,30 @@ export function bigNumberishArrayToDecimalStringArray(rawCalldata: BigNumberish[
61
61
  export function bigNumberishArrayToHexadecimalStringArray(rawCalldata: BigNumberish[]): string[] {
62
62
  return rawCalldata.map((x) => toHex(toBN(x)));
63
63
  }
64
+
65
+ export const isStringWholeNumber = (value: string) => /^\d+$/.test(value);
66
+ export const toHexString = (value: string) => toHex(toBN(value));
67
+
68
+ export function getDecimalString(value: string) {
69
+ if (isHex(value)) {
70
+ return hexToDecimalString(value);
71
+ }
72
+ if (isStringWholeNumber(value)) {
73
+ return value;
74
+ }
75
+ throw new Error(`${value} need to be hex-string or whole-number-string`);
76
+ }
77
+
78
+ export function getHexString(value: string) {
79
+ if (isHex(value)) {
80
+ return value;
81
+ }
82
+ if (isStringWholeNumber(value)) {
83
+ return toHexString(value);
84
+ }
85
+ throw new Error(`${value} need to be hex-string or whole-number-string`);
86
+ }
87
+
88
+ export function getHexStringArray(value: Array<string>) {
89
+ return value.map((el) => getHexString(el));
90
+ }
@@ -19,12 +19,9 @@ import { ResponseParser } from '.';
19
19
  export class SequencerAPIResponseParser extends ResponseParser {
20
20
  public parseGetBlockResponse(res: Sequencer.GetBlockResponse): GetBlockResponse {
21
21
  return {
22
- timestamp: res.timestamp,
23
- block_hash: res.block_hash,
24
- block_number: res.block_number,
22
+ ...res,
25
23
  new_root: res.state_root,
26
24
  parent_hash: res.parent_block_hash,
27
- status: res.status,
28
25
  transactions: Object.values(res.transactions)
29
26
  .map((value) => 'transaction_hash' in value && value.transaction_hash)
30
27
  .filter(Boolean) as Array<string>,
@@ -62,12 +59,20 @@ export class SequencerAPIResponseParser extends ResponseParser {
62
59
  ): GetTransactionReceiptResponse {
63
60
  return {
64
61
  transaction_hash: res.transaction_hash,
65
- actual_fee: 'actual_fee' in res ? res.actual_fee : undefined,
66
62
  status: res.status,
67
- status_data: undefined,
68
63
  messages_sent: res.l2_to_l1_messages as any, // TODO: parse
69
64
  events: res.events as any,
70
- l1_origin_message: undefined,
65
+ ...('block_hash' in res && { block_hash: res.block_hash }),
66
+ ...('block_number' in res && { block_number: res.block_number }),
67
+ ...('actual_fee' in res && { actual_fee: res.actual_fee }),
68
+ ...('transaction_index' in res && { transaction_index: res.transaction_index }),
69
+ ...('execution_resources' in res && { execution_resources: res.execution_resources }),
70
+ ...('l1_to_l2_consumed_message' in res && {
71
+ l1_to_l2_consumed_message: res['l1_to_l2_consumed_message'],
72
+ }),
73
+ ...('transaction_failure_reason' in res && {
74
+ transaction_failure_reason: res.transaction_failure_reason,
75
+ }),
71
76
  };
72
77
  }
73
78
 
@@ -15,15 +15,17 @@ export declare type GetContractAddressesResponse = {
15
15
  Starknet: string;
16
16
  GpsStatementVerifier: string;
17
17
  };
18
- export declare type InvokeFunctionTrace = {
18
+ export declare type FunctionInvocation = {
19
19
  caller_address: string;
20
20
  contract_address: string;
21
- code_address: string;
22
- selector: string;
23
21
  calldata: RawCalldata;
22
+ call_type?: string;
23
+ class_hash?: string;
24
+ selector?: string;
25
+ entry_point_type?: EntryPointType;
24
26
  result: Array<any>;
25
27
  execution_resources: ExecutionResources;
26
- internal_call: Array<InvokeFunctionTrace>;
28
+ internal_calls: Array<FunctionInvocation>;
27
29
  events: Array<any>;
28
30
  messages: Array<any>;
29
31
  };
@@ -40,18 +42,9 @@ export declare type ExecutionResources = {
40
42
  n_memory_holes: number;
41
43
  };
42
44
  export declare type GetTransactionTraceResponse = {
43
- function_invocation: {
44
- caller_address: string;
45
- contract_address: string;
46
- code_address: string;
47
- selector: string;
48
- calldata: RawArgs;
49
- result: Array<any>;
50
- execution_resources: ExecutionResources;
51
- internal_call: Array<any>;
52
- events: Array<any>;
53
- messages: Array<any>;
54
- };
45
+ validate_invocation?: FunctionInvocation;
46
+ function_invocation?: FunctionInvocation;
47
+ fee_transfer_invocation?: FunctionInvocation;
55
48
  signature: Signature;
56
49
  };
57
50
  export declare type RawArgs = {
@@ -60,6 +53,12 @@ export declare type RawArgs = {
60
53
  [k: string]: BigNumberish;
61
54
  };
62
55
  };
56
+ export declare type CallL1Handler = {
57
+ from_address: string;
58
+ to_address: string;
59
+ entry_point_selector: string;
60
+ payload: Array<string>;
61
+ };
63
62
  export declare namespace Sequencer {
64
63
  type DeclareTransaction = {
65
64
  type: 'DECLARE';
@@ -165,6 +164,7 @@ export declare namespace Sequencer {
165
164
  status: Status;
166
165
  gas_price: string;
167
166
  sequencer_address: string;
167
+ starknet_version: string;
168
168
  };
169
169
  type CallContractTransaction = Omit<InvokeFunctionTransaction, 'type' | 'entry_point_type' | 'nonce'> & {
170
170
  entry_point_selector: string;
@@ -296,5 +296,10 @@ export declare namespace Sequencer {
296
296
  REQUEST: never;
297
297
  RESPONSE: any;
298
298
  };
299
+ estimate_message_fee: {
300
+ QUERY: any;
301
+ REQUEST: any;
302
+ RESPONSE: EstimateFeeResponse;
303
+ };
299
304
  };
300
305
  }
@@ -13,6 +13,10 @@ export interface GetBlockResponse {
13
13
  parent_hash: string;
14
14
  status: Status;
15
15
  transactions: Array<string>;
16
+ gas_price?: string;
17
+ sequencer_address?: string;
18
+ starknet_version?: string;
19
+ transaction_receipts?: any;
16
20
  }
17
21
  export interface GetCodeResponse {
18
22
  bytecode: string[];
package/utils/hash.d.ts CHANGED
@@ -21,6 +21,12 @@ export declare function starknetKeccak(value: string): BN;
21
21
  * @returns hex selector of given abi function name
22
22
  */
23
23
  export declare function getSelectorFromName(funcName: string): string;
24
+ /**
25
+ * Function to get hex selector from function name, decimal string or hex string
26
+ * @param value hex string | decimal string | string
27
+ * @returns Hex selector
28
+ */
29
+ export declare function getSelector(value: string): string;
24
30
  export declare function pedersen(input: [BigNumberish, BigNumberish]): string;
25
31
  export declare function computeHashOnElements(data: BigNumberish[]): string;
26
32
  export declare function calculateTransactionHashCommon(txHashPrefix: TransactionHashPrefix, version: BigNumberish, contractAddress: BigNumberish, entryPointSelector: BigNumberish, calldata: BigNumberish[], maxFee: BigNumberish, chainId: StarknetChainId, additionalData?: BigNumberish[]): string;
package/utils/hash.js CHANGED
@@ -28,7 +28,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
28
28
  return (mod && mod.__esModule) ? mod : { "default": mod };
29
29
  };
30
30
  Object.defineProperty(exports, "__esModule", { value: true });
31
- exports.calculateContractAddressFromHash = exports.calculateTransactionHash = exports.calculateDeployTransactionHash = exports.calculateTransactionHashCommon = exports.computeHashOnElements = exports.pedersen = exports.getSelectorFromName = exports.starknetKeccak = exports.keccakBn = exports.feeTransactionVersion = exports.transactionVersion = void 0;
31
+ exports.calculateContractAddressFromHash = exports.calculateTransactionHash = exports.calculateDeployTransactionHash = exports.calculateTransactionHashCommon = exports.computeHashOnElements = exports.pedersen = exports.getSelector = exports.getSelectorFromName = exports.starknetKeccak = exports.keccakBn = exports.feeTransactionVersion = exports.transactionVersion = void 0;
32
32
  var keccak_1 = require("ethereum-cryptography/keccak");
33
33
  var utils_1 = require("ethereum-cryptography/utils");
34
34
  var minimalistic_assert_1 = __importDefault(require("minimalistic-assert"));
@@ -70,6 +70,21 @@ function getSelectorFromName(funcName) {
70
70
  return (0, number_1.toHex)(starknetKeccak(funcName));
71
71
  }
72
72
  exports.getSelectorFromName = getSelectorFromName;
73
+ /**
74
+ * Function to get hex selector from function name, decimal string or hex string
75
+ * @param value hex string | decimal string | string
76
+ * @returns Hex selector
77
+ */
78
+ function getSelector(value) {
79
+ if ((0, number_1.isHex)(value)) {
80
+ return value;
81
+ }
82
+ if ((0, number_1.isStringWholeNumber)(value)) {
83
+ return (0, number_1.toHexString)(value);
84
+ }
85
+ return getSelectorFromName(value);
86
+ }
87
+ exports.getSelector = getSelector;
73
88
  var constantPoints = constants_1.CONSTANT_POINTS.map(function (coords) {
74
89
  return ellipticCurve_1.ec.curve.point(coords[0], coords[1]);
75
90
  });
package/utils/merkle.js CHANGED
@@ -27,6 +27,7 @@ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
27
27
  Object.defineProperty(exports, "__esModule", { value: true });
28
28
  exports.proofMerklePath = exports.MerkleTree = void 0;
29
29
  var hash_1 = require("./hash");
30
+ var number_1 = require("./number");
30
31
  var MerkleTree = /** @class */ (function () {
31
32
  function MerkleTree(leafHashes) {
32
33
  this.branches = [];
@@ -52,7 +53,7 @@ var MerkleTree = /** @class */ (function () {
52
53
  return this.build(newLeaves);
53
54
  };
54
55
  MerkleTree.hash = function (a, b) {
55
- var _a = __read([a, b].sort(), 2), aSorted = _a[0], bSorted = _a[1];
56
+ var _a = __read([(0, number_1.toBN)(a), (0, number_1.toBN)(b)].sort(function (x, y) { return (x.gte(y) ? 1 : -1); }), 2), aSorted = _a[0], bSorted = _a[1];
56
57
  return (0, hash_1.pedersen)([aSorted, bSorted]);
57
58
  };
58
59
  MerkleTree.prototype.getProof = function (leaf, branch, hashPath) {
package/utils/number.d.ts CHANGED
@@ -8,3 +8,8 @@ export declare function toFelt(num: BigNumberish): string;
8
8
  export declare function assertInRange(input: BigNumberish, lowerBound: BigNumberish, upperBound: BigNumberish, inputName?: string): void;
9
9
  export declare function bigNumberishArrayToDecimalStringArray(rawCalldata: BigNumberish[]): string[];
10
10
  export declare function bigNumberishArrayToHexadecimalStringArray(rawCalldata: BigNumberish[]): string[];
11
+ export declare const isStringWholeNumber: (value: string) => boolean;
12
+ export declare const toHexString: (value: string) => string;
13
+ export declare function getDecimalString(value: string): string;
14
+ export declare function getHexString(value: string): string;
15
+ export declare function getHexStringArray(value: Array<string>): string[];
package/utils/number.js CHANGED
@@ -26,7 +26,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
26
26
  return (mod && mod.__esModule) ? mod : { "default": mod };
27
27
  };
28
28
  Object.defineProperty(exports, "__esModule", { value: true });
29
- exports.bigNumberishArrayToHexadecimalStringArray = exports.bigNumberishArrayToDecimalStringArray = exports.assertInRange = exports.toFelt = exports.hexToDecimalString = exports.toHex = exports.toBN = exports.isHex = void 0;
29
+ exports.getHexStringArray = exports.getHexString = exports.getDecimalString = exports.toHexString = exports.isStringWholeNumber = exports.bigNumberishArrayToHexadecimalStringArray = exports.bigNumberishArrayToDecimalStringArray = exports.assertInRange = exports.toFelt = exports.hexToDecimalString = exports.toHex = exports.toBN = exports.isHex = void 0;
30
30
  var bn_js_1 = __importStar(require("bn.js"));
31
31
  var minimalistic_assert_1 = __importDefault(require("minimalistic-assert"));
32
32
  var encode_1 = require("./encode");
@@ -80,3 +80,31 @@ function bigNumberishArrayToHexadecimalStringArray(rawCalldata) {
80
80
  return rawCalldata.map(function (x) { return toHex(toBN(x)); });
81
81
  }
82
82
  exports.bigNumberishArrayToHexadecimalStringArray = bigNumberishArrayToHexadecimalStringArray;
83
+ var isStringWholeNumber = function (value) { return /^\d+$/.test(value); };
84
+ exports.isStringWholeNumber = isStringWholeNumber;
85
+ var toHexString = function (value) { return toHex(toBN(value)); };
86
+ exports.toHexString = toHexString;
87
+ function getDecimalString(value) {
88
+ if (isHex(value)) {
89
+ return hexToDecimalString(value);
90
+ }
91
+ if ((0, exports.isStringWholeNumber)(value)) {
92
+ return value;
93
+ }
94
+ throw new Error("".concat(value, " need to be hex-string or whole-number-string"));
95
+ }
96
+ exports.getDecimalString = getDecimalString;
97
+ function getHexString(value) {
98
+ if (isHex(value)) {
99
+ return value;
100
+ }
101
+ if ((0, exports.isStringWholeNumber)(value)) {
102
+ return (0, exports.toHexString)(value);
103
+ }
104
+ throw new Error("".concat(value, " need to be hex-string or whole-number-string"));
105
+ }
106
+ exports.getHexString = getHexString;
107
+ function getHexStringArray(value) {
108
+ return value.map(function (el) { return getHexString(el); });
109
+ }
110
+ exports.getHexStringArray = getHexStringArray;
@@ -35,17 +35,9 @@ var SequencerAPIResponseParser = /** @class */ (function (_super) {
35
35
  return _super !== null && _super.apply(this, arguments) || this;
36
36
  }
37
37
  SequencerAPIResponseParser.prototype.parseGetBlockResponse = function (res) {
38
- return {
39
- timestamp: res.timestamp,
40
- block_hash: res.block_hash,
41
- block_number: res.block_number,
42
- new_root: res.state_root,
43
- parent_hash: res.parent_block_hash,
44
- status: res.status,
45
- transactions: Object.values(res.transactions)
38
+ return __assign(__assign({}, res), { new_root: res.state_root, parent_hash: res.parent_block_hash, transactions: Object.values(res.transactions)
46
39
  .map(function (value) { return 'transaction_hash' in value && value.transaction_hash; })
47
- .filter(Boolean),
48
- };
40
+ .filter(Boolean) });
49
41
  };
50
42
  SequencerAPIResponseParser.prototype.parseGetTransactionResponse = function (res) {
51
43
  return {
@@ -66,15 +58,11 @@ var SequencerAPIResponseParser = /** @class */ (function (_super) {
66
58
  };
67
59
  };
68
60
  SequencerAPIResponseParser.prototype.parseGetTransactionReceiptResponse = function (res) {
69
- return {
70
- transaction_hash: res.transaction_hash,
71
- actual_fee: 'actual_fee' in res ? res.actual_fee : undefined,
72
- status: res.status,
73
- status_data: undefined,
74
- messages_sent: res.l2_to_l1_messages,
75
- events: res.events,
76
- l1_origin_message: undefined,
77
- };
61
+ return __assign(__assign(__assign(__assign(__assign(__assign(__assign({ transaction_hash: res.transaction_hash, status: res.status, messages_sent: res.l2_to_l1_messages, events: res.events }, ('block_hash' in res && { block_hash: res.block_hash })), ('block_number' in res && { block_number: res.block_number })), ('actual_fee' in res && { actual_fee: res.actual_fee })), ('transaction_index' in res && { transaction_index: res.transaction_index })), ('execution_resources' in res && { execution_resources: res.execution_resources })), ('l1_to_l2_consumed_message' in res && {
62
+ l1_to_l2_consumed_message: res['l1_to_l2_consumed_message'],
63
+ })), ('transaction_failure_reason' in res && {
64
+ transaction_failure_reason: res.transaction_failure_reason,
65
+ }));
78
66
  };
79
67
  SequencerAPIResponseParser.prototype.parseFeeEstimateResponse = function (res) {
80
68
  if ('overall_fee' in res) {
@@ -271,21 +271,29 @@ Gets the transaction trace from a tx hash.
271
271
 
272
272
  ```typescript
273
273
  {
274
- function_invocation: {
274
+ validate_invocation?: FunctionInvocation;
275
+ function_invocation?: FunctionInvocation;
276
+ fee_transfer_invocation?: FunctionInvocation;
277
+ signature: Signature;
278
+ }
279
+
280
+ {
281
+ FunctionInvocation: {
275
282
  caller_address: string;
276
283
  contract_address: string;
277
- code_address: string;
278
- selector: string;
279
284
  calldata: {
280
285
  [inputName: string]: string | string[] | { type: 'struct'; [k: string]: BigNumberish };
281
286
  };
287
+ call_type?: string;
288
+ class_hash?: string;
289
+ selector?: string;
290
+ entry_point_type?: EntryPointType;
282
291
  result: Array<any>;
283
- execution_resources: any;
284
- internal_call: Array<any>;
292
+ execution_resources: ExecutionResources;
293
+ internal_calls: Array<FunctionInvocation>;
285
294
  events: Array<any>;
286
295
  messages: Array<any>;
287
296
  };
288
- signature: Signature;
289
297
  }
290
298
  ```
291
299
 
@@ -45,7 +45,7 @@ const compiledAccount = json.parse(
45
45
 
46
46
  > **Note**
47
47
  >
48
- > below example uses [Argent's](https://github.com/argentlabs/argent-contracts-starknet/blob/develop/contracts/ArgentAccount.cairo) account contract
48
+ > below example uses [Argent's](https://github.com/argentlabs/argent-contracts-starknet/blob/develop/contracts/account/ArgentAccount.cairo) account contract
49
49
 
50
50
  ```javascript
51
51
  const accountResponse = await defaultProvider.deployContract({