starknet 2.2.0 → 2.5.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 (73) hide show
  1. package/CHANGELOG.md +38 -0
  2. package/__tests__/account.test.ts +3 -3
  3. package/__tests__/signer.test.ts +17 -0
  4. package/__tests__/utils/ellipticalCurve.test.ts +1 -1
  5. package/__tests__/utils/shortString.test.ts +22 -0
  6. package/__tests__/utils/typedData.test.ts +72 -0
  7. package/__tests__/utils/uint256.test.ts +32 -0
  8. package/constants.d.ts +5 -5
  9. package/contract.d.ts +9 -3
  10. package/contract.js +23 -5
  11. package/dist/constants.d.ts +5 -5
  12. package/dist/contract.d.ts +6 -3
  13. package/dist/contract.js +19 -5
  14. package/dist/index.d.ts +3 -0
  15. package/dist/index.js +4 -1
  16. package/dist/provider/default.d.ts +6 -6
  17. package/dist/provider/default.js +18 -17
  18. package/dist/provider/interface.d.ts +6 -6
  19. package/dist/signer/default.d.ts +20 -3
  20. package/dist/signer/default.js +61 -20
  21. package/dist/signer/interface.d.ts +22 -3
  22. package/dist/types.d.ts +7 -5
  23. package/dist/utils/ellipticCurve.d.ts +8 -1
  24. package/dist/utils/ellipticCurve.js +48 -9
  25. package/dist/utils/shortString.d.ts +4 -0
  26. package/dist/utils/shortString.js +26 -0
  27. package/dist/utils/stark.d.ts +2 -3
  28. package/dist/utils/typedData/index.d.ts +91 -0
  29. package/dist/utils/typedData/index.js +183 -0
  30. package/dist/utils/typedData/types.d.ts +82 -0
  31. package/dist/utils/typedData/types.js +47 -0
  32. package/dist/utils/typedData/utils.d.ts +24 -0
  33. package/dist/utils/typedData/utils.js +15 -0
  34. package/dist/utils/uint256.d.ts +11 -0
  35. package/dist/utils/uint256.js +28 -0
  36. package/index.d.ts +3 -0
  37. package/index.js +7 -1
  38. package/package.json +3 -1
  39. package/provider/default.d.ts +9 -5
  40. package/provider/default.js +21 -19
  41. package/provider/interface.d.ts +6 -5
  42. package/signer/default.d.ts +20 -3
  43. package/signer/default.js +60 -17
  44. package/signer/interface.d.ts +22 -3
  45. package/src/constants.ts +4 -6
  46. package/src/contract.ts +17 -14
  47. package/src/index.ts +3 -0
  48. package/src/provider/default.ts +15 -13
  49. package/src/provider/interface.ts +6 -5
  50. package/src/signer/default.ts +49 -17
  51. package/src/signer/interface.ts +26 -3
  52. package/src/types.ts +13 -5
  53. package/src/utils/ellipticCurve.ts +31 -9
  54. package/src/utils/shortString.ts +21 -0
  55. package/src/utils/stark.ts +4 -4
  56. package/src/utils/typedData/index.ts +176 -0
  57. package/src/utils/typedData/types.ts +82 -0
  58. package/src/utils/typedData/utils.ts +13 -0
  59. package/src/utils/uint256.ts +32 -0
  60. package/types.d.ts +9 -6
  61. package/utils/ellipticCurve.d.ts +12 -1
  62. package/utils/ellipticCurve.js +72 -23
  63. package/utils/shortString.d.ts +4 -0
  64. package/utils/shortString.js +34 -0
  65. package/utils/stark.d.ts +2 -3
  66. package/utils/typedData/index.d.ts +113 -0
  67. package/utils/typedData/index.js +247 -0
  68. package/utils/typedData/types.d.ts +103 -0
  69. package/utils/typedData/types.js +57 -0
  70. package/utils/typedData/utils.d.ts +27 -0
  71. package/utils/typedData/utils.js +15 -0
  72. package/utils/uint256.d.ts +11 -0
  73. package/utils/uint256.js +38 -0
@@ -0,0 +1,82 @@
1
+ import { Infer } from 'superstruct';
2
+ export declare const ATOMIC_TYPES: string[];
3
+ /**
4
+ * Checks if a type is valid with the given `typedData`. The following types are valid:
5
+ * - Atomic types: felt, felt*
6
+ * - Reference types: struct type (e.g. SomeStruct)
7
+ *
8
+ * @param {Record<string, unknown>} types
9
+ * @param {string} type
10
+ * @return {boolean}
11
+ */
12
+ export declare const isValidType: (types: Record<string, unknown>, type: string) => boolean;
13
+ export declare const STARKNET_TYPE: import("superstruct").Struct<{
14
+ type: string;
15
+ name: string;
16
+ }, {
17
+ name: import("superstruct").Struct<string, null>;
18
+ type: import("superstruct").Struct<string, null>;
19
+ }>;
20
+ /**
21
+ * A single type, as part of a struct. The `type` field can be any of the EIP-712 supported types.
22
+ *
23
+ * Note that the `uint` and `int` aliases like in Solidity, and fixed point numbers are not supported by the EIP-712
24
+ * standard.
25
+ */
26
+ export declare type StarkNetType = Infer<typeof STARKNET_TYPE>;
27
+ export declare const STARKNET_DOMAIN_TYPE: import("superstruct").Struct<{
28
+ name?: string | undefined;
29
+ version?: string | undefined;
30
+ chainId?: string | number | undefined;
31
+ }, {
32
+ name: import("superstruct").Struct<string | undefined, null>;
33
+ version: import("superstruct").Struct<string | undefined, null>;
34
+ chainId: import("superstruct").Struct<string | number | undefined, null>;
35
+ }>;
36
+ /**
37
+ * The EIP712 domain struct. Any of these fields are optional, but it must contain at least one field.
38
+ */
39
+ export declare type StarkNetDomain = Infer<typeof STARKNET_DOMAIN_TYPE>;
40
+ export declare const STARKNET_TYPED_DATA_TYPE: import("superstruct").Struct<{
41
+ types: {
42
+ StarkNetDomain: {
43
+ type: string;
44
+ name: string;
45
+ }[];
46
+ } & Record<string, {
47
+ type: string;
48
+ name: string;
49
+ }[]>;
50
+ primaryType: string;
51
+ domain: {
52
+ name?: string | undefined;
53
+ version?: string | undefined;
54
+ chainId?: string | number | undefined;
55
+ };
56
+ message: Record<string, unknown>;
57
+ }, {
58
+ types: import("superstruct").Struct<{
59
+ StarkNetDomain: {
60
+ type: string;
61
+ name: string;
62
+ }[];
63
+ } & Record<string, {
64
+ type: string;
65
+ name: string;
66
+ }[]>, null>;
67
+ primaryType: import("superstruct").Struct<string, null>;
68
+ domain: import("superstruct").Struct<{
69
+ name?: string | undefined;
70
+ version?: string | undefined;
71
+ chainId?: string | number | undefined;
72
+ }, {
73
+ name: import("superstruct").Struct<string | undefined, null>;
74
+ version: import("superstruct").Struct<string | undefined, null>;
75
+ chainId: import("superstruct").Struct<string | number | undefined, null>;
76
+ }>;
77
+ message: import("superstruct").Struct<Record<string, unknown>, null>;
78
+ }>;
79
+ /**
80
+ * The complete typed data, with all the structs, domain data, primary type of the message, and the message itself.
81
+ */
82
+ export declare type TypedData = Infer<typeof STARKNET_TYPED_DATA_TYPE>;
@@ -0,0 +1,47 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.STARKNET_TYPED_DATA_TYPE = exports.STARKNET_DOMAIN_TYPE = exports.STARKNET_TYPE = exports.isValidType = exports.ATOMIC_TYPES = void 0;
4
+ var superstruct_1 = require("superstruct");
5
+ exports.ATOMIC_TYPES = ['felt', 'felt*'];
6
+ // Source: https://github.com/Mrtenz/eip-712/blob/master/src/eip-712.ts
7
+ // and modified to support starknet types
8
+ /**
9
+ * Checks if a type is valid with the given `typedData`. The following types are valid:
10
+ * - Atomic types: felt, felt*
11
+ * - Reference types: struct type (e.g. SomeStruct)
12
+ *
13
+ * @param {Record<string, unknown>} types
14
+ * @param {string} type
15
+ * @return {boolean}
16
+ */
17
+ var isValidType = function (types, type) {
18
+ if (exports.ATOMIC_TYPES.includes(type)) {
19
+ return true;
20
+ }
21
+ if (types[type]) {
22
+ return true;
23
+ }
24
+ return false;
25
+ };
26
+ exports.isValidType = isValidType;
27
+ var TYPE = (0, superstruct_1.refine)((0, superstruct_1.string)(), 'Type', function (type, context) {
28
+ return (0, exports.isValidType)(context.branch[0].types, type);
29
+ });
30
+ exports.STARKNET_TYPE = (0, superstruct_1.object)({
31
+ name: (0, superstruct_1.string)(),
32
+ type: TYPE,
33
+ });
34
+ exports.STARKNET_DOMAIN_TYPE = (0, superstruct_1.object)({
35
+ name: (0, superstruct_1.optional)((0, superstruct_1.string)()),
36
+ version: (0, superstruct_1.optional)((0, superstruct_1.string)()),
37
+ chainId: (0, superstruct_1.optional)((0, superstruct_1.union)([(0, superstruct_1.string)(), (0, superstruct_1.number)()])),
38
+ });
39
+ exports.STARKNET_TYPED_DATA_TYPE = (0, superstruct_1.object)({
40
+ types: (0, superstruct_1.intersection)([
41
+ (0, superstruct_1.type)({ StarkNetDomain: (0, superstruct_1.array)(exports.STARKNET_TYPE) }),
42
+ (0, superstruct_1.record)((0, superstruct_1.string)(), (0, superstruct_1.array)(exports.STARKNET_TYPE)),
43
+ ]),
44
+ primaryType: (0, superstruct_1.string)(),
45
+ domain: exports.STARKNET_DOMAIN_TYPE,
46
+ message: (0, superstruct_1.object)(),
47
+ });
@@ -0,0 +1,24 @@
1
+ /**
2
+ * Validates that `data` matches the EIP-712 JSON schema.
3
+ *
4
+ * @param {any} data
5
+ * @return {boolean}
6
+ */
7
+ export declare const validateTypedData: (data: unknown) => data is {
8
+ types: {
9
+ StarkNetDomain: {
10
+ type: string;
11
+ name: string;
12
+ }[];
13
+ } & Record<string, {
14
+ type: string;
15
+ name: string;
16
+ }[]>;
17
+ primaryType: string;
18
+ domain: {
19
+ name?: string | undefined;
20
+ version?: string | undefined;
21
+ chainId?: string | number | undefined;
22
+ };
23
+ message: Record<string, unknown>;
24
+ };
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.validateTypedData = void 0;
4
+ var superstruct_1 = require("superstruct");
5
+ var types_1 = require("./types");
6
+ /**
7
+ * Validates that `data` matches the EIP-712 JSON schema.
8
+ *
9
+ * @param {any} data
10
+ * @return {boolean}
11
+ */
12
+ var validateTypedData = function (data) {
13
+ return (0, superstruct_1.is)(data, types_1.STARKNET_TYPED_DATA_TYPE);
14
+ };
15
+ exports.validateTypedData = validateTypedData;
@@ -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;
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.bnToUint256 = exports.isUint256 = exports.UINT_256_MAX = exports.UINT_128_MAX = exports.uint256ToBN = void 0;
4
+ var encode_1 = require("./encode");
5
+ var number_1 = require("./number");
6
+ // function to convert Uint256 to BN
7
+ function uint256ToBN(uint256) {
8
+ return (0, number_1.toBN)(uint256.high).shln(128).add((0, number_1.toBN)(uint256.low));
9
+ }
10
+ exports.uint256ToBN = uint256ToBN;
11
+ exports.UINT_128_MAX = (0, number_1.toBN)(1).shln(128).sub((0, number_1.toBN)(1));
12
+ exports.UINT_256_MAX = (0, number_1.toBN)(1).shln(256).sub((0, number_1.toBN)(1));
13
+ // function to check if BN is smaller or equal 2**256-1
14
+ function isUint256(bn) {
15
+ return (0, number_1.toBN)(bn).lte(exports.UINT_256_MAX);
16
+ }
17
+ exports.isUint256 = isUint256;
18
+ // function to convert BN to Uint256
19
+ function bnToUint256(bignumber) {
20
+ var bn = (0, number_1.toBN)(bignumber);
21
+ if (!isUint256(bn))
22
+ throw new Error('Number is too large');
23
+ return {
24
+ low: (0, encode_1.addHexPrefix)(bn.maskn(128).toString(16)),
25
+ high: (0, encode_1.addHexPrefix)(bn.shrn(128).toString(16)),
26
+ };
27
+ }
28
+ exports.bnToUint256 = bnToUint256;
package/index.d.ts CHANGED
@@ -15,3 +15,6 @@ export * as json from './utils/json';
15
15
  export * as number from './utils/number';
16
16
  export * as stark from './utils/stark';
17
17
  export * as ec from './utils/ellipticCurve';
18
+ export * as uint256 from './utils/uint256';
19
+ export * as shortString from './utils/shortString';
20
+ export * as typedData from './utils/typedData';
package/index.js CHANGED
@@ -44,7 +44,10 @@ var __importStar =
44
44
  return result;
45
45
  };
46
46
  Object.defineProperty(exports, '__esModule', { value: true });
47
- exports.ec =
47
+ exports.typedData =
48
+ exports.shortString =
49
+ exports.uint256 =
50
+ exports.ec =
48
51
  exports.stark =
49
52
  exports.number =
50
53
  exports.json =
@@ -69,3 +72,6 @@ exports.json = __importStar(require('./utils/json'));
69
72
  exports.number = __importStar(require('./utils/number'));
70
73
  exports.stark = __importStar(require('./utils/stark'));
71
74
  exports.ec = __importStar(require('./utils/ellipticCurve'));
75
+ exports.uint256 = __importStar(require('./utils/uint256'));
76
+ exports.shortString = __importStar(require('./utils/shortString'));
77
+ exports.typedData = __importStar(require('./utils/typedData'));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "starknet",
3
- "version": "2.2.0",
3
+ "version": "2.5.0",
4
4
  "description": "JavaScript library for StarkNet",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -23,6 +23,7 @@
23
23
  "zk",
24
24
  "rollup"
25
25
  ],
26
+ "repository": "github:seanjameshan/starknet.js",
26
27
  "author": "Sean Han",
27
28
  "license": "MIT",
28
29
  "devDependencies": {
@@ -67,6 +68,7 @@
67
68
  "json-bigint": "^1.0.0",
68
69
  "minimalistic-assert": "^1.0.1",
69
70
  "pako": "^2.0.4",
71
+ "superstruct": "^0.15.3",
70
72
  "url-join": "^4.0.1"
71
73
  },
72
74
  "lint-staged": {
@@ -8,6 +8,7 @@ import {
8
8
  GetContractAddressesResponse,
9
9
  GetTransactionResponse,
10
10
  GetTransactionStatusResponse,
11
+ Signature,
11
12
  Transaction,
12
13
  } from '../types';
13
14
  import { BigNumberish } from '../utils/number';
@@ -40,11 +41,14 @@ export declare class Provider implements ProviderInterface {
40
41
  *
41
42
  * [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L17-L25)
42
43
  *
43
- * @param invokeTx - transaction to be invoked
44
+ * @param invokeTransaction - transaction to be invoked
44
45
  * @param blockId
45
46
  * @returns the result of the function on the smart contract.
46
47
  */
47
- callContract(invokeTx: CallContractTransaction, blockId?: number): Promise<CallContractResponse>;
48
+ callContract(
49
+ invokeTransaction: CallContractTransaction,
50
+ blockId?: number
51
+ ): Promise<CallContractResponse>;
48
52
  /**
49
53
  * Gets the block information from a block ID.
50
54
  *
@@ -98,10 +102,10 @@ export declare class Provider implements ProviderInterface {
98
102
  *
99
103
  * [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/gateway/gateway_client.py#L13-L17)
100
104
  *
101
- * @param tx - transaction to be invoked
105
+ * @param transaction - transaction to be invoked
102
106
  * @returns a confirmation of invoking a function on the starknet contract
103
107
  */
104
- addTransaction(tx: Transaction): Promise<AddTransactionResponse>;
108
+ addTransaction(transaction: Transaction): Promise<AddTransactionResponse>;
105
109
  /**
106
110
  * Deploys a given compiled contract (json) to starknet
107
111
  *
@@ -127,7 +131,7 @@ export declare class Provider implements ProviderInterface {
127
131
  contractAddress: string,
128
132
  entrypointSelector: string,
129
133
  calldata?: string[],
130
- signature?: [BigNumberish, BigNumberish]
134
+ signature?: Signature
131
135
  ): Promise<AddTransactionResponse>;
132
136
  waitForTx(txHash: BigNumberish, retryInterval?: number): Promise<void>;
133
137
  }
@@ -219,11 +219,11 @@ var Provider = /** @class */ (function () {
219
219
  *
220
220
  * [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L17-L25)
221
221
  *
222
- * @param invokeTx - transaction to be invoked
222
+ * @param invokeTransaction - transaction to be invoked
223
223
  * @param blockId
224
224
  * @returns the result of the function on the smart contract.
225
225
  */
226
- Provider.prototype.callContract = function (invokeTx, blockId) {
226
+ Provider.prototype.callContract = function (invokeTransaction, blockId) {
227
227
  return __awaiter(this, void 0, void 0, function () {
228
228
  var data;
229
229
  return __generator(this, function (_a) {
@@ -237,7 +237,7 @@ var Provider = /** @class */ (function () {
237
237
  'call_contract',
238
238
  '?blockId=' + (blockId !== null && blockId !== void 0 ? blockId : 'null')
239
239
  ),
240
- __assign({ signature: [], calldata: [] }, invokeTx)
240
+ __assign({ signature: [], calldata: [] }, invokeTransaction)
241
241
  ),
242
242
  ];
243
243
  case 1:
@@ -421,19 +421,21 @@ var Provider = /** @class */ (function () {
421
421
  *
422
422
  * [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/gateway/gateway_client.py#L13-L17)
423
423
  *
424
- * @param tx - transaction to be invoked
424
+ * @param transaction - transaction to be invoked
425
425
  * @returns a confirmation of invoking a function on the starknet contract
426
426
  */
427
- Provider.prototype.addTransaction = function (tx) {
427
+ Provider.prototype.addTransaction = function (transaction) {
428
428
  return __awaiter(this, void 0, void 0, function () {
429
429
  var signature, contract_address_salt, data;
430
430
  return __generator(this, function (_a) {
431
431
  switch (_a.label) {
432
432
  case 0:
433
- signature = tx.type === 'INVOKE_FUNCTION' && (0, stark_1.formatSignature)(tx.signature);
433
+ signature =
434
+ transaction.type === 'INVOKE_FUNCTION' &&
435
+ (0, stark_1.formatSignature)(transaction.signature);
434
436
  contract_address_salt =
435
- tx.type === 'DEPLOY' &&
436
- (0, number_1.toHex)((0, number_1.toBN)(tx.contract_address_salt));
437
+ transaction.type === 'DEPLOY' &&
438
+ (0, number_1.toHex)((0, number_1.toBN)(transaction.contract_address_salt));
437
439
  return [
438
440
  4 /*yield*/,
439
441
  axios_1.default.post(
@@ -441,7 +443,7 @@ var Provider = /** @class */ (function () {
441
443
  (0, json_1.stringify)(
442
444
  __assign(
443
445
  __assign(
444
- __assign({}, tx),
446
+ __assign({}, transaction),
445
447
  Array.isArray(signature) && { signature: signature }
446
448
  ),
447
449
  contract_address_salt && { contract_address_salt: contract_address_salt }
@@ -515,29 +517,29 @@ var Provider = /** @class */ (function () {
515
517
  switch (_a.label) {
516
518
  case 0:
517
519
  onchain = false;
518
- _a.label = 1;
520
+ return [4 /*yield*/, wait(retryInterval)];
519
521
  case 1:
520
- if (!!onchain) return [3 /*break*/, 4];
522
+ _a.sent();
523
+ _a.label = 2;
524
+ case 2:
525
+ if (!!onchain) return [3 /*break*/, 5];
521
526
  // eslint-disable-next-line no-await-in-loop
522
527
  return [4 /*yield*/, wait(retryInterval)];
523
- case 2:
528
+ case 3:
524
529
  // eslint-disable-next-line no-await-in-loop
525
530
  _a.sent();
526
531
  return [4 /*yield*/, this.getTransactionStatus(txHash)];
527
- case 3:
532
+ case 4:
528
533
  res = _a.sent();
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
- ) {
534
+ if (res.tx_status === 'ACCEPTED_ON_L1' || res.tx_status === 'ACCEPTED_ON_L2') {
533
535
  onchain = true;
534
536
  } else if (res.tx_status === 'REJECTED') {
535
537
  throw Error('REJECTED');
536
538
  } else if (res.tx_status === 'NOT_RECEIVED') {
537
539
  throw Error('NOT_RECEIVED');
538
540
  }
539
- return [3 /*break*/, 1];
540
- case 4:
541
+ return [3 /*break*/, 2];
542
+ case 5:
541
543
  return [2 /*return*/];
542
544
  }
543
545
  });
@@ -8,6 +8,7 @@ import type {
8
8
  GetContractAddressesResponse,
9
9
  GetTransactionResponse,
10
10
  GetTransactionStatusResponse,
11
+ Signature,
11
12
  Transaction,
12
13
  } from '../types';
13
14
  import type { BigNumberish } from '../utils/number';
@@ -27,12 +28,12 @@ export declare abstract class ProviderInterface {
27
28
  *
28
29
  * [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L17-L25)
29
30
  *
30
- * @param invokeTx - transaction to be invoked
31
+ * @param invokeTransaction - transaction to be invoked
31
32
  * @param blockId
32
33
  * @returns the result of the function on the smart contract.
33
34
  */
34
35
  abstract callContract(
35
- invokeTx: CallContractTransaction,
36
+ invokeTransaction: CallContractTransaction,
36
37
  blockId?: number
37
38
  ): Promise<CallContractResponse>;
38
39
  /**
@@ -88,10 +89,10 @@ export declare abstract class ProviderInterface {
88
89
  *
89
90
  * [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/gateway/gateway_client.py#L13-L17)
90
91
  *
91
- * @param tx - transaction to be invoked
92
+ * @param transaction - transaction to be invoked
92
93
  * @returns a confirmation of invoking a function on the starknet contract
93
94
  */
94
- abstract addTransaction(tx: Transaction): Promise<AddTransactionResponse>;
95
+ abstract addTransaction(transaction: Transaction): Promise<AddTransactionResponse>;
95
96
  /**
96
97
  * Deploys a given compiled contract (json) to starknet
97
98
  *
@@ -117,7 +118,7 @@ export declare abstract class ProviderInterface {
117
118
  contractAddress: string,
118
119
  entrypointSelector: string,
119
120
  calldata?: string[],
120
- signature?: [BigNumberish, BigNumberish]
121
+ signature?: Signature
121
122
  ): Promise<AddTransactionResponse>;
122
123
  abstract waitForTx(txHash: BigNumberish, retryInterval?: number): Promise<void>;
123
124
  }
@@ -1,5 +1,6 @@
1
1
  import { Provider } from '../provider';
2
- import { AddTransactionResponse, KeyPair, Transaction } from '../types';
2
+ import { AddTransactionResponse, KeyPair, Signature, Transaction } from '../types';
3
+ import { TypedData } from '../utils/typedData';
3
4
  import { SignerInterface } from './interface';
4
5
  export declare class Signer extends Provider implements SignerInterface {
5
6
  address: string;
@@ -10,8 +11,24 @@ export declare class Signer extends Provider implements SignerInterface {
10
11
  *
11
12
  * [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/gateway/gateway_client.py#L13-L17)
12
13
  *
13
- * @param tx - transaction to be invoked
14
+ * @param transaction - transaction to be invoked
14
15
  * @returns a confirmation of invoking a function on the starknet contract
15
16
  */
16
- addTransaction(tx: Transaction): Promise<AddTransactionResponse>;
17
+ addTransaction(transaction: Transaction): Promise<AddTransactionResponse>;
18
+ /**
19
+ * Sign an JSON object with the starknet private key and return the signature
20
+ *
21
+ * @param json - JSON object to be signed
22
+ * @returns the signature of the JSON object
23
+ * @throws {Error} if the JSON object is not a valid JSON
24
+ */
25
+ signMessage(typedData: TypedData): Promise<Signature>;
26
+ /**
27
+ * Hash a JSON object with pederson hash and return the hash
28
+ *
29
+ * @param json - JSON object to be hashed
30
+ * @returns the hash of the JSON object
31
+ * @throws {Error} if the JSON object is not a valid JSON
32
+ */
33
+ hashMessage(typedData: TypedData): Promise<string>;
17
34
  }
package/signer/default.js CHANGED
@@ -203,6 +203,7 @@ var encode_1 = require('../utils/encode');
203
203
  var hash_1 = require('../utils/hash');
204
204
  var number_1 = require('../utils/number');
205
205
  var stark_1 = require('../utils/stark');
206
+ var typedData_1 = require('../utils/typedData');
206
207
  var Signer = /** @class */ (function (_super) {
207
208
  __extends(Signer, _super);
208
209
  function Signer(provider, address, keyPair) {
@@ -216,19 +217,23 @@ var Signer = /** @class */ (function (_super) {
216
217
  *
217
218
  * [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/gateway/gateway_client.py#L13-L17)
218
219
  *
219
- * @param tx - transaction to be invoked
220
+ * @param transaction - transaction to be invoked
220
221
  * @returns a confirmation of invoking a function on the starknet contract
221
222
  */
222
- Signer.prototype.addTransaction = function (tx) {
223
+ Signer.prototype.addTransaction = function (transaction) {
223
224
  return __awaiter(this, void 0, void 0, function () {
224
- var result, nonceBn, calldataDecimal, msgHash, _a, r, s;
225
- return __generator(this, function (_b) {
226
- switch (_b.label) {
225
+ var nonceBn, result, calldataDecimal, msgHash, signature;
226
+ return __generator(this, function (_a) {
227
+ switch (_a.label) {
227
228
  case 0:
228
- if (tx.type === 'DEPLOY')
229
- return [2 /*return*/, _super.prototype.addTransaction.call(this, tx)];
229
+ if (transaction.type === 'DEPLOY')
230
+ return [2 /*return*/, _super.prototype.addTransaction.call(this, transaction)];
230
231
  (0,
231
- minimalistic_assert_1.default)(!tx.signature, "Adding signatures to a signer tx currently isn't supported");
232
+ minimalistic_assert_1.default)(!transaction.signature, "Adding signatures to a signer transaction currently isn't supported");
233
+ if (!transaction.nonce) return [3 /*break*/, 1];
234
+ nonceBn = (0, number_1.toBN)(transaction.nonce);
235
+ return [3 /*break*/, 3];
236
+ case 1:
232
237
  return [
233
238
  4 /*yield*/,
234
239
  this.callContract({
@@ -236,22 +241,24 @@ var Signer = /** @class */ (function (_super) {
236
241
  entry_point_selector: (0, stark_1.getSelectorFromName)('get_nonce'),
237
242
  }),
238
243
  ];
239
- case 1:
240
- result = _b.sent().result;
244
+ case 2:
245
+ result = _a.sent().result;
241
246
  nonceBn = (0, number_1.toBN)(result[0]);
242
- calldataDecimal = (tx.calldata || []).map(function (x) {
247
+ _a.label = 3;
248
+ case 3:
249
+ calldataDecimal = (transaction.calldata || []).map(function (x) {
243
250
  return (0, number_1.toBN)(x).toString();
244
251
  });
245
252
  msgHash = (0, encode_1.addHexPrefix)(
246
253
  (0, hash_1.hashMessage)(
247
254
  this.address,
248
- tx.contract_address,
249
- tx.entry_point_selector,
255
+ transaction.contract_address,
256
+ transaction.entry_point_selector,
250
257
  calldataDecimal,
251
258
  nonceBn.toString()
252
259
  )
253
260
  );
254
- (_a = (0, ellipticCurve_1.sign)(this.keyPair, msgHash)), (r = _a.r), (s = _a.s);
261
+ signature = (0, ellipticCurve_1.sign)(this.keyPair, msgHash);
255
262
  return [
256
263
  2 /*return*/,
257
264
  _super.prototype.addTransaction.call(this, {
@@ -260,8 +267,8 @@ var Signer = /** @class */ (function (_super) {
260
267
  calldata: __spreadArray(
261
268
  __spreadArray(
262
269
  [
263
- tx.contract_address,
264
- tx.entry_point_selector,
270
+ transaction.contract_address,
271
+ transaction.entry_point_selector,
265
272
  calldataDecimal.length.toString(),
266
273
  ],
267
274
  __read(calldataDecimal),
@@ -273,13 +280,49 @@ var Signer = /** @class */ (function (_super) {
273
280
  return (0, number_1.toBN)(x).toString();
274
281
  }),
275
282
  contract_address: this.address,
276
- signature: [r, s],
283
+ signature: signature,
277
284
  }),
278
285
  ];
279
286
  }
280
287
  });
281
288
  });
282
289
  };
290
+ /**
291
+ * Sign an JSON object with the starknet private key and return the signature
292
+ *
293
+ * @param json - JSON object to be signed
294
+ * @returns the signature of the JSON object
295
+ * @throws {Error} if the JSON object is not a valid JSON
296
+ */
297
+ Signer.prototype.signMessage = function (typedData) {
298
+ return __awaiter(this, void 0, void 0, function () {
299
+ var _a, _b;
300
+ return __generator(this, function (_c) {
301
+ switch (_c.label) {
302
+ case 0:
303
+ _a = ellipticCurve_1.sign;
304
+ _b = [this.keyPair];
305
+ return [4 /*yield*/, this.hashMessage(typedData)];
306
+ case 1:
307
+ return [2 /*return*/, _a.apply(void 0, _b.concat([_c.sent()]))];
308
+ }
309
+ });
310
+ });
311
+ };
312
+ /**
313
+ * Hash a JSON object with pederson hash and return the hash
314
+ *
315
+ * @param json - JSON object to be hashed
316
+ * @returns the hash of the JSON object
317
+ * @throws {Error} if the JSON object is not a valid JSON
318
+ */
319
+ Signer.prototype.hashMessage = function (typedData) {
320
+ return __awaiter(this, void 0, void 0, function () {
321
+ return __generator(this, function (_a) {
322
+ return [2 /*return*/, (0, typedData_1.getMessageHash)(typedData, this.address)];
323
+ });
324
+ });
325
+ };
283
326
  return Signer;
284
327
  })(provider_1.Provider);
285
328
  exports.Signer = Signer;
@@ -1,5 +1,6 @@
1
1
  import { Provider } from '../provider';
2
- import { AddTransactionResponse, Transaction } from '../types';
2
+ import { AddTransactionResponse, Signature, Transaction } from '../types';
3
+ import { TypedData } from '../utils/typedData/types';
3
4
  export declare abstract class SignerInterface extends Provider {
4
5
  abstract address: string;
5
6
  /**
@@ -7,8 +8,26 @@ export declare abstract class SignerInterface extends Provider {
7
8
  *
8
9
  * [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/gateway/gateway_client.py#L13-L17)
9
10
  *
10
- * @param tx - transaction to be invoked
11
+ * @param transaction - transaction to be invoked
11
12
  * @returns a confirmation of invoking a function on the starknet contract
12
13
  */
13
- abstract addTransaction(tx: Transaction): Promise<AddTransactionResponse>;
14
+ abstract addTransaction(transaction: Transaction): Promise<AddTransactionResponse>;
15
+ /**
16
+ * Sign an JSON object for off-chain usage with the starknet private key and return the signature
17
+ * This adds a message prefix so it cant be interchanged with transactions
18
+ *
19
+ * @param json - JSON object to be signed
20
+ * @returns the signature of the JSON object
21
+ * @throws {Error} if the JSON object is not a valid JSON
22
+ */
23
+ abstract signMessage(typedData: TypedData): Promise<Signature>;
24
+ /**
25
+ * Hash a JSON object with pederson hash and return the hash
26
+ * This adds a message prefix so it cant be interchanged with transactions
27
+ *
28
+ * @param json - JSON object to be hashed
29
+ * @returns the hash of the JSON object
30
+ * @throws {Error} if the JSON object is not a valid JSON
31
+ */
32
+ abstract hashMessage(typedData: TypedData): Promise<string>;
14
33
  }