starknet 3.0.0 → 3.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 +26 -0
- package/__mocks__/ArgentAccount.json +68548 -51944
- package/__mocks__/TestDapp.json +12962 -0
- package/__mocks__/contract.json +33191 -0
- package/__mocks__/multicall.json +8139 -0
- package/__tests__/account.test.ts +63 -49
- package/__tests__/accountContract.test.ts +51 -70
- package/__tests__/contract.test.ts +182 -35
- package/__tests__/fixtures.ts +13 -0
- package/__tests__/provider.test.ts +4 -14
- package/__tests__/utils/__snapshots__/utils.browser.test.ts.snap +2 -2
- package/__tests__/utils/__snapshots__/utils.test.ts.snap +2 -2
- package/__tests__/utils/ellipticalCurve.test.ts +20 -13
- package/__tests__/utils/utils.test.ts +3 -3
- package/account/default.d.ts +4 -4
- package/account/default.js +43 -92
- package/account/interface.d.ts +2 -2
- package/contract.d.ts +68 -9
- package/contract.js +229 -77
- package/dist/account/default.d.ts +3 -3
- package/dist/account/default.js +31 -57
- package/dist/account/interface.d.ts +2 -2
- package/dist/contract.d.ts +68 -6
- package/dist/contract.js +207 -55
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/provider/default.d.ts +15 -2
- package/dist/provider/default.js +61 -17
- package/dist/provider/interface.d.ts +5 -1
- package/dist/signer/default.d.ts +1 -1
- package/dist/signer/default.js +6 -18
- package/dist/signer/interface.d.ts +3 -2
- package/dist/types/api.d.ts +12 -0
- package/dist/types/lib.d.ts +3 -3
- package/dist/utils/ellipticCurve.js +1 -1
- package/dist/utils/hash.d.ts +12 -2
- package/dist/utils/hash.js +37 -9
- package/dist/utils/number.d.ts +1 -0
- package/dist/utils/number.js +28 -2
- package/dist/utils/stark.d.ts +2 -9
- package/dist/utils/stark.js +44 -14
- package/dist/utils/transaction.d.ts +19 -0
- package/dist/utils/transaction.js +75 -0
- package/dist/utils/typedData/index.d.ts +1 -1
- package/dist/utils/typedData/index.js +2 -3
- package/index.d.ts +1 -0
- package/index.js +1 -0
- package/package.json +2 -2
- package/provider/default.d.ts +20 -1
- package/provider/default.js +83 -19
- package/provider/interface.d.ts +5 -1
- package/signer/default.d.ts +1 -1
- package/signer/default.js +10 -44
- package/signer/interface.d.ts +3 -2
- package/src/account/default.ts +21 -43
- package/src/account/interface.ts +2 -2
- package/src/contract.ts +232 -62
- package/src/index.ts +1 -0
- package/src/provider/default.ts +58 -22
- package/src/provider/interface.ts +6 -1
- package/src/signer/default.ts +10 -26
- package/src/signer/interface.ts +3 -2
- package/src/types/api.ts +11 -0
- package/src/types/lib.ts +3 -4
- package/src/utils/ellipticCurve.ts +1 -1
- package/src/utils/hash.ts +39 -12
- package/src/utils/number.ts +8 -1
- package/src/utils/stark.ts +14 -15
- package/src/utils/transaction.ts +50 -0
- package/src/utils/typedData/index.ts +2 -3
- package/types/api.d.ts +15 -0
- package/types/lib.d.ts +3 -3
- package/utils/ellipticCurve.js +1 -1
- package/utils/hash.d.ts +15 -6
- package/utils/hash.js +42 -10
- package/utils/number.d.ts +1 -0
- package/utils/number.js +46 -1
- package/utils/stark.d.ts +2 -9
- package/utils/stark.js +64 -15
- package/utils/transaction.d.ts +19 -0
- package/utils/transaction.js +99 -0
- package/utils/typedData/index.d.ts +1 -1
- package/utils/typedData/index.js +2 -3
package/contract.js
CHANGED
|
@@ -184,7 +184,8 @@ var __importDefault =
|
|
|
184
184
|
return mod && mod.__esModule ? mod : { default: mod };
|
|
185
185
|
};
|
|
186
186
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
187
|
-
exports.Contract =
|
|
187
|
+
exports.Contract = void 0;
|
|
188
|
+
var bn_js_1 = __importDefault(require('bn.js'));
|
|
188
189
|
var minimalistic_assert_1 = __importDefault(require('minimalistic-assert'));
|
|
189
190
|
var provider_1 = require('./provider');
|
|
190
191
|
var number_1 = require('./utils/number');
|
|
@@ -195,42 +196,6 @@ function parseFelt(candidate) {
|
|
|
195
196
|
throw Error('Couldnt parse felt');
|
|
196
197
|
}
|
|
197
198
|
}
|
|
198
|
-
function isFelt(candidate) {
|
|
199
|
-
try {
|
|
200
|
-
parseFelt(candidate);
|
|
201
|
-
return true;
|
|
202
|
-
} catch (e) {
|
|
203
|
-
return false;
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
function compileCalldata(args) {
|
|
207
|
-
return Object.values(args).flatMap(function (value) {
|
|
208
|
-
if (Array.isArray(value))
|
|
209
|
-
return __spreadArray(
|
|
210
|
-
[(0, number_1.toBN)(value.length).toString()],
|
|
211
|
-
__read(
|
|
212
|
-
value.map(function (x) {
|
|
213
|
-
return (0, number_1.toBN)(x).toString();
|
|
214
|
-
})
|
|
215
|
-
),
|
|
216
|
-
false
|
|
217
|
-
);
|
|
218
|
-
if (typeof value === 'object' && 'type' in value)
|
|
219
|
-
return Object.entries(value)
|
|
220
|
-
.filter(function (_a) {
|
|
221
|
-
var _b = __read(_a, 1),
|
|
222
|
-
k = _b[0];
|
|
223
|
-
return k !== 'type';
|
|
224
|
-
})
|
|
225
|
-
.map(function (_a) {
|
|
226
|
-
var _b = __read(_a, 2),
|
|
227
|
-
v = _b[1];
|
|
228
|
-
return (0, number_1.toBN)(v).toString();
|
|
229
|
-
});
|
|
230
|
-
return (0, number_1.toBN)(value).toString();
|
|
231
|
-
});
|
|
232
|
-
}
|
|
233
|
-
exports.compileCalldata = compileCalldata;
|
|
234
199
|
var Contract = /** @class */ (function () {
|
|
235
200
|
/**
|
|
236
201
|
* Contract class to handle contract methods
|
|
@@ -258,11 +223,25 @@ var Contract = /** @class */ (function () {
|
|
|
258
223
|
return __assign(__assign({}, acc), ((_a = {}), (_a[abiEntry.name] = abiEntry), _a));
|
|
259
224
|
}, {});
|
|
260
225
|
}
|
|
226
|
+
/**
|
|
227
|
+
* Saves the address of the contract deployed on network that will be used for interaction
|
|
228
|
+
*
|
|
229
|
+
* @param address - address of the contract
|
|
230
|
+
* @returns Contract
|
|
231
|
+
*/
|
|
261
232
|
Contract.prototype.connect = function (address) {
|
|
262
233
|
this.connectedTo = address;
|
|
263
234
|
return this;
|
|
264
235
|
};
|
|
236
|
+
/**
|
|
237
|
+
* Validates if all arguments that are passed to the method are corresponding to the ones in the abi
|
|
238
|
+
*
|
|
239
|
+
* @param type - type of the method
|
|
240
|
+
* @param method - name of the method
|
|
241
|
+
* @param args - arguments that are passed to the method
|
|
242
|
+
*/
|
|
265
243
|
Contract.prototype.validateMethodAndArgs = function (type, method, args) {
|
|
244
|
+
var _this = this;
|
|
266
245
|
if (args === void 0) {
|
|
267
246
|
args = {};
|
|
268
247
|
}
|
|
@@ -289,58 +268,225 @@ var Contract = /** @class */ (function () {
|
|
|
289
268
|
if (arg !== undefined) {
|
|
290
269
|
if (input.type === 'felt') {
|
|
291
270
|
(0, minimalistic_assert_1.default)(
|
|
292
|
-
typeof arg === 'string',
|
|
293
|
-
'arg ' + input.name + ' should be a felt (string)'
|
|
294
|
-
);
|
|
295
|
-
(0, minimalistic_assert_1.default)(
|
|
296
|
-
isFelt(arg),
|
|
297
|
-
'arg ' + input.name + ' should be decimal or hexadecimal'
|
|
298
|
-
);
|
|
299
|
-
} else if (typeof arg === 'object' && 'type' in arg) {
|
|
300
|
-
(0, minimalistic_assert_1.default)(
|
|
301
|
-
arg.type === 'struct',
|
|
302
|
-
'arg ' + input.name + ' should be a struct'
|
|
271
|
+
typeof arg === 'string' || typeof arg === 'number' || arg instanceof bn_js_1.default,
|
|
272
|
+
'arg ' + input.name + ' should be a felt (string, number, BigNumber)'
|
|
303
273
|
);
|
|
274
|
+
} else if (typeof arg === 'object' && input.type in _this.structs) {
|
|
275
|
+
_this.structs[input.type].members.forEach(function (_a) {
|
|
276
|
+
var name = _a.name;
|
|
277
|
+
(0,
|
|
278
|
+
minimalistic_assert_1.default)(Object.keys(arg).includes(name), 'arg should have a property ' + name);
|
|
279
|
+
});
|
|
304
280
|
} else {
|
|
305
281
|
(0, minimalistic_assert_1.default)(
|
|
306
282
|
Array.isArray(arg),
|
|
307
|
-
'arg ' + input.name + ' should be
|
|
283
|
+
'arg ' + input.name + ' should be an Array'
|
|
308
284
|
);
|
|
309
|
-
|
|
310
|
-
(
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
})
|
|
285
|
+
if (input.type === 'felt*') {
|
|
286
|
+
arg.forEach(function (felt) {
|
|
287
|
+
(0,
|
|
288
|
+
minimalistic_assert_1.default)(typeof felt === 'string' || typeof felt === 'number' || felt instanceof bn_js_1.default, 'arg ' + input.name + ' should be an array of string, number or BigNumber');
|
|
289
|
+
});
|
|
290
|
+
} else if (/\(felt/.test(input.type)) {
|
|
291
|
+
var tupleLength = input.type.split(',').length;
|
|
292
|
+
(0, minimalistic_assert_1.default)(
|
|
293
|
+
arg.length === tupleLength,
|
|
294
|
+
'arg ' + input.name + ' should have ' + tupleLength + ' elements in tuple'
|
|
295
|
+
);
|
|
296
|
+
arg.forEach(function (felt) {
|
|
297
|
+
(0,
|
|
298
|
+
minimalistic_assert_1.default)(typeof felt === 'string' || typeof felt === 'number' || felt instanceof bn_js_1.default, 'arg ' + input.name + ' should be an array of string, number or BigNumber');
|
|
299
|
+
});
|
|
300
|
+
} else {
|
|
301
|
+
var arrayType_1 = input.type.replace('*', '');
|
|
302
|
+
arg.forEach(function (struct) {
|
|
303
|
+
_this.structs[arrayType_1].members.forEach(function (_a) {
|
|
304
|
+
var name = _a.name;
|
|
305
|
+
(0,
|
|
306
|
+
minimalistic_assert_1.default)(Object.keys(struct).includes(name), 'arg ' + input.name + ' should be an array of ' + arrayType_1);
|
|
307
|
+
});
|
|
308
|
+
});
|
|
309
|
+
}
|
|
315
310
|
}
|
|
316
311
|
}
|
|
317
312
|
});
|
|
318
313
|
};
|
|
319
|
-
|
|
314
|
+
/**
|
|
315
|
+
* Deep parse of the object that has been passed to the method
|
|
316
|
+
*
|
|
317
|
+
* @param element - element that needs to be parsed
|
|
318
|
+
* @param type - name of the method
|
|
319
|
+
* @return {string | string[]} - parsed arguments in format that contract is expecting
|
|
320
|
+
*/
|
|
321
|
+
Contract.prototype.parseCalldataObject = function (element, type) {
|
|
320
322
|
var _this = this;
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
return responseIterator.next().value;
|
|
323
|
+
if (element === undefined) {
|
|
324
|
+
throw Error('Missing element in calldata object');
|
|
324
325
|
}
|
|
325
|
-
if
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
326
|
+
// checking if the passed element is struct or element in struct
|
|
327
|
+
if (this.structs[type] && this.structs[type].members.length) {
|
|
328
|
+
// going through all the members of the struct and parsing the value
|
|
329
|
+
return this.structs[type].members.reduce(function (acc, member) {
|
|
330
|
+
// if the member of the struct is another struct this will return array of the felts if not it will be single felt
|
|
331
|
+
// TODO: refactor types so member name can be used as keyof ParsedStruct
|
|
332
|
+
/* @ts-ignore */
|
|
333
|
+
var parsedData = _this.parseCalldataObject(element[member.name], member.type);
|
|
334
|
+
if (typeof parsedData === 'string') {
|
|
335
|
+
acc.push(parsedData);
|
|
336
|
+
} else {
|
|
337
|
+
acc.push.apply(acc, __spreadArray([], __read(parsedData), false));
|
|
338
|
+
}
|
|
339
|
+
return acc;
|
|
340
|
+
}, []);
|
|
329
341
|
}
|
|
330
|
-
return
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
342
|
+
return (0, number_1.toFelt)(element);
|
|
343
|
+
};
|
|
344
|
+
/**
|
|
345
|
+
* Parse of the response elements that are converted to Object (Struct) by using the abi
|
|
346
|
+
*
|
|
347
|
+
* @param responseIterator - iterator of the response
|
|
348
|
+
* @param type - type of the struct
|
|
349
|
+
* @return {BigNumberish | ParsedStruct} - parsed arguments in format that contract is expecting
|
|
350
|
+
*/
|
|
351
|
+
Contract.prototype.parseResponseStruct = function (responseIterator, type) {
|
|
352
|
+
var _this = this;
|
|
353
|
+
// check the type of current element
|
|
354
|
+
if (type in this.structs && this.structs[type]) {
|
|
355
|
+
return this.structs[type].members.reduce(function (acc, el) {
|
|
356
|
+
// parse each member of the struct (member can felt or nested struct)
|
|
357
|
+
acc[el.name] = _this.parseResponseStruct(responseIterator, el.type);
|
|
358
|
+
return acc;
|
|
359
|
+
}, {});
|
|
360
|
+
}
|
|
361
|
+
return parseFelt(responseIterator.next().value);
|
|
337
362
|
};
|
|
363
|
+
/**
|
|
364
|
+
* Parse one field of the calldata by using input field from the abi for that method
|
|
365
|
+
*
|
|
366
|
+
* @param args - value of the field
|
|
367
|
+
* @param input - input(field) information from the abi that will be used to parse the data
|
|
368
|
+
* @return {string | string[]} - parsed arguments in format that contract is expecting
|
|
369
|
+
*/
|
|
370
|
+
Contract.prototype.parsCalldataField = function (args, input) {
|
|
371
|
+
var _this = this;
|
|
372
|
+
var name = input.name,
|
|
373
|
+
type = input.type;
|
|
374
|
+
var value = args[name];
|
|
375
|
+
var propName = name.replace(/_len$/, '');
|
|
376
|
+
switch (true) {
|
|
377
|
+
case /_len$/.test(name):
|
|
378
|
+
if (Array.isArray(args[propName])) {
|
|
379
|
+
var arr = args[propName];
|
|
380
|
+
return (0, number_1.toFelt)(arr.length);
|
|
381
|
+
}
|
|
382
|
+
throw Error('Expected ' + propName + ' to be array');
|
|
383
|
+
case /\*/.test(type):
|
|
384
|
+
if (Array.isArray(value)) {
|
|
385
|
+
return value.reduce(function (acc, el) {
|
|
386
|
+
if (/felt/.test(type)) {
|
|
387
|
+
acc.push((0, number_1.toFelt)(el));
|
|
388
|
+
} else {
|
|
389
|
+
acc.push.apply(
|
|
390
|
+
acc,
|
|
391
|
+
__spreadArray(
|
|
392
|
+
[],
|
|
393
|
+
__read(_this.parseCalldataObject(el, type.replace('*', ''))),
|
|
394
|
+
false
|
|
395
|
+
)
|
|
396
|
+
);
|
|
397
|
+
}
|
|
398
|
+
return acc;
|
|
399
|
+
}, []);
|
|
400
|
+
}
|
|
401
|
+
throw Error('Expected ' + name + ' to be array');
|
|
402
|
+
case type in this.structs:
|
|
403
|
+
return this.parseCalldataObject(value, type);
|
|
404
|
+
case /\(felt/.test(type):
|
|
405
|
+
if (Array.isArray(value)) {
|
|
406
|
+
return value.map(function (el) {
|
|
407
|
+
return (0, number_1.toFelt)(el);
|
|
408
|
+
});
|
|
409
|
+
}
|
|
410
|
+
throw Error('Expected ' + name + ' to be array');
|
|
411
|
+
default:
|
|
412
|
+
return (0, number_1.toFelt)(value);
|
|
413
|
+
}
|
|
414
|
+
};
|
|
415
|
+
/**
|
|
416
|
+
* Parse the calldata by using input fields from the abi for that method
|
|
417
|
+
*
|
|
418
|
+
* @param args - arguments passed the the method
|
|
419
|
+
* @param inputs - list of inputs(fields) that are in the abi
|
|
420
|
+
* @return {Calldata} - parsed arguments in format that contract is expecting
|
|
421
|
+
*/
|
|
422
|
+
Contract.prototype.compileCalldata = function (args, inputs) {
|
|
423
|
+
var _this = this;
|
|
424
|
+
return inputs.reduce(function (acc, input) {
|
|
425
|
+
var parsedData = _this.parsCalldataField(args, input);
|
|
426
|
+
if (Array.isArray(parsedData)) {
|
|
427
|
+
acc.push.apply(acc, __spreadArray([], __read(parsedData), false));
|
|
428
|
+
} else {
|
|
429
|
+
acc.push(parsedData);
|
|
430
|
+
}
|
|
431
|
+
return acc;
|
|
432
|
+
}, []);
|
|
433
|
+
};
|
|
434
|
+
/**
|
|
435
|
+
* Parse elements of the response and structuring them into one field by using output property from the abi for that method
|
|
436
|
+
*
|
|
437
|
+
* @param responseIterator - iterator of the response
|
|
438
|
+
* @param output - output(field) information from the abi that will be used to parse the data
|
|
439
|
+
* @return - parsed response corresponding to the abi structure of the field
|
|
440
|
+
*/
|
|
441
|
+
Contract.prototype.parseResponseField = function (responseIterator, output, parsedResult) {
|
|
442
|
+
var name = output.name,
|
|
443
|
+
type = output.type;
|
|
444
|
+
var arrLen;
|
|
445
|
+
var parsedDataArr = [];
|
|
446
|
+
switch (true) {
|
|
447
|
+
case /_len$/.test(name):
|
|
448
|
+
return parseFelt(responseIterator.next().value).toNumber();
|
|
449
|
+
case /\(felt/.test(type):
|
|
450
|
+
return type.split(',').reduce(function (acc) {
|
|
451
|
+
acc.push(parseFelt(responseIterator.next().value));
|
|
452
|
+
return acc;
|
|
453
|
+
}, []);
|
|
454
|
+
case /\*/.test(type):
|
|
455
|
+
if (parsedResult && parsedResult[name + '_len']) {
|
|
456
|
+
arrLen = parsedResult[name + '_len'];
|
|
457
|
+
while (parsedDataArr.length < arrLen) {
|
|
458
|
+
parsedDataArr.push(
|
|
459
|
+
this.parseResponseStruct(responseIterator, output.type.replace('*', ''))
|
|
460
|
+
);
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
return parsedDataArr;
|
|
464
|
+
case type in this.structs:
|
|
465
|
+
return this.parseResponseStruct(responseIterator, type);
|
|
466
|
+
default:
|
|
467
|
+
return parseFelt(responseIterator.next().value);
|
|
468
|
+
}
|
|
469
|
+
};
|
|
470
|
+
/**
|
|
471
|
+
* Parse elements of the response array and structuring them into response object
|
|
472
|
+
*
|
|
473
|
+
* @param method - method name
|
|
474
|
+
* @param response - response from the method
|
|
475
|
+
* @return - parsed response corresponding to the abi
|
|
476
|
+
*/
|
|
338
477
|
Contract.prototype.parseResponse = function (method, response) {
|
|
339
|
-
var
|
|
478
|
+
var _this = this;
|
|
479
|
+
var outputs = this.abi.find(function (abi) {
|
|
340
480
|
return abi.name === method;
|
|
341
|
-
});
|
|
481
|
+
}).outputs;
|
|
342
482
|
var responseIterator = response.flat()[Symbol.iterator]();
|
|
343
|
-
return
|
|
483
|
+
return outputs.flat().reduce(function (acc, output) {
|
|
484
|
+
acc[output.name] = _this.parseResponseField(responseIterator, output, acc);
|
|
485
|
+
if (acc[output.name] && acc[output.name + '_len']) {
|
|
486
|
+
delete acc[output.name + '_len'];
|
|
487
|
+
}
|
|
488
|
+
return acc;
|
|
489
|
+
}, {});
|
|
344
490
|
};
|
|
345
491
|
Contract.prototype.invoke = function (method, args, signature) {
|
|
346
492
|
if (args === void 0) {
|
|
@@ -353,8 +499,11 @@ var Contract = /** @class */ (function () {
|
|
|
353
499
|
);
|
|
354
500
|
// validate method and args
|
|
355
501
|
this.validateMethodAndArgs('INVOKE', method, args);
|
|
502
|
+
var inputs = this.abi.find(function (abi) {
|
|
503
|
+
return abi.name === method;
|
|
504
|
+
}).inputs;
|
|
356
505
|
// compile calldata
|
|
357
|
-
var calldata = compileCalldata(args);
|
|
506
|
+
var calldata = this.compileCalldata(args, inputs);
|
|
358
507
|
return this.provider.invokeFunction({
|
|
359
508
|
contractAddress: this.connectedTo,
|
|
360
509
|
signature: signature,
|
|
@@ -370,7 +519,7 @@ var Contract = /** @class */ (function () {
|
|
|
370
519
|
blockIdentifier = null;
|
|
371
520
|
}
|
|
372
521
|
return __awaiter(this, void 0, void 0, function () {
|
|
373
|
-
var calldata;
|
|
522
|
+
var inputs, calldata;
|
|
374
523
|
var _this = this;
|
|
375
524
|
return __generator(this, function (_a) {
|
|
376
525
|
// ensure contract is connected
|
|
@@ -378,15 +527,18 @@ var Contract = /** @class */ (function () {
|
|
|
378
527
|
minimalistic_assert_1.default)(this.connectedTo !== null, 'contract isnt connected to an address');
|
|
379
528
|
// validate method and args
|
|
380
529
|
this.validateMethodAndArgs('CALL', method, args);
|
|
381
|
-
|
|
530
|
+
inputs = this.abi.find(function (abi) {
|
|
531
|
+
return abi.name === method;
|
|
532
|
+
}).inputs;
|
|
533
|
+
calldata = this.compileCalldata(args, inputs);
|
|
382
534
|
return [
|
|
383
535
|
2 /*return*/,
|
|
384
536
|
this.provider
|
|
385
537
|
.callContract(
|
|
386
538
|
{
|
|
387
539
|
contractAddress: this.connectedTo,
|
|
388
|
-
entrypoint: method,
|
|
389
540
|
calldata: calldata,
|
|
541
|
+
entrypoint: method,
|
|
390
542
|
},
|
|
391
543
|
blockIdentifier
|
|
392
544
|
)
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Provider } from '../provider';
|
|
2
|
-
import { Abi, AddTransactionResponse,
|
|
2
|
+
import { Abi, AddTransactionResponse, Call, InvocationsDetails, KeyPair, Signature } from '../types';
|
|
3
3
|
import { BigNumberish } from '../utils/number';
|
|
4
4
|
import { TypedData } from '../utils/typedData';
|
|
5
5
|
import { AccountInterface } from './interface';
|
|
@@ -16,7 +16,7 @@ export declare class Account extends Provider implements AccountInterface {
|
|
|
16
16
|
* @param transaction - transaction to be invoked
|
|
17
17
|
* @returns a confirmation of invoking a function on the starknet contract
|
|
18
18
|
*/
|
|
19
|
-
execute(
|
|
19
|
+
execute(calls: Call | Call[], abis?: Abi[] | undefined, transactionsDetail?: InvocationsDetails): Promise<AddTransactionResponse>;
|
|
20
20
|
/**
|
|
21
21
|
* Sign an JSON object with the starknet private key and return the signature
|
|
22
22
|
*
|
|
@@ -36,7 +36,7 @@ export declare class Account extends Provider implements AccountInterface {
|
|
|
36
36
|
/**
|
|
37
37
|
* Verify a signature of a JSON object
|
|
38
38
|
*
|
|
39
|
-
* @param
|
|
39
|
+
* @param hash - JSON object to be verified
|
|
40
40
|
* @param signature - signature of the JSON object
|
|
41
41
|
* @returns true if the signature is valid, false otherwise
|
|
42
42
|
* @throws {Error} if the JSON object is not a valid JSON or the signature is not a valid signature
|
package/dist/account/default.js
CHANGED
|
@@ -14,17 +14,6 @@ var __extends = (this && this.__extends) || (function () {
|
|
|
14
14
|
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
15
15
|
};
|
|
16
16
|
})();
|
|
17
|
-
var __assign = (this && this.__assign) || function () {
|
|
18
|
-
__assign = Object.assign || function(t) {
|
|
19
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
20
|
-
s = arguments[i];
|
|
21
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
22
|
-
t[p] = s[p];
|
|
23
|
-
}
|
|
24
|
-
return t;
|
|
25
|
-
};
|
|
26
|
-
return __assign.apply(this, arguments);
|
|
27
|
-
};
|
|
28
17
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
29
18
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
30
19
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
@@ -61,17 +50,6 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
|
61
50
|
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
62
51
|
}
|
|
63
52
|
};
|
|
64
|
-
var __rest = (this && this.__rest) || function (s, e) {
|
|
65
|
-
var t = {};
|
|
66
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
67
|
-
t[p] = s[p];
|
|
68
|
-
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
69
|
-
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
70
|
-
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
71
|
-
t[p[i]] = s[p[i]];
|
|
72
|
-
}
|
|
73
|
-
return t;
|
|
74
|
-
};
|
|
75
53
|
var __read = (this && this.__read) || function (o, n) {
|
|
76
54
|
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
77
55
|
if (!m) return o;
|
|
@@ -99,11 +77,12 @@ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
|
99
77
|
};
|
|
100
78
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
101
79
|
exports.Account = void 0;
|
|
102
|
-
var contract_1 = require("../contract");
|
|
103
80
|
var provider_1 = require("../provider");
|
|
104
81
|
var signer_1 = require("../signer");
|
|
82
|
+
var hash_1 = require("../utils/hash");
|
|
105
83
|
var number_1 = require("../utils/number");
|
|
106
84
|
var stark_1 = require("../utils/stark");
|
|
85
|
+
var transaction_1 = require("../utils/transaction");
|
|
107
86
|
var typedData_1 = require("../utils/typedData");
|
|
108
87
|
var Account = /** @class */ (function (_super) {
|
|
109
88
|
__extends(Account, _super);
|
|
@@ -137,47 +116,42 @@ var Account = /** @class */ (function (_super) {
|
|
|
137
116
|
* @param transaction - transaction to be invoked
|
|
138
117
|
* @returns a confirmation of invoking a function on the starknet contract
|
|
139
118
|
*/
|
|
140
|
-
Account.prototype.execute = function (
|
|
141
|
-
|
|
119
|
+
Account.prototype.execute = function (calls, abis, transactionsDetail) {
|
|
120
|
+
var _a, _b;
|
|
121
|
+
if (abis === void 0) { abis = undefined; }
|
|
142
122
|
if (transactionsDetail === void 0) { transactionsDetail = {}; }
|
|
143
123
|
return __awaiter(this, void 0, void 0, function () {
|
|
144
|
-
var
|
|
145
|
-
|
|
146
|
-
|
|
124
|
+
var transactions, signerDetails, _c, _d, signature, calldata;
|
|
125
|
+
var _e;
|
|
126
|
+
return __generator(this, function (_f) {
|
|
127
|
+
switch (_f.label) {
|
|
147
128
|
case 0:
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
nonce = transactionsDetail.nonce;
|
|
129
|
+
transactions = Array.isArray(calls) ? calls : [calls];
|
|
130
|
+
_e = {
|
|
131
|
+
walletAddress: this.address
|
|
132
|
+
};
|
|
153
133
|
_c = number_1.toBN;
|
|
154
|
-
if (!(nonce !== null &&
|
|
155
|
-
_d =
|
|
134
|
+
if (!((_a = transactionsDetail.nonce) !== null && _a !== void 0)) return [3 /*break*/, 1];
|
|
135
|
+
_d = _a;
|
|
156
136
|
return [3 /*break*/, 3];
|
|
157
137
|
case 1: return [4 /*yield*/, this.getNonce()];
|
|
158
138
|
case 2:
|
|
159
|
-
_d = (
|
|
160
|
-
|
|
139
|
+
_d = (_f.sent());
|
|
140
|
+
_f.label = 3;
|
|
161
141
|
case 3:
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
], { walletAddress: this.address, nonce: nonceBn }, abis)];
|
|
142
|
+
signerDetails = (_e.nonce = _c.apply(void 0, [_d]),
|
|
143
|
+
_e.maxFee = (0, number_1.toBN)((_b = transactionsDetail.maxFee) !== null && _b !== void 0 ? _b : '0'),
|
|
144
|
+
_e);
|
|
145
|
+
return [4 /*yield*/, this.signer.signTransaction(transactions, signerDetails, abis)];
|
|
167
146
|
case 4:
|
|
168
|
-
signature =
|
|
169
|
-
|
|
170
|
-
return [2 /*return*/,
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
calldataDecimal.length.toString()
|
|
177
|
-
], __read(calldataDecimal), false), [
|
|
178
|
-
nonceBn.toString(),
|
|
179
|
-
], false),
|
|
180
|
-
signature: signature,
|
|
147
|
+
signature = _f.sent();
|
|
148
|
+
calldata = __spreadArray(__spreadArray([], __read((0, transaction_1.fromCallsToExecuteCalldata)(transactions)), false), [signerDetails.nonce.toString()], false);
|
|
149
|
+
return [2 /*return*/, this.fetchEndpoint('add_transaction', undefined, {
|
|
150
|
+
type: 'INVOKE_FUNCTION',
|
|
151
|
+
contract_address: this.address,
|
|
152
|
+
entry_point_selector: (0, hash_1.getSelectorFromName)('__execute__'),
|
|
153
|
+
calldata: calldata,
|
|
154
|
+
signature: (0, number_1.bigNumberishArrayToDecimalStringArray)(signature),
|
|
181
155
|
})];
|
|
182
156
|
}
|
|
183
157
|
});
|
|
@@ -214,7 +188,7 @@ var Account = /** @class */ (function (_super) {
|
|
|
214
188
|
/**
|
|
215
189
|
* Verify a signature of a JSON object
|
|
216
190
|
*
|
|
217
|
-
* @param
|
|
191
|
+
* @param hash - JSON object to be verified
|
|
218
192
|
* @param signature - signature of the JSON object
|
|
219
193
|
* @returns true if the signature is valid, false otherwise
|
|
220
194
|
* @throws {Error} if the JSON object is not a valid JSON or the signature is not a valid signature
|
|
@@ -229,7 +203,7 @@ var Account = /** @class */ (function (_super) {
|
|
|
229
203
|
return [4 /*yield*/, this.callContract({
|
|
230
204
|
contractAddress: this.address,
|
|
231
205
|
entrypoint: 'is_valid_signature',
|
|
232
|
-
calldata: (0,
|
|
206
|
+
calldata: (0, stark_1.compileCalldata)({
|
|
233
207
|
hash: (0, number_1.toBN)(hash).toString(),
|
|
234
208
|
signature: signature.map(function (x) { return (0, number_1.toBN)(x).toString(); }),
|
|
235
209
|
}),
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ProviderInterface } from '../provider';
|
|
2
|
-
import { Abi, AddTransactionResponse,
|
|
2
|
+
import { Abi, AddTransactionResponse, Call, DeployContractPayload, InvocationsDetails, Signature } from '../types';
|
|
3
3
|
import { BigNumberish } from '../utils/number';
|
|
4
4
|
import { TypedData } from '../utils/typedData/types';
|
|
5
5
|
export declare abstract class AccountInterface extends ProviderInterface {
|
|
@@ -27,7 +27,7 @@ export declare abstract class AccountInterface extends ProviderInterface {
|
|
|
27
27
|
*
|
|
28
28
|
* @returns response from addTransaction
|
|
29
29
|
*/
|
|
30
|
-
abstract execute(transactions:
|
|
30
|
+
abstract execute(transactions: Call | Call[], abis?: Abi[], transactionsDetail?: InvocationsDetails): Promise<AddTransactionResponse>;
|
|
31
31
|
/**
|
|
32
32
|
* Sign an JSON object for off-chain usage with the starknet private key and return the signature
|
|
33
33
|
* This adds a message prefix so it cant be interchanged with transactions
|
package/dist/contract.d.ts
CHANGED
|
@@ -1,14 +1,17 @@
|
|
|
1
1
|
import { Provider } from './provider';
|
|
2
2
|
import { BlockIdentifier } from './provider/utils';
|
|
3
|
-
import { Abi,
|
|
3
|
+
import { Abi, Signature, StructAbi } from './types';
|
|
4
4
|
import { BigNumberish } from './utils/number';
|
|
5
|
+
export declare type Struct = {
|
|
6
|
+
type: 'struct';
|
|
7
|
+
[k: string]: BigNumberish;
|
|
8
|
+
};
|
|
9
|
+
export declare type ParsedStruct = {
|
|
10
|
+
[key: string]: BigNumberish | ParsedStruct;
|
|
11
|
+
};
|
|
5
12
|
export declare type Args = {
|
|
6
|
-
[inputName: string]:
|
|
7
|
-
type: 'struct';
|
|
8
|
-
[k: string]: BigNumberish;
|
|
9
|
-
};
|
|
13
|
+
[inputName: string]: BigNumberish | BigNumberish[] | ParsedStruct | ParsedStruct[];
|
|
10
14
|
};
|
|
11
|
-
export declare function compileCalldata(args: Args): RawCalldata;
|
|
12
15
|
export declare class Contract {
|
|
13
16
|
connectedTo: string | null;
|
|
14
17
|
abi: Abi;
|
|
@@ -23,9 +26,68 @@ export declare class Contract {
|
|
|
23
26
|
* @param address (optional) - address to connect to
|
|
24
27
|
*/
|
|
25
28
|
constructor(abi: Abi, address?: string | null, provider?: Provider);
|
|
29
|
+
/**
|
|
30
|
+
* Saves the address of the contract deployed on network that will be used for interaction
|
|
31
|
+
*
|
|
32
|
+
* @param address - address of the contract
|
|
33
|
+
* @returns Contract
|
|
34
|
+
*/
|
|
26
35
|
connect(address: string): Contract;
|
|
36
|
+
/**
|
|
37
|
+
* Validates if all arguments that are passed to the method are corresponding to the ones in the abi
|
|
38
|
+
*
|
|
39
|
+
* @param type - type of the method
|
|
40
|
+
* @param method - name of the method
|
|
41
|
+
* @param args - arguments that are passed to the method
|
|
42
|
+
*/
|
|
27
43
|
private validateMethodAndArgs;
|
|
44
|
+
/**
|
|
45
|
+
* Deep parse of the object that has been passed to the method
|
|
46
|
+
*
|
|
47
|
+
* @param element - element that needs to be parsed
|
|
48
|
+
* @param type - name of the method
|
|
49
|
+
* @return {string | string[]} - parsed arguments in format that contract is expecting
|
|
50
|
+
*/
|
|
51
|
+
private parseCalldataObject;
|
|
52
|
+
/**
|
|
53
|
+
* Parse of the response elements that are converted to Object (Struct) by using the abi
|
|
54
|
+
*
|
|
55
|
+
* @param responseIterator - iterator of the response
|
|
56
|
+
* @param type - type of the struct
|
|
57
|
+
* @return {BigNumberish | ParsedStruct} - parsed arguments in format that contract is expecting
|
|
58
|
+
*/
|
|
59
|
+
private parseResponseStruct;
|
|
60
|
+
/**
|
|
61
|
+
* Parse one field of the calldata by using input field from the abi for that method
|
|
62
|
+
*
|
|
63
|
+
* @param args - value of the field
|
|
64
|
+
* @param input - input(field) information from the abi that will be used to parse the data
|
|
65
|
+
* @return {string | string[]} - parsed arguments in format that contract is expecting
|
|
66
|
+
*/
|
|
67
|
+
private parsCalldataField;
|
|
68
|
+
/**
|
|
69
|
+
* Parse the calldata by using input fields from the abi for that method
|
|
70
|
+
*
|
|
71
|
+
* @param args - arguments passed the the method
|
|
72
|
+
* @param inputs - list of inputs(fields) that are in the abi
|
|
73
|
+
* @return {Calldata} - parsed arguments in format that contract is expecting
|
|
74
|
+
*/
|
|
75
|
+
private compileCalldata;
|
|
76
|
+
/**
|
|
77
|
+
* Parse elements of the response and structuring them into one field by using output property from the abi for that method
|
|
78
|
+
*
|
|
79
|
+
* @param responseIterator - iterator of the response
|
|
80
|
+
* @param output - output(field) information from the abi that will be used to parse the data
|
|
81
|
+
* @return - parsed response corresponding to the abi structure of the field
|
|
82
|
+
*/
|
|
28
83
|
private parseResponseField;
|
|
84
|
+
/**
|
|
85
|
+
* Parse elements of the response array and structuring them into response object
|
|
86
|
+
*
|
|
87
|
+
* @param method - method name
|
|
88
|
+
* @param response - response from the method
|
|
89
|
+
* @return - parsed response corresponding to the abi
|
|
90
|
+
*/
|
|
29
91
|
private parseResponse;
|
|
30
92
|
invoke(method: string, args?: Args, signature?: Signature): Promise<import("./types").AddTransactionResponse>;
|
|
31
93
|
call(method: string, args?: Args, blockIdentifier?: BlockIdentifier): Promise<Args>;
|