starknet 2.8.0 → 2.9.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/CHANGELOG.md CHANGED
@@ -1,3 +1,13 @@
1
+ # [2.9.0](https://github.com/seanjameshan/starknet.js/compare/v2.8.0...v2.9.0) (2022-02-04)
2
+
3
+ ### Bug Fixes
4
+
5
+ - introduce block identifier type ([75599a9](https://github.com/seanjameshan/starknet.js/commit/75599a99bbcb5723cfc8575b5fbf994a0bbf5b67))
6
+
7
+ ### Features
8
+
9
+ - **utils:** add validateAndParseAddress function ([c067fc4](https://github.com/seanjameshan/starknet.js/commit/c067fc443e4dc9c22b78ed6c093978a2f37debde))
10
+
1
11
  # [2.8.0](https://github.com/seanjameshan/starknet.js/compare/v2.7.2...v2.8.0) (2022-02-02)
2
12
 
3
13
  ### Features
@@ -21,7 +21,7 @@ describe('defaultProvider', () => {
21
21
  ).resolves.not.toThrow();
22
22
  });
23
23
  test('getBlock(blockHash=undefined, blockNumber=36657)', () => {
24
- return expect(defaultProvider.getBlock(undefined, 36657)).resolves.not.toThrow();
24
+ return expect(defaultProvider.getBlock(36657)).resolves.not.toThrow();
25
25
  });
26
26
  test('getBlock(blockHash=undefined, blockNumber=null)', () => {
27
27
  return expect(defaultProvider.getBlock()).resolves.not.toThrow();
@@ -34,7 +34,6 @@ describe('defaultProvider', () => {
34
34
  return expect(
35
35
  defaultProvider.getCode(
36
36
  '0x01d1f307c073bb786a66e6e042ec2a9bdc385a3373bb3738d95b966d5ce56166',
37
- undefined,
38
37
  36663
39
38
  )
40
39
  ).resolves.not.toThrow();
@@ -51,7 +50,6 @@ describe('defaultProvider', () => {
51
50
  defaultProvider.getStorageAt(
52
51
  '0x01d1f307c073bb786a66e6e042ec2a9bdc385a3373bb3738d95b966d5ce56166',
53
52
  0,
54
- undefined,
55
53
  36663
56
54
  )
57
55
  ).resolves.not.toThrow();
@@ -0,0 +1,16 @@
1
+ import { addAddressPadding, validateAndParseAddress } from '../../src/utils/address';
2
+ // import { addHexPrefix, removeHexPrefix } from '../../src/utils/encode';
3
+
4
+ describe('validateAndParseAddress', () => {
5
+ test('should pass when correct starknet address is passed', () => {
6
+ const addr = '0x7ee790591d9fa3efc87067d95a643f8455e0b8190eb8cb7bfd39e4fb7571fdf';
7
+
8
+ return expect(validateAndParseAddress(addr)).toEqual(`${addAddressPadding(addr)}`);
9
+ });
10
+
11
+ test('should add 0x prefix if not provided', () => {
12
+ const addr = '0x6eff1d71068df8e6677f59a556151c56ed13e14ad431a9bef6fcb3fc5e6fa7';
13
+
14
+ return expect(validateAndParseAddress(addr)).toEqual(`${addAddressPadding(addr)}`);
15
+ });
16
+ });
package/constants.d.ts CHANGED
@@ -4,6 +4,7 @@ export declare const ZERO: import('bn.js');
4
4
  export declare const ONE: import('bn.js');
5
5
  export declare const TWO: import('bn.js');
6
6
  export declare const MASK_250: import('bn.js');
7
+ export declare const MASK_251: import('bn.js');
7
8
  /**
8
9
  * The following is taken from https://github.com/starkware-libs/starkex-resources/blob/master/crypto/starkware/crypto/signature/pedersen_params.json but converted to hex, because JS is very bad handling big integers by default
9
10
  * Please do not edit until the JSON changes.
@@ -11,6 +12,7 @@ export declare const MASK_250: import('bn.js');
11
12
  export declare const FIELD_PRIME =
12
13
  '800000000000011000000000000000000000000000000000000000000000001';
13
14
  export declare const FIELD_GEN = '3';
15
+ export declare const FIELD_SIZE = 251;
14
16
  export declare const EC_ORDER = '800000000000010FFFFFFFFFFFFFFFFB781126DCAE7B2321E66A241ADC64D2F';
15
17
  export declare const ALPHA = '1';
16
18
  export declare const BETA = '6F21413EFBE40DE150E596D72F7A8C5609AD26C15C915C1F4CDFCB99CEE9E89';
package/constants.js CHANGED
@@ -5,8 +5,10 @@ exports.CONSTANT_POINTS =
5
5
  exports.BETA =
6
6
  exports.ALPHA =
7
7
  exports.EC_ORDER =
8
+ exports.FIELD_SIZE =
8
9
  exports.FIELD_GEN =
9
10
  exports.FIELD_PRIME =
11
+ exports.MASK_251 =
10
12
  exports.MASK_250 =
11
13
  exports.TWO =
12
14
  exports.ONE =
@@ -25,12 +27,14 @@ exports.ZERO = (0, number_1.toBN)(0);
25
27
  exports.ONE = (0, number_1.toBN)(1);
26
28
  exports.TWO = (0, number_1.toBN)(2);
27
29
  exports.MASK_250 = exports.TWO.pow((0, number_1.toBN)(250)).sub(exports.ONE); // 2 ** 250 - 1
30
+ exports.MASK_251 = exports.TWO.pow((0, number_1.toBN)(251));
28
31
  /**
29
32
  * The following is taken from https://github.com/starkware-libs/starkex-resources/blob/master/crypto/starkware/crypto/signature/pedersen_params.json but converted to hex, because JS is very bad handling big integers by default
30
33
  * Please do not edit until the JSON changes.
31
34
  */
32
35
  exports.FIELD_PRIME = '800000000000011000000000000000000000000000000000000000000000001';
33
36
  exports.FIELD_GEN = '3';
37
+ exports.FIELD_SIZE = 251;
34
38
  exports.EC_ORDER = '800000000000010FFFFFFFFFFFFFFFFB781126DCAE7B2321E66A241ADC64D2F';
35
39
  exports.ALPHA = '1';
36
40
  exports.BETA = '6F21413EFBE40DE150E596D72F7A8C5609AD26C15C915C1F4CDFCB99CEE9E89';
@@ -4,12 +4,14 @@ export declare const ZERO: import("bn.js");
4
4
  export declare const ONE: import("bn.js");
5
5
  export declare const TWO: import("bn.js");
6
6
  export declare const MASK_250: import("bn.js");
7
+ export declare const MASK_251: import("bn.js");
7
8
  /**
8
9
  * The following is taken from https://github.com/starkware-libs/starkex-resources/blob/master/crypto/starkware/crypto/signature/pedersen_params.json but converted to hex, because JS is very bad handling big integers by default
9
10
  * Please do not edit until the JSON changes.
10
11
  */
11
12
  export declare const FIELD_PRIME = "800000000000011000000000000000000000000000000000000000000000001";
12
13
  export declare const FIELD_GEN = "3";
14
+ export declare const FIELD_SIZE = 251;
13
15
  export declare const EC_ORDER = "800000000000010FFFFFFFFFFFFFFFFB781126DCAE7B2321E66A241ADC64D2F";
14
16
  export declare const ALPHA = "1";
15
17
  export declare const BETA = "6F21413EFBE40DE150E596D72F7A8C5609AD26C15C915C1F4CDFCB99CEE9E89";
package/dist/constants.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.CONSTANT_POINTS = exports.MAX_ECDSA_VAL = exports.BETA = exports.ALPHA = exports.EC_ORDER = exports.FIELD_GEN = exports.FIELD_PRIME = exports.MASK_250 = exports.TWO = exports.ONE = exports.ZERO = exports.IS_BROWSER = void 0;
3
+ exports.CONSTANT_POINTS = exports.MAX_ECDSA_VAL = exports.BETA = exports.ALPHA = exports.EC_ORDER = exports.FIELD_SIZE = exports.FIELD_GEN = exports.FIELD_PRIME = exports.MASK_251 = exports.MASK_250 = exports.TWO = exports.ONE = exports.ZERO = exports.IS_BROWSER = void 0;
4
4
  var number_1 = require("./utils/number");
5
5
  var encode_1 = require("./utils/encode");
6
6
  Object.defineProperty(exports, "IS_BROWSER", { enumerable: true, get: function () { return encode_1.IS_BROWSER; } });
@@ -8,12 +8,14 @@ exports.ZERO = (0, number_1.toBN)(0);
8
8
  exports.ONE = (0, number_1.toBN)(1);
9
9
  exports.TWO = (0, number_1.toBN)(2);
10
10
  exports.MASK_250 = exports.TWO.pow((0, number_1.toBN)(250)).sub(exports.ONE); // 2 ** 250 - 1
11
+ exports.MASK_251 = exports.TWO.pow((0, number_1.toBN)(251));
11
12
  /**
12
13
  * The following is taken from https://github.com/starkware-libs/starkex-resources/blob/master/crypto/starkware/crypto/signature/pedersen_params.json but converted to hex, because JS is very bad handling big integers by default
13
14
  * Please do not edit until the JSON changes.
14
15
  */
15
16
  exports.FIELD_PRIME = '800000000000011000000000000000000000000000000000000000000000001';
16
17
  exports.FIELD_GEN = '3';
18
+ exports.FIELD_SIZE = 251;
17
19
  exports.EC_ORDER = '800000000000010FFFFFFFFFFFFFFFFB781126DCAE7B2321E66A241ADC64D2F';
18
20
  exports.ALPHA = '1';
19
21
  exports.BETA = '6F21413EFBE40DE150E596D72F7A8C5609AD26C15C915C1F4CDFCB99CEE9E89';
package/dist/index.d.ts CHANGED
@@ -18,3 +18,4 @@ export * as ec from './utils/ellipticCurve';
18
18
  export * as uint256 from './utils/uint256';
19
19
  export * as shortString from './utils/shortString';
20
20
  export * as typedData from './utils/typedData';
21
+ export * from './utils/address';
package/dist/index.js CHANGED
@@ -43,3 +43,4 @@ exports.ec = __importStar(require("./utils/ellipticCurve"));
43
43
  exports.uint256 = __importStar(require("./utils/uint256"));
44
44
  exports.shortString = __importStar(require("./utils/shortString"));
45
45
  exports.typedData = __importStar(require("./utils/typedData"));
46
+ __exportStar(require("./utils/address"), exports);
@@ -1,6 +1,7 @@
1
- import { AddTransactionResponse, BlockNumber, CallContractResponse, CallContractTransaction, CompiledContract, GetBlockResponse, GetCodeResponse, GetContractAddressesResponse, GetTransactionResponse, GetTransactionStatusResponse, Signature, Transaction } from '../types';
1
+ import { AddTransactionResponse, CallContractResponse, CallContractTransaction, CompiledContract, GetBlockResponse, GetCodeResponse, GetContractAddressesResponse, GetTransactionResponse, GetTransactionStatusResponse, Signature, Transaction } from '../types';
2
2
  import { BigNumberish } from '../utils/number';
3
3
  import { ProviderInterface } from './interface';
4
+ import { BlockIdentifier } from './utils';
4
5
  declare type NetworkName = 'mainnet-alpha' | 'goerli-alpha';
5
6
  declare type ProviderOptions = {
6
7
  network: NetworkName;
@@ -30,7 +31,7 @@ export declare class Provider implements ProviderInterface {
30
31
  * @param blockNumber
31
32
  * @returns the result of the function on the smart contract.
32
33
  */
33
- callContract(invokeTransaction: CallContractTransaction, blockHash?: BigNumberish, blockNumber?: BlockNumber): Promise<CallContractResponse>;
34
+ callContract(invokeTransaction: CallContractTransaction, blockIdentifier?: BlockIdentifier): Promise<CallContractResponse>;
34
35
  /**
35
36
  * Gets the block information
36
37
  *
@@ -40,7 +41,7 @@ export declare class Provider implements ProviderInterface {
40
41
  * @param blockNumber
41
42
  * @returns the block object { block_number, previous_block_number, state_root, status, timestamp, transaction_receipts, transactions }
42
43
  */
43
- getBlock(blockHash?: BigNumberish, blockNumber?: BlockNumber): Promise<GetBlockResponse>;
44
+ getBlock(blockIdentifier?: BlockIdentifier): Promise<GetBlockResponse>;
44
45
  /**
45
46
  * Gets the code of the deployed contract.
46
47
  *
@@ -51,7 +52,7 @@ export declare class Provider implements ProviderInterface {
51
52
  * @param blockNumber
52
53
  * @returns Bytecode and ABI of compiled contract
53
54
  */
54
- getCode(contractAddress: string, blockHash?: BigNumberish, blockNumber?: BlockNumber): Promise<GetCodeResponse>;
55
+ getCode(contractAddress: string, blockIdentifier?: BlockIdentifier): Promise<GetCodeResponse>;
55
56
  /**
56
57
  * Gets the contract's storage variable at a specific key.
57
58
  *
@@ -63,7 +64,7 @@ export declare class Provider implements ProviderInterface {
63
64
  * @param blockNumber
64
65
  * @returns the value of the storage variable
65
66
  */
66
- getStorageAt(contractAddress: string, key: number, blockHash?: BigNumberish, blockNumber?: BlockNumber): Promise<object>;
67
+ getStorageAt(contractAddress: string, key: number, blockIdentifier?: BlockIdentifier): Promise<object>;
67
68
  /**
68
69
  * Gets the status of a transaction.
69
70
  *
@@ -115,14 +115,14 @@ var Provider = /** @class */ (function () {
115
115
  * @param blockNumber
116
116
  * @returns the result of the function on the smart contract.
117
117
  */
118
- Provider.prototype.callContract = function (invokeTransaction, blockHash, blockNumber) {
119
- if (blockNumber === void 0) { blockNumber = null; }
118
+ Provider.prototype.callContract = function (invokeTransaction, blockIdentifier) {
119
+ if (blockIdentifier === void 0) { blockIdentifier = null; }
120
120
  return __awaiter(this, void 0, void 0, function () {
121
121
  var formattedBlockIdentifier, data;
122
122
  return __generator(this, function (_a) {
123
123
  switch (_a.label) {
124
124
  case 0:
125
- formattedBlockIdentifier = (0, utils_1.getFormattedBlockIdentifier)(blockHash, blockNumber);
125
+ formattedBlockIdentifier = (0, utils_1.getFormattedBlockIdentifier)(blockIdentifier);
126
126
  return [4 /*yield*/, axios_1.default.post((0, url_join_1.default)(this.feederGatewayUrl, 'call_contract', formattedBlockIdentifier), __assign({ signature: [], calldata: [] }, invokeTransaction))];
127
127
  case 1:
128
128
  data = (_a.sent()).data;
@@ -140,14 +140,14 @@ var Provider = /** @class */ (function () {
140
140
  * @param blockNumber
141
141
  * @returns the block object { block_number, previous_block_number, state_root, status, timestamp, transaction_receipts, transactions }
142
142
  */
143
- Provider.prototype.getBlock = function (blockHash, blockNumber) {
144
- if (blockNumber === void 0) { blockNumber = null; }
143
+ Provider.prototype.getBlock = function (blockIdentifier) {
144
+ if (blockIdentifier === void 0) { blockIdentifier = null; }
145
145
  return __awaiter(this, void 0, void 0, function () {
146
146
  var formattedBlockIdentifier, data;
147
147
  return __generator(this, function (_a) {
148
148
  switch (_a.label) {
149
149
  case 0:
150
- formattedBlockIdentifier = (0, utils_1.getFormattedBlockIdentifier)(blockHash, blockNumber);
150
+ formattedBlockIdentifier = (0, utils_1.getFormattedBlockIdentifier)(blockIdentifier);
151
151
  return [4 /*yield*/, axios_1.default.get((0, url_join_1.default)(this.feederGatewayUrl, 'get_block', formattedBlockIdentifier))];
152
152
  case 1:
153
153
  data = (_a.sent()).data;
@@ -166,14 +166,14 @@ var Provider = /** @class */ (function () {
166
166
  * @param blockNumber
167
167
  * @returns Bytecode and ABI of compiled contract
168
168
  */
169
- Provider.prototype.getCode = function (contractAddress, blockHash, blockNumber) {
170
- if (blockNumber === void 0) { blockNumber = null; }
169
+ Provider.prototype.getCode = function (contractAddress, blockIdentifier) {
170
+ if (blockIdentifier === void 0) { blockIdentifier = null; }
171
171
  return __awaiter(this, void 0, void 0, function () {
172
172
  var formattedBlockIdentifier, data;
173
173
  return __generator(this, function (_a) {
174
174
  switch (_a.label) {
175
175
  case 0:
176
- formattedBlockIdentifier = (0, utils_1.getFormattedBlockIdentifier)(blockHash, blockNumber);
176
+ formattedBlockIdentifier = (0, utils_1.getFormattedBlockIdentifier)(blockIdentifier);
177
177
  return [4 /*yield*/, axios_1.default.get((0, url_join_1.default)(this.feederGatewayUrl, 'get_code', "?contractAddress=" + contractAddress + "&" + formattedBlockIdentifier))];
178
178
  case 1:
179
179
  data = (_a.sent()).data;
@@ -194,14 +194,14 @@ var Provider = /** @class */ (function () {
194
194
  * @param blockNumber
195
195
  * @returns the value of the storage variable
196
196
  */
197
- Provider.prototype.getStorageAt = function (contractAddress, key, blockHash, blockNumber) {
198
- if (blockNumber === void 0) { blockNumber = null; }
197
+ Provider.prototype.getStorageAt = function (contractAddress, key, blockIdentifier) {
198
+ if (blockIdentifier === void 0) { blockIdentifier = null; }
199
199
  return __awaiter(this, void 0, void 0, function () {
200
200
  var formattedBlockIdentifier, data;
201
201
  return __generator(this, function (_a) {
202
202
  switch (_a.label) {
203
203
  case 0:
204
- formattedBlockIdentifier = (0, utils_1.getFormattedBlockIdentifier)(blockHash, blockNumber);
204
+ formattedBlockIdentifier = (0, utils_1.getFormattedBlockIdentifier)(blockIdentifier);
205
205
  return [4 /*yield*/, axios_1.default.get((0, url_join_1.default)(this.feederGatewayUrl, 'get_storage_at', "?contractAddress=" + contractAddress + "&key=" + key + "&" + formattedBlockIdentifier))];
206
206
  case 1:
207
207
  data = (_a.sent()).data;
@@ -1,5 +1,6 @@
1
- import type { AddTransactionResponse, BlockNumber, CallContractResponse, CallContractTransaction, CompiledContract, GetBlockResponse, GetCodeResponse, GetContractAddressesResponse, GetTransactionResponse, GetTransactionStatusResponse, Signature, Transaction } from '../types';
1
+ import type { AddTransactionResponse, CallContractResponse, CallContractTransaction, CompiledContract, GetBlockResponse, GetCodeResponse, GetContractAddressesResponse, GetTransactionResponse, GetTransactionStatusResponse, Signature, Transaction } from '../types';
2
2
  import type { BigNumberish } from '../utils/number';
3
+ import { BlockIdentifier } from './utils';
3
4
  export declare abstract class ProviderInterface {
4
5
  abstract baseUrl: string;
5
6
  abstract feederGatewayUrl: string;
@@ -17,32 +18,29 @@ export declare abstract class ProviderInterface {
17
18
  * [Reference](https://github.com/starkware-libs/cairo-lang/blob/fc97bdd8322a7df043c87c371634b26c15ed6cee/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L25-L39)
18
19
  *
19
20
  * @param invokeTransaction - transaction to be invoked
20
- * @param blockHash
21
- * @param blockNumber
21
+ * @param blockIdentifier - block identifier
22
22
  * @returns the result of the function on the smart contract.
23
23
  */
24
- abstract callContract(invokeTransaction: CallContractTransaction, blockHash?: BigNumberish, blockNumber?: BlockNumber): Promise<CallContractResponse>;
24
+ abstract callContract(invokeTransaction: CallContractTransaction, blockIdentifier?: BlockIdentifier): Promise<CallContractResponse>;
25
25
  /**
26
26
  * Gets the block information
27
27
  *
28
28
  * [Reference](https://github.com/starkware-libs/cairo-lang/blob/fc97bdd8322a7df043c87c371634b26c15ed6cee/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L41-L53)
29
29
  *
30
- * @param blockHash
31
- * @param blockNumber
30
+ * @param blockIdentifier - block identifier
32
31
  * @returns the block object { block_number, previous_block_number, state_root, status, timestamp, transaction_receipts, transactions }
33
32
  */
34
- abstract getBlock(blockHash?: BigNumberish, blockNumber?: BlockNumber): Promise<GetBlockResponse>;
33
+ abstract getBlock(blockIdentifier?: BlockIdentifier): Promise<GetBlockResponse>;
35
34
  /**
36
35
  * Gets the code of the deployed contract.
37
36
  *
38
37
  * [Reference](https://github.com/starkware-libs/cairo-lang/blob/fc97bdd8322a7df043c87c371634b26c15ed6cee/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L55-L68)
39
38
  *
40
- * @param contractAddress
41
- * @param blockHash
42
- * @param blockNumber
39
+ * @param contractAddress - contract address
40
+ * @param blockIdentifier - block identifier
43
41
  * @returns Bytecode and ABI of compiled contract
44
42
  */
45
- abstract getCode(contractAddress: string, blockHash?: BigNumberish, blockNumber?: BlockNumber): Promise<GetCodeResponse>;
43
+ abstract getCode(contractAddress: string, blockIdentifier?: BlockIdentifier): Promise<GetCodeResponse>;
46
44
  /**
47
45
  * Gets the contract's storage variable at a specific key.
48
46
  *
@@ -50,11 +48,10 @@ export declare abstract class ProviderInterface {
50
48
  *
51
49
  * @param contractAddress
52
50
  * @param key - from getStorageVarAddress('<STORAGE_VARIABLE_NAME>') (WIP)
53
- * @param blockHash
54
- * @param blockNumber
51
+ * @param blockIdentifier - block identifier
55
52
  * @returns the value of the storage variable
56
53
  */
57
- abstract getStorageAt(contractAddress: string, key: number, blockHash?: BigNumberish, blockNumber?: BlockNumber): Promise<object>;
54
+ abstract getStorageAt(contractAddress: string, key: number, blockIdentifier?: BlockIdentifier): Promise<object>;
58
55
  /**
59
56
  * Gets the status of a transaction.
60
57
  *
@@ -15,6 +15,21 @@ export declare function formatHash(): void;
15
15
  * @param txId
16
16
  */
17
17
  export declare function txIdentifier(): void;
18
+ export declare type BlockIdentifier = BlockNumber | BigNumberish;
19
+ declare type BlockIdentifierObject = {
20
+ type: 'BLOCK_NUMBER';
21
+ data: BlockNumber;
22
+ } | {
23
+ type: 'BLOCK_HASH';
24
+ data: BigNumberish;
25
+ };
26
+ /**
27
+ * Identifies the block to be queried.
28
+ *
29
+ * @param blockIdentifier - block identifier
30
+ * @returns block identifier object
31
+ */
32
+ export declare function getBlockIdentifier(blockIdentifier: BlockIdentifier): BlockIdentifierObject;
18
33
  /**
19
34
  * Gets the block identifier for API request
20
35
  *
@@ -24,4 +39,5 @@ export declare function txIdentifier(): void;
24
39
  * @param blockHash
25
40
  * @returns block identifier for API request
26
41
  */
27
- export declare function getFormattedBlockIdentifier(blockHash?: BigNumberish, blockNumber?: BlockNumber): string;
42
+ export declare function getFormattedBlockIdentifier(blockIdentifier?: BlockIdentifier): string;
43
+ export {};
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getFormattedBlockIdentifier = exports.txIdentifier = exports.formatHash = void 0;
3
+ exports.getFormattedBlockIdentifier = exports.getBlockIdentifier = exports.txIdentifier = exports.formatHash = void 0;
4
+ var number_1 = require("../utils/number");
4
5
  /**
5
6
  * TODO
6
7
  * [Reference](https://github.com/starkware-libs/cairo-lang/blob/fc97bdd8322a7df043c87c371634b26c15ed6cee/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L148-L153)
@@ -18,6 +19,34 @@ exports.formatHash = formatHash;
18
19
  */
19
20
  function txIdentifier() { }
20
21
  exports.txIdentifier = txIdentifier;
22
+ /**
23
+ * Identifies the block to be queried.
24
+ *
25
+ * @param blockIdentifier - block identifier
26
+ * @returns block identifier object
27
+ */
28
+ function getBlockIdentifier(blockIdentifier) {
29
+ if (typeof blockIdentifier === 'number') {
30
+ return { type: 'BLOCK_NUMBER', data: blockIdentifier };
31
+ }
32
+ if (typeof blockIdentifier === 'string' && blockIdentifier.startsWith('0x')) {
33
+ return { type: 'BLOCK_HASH', data: blockIdentifier };
34
+ }
35
+ if (typeof blockIdentifier === 'string' && !Number.isNaN(parseInt(blockIdentifier, 10))) {
36
+ return { type: 'BLOCK_NUMBER', data: parseInt(blockIdentifier, 10) };
37
+ }
38
+ if (blockIdentifier === null) {
39
+ return { type: 'BLOCK_NUMBER', data: null };
40
+ }
41
+ if (blockIdentifier === 'pending') {
42
+ return { type: 'BLOCK_NUMBER', data: 'pending' };
43
+ }
44
+ if (typeof blockIdentifier === 'string') {
45
+ throw new Error("Invalid block identifier: " + blockIdentifier);
46
+ }
47
+ return { type: 'BLOCK_HASH', data: blockIdentifier };
48
+ }
49
+ exports.getBlockIdentifier = getBlockIdentifier;
21
50
  /**
22
51
  * Gets the block identifier for API request
23
52
  *
@@ -27,11 +56,15 @@ exports.txIdentifier = txIdentifier;
27
56
  * @param blockHash
28
57
  * @returns block identifier for API request
29
58
  */
30
- function getFormattedBlockIdentifier(blockHash, blockNumber) {
31
- if (blockNumber === void 0) { blockNumber = null; }
32
- if (blockHash) {
33
- return "?blockHash=" + blockHash;
59
+ function getFormattedBlockIdentifier(blockIdentifier) {
60
+ if (blockIdentifier === void 0) { blockIdentifier = null; }
61
+ var blockIdentifierObject = getBlockIdentifier(blockIdentifier);
62
+ if (blockIdentifierObject.type === 'BLOCK_NUMBER' && blockIdentifierObject.data === null) {
63
+ return '';
64
+ }
65
+ if (blockIdentifierObject.type === 'BLOCK_NUMBER') {
66
+ return "?blockNumber=" + blockIdentifierObject.data;
34
67
  }
35
- return "?blockNumber=" + blockNumber;
68
+ return "?blockHash=" + (0, number_1.toHex)((0, number_1.toBN)(blockIdentifierObject.data));
36
69
  }
37
70
  exports.getFormattedBlockIdentifier = getFormattedBlockIdentifier;
@@ -0,0 +1,2 @@
1
+ export declare function addAddressPadding(address: string): string;
2
+ export declare function validateAndParseAddress(address: string): string;
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.validateAndParseAddress = exports.addAddressPadding = void 0;
4
+ var constants_1 = require("../constants");
5
+ var encode_1 = require("./encode");
6
+ var number_1 = require("./number");
7
+ function addAddressPadding(address) {
8
+ return (0, encode_1.addHexPrefix)((0, encode_1.removeHexPrefix)(address).padStart(64, '0'));
9
+ }
10
+ exports.addAddressPadding = addAddressPadding;
11
+ function validateAndParseAddress(address) {
12
+ if (typeof address !== 'string') {
13
+ throw new Error('Invalid Address Type');
14
+ }
15
+ (0, number_1.assertInRange)(address, constants_1.ZERO, constants_1.MASK_251, 'Starknet Address');
16
+ var result = addAddressPadding(address);
17
+ if (!result.match(/^(0x)?[0-9a-fA-F]{64}$/)) {
18
+ throw new Error('Invalid Address Format');
19
+ }
20
+ return result;
21
+ }
22
+ exports.validateAndParseAddress = validateAndParseAddress;
package/index.d.ts CHANGED
@@ -18,3 +18,4 @@ export * as ec from './utils/ellipticCurve';
18
18
  export * as uint256 from './utils/uint256';
19
19
  export * as shortString from './utils/shortString';
20
20
  export * as typedData from './utils/typedData';
21
+ export * from './utils/address';
package/index.js CHANGED
@@ -75,3 +75,4 @@ exports.ec = __importStar(require('./utils/ellipticCurve'));
75
75
  exports.uint256 = __importStar(require('./utils/uint256'));
76
76
  exports.shortString = __importStar(require('./utils/shortString'));
77
77
  exports.typedData = __importStar(require('./utils/typedData'));
78
+ __exportStar(require('./utils/address'), exports);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "starknet",
3
- "version": "2.8.0",
3
+ "version": "2.9.0",
4
4
  "description": "JavaScript library for StarkNet",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -1,6 +1,5 @@
1
1
  import {
2
2
  AddTransactionResponse,
3
- BlockNumber,
4
3
  CallContractResponse,
5
4
  CallContractTransaction,
6
5
  CompiledContract,
@@ -14,6 +13,7 @@ import {
14
13
  } from '../types';
15
14
  import { BigNumberish } from '../utils/number';
16
15
  import { ProviderInterface } from './interface';
16
+ import { BlockIdentifier } from './utils';
17
17
  declare type NetworkName = 'mainnet-alpha' | 'goerli-alpha';
18
18
  declare type ProviderOptions =
19
19
  | {
@@ -49,8 +49,7 @@ export declare class Provider implements ProviderInterface {
49
49
  */
50
50
  callContract(
51
51
  invokeTransaction: CallContractTransaction,
52
- blockHash?: BigNumberish,
53
- blockNumber?: BlockNumber
52
+ blockIdentifier?: BlockIdentifier
54
53
  ): Promise<CallContractResponse>;
55
54
  /**
56
55
  * Gets the block information
@@ -61,7 +60,7 @@ export declare class Provider implements ProviderInterface {
61
60
  * @param blockNumber
62
61
  * @returns the block object { block_number, previous_block_number, state_root, status, timestamp, transaction_receipts, transactions }
63
62
  */
64
- getBlock(blockHash?: BigNumberish, blockNumber?: BlockNumber): Promise<GetBlockResponse>;
63
+ getBlock(blockIdentifier?: BlockIdentifier): Promise<GetBlockResponse>;
65
64
  /**
66
65
  * Gets the code of the deployed contract.
67
66
  *
@@ -72,11 +71,7 @@ export declare class Provider implements ProviderInterface {
72
71
  * @param blockNumber
73
72
  * @returns Bytecode and ABI of compiled contract
74
73
  */
75
- getCode(
76
- contractAddress: string,
77
- blockHash?: BigNumberish,
78
- blockNumber?: BlockNumber
79
- ): Promise<GetCodeResponse>;
74
+ getCode(contractAddress: string, blockIdentifier?: BlockIdentifier): Promise<GetCodeResponse>;
80
75
  /**
81
76
  * Gets the contract's storage variable at a specific key.
82
77
  *
@@ -91,8 +86,7 @@ export declare class Provider implements ProviderInterface {
91
86
  getStorageAt(
92
87
  contractAddress: string,
93
88
  key: number,
94
- blockHash?: BigNumberish,
95
- blockNumber?: BlockNumber
89
+ blockIdentifier?: BlockIdentifier
96
90
  ): Promise<object>;
97
91
  /**
98
92
  * Gets the status of a transaction.
@@ -225,19 +225,16 @@ var Provider = /** @class */ (function () {
225
225
  * @param blockNumber
226
226
  * @returns the result of the function on the smart contract.
227
227
  */
228
- Provider.prototype.callContract = function (invokeTransaction, blockHash, blockNumber) {
229
- if (blockNumber === void 0) {
230
- blockNumber = null;
228
+ Provider.prototype.callContract = function (invokeTransaction, blockIdentifier) {
229
+ if (blockIdentifier === void 0) {
230
+ blockIdentifier = null;
231
231
  }
232
232
  return __awaiter(this, void 0, void 0, function () {
233
233
  var formattedBlockIdentifier, data;
234
234
  return __generator(this, function (_a) {
235
235
  switch (_a.label) {
236
236
  case 0:
237
- formattedBlockIdentifier = (0, utils_1.getFormattedBlockIdentifier)(
238
- blockHash,
239
- blockNumber
240
- );
237
+ formattedBlockIdentifier = (0, utils_1.getFormattedBlockIdentifier)(blockIdentifier);
241
238
  return [
242
239
  4 /*yield*/,
243
240
  axios_1.default.post(
@@ -265,19 +262,16 @@ var Provider = /** @class */ (function () {
265
262
  * @param blockNumber
266
263
  * @returns the block object { block_number, previous_block_number, state_root, status, timestamp, transaction_receipts, transactions }
267
264
  */
268
- Provider.prototype.getBlock = function (blockHash, blockNumber) {
269
- if (blockNumber === void 0) {
270
- blockNumber = null;
265
+ Provider.prototype.getBlock = function (blockIdentifier) {
266
+ if (blockIdentifier === void 0) {
267
+ blockIdentifier = null;
271
268
  }
272
269
  return __awaiter(this, void 0, void 0, function () {
273
270
  var formattedBlockIdentifier, data;
274
271
  return __generator(this, function (_a) {
275
272
  switch (_a.label) {
276
273
  case 0:
277
- formattedBlockIdentifier = (0, utils_1.getFormattedBlockIdentifier)(
278
- blockHash,
279
- blockNumber
280
- );
274
+ formattedBlockIdentifier = (0, utils_1.getFormattedBlockIdentifier)(blockIdentifier);
281
275
  return [
282
276
  4 /*yield*/,
283
277
  axios_1.default.get(
@@ -305,19 +299,16 @@ var Provider = /** @class */ (function () {
305
299
  * @param blockNumber
306
300
  * @returns Bytecode and ABI of compiled contract
307
301
  */
308
- Provider.prototype.getCode = function (contractAddress, blockHash, blockNumber) {
309
- if (blockNumber === void 0) {
310
- blockNumber = null;
302
+ Provider.prototype.getCode = function (contractAddress, blockIdentifier) {
303
+ if (blockIdentifier === void 0) {
304
+ blockIdentifier = null;
311
305
  }
312
306
  return __awaiter(this, void 0, void 0, function () {
313
307
  var formattedBlockIdentifier, data;
314
308
  return __generator(this, function (_a) {
315
309
  switch (_a.label) {
316
310
  case 0:
317
- formattedBlockIdentifier = (0, utils_1.getFormattedBlockIdentifier)(
318
- blockHash,
319
- blockNumber
320
- );
311
+ formattedBlockIdentifier = (0, utils_1.getFormattedBlockIdentifier)(blockIdentifier);
321
312
  return [
322
313
  4 /*yield*/,
323
314
  axios_1.default.get(
@@ -347,19 +338,16 @@ var Provider = /** @class */ (function () {
347
338
  * @param blockNumber
348
339
  * @returns the value of the storage variable
349
340
  */
350
- Provider.prototype.getStorageAt = function (contractAddress, key, blockHash, blockNumber) {
351
- if (blockNumber === void 0) {
352
- blockNumber = null;
341
+ Provider.prototype.getStorageAt = function (contractAddress, key, blockIdentifier) {
342
+ if (blockIdentifier === void 0) {
343
+ blockIdentifier = null;
353
344
  }
354
345
  return __awaiter(this, void 0, void 0, function () {
355
346
  var formattedBlockIdentifier, data;
356
347
  return __generator(this, function (_a) {
357
348
  switch (_a.label) {
358
349
  case 0:
359
- formattedBlockIdentifier = (0, utils_1.getFormattedBlockIdentifier)(
360
- blockHash,
361
- blockNumber
362
- );
350
+ formattedBlockIdentifier = (0, utils_1.getFormattedBlockIdentifier)(blockIdentifier);
363
351
  return [
364
352
  4 /*yield*/,
365
353
  axios_1.default.get(
@@ -1,6 +1,5 @@
1
1
  import type {
2
2
  AddTransactionResponse,
3
- BlockNumber,
4
3
  CallContractResponse,
5
4
  CallContractTransaction,
6
5
  CompiledContract,
@@ -13,6 +12,7 @@ import type {
13
12
  Transaction,
14
13
  } from '../types';
15
14
  import type { BigNumberish } from '../utils/number';
15
+ import { BlockIdentifier } from './utils';
16
16
  export declare abstract class ProviderInterface {
17
17
  abstract baseUrl: string;
18
18
  abstract feederGatewayUrl: string;
@@ -30,39 +30,34 @@ export declare abstract class ProviderInterface {
30
30
  * [Reference](https://github.com/starkware-libs/cairo-lang/blob/fc97bdd8322a7df043c87c371634b26c15ed6cee/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L25-L39)
31
31
  *
32
32
  * @param invokeTransaction - transaction to be invoked
33
- * @param blockHash
34
- * @param blockNumber
33
+ * @param blockIdentifier - block identifier
35
34
  * @returns the result of the function on the smart contract.
36
35
  */
37
36
  abstract callContract(
38
37
  invokeTransaction: CallContractTransaction,
39
- blockHash?: BigNumberish,
40
- blockNumber?: BlockNumber
38
+ blockIdentifier?: BlockIdentifier
41
39
  ): Promise<CallContractResponse>;
42
40
  /**
43
41
  * Gets the block information
44
42
  *
45
43
  * [Reference](https://github.com/starkware-libs/cairo-lang/blob/fc97bdd8322a7df043c87c371634b26c15ed6cee/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L41-L53)
46
44
  *
47
- * @param blockHash
48
- * @param blockNumber
45
+ * @param blockIdentifier - block identifier
49
46
  * @returns the block object { block_number, previous_block_number, state_root, status, timestamp, transaction_receipts, transactions }
50
47
  */
51
- abstract getBlock(blockHash?: BigNumberish, blockNumber?: BlockNumber): Promise<GetBlockResponse>;
48
+ abstract getBlock(blockIdentifier?: BlockIdentifier): Promise<GetBlockResponse>;
52
49
  /**
53
50
  * Gets the code of the deployed contract.
54
51
  *
55
52
  * [Reference](https://github.com/starkware-libs/cairo-lang/blob/fc97bdd8322a7df043c87c371634b26c15ed6cee/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L55-L68)
56
53
  *
57
- * @param contractAddress
58
- * @param blockHash
59
- * @param blockNumber
54
+ * @param contractAddress - contract address
55
+ * @param blockIdentifier - block identifier
60
56
  * @returns Bytecode and ABI of compiled contract
61
57
  */
62
58
  abstract getCode(
63
59
  contractAddress: string,
64
- blockHash?: BigNumberish,
65
- blockNumber?: BlockNumber
60
+ blockIdentifier?: BlockIdentifier
66
61
  ): Promise<GetCodeResponse>;
67
62
  /**
68
63
  * Gets the contract's storage variable at a specific key.
@@ -71,15 +66,13 @@ export declare abstract class ProviderInterface {
71
66
  *
72
67
  * @param contractAddress
73
68
  * @param key - from getStorageVarAddress('<STORAGE_VARIABLE_NAME>') (WIP)
74
- * @param blockHash
75
- * @param blockNumber
69
+ * @param blockIdentifier - block identifier
76
70
  * @returns the value of the storage variable
77
71
  */
78
72
  abstract getStorageAt(
79
73
  contractAddress: string,
80
74
  key: number,
81
- blockHash?: BigNumberish,
82
- blockNumber?: BlockNumber
75
+ blockIdentifier?: BlockIdentifier
83
76
  ): Promise<object>;
84
77
  /**
85
78
  * Gets the status of a transaction.
@@ -15,6 +15,23 @@ export declare function formatHash(): void;
15
15
  * @param txId
16
16
  */
17
17
  export declare function txIdentifier(): void;
18
+ export declare type BlockIdentifier = BlockNumber | BigNumberish;
19
+ declare type BlockIdentifierObject =
20
+ | {
21
+ type: 'BLOCK_NUMBER';
22
+ data: BlockNumber;
23
+ }
24
+ | {
25
+ type: 'BLOCK_HASH';
26
+ data: BigNumberish;
27
+ };
28
+ /**
29
+ * Identifies the block to be queried.
30
+ *
31
+ * @param blockIdentifier - block identifier
32
+ * @returns block identifier object
33
+ */
34
+ export declare function getBlockIdentifier(blockIdentifier: BlockIdentifier): BlockIdentifierObject;
18
35
  /**
19
36
  * Gets the block identifier for API request
20
37
  *
@@ -24,7 +41,5 @@ export declare function txIdentifier(): void;
24
41
  * @param blockHash
25
42
  * @returns block identifier for API request
26
43
  */
27
- export declare function getFormattedBlockIdentifier(
28
- blockHash?: BigNumberish,
29
- blockNumber?: BlockNumber
30
- ): string;
44
+ export declare function getFormattedBlockIdentifier(blockIdentifier?: BlockIdentifier): string;
45
+ export {};
package/provider/utils.js CHANGED
@@ -1,6 +1,11 @@
1
1
  'use strict';
2
2
  Object.defineProperty(exports, '__esModule', { value: true });
3
- exports.getFormattedBlockIdentifier = exports.txIdentifier = exports.formatHash = void 0;
3
+ exports.getFormattedBlockIdentifier =
4
+ exports.getBlockIdentifier =
5
+ exports.txIdentifier =
6
+ exports.formatHash =
7
+ void 0;
8
+ var number_1 = require('../utils/number');
4
9
  /**
5
10
  * TODO
6
11
  * [Reference](https://github.com/starkware-libs/cairo-lang/blob/fc97bdd8322a7df043c87c371634b26c15ed6cee/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L148-L153)
@@ -18,6 +23,34 @@ exports.formatHash = formatHash;
18
23
  */
19
24
  function txIdentifier() {}
20
25
  exports.txIdentifier = txIdentifier;
26
+ /**
27
+ * Identifies the block to be queried.
28
+ *
29
+ * @param blockIdentifier - block identifier
30
+ * @returns block identifier object
31
+ */
32
+ function getBlockIdentifier(blockIdentifier) {
33
+ if (typeof blockIdentifier === 'number') {
34
+ return { type: 'BLOCK_NUMBER', data: blockIdentifier };
35
+ }
36
+ if (typeof blockIdentifier === 'string' && blockIdentifier.startsWith('0x')) {
37
+ return { type: 'BLOCK_HASH', data: blockIdentifier };
38
+ }
39
+ if (typeof blockIdentifier === 'string' && !Number.isNaN(parseInt(blockIdentifier, 10))) {
40
+ return { type: 'BLOCK_NUMBER', data: parseInt(blockIdentifier, 10) };
41
+ }
42
+ if (blockIdentifier === null) {
43
+ return { type: 'BLOCK_NUMBER', data: null };
44
+ }
45
+ if (blockIdentifier === 'pending') {
46
+ return { type: 'BLOCK_NUMBER', data: 'pending' };
47
+ }
48
+ if (typeof blockIdentifier === 'string') {
49
+ throw new Error('Invalid block identifier: ' + blockIdentifier);
50
+ }
51
+ return { type: 'BLOCK_HASH', data: blockIdentifier };
52
+ }
53
+ exports.getBlockIdentifier = getBlockIdentifier;
21
54
  /**
22
55
  * Gets the block identifier for API request
23
56
  *
@@ -27,13 +60,17 @@ exports.txIdentifier = txIdentifier;
27
60
  * @param blockHash
28
61
  * @returns block identifier for API request
29
62
  */
30
- function getFormattedBlockIdentifier(blockHash, blockNumber) {
31
- if (blockNumber === void 0) {
32
- blockNumber = null;
63
+ function getFormattedBlockIdentifier(blockIdentifier) {
64
+ if (blockIdentifier === void 0) {
65
+ blockIdentifier = null;
66
+ }
67
+ var blockIdentifierObject = getBlockIdentifier(blockIdentifier);
68
+ if (blockIdentifierObject.type === 'BLOCK_NUMBER' && blockIdentifierObject.data === null) {
69
+ return '';
33
70
  }
34
- if (blockHash) {
35
- return '?blockHash=' + blockHash;
71
+ if (blockIdentifierObject.type === 'BLOCK_NUMBER') {
72
+ return '?blockNumber=' + blockIdentifierObject.data;
36
73
  }
37
- return '?blockNumber=' + blockNumber;
74
+ return '?blockHash=' + (0, number_1.toHex)((0, number_1.toBN)(blockIdentifierObject.data));
38
75
  }
39
76
  exports.getFormattedBlockIdentifier = getFormattedBlockIdentifier;
package/src/constants.ts CHANGED
@@ -6,6 +6,7 @@ export const ZERO = toBN(0);
6
6
  export const ONE = toBN(1);
7
7
  export const TWO = toBN(2);
8
8
  export const MASK_250 = TWO.pow(toBN(250)).sub(ONE); // 2 ** 250 - 1
9
+ export const MASK_251 = TWO.pow(toBN(251));
9
10
 
10
11
  /**
11
12
  * The following is taken from https://github.com/starkware-libs/starkex-resources/blob/master/crypto/starkware/crypto/signature/pedersen_params.json but converted to hex, because JS is very bad handling big integers by default
@@ -13,6 +14,7 @@ export const MASK_250 = TWO.pow(toBN(250)).sub(ONE); // 2 ** 250 - 1
13
14
  */
14
15
  export const FIELD_PRIME = '800000000000011000000000000000000000000000000000000000000000001';
15
16
  export const FIELD_GEN = '3';
17
+ export const FIELD_SIZE = 251;
16
18
  export const EC_ORDER = '800000000000010FFFFFFFFFFFFFFFFB781126DCAE7B2321E66A241ADC64D2F';
17
19
  export const ALPHA = '1';
18
20
  export const BETA = '6F21413EFBE40DE150E596D72F7A8C5609AD26C15C915C1F4CDFCB99CEE9E89';
package/src/index.ts CHANGED
@@ -19,3 +19,4 @@ export * as ec from './utils/ellipticCurve';
19
19
  export * as uint256 from './utils/uint256';
20
20
  export * as shortString from './utils/shortString';
21
21
  export * as typedData from './utils/typedData';
22
+ export * from './utils/address';
@@ -3,7 +3,6 @@ import urljoin from 'url-join';
3
3
 
4
4
  import {
5
5
  AddTransactionResponse,
6
- BlockNumber,
7
6
  CallContractResponse,
8
7
  CallContractTransaction,
9
8
  CompiledContract,
@@ -19,7 +18,7 @@ import { parse, stringify } from '../utils/json';
19
18
  import { BigNumberish, toBN, toHex } from '../utils/number';
20
19
  import { compressProgram, formatSignature, randomAddress } from '../utils/stark';
21
20
  import { ProviderInterface } from './interface';
22
- import { getFormattedBlockIdentifier } from './utils';
21
+ import { BlockIdentifier, getFormattedBlockIdentifier } from './utils';
23
22
 
24
23
  type NetworkName = 'mainnet-alpha' | 'goerli-alpha';
25
24
 
@@ -93,10 +92,9 @@ export class Provider implements ProviderInterface {
93
92
  */
94
93
  public async callContract(
95
94
  invokeTransaction: CallContractTransaction,
96
- blockHash?: BigNumberish,
97
- blockNumber: BlockNumber = null
95
+ blockIdentifier: BlockIdentifier = null
98
96
  ): Promise<CallContractResponse> {
99
- const formattedBlockIdentifier = getFormattedBlockIdentifier(blockHash, blockNumber);
97
+ const formattedBlockIdentifier = getFormattedBlockIdentifier(blockIdentifier);
100
98
 
101
99
  const { data } = await axios.post<CallContractResponse>(
102
100
  urljoin(this.feederGatewayUrl, 'call_contract', formattedBlockIdentifier),
@@ -118,11 +116,8 @@ export class Provider implements ProviderInterface {
118
116
  * @param blockNumber
119
117
  * @returns the block object { block_number, previous_block_number, state_root, status, timestamp, transaction_receipts, transactions }
120
118
  */
121
- public async getBlock(
122
- blockHash?: BigNumberish,
123
- blockNumber: BlockNumber = null
124
- ): Promise<GetBlockResponse> {
125
- const formattedBlockIdentifier = getFormattedBlockIdentifier(blockHash, blockNumber);
119
+ public async getBlock(blockIdentifier: BlockIdentifier = null): Promise<GetBlockResponse> {
120
+ const formattedBlockIdentifier = getFormattedBlockIdentifier(blockIdentifier);
126
121
 
127
122
  const { data } = await axios.get<GetBlockResponse>(
128
123
  urljoin(this.feederGatewayUrl, 'get_block', formattedBlockIdentifier)
@@ -142,10 +137,9 @@ export class Provider implements ProviderInterface {
142
137
  */
143
138
  public async getCode(
144
139
  contractAddress: string,
145
- blockHash?: BigNumberish,
146
- blockNumber: BlockNumber = null
140
+ blockIdentifier: BlockIdentifier = null
147
141
  ): Promise<GetCodeResponse> {
148
- const formattedBlockIdentifier = getFormattedBlockIdentifier(blockHash, blockNumber);
142
+ const formattedBlockIdentifier = getFormattedBlockIdentifier(blockIdentifier);
149
143
 
150
144
  const { data } = await axios.get<GetCodeResponse>(
151
145
  urljoin(
@@ -172,10 +166,9 @@ export class Provider implements ProviderInterface {
172
166
  public async getStorageAt(
173
167
  contractAddress: string,
174
168
  key: number,
175
- blockHash?: BigNumberish,
176
- blockNumber: BlockNumber = null
169
+ blockIdentifier: BlockIdentifier = null
177
170
  ): Promise<object> {
178
- const formattedBlockIdentifier = getFormattedBlockIdentifier(blockHash, blockNumber);
171
+ const formattedBlockIdentifier = getFormattedBlockIdentifier(blockIdentifier);
179
172
 
180
173
  const { data } = await axios.get<object>(
181
174
  urljoin(
@@ -1,6 +1,5 @@
1
1
  import type {
2
2
  AddTransactionResponse,
3
- BlockNumber,
4
3
  CallContractResponse,
5
4
  CallContractTransaction,
6
5
  CompiledContract,
@@ -13,6 +12,7 @@ import type {
13
12
  Transaction,
14
13
  } from '../types';
15
14
  import type { BigNumberish } from '../utils/number';
15
+ import { BlockIdentifier } from './utils';
16
16
 
17
17
  export abstract class ProviderInterface {
18
18
  public abstract baseUrl: string;
@@ -35,14 +35,12 @@ export abstract class ProviderInterface {
35
35
  * [Reference](https://github.com/starkware-libs/cairo-lang/blob/fc97bdd8322a7df043c87c371634b26c15ed6cee/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L25-L39)
36
36
  *
37
37
  * @param invokeTransaction - transaction to be invoked
38
- * @param blockHash
39
- * @param blockNumber
38
+ * @param blockIdentifier - block identifier
40
39
  * @returns the result of the function on the smart contract.
41
40
  */
42
41
  public abstract callContract(
43
42
  invokeTransaction: CallContractTransaction,
44
- blockHash?: BigNumberish,
45
- blockNumber?: BlockNumber
43
+ blockIdentifier?: BlockIdentifier
46
44
  ): Promise<CallContractResponse>;
47
45
 
48
46
  /**
@@ -50,29 +48,23 @@ export abstract class ProviderInterface {
50
48
  *
51
49
  * [Reference](https://github.com/starkware-libs/cairo-lang/blob/fc97bdd8322a7df043c87c371634b26c15ed6cee/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L41-L53)
52
50
  *
53
- * @param blockHash
54
- * @param blockNumber
51
+ * @param blockIdentifier - block identifier
55
52
  * @returns the block object { block_number, previous_block_number, state_root, status, timestamp, transaction_receipts, transactions }
56
53
  */
57
- public abstract getBlock(
58
- blockHash?: BigNumberish,
59
- blockNumber?: BlockNumber
60
- ): Promise<GetBlockResponse>;
54
+ public abstract getBlock(blockIdentifier?: BlockIdentifier): Promise<GetBlockResponse>;
61
55
 
62
56
  /**
63
57
  * Gets the code of the deployed contract.
64
58
  *
65
59
  * [Reference](https://github.com/starkware-libs/cairo-lang/blob/fc97bdd8322a7df043c87c371634b26c15ed6cee/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L55-L68)
66
60
  *
67
- * @param contractAddress
68
- * @param blockHash
69
- * @param blockNumber
61
+ * @param contractAddress - contract address
62
+ * @param blockIdentifier - block identifier
70
63
  * @returns Bytecode and ABI of compiled contract
71
64
  */
72
65
  public abstract getCode(
73
66
  contractAddress: string,
74
- blockHash?: BigNumberish,
75
- blockNumber?: BlockNumber
67
+ blockIdentifier?: BlockIdentifier
76
68
  ): Promise<GetCodeResponse>;
77
69
 
78
70
  // TODO: add proper type
@@ -83,15 +75,13 @@ export abstract class ProviderInterface {
83
75
  *
84
76
  * @param contractAddress
85
77
  * @param key - from getStorageVarAddress('<STORAGE_VARIABLE_NAME>') (WIP)
86
- * @param blockHash
87
- * @param blockNumber
78
+ * @param blockIdentifier - block identifier
88
79
  * @returns the value of the storage variable
89
80
  */
90
81
  public abstract getStorageAt(
91
82
  contractAddress: string,
92
83
  key: number,
93
- blockHash?: BigNumberish,
94
- blockNumber?: BlockNumber
84
+ blockIdentifier?: BlockIdentifier
95
85
  ): Promise<object>;
96
86
 
97
87
  /**
@@ -1,5 +1,5 @@
1
1
  import type { BlockNumber } from '../types';
2
- import { BigNumberish } from '../utils/number';
2
+ import { BigNumberish, toBN, toHex } from '../utils/number';
3
3
 
4
4
  /**
5
5
  * TODO
@@ -18,6 +18,42 @@ export function formatHash() {}
18
18
  */
19
19
  export function txIdentifier() {}
20
20
 
21
+ // hex string and BN are detected as block hashes
22
+ // decimal string and number are detected as block numbers
23
+ // null appends nothing to the request url
24
+ export type BlockIdentifier = BlockNumber | BigNumberish;
25
+ type BlockIdentifierObject =
26
+ | { type: 'BLOCK_NUMBER'; data: BlockNumber }
27
+ | { type: 'BLOCK_HASH'; data: BigNumberish };
28
+
29
+ /**
30
+ * Identifies the block to be queried.
31
+ *
32
+ * @param blockIdentifier - block identifier
33
+ * @returns block identifier object
34
+ */
35
+ export function getBlockIdentifier(blockIdentifier: BlockIdentifier): BlockIdentifierObject {
36
+ if (typeof blockIdentifier === 'number') {
37
+ return { type: 'BLOCK_NUMBER', data: blockIdentifier };
38
+ }
39
+ if (typeof blockIdentifier === 'string' && blockIdentifier.startsWith('0x')) {
40
+ return { type: 'BLOCK_HASH', data: blockIdentifier };
41
+ }
42
+ if (typeof blockIdentifier === 'string' && !Number.isNaN(parseInt(blockIdentifier, 10))) {
43
+ return { type: 'BLOCK_NUMBER', data: parseInt(blockIdentifier, 10) };
44
+ }
45
+ if (blockIdentifier === null) {
46
+ return { type: 'BLOCK_NUMBER', data: null };
47
+ }
48
+ if (blockIdentifier === 'pending') {
49
+ return { type: 'BLOCK_NUMBER', data: 'pending' };
50
+ }
51
+ if (typeof blockIdentifier === 'string') {
52
+ throw new Error(`Invalid block identifier: ${blockIdentifier}`);
53
+ }
54
+ return { type: 'BLOCK_HASH', data: blockIdentifier };
55
+ }
56
+
21
57
  /**
22
58
  * Gets the block identifier for API request
23
59
  *
@@ -27,12 +63,13 @@ export function txIdentifier() {}
27
63
  * @param blockHash
28
64
  * @returns block identifier for API request
29
65
  */
30
- export function getFormattedBlockIdentifier(
31
- blockHash?: BigNumberish,
32
- blockNumber: BlockNumber = null
33
- ): string {
34
- if (blockHash) {
35
- return `?blockHash=${blockHash}`;
36
- }
37
- return `?blockNumber=${blockNumber}`;
66
+ export function getFormattedBlockIdentifier(blockIdentifier: BlockIdentifier = null): string {
67
+ const blockIdentifierObject = getBlockIdentifier(blockIdentifier);
68
+ if (blockIdentifierObject.type === 'BLOCK_NUMBER' && blockIdentifierObject.data === null) {
69
+ return '';
70
+ }
71
+ if (blockIdentifierObject.type === 'BLOCK_NUMBER') {
72
+ return `?blockNumber=${blockIdentifierObject.data}`;
73
+ }
74
+ return `?blockHash=${toHex(toBN(blockIdentifierObject.data))}`;
38
75
  }
@@ -0,0 +1,23 @@
1
+ import { MASK_251, ZERO } from '../constants';
2
+ import { addHexPrefix, removeHexPrefix } from './encode';
3
+ import { assertInRange } from './number';
4
+
5
+ export function addAddressPadding(address: string): string {
6
+ return addHexPrefix(removeHexPrefix(address).padStart(64, '0'));
7
+ }
8
+
9
+ export function validateAndParseAddress(address: string): string {
10
+ if (typeof address !== 'string') {
11
+ throw new Error('Invalid Address Type');
12
+ }
13
+
14
+ assertInRange(address, ZERO, MASK_251, 'Starknet Address');
15
+
16
+ const result = addAddressPadding(address);
17
+
18
+ if (!result.match(/^(0x)?[0-9a-fA-F]{64}$/)) {
19
+ throw new Error('Invalid Address Format');
20
+ }
21
+
22
+ return result;
23
+ }
@@ -0,0 +1,2 @@
1
+ export declare function addAddressPadding(address: string): string;
2
+ export declare function validateAndParseAddress(address: string): string;
@@ -0,0 +1,22 @@
1
+ 'use strict';
2
+ Object.defineProperty(exports, '__esModule', { value: true });
3
+ exports.validateAndParseAddress = exports.addAddressPadding = void 0;
4
+ var constants_1 = require('../constants');
5
+ var encode_1 = require('./encode');
6
+ var number_1 = require('./number');
7
+ function addAddressPadding(address) {
8
+ return (0, encode_1.addHexPrefix)((0, encode_1.removeHexPrefix)(address).padStart(64, '0'));
9
+ }
10
+ exports.addAddressPadding = addAddressPadding;
11
+ function validateAndParseAddress(address) {
12
+ if (typeof address !== 'string') {
13
+ throw new Error('Invalid Address Type');
14
+ }
15
+ (0, number_1.assertInRange)(address, constants_1.ZERO, constants_1.MASK_251, 'Starknet Address');
16
+ var result = addAddressPadding(address);
17
+ if (!result.match(/^(0x)?[0-9a-fA-F]{64}$/)) {
18
+ throw new Error('Invalid Address Format');
19
+ }
20
+ return result;
21
+ }
22
+ exports.validateAndParseAddress = validateAndParseAddress;