starknet 3.18.2 → 4.0.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 (165) hide show
  1. package/CHANGELOG.md +70 -0
  2. package/README.md +1 -2
  3. package/__tests__/account.test.ts +11 -56
  4. package/__tests__/contract.test.ts +11 -49
  5. package/__tests__/defaultProvider.test.ts +321 -0
  6. package/__tests__/fixtures.ts +32 -11
  7. package/__tests__/jest.setup.ts +2 -3
  8. package/__tests__/rpcProvider.test.ts +17 -0
  9. package/__tests__/sequencerProvider.test.ts +45 -0
  10. package/account/default.d.ts +54 -77
  11. package/account/default.js +271 -596
  12. package/account/index.js +18 -31
  13. package/account/interface.d.ts +66 -95
  14. package/account/interface.js +20 -30
  15. package/constants.d.ts +17 -19
  16. package/constants.js +2038 -2059
  17. package/contract/contractFactory.d.ts +25 -29
  18. package/contract/contractFactory.js +94 -210
  19. package/contract/default.d.ts +117 -146
  20. package/contract/default.js +582 -776
  21. package/contract/index.js +19 -32
  22. package/contract/interface.d.ts +72 -92
  23. package/contract/interface.js +6 -5
  24. package/dist/account/default.d.ts +5 -9
  25. package/dist/account/default.js +35 -169
  26. package/dist/account/interface.d.ts +3 -15
  27. package/dist/contract/contractFactory.js +4 -4
  28. package/dist/contract/default.d.ts +3 -3
  29. package/dist/contract/default.js +3 -2
  30. package/dist/contract/interface.d.ts +2 -2
  31. package/dist/provider/default.d.ts +18 -134
  32. package/dist/provider/default.js +47 -411
  33. package/dist/provider/index.d.ts +2 -0
  34. package/dist/provider/index.js +2 -0
  35. package/dist/provider/interface.d.ts +45 -50
  36. package/dist/provider/rpc.d.ts +57 -0
  37. package/dist/provider/rpc.js +364 -0
  38. package/dist/provider/sequencer.d.ts +66 -0
  39. package/dist/provider/sequencer.js +444 -0
  40. package/dist/types/account.d.ts +2 -3
  41. package/dist/types/api/index.d.ts +16 -0
  42. package/dist/types/api/index.js +18 -0
  43. package/dist/types/api/rpc.d.ts +221 -0
  44. package/dist/types/{api.js → api/rpc.js} +0 -0
  45. package/dist/types/api/sequencer.d.ts +289 -0
  46. package/dist/types/api/sequencer.js +2 -0
  47. package/dist/types/index.d.ts +3 -1
  48. package/dist/types/index.js +15 -1
  49. package/dist/types/lib.d.ts +3 -1
  50. package/dist/types/provider.d.ts +86 -0
  51. package/dist/types/provider.js +2 -0
  52. package/dist/utils/fetchPonyfill.d.ts +2 -0
  53. package/dist/utils/fetchPonyfill.js +6 -0
  54. package/dist/utils/provider.d.ts +4 -0
  55. package/dist/utils/provider.js +38 -0
  56. package/dist/utils/responseParser/index.d.ts +11 -0
  57. package/dist/utils/responseParser/index.js +9 -0
  58. package/dist/utils/responseParser/rpc.d.ts +13 -0
  59. package/dist/utils/responseParser/rpc.js +96 -0
  60. package/dist/utils/responseParser/sequencer.d.ts +13 -0
  61. package/dist/utils/responseParser/sequencer.js +124 -0
  62. package/index.js +42 -75
  63. package/package.json +2 -3
  64. package/provider/default.d.ts +21 -175
  65. package/provider/default.js +139 -704
  66. package/provider/errors.d.ts +4 -4
  67. package/provider/errors.js +30 -40
  68. package/provider/index.d.ts +2 -0
  69. package/provider/index.js +22 -33
  70. package/provider/interface.d.ts +104 -131
  71. package/provider/interface.js +6 -5
  72. package/provider/rpc.d.ts +57 -0
  73. package/provider/rpc.js +364 -0
  74. package/provider/sequencer.d.ts +66 -0
  75. package/provider/sequencer.js +444 -0
  76. package/provider/utils.d.ts +7 -9
  77. package/provider/utils.js +39 -44
  78. package/signer/default.d.ts +5 -9
  79. package/signer/default.js +72 -177
  80. package/signer/index.js +18 -31
  81. package/signer/interface.d.ts +29 -33
  82. package/signer/interface.js +6 -5
  83. package/src/account/default.ts +26 -146
  84. package/src/account/interface.ts +5 -20
  85. package/src/contract/contractFactory.ts +3 -6
  86. package/src/contract/default.ts +6 -4
  87. package/src/contract/interface.ts +2 -2
  88. package/src/provider/default.ts +63 -395
  89. package/src/provider/index.ts +2 -0
  90. package/src/provider/interface.ts +68 -63
  91. package/src/provider/rpc.ts +299 -0
  92. package/src/provider/sequencer.ts +385 -0
  93. package/src/types/account.ts +2 -3
  94. package/src/types/api/index.ts +17 -0
  95. package/src/types/api/rpc.ts +247 -0
  96. package/src/types/api/sequencer.ts +331 -0
  97. package/src/types/index.ts +3 -1
  98. package/src/types/lib.ts +3 -1
  99. package/src/types/provider.ts +108 -0
  100. package/src/utils/fetchPonyfill.ts +4 -0
  101. package/src/utils/provider.ts +28 -0
  102. package/src/utils/responseParser/index.ts +28 -0
  103. package/src/utils/responseParser/rpc.ts +93 -0
  104. package/src/utils/responseParser/sequencer.ts +127 -0
  105. package/types/account.d.ts +5 -7
  106. package/types/account.js +2 -2
  107. package/types/api/index.d.ts +16 -0
  108. package/types/api/index.js +18 -0
  109. package/types/api/rpc.d.ts +221 -0
  110. package/types/api/rpc.js +2 -0
  111. package/types/api/sequencer.d.ts +289 -0
  112. package/types/api/sequencer.js +2 -0
  113. package/types/contract.d.ts +1 -1
  114. package/types/contract.js +2 -2
  115. package/types/index.d.ts +3 -1
  116. package/types/index.js +35 -34
  117. package/types/lib.d.ts +36 -41
  118. package/types/lib.js +2 -2
  119. package/types/provider.d.ts +86 -0
  120. package/types/provider.js +2 -0
  121. package/types/signer.d.ts +2 -2
  122. package/types/signer.js +2 -2
  123. package/utils/address.js +26 -37
  124. package/utils/ellipticCurve.d.ts +1 -6
  125. package/utils/ellipticCurve.js +73 -137
  126. package/utils/encode.js +49 -85
  127. package/utils/fetchPonyfill.d.ts +2 -0
  128. package/utils/fetchPonyfill.js +6 -0
  129. package/utils/hash.d.ts +4 -31
  130. package/utils/hash.js +76 -141
  131. package/utils/json.d.ts +13 -45
  132. package/utils/json.js +15 -22
  133. package/utils/number.d.ts +2 -9
  134. package/utils/number.js +47 -81
  135. package/utils/provider.d.ts +4 -0
  136. package/utils/provider.js +38 -0
  137. package/utils/responseParser/index.d.ts +11 -0
  138. package/utils/responseParser/index.js +9 -0
  139. package/utils/responseParser/rpc.d.ts +13 -0
  140. package/utils/responseParser/rpc.js +96 -0
  141. package/utils/responseParser/sequencer.d.ts +13 -0
  142. package/utils/responseParser/sequencer.js +124 -0
  143. package/utils/shortString.js +13 -21
  144. package/utils/stark.d.ts +0 -1
  145. package/utils/stark.js +59 -93
  146. package/utils/transaction.d.ts +3 -6
  147. package/utils/transaction.js +50 -81
  148. package/utils/typedData/index.d.ts +3 -15
  149. package/utils/typedData/index.js +109 -175
  150. package/utils/typedData/types.d.ts +9 -9
  151. package/utils/typedData/types.js +2 -2
  152. package/utils/typedData/utils.js +6 -6
  153. package/utils/uint256.d.ts +5 -5
  154. package/utils/uint256.js +16 -26
  155. package/www/docs/API/account.md +3 -4
  156. package/www/docs/API/contract.md +2 -2
  157. package/www/docs/API/contractFactory.md +2 -2
  158. package/www/docs/API/provider.md +185 -74
  159. package/www/guides/account.md +1 -8
  160. package/www/guides/erc20.md +3 -0
  161. package/__tests__/provider.test.ts +0 -168
  162. package/dist/types/api.d.ts +0 -261
  163. package/src/types/api.ts +0 -303
  164. package/types/api.d.ts +0 -287
  165. package/types/api.js +0 -2
package/signer/default.js CHANGED
@@ -1,184 +1,79 @@
1
- 'use strict';
2
- var __awaiter =
3
- (this && this.__awaiter) ||
4
- function (thisArg, _arguments, P, generator) {
5
- function adopt(value) {
6
- return value instanceof P
7
- ? value
8
- : new P(function (resolve) {
9
- resolve(value);
10
- });
11
- }
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
12
4
  return new (P || (P = Promise))(function (resolve, reject) {
13
- function fulfilled(value) {
14
- try {
15
- step(generator.next(value));
16
- } catch (e) {
17
- reject(e);
18
- }
19
- }
20
- function rejected(value) {
21
- try {
22
- step(generator['throw'](value));
23
- } catch (e) {
24
- reject(e);
25
- }
26
- }
27
- function step(result) {
28
- result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
29
- }
30
- step((generator = generator.apply(thisArg, _arguments || [])).next());
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
31
9
  });
32
- };
33
- var __generator =
34
- (this && this.__generator) ||
35
- function (thisArg, body) {
36
- var _ = {
37
- label: 0,
38
- sent: function () {
39
- if (t[0] & 1) throw t[1];
40
- return t[1];
41
- },
42
- trys: [],
43
- ops: [],
44
- },
45
- f,
46
- y,
47
- t,
48
- g;
49
- return (
50
- (g = { next: verb(0), throw: verb(1), return: verb(2) }),
51
- typeof Symbol === 'function' &&
52
- (g[Symbol.iterator] = function () {
53
- return this;
54
- }),
55
- g
56
- );
57
- function verb(n) {
58
- return function (v) {
59
- return step([n, v]);
60
- };
61
- }
10
+ };
11
+ var __generator = (this && this.__generator) || function (thisArg, body) {
12
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
+ function verb(n) { return function (v) { return step([n, v]); }; }
62
15
  function step(op) {
63
- if (f) throw new TypeError('Generator is already executing.');
64
- while (_)
65
- try {
66
- if (
67
- ((f = 1),
68
- y &&
69
- (t =
70
- op[0] & 2
71
- ? y['return']
72
- : op[0]
73
- ? y['throw'] || ((t = y['return']) && t.call(y), 0)
74
- : y.next) &&
75
- !(t = t.call(y, op[1])).done)
76
- )
77
- return t;
78
- if (((y = 0), t)) op = [op[0] & 2, t.value];
79
- switch (op[0]) {
80
- case 0:
81
- case 1:
82
- t = op;
83
- break;
84
- case 4:
85
- _.label++;
86
- return { value: op[1], done: false };
87
- case 5:
88
- _.label++;
89
- y = op[1];
90
- op = [0];
91
- continue;
92
- case 7:
93
- op = _.ops.pop();
94
- _.trys.pop();
95
- continue;
96
- default:
97
- if (
98
- !((t = _.trys), (t = t.length > 0 && t[t.length - 1])) &&
99
- (op[0] === 6 || op[0] === 2)
100
- ) {
101
- _ = 0;
102
- continue;
103
- }
104
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) {
105
- _.label = op[1];
106
- break;
107
- }
108
- if (op[0] === 6 && _.label < t[1]) {
109
- _.label = t[1];
110
- t = op;
111
- break;
112
- }
113
- if (t && _.label < t[2]) {
114
- _.label = t[2];
115
- _.ops.push(op);
116
- break;
117
- }
118
- if (t[2]) _.ops.pop();
119
- _.trys.pop();
120
- continue;
121
- }
122
- op = body.call(thisArg, _);
123
- } catch (e) {
124
- op = [6, e];
125
- y = 0;
126
- } finally {
127
- f = t = 0;
128
- }
129
- if (op[0] & 5) throw op[1];
130
- return { value: op[0] ? op[1] : void 0, done: true };
16
+ if (f) throw new TypeError("Generator is already executing.");
17
+ while (_) try {
18
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
+ if (y = 0, t) op = [op[0] & 2, t.value];
20
+ switch (op[0]) {
21
+ case 0: case 1: t = op; break;
22
+ case 4: _.label++; return { value: op[1], done: false };
23
+ case 5: _.label++; y = op[1]; op = [0]; continue;
24
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
+ default:
26
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
+ if (t[2]) _.ops.pop();
31
+ _.trys.pop(); continue;
32
+ }
33
+ op = body.call(thisArg, _);
34
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
131
36
  }
132
- };
133
- Object.defineProperty(exports, '__esModule', { value: true });
37
+ };
38
+ Object.defineProperty(exports, "__esModule", { value: true });
134
39
  exports.Signer = void 0;
135
- var ellipticCurve_1 = require('../utils/ellipticCurve');
136
- var hash_1 = require('../utils/hash');
137
- var transaction_1 = require('../utils/transaction');
138
- var typedData_1 = require('../utils/typedData');
40
+ var ellipticCurve_1 = require("../utils/ellipticCurve");
41
+ var hash_1 = require("../utils/hash");
42
+ var transaction_1 = require("../utils/transaction");
43
+ var typedData_1 = require("../utils/typedData");
139
44
  var Signer = /** @class */ (function () {
140
- function Signer(keyPair) {
141
- this.keyPair = keyPair;
142
- }
143
- Signer.prototype.getPubKey = function () {
144
- return __awaiter(this, void 0, void 0, function () {
145
- return __generator(this, function (_a) {
146
- return [2 /*return*/, (0, ellipticCurve_1.getStarkKey)(this.keyPair)];
147
- });
148
- });
149
- };
150
- Signer.prototype.signTransaction = function (transactions, transactionsDetail, abis) {
151
- return __awaiter(this, void 0, void 0, function () {
152
- var calldata, msgHash;
153
- return __generator(this, function (_a) {
154
- if (abis && abis.length !== transactions.length) {
155
- throw new Error('ABI must be provided for each transaction or no transaction');
156
- }
157
- calldata = (0, transaction_1.fromCallsToExecuteCalldataWithNonce)(
158
- transactions,
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
168
- );
169
- return [2 /*return*/, (0, ellipticCurve_1.sign)(this.keyPair, msgHash)];
170
- });
171
- });
172
- };
173
- Signer.prototype.signMessage = function (typedData, accountAddress) {
174
- return __awaiter(this, void 0, void 0, function () {
175
- var msgHash;
176
- return __generator(this, function (_a) {
177
- msgHash = (0, typedData_1.getMessageHash)(typedData, accountAddress);
178
- return [2 /*return*/, (0, ellipticCurve_1.sign)(this.keyPair, msgHash)];
179
- });
180
- });
181
- };
182
- return Signer;
183
- })();
45
+ function Signer(keyPair) {
46
+ this.keyPair = keyPair;
47
+ }
48
+ Signer.prototype.getPubKey = function () {
49
+ return __awaiter(this, void 0, void 0, function () {
50
+ return __generator(this, function (_a) {
51
+ return [2 /*return*/, (0, ellipticCurve_1.getStarkKey)(this.keyPair)];
52
+ });
53
+ });
54
+ };
55
+ Signer.prototype.signTransaction = function (transactions, transactionsDetail, abis) {
56
+ return __awaiter(this, void 0, void 0, function () {
57
+ var calldata, msgHash;
58
+ return __generator(this, function (_a) {
59
+ if (abis && abis.length !== transactions.length) {
60
+ throw new Error('ABI must be provided for each transaction or no transaction');
61
+ }
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);
64
+ return [2 /*return*/, (0, ellipticCurve_1.sign)(this.keyPair, msgHash)];
65
+ });
66
+ });
67
+ };
68
+ Signer.prototype.signMessage = function (typedData, accountAddress) {
69
+ return __awaiter(this, void 0, void 0, function () {
70
+ var msgHash;
71
+ return __generator(this, function (_a) {
72
+ msgHash = (0, typedData_1.getMessageHash)(typedData, accountAddress);
73
+ return [2 /*return*/, (0, ellipticCurve_1.sign)(this.keyPair, msgHash)];
74
+ });
75
+ });
76
+ };
77
+ return Signer;
78
+ }());
184
79
  exports.Signer = Signer;
package/signer/index.js CHANGED
@@ -1,31 +1,18 @@
1
- 'use strict';
2
- var __createBinding =
3
- (this && this.__createBinding) ||
4
- (Object.create
5
- ? function (o, m, k, k2) {
6
- if (k2 === undefined) k2 = k;
7
- var desc = Object.getOwnPropertyDescriptor(m, k);
8
- if (!desc || ('get' in desc ? !m.__esModule : desc.writable || desc.configurable)) {
9
- desc = {
10
- enumerable: true,
11
- get: function () {
12
- return m[k];
13
- },
14
- };
15
- }
16
- Object.defineProperty(o, k2, desc);
17
- }
18
- : function (o, m, k, k2) {
19
- if (k2 === undefined) k2 = k;
20
- o[k2] = m[k];
21
- });
22
- var __exportStar =
23
- (this && this.__exportStar) ||
24
- function (m, exports) {
25
- for (var p in m)
26
- if (p !== 'default' && !Object.prototype.hasOwnProperty.call(exports, p))
27
- __createBinding(exports, m, p);
28
- };
29
- Object.defineProperty(exports, '__esModule', { value: true });
30
- __exportStar(require('./interface'), exports);
31
- __exportStar(require('./default'), exports);
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./interface"), exports);
18
+ __exportStar(require("./default"), exports);
@@ -1,37 +1,33 @@
1
1
  import { Abi, Invocation, InvocationsSignerDetails, Signature } from '../types';
2
2
  import { TypedData } from '../utils/typedData';
3
3
  export declare abstract class SignerInterface {
4
- /**
5
- * Method to get the public key of the signer
6
- *
7
- * @returns public key of signer as hex string with 0x prefix
8
- */
9
- abstract getPubKey(): Promise<string>;
10
- /**
11
- * Sign an JSON object for off-chain usage with the starknet private key and return the signature
12
- * This adds a message prefix so it cant be interchanged with transactions
13
- *
14
- * @param typedData - JSON object to be signed
15
- * @param accountAddress - account
16
- * @returns the signature of the JSON object
17
- * @throws {Error} if the JSON object is not a valid JSON
18
- */
19
- abstract signMessage(typedData: TypedData, accountAddress: string): Promise<Signature>;
20
- /**
21
- * Signs a transaction with the starknet private key and returns the signature
22
- *
23
- * @param invocation the invocation object containing:
24
- * - contractAddress - the address of the contract
25
- * - entrypoint - the entrypoint of the contract
26
- * - calldata - (defaults to []) the calldata
27
- * - signature - (defaults to []) the signature
28
- * @param abi (optional) the abi of the contract for better displaying
29
- *
30
- * @returns signature
31
- */
32
- abstract signTransaction(
33
- transactions: Invocation[],
34
- transactionsDetail: InvocationsSignerDetails,
35
- abis?: Abi[]
36
- ): Promise<Signature>;
4
+ /**
5
+ * Method to get the public key of the signer
6
+ *
7
+ * @returns public key of signer as hex string with 0x prefix
8
+ */
9
+ abstract getPubKey(): Promise<string>;
10
+ /**
11
+ * Sign an JSON object for off-chain usage with the starknet private key and return the signature
12
+ * This adds a message prefix so it cant be interchanged with transactions
13
+ *
14
+ * @param typedData - JSON object to be signed
15
+ * @param accountAddress - account
16
+ * @returns the signature of the JSON object
17
+ * @throws {Error} if the JSON object is not a valid JSON
18
+ */
19
+ abstract signMessage(typedData: TypedData, accountAddress: string): Promise<Signature>;
20
+ /**
21
+ * Signs a transaction with the starknet private key and returns the signature
22
+ *
23
+ * @param invocation the invocation object containing:
24
+ * - contractAddress - the address of the contract
25
+ * - entrypoint - the entrypoint of the contract
26
+ * - calldata - (defaults to []) the calldata
27
+ * - signature - (defaults to []) the signature
28
+ * @param abi (optional) the abi of the contract for better displaying
29
+ *
30
+ * @returns signature
31
+ */
32
+ abstract signTransaction(transactions: Invocation[], transactionsDetail: InvocationsSignerDetails, abis?: Abi[]): Promise<Signature>;
37
33
  }
@@ -1,8 +1,9 @@
1
- 'use strict';
2
- Object.defineProperty(exports, '__esModule', { value: true });
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.SignerInterface = void 0;
4
4
  var SignerInterface = /** @class */ (function () {
5
- function SignerInterface() {}
6
- return SignerInterface;
7
- })();
5
+ function SignerInterface() {
6
+ }
7
+ return SignerInterface;
8
+ }());
8
9
  exports.SignerInterface = SignerInterface;
@@ -1,48 +1,35 @@
1
- import assert from 'minimalistic-assert';
2
-
3
1
  import { ZERO } from '../constants';
4
- import { Provider, ProviderInterface } from '../provider';
2
+ import { ProviderInterface, ProviderOptions } from '../provider';
3
+ import { Provider } from '../provider/default';
5
4
  import { Signer, SignerInterface } from '../signer';
6
5
  import {
7
6
  Abi,
8
- AddTransactionResponse,
9
7
  Call,
10
8
  InvocationsDetails,
11
9
  InvocationsSignerDetails,
12
- InvokeFunctionTransaction,
10
+ InvokeFunctionResponse,
13
11
  KeyPair,
14
12
  Signature,
15
- Transaction,
16
13
  } from '../types';
17
14
  import { EstimateFee, EstimateFeeDetails } from '../types/account';
18
- import { sign } from '../utils/ellipticCurve';
19
- import {
20
- computeHashOnElements,
21
- feeTransactionVersion,
22
- getSelectorFromName,
23
- transactionVersion,
24
- } from '../utils/hash';
25
- import { BigNumberish, bigNumberishArrayToDecimalStringArray, toBN, toHex } from '../utils/number';
26
- import { encodeShortString } from '../utils/shortString';
15
+ import { feeTransactionVersion, transactionVersion } from '../utils/hash';
16
+ import { BigNumberish, toBN, toHex } from '../utils/number';
27
17
  import { compileCalldata, estimatedFeeToMaxFee } from '../utils/stark';
28
18
  import { fromCallsToExecuteCalldataWithNonce } from '../utils/transaction';
29
19
  import { TypedData, getMessageHash } from '../utils/typedData';
30
20
  import { AccountInterface } from './interface';
31
21
 
32
22
  export class Account extends Provider implements AccountInterface {
33
- public address: string;
34
-
35
23
  public signer: SignerInterface;
36
24
 
37
25
  constructor(
38
- provider: ProviderInterface,
39
- address: string,
26
+ providerOrOptions: ProviderOptions | ProviderInterface,
27
+ public address: string,
40
28
  keyPairOrSigner: KeyPair | SignerInterface
41
29
  ) {
42
- super(provider);
30
+ super(providerOrOptions);
43
31
  this.signer =
44
32
  'getPubKey' in keyPairOrSigner ? keyPairOrSigner : new Signer(keyPairOrSigner as KeyPair);
45
- this.address = address;
46
33
  }
47
34
 
48
35
  public async getNonce(): Promise<string> {
@@ -55,7 +42,7 @@ export class Account extends Provider implements AccountInterface {
55
42
 
56
43
  public async estimateFee(
57
44
  calls: Call | Call[],
58
- { nonce: providedNonce, blockIdentifier = 'pending' }: EstimateFeeDetails = {}
45
+ { nonce: providedNonce, blockIdentifier }: EstimateFeeDetails = {}
59
46
  ): Promise<EstimateFee> {
60
47
  const transactions = Array.isArray(calls) ? calls : [calls];
61
48
  const nonce = providedNonce ?? (await this.getNonce());
@@ -72,26 +59,16 @@ export class Account extends Provider implements AccountInterface {
72
59
  const signature = await this.signer.signTransaction(transactions, signerDetails);
73
60
 
74
61
  const calldata = fromCallsToExecuteCalldataWithNonce(transactions, nonce);
75
- const fetchedEstimate = await this.fetchEndpoint(
76
- 'estimate_fee',
77
- { blockIdentifier },
78
- {
79
- contract_address: this.address,
80
- entry_point_selector: getSelectorFromName('__execute__'),
81
- calldata,
82
- version: toHex(version),
83
- signature: bigNumberishArrayToDecimalStringArray(signature),
84
- }
62
+ const response = await super.getEstimateFee(
63
+ { contractAddress: this.address, entrypoint: '__execute__', calldata, signature },
64
+ blockIdentifier,
65
+ { version }
85
66
  );
86
- const fee = fetchedEstimate.overall_fee ?? fetchedEstimate.amount;
87
- if (fee === undefined) {
88
- throw new Error('Expected either amount or overall_fee in estimate_fee response');
89
- }
90
- const suggestedMaxFee = estimatedFeeToMaxFee(fee);
67
+
68
+ const suggestedMaxFee = estimatedFeeToMaxFee(response.overall_fee);
91
69
 
92
70
  return {
93
- amount: fee,
94
- unit: fetchedEstimate.unit,
71
+ ...response,
95
72
  suggestedMaxFee,
96
73
  };
97
74
  }
@@ -110,7 +87,7 @@ export class Account extends Provider implements AccountInterface {
110
87
  calls: Call | Call[],
111
88
  abis: Abi[] | undefined = undefined,
112
89
  transactionsDetail: InvocationsDetails = {}
113
- ): Promise<AddTransactionResponse> {
90
+ ): Promise<InvokeFunctionResponse> {
114
91
  const transactions = Array.isArray(calls) ? calls : [calls];
115
92
  const nonce = toBN(transactionsDetail.nonce ?? (await this.getNonce()));
116
93
  let maxFee: BigNumberish = '0';
@@ -121,124 +98,27 @@ export class Account extends Provider implements AccountInterface {
121
98
  maxFee = suggestedMaxFee.toString();
122
99
  }
123
100
 
101
+ const version = toBN(transactionVersion);
102
+
124
103
  const signerDetails: InvocationsSignerDetails = {
125
104
  walletAddress: this.address,
126
105
  nonce,
127
106
  maxFee,
128
- version: toBN(transactionVersion),
107
+ version,
129
108
  chainId: this.chainId,
130
109
  };
131
110
 
132
111
  const signature = await this.signer.signTransaction(transactions, signerDetails, abis);
133
112
 
134
113
  const calldata = fromCallsToExecuteCalldataWithNonce(transactions, nonce);
135
- return this.fetchEndpoint('add_transaction', undefined, {
136
- type: 'INVOKE_FUNCTION',
137
- contract_address: this.address,
138
- entry_point_selector: getSelectorFromName('__execute__'),
139
- calldata,
140
- signature: bigNumberishArrayToDecimalStringArray(signature),
141
- max_fee: toHex(toBN(maxFee)),
142
- });
143
- }
144
-
145
- /**
146
- * Temporary method to allow dapps on starknet.js v2 to work with Argent X v3
147
- * @deprecated to remove ASAP
148
- */
149
- public async LEGACY_addTransaction(transaction: Transaction): Promise<AddTransactionResponse> {
150
- if (transaction.type === 'DEPLOY') throw new Error('No DEPLOYS');
151
- if (transaction.type === 'DECLARE') throw new Error('No DECLARES');
152
114
 
153
- assert(
154
- !transaction.signature,
155
- "Adding signatures to a signer transaction currently isn't supported"
156
- );
157
-
158
- let nonceBn;
159
- if (transaction.nonce) {
160
- nonceBn = toBN(transaction.nonce);
161
- } else {
162
- const { result } = await this.callContract({
163
- contractAddress: this.address,
164
- entrypoint: 'get_nonce',
165
- });
166
- nonceBn = toBN(result[0]);
167
- }
168
-
169
- function hashMulticall(
170
- account: string,
171
- transactions: InvokeFunctionTransaction[],
172
- nonce: string,
173
- maxFee: string
174
- ) {
175
- const hashArray = transactions
176
- .map(({ contract_address, entry_point_selector, calldata }) => [
177
- contract_address,
178
- entry_point_selector,
179
- computeHashOnElements(calldata || []),
180
- ])
181
- .map(bigNumberishArrayToDecimalStringArray)
182
- .map(computeHashOnElements);
183
-
184
- return computeHashOnElements([
185
- encodeShortString('StarkNet Transaction'),
186
- account,
187
- computeHashOnElements(hashArray),
188
- nonce,
115
+ return this.invokeFunction(
116
+ { contractAddress: this.address, entrypoint: '__execute__', calldata, signature },
117
+ {
189
118
  maxFee,
190
- transactionVersion,
191
- ]);
192
- }
193
- const msgHash = hashMulticall(this.address, [transaction], nonceBn.toString(), '0');
194
- if (!('keyPair' in this.signer)) {
195
- throw new Error('No keyPair');
196
- }
197
- const signature = sign((this.signer as any).keyPair, msgHash);
198
-
199
- const transformCallsToMulticallArrays = (calls: InvokeFunctionTransaction[]) => {
200
- const callArray: any[] = [];
201
- const calldata: BigNumberish[] = [];
202
- calls.forEach((call) => {
203
- const data = call.calldata || [];
204
- callArray.push({
205
- to: toBN(call.contract_address).toString(10),
206
- selector: toBN(call.entry_point_selector).toString(10),
207
- data_offset: calldata.length.toString(),
208
- data_len: data.length.toString(),
209
- });
210
- calldata.push(...data);
211
- });
212
- return {
213
- callArray,
214
- calldata: bigNumberishArrayToDecimalStringArray(calldata),
215
- };
216
- };
217
-
218
- const fromCallsToExecuteCalldata2 = (calls: InvokeFunctionTransaction[]): string[] => {
219
- const { callArray, calldata } = transformCallsToMulticallArrays(calls);
220
- return [
221
- callArray.length.toString(),
222
- ...callArray
223
- .map(
224
- ({ to, selector, data_offset, data_len }) =>
225
- [to, selector, data_offset, data_len] as string[]
226
- )
227
- .flat(),
228
- calldata.length.toString(),
229
- ...calldata,
230
- ];
231
- };
232
-
233
- const calldata = [...fromCallsToExecuteCalldata2([transaction]), nonceBn.toString()];
234
-
235
- return this.fetchEndpoint('add_transaction', undefined, {
236
- type: 'INVOKE_FUNCTION',
237
- contract_address: this.address,
238
- entry_point_selector: getSelectorFromName('__execute__'),
239
- calldata,
240
- signature: bigNumberishArrayToDecimalStringArray(signature),
241
- });
119
+ version,
120
+ }
121
+ );
242
122
  }
243
123
 
244
124
  /**