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
@@ -1,5 +1,6 @@
1
- import type { AddTransactionResponse, BlockNumber, CallContractResponse, CallContractTransaction, CompiledContract, GetBlockResponse, GetCodeResponse, GetContractAddressesResponse, GetTransactionResponse, GetTransactionStatusResponse, Signature, Transaction } from '../types';
1
+ import type { AddTransactionResponse, Call, CallContractResponse, DeployContractPayload, GetBlockResponse, GetCodeResponse, GetContractAddressesResponse, GetTransactionResponse, GetTransactionStatusResponse, Invocation, TransactionReceipt } from '../types';
2
2
  import type { BigNumberish } from '../utils/number';
3
+ import { BlockIdentifier } from './utils';
3
4
  export declare abstract class ProviderInterface {
4
5
  abstract baseUrl: string;
5
6
  abstract feederGatewayUrl: string;
@@ -16,33 +17,30 @@ export declare abstract class ProviderInterface {
16
17
  *
17
18
  * [Reference](https://github.com/starkware-libs/cairo-lang/blob/fc97bdd8322a7df043c87c371634b26c15ed6cee/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L25-L39)
18
19
  *
19
- * @param invokeTransaction - transaction to be invoked
20
- * @param blockHash
21
- * @param blockNumber
20
+ * @param invokeTransaction transaction to be invoked
21
+ * @param blockIdentifier block identifier
22
22
  * @returns the result of the function on the smart contract.
23
23
  */
24
- abstract callContract(invokeTransaction: CallContractTransaction, blockHash?: BigNumberish, blockNumber?: BlockNumber): Promise<CallContractResponse>;
24
+ abstract callContract(invokeTransaction: Call, blockIdentifier?: BlockIdentifier): Promise<CallContractResponse>;
25
25
  /**
26
26
  * Gets the block information
27
27
  *
28
28
  * [Reference](https://github.com/starkware-libs/cairo-lang/blob/fc97bdd8322a7df043c87c371634b26c15ed6cee/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L41-L53)
29
29
  *
30
- * @param blockHash
31
- * @param blockNumber
30
+ * @param blockIdentifier block identifier
32
31
  * @returns the block object { block_number, previous_block_number, state_root, status, timestamp, transaction_receipts, transactions }
33
32
  */
34
- abstract getBlock(blockHash?: BigNumberish, blockNumber?: BlockNumber): Promise<GetBlockResponse>;
33
+ abstract getBlock(blockIdentifier?: BlockIdentifier): Promise<GetBlockResponse>;
35
34
  /**
36
35
  * Gets the code of the deployed contract.
37
36
  *
38
37
  * [Reference](https://github.com/starkware-libs/cairo-lang/blob/fc97bdd8322a7df043c87c371634b26c15ed6cee/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L55-L68)
39
38
  *
40
- * @param contractAddress
41
- * @param blockHash
42
- * @param blockNumber
39
+ * @param contractAddress - contract address
40
+ * @param blockIdentifier - block identifier
43
41
  * @returns Bytecode and ABI of compiled contract
44
42
  */
45
- abstract getCode(contractAddress: string, blockHash?: BigNumberish, blockNumber?: BlockNumber): Promise<GetCodeResponse>;
43
+ abstract getCode(contractAddress: string, blockIdentifier?: BlockIdentifier): Promise<GetCodeResponse>;
46
44
  /**
47
45
  * Gets the contract's storage variable at a specific key.
48
46
  *
@@ -50,11 +48,10 @@ export declare abstract class ProviderInterface {
50
48
  *
51
49
  * @param contractAddress
52
50
  * @param key - from getStorageVarAddress('<STORAGE_VARIABLE_NAME>') (WIP)
53
- * @param blockHash
54
- * @param blockNumber
51
+ * @param blockIdentifier - block identifier
55
52
  * @returns the value of the storage variable
56
53
  */
57
- abstract getStorageAt(contractAddress: string, key: number, blockHash?: BigNumberish, blockNumber?: BlockNumber): Promise<object>;
54
+ abstract getStorageAt(contractAddress: string, key: number, blockIdentifier?: BlockIdentifier): Promise<object>;
58
55
  /**
59
56
  * Gets the status of a transaction.
60
57
  *
@@ -73,32 +70,32 @@ export declare abstract class ProviderInterface {
73
70
  * @returns the transacton object { transaction_id, status, transaction, block_number?, block_number?, transaction_index?, transaction_failure_reason? }
74
71
  */
75
72
  abstract getTransaction(txHash: BigNumberish): Promise<GetTransactionResponse>;
76
- /**
77
- * Invoke a function on the starknet contract
78
- *
79
- * [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/gateway/gateway_client.py#L13-L17)
80
- *
81
- * @param transaction - transaction to be invoked
82
- * @returns a confirmation of invoking a function on the starknet contract
83
- */
84
- abstract addTransaction(transaction: Transaction): Promise<AddTransactionResponse>;
73
+ abstract getTransactionReceipt({ txHash, txId, }: {
74
+ txHash?: BigNumberish;
75
+ txId?: BigNumberish;
76
+ }): Promise<TransactionReceipt>;
85
77
  /**
86
78
  * Deploys a given compiled contract (json) to starknet
87
79
  *
88
- * @param contract - a json object containing the compiled contract
89
- * @param address - (optional, defaults to a random address) the address where the contract should be deployed (alpha)
80
+ * @param payload payload to be deployed containing:
81
+ * - compiled contract code
82
+ * - constructor calldata
83
+ * - address salt
90
84
  * @returns a confirmation of sending a transaction on the starknet contract
91
85
  */
92
- abstract deployContract(contract: CompiledContract | string, constructorCalldata: string[], addressSalt: BigNumberish): Promise<AddTransactionResponse>;
86
+ abstract deployContract(payload: DeployContractPayload): Promise<AddTransactionResponse>;
93
87
  /**
94
88
  * Invokes a function on starknet
89
+ * @deprecated This method wont be supported as soon as fees are mandatory
90
+ *
91
+ * @param invocation the invocation object containing:
92
+ * - contractAddress - the address of the contract
93
+ * - entrypoint - the entrypoint of the contract
94
+ * - calldata - (defaults to []) the calldata
95
+ * - signature - (defaults to []) the signature
95
96
  *
96
- * @param contractAddress - target contract address for invoke
97
- * @param entrypointSelector - target entrypoint selector for
98
- * @param calldata - (optional, default []) calldata
99
- * @param signature - (optional) signature to send along
100
97
  * @returns response from addTransaction
101
98
  */
102
- abstract invokeFunction(contractAddress: string, entrypointSelector: string, calldata?: string[], signature?: Signature): Promise<AddTransactionResponse>;
99
+ abstract invokeFunction(invocation: Invocation): Promise<AddTransactionResponse>;
103
100
  abstract waitForTx(txHash: BigNumberish, retryInterval?: number): Promise<void>;
104
101
  }
@@ -1,20 +1,35 @@
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
+ type: 'BLOCK_NUMBER';
21
+ data: BlockNumber;
22
+ } | {
23
+ type: 'BLOCK_HASH';
24
+ data: BigNumberish;
25
+ };
26
+ /**
27
+ * Identifies the block to be queried.
28
+ *
29
+ * @param blockIdentifier - block identifier
30
+ * @returns block identifier object
31
+ */
32
+ export declare function getBlockIdentifier(blockIdentifier: BlockIdentifier): BlockIdentifierObject;
18
33
  /**
19
34
  * Gets the block identifier for API request
20
35
  *
@@ -24,4 +39,5 @@ export declare function txIdentifier(): void;
24
39
  * @param blockHash
25
40
  * @returns block identifier for API request
26
41
  */
27
- export declare function getFormattedBlockIdentifier(blockHash?: BigNumberish, blockNumber?: BlockNumber): string;
42
+ export declare function getFormattedBlockIdentifier(blockIdentifier?: BlockIdentifier): string;
43
+ export {};
@@ -1,23 +1,62 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getFormattedBlockIdentifier = exports.txIdentifier = exports.formatHash = void 0;
3
+ exports.getFormattedBlockIdentifier = exports.getBlockIdentifier = exports.txIdentifier = exports.formatHash = void 0;
4
+ var number_1 = require("../utils/number");
4
5
  /**
5
- * TODO
6
+ *
6
7
  * [Reference](https://github.com/starkware-libs/cairo-lang/blob/fc97bdd8322a7df043c87c371634b26c15ed6cee/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L148-L153)
7
8
  *
8
9
  * @param hashValue
9
10
  * @param hashField
10
11
  */
11
- function formatHash() { }
12
+ function formatHash(hashValue) {
13
+ if (typeof hashValue === 'string')
14
+ return hashValue;
15
+ return (0, number_1.toHex)((0, number_1.toBN)(hashValue));
16
+ }
12
17
  exports.formatHash = formatHash;
13
18
  /**
14
- * TODO
19
+ *
15
20
  * [Reference](https://github.com/starkware-libs/cairo-lang/blob/fc97bdd8322a7df043c87c371634b26c15ed6cee/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L156-L161)
16
21
  * @param txHash
17
22
  * @param txId
18
23
  */
19
- function txIdentifier() { }
24
+ function txIdentifier(txHash, txId) {
25
+ if (!txHash) {
26
+ return "transactionId=" + JSON.stringify(txId);
27
+ }
28
+ var hashString = formatHash(txHash);
29
+ return "transactionHash=" + hashString;
30
+ }
20
31
  exports.txIdentifier = txIdentifier;
32
+ /**
33
+ * Identifies the block to be queried.
34
+ *
35
+ * @param blockIdentifier - block identifier
36
+ * @returns block identifier object
37
+ */
38
+ function getBlockIdentifier(blockIdentifier) {
39
+ if (typeof blockIdentifier === 'number') {
40
+ return { type: 'BLOCK_NUMBER', data: blockIdentifier };
41
+ }
42
+ if (typeof blockIdentifier === 'string' && blockIdentifier.startsWith('0x')) {
43
+ return { type: 'BLOCK_HASH', data: blockIdentifier };
44
+ }
45
+ if (typeof blockIdentifier === 'string' && !Number.isNaN(parseInt(blockIdentifier, 10))) {
46
+ return { type: 'BLOCK_NUMBER', data: parseInt(blockIdentifier, 10) };
47
+ }
48
+ if (blockIdentifier === null) {
49
+ return { type: 'BLOCK_NUMBER', data: null };
50
+ }
51
+ if (blockIdentifier === 'pending') {
52
+ return { type: 'BLOCK_NUMBER', data: 'pending' };
53
+ }
54
+ if (typeof blockIdentifier === 'string') {
55
+ throw new Error("Invalid block identifier: " + blockIdentifier);
56
+ }
57
+ return { type: 'BLOCK_HASH', data: blockIdentifier };
58
+ }
59
+ exports.getBlockIdentifier = getBlockIdentifier;
21
60
  /**
22
61
  * Gets the block identifier for API request
23
62
  *
@@ -27,11 +66,15 @@ exports.txIdentifier = txIdentifier;
27
66
  * @param blockHash
28
67
  * @returns block identifier for API request
29
68
  */
30
- function getFormattedBlockIdentifier(blockHash, blockNumber) {
31
- if (blockNumber === void 0) { blockNumber = null; }
32
- if (blockHash) {
33
- return "?blockHash=" + blockHash;
69
+ function getFormattedBlockIdentifier(blockIdentifier) {
70
+ if (blockIdentifier === void 0) { blockIdentifier = null; }
71
+ var blockIdentifierObject = getBlockIdentifier(blockIdentifier);
72
+ if (blockIdentifierObject.type === 'BLOCK_NUMBER' && blockIdentifierObject.data === null) {
73
+ return '';
74
+ }
75
+ if (blockIdentifierObject.type === 'BLOCK_NUMBER') {
76
+ return "blockNumber=" + blockIdentifierObject.data;
34
77
  }
35
- return "?blockNumber=" + blockNumber;
78
+ return "blockHash=" + (0, number_1.toHex)((0, number_1.toBN)(blockIdentifierObject.data));
36
79
  }
37
80
  exports.getFormattedBlockIdentifier = getFormattedBlockIdentifier;
@@ -1,34 +1,10 @@
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(transactions: Invocation[], transactionsDetail: InvocationsSignerDetails, abis?: Abi[]): Promise<Signature>;
9
+ signMessage(typedData: TypedData, walletAddress: string): Promise<Signature>;
34
10
  }
@@ -1,19 +1,4 @@
1
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
2
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
18
3
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
19
4
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -50,136 +35,55 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
50
35
  if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
51
36
  }
52
37
  };
53
- var __read = (this && this.__read) || function (o, n) {
54
- var m = typeof Symbol === "function" && o[Symbol.iterator];
55
- if (!m) return o;
56
- var i = m.call(o), r, ar = [], e;
57
- try {
58
- while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
59
- }
60
- catch (error) { e = { error: error }; }
61
- finally {
62
- try {
63
- if (r && !r.done && (m = i["return"])) m.call(i);
64
- }
65
- finally { if (e) throw e.error; }
66
- }
67
- return ar;
68
- };
69
- var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
70
- if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
71
- if (ar || !(i in from)) {
72
- if (!ar) ar = Array.prototype.slice.call(from, 0, i);
73
- ar[i] = from[i];
74
- }
75
- }
76
- return to.concat(ar || Array.prototype.slice.call(from));
77
- };
78
- var __importDefault = (this && this.__importDefault) || function (mod) {
79
- return (mod && mod.__esModule) ? mod : { "default": mod };
80
- };
81
38
  Object.defineProperty(exports, "__esModule", { value: true });
82
39
  exports.Signer = void 0;
83
- var minimalistic_assert_1 = __importDefault(require("minimalistic-assert"));
84
- var provider_1 = require("../provider");
85
40
  var ellipticCurve_1 = require("../utils/ellipticCurve");
86
41
  var encode_1 = require("../utils/encode");
87
42
  var hash_1 = require("../utils/hash");
88
43
  var number_1 = require("../utils/number");
89
44
  var stark_1 = require("../utils/stark");
90
45
  var typedData_1 = require("../utils/typedData");
91
- var Signer = /** @class */ (function (_super) {
92
- __extends(Signer, _super);
93
- function Signer(provider, address, keyPair) {
94
- var _this = _super.call(this, provider) || this;
95
- _this.keyPair = keyPair;
96
- _this.address = address;
97
- return _this;
46
+ var Signer = /** @class */ (function () {
47
+ function Signer(keyPair) {
48
+ this.keyPair = keyPair;
98
49
  }
99
- /**
100
- * Invoke a function on the starknet contract
101
- *
102
- * [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/gateway/gateway_client.py#L13-L17)
103
- *
104
- * @param transaction - transaction to be invoked
105
- * @returns a confirmation of invoking a function on the starknet contract
106
- */
107
- Signer.prototype.addTransaction = function (transaction) {
50
+ Signer.prototype.getPubKey = function () {
108
51
  return __awaiter(this, void 0, void 0, function () {
109
- var nonceBn, result, calldataDecimal, msgHash, signature;
110
52
  return __generator(this, function (_a) {
111
- switch (_a.label) {
112
- case 0:
113
- if (transaction.type === 'DEPLOY')
114
- return [2 /*return*/, _super.prototype.addTransaction.call(this, transaction)];
115
- (0, minimalistic_assert_1.default)(!transaction.signature, "Adding signatures to a signer transaction currently isn't supported");
116
- if (!transaction.nonce) return [3 /*break*/, 1];
117
- nonceBn = (0, number_1.toBN)(transaction.nonce);
118
- return [3 /*break*/, 3];
119
- case 1: return [4 /*yield*/, this.callContract({
120
- contract_address: this.address,
121
- entry_point_selector: (0, stark_1.getSelectorFromName)('get_nonce'),
122
- })];
123
- case 2:
124
- result = (_a.sent()).result;
125
- nonceBn = (0, number_1.toBN)(result[0]);
126
- _a.label = 3;
127
- case 3:
128
- calldataDecimal = (transaction.calldata || []).map(function (x) { return (0, number_1.toBN)(x).toString(); });
129
- msgHash = (0, encode_1.addHexPrefix)((0, hash_1.hashMessage)(this.address, transaction.contract_address, transaction.entry_point_selector, calldataDecimal, nonceBn.toString()));
130
- signature = (0, ellipticCurve_1.sign)(this.keyPair, msgHash);
131
- return [2 /*return*/, _super.prototype.addTransaction.call(this, {
132
- type: 'INVOKE_FUNCTION',
133
- entry_point_selector: (0, stark_1.getSelectorFromName)('execute'),
134
- calldata: __spreadArray(__spreadArray([
135
- transaction.contract_address,
136
- transaction.entry_point_selector,
137
- calldataDecimal.length.toString()
138
- ], __read(calldataDecimal), false), [
139
- nonceBn.toString(),
140
- ], false).map(function (x) { return (0, number_1.toBN)(x).toString(); }),
141
- contract_address: this.address,
142
- signature: signature,
143
- })];
144
- }
53
+ return [2 /*return*/, (0, ellipticCurve_1.getStarkKey)(this.keyPair)];
145
54
  });
146
55
  });
147
56
  };
148
- /**
149
- * Sign an JSON object with the starknet private key and return the signature
150
- *
151
- * @param json - JSON object to be signed
152
- * @returns the signature of the JSON object
153
- * @throws {Error} if the JSON object is not a valid JSON
154
- */
155
- Signer.prototype.signMessage = function (typedData) {
57
+ Signer.prototype.signTransaction = function (transactions, transactionsDetail, abis) {
58
+ if (abis === void 0) { abis = []; }
156
59
  return __awaiter(this, void 0, void 0, function () {
157
- var _a, _b;
60
+ var _a, contractAddress, entrypoint, _b, calldata, nonce, walletAddress, nonceBn, entrypointSelector, calldataDecimal, msgHash;
158
61
  return __generator(this, function (_c) {
159
- switch (_c.label) {
160
- case 0:
161
- _a = ellipticCurve_1.sign;
162
- _b = [this.keyPair];
163
- return [4 /*yield*/, this.hashMessage(typedData)];
164
- case 1: return [2 /*return*/, _a.apply(void 0, _b.concat([_c.sent()]))];
62
+ if (transactions.length !== 1) {
63
+ throw new Error('Only one transaction at a time is currently supported by this signer');
64
+ }
65
+ if ((abis === null || abis === void 0 ? void 0 : abis.length) !== 0 && abis.length !== transactions.length) {
66
+ throw new Error('ABI must be provided for each transaction or no transaction');
165
67
  }
68
+ _a = transactions[0], contractAddress = _a.contractAddress, entrypoint = _a.entrypoint, _b = _a.calldata, calldata = _b === void 0 ? [] : _b;
69
+ nonce = transactionsDetail.nonce, walletAddress = transactionsDetail.walletAddress;
70
+ nonceBn = (0, number_1.toBN)(nonce);
71
+ entrypointSelector = (0, stark_1.getSelectorFromName)(entrypoint);
72
+ calldataDecimal = (0, number_1.bigNumberishArrayToDecimalStringArray)(calldata);
73
+ msgHash = (0, encode_1.addHexPrefix)((0, hash_1.hashMessage)(walletAddress, contractAddress, entrypointSelector, calldataDecimal, nonceBn.toString()));
74
+ return [2 /*return*/, (0, ellipticCurve_1.sign)(this.keyPair, msgHash)];
166
75
  });
167
76
  });
168
77
  };
169
- /**
170
- * Hash a JSON object with pederson hash and return the hash
171
- *
172
- * @param json - JSON object to be hashed
173
- * @returns the hash of the JSON object
174
- * @throws {Error} if the JSON object is not a valid JSON
175
- */
176
- Signer.prototype.hashMessage = function (typedData) {
78
+ Signer.prototype.signMessage = function (typedData, walletAddress) {
177
79
  return __awaiter(this, void 0, void 0, function () {
80
+ var msgHash;
178
81
  return __generator(this, function (_a) {
179
- return [2 /*return*/, (0, typedData_1.getMessageHash)(typedData, this.address)];
82
+ msgHash = (0, typedData_1.getMessageHash)(typedData, walletAddress);
83
+ return [2 /*return*/, (0, ellipticCurve_1.sign)(this.keyPair, msgHash)];
180
84
  });
181
85
  });
182
86
  };
183
87
  return Signer;
184
- }(provider_1.Provider));
88
+ }());
185
89
  exports.Signer = Signer;
@@ -1,2 +1,2 @@
1
- export * from './default';
2
1
  export * from './interface';
2
+ export * from './default';
@@ -10,5 +10,5 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
10
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11
11
  };
12
12
  Object.defineProperty(exports, "__esModule", { value: true });
13
- __exportStar(require("./default"), exports);
14
13
  __exportStar(require("./interface"), exports);
14
+ __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,18 @@ 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(transactions: Invocation[], transactionsDetail: InvocationsSignerDetails, abis?: Abi[]): Promise<Signature>;
33
32
  }
@@ -1,27 +1,9 @@
1
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
2
  Object.defineProperty(exports, "__esModule", { value: true });
18
3
  exports.SignerInterface = void 0;
19
- var provider_1 = require("../provider");
20
- var SignerInterface = /** @class */ (function (_super) {
21
- __extends(SignerInterface, _super);
4
+ var SignerInterface = /** @class */ (function () {
22
5
  function SignerInterface() {
23
- return _super !== null && _super.apply(this, arguments) || this;
24
6
  }
25
7
  return SignerInterface;
26
- }(provider_1.Provider));
8
+ }());
27
9
  exports.SignerInterface = SignerInterface;