starknet 2.7.2 → 3.1.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 (111) hide show
  1. package/.eslintrc +3 -1
  2. package/CHANGELOG.md +54 -0
  3. package/README.md +16 -14
  4. package/__mocks__/contract.json +33191 -0
  5. package/__mocks__/multicall.json +8139 -0
  6. package/__mocks__/typedDataExample.json +35 -0
  7. package/__tests__/account.test.ts +53 -87
  8. package/__tests__/accountContract.test.ts +161 -0
  9. package/__tests__/contract.test.ts +167 -30
  10. package/__tests__/jest.setup.ts +9 -0
  11. package/__tests__/provider.test.ts +19 -34
  12. package/__tests__/utils/address.test.ts +16 -0
  13. package/__tests__/utils/typedData.test.ts +1 -36
  14. package/account/default.d.ts +66 -0
  15. package/account/default.js +439 -0
  16. package/account/index.d.ts +2 -0
  17. package/account/index.js +27 -0
  18. package/account/interface.d.ts +83 -0
  19. package/account/interface.js +37 -0
  20. package/constants.d.ts +2 -0
  21. package/constants.js +4 -0
  22. package/contract.d.ts +71 -12
  23. package/contract.js +243 -89
  24. package/dist/account/default.d.ts +55 -0
  25. package/dist/account/default.js +271 -0
  26. package/dist/account/index.d.ts +2 -0
  27. package/dist/account/index.js +14 -0
  28. package/dist/account/interface.d.ts +69 -0
  29. package/dist/account/interface.js +27 -0
  30. package/dist/constants.d.ts +2 -0
  31. package/dist/constants.js +3 -1
  32. package/dist/contract.d.ts +71 -9
  33. package/dist/contract.js +214 -65
  34. package/dist/index.d.ts +2 -1
  35. package/dist/index.js +2 -1
  36. package/dist/provider/default.d.ts +27 -16
  37. package/dist/provider/default.js +157 -100
  38. package/dist/provider/interface.d.ts +29 -32
  39. package/dist/provider/utils.d.ts +21 -5
  40. package/dist/provider/utils.js +53 -10
  41. package/dist/signer/default.d.ts +7 -31
  42. package/dist/signer/default.js +25 -121
  43. package/dist/signer/index.d.ts +1 -1
  44. package/dist/signer/index.js +1 -1
  45. package/dist/signer/interface.d.ts +17 -18
  46. package/dist/signer/interface.js +2 -20
  47. package/dist/types/api.d.ts +147 -0
  48. package/dist/{types.js → types/api.js} +0 -0
  49. package/dist/types/index.d.ts +3 -0
  50. package/dist/types/index.js +15 -0
  51. package/dist/types/lib.d.ts +57 -0
  52. package/dist/types/lib.js +2 -0
  53. package/dist/types/signer.d.ts +4 -0
  54. package/dist/types/signer.js +2 -0
  55. package/dist/utils/address.d.ts +2 -0
  56. package/dist/utils/address.js +22 -0
  57. package/dist/utils/number.d.ts +2 -0
  58. package/dist/utils/number.js +32 -2
  59. package/dist/utils/stark.d.ts +2 -1
  60. package/dist/utils/stark.js +44 -1
  61. package/index.d.ts +2 -1
  62. package/index.js +2 -1
  63. package/package.json +9 -3
  64. package/provider/default.d.ts +45 -36
  65. package/provider/default.js +216 -201
  66. package/provider/interface.d.ts +36 -49
  67. package/provider/utils.d.ts +23 -8
  68. package/provider/utils.js +57 -11
  69. package/signer/default.d.ts +11 -31
  70. package/signer/default.js +52 -169
  71. package/signer/index.d.ts +1 -1
  72. package/signer/index.js +1 -1
  73. package/signer/interface.d.ts +21 -18
  74. package/signer/interface.js +3 -32
  75. package/src/account/default.ts +151 -0
  76. package/src/account/index.ts +2 -0
  77. package/src/account/interface.ts +91 -0
  78. package/src/constants.ts +2 -0
  79. package/src/contract.ts +246 -77
  80. package/src/index.ts +2 -1
  81. package/src/provider/default.ts +141 -110
  82. package/src/provider/interface.ts +36 -52
  83. package/src/provider/utils.ts +60 -13
  84. package/src/signer/default.ts +33 -76
  85. package/src/signer/index.ts +1 -1
  86. package/src/signer/interface.ts +21 -20
  87. package/src/types/api.ts +171 -0
  88. package/src/types/index.ts +3 -0
  89. package/src/types/lib.ts +73 -0
  90. package/src/types/signer.ts +5 -0
  91. package/src/utils/address.ts +23 -0
  92. package/src/utils/number.ts +12 -1
  93. package/src/utils/stark.ts +13 -1
  94. package/types/api.d.ts +162 -0
  95. package/{types.js → types/api.js} +0 -0
  96. package/types/index.d.ts +3 -0
  97. package/types/index.js +28 -0
  98. package/types/lib.d.ts +64 -0
  99. package/types/lib.js +2 -0
  100. package/types/signer.d.ts +4 -0
  101. package/types/signer.js +2 -0
  102. package/utils/address.d.ts +2 -0
  103. package/utils/address.js +22 -0
  104. package/utils/number.d.ts +4 -0
  105. package/utils/number.js +54 -2
  106. package/utils/stark.d.ts +2 -1
  107. package/utils/stark.js +64 -1
  108. package/__tests__/signer.test.ts +0 -119
  109. package/dist/types.d.ts +0 -109
  110. package/src/types.ts +0 -131
  111. package/types.d.ts +0 -116
@@ -1,18 +1,18 @@
1
1
  import type {
2
2
  AddTransactionResponse,
3
- BlockNumber,
3
+ Call,
4
4
  CallContractResponse,
5
- CallContractTransaction,
6
- CompiledContract,
5
+ DeployContractPayload,
7
6
  GetBlockResponse,
8
7
  GetCodeResponse,
9
8
  GetContractAddressesResponse,
10
9
  GetTransactionResponse,
11
10
  GetTransactionStatusResponse,
12
- Signature,
13
- Transaction,
11
+ Invocation,
12
+ TransactionReceipt,
14
13
  } from '../types';
15
14
  import type { BigNumberish } from '../utils/number';
15
+ import { BlockIdentifier } from './utils';
16
16
  export declare abstract class ProviderInterface {
17
17
  abstract baseUrl: string;
18
18
  abstract feederGatewayUrl: string;
@@ -29,40 +29,35 @@ export declare abstract class ProviderInterface {
29
29
  *
30
30
  * [Reference](https://github.com/starkware-libs/cairo-lang/blob/fc97bdd8322a7df043c87c371634b26c15ed6cee/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L25-L39)
31
31
  *
32
- * @param invokeTransaction - transaction to be invoked
33
- * @param blockHash
34
- * @param blockNumber
32
+ * @param invokeTransaction transaction to be invoked
33
+ * @param blockIdentifier block identifier
35
34
  * @returns the result of the function on the smart contract.
36
35
  */
37
36
  abstract callContract(
38
- invokeTransaction: CallContractTransaction,
39
- blockHash?: BigNumberish,
40
- blockNumber?: BlockNumber
37
+ invokeTransaction: Call,
38
+ blockIdentifier?: BlockIdentifier
41
39
  ): Promise<CallContractResponse>;
42
40
  /**
43
41
  * Gets the block information
44
42
  *
45
43
  * [Reference](https://github.com/starkware-libs/cairo-lang/blob/fc97bdd8322a7df043c87c371634b26c15ed6cee/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L41-L53)
46
44
  *
47
- * @param blockHash
48
- * @param blockNumber
45
+ * @param blockIdentifier block identifier
49
46
  * @returns the block object { block_number, previous_block_number, state_root, status, timestamp, transaction_receipts, transactions }
50
47
  */
51
- abstract getBlock(blockHash?: BigNumberish, blockNumber?: BlockNumber): Promise<GetBlockResponse>;
48
+ abstract getBlock(blockIdentifier?: BlockIdentifier): Promise<GetBlockResponse>;
52
49
  /**
53
50
  * Gets the code of the deployed contract.
54
51
  *
55
52
  * [Reference](https://github.com/starkware-libs/cairo-lang/blob/fc97bdd8322a7df043c87c371634b26c15ed6cee/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L55-L68)
56
53
  *
57
- * @param contractAddress
58
- * @param blockHash
59
- * @param blockNumber
54
+ * @param contractAddress - contract address
55
+ * @param blockIdentifier - block identifier
60
56
  * @returns Bytecode and ABI of compiled contract
61
57
  */
62
58
  abstract getCode(
63
59
  contractAddress: string,
64
- blockHash?: BigNumberish,
65
- blockNumber?: BlockNumber
60
+ blockIdentifier?: BlockIdentifier
66
61
  ): Promise<GetCodeResponse>;
67
62
  /**
68
63
  * Gets the contract's storage variable at a specific key.
@@ -71,15 +66,13 @@ export declare abstract class ProviderInterface {
71
66
  *
72
67
  * @param contractAddress
73
68
  * @param key - from getStorageVarAddress('<STORAGE_VARIABLE_NAME>') (WIP)
74
- * @param blockHash
75
- * @param blockNumber
69
+ * @param blockIdentifier - block identifier
76
70
  * @returns the value of the storage variable
77
71
  */
78
72
  abstract getStorageAt(
79
73
  contractAddress: string,
80
74
  key: number,
81
- blockHash?: BigNumberish,
82
- blockNumber?: BlockNumber
75
+ blockIdentifier?: BlockIdentifier
83
76
  ): Promise<object>;
84
77
  /**
85
78
  * Gets the status of a transaction.
@@ -99,41 +92,35 @@ export declare abstract class ProviderInterface {
99
92
  * @returns the transacton object { transaction_id, status, transaction, block_number?, block_number?, transaction_index?, transaction_failure_reason? }
100
93
  */
101
94
  abstract getTransaction(txHash: BigNumberish): Promise<GetTransactionResponse>;
102
- /**
103
- * Invoke a function on the starknet contract
104
- *
105
- * [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/gateway/gateway_client.py#L13-L17)
106
- *
107
- * @param transaction - transaction to be invoked
108
- * @returns a confirmation of invoking a function on the starknet contract
109
- */
110
- abstract addTransaction(transaction: Transaction): Promise<AddTransactionResponse>;
95
+ abstract getTransactionReceipt({
96
+ txHash,
97
+ txId,
98
+ }: {
99
+ txHash?: BigNumberish;
100
+ txId?: BigNumberish;
101
+ }): Promise<TransactionReceipt>;
111
102
  /**
112
103
  * Deploys a given compiled contract (json) to starknet
113
104
  *
114
- * @param contract - a json object containing the compiled contract
115
- * @param address - (optional, defaults to a random address) the address where the contract should be deployed (alpha)
105
+ * @param payload payload to be deployed containing:
106
+ * - compiled contract code
107
+ * - constructor calldata
108
+ * - address salt
116
109
  * @returns a confirmation of sending a transaction on the starknet contract
117
110
  */
118
- abstract deployContract(
119
- contract: CompiledContract | string,
120
- constructorCalldata: string[],
121
- addressSalt: BigNumberish
122
- ): Promise<AddTransactionResponse>;
111
+ abstract deployContract(payload: DeployContractPayload): Promise<AddTransactionResponse>;
123
112
  /**
124
113
  * Invokes a function on starknet
114
+ * @deprecated This method wont be supported as soon as fees are mandatory
115
+ *
116
+ * @param invocation the invocation object containing:
117
+ * - contractAddress - the address of the contract
118
+ * - entrypoint - the entrypoint of the contract
119
+ * - calldata - (defaults to []) the calldata
120
+ * - signature - (defaults to []) the signature
125
121
  *
126
- * @param contractAddress - target contract address for invoke
127
- * @param entrypointSelector - target entrypoint selector for
128
- * @param calldata - (optional, default []) calldata
129
- * @param signature - (optional) signature to send along
130
122
  * @returns response from addTransaction
131
123
  */
132
- abstract invokeFunction(
133
- contractAddress: string,
134
- entrypointSelector: string,
135
- calldata?: string[],
136
- signature?: Signature
137
- ): Promise<AddTransactionResponse>;
124
+ abstract invokeFunction(invocation: Invocation): Promise<AddTransactionResponse>;
138
125
  abstract waitForTx(txHash: BigNumberish, retryInterval?: number): Promise<void>;
139
126
  }
@@ -1,20 +1,37 @@
1
1
  import type { BlockNumber } from '../types';
2
2
  import { BigNumberish } from '../utils/number';
3
3
  /**
4
- * TODO
4
+ *
5
5
  * [Reference](https://github.com/starkware-libs/cairo-lang/blob/fc97bdd8322a7df043c87c371634b26c15ed6cee/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L148-L153)
6
6
  *
7
7
  * @param hashValue
8
8
  * @param hashField
9
9
  */
10
- export declare function formatHash(): void;
10
+ export declare function formatHash(hashValue: BigNumberish): string;
11
11
  /**
12
- * TODO
12
+ *
13
13
  * [Reference](https://github.com/starkware-libs/cairo-lang/blob/fc97bdd8322a7df043c87c371634b26c15ed6cee/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L156-L161)
14
14
  * @param txHash
15
15
  * @param txId
16
16
  */
17
- export declare function txIdentifier(): void;
17
+ export declare function txIdentifier(txHash?: BigNumberish, txId?: BigNumberish): string;
18
+ export declare type BlockIdentifier = BlockNumber | BigNumberish;
19
+ declare type BlockIdentifierObject =
20
+ | {
21
+ type: 'BLOCK_NUMBER';
22
+ data: BlockNumber;
23
+ }
24
+ | {
25
+ type: 'BLOCK_HASH';
26
+ data: BigNumberish;
27
+ };
28
+ /**
29
+ * Identifies the block to be queried.
30
+ *
31
+ * @param blockIdentifier - block identifier
32
+ * @returns block identifier object
33
+ */
34
+ export declare function getBlockIdentifier(blockIdentifier: BlockIdentifier): BlockIdentifierObject;
18
35
  /**
19
36
  * Gets the block identifier for API request
20
37
  *
@@ -24,7 +41,5 @@ export declare function txIdentifier(): void;
24
41
  * @param blockHash
25
42
  * @returns block identifier for API request
26
43
  */
27
- export declare function getFormattedBlockIdentifier(
28
- blockHash?: BigNumberish,
29
- blockNumber?: BlockNumber
30
- ): string;
44
+ export declare function getFormattedBlockIdentifier(blockIdentifier?: BlockIdentifier): string;
45
+ export {};
package/provider/utils.js CHANGED
@@ -1,23 +1,65 @@
1
1
  'use strict';
2
2
  Object.defineProperty(exports, '__esModule', { value: true });
3
- exports.getFormattedBlockIdentifier = exports.txIdentifier = exports.formatHash = void 0;
3
+ exports.getFormattedBlockIdentifier =
4
+ exports.getBlockIdentifier =
5
+ exports.txIdentifier =
6
+ exports.formatHash =
7
+ void 0;
8
+ var number_1 = require('../utils/number');
4
9
  /**
5
- * TODO
10
+ *
6
11
  * [Reference](https://github.com/starkware-libs/cairo-lang/blob/fc97bdd8322a7df043c87c371634b26c15ed6cee/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L148-L153)
7
12
  *
8
13
  * @param hashValue
9
14
  * @param hashField
10
15
  */
11
- function formatHash() {}
16
+ function formatHash(hashValue) {
17
+ if (typeof hashValue === 'string') return hashValue;
18
+ return (0, number_1.toHex)((0, number_1.toBN)(hashValue));
19
+ }
12
20
  exports.formatHash = formatHash;
13
21
  /**
14
- * TODO
22
+ *
15
23
  * [Reference](https://github.com/starkware-libs/cairo-lang/blob/fc97bdd8322a7df043c87c371634b26c15ed6cee/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L156-L161)
16
24
  * @param txHash
17
25
  * @param txId
18
26
  */
19
- function txIdentifier() {}
27
+ function txIdentifier(txHash, txId) {
28
+ if (!txHash) {
29
+ return 'transactionId=' + JSON.stringify(txId);
30
+ }
31
+ var hashString = formatHash(txHash);
32
+ return 'transactionHash=' + hashString;
33
+ }
20
34
  exports.txIdentifier = txIdentifier;
35
+ /**
36
+ * Identifies the block to be queried.
37
+ *
38
+ * @param blockIdentifier - block identifier
39
+ * @returns block identifier object
40
+ */
41
+ function getBlockIdentifier(blockIdentifier) {
42
+ if (typeof blockIdentifier === 'number') {
43
+ return { type: 'BLOCK_NUMBER', data: blockIdentifier };
44
+ }
45
+ if (typeof blockIdentifier === 'string' && blockIdentifier.startsWith('0x')) {
46
+ return { type: 'BLOCK_HASH', data: blockIdentifier };
47
+ }
48
+ if (typeof blockIdentifier === 'string' && !Number.isNaN(parseInt(blockIdentifier, 10))) {
49
+ return { type: 'BLOCK_NUMBER', data: parseInt(blockIdentifier, 10) };
50
+ }
51
+ if (blockIdentifier === null) {
52
+ return { type: 'BLOCK_NUMBER', data: null };
53
+ }
54
+ if (blockIdentifier === 'pending') {
55
+ return { type: 'BLOCK_NUMBER', data: 'pending' };
56
+ }
57
+ if (typeof blockIdentifier === 'string') {
58
+ throw new Error('Invalid block identifier: ' + blockIdentifier);
59
+ }
60
+ return { type: 'BLOCK_HASH', data: blockIdentifier };
61
+ }
62
+ exports.getBlockIdentifier = getBlockIdentifier;
21
63
  /**
22
64
  * Gets the block identifier for API request
23
65
  *
@@ -27,13 +69,17 @@ exports.txIdentifier = txIdentifier;
27
69
  * @param blockHash
28
70
  * @returns block identifier for API request
29
71
  */
30
- function getFormattedBlockIdentifier(blockHash, blockNumber) {
31
- if (blockNumber === void 0) {
32
- blockNumber = null;
72
+ function getFormattedBlockIdentifier(blockIdentifier) {
73
+ if (blockIdentifier === void 0) {
74
+ blockIdentifier = null;
75
+ }
76
+ var blockIdentifierObject = getBlockIdentifier(blockIdentifier);
77
+ if (blockIdentifierObject.type === 'BLOCK_NUMBER' && blockIdentifierObject.data === null) {
78
+ return '';
33
79
  }
34
- if (blockHash) {
35
- return '?blockHash=' + blockHash;
80
+ if (blockIdentifierObject.type === 'BLOCK_NUMBER') {
81
+ return 'blockNumber=' + blockIdentifierObject.data;
36
82
  }
37
- return '?blockNumber=' + blockNumber;
83
+ return 'blockHash=' + (0, number_1.toHex)((0, number_1.toBN)(blockIdentifierObject.data));
38
84
  }
39
85
  exports.getFormattedBlockIdentifier = getFormattedBlockIdentifier;
@@ -1,34 +1,14 @@
1
- import { Provider } from '../provider';
2
- import { AddTransactionResponse, KeyPair, Signature, Transaction } from '../types';
1
+ import { Abi, Invocation, InvocationsSignerDetails, KeyPair, Signature } from '../types';
3
2
  import { TypedData } from '../utils/typedData';
4
3
  import { SignerInterface } from './interface';
5
- export declare class Signer extends Provider implements SignerInterface {
6
- address: string;
7
- private keyPair;
8
- constructor(provider: Provider, address: string, keyPair: KeyPair);
9
- /**
10
- * Invoke a function on the starknet contract
11
- *
12
- * [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/gateway/gateway_client.py#L13-L17)
13
- *
14
- * @param transaction - transaction to be invoked
15
- * @returns a confirmation of invoking a function on the starknet contract
16
- */
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>;
4
+ export declare class Signer implements SignerInterface {
5
+ protected keyPair: KeyPair;
6
+ constructor(keyPair: KeyPair);
7
+ getPubKey(): Promise<string>;
8
+ signTransaction(
9
+ transactions: Invocation[],
10
+ transactionsDetail: InvocationsSignerDetails,
11
+ abis?: Abi[]
12
+ ): Promise<Signature>;
13
+ signMessage(typedData: TypedData, walletAddress: string): Promise<Signature>;
34
14
  }
package/signer/default.js CHANGED
@@ -1,29 +1,4 @@
1
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
2
  var __awaiter =
28
3
  (this && this.__awaiter) ||
29
4
  function (thisArg, _arguments, P, generator) {
@@ -155,174 +130,82 @@ var __generator =
155
130
  return { value: op[0] ? op[1] : void 0, done: true };
156
131
  }
157
132
  };
158
- var __read =
159
- (this && this.__read) ||
160
- function (o, n) {
161
- var m = typeof Symbol === 'function' && o[Symbol.iterator];
162
- if (!m) return o;
163
- var i = m.call(o),
164
- r,
165
- ar = [],
166
- e;
167
- try {
168
- while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
169
- } catch (error) {
170
- e = { error: error };
171
- } finally {
172
- try {
173
- if (r && !r.done && (m = i['return'])) m.call(i);
174
- } finally {
175
- if (e) throw e.error;
176
- }
177
- }
178
- return ar;
179
- };
180
- var __spreadArray =
181
- (this && this.__spreadArray) ||
182
- function (to, from, pack) {
183
- if (pack || arguments.length === 2)
184
- for (var i = 0, l = from.length, ar; i < l; i++) {
185
- if (ar || !(i in from)) {
186
- if (!ar) ar = Array.prototype.slice.call(from, 0, i);
187
- ar[i] = from[i];
188
- }
189
- }
190
- return to.concat(ar || Array.prototype.slice.call(from));
191
- };
192
- var __importDefault =
193
- (this && this.__importDefault) ||
194
- function (mod) {
195
- return mod && mod.__esModule ? mod : { default: mod };
196
- };
197
133
  Object.defineProperty(exports, '__esModule', { value: true });
198
134
  exports.Signer = void 0;
199
- var minimalistic_assert_1 = __importDefault(require('minimalistic-assert'));
200
- var provider_1 = require('../provider');
201
135
  var ellipticCurve_1 = require('../utils/ellipticCurve');
202
136
  var encode_1 = require('../utils/encode');
203
137
  var hash_1 = require('../utils/hash');
204
138
  var number_1 = require('../utils/number');
205
139
  var stark_1 = require('../utils/stark');
206
140
  var typedData_1 = require('../utils/typedData');
207
- var Signer = /** @class */ (function (_super) {
208
- __extends(Signer, _super);
209
- function Signer(provider, address, keyPair) {
210
- var _this = _super.call(this, provider) || this;
211
- _this.keyPair = keyPair;
212
- _this.address = address;
213
- return _this;
141
+ var Signer = /** @class */ (function () {
142
+ function Signer(keyPair) {
143
+ this.keyPair = keyPair;
214
144
  }
215
- /**
216
- * Invoke a function on the starknet contract
217
- *
218
- * [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/gateway/gateway_client.py#L13-L17)
219
- *
220
- * @param transaction - transaction to be invoked
221
- * @returns a confirmation of invoking a function on the starknet contract
222
- */
223
- Signer.prototype.addTransaction = function (transaction) {
145
+ Signer.prototype.getPubKey = function () {
224
146
  return __awaiter(this, void 0, void 0, function () {
225
- var nonceBn, result, calldataDecimal, msgHash, signature;
226
147
  return __generator(this, function (_a) {
227
- switch (_a.label) {
228
- case 0:
229
- if (transaction.type === 'DEPLOY')
230
- return [2 /*return*/, _super.prototype.addTransaction.call(this, transaction)];
231
- (0,
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:
237
- return [
238
- 4 /*yield*/,
239
- this.callContract({
240
- contract_address: this.address,
241
- entry_point_selector: (0, stark_1.getSelectorFromName)('get_nonce'),
242
- }),
243
- ];
244
- case 2:
245
- result = _a.sent().result;
246
- nonceBn = (0, number_1.toBN)(result[0]);
247
- _a.label = 3;
248
- case 3:
249
- calldataDecimal = (transaction.calldata || []).map(function (x) {
250
- return (0, number_1.toBN)(x).toString();
251
- });
252
- msgHash = (0, encode_1.addHexPrefix)(
253
- (0, hash_1.hashMessage)(
254
- this.address,
255
- transaction.contract_address,
256
- transaction.entry_point_selector,
257
- calldataDecimal,
258
- nonceBn.toString()
259
- )
260
- );
261
- signature = (0, ellipticCurve_1.sign)(this.keyPair, msgHash);
262
- return [
263
- 2 /*return*/,
264
- _super.prototype.addTransaction.call(this, {
265
- type: 'INVOKE_FUNCTION',
266
- entry_point_selector: (0, stark_1.getSelectorFromName)('execute'),
267
- calldata: __spreadArray(
268
- __spreadArray(
269
- [
270
- transaction.contract_address,
271
- transaction.entry_point_selector,
272
- calldataDecimal.length.toString(),
273
- ],
274
- __read(calldataDecimal),
275
- false
276
- ),
277
- [nonceBn.toString()],
278
- false
279
- ).map(function (x) {
280
- return (0, number_1.toBN)(x).toString();
281
- }),
282
- contract_address: this.address,
283
- signature: signature,
284
- }),
285
- ];
286
- }
148
+ return [2 /*return*/, (0, ellipticCurve_1.getStarkKey)(this.keyPair)];
287
149
  });
288
150
  });
289
151
  };
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) {
152
+ Signer.prototype.signTransaction = function (transactions, transactionsDetail, abis) {
153
+ if (abis === void 0) {
154
+ abis = [];
155
+ }
298
156
  return __awaiter(this, void 0, void 0, function () {
299
- var _a, _b;
157
+ var _a,
158
+ contractAddress,
159
+ entrypoint,
160
+ _b,
161
+ calldata,
162
+ nonce,
163
+ walletAddress,
164
+ nonceBn,
165
+ entrypointSelector,
166
+ calldataDecimal,
167
+ msgHash;
300
168
  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()]))];
169
+ if (transactions.length !== 1) {
170
+ throw new Error('Only one transaction at a time is currently supported by this signer');
171
+ }
172
+ if (
173
+ (abis === null || abis === void 0 ? void 0 : abis.length) !== 0 &&
174
+ abis.length !== transactions.length
175
+ ) {
176
+ throw new Error('ABI must be provided for each transaction or no transaction');
308
177
  }
178
+ (_a = transactions[0]),
179
+ (contractAddress = _a.contractAddress),
180
+ (entrypoint = _a.entrypoint),
181
+ (_b = _a.calldata),
182
+ (calldata = _b === void 0 ? [] : _b);
183
+ (nonce = transactionsDetail.nonce), (walletAddress = transactionsDetail.walletAddress);
184
+ nonceBn = (0, number_1.toBN)(nonce);
185
+ entrypointSelector = (0, stark_1.getSelectorFromName)(entrypoint);
186
+ calldataDecimal = (0, number_1.bigNumberishArrayToDecimalStringArray)(calldata);
187
+ msgHash = (0, encode_1.addHexPrefix)(
188
+ (0, hash_1.hashMessage)(
189
+ walletAddress,
190
+ contractAddress,
191
+ entrypointSelector,
192
+ calldataDecimal,
193
+ nonceBn.toString()
194
+ )
195
+ );
196
+ return [2 /*return*/, (0, ellipticCurve_1.sign)(this.keyPair, msgHash)];
309
197
  });
310
198
  });
311
199
  };
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) {
200
+ Signer.prototype.signMessage = function (typedData, walletAddress) {
320
201
  return __awaiter(this, void 0, void 0, function () {
202
+ var msgHash;
321
203
  return __generator(this, function (_a) {
322
- return [2 /*return*/, (0, typedData_1.getMessageHash)(typedData, this.address)];
204
+ msgHash = (0, typedData_1.getMessageHash)(typedData, walletAddress);
205
+ return [2 /*return*/, (0, ellipticCurve_1.sign)(this.keyPair, msgHash)];
323
206
  });
324
207
  });
325
208
  };
326
209
  return Signer;
327
- })(provider_1.Provider);
210
+ })();
328
211
  exports.Signer = Signer;
package/signer/index.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- export * from './default';
2
1
  export * from './interface';
2
+ export * from './default';
package/signer/index.js CHANGED
@@ -23,5 +23,5 @@ var __exportStar =
23
23
  __createBinding(exports, m, p);
24
24
  };
25
25
  Object.defineProperty(exports, '__esModule', { value: true });
26
- __exportStar(require('./default'), exports);
27
26
  __exportStar(require('./interface'), exports);
27
+ __exportStar(require('./default'), exports);
@@ -1,17 +1,12 @@
1
- import { Provider } from '../provider';
2
- import { AddTransactionResponse, Signature, Transaction } from '../types';
3
- import { TypedData } from '../utils/typedData/types';
4
- export declare abstract class SignerInterface extends Provider {
5
- abstract address: string;
1
+ import { Abi, Invocation, InvocationsSignerDetails, Signature } from '../types';
2
+ import { TypedData } from '../utils/typedData';
3
+ export declare abstract class SignerInterface {
6
4
  /**
7
- * Invoke a function on the starknet contract
5
+ * Method to get the public key of the signer
8
6
  *
9
- * [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/gateway/gateway_client.py#L13-L17)
10
- *
11
- * @param transaction - transaction to be invoked
12
- * @returns a confirmation of invoking a function on the starknet contract
7
+ * @returns public key of signer as hex string with 0x prefix
13
8
  */
14
- abstract addTransaction(transaction: Transaction): Promise<AddTransactionResponse>;
9
+ abstract getPubKey(): Promise<string>;
15
10
  /**
16
11
  * Sign an JSON object for off-chain usage with the starknet private key and return the signature
17
12
  * This adds a message prefix so it cant be interchanged with transactions
@@ -20,14 +15,22 @@ export declare abstract class SignerInterface extends Provider {
20
15
  * @returns the signature of the JSON object
21
16
  * @throws {Error} if the JSON object is not a valid JSON
22
17
  */
23
- abstract signMessage(typedData: TypedData): Promise<Signature>;
18
+ abstract signMessage(typedData: TypedData, walletAddress: string): Promise<Signature>;
24
19
  /**
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
20
+ * Signs a transaction with the starknet private key and returns the signature
27
21
  *
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
22
+ * @param invocation the invocation object containing:
23
+ * - contractAddress - the address of the contract
24
+ * - entrypoint - the entrypoint of the contract
25
+ * - calldata - (defaults to []) the calldata
26
+ * - signature - (defaults to []) the signature
27
+ * @param abi (optional) the abi of the contract for better displaying
28
+ *
29
+ * @returns signature
31
30
  */
32
- abstract hashMessage(typedData: TypedData): Promise<string>;
31
+ abstract signTransaction(
32
+ transactions: Invocation[],
33
+ transactionsDetail: InvocationsSignerDetails,
34
+ abis?: Abi[]
35
+ ): Promise<Signature>;
33
36
  }