starknet 3.10.3 → 3.11.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,17 @@
1
+ # [3.11.0](https://github.com/seanjameshan/starknet.js/compare/v3.10.3...v3.11.0) (2022-05-11)
2
+
3
+ ### Bug Fixes
4
+
5
+ - review ([9ee4987](https://github.com/seanjameshan/starknet.js/commit/9ee498788185a35a75f2b429b3f1ec55dc4ee2a3))
6
+ - tests ([f535edb](https://github.com/seanjameshan/starknet.js/commit/f535edbef8da6050e54575792926488774e3ed0f))
7
+ - transaction receipt ([806eb7d](https://github.com/seanjameshan/starknet.js/commit/806eb7d63a01e158231f7b63cc4fc1fb0c30717e))
8
+ - use npm again ([3b9e176](https://github.com/seanjameshan/starknet.js/commit/3b9e176812f6401e167a207db2d9ff3686260e13))
9
+
10
+ ### Features
11
+
12
+ - add checksum addresses ([2d32ed8](https://github.com/seanjameshan/starknet.js/commit/2d32ed828f908090642a42d9f6620f050a75b43a))
13
+ - use BigNumber for estimate fee amount ([751c2ed](https://github.com/seanjameshan/starknet.js/commit/751c2edf89d019f365f5fba8123a9df0320ff543))
14
+
1
15
  ## [3.10.3](https://github.com/seanjameshan/starknet.js/compare/v3.10.2...v3.10.3) (2022-05-04)
2
16
 
3
17
  ### Bug Fixes
@@ -1,3 +1,5 @@
1
+ import { isBN } from 'bn.js';
2
+
1
3
  import typedDataExample from '../__mocks__/typedDataExample.json';
2
4
  import { Account, Contract, defaultProvider, ec, number, stark } from '../src';
3
5
  import { toBN } from '../src/utils/number';
@@ -50,7 +52,7 @@ describe('deploy and test Wallet', () => {
50
52
  entrypoint: 'transfer',
51
53
  calldata: [erc20.address, '10'],
52
54
  });
53
- expect(typeof amount).toBe('number');
55
+ expect(isBN(amount)).toBe(true);
54
56
  expect(typeof unit).toBe('string');
55
57
  });
56
58
 
@@ -76,9 +76,9 @@ describe('defaultProvider', () => {
76
76
 
77
77
  test('getTransactionReceipt', async () => {
78
78
  return expect(
79
- defaultProvider.getTransactionReceipt({
80
- txHash: '0x37013e1cb9c133e6fe51b4b371b76b317a480f56d80576730754c1662582348',
81
- })
79
+ defaultProvider.getTransactionReceipt(
80
+ '0x37013e1cb9c133e6fe51b4b371b76b317a480f56d80576730754c1662582348'
81
+ )
82
82
  ).resolves.not.toThrow();
83
83
  });
84
84
 
@@ -1,4 +1,9 @@
1
- import { addAddressPadding, validateAndParseAddress } from '../../src/utils/address';
1
+ import {
2
+ addAddressPadding,
3
+ getChecksumAddress,
4
+ validateAndParseAddress,
5
+ validateChecksumAddress,
6
+ } from '../../src/utils/address';
2
7
  // import { addHexPrefix, removeHexPrefix } from '../../src/utils/encode';
3
8
 
4
9
  describe('validateAndParseAddress', () => {
@@ -14,3 +19,27 @@ describe('validateAndParseAddress', () => {
14
19
  return expect(validateAndParseAddress(addr)).toEqual(`${addAddressPadding(addr)}`);
15
20
  });
16
21
  });
22
+
23
+ describe('address checksums', () => {
24
+ test('should be able to calculate checksum address', () => {
25
+ const checksumAddress = getChecksumAddress(
26
+ '0x2fd23d9182193775423497fc0c472e156c57c69e4089a1967fb288a2d84e914'
27
+ );
28
+ expect(checksumAddress).toEqual(
29
+ '0x02FD23D9182193775423497Fc0c472E156C57C69E4089a1967fb288a2D84e914'
30
+ );
31
+ });
32
+ test('should be able to verify checksum address', () => {
33
+ const isValid = validateChecksumAddress(
34
+ '0x02FD23D9182193775423497Fc0c472E156C57C69E4089a1967fb288a2D84e914'
35
+ );
36
+ expect(isValid).toEqual(true);
37
+ });
38
+ test('calculated checksum address should validate', () => {
39
+ const checksumAddress = getChecksumAddress(
40
+ '0x26cb0b500d175111341fabb53bf7fa4f5a0b8c5cbb31896cec1e8383a5edda8'
41
+ );
42
+ const isValid = validateChecksumAddress(checksumAddress);
43
+ expect(isValid).toEqual(true);
44
+ });
45
+ });
@@ -1,5 +1,5 @@
1
1
  import { StarknetChainId } from '../constants';
2
- import { Abi, AddTransactionResponse, Call, CallContractResponse, DeployContractPayload, Endpoints, GetBlockResponse, GetCodeResponse, GetContractAddressesResponse, GetTransactionResponse, GetTransactionStatusResponse, GetTransactionTraceResponse, Invocation, TransactionReceipt } from '../types';
2
+ import { Abi, AddTransactionResponse, Call, CallContractResponse, DeployContractPayload, Endpoints, GetBlockResponse, GetCodeResponse, GetContractAddressesResponse, GetTransactionResponse, GetTransactionStatusResponse, GetTransactionTraceResponse, Invocation, TransactionReceiptResponse } from '../types';
3
3
  import { BigNumberish } from '../utils/number';
4
4
  import { ProviderInterface } from './interface';
5
5
  import { BlockIdentifier } from './utils';
@@ -90,13 +90,9 @@ export declare class Provider implements ProviderInterface {
90
90
  * [Reference] (https://github.com/starkware-libs/cairo-lang/blob/master/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L104-L111)
91
91
  *
92
92
  * @param txHash
93
- * @param txId
94
93
  * @returns the transaction receipt object
95
94
  */
96
- getTransactionReceipt({ txHash, txId, }: {
97
- txHash?: BigNumberish;
98
- txId?: BigNumberish;
99
- }): Promise<TransactionReceipt>;
95
+ getTransactionReceipt(txHash: BigNumberish): Promise<TransactionReceiptResponse>;
100
96
  /**
101
97
  * Gets the transaction information from a tx id.
102
98
  *
@@ -196,6 +196,16 @@ var Provider = /** @class */ (function () {
196
196
  _d.trys.push([1, 3, , 4]);
197
197
  return [4 /*yield*/, axios_1.default.request({
198
198
  method: method,
199
+ transformResponse: endpoint === 'estimate_fee'
200
+ ? function (res) {
201
+ return (0, json_1.parse)(res, function (_, v) {
202
+ if (v && typeof v === 'bigint') {
203
+ return (0, number_1.toBN)(v.toString());
204
+ }
205
+ return v;
206
+ });
207
+ }
208
+ : axios_1.default.defaults.transformResponse,
199
209
  url: (0, url_join_1.default)(baseUrl, endpoint, queryString),
200
210
  data: (0, json_1.stringify)(request),
201
211
  headers: headers,
@@ -330,20 +340,14 @@ var Provider = /** @class */ (function () {
330
340
  * [Reference] (https://github.com/starkware-libs/cairo-lang/blob/master/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L104-L111)
331
341
  *
332
342
  * @param txHash
333
- * @param txId
334
343
  * @returns the transaction receipt object
335
344
  */
336
- Provider.prototype.getTransactionReceipt = function (_a) {
337
- var txHash = _a.txHash, txId = _a.txId;
345
+ Provider.prototype.getTransactionReceipt = function (txHash) {
338
346
  return __awaiter(this, void 0, void 0, function () {
339
- var data;
340
- return __generator(this, function (_b) {
341
- switch (_b.label) {
342
- case 0: return [4 /*yield*/, axios_1.default.get((0, url_join_1.default)(this.feederGatewayUrl, 'get_transaction_receipt', "?" + (0, utils_1.txIdentifier)(txHash, txId)))];
343
- case 1:
344
- data = (_b.sent()).data;
345
- return [2 /*return*/, data];
346
- }
347
+ var txHashHex;
348
+ return __generator(this, function (_a) {
349
+ txHashHex = (0, number_1.toHex)((0, number_1.toBN)(txHash));
350
+ return [2 /*return*/, this.fetchEndpoint('get_transaction_receipt', { transactionHash: txHashHex })];
347
351
  });
348
352
  });
349
353
  };
@@ -1,5 +1,5 @@
1
1
  import { StarknetChainId } from '../constants';
2
- import type { AddTransactionResponse, Call, CallContractResponse, DeployContractPayload, GetBlockResponse, GetCodeResponse, GetContractAddressesResponse, GetTransactionResponse, GetTransactionStatusResponse, Invocation, TransactionReceipt } from '../types';
2
+ import type { AddTransactionResponse, Call, CallContractResponse, DeployContractPayload, GetBlockResponse, GetCodeResponse, GetContractAddressesResponse, GetTransactionResponse, GetTransactionStatusResponse, Invocation, TransactionReceiptResponse } from '../types';
3
3
  import type { BigNumberish } from '../utils/number';
4
4
  import { BlockIdentifier } from './utils';
5
5
  export declare abstract class ProviderInterface {
@@ -74,10 +74,7 @@ export declare abstract class ProviderInterface {
74
74
  * @returns the transacton object { transaction_id, status, transaction, block_number?, block_number?, transaction_index?, transaction_failure_reason? }
75
75
  */
76
76
  abstract getTransaction(txHash: BigNumberish): Promise<GetTransactionResponse>;
77
- abstract getTransactionReceipt({ txHash, txId, }: {
78
- txHash?: BigNumberish;
79
- txId?: BigNumberish;
80
- }): Promise<TransactionReceipt>;
77
+ abstract getTransactionReceipt(txHash: BigNumberish): Promise<TransactionReceiptResponse>;
81
78
  /**
82
79
  * Deploys a given compiled contract (json) to starknet
83
80
  *
@@ -1,3 +1,4 @@
1
+ import BN from 'bn.js';
1
2
  import { BlockIdentifier } from '../provider/utils';
2
3
  import { BigNumberish } from '../utils/number';
3
4
  import { Abi, BlockNumber, CompressedCompiledContract, EntryPointType, RawCalldata, Signature, Status, TransactionStatus } from './lib';
@@ -33,6 +34,13 @@ export declare type Endpoints = {
33
34
  REQUEST: never;
34
35
  RESPONSE: GetTransactionTraceResponse;
35
36
  };
37
+ get_transaction_receipt: {
38
+ QUERY: {
39
+ transactionHash: string;
40
+ };
41
+ REQUEST: never;
42
+ RESPONSE: TransactionReceiptResponse;
43
+ };
36
44
  get_storage_at: {
37
45
  QUERY: {
38
46
  contractAddress: string;
@@ -189,7 +197,7 @@ export declare type AddTransactionResponse = {
189
197
  transaction_hash: string;
190
198
  address?: string;
191
199
  };
192
- export declare type TransactionReceipt = {
200
+ export declare type TransactionReceiptResponse = {
193
201
  status: Status;
194
202
  transaction_hash: string;
195
203
  transaction_index: number;
@@ -199,7 +207,7 @@ export declare type TransactionReceipt = {
199
207
  events: string[];
200
208
  };
201
209
  export declare type EstimateFeeResponse = {
202
- amount: number;
210
+ amount: BN;
203
211
  unit: string;
204
212
  };
205
213
  export declare type RawArgs = {
@@ -1,2 +1,5 @@
1
- export declare function addAddressPadding(address: string): string;
2
- export declare function validateAndParseAddress(address: string): string;
1
+ import { BigNumberish } from './number';
2
+ export declare function addAddressPadding(address: BigNumberish): string;
3
+ export declare function validateAndParseAddress(address: BigNumberish): string;
4
+ export declare function getChecksumAddress(address: BigNumberish): string;
5
+ export declare function validateChecksumAddress(address: string): boolean;
@@ -1,17 +1,17 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.validateAndParseAddress = exports.addAddressPadding = void 0;
3
+ exports.validateChecksumAddress = exports.getChecksumAddress = exports.validateAndParseAddress = exports.addAddressPadding = void 0;
4
+ /* eslint-disable no-bitwise */
5
+ var bytes_1 = require("@ethersproject/bytes");
4
6
  var constants_1 = require("../constants");
5
7
  var encode_1 = require("./encode");
8
+ var hash_1 = require("./hash");
6
9
  var number_1 = require("./number");
7
10
  function addAddressPadding(address) {
8
- return (0, encode_1.addHexPrefix)((0, encode_1.removeHexPrefix)(address).padStart(64, '0'));
11
+ return (0, encode_1.addHexPrefix)((0, encode_1.removeHexPrefix)((0, number_1.toHex)((0, number_1.toBN)(address))).padStart(64, '0'));
9
12
  }
10
13
  exports.addAddressPadding = addAddressPadding;
11
14
  function validateAndParseAddress(address) {
12
- if (typeof address !== 'string') {
13
- throw new Error('Invalid Address Type');
14
- }
15
15
  (0, number_1.assertInRange)(address, constants_1.ZERO, constants_1.MASK_251, 'Starknet Address');
16
16
  var result = addAddressPadding(address);
17
17
  if (!result.match(/^(0x)?[0-9a-fA-F]{64}$/)) {
@@ -20,3 +20,22 @@ function validateAndParseAddress(address) {
20
20
  return result;
21
21
  }
22
22
  exports.validateAndParseAddress = validateAndParseAddress;
23
+ // from https://github.com/ethers-io/ethers.js/blob/fc1e006575d59792fa97b4efb9ea2f8cca1944cf/packages/address/src.ts/index.ts#L12
24
+ function getChecksumAddress(address) {
25
+ var chars = (0, encode_1.removeHexPrefix)(validateAndParseAddress(address)).toLowerCase().split('');
26
+ var hashed = (0, bytes_1.arrayify)((0, hash_1.pedersen)([0, address]), { hexPad: 'left' }); // as the hash will be 251 bits (63 chars) we need to pad it to 64 chars without changing the number value ("left")
27
+ for (var i = 0; i < chars.length; i += 2) {
28
+ if (hashed[i >> 1] >> 4 >= 8) {
29
+ chars[i] = chars[i].toUpperCase();
30
+ }
31
+ if ((hashed[i >> 1] & 0x0f) >= 8) {
32
+ chars[i + 1] = chars[i + 1].toUpperCase();
33
+ }
34
+ }
35
+ return (0, encode_1.addHexPrefix)(chars.join(''));
36
+ }
37
+ exports.getChecksumAddress = getChecksumAddress;
38
+ function validateChecksumAddress(address) {
39
+ return getChecksumAddress(address) === address;
40
+ }
41
+ exports.validateChecksumAddress = validateChecksumAddress;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "starknet",
3
- "version": "3.10.3",
3
+ "version": "3.11.0",
4
4
  "description": "JavaScript library for StarkNet",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -61,6 +61,7 @@
61
61
  "typescript": "^4.4.4"
62
62
  },
63
63
  "dependencies": {
64
+ "@ethersproject/bytes": "^5.6.1",
64
65
  "axios": "^0.23.0",
65
66
  "bn.js": "^5.2.0",
66
67
  "elliptic": "^6.5.4",
@@ -13,7 +13,7 @@ import {
13
13
  GetTransactionStatusResponse,
14
14
  GetTransactionTraceResponse,
15
15
  Invocation,
16
- TransactionReceipt,
16
+ TransactionReceiptResponse,
17
17
  } from '../types';
18
18
  import { BigNumberish } from '../utils/number';
19
19
  import { ProviderInterface } from './interface';
@@ -127,16 +127,9 @@ export declare class Provider implements ProviderInterface {
127
127
  * [Reference] (https://github.com/starkware-libs/cairo-lang/blob/master/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L104-L111)
128
128
  *
129
129
  * @param txHash
130
- * @param txId
131
130
  * @returns the transaction receipt object
132
131
  */
133
- getTransactionReceipt({
134
- txHash,
135
- txId,
136
- }: {
137
- txHash?: BigNumberish;
138
- txId?: BigNumberish;
139
- }): Promise<TransactionReceipt>;
132
+ getTransactionReceipt(txHash: BigNumberish): Promise<TransactionReceiptResponse>;
140
133
  /**
141
134
  * Gets the transaction information from a tx id.
142
135
  *
@@ -315,6 +315,17 @@ var Provider = /** @class */ (function () {
315
315
  4 /*yield*/,
316
316
  axios_1.default.request({
317
317
  method: method,
318
+ transformResponse:
319
+ endpoint === 'estimate_fee'
320
+ ? function (res) {
321
+ return (0, json_1.parse)(res, function (_, v) {
322
+ if (v && typeof v === 'bigint') {
323
+ return (0, number_1.toBN)(v.toString());
324
+ }
325
+ return v;
326
+ });
327
+ }
328
+ : axios_1.default.defaults.transformResponse,
318
329
  url: (0, url_join_1.default)(baseUrl, endpoint, queryString),
319
330
  data: (0, json_1.stringify)(request),
320
331
  headers: headers,
@@ -492,31 +503,17 @@ var Provider = /** @class */ (function () {
492
503
  * [Reference] (https://github.com/starkware-libs/cairo-lang/blob/master/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L104-L111)
493
504
  *
494
505
  * @param txHash
495
- * @param txId
496
506
  * @returns the transaction receipt object
497
507
  */
498
- Provider.prototype.getTransactionReceipt = function (_a) {
499
- var txHash = _a.txHash,
500
- txId = _a.txId;
508
+ Provider.prototype.getTransactionReceipt = function (txHash) {
501
509
  return __awaiter(this, void 0, void 0, function () {
502
- var data;
503
- return __generator(this, function (_b) {
504
- switch (_b.label) {
505
- case 0:
506
- return [
507
- 4 /*yield*/,
508
- axios_1.default.get(
509
- (0, url_join_1.default)(
510
- this.feederGatewayUrl,
511
- 'get_transaction_receipt',
512
- '?' + (0, utils_1.txIdentifier)(txHash, txId)
513
- )
514
- ),
515
- ];
516
- case 1:
517
- data = _b.sent().data;
518
- return [2 /*return*/, data];
519
- }
510
+ var txHashHex;
511
+ return __generator(this, function (_a) {
512
+ txHashHex = (0, number_1.toHex)((0, number_1.toBN)(txHash));
513
+ return [
514
+ 2 /*return*/,
515
+ this.fetchEndpoint('get_transaction_receipt', { transactionHash: txHashHex }),
516
+ ];
520
517
  });
521
518
  });
522
519
  };
@@ -10,7 +10,7 @@ import type {
10
10
  GetTransactionResponse,
11
11
  GetTransactionStatusResponse,
12
12
  Invocation,
13
- TransactionReceipt,
13
+ TransactionReceiptResponse,
14
14
  } from '../types';
15
15
  import type { BigNumberish } from '../utils/number';
16
16
  import { BlockIdentifier } from './utils';
@@ -96,13 +96,7 @@ export declare abstract class ProviderInterface {
96
96
  * @returns the transacton object { transaction_id, status, transaction, block_number?, block_number?, transaction_index?, transaction_failure_reason? }
97
97
  */
98
98
  abstract getTransaction(txHash: BigNumberish): Promise<GetTransactionResponse>;
99
- abstract getTransactionReceipt({
100
- txHash,
101
- txId,
102
- }: {
103
- txHash?: BigNumberish;
104
- txId?: BigNumberish;
105
- }): Promise<TransactionReceipt>;
99
+ abstract getTransactionReceipt(txHash: BigNumberish): Promise<TransactionReceiptResponse>;
106
100
  /**
107
101
  * Deploys a given compiled contract (json) to starknet
108
102
  *
@@ -10,6 +10,7 @@ import {
10
10
  CompiledContract,
11
11
  DeployContractPayload,
12
12
  Endpoints,
13
+ EstimateFeeResponse,
13
14
  GetBlockResponse,
14
15
  GetCodeResponse,
15
16
  GetContractAddressesResponse,
@@ -17,14 +18,14 @@ import {
17
18
  GetTransactionStatusResponse,
18
19
  GetTransactionTraceResponse,
19
20
  Invocation,
20
- TransactionReceipt,
21
+ TransactionReceiptResponse,
21
22
  } from '../types';
22
23
  import { getSelectorFromName } from '../utils/hash';
23
24
  import { parse, stringify } from '../utils/json';
24
25
  import { BigNumberish, bigNumberishArrayToDecimalStringArray, toBN, toHex } from '../utils/number';
25
26
  import { compressProgram, randomAddress } from '../utils/stark';
26
27
  import { ProviderInterface } from './interface';
27
- import { BlockIdentifier, getFormattedBlockIdentifier, txIdentifier } from './utils';
28
+ import { BlockIdentifier, getFormattedBlockIdentifier } from './utils';
28
29
 
29
30
  type NetworkName = 'mainnet-alpha' | 'goerli-alpha';
30
31
 
@@ -153,6 +154,17 @@ export class Provider implements ProviderInterface {
153
154
  try {
154
155
  const { data } = await axios.request<Endpoints[T]['RESPONSE']>({
155
156
  method,
157
+ transformResponse:
158
+ endpoint === 'estimate_fee'
159
+ ? (res): EstimateFeeResponse => {
160
+ return parse(res, (_, v) => {
161
+ if (v && typeof v === 'bigint') {
162
+ return toBN(v.toString());
163
+ }
164
+ return v;
165
+ });
166
+ }
167
+ : axios.defaults.transformResponse,
156
168
  url: urljoin(baseUrl, endpoint, queryString),
157
169
  data: stringify(request),
158
170
  headers,
@@ -272,22 +284,12 @@ export class Provider implements ProviderInterface {
272
284
  * [Reference] (https://github.com/starkware-libs/cairo-lang/blob/master/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L104-L111)
273
285
  *
274
286
  * @param txHash
275
- * @param txId
276
287
  * @returns the transaction receipt object
277
288
  */
278
289
 
279
- public async getTransactionReceipt({
280
- txHash,
281
- txId,
282
- }: {
283
- txHash?: BigNumberish;
284
- txId?: BigNumberish;
285
- }): Promise<TransactionReceipt> {
286
- const { data } = await axios.get<TransactionReceipt>(
287
- urljoin(this.feederGatewayUrl, 'get_transaction_receipt', `?${txIdentifier(txHash, txId)}`)
288
- );
289
-
290
- return data;
290
+ public async getTransactionReceipt(txHash: BigNumberish): Promise<TransactionReceiptResponse> {
291
+ const txHashHex = toHex(toBN(txHash));
292
+ return this.fetchEndpoint('get_transaction_receipt', { transactionHash: txHashHex });
291
293
  }
292
294
 
293
295
  /**
@@ -10,7 +10,7 @@ import type {
10
10
  GetTransactionResponse,
11
11
  GetTransactionStatusResponse,
12
12
  Invocation,
13
- TransactionReceipt,
13
+ TransactionReceiptResponse,
14
14
  } from '../types';
15
15
  import type { BigNumberish } from '../utils/number';
16
16
  import { BlockIdentifier } from './utils';
@@ -109,13 +109,7 @@ export abstract class ProviderInterface {
109
109
  */
110
110
  public abstract getTransaction(txHash: BigNumberish): Promise<GetTransactionResponse>;
111
111
 
112
- public abstract getTransactionReceipt({
113
- txHash,
114
- txId,
115
- }: {
116
- txHash?: BigNumberish;
117
- txId?: BigNumberish;
118
- }): Promise<TransactionReceipt>;
112
+ public abstract getTransactionReceipt(txHash: BigNumberish): Promise<TransactionReceiptResponse>;
119
113
 
120
114
  /**
121
115
  * Deploys a given compiled contract (json) to starknet
package/src/types/api.ts CHANGED
@@ -1,3 +1,5 @@
1
+ import BN from 'bn.js';
2
+
1
3
  import { BlockIdentifier } from '../provider/utils';
2
4
  import { BigNumberish } from '../utils/number';
3
5
  import {
@@ -43,6 +45,13 @@ export type Endpoints = {
43
45
  REQUEST: never;
44
46
  RESPONSE: GetTransactionTraceResponse;
45
47
  };
48
+ get_transaction_receipt: {
49
+ QUERY: {
50
+ transactionHash: string;
51
+ };
52
+ REQUEST: never;
53
+ RESPONSE: TransactionReceiptResponse;
54
+ };
46
55
  get_storage_at: {
47
56
  QUERY: {
48
57
  contractAddress: string;
@@ -217,7 +226,7 @@ export type AddTransactionResponse = {
217
226
  address?: string;
218
227
  };
219
228
 
220
- export type TransactionReceipt = {
229
+ export type TransactionReceiptResponse = {
221
230
  status: Status;
222
231
  transaction_hash: string;
223
232
  transaction_index: number;
@@ -228,7 +237,7 @@ export type TransactionReceipt = {
228
237
  };
229
238
 
230
239
  export type EstimateFeeResponse = {
231
- amount: number;
240
+ amount: BN;
232
241
  unit: string;
233
242
  };
234
243
 
@@ -1,16 +1,16 @@
1
+ /* eslint-disable no-bitwise */
2
+ import { arrayify } from '@ethersproject/bytes';
3
+
1
4
  import { MASK_251, ZERO } from '../constants';
2
5
  import { addHexPrefix, removeHexPrefix } from './encode';
3
- import { assertInRange } from './number';
6
+ import { pedersen } from './hash';
7
+ import { BigNumberish, assertInRange, toBN, toHex } from './number';
4
8
 
5
- export function addAddressPadding(address: string): string {
6
- return addHexPrefix(removeHexPrefix(address).padStart(64, '0'));
9
+ export function addAddressPadding(address: BigNumberish): string {
10
+ return addHexPrefix(removeHexPrefix(toHex(toBN(address))).padStart(64, '0'));
7
11
  }
8
12
 
9
- export function validateAndParseAddress(address: string): string {
10
- if (typeof address !== 'string') {
11
- throw new Error('Invalid Address Type');
12
- }
13
-
13
+ export function validateAndParseAddress(address: BigNumberish): string {
14
14
  assertInRange(address, ZERO, MASK_251, 'Starknet Address');
15
15
 
16
16
  const result = addAddressPadding(address);
@@ -21,3 +21,24 @@ export function validateAndParseAddress(address: string): string {
21
21
 
22
22
  return result;
23
23
  }
24
+
25
+ // from https://github.com/ethers-io/ethers.js/blob/fc1e006575d59792fa97b4efb9ea2f8cca1944cf/packages/address/src.ts/index.ts#L12
26
+ export function getChecksumAddress(address: BigNumberish): string {
27
+ const chars = removeHexPrefix(validateAndParseAddress(address)).toLowerCase().split('');
28
+ const hashed = arrayify(pedersen([0, address]), { hexPad: 'left' }); // as the hash will be 251 bits (63 chars) we need to pad it to 64 chars without changing the number value ("left")
29
+
30
+ for (let i = 0; i < chars.length; i += 2) {
31
+ if (hashed[i >> 1] >> 4 >= 8) {
32
+ chars[i] = chars[i].toUpperCase();
33
+ }
34
+ if ((hashed[i >> 1] & 0x0f) >= 8) {
35
+ chars[i + 1] = chars[i + 1].toUpperCase();
36
+ }
37
+ }
38
+
39
+ return addHexPrefix(chars.join(''));
40
+ }
41
+
42
+ export function validateChecksumAddress(address: string): boolean {
43
+ return getChecksumAddress(address) === address;
44
+ }
package/types/api.d.ts CHANGED
@@ -1,3 +1,5 @@
1
+ import BN from 'bn.js';
2
+
1
3
  import { BlockIdentifier } from '../provider/utils';
2
4
  import { BigNumberish } from '../utils/number';
3
5
  import {
@@ -42,6 +44,13 @@ export declare type Endpoints = {
42
44
  REQUEST: never;
43
45
  RESPONSE: GetTransactionTraceResponse;
44
46
  };
47
+ get_transaction_receipt: {
48
+ QUERY: {
49
+ transactionHash: string;
50
+ };
51
+ REQUEST: never;
52
+ RESPONSE: TransactionReceiptResponse;
53
+ };
45
54
  get_storage_at: {
46
55
  QUERY: {
47
56
  contractAddress: string;
@@ -201,7 +210,7 @@ export declare type AddTransactionResponse = {
201
210
  transaction_hash: string;
202
211
  address?: string;
203
212
  };
204
- export declare type TransactionReceipt = {
213
+ export declare type TransactionReceiptResponse = {
205
214
  status: Status;
206
215
  transaction_hash: string;
207
216
  transaction_index: number;
@@ -211,7 +220,7 @@ export declare type TransactionReceipt = {
211
220
  events: string[];
212
221
  };
213
222
  export declare type EstimateFeeResponse = {
214
- amount: number;
223
+ amount: BN;
215
224
  unit: string;
216
225
  };
217
226
  export declare type RawArgs = {
@@ -1,2 +1,5 @@
1
- export declare function addAddressPadding(address: string): string;
2
- export declare function validateAndParseAddress(address: string): string;
1
+ import { BigNumberish } from './number';
2
+ export declare function addAddressPadding(address: BigNumberish): string;
3
+ export declare function validateAndParseAddress(address: BigNumberish): string;
4
+ export declare function getChecksumAddress(address: BigNumberish): string;
5
+ export declare function validateChecksumAddress(address: string): boolean;
package/utils/address.js CHANGED
@@ -1,17 +1,26 @@
1
1
  'use strict';
2
2
  Object.defineProperty(exports, '__esModule', { value: true });
3
- exports.validateAndParseAddress = exports.addAddressPadding = void 0;
3
+ exports.validateChecksumAddress =
4
+ exports.getChecksumAddress =
5
+ exports.validateAndParseAddress =
6
+ exports.addAddressPadding =
7
+ void 0;
8
+ /* eslint-disable no-bitwise */
9
+ var bytes_1 = require('@ethersproject/bytes');
4
10
  var constants_1 = require('../constants');
5
11
  var encode_1 = require('./encode');
12
+ var hash_1 = require('./hash');
6
13
  var number_1 = require('./number');
7
14
  function addAddressPadding(address) {
8
- return (0, encode_1.addHexPrefix)((0, encode_1.removeHexPrefix)(address).padStart(64, '0'));
15
+ return (0, encode_1.addHexPrefix)(
16
+ (0, encode_1.removeHexPrefix)((0, number_1.toHex)((0, number_1.toBN)(address))).padStart(
17
+ 64,
18
+ '0'
19
+ )
20
+ );
9
21
  }
10
22
  exports.addAddressPadding = addAddressPadding;
11
23
  function validateAndParseAddress(address) {
12
- if (typeof address !== 'string') {
13
- throw new Error('Invalid Address Type');
14
- }
15
24
  (0, number_1.assertInRange)(address, constants_1.ZERO, constants_1.MASK_251, 'Starknet Address');
16
25
  var result = addAddressPadding(address);
17
26
  if (!result.match(/^(0x)?[0-9a-fA-F]{64}$/)) {
@@ -20,3 +29,24 @@ function validateAndParseAddress(address) {
20
29
  return result;
21
30
  }
22
31
  exports.validateAndParseAddress = validateAndParseAddress;
32
+ // from https://github.com/ethers-io/ethers.js/blob/fc1e006575d59792fa97b4efb9ea2f8cca1944cf/packages/address/src.ts/index.ts#L12
33
+ function getChecksumAddress(address) {
34
+ var chars = (0, encode_1.removeHexPrefix)(validateAndParseAddress(address))
35
+ .toLowerCase()
36
+ .split('');
37
+ var hashed = (0, bytes_1.arrayify)((0, hash_1.pedersen)([0, address]), { hexPad: 'left' }); // as the hash will be 251 bits (63 chars) we need to pad it to 64 chars without changing the number value ("left")
38
+ for (var i = 0; i < chars.length; i += 2) {
39
+ if (hashed[i >> 1] >> 4 >= 8) {
40
+ chars[i] = chars[i].toUpperCase();
41
+ }
42
+ if ((hashed[i >> 1] & 0x0f) >= 8) {
43
+ chars[i + 1] = chars[i + 1].toUpperCase();
44
+ }
45
+ }
46
+ return (0, encode_1.addHexPrefix)(chars.join(''));
47
+ }
48
+ exports.getChecksumAddress = getChecksumAddress;
49
+ function validateChecksumAddress(address) {
50
+ return getChecksumAddress(address) === address;
51
+ }
52
+ exports.validateChecksumAddress = validateChecksumAddress;
@@ -1,5 +1,5 @@
1
1
  ---
2
- sidebar_position: 6
2
+ sidebar_position: 7
3
3
  ---
4
4
 
5
5
  # CHANGELOG
@@ -14,7 +14,7 @@ Contracts allow you to transform Cairo values, like `Uint256` to `BigNumber`. It
14
14
 
15
15
  `contract.attach(providerOrAccount)` _for changing the provider or account_
16
16
 
17
- `contract.connect(providerOrAccount)` _for changing the address of the connected contract_
17
+ `contract.connect(address)` _for changing the address of the connected contract_
18
18
 
19
19
  ## Contract properties
20
20
 
@@ -12,9 +12,9 @@ Contract Factory allow you to deploy contracts onto StarkNet. To deploy a Contra
12
12
 
13
13
  Creates a new instance of a ContractFactory for the contract described by the _compiledContract_.
14
14
 
15
- `contractFacotry.connect(providerOrAccount)` _for changing the provider or account_
15
+ `contractFactory.connect(providerOrAccount)` _for changing the provider or account_
16
16
 
17
- `contractFacotry.attach(address)` _for changing the address of the connected contract factory_
17
+ `contractFactory.attach(address)` _for changing the address of the connected contract factory_
18
18
 
19
19
  ## Properties
20
20
 
@@ -0,0 +1,34 @@
1
+ ---
2
+ sidebar_position: 6
3
+ ---
4
+
5
+ # Utils
6
+
7
+ Util functions are provided so you can use low level functions in your application.
8
+
9
+ ## `address`
10
+
11
+ the address helpers can be imported using:
12
+
13
+ ```js
14
+ import { address } from 'starknet.js';
15
+ ```
16
+
17
+ ### `getChecksumAddress(address: BigNumberish): string`
18
+
19
+ This function accepts an address as a `BigNumberish` and returns the checksummed address as a string.
20
+ An example:
21
+
22
+ ```js
23
+ import { address } from 'starknet.js';
24
+
25
+ const addressToCheck = '0x2fd23d9182193775423497fc0c472e156c57c69e4089a1967fb288a2d84e914';
26
+
27
+ const checksummedAddress = address.getChecksumAddress(addressToCheck);
28
+
29
+ console.log(checksummedAddress); // 0x02FD23D9182193775423497Fc0c472E156C57C69E4089a1967fb288a2D84e914
30
+ ```
31
+
32
+ ### `validateChecksumAddress(address: string): boolean`
33
+
34
+ This function validates the checksum address. It returns true if the address is valid, false otherwise.