starknet 4.2.0 → 4.4.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.
Files changed (102) hide show
  1. package/CHANGELOG.md +44 -0
  2. package/__mocks__/typedDataSessionExample.json +42 -0
  3. package/__tests__/defaultProvider.test.ts +11 -24
  4. package/__tests__/rpcProvider.test.ts +3 -3
  5. package/__tests__/sequencerProvider.test.ts +40 -2
  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 +2 -0
  11. package/dist/index.js +3 -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 +67 -25
  17. package/dist/provider/sequencer.d.ts +2 -2
  18. package/dist/provider/sequencer.js +9 -9
  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.js +1 -1
  33. package/dist/utils/responseParser/rpc.d.ts +13 -3
  34. package/dist/utils/responseParser/rpc.js +2 -10
  35. package/dist/utils/responseParser/sequencer.d.ts +4 -1
  36. package/dist/utils/responseParser/sequencer.js +1 -7
  37. package/dist/utils/typedData/index.d.ts +23 -8
  38. package/dist/utils/typedData/index.js +70 -31
  39. package/dist/utils/typedData/types.d.ts +8 -3
  40. package/dist/utils/url.d.ts +7 -0
  41. package/dist/utils/url.js +49 -0
  42. package/index.d.ts +2 -0
  43. package/index.js +3 -1
  44. package/package.json +1 -1
  45. package/provider/default.d.ts +2 -2
  46. package/provider/default.js +3 -3
  47. package/provider/interface.d.ts +6 -3
  48. package/provider/rpc.d.ts +9 -4
  49. package/provider/rpc.js +67 -25
  50. package/provider/sequencer.d.ts +2 -2
  51. package/provider/sequencer.js +9 -9
  52. package/provider/utils.d.ts +12 -0
  53. package/provider/utils.js +17 -1
  54. package/signer/default.d.ts +1 -1
  55. package/signer/default.js +1 -0
  56. package/src/index.ts +2 -0
  57. package/src/provider/default.ts +2 -3
  58. package/src/provider/interface.ts +5 -3
  59. package/src/provider/rpc.ts +56 -34
  60. package/src/provider/sequencer.ts +11 -9
  61. package/src/provider/utils.ts +22 -1
  62. package/src/signer/default.ts +2 -2
  63. package/src/types/api/openrpc.ts +168 -0
  64. package/src/types/api/rpc.ts +22 -45
  65. package/src/types/provider.ts +5 -5
  66. package/src/utils/ellipticCurve.ts +20 -16
  67. package/src/utils/hash.ts +8 -6
  68. package/src/utils/merkle.ts +70 -0
  69. package/src/utils/number.ts +1 -1
  70. package/src/utils/responseParser/rpc.ts +16 -13
  71. package/src/utils/responseParser/sequencer.ts +5 -8
  72. package/src/utils/typedData/index.ts +88 -34
  73. package/src/utils/typedData/types.ts +12 -4
  74. package/src/utils/url.ts +53 -0
  75. package/types/api/openrpc.d.ts +151 -0
  76. package/types/api/openrpc.js +9 -0
  77. package/types/api/rpc.d.ts +22 -43
  78. package/types/provider.d.ts +5 -5
  79. package/utils/ellipticCurve.d.ts +13 -0
  80. package/utils/ellipticCurve.js +20 -16
  81. package/utils/hash.js +8 -6
  82. package/utils/merkle.d.ts +10 -0
  83. package/utils/merkle.js +90 -0
  84. package/utils/number.js +1 -1
  85. package/utils/responseParser/rpc.d.ts +13 -3
  86. package/utils/responseParser/rpc.js +2 -10
  87. package/utils/responseParser/sequencer.d.ts +4 -1
  88. package/utils/responseParser/sequencer.js +1 -7
  89. package/utils/typedData/index.d.ts +23 -8
  90. package/utils/typedData/index.js +70 -31
  91. package/utils/typedData/types.d.ts +8 -3
  92. package/utils/url.d.ts +7 -0
  93. package/utils/url.js +49 -0
  94. package/www/docs/API/account.md +20 -18
  95. package/www/docs/API/contract.md +10 -10
  96. package/www/docs/API/contractFactory.md +14 -11
  97. package/www/docs/API/provider.md +60 -37
  98. package/www/docs/API/signer.md +8 -10
  99. package/www/docs/API/utils.md +151 -74
  100. package/www/guides/account.md +12 -12
  101. package/www/guides/erc20.md +19 -4
  102. package/www/guides/intro.md +3 -1
@@ -0,0 +1,53 @@
1
+ import urljoin from 'url-join';
2
+
3
+ /**
4
+ * Inspired from https://github.com/segmentio/is-url
5
+ */
6
+
7
+ /**
8
+ * RegExps.
9
+ * A URL must match #1 and then at least one of #2/#3.
10
+ * Use two levels of REs to avoid REDOS.
11
+ */
12
+ const protocolAndDomainRE = /^(?:\w+:)?\/\/(\S+)$/;
13
+
14
+ const localhostDomainRE = /^localhost[:?\d]*(?:[^:?\d]\S*)?$/;
15
+ const nonLocalhostDomainRE = /^[^\s.]+\.\S{2,}$/;
16
+
17
+ /**
18
+ * Loosely validate a URL `string`.
19
+ * @param {String} s
20
+ * @return {Boolean}
21
+ */
22
+ export function isUrl(s?: string): boolean {
23
+ if (!s) {
24
+ return false;
25
+ }
26
+
27
+ if (typeof s !== 'string') {
28
+ return false;
29
+ }
30
+
31
+ const match = s.match(protocolAndDomainRE);
32
+ if (!match) {
33
+ return false;
34
+ }
35
+
36
+ const everythingAfterProtocol = match[1];
37
+ if (!everythingAfterProtocol) {
38
+ return false;
39
+ }
40
+
41
+ if (
42
+ localhostDomainRE.test(everythingAfterProtocol) ||
43
+ nonLocalhostDomainRE.test(everythingAfterProtocol)
44
+ ) {
45
+ return true;
46
+ }
47
+
48
+ return false;
49
+ }
50
+
51
+ export function buildUrl(baseUrl: string, defaultPath: string, urlOrPath?: string) {
52
+ return isUrl(urlOrPath) ? urlOrPath! : urljoin(baseUrl, urlOrPath ?? defaultPath);
53
+ }
@@ -0,0 +1,151 @@
1
+ /**
2
+ * Starknet RPC version 0.1.0
3
+ * starknet_api_openrpc version 0.31.0
4
+ *
5
+ * TypeScript Representation of OpenRpc protocol types | results
6
+ * errors are not implemented here only results
7
+ */
8
+ /**
9
+ * "type": "string",
10
+ * "title": "Field element",
11
+ * "$comment": "A field element, represented as a string of hex digits",
12
+ * "description": "A field element. Represented as up to 63 hex digits and leading 4 bits zeroed.",
13
+ * "pattern": "^0x0[a-fA-F0-9]{1,63}$"
14
+ */
15
+ declare type FELT = string;
16
+ declare type BLOCK_NUMBER = number;
17
+ declare type BLOCK_HASH = FELT;
18
+ declare type TXN_HASH = FELT;
19
+ declare type TXN_STATUS = 'PENDING' | 'ACCEPTED_ON_L2' | 'ACCEPTED_ON_L1' | 'REJECTED';
20
+ declare type TXN_TYPE = 'DECLARE' | 'DEPLOY' | 'INVOKE' | 'L1_HANDLER';
21
+ declare type MSG_TO_L1 = {
22
+ to_address: FELT;
23
+ payload: Array<FELT>;
24
+ };
25
+ declare type EVENT = {
26
+ from_address: FELT;
27
+ keys: Array<FELT>;
28
+ data: Array<FELT>;
29
+ };
30
+ declare type COMMON_RECEIPT_PROPERTIES = {
31
+ transaction_hash: TXN_HASH;
32
+ actual_fee: FELT;
33
+ status: TXN_STATUS;
34
+ block_hash: BLOCK_HASH;
35
+ block_number: BLOCK_NUMBER;
36
+ type?: TXN_TYPE;
37
+ };
38
+ declare type INVOKE_TXN_RECEIPT_PROPERTIES = {
39
+ messages_sent: MSG_TO_L1;
40
+ events: EVENT;
41
+ };
42
+ declare type PENDING_COMMON_RECEIPT_PROPERTIES = {
43
+ transaction_hash: TXN_HASH;
44
+ actual_fee: FELT;
45
+ type?: TXN_TYPE;
46
+ };
47
+ declare type INVOKE_TXN_RECEIPT = COMMON_RECEIPT_PROPERTIES & INVOKE_TXN_RECEIPT_PROPERTIES;
48
+ declare type L1_HANDLER_TXN_RECEIPT = COMMON_RECEIPT_PROPERTIES;
49
+ declare type DECLARE_TXN_RECEIPT = COMMON_RECEIPT_PROPERTIES;
50
+ declare type DEPLOY_TXN_RECEIPT = COMMON_RECEIPT_PROPERTIES;
51
+ declare type PENDING_INVOKE_TXN_RECEIPT = PENDING_COMMON_RECEIPT_PROPERTIES & INVOKE_TXN_RECEIPT_PROPERTIES;
52
+ declare type PENDING_TXN_RECEIPT = PENDING_INVOKE_TXN_RECEIPT | PENDING_COMMON_RECEIPT_PROPERTIES;
53
+ declare type TXN_RECEIPT = INVOKE_TXN_RECEIPT | L1_HANDLER_TXN_RECEIPT | DECLARE_TXN_RECEIPT | DEPLOY_TXN_RECEIPT | PENDING_TXN_RECEIPT;
54
+ export declare namespace RPC_1 {
55
+ type GetTransactionReceiptResponse = TXN_RECEIPT;
56
+ type Methods = {
57
+ starknet_getTransactionReceipt: {
58
+ QUERY: never;
59
+ REQUEST: any[];
60
+ RESPONSE: GetTransactionReceiptResponse;
61
+ };
62
+ };
63
+ }
64
+ declare type BLOCK_HEADER = {
65
+ block_hash: BLOCK_HASH;
66
+ parent_hash: BLOCK_HASH;
67
+ block_number: BLOCK_NUMBER;
68
+ new_root: FELT;
69
+ timestamp: number;
70
+ sequencer_address: FELT;
71
+ };
72
+ declare type BLOCK_BODY_WITH_TX_HASHES = {
73
+ transactions: Array<TXN_HASH>;
74
+ };
75
+ declare type BLOCK_WITH_TX_HASHES = {
76
+ status: TXN_STATUS;
77
+ } & BLOCK_HEADER & BLOCK_BODY_WITH_TX_HASHES;
78
+ declare type PENDING_BLOCK_WITH_TX_HASHES = BLOCK_BODY_WITH_TX_HASHES & {
79
+ timestamp: number;
80
+ sequencer_address: FELT;
81
+ parent_hash: BLOCK_HASH;
82
+ };
83
+ declare type BLOCK_STATUS = 'PENDING' | 'ACCEPTED_ON_L2' | 'ACCEPTED_ON_L1' | 'REJECTED';
84
+ /**
85
+ * "title": "An integer number in hex format (0x...)",
86
+ * "pattern": "^0x[a-fA-F0-9]+$"
87
+ */
88
+ declare type NUM_AS_HEX = string;
89
+ declare type SIGNATURE = Array<FELT>;
90
+ declare type COMMON_TXN_PROPERTIES = {
91
+ transaction_hash: TXN_HASH;
92
+ max_fee: FELT;
93
+ version: NUM_AS_HEX;
94
+ signature: SIGNATURE;
95
+ nonce: FELT;
96
+ type: TXN_TYPE;
97
+ };
98
+ declare type ADDRESS = FELT;
99
+ declare type FUNCTION_CALL = {
100
+ contract_address: ADDRESS;
101
+ entry_point_selector: FELT;
102
+ calldata: Array<FELT>;
103
+ };
104
+ declare type INVOKE_TXN = COMMON_TXN_PROPERTIES & FUNCTION_CALL;
105
+ declare type DECLARE_TXN = COMMON_TXN_PROPERTIES & {
106
+ class_hash: FELT;
107
+ sender_address: ADDRESS;
108
+ };
109
+ declare type DEPLOY_TXN = {
110
+ transaction_hash: TXN_HASH;
111
+ class_hash: FELT;
112
+ version: NUM_AS_HEX;
113
+ type: TXN_TYPE;
114
+ contract_address: FELT;
115
+ contract_address_salt: FELT;
116
+ constructor_calldata: Array<FELT>;
117
+ };
118
+ declare type TXN = INVOKE_TXN | DECLARE_TXN | DEPLOY_TXN;
119
+ declare type BLOCK_BODY_WITH_TXS = {
120
+ transactions: Array<TXN>;
121
+ };
122
+ declare type BLOCK_WITH_TXS = {
123
+ status: BLOCK_STATUS;
124
+ } & BLOCK_HEADER & BLOCK_BODY_WITH_TXS;
125
+ declare type PENDING_BLOCK_WITH_TXS = BLOCK_BODY_WITH_TXS & {
126
+ timestamp: number;
127
+ sequencer_address: FELT;
128
+ parent_hash: BLOCK_HASH;
129
+ };
130
+ declare type CONTRACT_CLASS = {
131
+ program: string;
132
+ entry_points_by_type: {
133
+ CONSTRUCTOR: CONTRACT_ENTRY_POINT_LIST;
134
+ EXTERNAL: CONTRACT_ENTRY_POINT_LIST;
135
+ L1_HANDLER: CONTRACT_ENTRY_POINT_LIST;
136
+ };
137
+ };
138
+ declare type CONTRACT_ENTRY_POINT_LIST = Array<CONTRACT_ENTRY_POINT>;
139
+ declare type CONTRACT_ENTRY_POINT = {
140
+ offset: NUM_AS_HEX;
141
+ selector: FELT;
142
+ };
143
+ export declare namespace OPENRPC {
144
+ type GetBlockWithTxHashesResponse = BLOCK_WITH_TX_HASHES | PENDING_BLOCK_WITH_TX_HASHES;
145
+ type GetBlockWithTxs = BLOCK_WITH_TXS | PENDING_BLOCK_WITH_TXS;
146
+ type GetStorageAtResponse = FELT;
147
+ type GetTransactionByHashResponse = TXN;
148
+ type GetTransactionByBlockIdAndIndex = TXN;
149
+ type GetClassResponse = CONTRACT_CLASS;
150
+ }
151
+ export {};
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ /**
3
+ * Starknet RPC version 0.1.0
4
+ * starknet_api_openrpc version 0.31.0
5
+ *
6
+ * TypeScript Representation of OpenRpc protocol types | results
7
+ * errors are not implemented here only results
8
+ */
9
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,5 +1,7 @@
1
1
  import { StarknetChainId } from '../../constants';
2
+ import { BlockIdentifier } from '../../provider/utils';
2
3
  import { Status } from '../lib';
4
+ import { OPENRPC } from './openrpc';
3
5
  export declare namespace RPC {
4
6
  export type Response = {
5
7
  id: number;
@@ -26,23 +28,9 @@ export declare namespace RPC {
26
28
  gas_consumed: number;
27
29
  gas_price: number;
28
30
  };
29
- export type GetBlockResponse = {
30
- block_hash: string;
31
- parent_hash: string;
32
- block_number: number;
33
- status: Status;
34
- sequencer: string;
35
- new_root: string;
36
- old_root: string;
37
- accepted_time: number;
38
- gas_price: string;
39
- transactions: string[];
40
- };
41
- export type GetCodeResponse = {
42
- bytecode: string[];
43
- abi: string;
44
- };
45
- export type GetStorageAtResponse = string;
31
+ export type GetBlockWithTxHashesResponse = OPENRPC.GetBlockWithTxHashesResponse;
32
+ export type GetBlockWithTxs = OPENRPC.GetBlockWithTxs;
33
+ export type GetStorageAtResponse = OPENRPC.GetStorageAtResponse;
46
34
  export type GetTransactionReceiptResponse = {
47
35
  txn_hash: string;
48
36
  actual_fee: string;
@@ -68,7 +56,8 @@ export declare namespace RPC {
68
56
  contract_class?: GetClassResponse;
69
57
  sender_address?: string;
70
58
  }
71
- export type GetTransactionResponse = InvokeTransactionResponse & DeclareTransactionResponse;
59
+ export type GetTransactionByHashResponse = OPENRPC.GetTransactionByHashResponse;
60
+ export type GetTransactionByBlockIdAndIndex = OPENRPC.GetTransactionByBlockIdAndIndex;
72
61
  export type GetTransactionCountResponse = number;
73
62
  export type GetBlockNumberResponse = number;
74
63
  export type GetSyncingStatsResponse = {
@@ -80,8 +69,8 @@ export declare namespace RPC {
80
69
  highest_block_num: string;
81
70
  } | boolean;
82
71
  export type EventFilter = {
83
- fromBlock: string;
84
- toBlock: string;
72
+ fromBlock: BlockIdentifier;
73
+ toBlock: BlockIdentifier;
85
74
  address: string;
86
75
  keys: string[];
87
76
  page_size: number;
@@ -116,56 +105,46 @@ export declare namespace RPC {
116
105
  payload: string[];
117
106
  };
118
107
  export type Methods = {
119
- starknet_getBlockByHash: {
108
+ starknet_getBlockWithTxHashes: {
120
109
  QUERY: never;
121
110
  REQUEST: any[];
122
- RESPONSE: GetBlockResponse;
111
+ RESPONSE: GetBlockWithTxHashesResponse;
123
112
  };
124
- starknet_getBlockByNumber: {
113
+ starknet_getBlockWithTxs: {
125
114
  QUERY: never;
126
115
  REQUEST: any[];
127
- RESPONSE: GetBlockResponse;
116
+ RESPONSE: GetBlockWithTxs;
128
117
  };
129
- starknet_getStorageAt: {
118
+ starknet_getNonce: {
130
119
  QUERY: never;
131
120
  REQUEST: any[];
132
- RESPONSE: GetStorageAtResponse;
121
+ RESPONSE: string;
133
122
  };
134
- starknet_getTransactionByHash: {
123
+ starknet_getStorageAt: {
135
124
  QUERY: never;
136
125
  REQUEST: any[];
137
- RESPONSE: GetTransactionResponse;
126
+ RESPONSE: GetStorageAtResponse;
138
127
  };
139
- starknet_getTransactionByBlockHashAndIndex: {
128
+ starknet_getTransactionByHash: {
140
129
  QUERY: never;
141
130
  REQUEST: any[];
142
- RESPONSE: GetTransactionResponse;
131
+ RESPONSE: GetTransactionByHashResponse;
143
132
  };
144
- starknet_getTransactionByBlockNumberAndIndex: {
133
+ starknet_getTransactionByBlockIdAndIndex: {
145
134
  QUERY: never;
146
135
  REQUEST: any[];
147
- RESPONSE: GetTransactionResponse;
136
+ RESPONSE: GetTransactionByBlockIdAndIndex;
148
137
  };
149
138
  starknet_getTransactionReceipt: {
150
139
  QUERY: never;
151
140
  REQUEST: any[];
152
141
  RESPONSE: GetTransactionReceiptResponse;
153
142
  };
154
- starknet_getBlockTransactionCountByHash: {
143
+ starknet_getBlockTransactionCount: {
155
144
  QUERY: never;
156
145
  REQUEST: any[];
157
146
  RESPONSE: GetTransactionCountResponse;
158
147
  };
159
- starknet_getBlockTransactionCountByNumber: {
160
- QUERY: never;
161
- REQUEST: any[];
162
- RESPONSE: GetTransactionCountResponse;
163
- };
164
- starknet_getCode: {
165
- QUERY: never;
166
- REQUEST: any[];
167
- RESPONSE: GetCodeResponse;
168
- };
169
148
  starknet_call: {
170
149
  QUERY: never;
171
150
  REQUEST: any[];
@@ -1,17 +1,17 @@
1
+ /**
2
+ * Common interface response
3
+ * Intersection (sequencer response ∩ (∪ rpc responses))
4
+ */
1
5
  import BN from 'bn.js';
2
6
  import { Abi, CompressedProgram, EntryPointsByType, RawCalldata, Signature, Status } from './lib';
3
7
  export interface GetBlockResponse {
4
- accepted_time: number;
8
+ timestamp: number;
5
9
  block_hash: string;
6
10
  block_number: number;
7
- gas_price: string;
8
11
  new_root: string;
9
- old_root?: string;
10
12
  parent_hash: string;
11
- sequencer: string;
12
13
  status: Status;
13
14
  transactions: Array<string>;
14
- starknet_version?: string;
15
15
  }
16
16
  export interface GetCodeResponse {
17
17
  bytecode: string[];
@@ -12,5 +12,18 @@ export declare function getStarkKey(keyPair: KeyPair): string;
12
12
  * @returns keyPair with public key only, which can be used to verify signatures, but cant sign anything
13
13
  */
14
14
  export declare function getKeyPairFromPublicKey(publicKey: BigNumberish): KeyPair;
15
+ /**
16
+ * Signs a message using the provided key.
17
+ *
18
+ * @param keyPair should be an KeyPair with a valid private key.
19
+ * @returns an Signature.
20
+ */
15
21
  export declare function sign(keyPair: KeyPair, msgHash: string): Signature;
22
+ /**
23
+ * Verifies a message using the provided key.
24
+ *
25
+ * @param keyPair should be an KeyPair with a valid public key.
26
+ * @param sig should be an Signature.
27
+ * @returns true if the verification succeeds.
28
+ */
16
29
  export declare function verify(keyPair: KeyPair | KeyPair[], msgHash: string, sig: Signature): boolean;
@@ -37,11 +37,13 @@ exports.ec = new elliptic_1.ec(new elliptic_1.curves.PresetCurve({
37
37
  gRed: false,
38
38
  g: constants_1.CONSTANT_POINTS[1],
39
39
  }));
40
- /*
41
- The function _truncateToN in lib/elliptic/ec/index.js does a shift-right of 4 bits
42
- in some cases. This function does the opposite operation so that
43
- _truncateToN(fixMessage(msg)) == msg.
44
- */
40
+ /**
41
+ * The function _truncateToN in lib/elliptic/ec/index.js does a shift-right of 4 bits
42
+ * in some cases. This function does the opposite operation so that
43
+ * _truncateToN(fixMessage(msg)) == msg.
44
+ *
45
+ * @param msg
46
+ */
45
47
  function fixMessage(msg) {
46
48
  var pureHex = msg.replace(/^0x0*/, '');
47
49
  if (pureHex.length <= 62) {
@@ -77,11 +79,12 @@ function getKeyPairFromPublicKey(publicKey) {
77
79
  return exports.ec.keyFromPublic((0, encode_1.removeHexPrefix)((0, number_1.toHex)(publicKeyBn)), 'hex');
78
80
  }
79
81
  exports.getKeyPairFromPublicKey = getKeyPairFromPublicKey;
80
- /*
81
- Signs a message using the provided key.
82
- key should be an KeyPair with a valid private key.
83
- Returns an Signature.
84
- */
82
+ /**
83
+ * Signs a message using the provided key.
84
+ *
85
+ * @param keyPair should be an KeyPair with a valid private key.
86
+ * @returns an Signature.
87
+ */
85
88
  function sign(keyPair, msgHash) {
86
89
  var msgHashBN = (0, number_1.toBN)((0, encode_1.addHexPrefix)(msgHash));
87
90
  // Verify message hash has valid length.
@@ -101,12 +104,13 @@ function chunkArray(arr, n) {
101
104
  .fill('')
102
105
  .map(function (_, i) { return arr.slice(i * n, i * n + n); });
103
106
  }
104
- /*
105
- Verifies a message using the provided key.
106
- key should be an KeyPair with a valid public key.
107
- msgSignature should be an Signature.
108
- Returns a boolean true if the verification succeeds.
109
- */
107
+ /**
108
+ * Verifies a message using the provided key.
109
+ *
110
+ * @param keyPair should be an KeyPair with a valid public key.
111
+ * @param sig should be an Signature.
112
+ * @returns true if the verification succeeds.
113
+ */
110
114
  function verify(keyPair, msgHash, sig) {
111
115
  var keyPairArray = Array.isArray(keyPair) ? keyPair : [keyPair];
112
116
  var msgHashBN = (0, number_1.toBN)((0, encode_1.addHexPrefix)(msgHash));
package/utils/hash.js CHANGED
@@ -78,13 +78,15 @@ function pedersen(input) {
78
78
  for (var i = 0; i < input.length; i += 1) {
79
79
  var x = (0, number_1.toBN)(input[i]);
80
80
  (0, minimalistic_assert_1.default)(x.gte(constants_1.ZERO) && x.lt((0, number_1.toBN)((0, encode_1.addHexPrefix)(constants_1.FIELD_PRIME))), "Invalid input: ".concat(input[i]));
81
- for (var j = 0; j < 252; j += 1) {
82
- var pt = constantPoints[2 + i * 252 + j];
83
- (0, minimalistic_assert_1.default)(!point.getX().eq(pt.getX()));
84
- if (x.and(constants_1.ONE).toNumber() !== 0) {
85
- point = point.add(pt);
81
+ if (!x.isZero()) {
82
+ for (var j = 0; j < 252; j += 1) {
83
+ var pt = constantPoints[2 + i * 252 + j];
84
+ (0, minimalistic_assert_1.default)(!point.getX().eq(pt.getX()));
85
+ if (x.and(constants_1.ONE).toNumber() !== 0) {
86
+ point = point.add(pt);
87
+ }
88
+ x = x.shrn(1);
86
89
  }
87
- x = x.shrn(1);
88
90
  }
89
91
  }
90
92
  return (0, encode_1.addHexPrefix)(point.getX().toString(16));
@@ -0,0 +1,10 @@
1
+ export declare class MerkleTree {
2
+ leaves: string[];
3
+ branches: string[][];
4
+ root: string;
5
+ constructor(leafHashes: string[]);
6
+ private build;
7
+ static hash(a: string, b: string): string;
8
+ getProof(leaf: string, branch?: string[], hashPath?: string[]): string[];
9
+ }
10
+ export declare function proofMerklePath(root: string, leaf: string, path: string[]): boolean;
@@ -0,0 +1,90 @@
1
+ "use strict";
2
+ var __read = (this && this.__read) || function (o, n) {
3
+ var m = typeof Symbol === "function" && o[Symbol.iterator];
4
+ if (!m) return o;
5
+ var i = m.call(o), r, ar = [], e;
6
+ try {
7
+ while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
8
+ }
9
+ catch (error) { e = { error: error }; }
10
+ finally {
11
+ try {
12
+ if (r && !r.done && (m = i["return"])) m.call(i);
13
+ }
14
+ finally { if (e) throw e.error; }
15
+ }
16
+ return ar;
17
+ };
18
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
19
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
20
+ if (ar || !(i in from)) {
21
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
22
+ ar[i] = from[i];
23
+ }
24
+ }
25
+ return to.concat(ar || Array.prototype.slice.call(from));
26
+ };
27
+ Object.defineProperty(exports, "__esModule", { value: true });
28
+ exports.proofMerklePath = exports.MerkleTree = void 0;
29
+ var hash_1 = require("./hash");
30
+ var MerkleTree = /** @class */ (function () {
31
+ function MerkleTree(leafHashes) {
32
+ this.branches = [];
33
+ this.leaves = leafHashes;
34
+ this.root = this.build(leafHashes);
35
+ }
36
+ MerkleTree.prototype.build = function (leaves) {
37
+ if (leaves.length === 1) {
38
+ return leaves[0];
39
+ }
40
+ if (leaves.length !== this.leaves.length) {
41
+ this.branches.push(leaves);
42
+ }
43
+ var newLeaves = [];
44
+ for (var i = 0; i < leaves.length; i += 2) {
45
+ if (i + 1 === leaves.length) {
46
+ newLeaves.push(leaves[i]);
47
+ }
48
+ else {
49
+ newLeaves.push(MerkleTree.hash(leaves[i], leaves[i + 1]));
50
+ }
51
+ }
52
+ return this.build(newLeaves);
53
+ };
54
+ MerkleTree.hash = function (a, b) {
55
+ var _a = __read([a, b].sort(), 2), aSorted = _a[0], bSorted = _a[1];
56
+ return (0, hash_1.pedersen)([aSorted, bSorted]);
57
+ };
58
+ MerkleTree.prototype.getProof = function (leaf, branch, hashPath) {
59
+ var _a, _b;
60
+ if (branch === void 0) { branch = this.leaves; }
61
+ if (hashPath === void 0) { hashPath = []; }
62
+ var index = branch.indexOf(leaf);
63
+ if (index === -1) {
64
+ throw new Error('leaf not found');
65
+ }
66
+ if (branch.length === 1) {
67
+ return hashPath;
68
+ }
69
+ var isLeft = index % 2 === 0;
70
+ var neededBranch = (_a = (isLeft ? branch[index + 1] : branch[index - 1])) !== null && _a !== void 0 ? _a : '0x0';
71
+ var newHashPath = __spreadArray(__spreadArray([], __read(hashPath), false), [neededBranch], false);
72
+ var currentBranchLevelIndex = this.leaves.length === branch.length
73
+ ? -1
74
+ : this.branches.findIndex(function (b) { return b.length === branch.length; });
75
+ var nextBranch = (_b = this.branches[currentBranchLevelIndex + 1]) !== null && _b !== void 0 ? _b : [this.root];
76
+ return this.getProof(neededBranch === '0x0'
77
+ ? leaf
78
+ : MerkleTree.hash(isLeft ? leaf : neededBranch, isLeft ? neededBranch : leaf), nextBranch, newHashPath);
79
+ };
80
+ return MerkleTree;
81
+ }());
82
+ exports.MerkleTree = MerkleTree;
83
+ function proofMerklePath(root, leaf, path) {
84
+ if (path.length === 0) {
85
+ return root === leaf;
86
+ }
87
+ var _a = __read(path), next = _a[0], rest = _a.slice(1);
88
+ return proofMerklePath(root, MerkleTree.hash(leaf, next), rest);
89
+ }
90
+ exports.proofMerklePath = proofMerklePath;
package/utils/number.js CHANGED
@@ -31,7 +31,7 @@ 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");
33
33
  function isHex(hex) {
34
- return hex.startsWith('0x');
34
+ return /^0x[0-9a-f]*$/i.test(hex);
35
35
  }
36
36
  exports.isHex = isHex;
37
37
  function toBN(number, base) {
@@ -1,14 +1,24 @@
1
+ /**
2
+ * Map RPC Response to common interface response
3
+ * Intersection (sequencer response ∩ (∪ rpc responses))
4
+ */
1
5
  import { CallContractResponse, DeclareContractResponse, DeployContractResponse, EstimateFeeResponse, GetBlockResponse, GetTransactionReceiptResponse, GetTransactionResponse, InvokeFunctionResponse } from '../../types';
2
6
  import { RPC } from '../../types/api';
3
7
  import { ResponseParser } from '.';
8
+ declare type RpcGetBlockResponse = RPC.GetBlockWithTxHashesResponse & {
9
+ [key: string]: any;
10
+ };
11
+ declare type GetTransactionByHashResponse = RPC.GetTransactionByHashResponse & {
12
+ [key: string]: any;
13
+ };
4
14
  export declare class RPCResponseParser extends ResponseParser {
5
- parseGetBlockResponse(res: RPC.GetBlockResponse): GetBlockResponse;
6
- parseGetTransactionResponse(res: RPC.GetTransactionResponse): GetTransactionResponse;
15
+ parseGetBlockResponse(res: RpcGetBlockResponse): GetBlockResponse;
16
+ parseGetTransactionResponse(res: GetTransactionByHashResponse): GetTransactionResponse;
7
17
  parseGetTransactionReceiptResponse(res: RPC.GetTransactionReceiptResponse): GetTransactionReceiptResponse;
8
- parseGetCodeResponse(res: RPC.GetCodeResponse): RPC.GetCodeResponse;
9
18
  parseFeeEstimateResponse(res: RPC.EstimateFeeResponse): EstimateFeeResponse;
10
19
  parseCallContractResponse(res: Array<string>): CallContractResponse;
11
20
  parseInvokeFunctionResponse(res: RPC.AddTransactionResponse): InvokeFunctionResponse;
12
21
  parseDeployContractResponse(res: RPC.DeployContractResponse): DeployContractResponse;
13
22
  parseDeclareContractResponse(res: RPC.DeclareResponse): DeclareContractResponse;
14
23
  }
24
+ export {};
@@ -25,14 +25,11 @@ var RPCResponseParser = /** @class */ (function (_super) {
25
25
  }
26
26
  RPCResponseParser.prototype.parseGetBlockResponse = function (res) {
27
27
  return {
28
- accepted_time: res.accepted_time,
28
+ timestamp: res.timestamp,
29
29
  block_hash: res.block_hash,
30
30
  block_number: res.block_number,
31
- gas_price: res.gas_price,
32
31
  new_root: res.new_root,
33
- old_root: res.old_root,
34
32
  parent_hash: res.parent_hash,
35
- sequencer: res.sequencer,
36
33
  status: res.status,
37
34
  transactions: res.transactions,
38
35
  };
@@ -41,13 +38,11 @@ var RPCResponseParser = /** @class */ (function (_super) {
41
38
  return {
42
39
  calldata: res.calldata || [],
43
40
  contract_address: res.contract_address,
44
- contract_class: res.contract_class,
45
41
  entry_point_selector: res.entry_point_selector,
46
42
  max_fee: res.max_fee,
47
43
  nonce: res.nonce,
48
- sender_address: res.sender_address,
49
44
  signature: res.signature || [],
50
- transaction_hash: res.txn_hash,
45
+ transaction_hash: res.transaction_hash,
51
46
  version: res.version,
52
47
  };
53
48
  };
@@ -62,9 +57,6 @@ var RPCResponseParser = /** @class */ (function (_super) {
62
57
  events: res.events,
63
58
  };
64
59
  };
65
- RPCResponseParser.prototype.parseGetCodeResponse = function (res) {
66
- return res;
67
- };
68
60
  RPCResponseParser.prototype.parseFeeEstimateResponse = function (res) {
69
61
  return {
70
62
  overall_fee: (0, number_1.toBN)(res.overall_fee),
@@ -1,3 +1,7 @@
1
+ /**
2
+ * Map Sequencer Response to common interface response
3
+ * Intersection (sequencer response ∩ (∪ rpc responses))
4
+ */
1
5
  import { CallContractResponse, DeclareContractResponse, DeployContractResponse, EstimateFeeResponse, GetBlockResponse, GetTransactionReceiptResponse, GetTransactionResponse, InvokeFunctionResponse } from '../../types';
2
6
  import { Sequencer } from '../../types/api';
3
7
  import { ResponseParser } from '.';
@@ -5,7 +9,6 @@ export declare class SequencerAPIResponseParser extends ResponseParser {
5
9
  parseGetBlockResponse(res: Sequencer.GetBlockResponse): GetBlockResponse;
6
10
  parseGetTransactionResponse(res: Sequencer.GetTransactionResponse): GetTransactionResponse;
7
11
  parseGetTransactionReceiptResponse(res: Sequencer.TransactionReceiptResponse): GetTransactionReceiptResponse;
8
- parseGetCodeResponse(res: Sequencer.GetCodeResponse): Sequencer.GetCodeResponse;
9
12
  parseFeeEstimateResponse(res: Sequencer.EstimateFeeResponse): EstimateFeeResponse;
10
13
  parseCallContractResponse(res: Sequencer.CallContractResponse): CallContractResponse;
11
14
  parseInvokeFunctionResponse(res: Sequencer.AddTransactionResponse): InvokeFunctionResponse;