starknet 3.10.3 → 3.12.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. package/.releaserc +1 -1
  2. package/CHANGELOG.md +30 -0
  3. package/__tests__/account.test.ts +3 -1
  4. package/__tests__/provider.test.ts +3 -3
  5. package/__tests__/utils/address.test.ts +30 -1
  6. package/account/default.d.ts +5 -3
  7. package/account/default.js +37 -6
  8. package/account/interface.d.ts +2 -2
  9. package/contract/default.d.ts +1 -1
  10. package/dist/account/default.d.ts +6 -3
  11. package/dist/account/default.js +23 -6
  12. package/dist/account/interface.d.ts +3 -2
  13. package/dist/contract/default.d.ts +1 -1
  14. package/dist/provider/default.d.ts +2 -6
  15. package/dist/provider/default.js +15 -11
  16. package/dist/provider/interface.d.ts +2 -5
  17. package/dist/provider/utils.js +2 -2
  18. package/dist/types/account.d.ts +5 -0
  19. package/dist/types/account.js +2 -0
  20. package/dist/types/api.d.ts +10 -2
  21. package/dist/types/lib.d.ts +1 -1
  22. package/dist/utils/address.d.ts +5 -2
  23. package/dist/utils/address.js +24 -5
  24. package/dist/utils/stark.js +1 -1
  25. package/package.json +2 -1
  26. package/provider/default.d.ts +2 -9
  27. package/provider/default.js +19 -22
  28. package/provider/interface.d.ts +2 -8
  29. package/provider/utils.js +2 -2
  30. package/src/account/default.ts +14 -6
  31. package/src/account/interface.ts +2 -2
  32. package/src/provider/default.ts +17 -15
  33. package/src/provider/interface.ts +2 -8
  34. package/src/provider/utils.ts +2 -2
  35. package/src/types/account.ts +7 -0
  36. package/src/types/api.ts +11 -2
  37. package/src/types/lib.ts +1 -1
  38. package/src/utils/address.ts +29 -8
  39. package/src/utils/stark.ts +1 -1
  40. package/types/account.d.ts +6 -0
  41. package/types/account.js +2 -0
  42. package/types/api.d.ts +11 -2
  43. package/types/lib.d.ts +1 -1
  44. package/utils/address.d.ts +5 -2
  45. package/utils/address.js +35 -5
  46. package/utils/stark.js +1 -1
  47. package/www/docs/API/changelog.md +1 -1
  48. package/www/docs/API/contract.md +1 -1
  49. package/www/docs/API/contractFacotry.md +2 -2
  50. package/www/docs/API/utils.md +34 -0
@@ -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
  *
package/provider/utils.js CHANGED
@@ -39,8 +39,8 @@ exports.txIdentifier = txIdentifier;
39
39
  * @returns block identifier object
40
40
  */
41
41
  function getBlockIdentifier(blockIdentifier) {
42
- if (blockIdentifier === null) {
43
- return { type: 'BLOCK_NUMBER', data: null };
42
+ if (blockIdentifier === null || blockIdentifier === 'latest') {
43
+ return { type: 'BLOCK_NUMBER', data: 'latest' }; // default to latest block
44
44
  }
45
45
  if (blockIdentifier === 'pending') {
46
46
  return { type: 'BLOCK_NUMBER', data: 'pending' };
@@ -8,7 +8,6 @@ import {
8
8
  Abi,
9
9
  AddTransactionResponse,
10
10
  Call,
11
- EstimateFeeResponse,
12
11
  InvocationsDetails,
13
12
  InvocationsSignerDetails,
14
13
  InvokeFunctionTransaction,
@@ -16,6 +15,7 @@ import {
16
15
  Signature,
17
16
  Transaction,
18
17
  } from '../types';
18
+ import { EstimateFee } from '../types/account';
19
19
  import { sign } from '../utils/ellipticCurve';
20
20
  import {
21
21
  computeHashOnElements,
@@ -60,7 +60,7 @@ export class Account extends Provider implements AccountInterface {
60
60
  nonce: providedNonce,
61
61
  blockIdentifier = 'pending',
62
62
  }: { nonce?: BigNumberish; blockIdentifier?: BlockIdentifier } = {}
63
- ): Promise<EstimateFeeResponse> {
63
+ ): Promise<EstimateFee> {
64
64
  const transactions = Array.isArray(calls) ? calls : [calls];
65
65
  const nonce = providedNonce ?? (await this.getNonce());
66
66
  const version = toBN(feeTransactionVersion);
@@ -76,7 +76,7 @@ export class Account extends Provider implements AccountInterface {
76
76
  const signature = await this.signer.signTransaction(transactions, signerDetails);
77
77
 
78
78
  const calldata = fromCallsToExecuteCalldataWithNonce(transactions, nonce);
79
- return this.fetchEndpoint(
79
+ const fetchedEstimate = await this.fetchEndpoint(
80
80
  'estimate_fee',
81
81
  { blockIdentifier },
82
82
  {
@@ -87,6 +87,12 @@ export class Account extends Provider implements AccountInterface {
87
87
  signature: bigNumberishArrayToDecimalStringArray(signature),
88
88
  }
89
89
  );
90
+ const suggestedMaxFee = estimatedFeeToMaxFee(fetchedEstimate.amount);
91
+
92
+ return {
93
+ ...fetchedEstimate,
94
+ suggestedMaxFee,
95
+ };
90
96
  }
91
97
 
92
98
  /**
@@ -94,7 +100,9 @@ export class Account extends Provider implements AccountInterface {
94
100
  *
95
101
  * [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/gateway/gateway_client.py#L13-L17)
96
102
  *
97
- * @param transaction - transaction to be invoked
103
+ * @param calls - one or more calls to be executed
104
+ * @param abis - one or more abis which can be used to display the calls
105
+ * @param transactionsDetail - optional transaction details
98
106
  * @returns a confirmation of invoking a function on the starknet contract
99
107
  */
100
108
  public async execute(
@@ -108,8 +116,8 @@ export class Account extends Provider implements AccountInterface {
108
116
  if (transactionsDetail.maxFee || transactionsDetail.maxFee === 0) {
109
117
  maxFee = transactionsDetail.maxFee;
110
118
  } else {
111
- const estimatedFee = (await this.estimateFee(transactions, { nonce })).amount;
112
- maxFee = estimatedFeeToMaxFee(estimatedFee).toString();
119
+ const { suggestedMaxFee } = await this.estimateFee(transactions, { nonce });
120
+ maxFee = suggestedMaxFee.toString();
113
121
  }
114
122
 
115
123
  const signerDetails: InvocationsSignerDetails = {
@@ -5,11 +5,11 @@ import {
5
5
  AddTransactionResponse,
6
6
  Call,
7
7
  DeployContractPayload,
8
- EstimateFeeResponse,
9
8
  Invocation,
10
9
  InvocationsDetails,
11
10
  Signature,
12
11
  } from '../types';
12
+ import { EstimateFee } from '../types/account';
13
13
  import { BigNumberish } from '../utils/number';
14
14
  import { TypedData } from '../utils/typedData/types';
15
15
 
@@ -44,7 +44,7 @@ export abstract class AccountInterface extends ProviderInterface {
44
44
  *
45
45
  * @returns response from addTransaction
46
46
  */
47
- public abstract estimateFee(invocation: Invocation): Promise<EstimateFeeResponse>;
47
+ public abstract estimateFee(invocation: Invocation): Promise<EstimateFee>;
48
48
 
49
49
  /**
50
50
  * Invoke execute function in account contract
@@ -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
@@ -43,8 +43,8 @@ type BlockIdentifierObject =
43
43
  * @returns block identifier object
44
44
  */
45
45
  export function getBlockIdentifier(blockIdentifier: BlockIdentifier): BlockIdentifierObject {
46
- if (blockIdentifier === null) {
47
- return { type: 'BLOCK_NUMBER', data: null };
46
+ if (blockIdentifier === null || blockIdentifier === 'latest') {
47
+ return { type: 'BLOCK_NUMBER', data: 'latest' }; // default to latest block
48
48
  }
49
49
  if (blockIdentifier === 'pending') {
50
50
  return { type: 'BLOCK_NUMBER', data: 'pending' };
@@ -0,0 +1,7 @@
1
+ import BN from 'bn.js';
2
+
3
+ import { EstimateFeeResponse } from './api';
4
+
5
+ export interface EstimateFee extends EstimateFeeResponse {
6
+ suggestedMaxFee: BN;
7
+ }
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
 
package/src/types/lib.ts CHANGED
@@ -60,7 +60,7 @@ export type Abi = Array<FunctionAbi | StructAbi>;
60
60
 
61
61
  export type EntryPointsByType = object;
62
62
  export type Program = Record<any, any>;
63
- export type BlockNumber = 'pending' | null | number;
63
+ export type BlockNumber = 'pending' | 'latest' | null | number;
64
64
 
65
65
  export type CompiledContract = {
66
66
  abi: Abi;
@@ -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
+ }
@@ -50,7 +50,7 @@ export function compileCalldata(args: RawArgs): Calldata {
50
50
  });
51
51
  }
52
52
 
53
- export function estimatedFeeToMaxFee(estimatedFee: BigNumberish, overhead: number = 0.15): BN {
53
+ export function estimatedFeeToMaxFee(estimatedFee: BigNumberish, overhead: number = 0.5): BN {
54
54
  // BN can only handle Integers, so we need to do all calulations with integers
55
55
  const overHeadPercent = Math.round((1 + overhead) * 100);
56
56
  return toBN(estimatedFee).mul(toBN(overHeadPercent)).div(toBN(100));
@@ -0,0 +1,6 @@
1
+ import BN from 'bn.js';
2
+
3
+ import { EstimateFeeResponse } from './api';
4
+ export interface EstimateFee extends EstimateFeeResponse {
5
+ suggestedMaxFee: BN;
6
+ }
@@ -0,0 +1,2 @@
1
+ 'use strict';
2
+ Object.defineProperty(exports, '__esModule', { value: true });
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 = {
package/types/lib.d.ts CHANGED
@@ -54,7 +54,7 @@ export declare type StructAbi = {
54
54
  export declare type Abi = Array<FunctionAbi | StructAbi>;
55
55
  export declare type EntryPointsByType = object;
56
56
  export declare type Program = Record<any, any>;
57
- export declare type BlockNumber = 'pending' | null | number;
57
+ export declare type BlockNumber = 'pending' | 'latest' | null | number;
58
58
  export declare type CompiledContract = {
59
59
  abi: Abi;
60
60
  entry_points_by_type: EntryPointsByType;
@@ -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;
package/utils/stark.js CHANGED
@@ -114,7 +114,7 @@ function compileCalldata(args) {
114
114
  exports.compileCalldata = compileCalldata;
115
115
  function estimatedFeeToMaxFee(estimatedFee, overhead) {
116
116
  if (overhead === void 0) {
117
- overhead = 0.15;
117
+ overhead = 0.5;
118
118
  }
119
119
  // BN can only handle Integers, so we need to do all calulations with integers
120
120
  var overHeadPercent = Math.round((1 + overhead) * 100);
@@ -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.