starknet 2.7.1 → 3.0.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.
Files changed (104) hide show
  1. package/.eslintrc +3 -1
  2. package/CHANGELOG.md +47 -0
  3. package/CONTRIBUTING.md +1 -1
  4. package/README.md +18 -16
  5. package/__mocks__/typedDataExample.json +35 -0
  6. package/__tests__/account.test.ts +52 -87
  7. package/__tests__/accountContract.test.ts +160 -0
  8. package/__tests__/contract.test.ts +3 -1
  9. package/__tests__/jest.setup.ts +9 -0
  10. package/__tests__/provider.test.ts +16 -33
  11. package/__tests__/utils/address.test.ts +16 -0
  12. package/__tests__/utils/typedData.test.ts +1 -36
  13. package/account/default.d.ts +66 -0
  14. package/account/default.js +440 -0
  15. package/account/index.d.ts +2 -0
  16. package/account/index.js +27 -0
  17. package/account/interface.d.ts +83 -0
  18. package/account/interface.js +37 -0
  19. package/constants.d.ts +2 -0
  20. package/constants.js +4 -0
  21. package/contract.d.ts +6 -6
  22. package/contract.js +16 -14
  23. package/dist/account/default.d.ts +55 -0
  24. package/dist/account/default.js +272 -0
  25. package/dist/account/index.d.ts +2 -0
  26. package/dist/account/index.js +14 -0
  27. package/dist/account/interface.d.ts +69 -0
  28. package/dist/account/interface.js +27 -0
  29. package/dist/constants.d.ts +2 -0
  30. package/dist/constants.js +3 -1
  31. package/dist/contract.d.ts +6 -6
  32. package/dist/contract.js +9 -12
  33. package/dist/index.d.ts +2 -1
  34. package/dist/index.js +2 -1
  35. package/dist/provider/default.d.ts +27 -16
  36. package/dist/provider/default.js +157 -100
  37. package/dist/provider/interface.d.ts +29 -32
  38. package/dist/provider/utils.d.ts +21 -5
  39. package/dist/provider/utils.js +53 -10
  40. package/dist/signer/default.d.ts +7 -31
  41. package/dist/signer/default.js +25 -121
  42. package/dist/signer/index.d.ts +1 -1
  43. package/dist/signer/index.js +1 -1
  44. package/dist/signer/interface.d.ts +17 -18
  45. package/dist/signer/interface.js +2 -20
  46. package/dist/{types.d.ts → types/api.d.ts} +72 -41
  47. package/dist/{types.js → types/api.js} +0 -0
  48. package/dist/types/index.d.ts +3 -0
  49. package/dist/types/index.js +15 -0
  50. package/dist/types/lib.d.ts +57 -0
  51. package/dist/types/lib.js +2 -0
  52. package/dist/types/signer.d.ts +4 -0
  53. package/dist/types/signer.js +2 -0
  54. package/dist/utils/address.d.ts +2 -0
  55. package/dist/utils/address.js +22 -0
  56. package/dist/utils/number.d.ts +1 -0
  57. package/dist/utils/number.js +5 -1
  58. package/index.d.ts +2 -1
  59. package/index.js +2 -1
  60. package/package.json +8 -2
  61. package/provider/default.d.ts +45 -36
  62. package/provider/default.js +216 -201
  63. package/provider/interface.d.ts +36 -49
  64. package/provider/utils.d.ts +23 -8
  65. package/provider/utils.js +57 -11
  66. package/signer/default.d.ts +11 -31
  67. package/signer/default.js +52 -169
  68. package/signer/index.d.ts +1 -1
  69. package/signer/index.js +1 -1
  70. package/signer/interface.d.ts +21 -18
  71. package/signer/interface.js +3 -32
  72. package/src/account/default.ts +152 -0
  73. package/src/account/index.ts +2 -0
  74. package/src/account/interface.ts +91 -0
  75. package/src/constants.ts +2 -0
  76. package/src/contract.ts +17 -18
  77. package/src/index.ts +2 -1
  78. package/src/provider/default.ts +141 -110
  79. package/src/provider/interface.ts +36 -52
  80. package/src/provider/utils.ts +60 -13
  81. package/src/signer/default.ts +33 -76
  82. package/src/signer/index.ts +1 -1
  83. package/src/signer/interface.ts +21 -20
  84. package/src/types/api.ts +165 -0
  85. package/src/types/index.ts +3 -0
  86. package/src/types/lib.ts +73 -0
  87. package/src/types/signer.ts +5 -0
  88. package/src/utils/address.ts +23 -0
  89. package/src/utils/number.ts +4 -0
  90. package/types/api.d.ts +152 -0
  91. package/{types.js → types/api.js} +0 -0
  92. package/types/index.d.ts +3 -0
  93. package/types/index.js +28 -0
  94. package/types/lib.d.ts +64 -0
  95. package/types/lib.js +2 -0
  96. package/types/signer.d.ts +4 -0
  97. package/types/signer.js +2 -0
  98. package/utils/address.d.ts +2 -0
  99. package/utils/address.js +22 -0
  100. package/utils/number.d.ts +3 -0
  101. package/utils/number.js +8 -1
  102. package/__tests__/signer.test.ts +0 -119
  103. package/src/types.ts +0 -131
  104. package/types.d.ts +0 -116
@@ -0,0 +1,37 @@
1
+ 'use strict';
2
+ var __extends =
3
+ (this && this.__extends) ||
4
+ (function () {
5
+ var extendStatics = function (d, b) {
6
+ extendStatics =
7
+ Object.setPrototypeOf ||
8
+ ({ __proto__: [] } instanceof Array &&
9
+ function (d, b) {
10
+ d.__proto__ = b;
11
+ }) ||
12
+ function (d, b) {
13
+ for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p];
14
+ };
15
+ return extendStatics(d, b);
16
+ };
17
+ return function (d, b) {
18
+ if (typeof b !== 'function' && b !== null)
19
+ throw new TypeError('Class extends value ' + String(b) + ' is not a constructor or null');
20
+ extendStatics(d, b);
21
+ function __() {
22
+ this.constructor = d;
23
+ }
24
+ d.prototype = b === null ? Object.create(b) : ((__.prototype = b.prototype), new __());
25
+ };
26
+ })();
27
+ Object.defineProperty(exports, '__esModule', { value: true });
28
+ exports.AccountInterface = void 0;
29
+ var provider_1 = require('../provider');
30
+ var AccountInterface = /** @class */ (function (_super) {
31
+ __extends(AccountInterface, _super);
32
+ function AccountInterface() {
33
+ return (_super !== null && _super.apply(this, arguments)) || this;
34
+ }
35
+ return AccountInterface;
36
+ })(provider_1.ProviderInterface);
37
+ exports.AccountInterface = AccountInterface;
package/constants.d.ts CHANGED
@@ -4,6 +4,7 @@ export declare const ZERO: import('bn.js');
4
4
  export declare const ONE: import('bn.js');
5
5
  export declare const TWO: import('bn.js');
6
6
  export declare const MASK_250: import('bn.js');
7
+ export declare const MASK_251: import('bn.js');
7
8
  /**
8
9
  * 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
9
10
  * Please do not edit until the JSON changes.
@@ -11,6 +12,7 @@ export declare const MASK_250: import('bn.js');
11
12
  export declare const FIELD_PRIME =
12
13
  '800000000000011000000000000000000000000000000000000000000000001';
13
14
  export declare const FIELD_GEN = '3';
15
+ export declare const FIELD_SIZE = 251;
14
16
  export declare const EC_ORDER = '800000000000010FFFFFFFFFFFFFFFFB781126DCAE7B2321E66A241ADC64D2F';
15
17
  export declare const ALPHA = '1';
16
18
  export declare const BETA = '6F21413EFBE40DE150E596D72F7A8C5609AD26C15C915C1F4CDFCB99CEE9E89';
package/constants.js CHANGED
@@ -5,8 +5,10 @@ exports.CONSTANT_POINTS =
5
5
  exports.BETA =
6
6
  exports.ALPHA =
7
7
  exports.EC_ORDER =
8
+ exports.FIELD_SIZE =
8
9
  exports.FIELD_GEN =
9
10
  exports.FIELD_PRIME =
11
+ exports.MASK_251 =
10
12
  exports.MASK_250 =
11
13
  exports.TWO =
12
14
  exports.ONE =
@@ -25,12 +27,14 @@ exports.ZERO = (0, number_1.toBN)(0);
25
27
  exports.ONE = (0, number_1.toBN)(1);
26
28
  exports.TWO = (0, number_1.toBN)(2);
27
29
  exports.MASK_250 = exports.TWO.pow((0, number_1.toBN)(250)).sub(exports.ONE); // 2 ** 250 - 1
30
+ exports.MASK_251 = exports.TWO.pow((0, number_1.toBN)(251));
28
31
  /**
29
32
  * 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
30
33
  * Please do not edit until the JSON changes.
31
34
  */
32
35
  exports.FIELD_PRIME = '800000000000011000000000000000000000000000000000000000000000001';
33
36
  exports.FIELD_GEN = '3';
37
+ exports.FIELD_SIZE = 251;
34
38
  exports.EC_ORDER = '800000000000010FFFFFFFFFFFFFFFFB781126DCAE7B2321E66A241ADC64D2F';
35
39
  exports.ALPHA = '1';
36
40
  exports.BETA = '6F21413EFBE40DE150E596D72F7A8C5609AD26C15C915C1F4CDFCB99CEE9E89';
package/contract.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { Provider } from './provider';
2
- import { Abi, Signature, StructAbi } from './types';
2
+ import { BlockIdentifier } from './provider/utils';
3
+ import { Abi, RawCalldata, Signature, StructAbi } from './types';
3
4
  import { BigNumberish } from './utils/number';
4
5
  export declare type Args = {
5
6
  [inputName: string]:
@@ -10,11 +11,10 @@ export declare type Args = {
10
11
  [k: string]: BigNumberish;
11
12
  };
12
13
  };
13
- export declare type Calldata = string[];
14
- export declare function compileCalldata(args: Args): Calldata;
14
+ export declare function compileCalldata(args: Args): RawCalldata;
15
15
  export declare class Contract {
16
16
  connectedTo: string | null;
17
- abi: Abi[];
17
+ abi: Abi;
18
18
  structs: {
19
19
  [name: string]: StructAbi;
20
20
  };
@@ -25,7 +25,7 @@ export declare class Contract {
25
25
  * @param abi - Abi of the contract object
26
26
  * @param address (optional) - address to connect to
27
27
  */
28
- constructor(abi: Abi[], address?: string | null, provider?: Provider);
28
+ constructor(abi: Abi, address?: string | null, provider?: Provider);
29
29
  connect(address: string): Contract;
30
30
  private validateMethodAndArgs;
31
31
  private parseResponseField;
@@ -35,5 +35,5 @@ export declare class Contract {
35
35
  args?: Args,
36
36
  signature?: Signature
37
37
  ): Promise<import('./types').AddTransactionResponse>;
38
- call(method: string, args?: Args): Promise<Args>;
38
+ call(method: string, args?: Args, blockIdentifier?: BlockIdentifier): Promise<Args>;
39
39
  }
package/contract.js CHANGED
@@ -188,7 +188,6 @@ exports.Contract = exports.compileCalldata = void 0;
188
188
  var minimalistic_assert_1 = __importDefault(require('minimalistic-assert'));
189
189
  var provider_1 = require('./provider');
190
190
  var number_1 = require('./utils/number');
191
- var stark_1 = require('./utils/stark');
192
191
  function parseFelt(candidate) {
193
192
  try {
194
193
  return (0, number_1.toBN)(candidate);
@@ -355,22 +354,23 @@ var Contract = /** @class */ (function () {
355
354
  // validate method and args
356
355
  this.validateMethodAndArgs('INVOKE', method, args);
357
356
  // compile calldata
358
- var entrypointSelector = (0, stark_1.getSelectorFromName)(method);
359
357
  var calldata = compileCalldata(args);
360
- return this.provider.addTransaction({
361
- type: 'INVOKE_FUNCTION',
362
- contract_address: this.connectedTo,
358
+ return this.provider.invokeFunction({
359
+ contractAddress: this.connectedTo,
363
360
  signature: signature,
364
361
  calldata: calldata,
365
- entry_point_selector: entrypointSelector,
362
+ entrypoint: method,
366
363
  });
367
364
  };
368
- Contract.prototype.call = function (method, args) {
365
+ Contract.prototype.call = function (method, args, blockIdentifier) {
369
366
  if (args === void 0) {
370
367
  args = {};
371
368
  }
369
+ if (blockIdentifier === void 0) {
370
+ blockIdentifier = null;
371
+ }
372
372
  return __awaiter(this, void 0, void 0, function () {
373
- var entrypointSelector, calldata;
373
+ var calldata;
374
374
  var _this = this;
375
375
  return __generator(this, function (_a) {
376
376
  // ensure contract is connected
@@ -378,16 +378,18 @@ var Contract = /** @class */ (function () {
378
378
  minimalistic_assert_1.default)(this.connectedTo !== null, 'contract isnt connected to an address');
379
379
  // validate method and args
380
380
  this.validateMethodAndArgs('CALL', method, args);
381
- entrypointSelector = (0, stark_1.getSelectorFromName)(method);
382
381
  calldata = compileCalldata(args);
383
382
  return [
384
383
  2 /*return*/,
385
384
  this.provider
386
- .callContract({
387
- contract_address: this.connectedTo,
388
- calldata: calldata,
389
- entry_point_selector: entrypointSelector,
390
- })
385
+ .callContract(
386
+ {
387
+ contractAddress: this.connectedTo,
388
+ entrypoint: method,
389
+ calldata: calldata,
390
+ },
391
+ blockIdentifier
392
+ )
391
393
  .then(function (x) {
392
394
  return _this.parseResponse(method, x.result);
393
395
  }),
@@ -0,0 +1,55 @@
1
+ import { Provider } from '../provider';
2
+ import { Abi, AddTransactionResponse, ExecuteInvocation, InvocationsDetails, KeyPair, Signature } from '../types';
3
+ import { BigNumberish } from '../utils/number';
4
+ import { TypedData } from '../utils/typedData';
5
+ import { AccountInterface } from './interface';
6
+ export declare class Account extends Provider implements AccountInterface {
7
+ address: string;
8
+ private signer;
9
+ constructor(provider: Provider, address: string, keyPair: KeyPair);
10
+ getNonce(): Promise<string>;
11
+ /**
12
+ * Invoke execute function in account contract
13
+ *
14
+ * [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/gateway/gateway_client.py#L13-L17)
15
+ *
16
+ * @param transaction - transaction to be invoked
17
+ * @returns a confirmation of invoking a function on the starknet contract
18
+ */
19
+ execute(transactions: ExecuteInvocation | ExecuteInvocation[], abis?: Abi[], transactionsDetail?: InvocationsDetails): Promise<AddTransactionResponse>;
20
+ /**
21
+ * Sign an JSON object with the starknet private key and return the signature
22
+ *
23
+ * @param json - JSON object to be signed
24
+ * @returns the signature of the JSON object
25
+ * @throws {Error} if the JSON object is not a valid JSON
26
+ */
27
+ signMessage(typedData: TypedData): Promise<Signature>;
28
+ /**
29
+ * Hash a JSON object with pederson hash and return the hash
30
+ *
31
+ * @param json - JSON object to be hashed
32
+ * @returns the hash of the JSON object
33
+ * @throws {Error} if the JSON object is not a valid JSON
34
+ */
35
+ hashMessage(typedData: TypedData): Promise<string>;
36
+ /**
37
+ * Verify a signature of a JSON object
38
+ *
39
+ * @param json - JSON object to be verified
40
+ * @param signature - signature of the JSON object
41
+ * @returns true if the signature is valid, false otherwise
42
+ * @throws {Error} if the JSON object is not a valid JSON or the signature is not a valid signature
43
+ */
44
+ verifyMessageHash(hash: BigNumberish, signature: Signature): Promise<boolean>;
45
+ /**
46
+ * Verify a signature of a given hash
47
+ * @warning This method is not recommended, use verifyMessage instead
48
+ *
49
+ * @param hash - hash to be verified
50
+ * @param signature - signature of the hash
51
+ * @returns true if the signature is valid, false otherwise
52
+ * @throws {Error} if the signature is not a valid signature
53
+ */
54
+ verifyMessage(typedData: TypedData, signature: Signature): Promise<boolean>;
55
+ }
@@ -0,0 +1,272 @@
1
+ "use strict";
2
+ var __extends = (this && this.__extends) || (function () {
3
+ var extendStatics = function (d, b) {
4
+ extendStatics = Object.setPrototypeOf ||
5
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
+ return extendStatics(d, b);
8
+ };
9
+ return function (d, b) {
10
+ if (typeof b !== "function" && b !== null)
11
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
+ extendStatics(d, b);
13
+ function __() { this.constructor = d; }
14
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
+ };
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
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
29
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
30
+ return new (P || (P = Promise))(function (resolve, reject) {
31
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
32
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
33
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
34
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
35
+ });
36
+ };
37
+ var __generator = (this && this.__generator) || function (thisArg, body) {
38
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
39
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
40
+ function verb(n) { return function (v) { return step([n, v]); }; }
41
+ function step(op) {
42
+ if (f) throw new TypeError("Generator is already executing.");
43
+ while (_) try {
44
+ 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;
45
+ if (y = 0, t) op = [op[0] & 2, t.value];
46
+ switch (op[0]) {
47
+ case 0: case 1: t = op; break;
48
+ case 4: _.label++; return { value: op[1], done: false };
49
+ case 5: _.label++; y = op[1]; op = [0]; continue;
50
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
51
+ default:
52
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
53
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
54
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
55
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
56
+ if (t[2]) _.ops.pop();
57
+ _.trys.pop(); continue;
58
+ }
59
+ op = body.call(thisArg, _);
60
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
61
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
62
+ }
63
+ };
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
+ var __read = (this && this.__read) || function (o, n) {
76
+ var m = typeof Symbol === "function" && o[Symbol.iterator];
77
+ if (!m) return o;
78
+ var i = m.call(o), r, ar = [], e;
79
+ try {
80
+ while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
81
+ }
82
+ catch (error) { e = { error: error }; }
83
+ finally {
84
+ try {
85
+ if (r && !r.done && (m = i["return"])) m.call(i);
86
+ }
87
+ finally { if (e) throw e.error; }
88
+ }
89
+ return ar;
90
+ };
91
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
92
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
93
+ if (ar || !(i in from)) {
94
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
95
+ ar[i] = from[i];
96
+ }
97
+ }
98
+ return to.concat(ar || Array.prototype.slice.call(from));
99
+ };
100
+ Object.defineProperty(exports, "__esModule", { value: true });
101
+ exports.Account = void 0;
102
+ var contract_1 = require("../contract");
103
+ var provider_1 = require("../provider");
104
+ var signer_1 = require("../signer");
105
+ var number_1 = require("../utils/number");
106
+ var stark_1 = require("../utils/stark");
107
+ var typedData_1 = require("../utils/typedData");
108
+ var Account = /** @class */ (function (_super) {
109
+ __extends(Account, _super);
110
+ function Account(provider, address, keyPair) {
111
+ var _this = _super.call(this, provider) || this;
112
+ _this.signer = new signer_1.Signer(keyPair);
113
+ _this.address = address;
114
+ return _this;
115
+ }
116
+ Account.prototype.getNonce = function () {
117
+ return __awaiter(this, void 0, void 0, function () {
118
+ var result;
119
+ return __generator(this, function (_a) {
120
+ switch (_a.label) {
121
+ case 0: return [4 /*yield*/, this.callContract({
122
+ contractAddress: this.address,
123
+ entrypoint: 'get_nonce',
124
+ })];
125
+ case 1:
126
+ result = (_a.sent()).result;
127
+ return [2 /*return*/, (0, number_1.toHex)((0, number_1.toBN)(result[0]))];
128
+ }
129
+ });
130
+ });
131
+ };
132
+ /**
133
+ * Invoke execute function in account contract
134
+ *
135
+ * [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/gateway/gateway_client.py#L13-L17)
136
+ *
137
+ * @param transaction - transaction to be invoked
138
+ * @returns a confirmation of invoking a function on the starknet contract
139
+ */
140
+ Account.prototype.execute = function (transactions, abis, transactionsDetail) {
141
+ if (abis === void 0) { abis = []; }
142
+ if (transactionsDetail === void 0) { transactionsDetail = {}; }
143
+ return __awaiter(this, void 0, void 0, function () {
144
+ var _a, contractAddress, _b, calldata, entrypoint, invocation, nonce, nonceBn, _c, _d, calldataDecimal, signature, entrypointSelector;
145
+ return __generator(this, function (_e) {
146
+ switch (_e.label) {
147
+ case 0:
148
+ if (Array.isArray(transactions) && transactions.length !== 1) {
149
+ throw new Error('Only one transaction at a time is currently supported');
150
+ }
151
+ _a = Array.isArray(transactions) ? transactions[0] : transactions, contractAddress = _a.contractAddress, _b = _a.calldata, calldata = _b === void 0 ? [] : _b, entrypoint = _a.entrypoint, invocation = __rest(_a, ["contractAddress", "calldata", "entrypoint"]);
152
+ nonce = transactionsDetail.nonce;
153
+ _c = number_1.toBN;
154
+ if (!(nonce !== null && nonce !== void 0)) return [3 /*break*/, 1];
155
+ _d = nonce;
156
+ return [3 /*break*/, 3];
157
+ case 1: return [4 /*yield*/, this.getNonce()];
158
+ case 2:
159
+ _d = (_e.sent());
160
+ _e.label = 3;
161
+ case 3:
162
+ nonceBn = _c.apply(void 0, [_d]);
163
+ calldataDecimal = (0, number_1.bigNumberishArrayToDecimalStringArray)(calldata);
164
+ return [4 /*yield*/, this.signer.signTransaction([
165
+ __assign(__assign({}, invocation), { contractAddress: contractAddress, calldata: calldataDecimal, entrypoint: entrypoint }),
166
+ ], { walletAddress: this.address, nonce: nonceBn }, abis)];
167
+ case 4:
168
+ signature = _e.sent();
169
+ entrypointSelector = (0, stark_1.getSelectorFromName)(entrypoint);
170
+ return [2 /*return*/, _super.prototype.invokeFunction.call(this, {
171
+ contractAddress: this.address,
172
+ entrypoint: 'execute',
173
+ calldata: __spreadArray(__spreadArray([
174
+ contractAddress,
175
+ entrypointSelector,
176
+ calldataDecimal.length.toString()
177
+ ], __read(calldataDecimal), false), [
178
+ nonceBn.toString(),
179
+ ], false),
180
+ signature: signature,
181
+ })];
182
+ }
183
+ });
184
+ });
185
+ };
186
+ /**
187
+ * Sign an JSON object with the starknet private key and return the signature
188
+ *
189
+ * @param json - JSON object to be signed
190
+ * @returns the signature of the JSON object
191
+ * @throws {Error} if the JSON object is not a valid JSON
192
+ */
193
+ Account.prototype.signMessage = function (typedData) {
194
+ return __awaiter(this, void 0, void 0, function () {
195
+ return __generator(this, function (_a) {
196
+ return [2 /*return*/, this.signer.signMessage(typedData, this.address)];
197
+ });
198
+ });
199
+ };
200
+ /**
201
+ * Hash a JSON object with pederson hash and return the hash
202
+ *
203
+ * @param json - JSON object to be hashed
204
+ * @returns the hash of the JSON object
205
+ * @throws {Error} if the JSON object is not a valid JSON
206
+ */
207
+ Account.prototype.hashMessage = function (typedData) {
208
+ return __awaiter(this, void 0, void 0, function () {
209
+ return __generator(this, function (_a) {
210
+ return [2 /*return*/, (0, typedData_1.getMessageHash)(typedData, this.address)];
211
+ });
212
+ });
213
+ };
214
+ /**
215
+ * Verify a signature of a JSON object
216
+ *
217
+ * @param json - JSON object to be verified
218
+ * @param signature - signature of the JSON object
219
+ * @returns true if the signature is valid, false otherwise
220
+ * @throws {Error} if the JSON object is not a valid JSON or the signature is not a valid signature
221
+ */
222
+ Account.prototype.verifyMessageHash = function (hash, signature) {
223
+ return __awaiter(this, void 0, void 0, function () {
224
+ var _a;
225
+ return __generator(this, function (_b) {
226
+ switch (_b.label) {
227
+ case 0:
228
+ _b.trys.push([0, 2, , 3]);
229
+ return [4 /*yield*/, this.callContract({
230
+ contractAddress: this.address,
231
+ entrypoint: 'is_valid_signature',
232
+ calldata: (0, contract_1.compileCalldata)({
233
+ hash: (0, number_1.toBN)(hash).toString(),
234
+ signature: signature.map(function (x) { return (0, number_1.toBN)(x).toString(); }),
235
+ }),
236
+ })];
237
+ case 1:
238
+ _b.sent();
239
+ return [2 /*return*/, true];
240
+ case 2:
241
+ _a = _b.sent();
242
+ return [2 /*return*/, false];
243
+ case 3: return [2 /*return*/];
244
+ }
245
+ });
246
+ });
247
+ };
248
+ /**
249
+ * Verify a signature of a given hash
250
+ * @warning This method is not recommended, use verifyMessage instead
251
+ *
252
+ * @param hash - hash to be verified
253
+ * @param signature - signature of the hash
254
+ * @returns true if the signature is valid, false otherwise
255
+ * @throws {Error} if the signature is not a valid signature
256
+ */
257
+ Account.prototype.verifyMessage = function (typedData, signature) {
258
+ return __awaiter(this, void 0, void 0, function () {
259
+ var hash;
260
+ return __generator(this, function (_a) {
261
+ switch (_a.label) {
262
+ case 0: return [4 /*yield*/, this.hashMessage(typedData)];
263
+ case 1:
264
+ hash = _a.sent();
265
+ return [2 /*return*/, this.verifyMessageHash(hash, signature)];
266
+ }
267
+ });
268
+ });
269
+ };
270
+ return Account;
271
+ }(provider_1.Provider));
272
+ exports.Account = Account;
@@ -0,0 +1,2 @@
1
+ export * from './default';
2
+ export * from './interface';
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11
+ };
12
+ Object.defineProperty(exports, "__esModule", { value: true });
13
+ __exportStar(require("./default"), exports);
14
+ __exportStar(require("./interface"), exports);
@@ -0,0 +1,69 @@
1
+ import { ProviderInterface } from '../provider';
2
+ import { Abi, AddTransactionResponse, DeployContractPayload, ExecuteInvocation, InvocationsDetails, Signature } from '../types';
3
+ import { BigNumberish } from '../utils/number';
4
+ import { TypedData } from '../utils/typedData/types';
5
+ export declare abstract class AccountInterface extends ProviderInterface {
6
+ abstract address: string;
7
+ /**
8
+ * Deploys a given compiled contract (json) to starknet
9
+ *
10
+ * @param payload payload to be deployed containing:
11
+ * - compiled contract code
12
+ * - constructor calldata
13
+ * - address salt
14
+ * @param abi the abi of the contract
15
+ * @returns a confirmation of sending a transaction on the starknet contract
16
+ */
17
+ abstract deployContract(payload: DeployContractPayload, abi?: Abi): Promise<AddTransactionResponse>;
18
+ /**
19
+ * Invoke execute function in account contract
20
+ *
21
+ * @param transactions the invocation object or an array of them, containing:
22
+ * - contractAddress - the address of the contract
23
+ * - entrypoint - the entrypoint of the contract
24
+ * - calldata - (defaults to []) the calldata
25
+ * - signature - (defaults to []) the signature
26
+ * @param abi (optional) the abi of the contract for better displaying
27
+ *
28
+ * @returns response from addTransaction
29
+ */
30
+ abstract execute(transactions: ExecuteInvocation | ExecuteInvocation[], abis?: Abi[], transactionsDetail?: InvocationsDetails): Promise<AddTransactionResponse>;
31
+ /**
32
+ * Sign an JSON object for off-chain usage with the starknet private key and return the signature
33
+ * This adds a message prefix so it cant be interchanged with transactions
34
+ *
35
+ * @param json - JSON object to be signed
36
+ * @returns the signature of the JSON object
37
+ * @throws {Error} if the JSON object is not a valid JSON
38
+ */
39
+ abstract signMessage(typedData: TypedData): Promise<Signature>;
40
+ /**
41
+ * Hash a JSON object with pederson hash and return the hash
42
+ * This adds a message prefix so it cant be interchanged with transactions
43
+ *
44
+ * @param json - JSON object to be hashed
45
+ * @returns the hash of the JSON object
46
+ * @throws {Error} if the JSON object is not a valid JSON
47
+ */
48
+ abstract hashMessage(typedData: TypedData): Promise<string>;
49
+ /**
50
+ * Verify a signature of a JSON object
51
+ *
52
+ * @param json - JSON object to be verified
53
+ * @param signature - signature of the JSON object
54
+ * @returns true if the signature is valid, false otherwise
55
+ * @throws {Error} if the JSON object is not a valid JSON or the signature is not a valid signature
56
+ */
57
+ abstract verifyMessage(typedData: TypedData, signature: Signature): Promise<boolean>;
58
+ /**
59
+ * Verify a signature of a given hash
60
+ * @warning This method is not recommended, use verifyMessage instead
61
+ *
62
+ * @param hash - hash to be verified
63
+ * @param signature - signature of the hash
64
+ * @returns true if the signature is valid, false otherwise
65
+ * @throws {Error} if the signature is not a valid signature
66
+ */
67
+ abstract verifyMessageHash(hash: BigNumberish, signature: Signature): Promise<boolean>;
68
+ abstract getNonce(): Promise<string>;
69
+ }
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+ var __extends = (this && this.__extends) || (function () {
3
+ var extendStatics = function (d, b) {
4
+ extendStatics = Object.setPrototypeOf ||
5
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
+ return extendStatics(d, b);
8
+ };
9
+ return function (d, b) {
10
+ if (typeof b !== "function" && b !== null)
11
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
+ extendStatics(d, b);
13
+ function __() { this.constructor = d; }
14
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
+ };
16
+ })();
17
+ Object.defineProperty(exports, "__esModule", { value: true });
18
+ exports.AccountInterface = void 0;
19
+ var provider_1 = require("../provider");
20
+ var AccountInterface = /** @class */ (function (_super) {
21
+ __extends(AccountInterface, _super);
22
+ function AccountInterface() {
23
+ return _super !== null && _super.apply(this, arguments) || this;
24
+ }
25
+ return AccountInterface;
26
+ }(provider_1.ProviderInterface));
27
+ exports.AccountInterface = AccountInterface;
@@ -4,12 +4,14 @@ export declare const ZERO: import("bn.js");
4
4
  export declare const ONE: import("bn.js");
5
5
  export declare const TWO: import("bn.js");
6
6
  export declare const MASK_250: import("bn.js");
7
+ export declare const MASK_251: import("bn.js");
7
8
  /**
8
9
  * 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
9
10
  * Please do not edit until the JSON changes.
10
11
  */
11
12
  export declare const FIELD_PRIME = "800000000000011000000000000000000000000000000000000000000000001";
12
13
  export declare const FIELD_GEN = "3";
14
+ export declare const FIELD_SIZE = 251;
13
15
  export declare const EC_ORDER = "800000000000010FFFFFFFFFFFFFFFFB781126DCAE7B2321E66A241ADC64D2F";
14
16
  export declare const ALPHA = "1";
15
17
  export declare const BETA = "6F21413EFBE40DE150E596D72F7A8C5609AD26C15C915C1F4CDFCB99CEE9E89";
package/dist/constants.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.CONSTANT_POINTS = exports.MAX_ECDSA_VAL = exports.BETA = exports.ALPHA = exports.EC_ORDER = exports.FIELD_GEN = exports.FIELD_PRIME = exports.MASK_250 = exports.TWO = exports.ONE = exports.ZERO = exports.IS_BROWSER = void 0;
3
+ exports.CONSTANT_POINTS = exports.MAX_ECDSA_VAL = exports.BETA = exports.ALPHA = exports.EC_ORDER = exports.FIELD_SIZE = exports.FIELD_GEN = exports.FIELD_PRIME = exports.MASK_251 = exports.MASK_250 = exports.TWO = exports.ONE = exports.ZERO = exports.IS_BROWSER = void 0;
4
4
  var number_1 = require("./utils/number");
5
5
  var encode_1 = require("./utils/encode");
6
6
  Object.defineProperty(exports, "IS_BROWSER", { enumerable: true, get: function () { return encode_1.IS_BROWSER; } });
@@ -8,12 +8,14 @@ exports.ZERO = (0, number_1.toBN)(0);
8
8
  exports.ONE = (0, number_1.toBN)(1);
9
9
  exports.TWO = (0, number_1.toBN)(2);
10
10
  exports.MASK_250 = exports.TWO.pow((0, number_1.toBN)(250)).sub(exports.ONE); // 2 ** 250 - 1
11
+ exports.MASK_251 = exports.TWO.pow((0, number_1.toBN)(251));
11
12
  /**
12
13
  * 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
13
14
  * Please do not edit until the JSON changes.
14
15
  */
15
16
  exports.FIELD_PRIME = '800000000000011000000000000000000000000000000000000000000000001';
16
17
  exports.FIELD_GEN = '3';
18
+ exports.FIELD_SIZE = 251;
17
19
  exports.EC_ORDER = '800000000000010FFFFFFFFFFFFFFFFB781126DCAE7B2321E66A241ADC64D2F';
18
20
  exports.ALPHA = '1';
19
21
  exports.BETA = '6F21413EFBE40DE150E596D72F7A8C5609AD26C15C915C1F4CDFCB99CEE9E89';