starknet 3.8.0 → 3.10.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 (80) hide show
  1. package/CHANGELOG.md +35 -0
  2. package/README.md +18 -53
  3. package/__mocks__/ArgentAccount.json +32022 -38726
  4. package/__tests__/accountContract.test.ts +42 -32
  5. package/__tests__/contract.test.ts +20 -6
  6. package/__tests__/utils/__snapshots__/utils.browser.test.ts.snap +2 -2
  7. package/__tests__/utils/__snapshots__/utils.test.ts.snap +2 -2
  8. package/__tests__/utils/ellipticalCurve.test.ts +26 -8
  9. package/__tests__/utils/transactionHash.test.ts +17 -0
  10. package/__tests__/utils/utils.test.ts +10 -0
  11. package/account/default.d.ts +10 -6
  12. package/account/default.js +32 -39
  13. package/account/interface.d.ts +2 -0
  14. package/constants.d.ts +9 -0
  15. package/constants.js +13 -0
  16. package/contract/contractFactory.d.ts +5 -5
  17. package/contract/default.js +14 -2
  18. package/dist/account/default.d.ts +6 -6
  19. package/dist/account/default.js +32 -23
  20. package/dist/account/interface.d.ts +2 -0
  21. package/dist/constants.d.ts +9 -0
  22. package/dist/constants.js +12 -1
  23. package/dist/contract/contractFactory.d.ts +5 -5
  24. package/dist/contract/default.js +14 -2
  25. package/dist/index.d.ts +1 -0
  26. package/dist/index.js +2 -1
  27. package/dist/provider/default.d.ts +4 -1
  28. package/dist/provider/default.js +20 -1
  29. package/dist/provider/interface.d.ts +2 -0
  30. package/dist/signer/default.js +4 -2
  31. package/dist/signer/ledger.js +4 -2
  32. package/dist/types/api.d.ts +1 -0
  33. package/dist/types/lib.d.ts +1 -0
  34. package/dist/types/signer.d.ts +2 -0
  35. package/dist/utils/hash.d.ts +5 -3
  36. package/dist/utils/hash.js +25 -23
  37. package/dist/utils/stark.d.ts +3 -0
  38. package/dist/utils/stark.js +8 -1
  39. package/dist/utils/transaction.d.ts +2 -0
  40. package/dist/utils/transaction.js +5 -1
  41. package/dist/utils/typedData/index.d.ts +2 -2
  42. package/dist/utils/typedData/types.d.ts +3 -3
  43. package/dist/utils/typedData/utils.d.ts +1 -1
  44. package/index.d.ts +1 -0
  45. package/index.js +2 -0
  46. package/package.json +1 -1
  47. package/provider/default.d.ts +4 -1
  48. package/provider/default.js +21 -1
  49. package/provider/interface.d.ts +2 -0
  50. package/signer/default.js +12 -5
  51. package/signer/ledger.js +12 -5
  52. package/src/account/default.ts +38 -17
  53. package/src/account/interface.ts +3 -0
  54. package/src/constants.ts +10 -0
  55. package/src/contract/contractFactory.ts +5 -5
  56. package/src/contract/default.ts +13 -2
  57. package/src/index.ts +1 -0
  58. package/src/provider/default.ts +23 -2
  59. package/src/provider/interface.ts +3 -0
  60. package/src/signer/default.ts +10 -5
  61. package/src/signer/ledger.ts +10 -5
  62. package/src/types/api.ts +1 -0
  63. package/src/types/lib.ts +1 -0
  64. package/src/types/signer.ts +2 -0
  65. package/src/utils/hash.ts +70 -26
  66. package/src/utils/stark.ts +8 -1
  67. package/src/utils/transaction.ts +7 -0
  68. package/types/api.d.ts +1 -0
  69. package/types/lib.d.ts +1 -0
  70. package/types/signer.d.ts +2 -0
  71. package/utils/hash.d.ts +25 -7
  72. package/utils/hash.js +60 -26
  73. package/utils/stark.d.ts +4 -0
  74. package/utils/stark.js +13 -1
  75. package/utils/transaction.d.ts +5 -0
  76. package/utils/transaction.js +12 -1
  77. package/utils/typedData/index.d.ts +2 -2
  78. package/utils/typedData/types.d.ts +3 -3
  79. package/utils/typedData/utils.d.ts +1 -1
  80. package/__tests__/constancts.ts +0 -2
@@ -1,13 +1,13 @@
1
- import { Account } from '../account';
2
- import { Provider } from '../provider';
1
+ import { AccountInterface } from '../account';
2
+ import { ProviderInterface } from '../provider';
3
3
  import { Abi, CompiledContract, RawCalldata } from '../types';
4
4
  import { BigNumberish } from '../utils/number';
5
5
  import { Contract } from './default';
6
6
  export declare class ContractFactory {
7
7
  abi: Abi;
8
8
  compiledContract: CompiledContract;
9
- providerOrAccount: Provider | Account;
10
- constructor(compiledContract: CompiledContract, providerOrAccount?: Provider | Account, abi?: Abi);
9
+ providerOrAccount: ProviderInterface | AccountInterface;
10
+ constructor(compiledContract: CompiledContract, providerOrAccount?: ProviderInterface | AccountInterface, abi?: Abi);
11
11
  /**
12
12
  * Deploys contract and returns new instance of the Contract
13
13
  *
@@ -21,7 +21,7 @@ export declare class ContractFactory {
21
21
  *
22
22
  * @param providerOrAccount - new Provider or Account to attach to
23
23
  */
24
- connect(providerOrAccount: Provider | Account): ContractFactory;
24
+ connect(providerOrAccount: ProviderInterface | AccountInterface): ContractFactory;
25
25
  /**
26
26
  * Attaches current abi and provider or account to the new address
27
27
  *
@@ -116,8 +116,20 @@ function buildInvoke(contract, functionAbi) {
116
116
  args[_i] = arguments[_i];
117
117
  }
118
118
  return __awaiter(this, void 0, void 0, function () {
119
+ var inputs, inputsLength, options;
119
120
  return __generator(this, function (_a) {
120
- return [2 /*return*/, contract.invoke(functionAbi.name, args)];
121
+ inputs = functionAbi.inputs;
122
+ inputsLength = inputs.reduce(function (acc, input) {
123
+ if (!/_len$/.test(input.name)) {
124
+ return acc + 1;
125
+ }
126
+ return acc;
127
+ }, 0);
128
+ options = {};
129
+ if (inputsLength + 1 === args.length && typeof args[args.length - 1] === 'object') {
130
+ Object.assign(options, args.pop());
131
+ }
132
+ return [2 /*return*/, contract.invoke(functionAbi.name, args, options)];
121
133
  });
122
134
  });
123
135
  };
@@ -371,7 +383,7 @@ var Contract = /** @class */ (function () {
371
383
  if (member.type === 'felt') {
372
384
  return acc + 1;
373
385
  }
374
- return acc + _this.structMemberNum(member.type);
386
+ return acc + _this.calculateStructMembers(member.type);
375
387
  }, 0);
376
388
  };
377
389
  /**
package/dist/index.d.ts CHANGED
@@ -14,6 +14,7 @@ export * as encode from './utils/encode';
14
14
  export * as hash from './utils/hash';
15
15
  export * as json from './utils/json';
16
16
  export * as number from './utils/number';
17
+ export * as transaction from './utils/transaction';
17
18
  export * as stark from './utils/stark';
18
19
  export * as ec from './utils/ellipticCurve';
19
20
  export * as uint256 from './utils/uint256';
package/dist/index.js CHANGED
@@ -22,7 +22,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
22
22
  return result;
23
23
  };
24
24
  Object.defineProperty(exports, "__esModule", { value: true });
25
- exports.typedData = exports.shortString = exports.uint256 = exports.ec = exports.stark = exports.number = exports.json = exports.hash = exports.encode = exports.constants = void 0;
25
+ exports.typedData = exports.shortString = exports.uint256 = exports.ec = exports.stark = exports.transaction = exports.number = exports.json = exports.hash = exports.encode = exports.constants = void 0;
26
26
  /**
27
27
  * Main
28
28
  */
@@ -39,6 +39,7 @@ exports.encode = __importStar(require("./utils/encode"));
39
39
  exports.hash = __importStar(require("./utils/hash"));
40
40
  exports.json = __importStar(require("./utils/json"));
41
41
  exports.number = __importStar(require("./utils/number"));
42
+ exports.transaction = __importStar(require("./utils/transaction"));
42
43
  exports.stark = __importStar(require("./utils/stark"));
43
44
  exports.ec = __importStar(require("./utils/ellipticCurve"));
44
45
  exports.uint256 = __importStar(require("./utils/uint256"));
@@ -1,3 +1,4 @@
1
+ import { StarknetChainId } from '../constants';
1
2
  import { Abi, AddTransactionResponse, Call, CallContractResponse, DeployContractPayload, Endpoints, GetBlockResponse, GetCodeResponse, GetContractAddressesResponse, GetTransactionResponse, GetTransactionStatusResponse, GetTransactionTraceResponse, Invocation, TransactionReceipt } from '../types';
2
3
  import { BigNumberish } from '../utils/number';
3
4
  import { ProviderInterface } from './interface';
@@ -12,8 +13,10 @@ export declare class Provider implements ProviderInterface {
12
13
  baseUrl: string;
13
14
  feederGatewayUrl: string;
14
15
  gatewayUrl: string;
15
- constructor(optionsOrProvider?: ProviderOptions | Provider);
16
+ chainId: StarknetChainId;
17
+ constructor(optionsOrProvider?: ProviderOptions | ProviderInterface);
16
18
  protected static getNetworkFromName(name: NetworkName): "https://alpha-mainnet.starknet.io" | "https://alpha4.starknet.io";
19
+ protected static getChainIdFromBaseUrl(baseUrl: string): StarknetChainId;
17
20
  private getFetchUrl;
18
21
  private getFetchMethod;
19
22
  private getQueryString;
@@ -69,10 +69,12 @@ Object.defineProperty(exports, "__esModule", { value: true });
69
69
  exports.Provider = void 0;
70
70
  var axios_1 = __importDefault(require("axios"));
71
71
  var url_join_1 = __importDefault(require("url-join"));
72
+ var constants_1 = require("../constants");
72
73
  var hash_1 = require("../utils/hash");
73
74
  var json_1 = require("../utils/json");
74
75
  var number_1 = require("../utils/number");
75
76
  var stark_1 = require("../utils/stark");
77
+ var interface_1 = require("./interface");
76
78
  var utils_1 = require("./utils");
77
79
  function wait(delay) {
78
80
  return new Promise(function (res) { return setTimeout(res, delay); });
@@ -89,16 +91,20 @@ function isEmptyQueryObject(obj) {
89
91
  var Provider = /** @class */ (function () {
90
92
  function Provider(optionsOrProvider) {
91
93
  if (optionsOrProvider === void 0) { optionsOrProvider = { network: 'goerli-alpha' }; }
92
- if (optionsOrProvider instanceof Provider) {
94
+ var _a;
95
+ if (optionsOrProvider instanceof interface_1.ProviderInterface) {
93
96
  this.baseUrl = optionsOrProvider.baseUrl;
94
97
  this.feederGatewayUrl = optionsOrProvider.feederGatewayUrl;
95
98
  this.gatewayUrl = optionsOrProvider.gatewayUrl;
99
+ this.chainId =
100
+ (_a = optionsOrProvider.chainId) !== null && _a !== void 0 ? _a : Provider.getChainIdFromBaseUrl(optionsOrProvider.baseUrl);
96
101
  }
97
102
  else {
98
103
  var baseUrl = 'baseUrl' in optionsOrProvider
99
104
  ? optionsOrProvider.baseUrl
100
105
  : Provider.getNetworkFromName(optionsOrProvider.network);
101
106
  this.baseUrl = baseUrl;
107
+ this.chainId = Provider.getChainIdFromBaseUrl(baseUrl);
102
108
  this.feederGatewayUrl = (0, url_join_1.default)(baseUrl, 'feeder_gateway');
103
109
  this.gatewayUrl = (0, url_join_1.default)(baseUrl, 'gateway');
104
110
  }
@@ -112,6 +118,19 @@ var Provider = /** @class */ (function () {
112
118
  return 'https://alpha4.starknet.io';
113
119
  }
114
120
  };
121
+ Provider.getChainIdFromBaseUrl = function (baseUrl) {
122
+ try {
123
+ var url = new URL(baseUrl);
124
+ if (url.host.includes('mainnet.starknet.io')) {
125
+ return constants_1.StarknetChainId.MAINNET;
126
+ }
127
+ }
128
+ catch (_a) {
129
+ // eslint-disable-next-line no-console
130
+ console.error("Could not parse baseUrl: " + baseUrl);
131
+ }
132
+ return constants_1.StarknetChainId.TESTNET;
133
+ };
115
134
  Provider.prototype.getFetchUrl = function (endpoint) {
116
135
  var gatewayUrlEndpoints = ['add_transaction'];
117
136
  return gatewayUrlEndpoints.includes(endpoint) ? this.gatewayUrl : this.feederGatewayUrl;
@@ -1,3 +1,4 @@
1
+ import { StarknetChainId } from '../constants';
1
2
  import type { AddTransactionResponse, Call, CallContractResponse, DeployContractPayload, GetBlockResponse, GetCodeResponse, GetContractAddressesResponse, GetTransactionResponse, GetTransactionStatusResponse, Invocation, TransactionReceipt } from '../types';
2
3
  import type { BigNumberish } from '../utils/number';
3
4
  import { BlockIdentifier } from './utils';
@@ -5,6 +6,7 @@ export declare abstract class ProviderInterface {
5
6
  abstract baseUrl: string;
6
7
  abstract feederGatewayUrl: string;
7
8
  abstract gatewayUrl: string;
9
+ abstract chainId: StarknetChainId;
8
10
  /**
9
11
  * Gets the smart contract address on the goerli testnet.
10
12
  *
@@ -39,6 +39,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
39
39
  exports.Signer = void 0;
40
40
  var ellipticCurve_1 = require("../utils/ellipticCurve");
41
41
  var hash_1 = require("../utils/hash");
42
+ var transaction_1 = require("../utils/transaction");
42
43
  var typedData_1 = require("../utils/typedData");
43
44
  var Signer = /** @class */ (function () {
44
45
  function Signer(keyPair) {
@@ -53,12 +54,13 @@ var Signer = /** @class */ (function () {
53
54
  };
54
55
  Signer.prototype.signTransaction = function (transactions, transactionsDetail, abis) {
55
56
  return __awaiter(this, void 0, void 0, function () {
56
- var msgHash;
57
+ var calldata, msgHash;
57
58
  return __generator(this, function (_a) {
58
59
  if (abis && abis.length !== transactions.length) {
59
60
  throw new Error('ABI must be provided for each transaction or no transaction');
60
61
  }
61
- msgHash = (0, hash_1.hashMulticall)(transactionsDetail.walletAddress, transactions, transactionsDetail.nonce.toString(), transactionsDetail.maxFee.toString());
62
+ calldata = (0, transaction_1.fromCallsToExecuteCalldataWithNonce)(transactions, transactionsDetail.nonce);
63
+ msgHash = (0, hash_1.calculcateTransactionHash)(transactionsDetail.walletAddress, transactionsDetail.version, (0, hash_1.getSelectorFromName)('__execute__'), calldata, transactionsDetail.maxFee, transactionsDetail.chainId);
62
64
  return [2 /*return*/, (0, ellipticCurve_1.sign)(this.keyPair, msgHash)];
63
65
  });
64
66
  });
@@ -44,6 +44,7 @@ var hw_app_eth_1 = __importDefault(require("@ledgerhq/hw-app-eth"));
44
44
  var hw_transport_webhid_1 = __importDefault(require("@ledgerhq/hw-transport-webhid"));
45
45
  var encode_1 = require("../utils/encode");
46
46
  var hash_1 = require("../utils/hash");
47
+ var transaction_1 = require("../utils/transaction");
47
48
  var typedData_1 = require("../utils/typedData");
48
49
  function hexZeroPad(hash, length) {
49
50
  var value = hash;
@@ -101,9 +102,10 @@ var LedgerBlindSigner = /** @class */ (function () {
101
102
  };
102
103
  LedgerBlindSigner.prototype.signTransaction = function (transactions, transactionsDetail) {
103
104
  return __awaiter(this, void 0, void 0, function () {
104
- var msgHash;
105
+ var calldata, msgHash;
105
106
  return __generator(this, function (_a) {
106
- msgHash = (0, hash_1.hashMulticall)(transactionsDetail.walletAddress, transactions, transactionsDetail.nonce.toString(), transactionsDetail.maxFee.toString());
107
+ calldata = (0, transaction_1.fromCallsToExecuteCalldataWithNonce)(transactions, transactionsDetail.nonce);
108
+ msgHash = (0, hash_1.calculcateTransactionHash)(transactionsDetail.walletAddress, transactionsDetail.version, (0, hash_1.getSelectorFromName)('__execute__'), calldata, transactionsDetail.maxFee, transactionsDetail.chainId);
107
109
  return [2 /*return*/, this.sign(msgHash)];
108
110
  });
109
111
  });
@@ -92,6 +92,7 @@ export declare type InvokeFunctionTransaction = {
92
92
  calldata?: RawCalldata;
93
93
  nonce?: BigNumberish;
94
94
  max_fee?: BigNumberish;
95
+ version?: BigNumberish;
95
96
  };
96
97
  export declare type InvokeFunctionTrace = {
97
98
  caller_address: string;
@@ -18,6 +18,7 @@ export declare type Call = Omit<Invocation, 'signature'>;
18
18
  export declare type InvocationsDetails = {
19
19
  nonce?: BigNumberish;
20
20
  maxFee?: BigNumberish;
21
+ version?: BigNumberish;
21
22
  };
22
23
  export declare type Status = 'NOT_RECEIVED' | 'RECEIVED' | 'PENDING' | 'ACCEPTED_ON_L2' | 'ACCEPTED_ON_L1' | 'REJECTED';
23
24
  export declare type TransactionStatus = 'TRANSACTION_RECEIVED';
@@ -1,4 +1,6 @@
1
+ import { StarknetChainId } from '../constants';
1
2
  import { InvocationsDetails } from './lib';
2
3
  export interface InvocationsSignerDetails extends Required<InvocationsDetails> {
3
4
  walletAddress: string;
5
+ chainId: StarknetChainId;
4
6
  }
@@ -1,8 +1,8 @@
1
1
  import BN from 'bn.js';
2
- import { Call } from '../types';
2
+ import { StarknetChainId, TransactionHashPrefix } from '../constants';
3
3
  import { BigNumberish } from './number';
4
- export declare const transactionPrefix: string;
5
4
  export declare const transactionVersion = 0;
5
+ export declare const feeTransactionVersion: BN;
6
6
  /**
7
7
  * Function to get the starknet keccak hash from a string
8
8
  *
@@ -21,4 +21,6 @@ export declare function starknetKeccak(value: string): BN;
21
21
  export declare function getSelectorFromName(funcName: string): string;
22
22
  export declare function pedersen(input: [BigNumberish, BigNumberish]): string;
23
23
  export declare function computeHashOnElements(data: BigNumberish[]): string;
24
- export declare function hashMulticall(account: string, transactions: Call[], nonce: string, maxFee: string): string;
24
+ export declare function calculateTransactionHashCommon(txHashPrefix: TransactionHashPrefix, version: BigNumberish, contractAddress: BigNumberish, entryPointSelector: BigNumberish, calldata: BigNumberish[], maxFee: BigNumberish, chainId: StarknetChainId, additionalData?: BigNumberish[]): string;
25
+ export declare function calculateDeployTransactionHash(contractAddress: BigNumberish, constructorCalldata: BigNumberish[], version: BigNumberish, chainId: StarknetChainId): string;
26
+ export declare function calculcateTransactionHash(contractAddress: BigNumberish, version: BigNumberish, entryPointSelector: BigNumberish, calldata: BigNumberish[], maxFee: BigNumberish, chainId: StarknetChainId): string;
@@ -28,16 +28,15 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
28
28
  return (mod && mod.__esModule) ? mod : { "default": mod };
29
29
  };
30
30
  Object.defineProperty(exports, "__esModule", { value: true });
31
- exports.hashMulticall = exports.computeHashOnElements = exports.pedersen = exports.getSelectorFromName = exports.starknetKeccak = exports.transactionVersion = exports.transactionPrefix = void 0;
31
+ exports.calculcateTransactionHash = exports.calculateDeployTransactionHash = exports.calculateTransactionHashCommon = exports.computeHashOnElements = exports.pedersen = exports.getSelectorFromName = exports.starknetKeccak = exports.feeTransactionVersion = exports.transactionVersion = void 0;
32
32
  var keccak_1 = require("ethereum-cryptography/keccak");
33
33
  var minimalistic_assert_1 = __importDefault(require("minimalistic-assert"));
34
34
  var constants_1 = require("../constants");
35
35
  var ellipticCurve_1 = require("./ellipticCurve");
36
36
  var encode_1 = require("./encode");
37
37
  var number_1 = require("./number");
38
- var shortString_1 = require("./shortString");
39
- exports.transactionPrefix = (0, shortString_1.encodeShortString)('StarkNet Transaction');
40
38
  exports.transactionVersion = 0;
39
+ exports.feeTransactionVersion = (0, number_1.toBN)(2).pow((0, number_1.toBN)(128)).add((0, number_1.toBN)(exports.transactionVersion));
41
40
  function keccakHex(value) {
42
41
  return (0, encode_1.addHexPrefix)((0, encode_1.buf2hex)((0, keccak_1.keccak256)((0, encode_1.utf8ToArray)(value))));
43
42
  }
@@ -88,25 +87,28 @@ function computeHashOnElements(data) {
88
87
  return __spreadArray(__spreadArray([], __read(data), false), [data.length], false).reduce(function (x, y) { return pedersen([x, y]); }, 0).toString();
89
88
  }
90
89
  exports.computeHashOnElements = computeHashOnElements;
91
- function hashMulticall(account, transactions, nonce, maxFee) {
92
- var hashArray = transactions
93
- .map(function (_a) {
94
- var contractAddress = _a.contractAddress, entrypoint = _a.entrypoint, calldata = _a.calldata;
95
- return [
96
- contractAddress,
97
- getSelectorFromName(entrypoint),
98
- computeHashOnElements(calldata || []),
99
- ];
100
- })
101
- .map(number_1.bigNumberishArrayToDecimalStringArray)
102
- .map(computeHashOnElements);
103
- return computeHashOnElements([
104
- exports.transactionPrefix,
105
- account,
106
- computeHashOnElements(hashArray),
107
- nonce,
90
+ // following implementation is based on this python implementation:
91
+ // https://github.com/starkware-libs/cairo-lang/blob/b614d1867c64f3fb2cf4a4879348cfcf87c3a5a7/src/starkware/starknet/core/os/transaction_hash/transaction_hash.py
92
+ function calculateTransactionHashCommon(txHashPrefix, version, contractAddress, entryPointSelector, calldata, maxFee, chainId, additionalData) {
93
+ if (additionalData === void 0) { additionalData = []; }
94
+ var calldataHash = computeHashOnElements(calldata);
95
+ var dataToHash = __spreadArray([
96
+ txHashPrefix,
97
+ version,
98
+ contractAddress,
99
+ entryPointSelector,
100
+ calldataHash,
108
101
  maxFee,
109
- exports.transactionVersion,
110
- ]);
102
+ chainId
103
+ ], __read(additionalData), false);
104
+ return computeHashOnElements(dataToHash);
111
105
  }
112
- exports.hashMulticall = hashMulticall;
106
+ exports.calculateTransactionHashCommon = calculateTransactionHashCommon;
107
+ function calculateDeployTransactionHash(contractAddress, constructorCalldata, version, chainId) {
108
+ return calculateTransactionHashCommon(constants_1.TransactionHashPrefix.DEPLOY, version, contractAddress, getSelectorFromName('constructor'), constructorCalldata, constants_1.ZERO, chainId);
109
+ }
110
+ exports.calculateDeployTransactionHash = calculateDeployTransactionHash;
111
+ function calculcateTransactionHash(contractAddress, version, entryPointSelector, calldata, maxFee, chainId) {
112
+ return calculateTransactionHashCommon(constants_1.TransactionHashPrefix.INVOKE, version, contractAddress, entryPointSelector, calldata, maxFee, chainId);
113
+ }
114
+ exports.calculcateTransactionHash = calculcateTransactionHash;
@@ -1,4 +1,6 @@
1
+ import BN from 'bn.js';
1
2
  import { Calldata, CompressedProgram, Program, RawArgs, Signature } from '../types';
3
+ import { BigNumberish } from './number';
2
4
  /**
3
5
  * Function to compress compiled cairo program
4
6
  *
@@ -11,3 +13,4 @@ export declare function randomAddress(): string;
11
13
  export declare function makeAddress(input: string): string;
12
14
  export declare function formatSignature(sig?: Signature): string[];
13
15
  export declare function compileCalldata(args: RawArgs): Calldata;
16
+ export declare function estimatedFeeToMaxFee(estimatedFee: BigNumberish, overhead?: number): BN;
@@ -25,7 +25,7 @@ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
25
25
  return to.concat(ar || Array.prototype.slice.call(from));
26
26
  };
27
27
  Object.defineProperty(exports, "__esModule", { value: true });
28
- exports.compileCalldata = exports.formatSignature = exports.makeAddress = exports.randomAddress = exports.compressProgram = void 0;
28
+ exports.estimatedFeeToMaxFee = exports.compileCalldata = exports.formatSignature = exports.makeAddress = exports.randomAddress = exports.compressProgram = void 0;
29
29
  var pako_1 = require("pako");
30
30
  var ellipticCurve_1 = require("./ellipticCurve");
31
31
  var encode_1 = require("./encode");
@@ -82,3 +82,10 @@ function compileCalldata(args) {
82
82
  });
83
83
  }
84
84
  exports.compileCalldata = compileCalldata;
85
+ function estimatedFeeToMaxFee(estimatedFee, overhead) {
86
+ if (overhead === void 0) { overhead = 0.15; }
87
+ // BN can only handle Integers, so we need to do all calulations with integers
88
+ var overHeadPercent = Math.round((1 + overhead) * 100);
89
+ return (0, number_1.toBN)(estimatedFee).mul((0, number_1.toBN)(overHeadPercent)).div((0, number_1.toBN)(100));
90
+ }
91
+ exports.estimatedFeeToMaxFee = estimatedFeeToMaxFee;
@@ -1,4 +1,5 @@
1
1
  import { Call, ParsedStruct } from '../types';
2
+ import { BigNumberish } from './number';
2
3
  /**
3
4
  * Transforms a list of Calls, each with their own calldata, into
4
5
  * two arrays: one with the entrypoints, and one with the concatenated calldata.
@@ -16,3 +17,4 @@ export declare const transformCallsToMulticallArrays: (calls: Call[]) => {
16
17
  * @returns
17
18
  */
18
19
  export declare const fromCallsToExecuteCalldata: (calls: Call[]) => string[];
20
+ export declare const fromCallsToExecuteCalldataWithNonce: (calls: Call[], nonce: BigNumberish) => string[];
@@ -25,7 +25,7 @@ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
25
25
  return to.concat(ar || Array.prototype.slice.call(from));
26
26
  };
27
27
  Object.defineProperty(exports, "__esModule", { value: true });
28
- exports.fromCallsToExecuteCalldata = exports.transformCallsToMulticallArrays = void 0;
28
+ exports.fromCallsToExecuteCalldataWithNonce = exports.fromCallsToExecuteCalldata = exports.transformCallsToMulticallArrays = void 0;
29
29
  var hash_1 = require("./hash");
30
30
  var number_1 = require("./number");
31
31
  /**
@@ -73,3 +73,7 @@ var fromCallsToExecuteCalldata = function (calls) {
73
73
  ], false), __read(calldata), false);
74
74
  };
75
75
  exports.fromCallsToExecuteCalldata = fromCallsToExecuteCalldata;
76
+ var fromCallsToExecuteCalldataWithNonce = function (calls, nonce) {
77
+ return __spreadArray(__spreadArray([], __read((0, exports.fromCallsToExecuteCalldata)(calls)), false), [(0, number_1.toBN)(nonce).toString()], false);
78
+ };
79
+ exports.fromCallsToExecuteCalldataWithNonce = fromCallsToExecuteCalldataWithNonce;
@@ -47,9 +47,9 @@ export declare const encodeData: <T extends {
47
47
  }[]>;
48
48
  primaryType: string;
49
49
  domain: {
50
- name?: string | undefined;
51
50
  version?: string | undefined;
52
51
  chainId?: string | number | undefined;
52
+ name?: string | undefined;
53
53
  };
54
54
  message: Record<string, unknown>;
55
55
  }>(typedData: T, type: string, data: T["message"]) => string[][];
@@ -74,9 +74,9 @@ export declare const getStructHash: <T extends {
74
74
  }[]>;
75
75
  primaryType: string;
76
76
  domain: {
77
- name?: string | undefined;
78
77
  version?: string | undefined;
79
78
  chainId?: string | number | undefined;
79
+ name?: string | undefined;
80
80
  };
81
81
  message: Record<string, unknown>;
82
82
  }>(typedData: T, type: string, data: T["message"]) => string;
@@ -25,9 +25,9 @@ export declare const STARKNET_TYPE: import("superstruct").Struct<{
25
25
  */
26
26
  export declare type StarkNetType = Infer<typeof STARKNET_TYPE>;
27
27
  export declare const STARKNET_DOMAIN_TYPE: import("superstruct").Struct<{
28
- name?: string | undefined;
29
28
  version?: string | undefined;
30
29
  chainId?: string | number | undefined;
30
+ name?: string | undefined;
31
31
  }, {
32
32
  name: import("superstruct").Struct<string | undefined, null>;
33
33
  version: import("superstruct").Struct<string | undefined, null>;
@@ -49,9 +49,9 @@ export declare const STARKNET_TYPED_DATA_TYPE: import("superstruct").Struct<{
49
49
  }[]>;
50
50
  primaryType: string;
51
51
  domain: {
52
- name?: string | undefined;
53
52
  version?: string | undefined;
54
53
  chainId?: string | number | undefined;
54
+ name?: string | undefined;
55
55
  };
56
56
  message: Record<string, unknown>;
57
57
  }, {
@@ -66,9 +66,9 @@ export declare const STARKNET_TYPED_DATA_TYPE: import("superstruct").Struct<{
66
66
  }[]>, null>;
67
67
  primaryType: import("superstruct").Struct<string, null>;
68
68
  domain: import("superstruct").Struct<{
69
- name?: string | undefined;
70
69
  version?: string | undefined;
71
70
  chainId?: string | number | undefined;
71
+ name?: string | undefined;
72
72
  }, {
73
73
  name: import("superstruct").Struct<string | undefined, null>;
74
74
  version: import("superstruct").Struct<string | undefined, null>;
@@ -16,9 +16,9 @@ export declare const validateTypedData: (data: unknown) => data is {
16
16
  }[]>;
17
17
  primaryType: string;
18
18
  domain: {
19
- name?: string | undefined;
20
19
  version?: string | undefined;
21
20
  chainId?: string | number | undefined;
21
+ name?: string | undefined;
22
22
  };
23
23
  message: Record<string, unknown>;
24
24
  };
package/index.d.ts CHANGED
@@ -14,6 +14,7 @@ export * as encode from './utils/encode';
14
14
  export * as hash from './utils/hash';
15
15
  export * as json from './utils/json';
16
16
  export * as number from './utils/number';
17
+ export * as transaction from './utils/transaction';
17
18
  export * as stark from './utils/stark';
18
19
  export * as ec from './utils/ellipticCurve';
19
20
  export * as uint256 from './utils/uint256';
package/index.js CHANGED
@@ -49,6 +49,7 @@ exports.typedData =
49
49
  exports.uint256 =
50
50
  exports.ec =
51
51
  exports.stark =
52
+ exports.transaction =
52
53
  exports.number =
53
54
  exports.json =
54
55
  exports.hash =
@@ -71,6 +72,7 @@ exports.encode = __importStar(require('./utils/encode'));
71
72
  exports.hash = __importStar(require('./utils/hash'));
72
73
  exports.json = __importStar(require('./utils/json'));
73
74
  exports.number = __importStar(require('./utils/number'));
75
+ exports.transaction = __importStar(require('./utils/transaction'));
74
76
  exports.stark = __importStar(require('./utils/stark'));
75
77
  exports.ec = __importStar(require('./utils/ellipticCurve'));
76
78
  exports.uint256 = __importStar(require('./utils/uint256'));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "starknet",
3
- "version": "3.8.0",
3
+ "version": "3.10.1",
4
4
  "description": "JavaScript library for StarkNet",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -1,3 +1,4 @@
1
+ import { StarknetChainId } from '../constants';
1
2
  import {
2
3
  Abi,
3
4
  AddTransactionResponse,
@@ -29,10 +30,12 @@ export declare class Provider implements ProviderInterface {
29
30
  baseUrl: string;
30
31
  feederGatewayUrl: string;
31
32
  gatewayUrl: string;
32
- constructor(optionsOrProvider?: ProviderOptions | Provider);
33
+ chainId: StarknetChainId;
34
+ constructor(optionsOrProvider?: ProviderOptions | ProviderInterface);
33
35
  protected static getNetworkFromName(
34
36
  name: NetworkName
35
37
  ): 'https://alpha-mainnet.starknet.io' | 'https://alpha4.starknet.io';
38
+ protected static getChainIdFromBaseUrl(baseUrl: string): StarknetChainId;
36
39
  private getFetchUrl;
37
40
  private getFetchMethod;
38
41
  private getQueryString;
@@ -175,10 +175,12 @@ Object.defineProperty(exports, '__esModule', { value: true });
175
175
  exports.Provider = void 0;
176
176
  var axios_1 = __importDefault(require('axios'));
177
177
  var url_join_1 = __importDefault(require('url-join'));
178
+ var constants_1 = require('../constants');
178
179
  var hash_1 = require('../utils/hash');
179
180
  var json_1 = require('../utils/json');
180
181
  var number_1 = require('../utils/number');
181
182
  var stark_1 = require('../utils/stark');
183
+ var interface_1 = require('./interface');
182
184
  var utils_1 = require('./utils');
183
185
  function wait(delay) {
184
186
  return new Promise(function (res) {
@@ -203,16 +205,22 @@ var Provider = /** @class */ (function () {
203
205
  if (optionsOrProvider === void 0) {
204
206
  optionsOrProvider = { network: 'goerli-alpha' };
205
207
  }
206
- if (optionsOrProvider instanceof Provider) {
208
+ var _a;
209
+ if (optionsOrProvider instanceof interface_1.ProviderInterface) {
207
210
  this.baseUrl = optionsOrProvider.baseUrl;
208
211
  this.feederGatewayUrl = optionsOrProvider.feederGatewayUrl;
209
212
  this.gatewayUrl = optionsOrProvider.gatewayUrl;
213
+ this.chainId =
214
+ (_a = optionsOrProvider.chainId) !== null && _a !== void 0
215
+ ? _a
216
+ : Provider.getChainIdFromBaseUrl(optionsOrProvider.baseUrl);
210
217
  } else {
211
218
  var baseUrl =
212
219
  'baseUrl' in optionsOrProvider
213
220
  ? optionsOrProvider.baseUrl
214
221
  : Provider.getNetworkFromName(optionsOrProvider.network);
215
222
  this.baseUrl = baseUrl;
223
+ this.chainId = Provider.getChainIdFromBaseUrl(baseUrl);
216
224
  this.feederGatewayUrl = (0, url_join_1.default)(baseUrl, 'feeder_gateway');
217
225
  this.gatewayUrl = (0, url_join_1.default)(baseUrl, 'gateway');
218
226
  }
@@ -226,6 +234,18 @@ var Provider = /** @class */ (function () {
226
234
  return 'https://alpha4.starknet.io';
227
235
  }
228
236
  };
237
+ Provider.getChainIdFromBaseUrl = function (baseUrl) {
238
+ try {
239
+ var url = new URL(baseUrl);
240
+ if (url.host.includes('mainnet.starknet.io')) {
241
+ return constants_1.StarknetChainId.MAINNET;
242
+ }
243
+ } catch (_a) {
244
+ // eslint-disable-next-line no-console
245
+ console.error('Could not parse baseUrl: ' + baseUrl);
246
+ }
247
+ return constants_1.StarknetChainId.TESTNET;
248
+ };
229
249
  Provider.prototype.getFetchUrl = function (endpoint) {
230
250
  var gatewayUrlEndpoints = ['add_transaction'];
231
251
  return gatewayUrlEndpoints.includes(endpoint) ? this.gatewayUrl : this.feederGatewayUrl;
@@ -1,3 +1,4 @@
1
+ import { StarknetChainId } from '../constants';
1
2
  import type {
2
3
  AddTransactionResponse,
3
4
  Call,
@@ -17,6 +18,7 @@ export declare abstract class ProviderInterface {
17
18
  abstract baseUrl: string;
18
19
  abstract feederGatewayUrl: string;
19
20
  abstract gatewayUrl: string;
21
+ abstract chainId: StarknetChainId;
20
22
  /**
21
23
  * Gets the smart contract address on the goerli testnet.
22
24
  *
package/signer/default.js CHANGED
@@ -134,6 +134,7 @@ Object.defineProperty(exports, '__esModule', { value: true });
134
134
  exports.Signer = void 0;
135
135
  var ellipticCurve_1 = require('../utils/ellipticCurve');
136
136
  var hash_1 = require('../utils/hash');
137
+ var transaction_1 = require('../utils/transaction');
137
138
  var typedData_1 = require('../utils/typedData');
138
139
  var Signer = /** @class */ (function () {
139
140
  function Signer(keyPair) {
@@ -148,16 +149,22 @@ var Signer = /** @class */ (function () {
148
149
  };
149
150
  Signer.prototype.signTransaction = function (transactions, transactionsDetail, abis) {
150
151
  return __awaiter(this, void 0, void 0, function () {
151
- var msgHash;
152
+ var calldata, msgHash;
152
153
  return __generator(this, function (_a) {
153
154
  if (abis && abis.length !== transactions.length) {
154
155
  throw new Error('ABI must be provided for each transaction or no transaction');
155
156
  }
156
- msgHash = (0, hash_1.hashMulticall)(
157
- transactionsDetail.walletAddress,
157
+ calldata = (0, transaction_1.fromCallsToExecuteCalldataWithNonce)(
158
158
  transactions,
159
- transactionsDetail.nonce.toString(),
160
- transactionsDetail.maxFee.toString()
159
+ transactionsDetail.nonce
160
+ );
161
+ msgHash = (0, hash_1.calculcateTransactionHash)(
162
+ transactionsDetail.walletAddress,
163
+ transactionsDetail.version,
164
+ (0, hash_1.getSelectorFromName)('__execute__'),
165
+ calldata,
166
+ transactionsDetail.maxFee,
167
+ transactionsDetail.chainId
161
168
  );
162
169
  return [2 /*return*/, (0, ellipticCurve_1.sign)(this.keyPair, msgHash)];
163
170
  });