starknet 2.0.2 → 2.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +48 -0
- package/README.md +44 -4
- package/__tests__/provider.test.ts +3 -3
- package/__tests__/utils/ellipticalCurve.test.ts +1 -1
- package/__tests__/utils/shortString.test.ts +22 -0
- package/__tests__/utils/uint256.test.ts +32 -0
- package/constants.d.ts +5 -5
- package/dist/constants.d.ts +5 -5
- package/dist/index.d.ts +2 -0
- package/dist/index.js +3 -1
- package/dist/provider/default.d.ts +7 -5
- package/dist/provider/default.js +24 -19
- package/dist/types.d.ts +5 -5
- package/dist/utils/shortString.d.ts +4 -0
- package/dist/utils/shortString.js +26 -0
- package/dist/utils/stark.d.ts +3 -0
- package/dist/utils/stark.js +5 -1
- package/dist/utils/uint256.d.ts +11 -0
- package/dist/utils/uint256.js +28 -0
- package/index.d.ts +2 -0
- package/index.js +5 -1
- package/package.json +4 -2
- package/provider/default.d.ts +11 -5
- package/provider/default.js +62 -38
- package/src/constants.ts +4 -6
- package/src/index.ts +2 -0
- package/src/provider/default.ts +45 -27
- package/src/types.ts +5 -5
- package/src/utils/shortString.ts +21 -0
- package/src/utils/stark.ts +4 -0
- package/src/utils/uint256.ts +32 -0
- package/types.d.ts +5 -5
- package/utils/shortString.d.ts +4 -0
- package/utils/shortString.js +34 -0
- package/utils/stark.d.ts +5 -0
- package/utils/stark.js +6 -1
- package/utils/uint256.d.ts +11 -0
- package/utils/uint256.js +38 -0
package/provider/default.d.ts
CHANGED
|
@@ -12,16 +12,22 @@ import {
|
|
|
12
12
|
} from '../types';
|
|
13
13
|
import { BigNumberish } from '../utils/number';
|
|
14
14
|
import { ProviderInterface } from './interface';
|
|
15
|
-
declare type NetworkName = 'alpha';
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
15
|
+
declare type NetworkName = 'mainnet-alpha' | 'georli-alpha';
|
|
16
|
+
declare type ProviderOptions =
|
|
17
|
+
| {
|
|
18
|
+
network: NetworkName;
|
|
19
|
+
}
|
|
20
|
+
| {
|
|
21
|
+
baseUrl: string;
|
|
22
|
+
};
|
|
19
23
|
export declare class Provider implements ProviderInterface {
|
|
20
24
|
baseUrl: string;
|
|
21
25
|
feederGatewayUrl: string;
|
|
22
26
|
gatewayUrl: string;
|
|
23
27
|
constructor(optionsOrProvider?: ProviderOptions | Provider);
|
|
24
|
-
protected static getNetworkFromName(
|
|
28
|
+
protected static getNetworkFromName(
|
|
29
|
+
name: NetworkName
|
|
30
|
+
): 'https://alpha-mainnet.starknet.io' | 'https://alpha4.starknet.io';
|
|
25
31
|
/**
|
|
26
32
|
* Gets the smart contract address on the goerli testnet.
|
|
27
33
|
*
|
package/provider/default.js
CHANGED
|
@@ -152,6 +152,7 @@ var __importDefault =
|
|
|
152
152
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
153
153
|
exports.Provider = void 0;
|
|
154
154
|
var axios_1 = __importDefault(require('axios'));
|
|
155
|
+
var url_join_1 = __importDefault(require('url-join'));
|
|
155
156
|
var json_1 = require('../utils/json');
|
|
156
157
|
var number_1 = require('../utils/number');
|
|
157
158
|
var stark_1 = require('../utils/stark');
|
|
@@ -162,22 +163,28 @@ function wait(delay) {
|
|
|
162
163
|
}
|
|
163
164
|
var Provider = /** @class */ (function () {
|
|
164
165
|
function Provider(optionsOrProvider) {
|
|
166
|
+
if (optionsOrProvider === void 0) {
|
|
167
|
+
optionsOrProvider = { network: 'georli-alpha' };
|
|
168
|
+
}
|
|
165
169
|
if (optionsOrProvider instanceof Provider) {
|
|
166
170
|
this.baseUrl = optionsOrProvider.baseUrl;
|
|
167
171
|
this.feederGatewayUrl = optionsOrProvider.feederGatewayUrl;
|
|
168
172
|
this.gatewayUrl = optionsOrProvider.gatewayUrl;
|
|
169
173
|
} else {
|
|
170
|
-
var
|
|
171
|
-
|
|
172
|
-
|
|
174
|
+
var baseUrl =
|
|
175
|
+
'baseUrl' in optionsOrProvider
|
|
176
|
+
? optionsOrProvider.baseUrl
|
|
177
|
+
: Provider.getNetworkFromName(optionsOrProvider.network);
|
|
173
178
|
this.baseUrl = baseUrl;
|
|
174
|
-
this.feederGatewayUrl = baseUrl
|
|
175
|
-
this.gatewayUrl = baseUrl
|
|
179
|
+
this.feederGatewayUrl = (0, url_join_1.default)(baseUrl, 'feeder_gateway');
|
|
180
|
+
this.gatewayUrl = (0, url_join_1.default)(baseUrl, 'gateway');
|
|
176
181
|
}
|
|
177
182
|
}
|
|
178
183
|
Provider.getNetworkFromName = function (name) {
|
|
179
184
|
switch (name) {
|
|
180
|
-
case 'alpha':
|
|
185
|
+
case 'mainnet-alpha':
|
|
186
|
+
return 'https://alpha-mainnet.starknet.io';
|
|
187
|
+
case 'georli-alpha':
|
|
181
188
|
default:
|
|
182
189
|
return 'https://alpha4.starknet.io';
|
|
183
190
|
}
|
|
@@ -196,7 +203,9 @@ var Provider = /** @class */ (function () {
|
|
|
196
203
|
case 0:
|
|
197
204
|
return [
|
|
198
205
|
4 /*yield*/,
|
|
199
|
-
axios_1.default.get(
|
|
206
|
+
axios_1.default.get(
|
|
207
|
+
(0, url_join_1.default)(this.feederGatewayUrl, 'get_contract_addresses')
|
|
208
|
+
),
|
|
200
209
|
];
|
|
201
210
|
case 1:
|
|
202
211
|
data = _a.sent().data;
|
|
@@ -223,9 +232,11 @@ var Provider = /** @class */ (function () {
|
|
|
223
232
|
return [
|
|
224
233
|
4 /*yield*/,
|
|
225
234
|
axios_1.default.post(
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
235
|
+
(0, url_join_1.default)(
|
|
236
|
+
this.feederGatewayUrl,
|
|
237
|
+
'call_contract',
|
|
238
|
+
'?blockId=' + (blockId !== null && blockId !== void 0 ? blockId : 'null')
|
|
239
|
+
),
|
|
229
240
|
__assign({ signature: [], calldata: [] }, invokeTx)
|
|
230
241
|
),
|
|
231
242
|
];
|
|
@@ -253,9 +264,11 @@ var Provider = /** @class */ (function () {
|
|
|
253
264
|
return [
|
|
254
265
|
4 /*yield*/,
|
|
255
266
|
axios_1.default.get(
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
267
|
+
(0, url_join_1.default)(
|
|
268
|
+
this.feederGatewayUrl,
|
|
269
|
+
'get_block',
|
|
270
|
+
'?blockId=' + (blockId !== null && blockId !== void 0 ? blockId : 'null')
|
|
271
|
+
)
|
|
259
272
|
),
|
|
260
273
|
];
|
|
261
274
|
case 1:
|
|
@@ -283,11 +296,14 @@ var Provider = /** @class */ (function () {
|
|
|
283
296
|
return [
|
|
284
297
|
4 /*yield*/,
|
|
285
298
|
axios_1.default.get(
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
'
|
|
290
|
-
|
|
299
|
+
(0, url_join_1.default)(
|
|
300
|
+
this.feederGatewayUrl,
|
|
301
|
+
'get_code',
|
|
302
|
+
'?contractAddress=' +
|
|
303
|
+
contractAddress +
|
|
304
|
+
'&blockId=' +
|
|
305
|
+
(blockId !== null && blockId !== void 0 ? blockId : 'null')
|
|
306
|
+
)
|
|
291
307
|
),
|
|
292
308
|
];
|
|
293
309
|
case 1:
|
|
@@ -317,13 +333,16 @@ var Provider = /** @class */ (function () {
|
|
|
317
333
|
return [
|
|
318
334
|
4 /*yield*/,
|
|
319
335
|
axios_1.default.get(
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
'
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
336
|
+
(0, url_join_1.default)(
|
|
337
|
+
this.feederGatewayUrl,
|
|
338
|
+
'get_storage_at',
|
|
339
|
+
'?contractAddress=' +
|
|
340
|
+
contractAddress +
|
|
341
|
+
'&key=' +
|
|
342
|
+
key +
|
|
343
|
+
'&blockId=' +
|
|
344
|
+
(blockId !== null && blockId !== void 0 ? blockId : 'null')
|
|
345
|
+
)
|
|
327
346
|
),
|
|
328
347
|
];
|
|
329
348
|
case 1:
|
|
@@ -351,9 +370,11 @@ var Provider = /** @class */ (function () {
|
|
|
351
370
|
return [
|
|
352
371
|
4 /*yield*/,
|
|
353
372
|
axios_1.default.get(
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
373
|
+
(0, url_join_1.default)(
|
|
374
|
+
this.feederGatewayUrl,
|
|
375
|
+
'get_transaction_status',
|
|
376
|
+
'?transactionHash=' + (0, number_1.toHex)(txHashBn)
|
|
377
|
+
)
|
|
357
378
|
),
|
|
358
379
|
];
|
|
359
380
|
case 1:
|
|
@@ -381,9 +402,11 @@ var Provider = /** @class */ (function () {
|
|
|
381
402
|
return [
|
|
382
403
|
4 /*yield*/,
|
|
383
404
|
axios_1.default.get(
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
405
|
+
(0, url_join_1.default)(
|
|
406
|
+
this.feederGatewayUrl,
|
|
407
|
+
'get_transaction',
|
|
408
|
+
'?transactionHash=' + (0, number_1.toHex)(txHashBn)
|
|
409
|
+
)
|
|
387
410
|
),
|
|
388
411
|
];
|
|
389
412
|
case 1:
|
|
@@ -414,7 +437,7 @@ var Provider = /** @class */ (function () {
|
|
|
414
437
|
return [
|
|
415
438
|
4 /*yield*/,
|
|
416
439
|
axios_1.default.post(
|
|
417
|
-
this.gatewayUrl
|
|
440
|
+
(0, url_join_1.default)(this.gatewayUrl, 'add_transaction'),
|
|
418
441
|
(0, json_1.stringify)(
|
|
419
442
|
__assign(
|
|
420
443
|
__assign(
|
|
@@ -484,15 +507,14 @@ var Provider = /** @class */ (function () {
|
|
|
484
507
|
};
|
|
485
508
|
Provider.prototype.waitForTx = function (txHash, retryInterval) {
|
|
486
509
|
if (retryInterval === void 0) {
|
|
487
|
-
retryInterval =
|
|
510
|
+
retryInterval = 8000;
|
|
488
511
|
}
|
|
489
512
|
return __awaiter(this, void 0, void 0, function () {
|
|
490
|
-
var onchain,
|
|
513
|
+
var onchain, res;
|
|
491
514
|
return __generator(this, function (_a) {
|
|
492
515
|
switch (_a.label) {
|
|
493
516
|
case 0:
|
|
494
517
|
onchain = false;
|
|
495
|
-
firstRun = true;
|
|
496
518
|
_a.label = 1;
|
|
497
519
|
case 1:
|
|
498
520
|
if (!!onchain) return [3 /*break*/, 4];
|
|
@@ -504,14 +526,16 @@ var Provider = /** @class */ (function () {
|
|
|
504
526
|
return [4 /*yield*/, this.getTransactionStatus(txHash)];
|
|
505
527
|
case 3:
|
|
506
528
|
res = _a.sent();
|
|
507
|
-
if (
|
|
529
|
+
if (
|
|
530
|
+
res.tx_status === 'ACCEPTED_ONCHAIN' ||
|
|
531
|
+
(res.tx_status === 'PENDING' && res.block_hash !== 'pending') // This is needed as of today. In the future there will be a different status for pending transactions.
|
|
532
|
+
) {
|
|
508
533
|
onchain = true;
|
|
509
534
|
} else if (res.tx_status === 'REJECTED') {
|
|
510
535
|
throw Error('REJECTED');
|
|
511
|
-
} else if (res.tx_status === 'NOT_RECEIVED'
|
|
536
|
+
} else if (res.tx_status === 'NOT_RECEIVED') {
|
|
512
537
|
throw Error('NOT_RECEIVED');
|
|
513
538
|
}
|
|
514
|
-
firstRun = false;
|
|
515
539
|
return [3 /*break*/, 1];
|
|
516
540
|
case 4:
|
|
517
541
|
return [2 /*return*/];
|
package/src/constants.ts
CHANGED
|
@@ -1,13 +1,11 @@
|
|
|
1
|
-
import BN from 'bn.js';
|
|
2
|
-
|
|
3
1
|
import { toBN } from './utils/number';
|
|
4
2
|
|
|
5
3
|
export { IS_BROWSER } from './utils/encode';
|
|
6
4
|
|
|
7
|
-
export const ZERO
|
|
8
|
-
export const ONE
|
|
9
|
-
export const TWO
|
|
10
|
-
export const MASK_250
|
|
5
|
+
export const ZERO = toBN(0);
|
|
6
|
+
export const ONE = toBN(1);
|
|
7
|
+
export const TWO = toBN(2);
|
|
8
|
+
export const MASK_250 = TWO.pow(toBN(250)).sub(ONE); // 2 ** 250 - 1
|
|
11
9
|
|
|
12
10
|
/**
|
|
13
11
|
* The following is taken from https://github.com/starkware-libs/starkex-resources/blob/master/crypto/starkware/crypto/signature/pedersen_params.json but converted to hex, because JS is very bad handling big integers by default
|
package/src/index.ts
CHANGED
|
@@ -16,3 +16,5 @@ export * as json from './utils/json';
|
|
|
16
16
|
export * as number from './utils/number';
|
|
17
17
|
export * as stark from './utils/stark';
|
|
18
18
|
export * as ec from './utils/ellipticCurve';
|
|
19
|
+
export * as uint256 from './utils/uint256';
|
|
20
|
+
export * as shortString from './utils/shortString';
|
package/src/provider/default.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import axios from 'axios';
|
|
2
|
+
import urljoin from 'url-join';
|
|
2
3
|
|
|
3
4
|
import {
|
|
4
5
|
AddTransactionResponse,
|
|
@@ -17,11 +18,15 @@ import { BigNumberish, toBN, toHex } from '../utils/number';
|
|
|
17
18
|
import { compressProgram, formatSignature, randomAddress } from '../utils/stark';
|
|
18
19
|
import { ProviderInterface } from './interface';
|
|
19
20
|
|
|
20
|
-
type NetworkName = 'alpha';
|
|
21
|
+
type NetworkName = 'mainnet-alpha' | 'georli-alpha';
|
|
21
22
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
23
|
+
type ProviderOptions =
|
|
24
|
+
| {
|
|
25
|
+
network: NetworkName;
|
|
26
|
+
}
|
|
27
|
+
| {
|
|
28
|
+
baseUrl: string;
|
|
29
|
+
};
|
|
25
30
|
|
|
26
31
|
function wait(delay: number) {
|
|
27
32
|
return new Promise((res) => setTimeout(res, delay));
|
|
@@ -34,23 +39,27 @@ export class Provider implements ProviderInterface {
|
|
|
34
39
|
|
|
35
40
|
public gatewayUrl: string;
|
|
36
41
|
|
|
37
|
-
constructor(optionsOrProvider
|
|
42
|
+
constructor(optionsOrProvider: ProviderOptions | Provider = { network: 'georli-alpha' }) {
|
|
38
43
|
if (optionsOrProvider instanceof Provider) {
|
|
39
44
|
this.baseUrl = optionsOrProvider.baseUrl;
|
|
40
45
|
this.feederGatewayUrl = optionsOrProvider.feederGatewayUrl;
|
|
41
46
|
this.gatewayUrl = optionsOrProvider.gatewayUrl;
|
|
42
47
|
} else {
|
|
43
|
-
const
|
|
44
|
-
|
|
48
|
+
const baseUrl =
|
|
49
|
+
'baseUrl' in optionsOrProvider
|
|
50
|
+
? optionsOrProvider.baseUrl
|
|
51
|
+
: Provider.getNetworkFromName(optionsOrProvider.network);
|
|
45
52
|
this.baseUrl = baseUrl;
|
|
46
|
-
this.feederGatewayUrl =
|
|
47
|
-
this.gatewayUrl =
|
|
53
|
+
this.feederGatewayUrl = urljoin(baseUrl, 'feeder_gateway');
|
|
54
|
+
this.gatewayUrl = urljoin(baseUrl, 'gateway');
|
|
48
55
|
}
|
|
49
56
|
}
|
|
50
57
|
|
|
51
58
|
protected static getNetworkFromName(name: NetworkName) {
|
|
52
59
|
switch (name) {
|
|
53
|
-
case 'alpha':
|
|
60
|
+
case 'mainnet-alpha':
|
|
61
|
+
return 'https://alpha-mainnet.starknet.io';
|
|
62
|
+
case 'georli-alpha':
|
|
54
63
|
default:
|
|
55
64
|
return 'https://alpha4.starknet.io';
|
|
56
65
|
}
|
|
@@ -64,7 +73,7 @@ export class Provider implements ProviderInterface {
|
|
|
64
73
|
*/
|
|
65
74
|
public async getContractAddresses(): Promise<GetContractAddressesResponse> {
|
|
66
75
|
const { data } = await axios.get<GetContractAddressesResponse>(
|
|
67
|
-
|
|
76
|
+
urljoin(this.feederGatewayUrl, 'get_contract_addresses')
|
|
68
77
|
);
|
|
69
78
|
return data;
|
|
70
79
|
}
|
|
@@ -83,7 +92,7 @@ export class Provider implements ProviderInterface {
|
|
|
83
92
|
blockId?: number
|
|
84
93
|
): Promise<CallContractResponse> {
|
|
85
94
|
const { data } = await axios.post<CallContractResponse>(
|
|
86
|
-
|
|
95
|
+
urljoin(this.feederGatewayUrl, 'call_contract', `?blockId=${blockId ?? 'null'}`),
|
|
87
96
|
{
|
|
88
97
|
signature: [],
|
|
89
98
|
calldata: [],
|
|
@@ -103,7 +112,7 @@ export class Provider implements ProviderInterface {
|
|
|
103
112
|
*/
|
|
104
113
|
public async getBlock(blockId?: number): Promise<GetBlockResponse> {
|
|
105
114
|
const { data } = await axios.get<GetBlockResponse>(
|
|
106
|
-
|
|
115
|
+
urljoin(this.feederGatewayUrl, 'get_block', `?blockId=${blockId ?? 'null'}`)
|
|
107
116
|
);
|
|
108
117
|
return data;
|
|
109
118
|
}
|
|
@@ -119,9 +128,11 @@ export class Provider implements ProviderInterface {
|
|
|
119
128
|
*/
|
|
120
129
|
public async getCode(contractAddress: string, blockId?: number): Promise<GetCodeResponse> {
|
|
121
130
|
const { data } = await axios.get<GetCodeResponse>(
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
131
|
+
urljoin(
|
|
132
|
+
this.feederGatewayUrl,
|
|
133
|
+
'get_code',
|
|
134
|
+
`?contractAddress=${contractAddress}&blockId=${blockId ?? 'null'}`
|
|
135
|
+
)
|
|
125
136
|
);
|
|
126
137
|
return data;
|
|
127
138
|
}
|
|
@@ -143,9 +154,11 @@ export class Provider implements ProviderInterface {
|
|
|
143
154
|
blockId?: number
|
|
144
155
|
): Promise<object> {
|
|
145
156
|
const { data } = await axios.get<object>(
|
|
146
|
-
|
|
147
|
-
this.feederGatewayUrl
|
|
148
|
-
|
|
157
|
+
urljoin(
|
|
158
|
+
this.feederGatewayUrl,
|
|
159
|
+
'get_storage_at',
|
|
160
|
+
`?contractAddress=${contractAddress}&key=${key}&blockId=${blockId ?? 'null'}`
|
|
161
|
+
)
|
|
149
162
|
);
|
|
150
163
|
return data;
|
|
151
164
|
}
|
|
@@ -161,7 +174,11 @@ export class Provider implements ProviderInterface {
|
|
|
161
174
|
public async getTransactionStatus(txHash: BigNumberish): Promise<GetTransactionStatusResponse> {
|
|
162
175
|
const txHashBn = toBN(txHash);
|
|
163
176
|
const { data } = await axios.get<GetTransactionStatusResponse>(
|
|
164
|
-
|
|
177
|
+
urljoin(
|
|
178
|
+
this.feederGatewayUrl,
|
|
179
|
+
'get_transaction_status',
|
|
180
|
+
`?transactionHash=${toHex(txHashBn)}`
|
|
181
|
+
)
|
|
165
182
|
);
|
|
166
183
|
return data;
|
|
167
184
|
}
|
|
@@ -177,7 +194,7 @@ export class Provider implements ProviderInterface {
|
|
|
177
194
|
public async getTransaction(txHash: BigNumberish): Promise<GetTransactionResponse> {
|
|
178
195
|
const txHashBn = toBN(txHash);
|
|
179
196
|
const { data } = await axios.get<GetTransactionResponse>(
|
|
180
|
-
|
|
197
|
+
urljoin(this.feederGatewayUrl, 'get_transaction', `?transactionHash=${toHex(txHashBn)}`)
|
|
181
198
|
);
|
|
182
199
|
return data;
|
|
183
200
|
}
|
|
@@ -195,7 +212,7 @@ export class Provider implements ProviderInterface {
|
|
|
195
212
|
const contract_address_salt = tx.type === 'DEPLOY' && toHex(toBN(tx.contract_address_salt));
|
|
196
213
|
|
|
197
214
|
const { data } = await axios.post<AddTransactionResponse>(
|
|
198
|
-
|
|
215
|
+
urljoin(this.gatewayUrl, 'add_transaction'),
|
|
199
216
|
stringify({
|
|
200
217
|
...tx, // the tx can contain BigInts, so we use our own `stringify`
|
|
201
218
|
...(Array.isArray(signature) && { signature }), // not needed on deploy tx
|
|
@@ -257,23 +274,24 @@ export class Provider implements ProviderInterface {
|
|
|
257
274
|
});
|
|
258
275
|
}
|
|
259
276
|
|
|
260
|
-
public async waitForTx(txHash: BigNumberish, retryInterval: number =
|
|
277
|
+
public async waitForTx(txHash: BigNumberish, retryInterval: number = 8000) {
|
|
261
278
|
let onchain = false;
|
|
262
|
-
let firstRun = true;
|
|
263
279
|
while (!onchain) {
|
|
264
280
|
// eslint-disable-next-line no-await-in-loop
|
|
265
281
|
await wait(retryInterval);
|
|
266
282
|
// eslint-disable-next-line no-await-in-loop
|
|
267
283
|
const res = await this.getTransactionStatus(txHash);
|
|
268
284
|
|
|
269
|
-
if (
|
|
285
|
+
if (
|
|
286
|
+
res.tx_status === 'ACCEPTED_ONCHAIN' ||
|
|
287
|
+
(res.tx_status === 'PENDING' && res.block_hash !== 'pending') // This is needed as of today. In the future there will be a different status for pending transactions.
|
|
288
|
+
) {
|
|
270
289
|
onchain = true;
|
|
271
290
|
} else if (res.tx_status === 'REJECTED') {
|
|
272
291
|
throw Error('REJECTED');
|
|
273
|
-
} else if (res.tx_status === 'NOT_RECEIVED'
|
|
292
|
+
} else if (res.tx_status === 'NOT_RECEIVED') {
|
|
274
293
|
throw Error('NOT_RECEIVED');
|
|
275
294
|
}
|
|
276
|
-
firstRun = false;
|
|
277
295
|
}
|
|
278
296
|
}
|
|
279
297
|
}
|
package/src/types.ts
CHANGED
|
@@ -73,14 +73,14 @@ export type CallContractResponse = {
|
|
|
73
73
|
export type GetBlockResponse = {
|
|
74
74
|
sequence_number: number;
|
|
75
75
|
state_root: string;
|
|
76
|
-
|
|
76
|
+
block_hash: string;
|
|
77
77
|
transactions: {
|
|
78
78
|
[txHash: string]: Transaction;
|
|
79
79
|
};
|
|
80
80
|
timestamp: number;
|
|
81
81
|
transaction_receipts: {
|
|
82
82
|
[txHash: string]: {
|
|
83
|
-
|
|
83
|
+
block_hash: string;
|
|
84
84
|
transaction_hash: string;
|
|
85
85
|
l2_to_l1_messages: {
|
|
86
86
|
to_address: string;
|
|
@@ -92,7 +92,7 @@ export type GetBlockResponse = {
|
|
|
92
92
|
transaction_index: number;
|
|
93
93
|
};
|
|
94
94
|
};
|
|
95
|
-
|
|
95
|
+
previous_block_hash: string;
|
|
96
96
|
status: Status;
|
|
97
97
|
};
|
|
98
98
|
|
|
@@ -103,13 +103,13 @@ export type GetCodeResponse = {
|
|
|
103
103
|
|
|
104
104
|
export type GetTransactionStatusResponse = {
|
|
105
105
|
tx_status: Status;
|
|
106
|
-
|
|
106
|
+
block_hash: string;
|
|
107
107
|
};
|
|
108
108
|
|
|
109
109
|
export type GetTransactionResponse = {
|
|
110
110
|
status: Status;
|
|
111
111
|
transaction: Transaction;
|
|
112
|
-
|
|
112
|
+
block_hash: string;
|
|
113
113
|
block_number: number;
|
|
114
114
|
transaction_index: number;
|
|
115
115
|
transaction_hash: string;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { addHexPrefix, removeHexPrefix } from './encode';
|
|
2
|
+
|
|
3
|
+
export function isASCII(str: string) {
|
|
4
|
+
// eslint-disable-next-line no-control-regex
|
|
5
|
+
return /^[\x00-\x7F]*$/.test(str);
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
// function to check if string has less or equal 31 characters
|
|
9
|
+
export function isShortString(str: string) {
|
|
10
|
+
return str.length <= 31;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function encodeShortString(str: string) {
|
|
14
|
+
if (!isASCII(str)) throw new Error(`${str} is not an ASCII string`);
|
|
15
|
+
if (!isShortString(str)) throw new Error(`${str} is too long`);
|
|
16
|
+
return addHexPrefix(str.replace(/./g, (char) => char.charCodeAt(0).toString(16)));
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function decodeShortString(str: string) {
|
|
20
|
+
return removeHexPrefix(str).replace(/.{2}/g, (hex) => String.fromCharCode(parseInt(hex, 16)));
|
|
21
|
+
}
|
package/src/utils/stark.ts
CHANGED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { addHexPrefix } from './encode';
|
|
2
|
+
import { BigNumberish, toBN } from './number';
|
|
3
|
+
|
|
4
|
+
// Represents an integer in the range [0, 2^256).
|
|
5
|
+
export interface Uint256 {
|
|
6
|
+
// The low 128 bits of the value.
|
|
7
|
+
low: BigNumberish;
|
|
8
|
+
// The high 128 bits of the value.
|
|
9
|
+
high: BigNumberish;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
// function to convert Uint256 to BN
|
|
13
|
+
export function uint256ToBN(uint256: Uint256) {
|
|
14
|
+
return toBN(uint256.high).shln(128).add(toBN(uint256.low));
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export const UINT_128_MAX = toBN(1).shln(128).sub(toBN(1));
|
|
18
|
+
export const UINT_256_MAX = toBN(1).shln(256).sub(toBN(1));
|
|
19
|
+
// function to check if BN is smaller or equal 2**256-1
|
|
20
|
+
export function isUint256(bn: BigNumberish): boolean {
|
|
21
|
+
return toBN(bn).lte(UINT_256_MAX);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// function to convert BN to Uint256
|
|
25
|
+
export function bnToUint256(bignumber: BigNumberish): Uint256 {
|
|
26
|
+
const bn = toBN(bignumber);
|
|
27
|
+
if (!isUint256(bn)) throw new Error('Number is too large');
|
|
28
|
+
return {
|
|
29
|
+
low: addHexPrefix(bn.maskn(128).toString(16)),
|
|
30
|
+
high: addHexPrefix(bn.shrn(128).toString(16)),
|
|
31
|
+
};
|
|
32
|
+
}
|
package/types.d.ts
CHANGED
|
@@ -67,14 +67,14 @@ export declare type CallContractResponse = {
|
|
|
67
67
|
export declare type GetBlockResponse = {
|
|
68
68
|
sequence_number: number;
|
|
69
69
|
state_root: string;
|
|
70
|
-
|
|
70
|
+
block_hash: string;
|
|
71
71
|
transactions: {
|
|
72
72
|
[txHash: string]: Transaction;
|
|
73
73
|
};
|
|
74
74
|
timestamp: number;
|
|
75
75
|
transaction_receipts: {
|
|
76
76
|
[txHash: string]: {
|
|
77
|
-
|
|
77
|
+
block_hash: string;
|
|
78
78
|
transaction_hash: string;
|
|
79
79
|
l2_to_l1_messages: {
|
|
80
80
|
to_address: string;
|
|
@@ -86,7 +86,7 @@ export declare type GetBlockResponse = {
|
|
|
86
86
|
transaction_index: number;
|
|
87
87
|
};
|
|
88
88
|
};
|
|
89
|
-
|
|
89
|
+
previous_block_hash: string;
|
|
90
90
|
status: Status;
|
|
91
91
|
};
|
|
92
92
|
export declare type GetCodeResponse = {
|
|
@@ -95,12 +95,12 @@ export declare type GetCodeResponse = {
|
|
|
95
95
|
};
|
|
96
96
|
export declare type GetTransactionStatusResponse = {
|
|
97
97
|
tx_status: Status;
|
|
98
|
-
|
|
98
|
+
block_hash: string;
|
|
99
99
|
};
|
|
100
100
|
export declare type GetTransactionResponse = {
|
|
101
101
|
status: Status;
|
|
102
102
|
transaction: Transaction;
|
|
103
|
-
|
|
103
|
+
block_hash: string;
|
|
104
104
|
block_number: number;
|
|
105
105
|
transaction_index: number;
|
|
106
106
|
transaction_hash: string;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
3
|
+
exports.decodeShortString =
|
|
4
|
+
exports.encodeShortString =
|
|
5
|
+
exports.isShortString =
|
|
6
|
+
exports.isASCII =
|
|
7
|
+
void 0;
|
|
8
|
+
var encode_1 = require('./encode');
|
|
9
|
+
function isASCII(str) {
|
|
10
|
+
// eslint-disable-next-line no-control-regex
|
|
11
|
+
return /^[\x00-\x7F]*$/.test(str);
|
|
12
|
+
}
|
|
13
|
+
exports.isASCII = isASCII;
|
|
14
|
+
// function to check if string has less or equal 31 characters
|
|
15
|
+
function isShortString(str) {
|
|
16
|
+
return str.length <= 31;
|
|
17
|
+
}
|
|
18
|
+
exports.isShortString = isShortString;
|
|
19
|
+
function encodeShortString(str) {
|
|
20
|
+
if (!isASCII(str)) throw new Error(str + ' is not an ASCII string');
|
|
21
|
+
if (!isShortString(str)) throw new Error(str + ' is too long');
|
|
22
|
+
return (0, encode_1.addHexPrefix)(
|
|
23
|
+
str.replace(/./g, function (char) {
|
|
24
|
+
return char.charCodeAt(0).toString(16);
|
|
25
|
+
})
|
|
26
|
+
);
|
|
27
|
+
}
|
|
28
|
+
exports.encodeShortString = encodeShortString;
|
|
29
|
+
function decodeShortString(str) {
|
|
30
|
+
return (0, encode_1.removeHexPrefix)(str).replace(/.{2}/g, function (hex) {
|
|
31
|
+
return String.fromCharCode(parseInt(hex, 16));
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
exports.decodeShortString = decodeShortString;
|
package/utils/stark.d.ts
CHANGED
|
@@ -19,3 +19,8 @@ export declare function getSelectorFromName(funcName: string): string;
|
|
|
19
19
|
export declare function randomAddress(): string;
|
|
20
20
|
export declare function makeAddress(input: string): string;
|
|
21
21
|
export declare function formatSignature(sig?: [BigNumberish, BigNumberish]): [string, string] | [];
|
|
22
|
+
export declare function compileStructToCalldata<
|
|
23
|
+
S extends {
|
|
24
|
+
[k: string]: string;
|
|
25
|
+
}
|
|
26
|
+
>(struct: S): string[];
|
package/utils/stark.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.compileStructToCalldata =
|
|
4
|
+
exports.formatSignature =
|
|
4
5
|
exports.makeAddress =
|
|
5
6
|
exports.randomAddress =
|
|
6
7
|
exports.getSelectorFromName =
|
|
@@ -62,3 +63,7 @@ function formatSignature(sig) {
|
|
|
62
63
|
}
|
|
63
64
|
}
|
|
64
65
|
exports.formatSignature = formatSignature;
|
|
66
|
+
function compileStructToCalldata(struct) {
|
|
67
|
+
return Object.values(struct);
|
|
68
|
+
}
|
|
69
|
+
exports.compileStructToCalldata = compileStructToCalldata;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/// <reference types="bn.js" />
|
|
2
|
+
import { BigNumberish } from './number';
|
|
3
|
+
export interface Uint256 {
|
|
4
|
+
low: BigNumberish;
|
|
5
|
+
high: BigNumberish;
|
|
6
|
+
}
|
|
7
|
+
export declare function uint256ToBN(uint256: Uint256): import('bn.js');
|
|
8
|
+
export declare const UINT_128_MAX: import('bn.js');
|
|
9
|
+
export declare const UINT_256_MAX: import('bn.js');
|
|
10
|
+
export declare function isUint256(bn: BigNumberish): boolean;
|
|
11
|
+
export declare function bnToUint256(bignumber: BigNumberish): Uint256;
|