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,101 +1,58 @@
1
- import assert from 'minimalistic-assert';
2
-
3
- import { Provider } from '../provider';
4
- import { AddTransactionResponse, KeyPair, Signature, Transaction } from '../types';
5
- import { sign } from '../utils/ellipticCurve';
1
+ import { Abi, Invocation, InvocationsSignerDetails, KeyPair, Signature } from '../types';
2
+ import { getStarkKey, sign } from '../utils/ellipticCurve';
6
3
  import { addHexPrefix } from '../utils/encode';
7
4
  import { hashMessage } from '../utils/hash';
8
- import { toBN } from '../utils/number';
5
+ import { bigNumberishArrayToDecimalStringArray, toBN } from '../utils/number';
9
6
  import { getSelectorFromName } from '../utils/stark';
10
7
  import { TypedData, getMessageHash } from '../utils/typedData';
11
8
  import { SignerInterface } from './interface';
12
9
 
13
- export class Signer extends Provider implements SignerInterface {
14
- public address: string;
15
-
16
- private keyPair: KeyPair;
10
+ export class Signer implements SignerInterface {
11
+ protected keyPair: KeyPair;
17
12
 
18
- constructor(provider: Provider, address: string, keyPair: KeyPair) {
19
- super(provider);
13
+ constructor(keyPair: KeyPair) {
20
14
  this.keyPair = keyPair;
21
- this.address = address;
22
15
  }
23
16
 
24
- /**
25
- * Invoke a function on the starknet contract
26
- *
27
- * [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/gateway/gateway_client.py#L13-L17)
28
- *
29
- * @param transaction - transaction to be invoked
30
- * @returns a confirmation of invoking a function on the starknet contract
31
- */
32
- public override async addTransaction(transaction: Transaction): Promise<AddTransactionResponse> {
33
- if (transaction.type === 'DEPLOY') return super.addTransaction(transaction);
34
-
35
- assert(
36
- !transaction.signature,
37
- "Adding signatures to a signer transaction currently isn't supported"
38
- );
17
+ public async getPubKey(): Promise<string> {
18
+ return getStarkKey(this.keyPair);
19
+ }
39
20
 
40
- let nonceBn;
41
- if (transaction.nonce) {
42
- nonceBn = toBN(transaction.nonce);
43
- } else {
44
- const { result } = await this.callContract({
45
- contract_address: this.address,
46
- entry_point_selector: getSelectorFromName('get_nonce'),
47
- });
48
- nonceBn = toBN(result[0]);
21
+ public async signTransaction(
22
+ transactions: Invocation[],
23
+ transactionsDetail: InvocationsSignerDetails,
24
+ abis: Abi[] = []
25
+ ): Promise<Signature> {
26
+ if (transactions.length !== 1) {
27
+ throw new Error('Only one transaction at a time is currently supported by this signer');
49
28
  }
29
+ if (abis?.length !== 0 && abis.length !== transactions.length) {
30
+ throw new Error('ABI must be provided for each transaction or no transaction');
31
+ }
32
+ // now use abi to display decoded data somewhere, but as this signer is headless, we can't do that
33
+
34
+ const { contractAddress, entrypoint, calldata = [] } = transactions[0];
35
+ const { nonce, walletAddress } = transactionsDetail;
50
36
 
51
- const calldataDecimal = (transaction.calldata || []).map((x) => toBN(x).toString());
37
+ const nonceBn = toBN(nonce);
38
+ const entrypointSelector = getSelectorFromName(entrypoint);
39
+ const calldataDecimal = bigNumberishArrayToDecimalStringArray(calldata);
52
40
 
53
41
  const msgHash = addHexPrefix(
54
42
  hashMessage(
55
- this.address,
56
- transaction.contract_address,
57
- transaction.entry_point_selector,
43
+ walletAddress,
44
+ contractAddress,
45
+ entrypointSelector,
58
46
  calldataDecimal,
59
47
  nonceBn.toString()
60
48
  )
61
49
  );
62
50
 
63
- const signature = sign(this.keyPair, msgHash);
64
-
65
- return super.addTransaction({
66
- type: 'INVOKE_FUNCTION',
67
- entry_point_selector: getSelectorFromName('execute'),
68
- calldata: [
69
- transaction.contract_address,
70
- transaction.entry_point_selector,
71
- calldataDecimal.length.toString(),
72
- ...calldataDecimal,
73
- nonceBn.toString(),
74
- ].map((x) => toBN(x).toString()),
75
- contract_address: this.address,
76
- signature,
77
- });
78
- }
79
-
80
- /**
81
- * Sign an JSON object with the starknet private key and return the signature
82
- *
83
- * @param json - JSON object to be signed
84
- * @returns the signature of the JSON object
85
- * @throws {Error} if the JSON object is not a valid JSON
86
- */
87
- public async signMessage(typedData: TypedData): Promise<Signature> {
88
- return sign(this.keyPair, await this.hashMessage(typedData));
51
+ return sign(this.keyPair, msgHash);
89
52
  }
90
53
 
91
- /**
92
- * Hash a JSON object with pederson hash and return the hash
93
- *
94
- * @param json - JSON object to be hashed
95
- * @returns the hash of the JSON object
96
- * @throws {Error} if the JSON object is not a valid JSON
97
- */
98
- public async hashMessage(typedData: TypedData): Promise<string> {
99
- return getMessageHash(typedData, this.address);
54
+ public async signMessage(typedData: TypedData, walletAddress: string): Promise<Signature> {
55
+ const msgHash = getMessageHash(typedData, walletAddress);
56
+ return sign(this.keyPair, msgHash);
100
57
  }
101
58
  }
@@ -1,2 +1,2 @@
1
- export * from './default';
2
1
  export * from './interface';
2
+ export * from './default';
@@ -1,20 +1,13 @@
1
- import { Provider } from '../provider';
2
- import { AddTransactionResponse, Signature, Transaction } from '../types';
3
- import { TypedData } from '../utils/typedData/types';
1
+ import { Abi, Invocation, InvocationsSignerDetails, Signature } from '../types';
2
+ import { TypedData } from '../utils/typedData';
4
3
 
5
- export abstract class SignerInterface extends Provider {
6
- public abstract address: string;
4
+ export abstract class SignerInterface {
7
5
  /**
8
- * Invoke a function on the starknet contract
6
+ * Method to get the public key of the signer
9
7
  *
10
- * [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/gateway/gateway_client.py#L13-L17)
11
- *
12
- * @param transaction - transaction to be invoked
13
- * @returns a confirmation of invoking a function on the starknet contract
8
+ * @returns public key of signer as hex string with 0x prefix
14
9
  */
15
- public abstract override addTransaction(
16
- transaction: Transaction
17
- ): Promise<AddTransactionResponse>;
10
+ public abstract getPubKey(): Promise<string>;
18
11
 
19
12
  /**
20
13
  * Sign an JSON object for off-chain usage with the starknet private key and return the signature
@@ -24,15 +17,23 @@ export abstract class SignerInterface extends Provider {
24
17
  * @returns the signature of the JSON object
25
18
  * @throws {Error} if the JSON object is not a valid JSON
26
19
  */
27
- public abstract signMessage(typedData: TypedData): Promise<Signature>;
20
+ public abstract signMessage(typedData: TypedData, walletAddress: string): Promise<Signature>;
28
21
 
29
22
  /**
30
- * Hash a JSON object with pederson hash and return the hash
31
- * This adds a message prefix so it cant be interchanged with transactions
23
+ * Signs a transaction with the starknet private key and returns the signature
32
24
  *
33
- * @param json - JSON object to be hashed
34
- * @returns the hash of the JSON object
35
- * @throws {Error} if the JSON object is not a valid JSON
25
+ * @param invocation the invocation object containing:
26
+ * - contractAddress - the address of the contract
27
+ * - entrypoint - the entrypoint of the contract
28
+ * - calldata - (defaults to []) the calldata
29
+ * - signature - (defaults to []) the signature
30
+ * @param abi (optional) the abi of the contract for better displaying
31
+ *
32
+ * @returns signature
36
33
  */
37
- public abstract hashMessage(typedData: TypedData): Promise<string>;
34
+ public abstract signTransaction(
35
+ transactions: Invocation[],
36
+ transactionsDetail: InvocationsSignerDetails,
37
+ abis?: Abi[]
38
+ ): Promise<Signature>;
38
39
  }
@@ -0,0 +1,171 @@
1
+ import { BlockIdentifier } from '../provider/utils';
2
+ import { BigNumberish } from '../utils/number';
3
+ import {
4
+ Abi,
5
+ BlockNumber,
6
+ CompressedCompiledContract,
7
+ EntryPointType,
8
+ RawCalldata,
9
+ Signature,
10
+ Status,
11
+ TransactionStatus,
12
+ } from './lib';
13
+
14
+ export type Endpoints = {
15
+ get_contract_addresses: {
16
+ QUERY: never;
17
+ REQUEST: never;
18
+ RESPONSE: GetContractAddressesResponse;
19
+ };
20
+ add_transaction: {
21
+ QUERY: never;
22
+ REQUEST: Transaction;
23
+ RESPONSE: AddTransactionResponse;
24
+ };
25
+ get_transaction: {
26
+ QUERY: {
27
+ transactionHash: string;
28
+ };
29
+ REQUEST: never;
30
+ RESPONSE: GetTransactionResponse;
31
+ };
32
+ get_transaction_status: {
33
+ QUERY: {
34
+ transactionHash: string;
35
+ };
36
+ REQUEST: never;
37
+ RESPONSE: GetTransactionStatusResponse;
38
+ };
39
+ get_storage_at: {
40
+ QUERY: {
41
+ contractAddress: string;
42
+ key: number;
43
+ blockIdentifier: BlockIdentifier;
44
+ };
45
+ REQUEST: never;
46
+ RESPONSE: object;
47
+ };
48
+ get_code: {
49
+ QUERY: {
50
+ contractAddress: string;
51
+ blockIdentifier: BlockIdentifier;
52
+ };
53
+ REQUEST: never;
54
+ RESPONSE: GetCodeResponse;
55
+ };
56
+ get_block: {
57
+ QUERY: {
58
+ blockIdentifier: BlockIdentifier;
59
+ };
60
+ REQUEST: never;
61
+ RESPONSE: GetBlockResponse;
62
+ };
63
+ call_contract: {
64
+ QUERY: {
65
+ blockIdentifier: BlockIdentifier;
66
+ };
67
+ REQUEST: CallContractTransaction;
68
+ RESPONSE: CallContractResponse;
69
+ };
70
+ };
71
+
72
+ export type GetContractAddressesResponse = {
73
+ Starknet: string;
74
+ GpsStatementVerifier: string;
75
+ };
76
+
77
+ export type DeployTransaction = {
78
+ type: 'DEPLOY';
79
+ contract_definition: CompressedCompiledContract;
80
+ contract_address_salt: BigNumberish;
81
+ constructor_calldata: string[];
82
+ nonce?: BigNumberish;
83
+ };
84
+
85
+ export type InvokeFunctionTransaction = {
86
+ type: 'INVOKE_FUNCTION';
87
+ contract_address: string;
88
+ signature?: Signature;
89
+ entry_point_type?: EntryPointType;
90
+ entry_point_selector: string;
91
+ calldata?: RawCalldata;
92
+ nonce?: BigNumberish;
93
+ };
94
+
95
+ export type CallContractTransaction = Omit<
96
+ InvokeFunctionTransaction,
97
+ 'type' | 'entry_point_type' | 'nonce'
98
+ >;
99
+
100
+ export type Transaction = DeployTransaction | InvokeFunctionTransaction;
101
+
102
+ export type CallContractResponse = {
103
+ result: string[];
104
+ };
105
+
106
+ export type GetBlockResponse = {
107
+ block_number: number;
108
+ state_root: string;
109
+ block_hash: string;
110
+ transactions: {
111
+ [txHash: string]: Transaction;
112
+ };
113
+ timestamp: number;
114
+ transaction_receipts: {
115
+ [txHash: string]: {
116
+ block_hash: string;
117
+ transaction_hash: string;
118
+ l2_to_l1_messages: {
119
+ to_address: string;
120
+ payload: string[];
121
+ from_address: string;
122
+ }[];
123
+ block_number: BlockNumber;
124
+ status: Status;
125
+ transaction_index: number;
126
+ };
127
+ };
128
+ previous_block_hash: string;
129
+ status: Status;
130
+ };
131
+
132
+ export type GetCodeResponse = {
133
+ bytecode: string[];
134
+ abi: Abi;
135
+ };
136
+
137
+ export type GetTransactionStatusResponse = {
138
+ tx_status: Status;
139
+ block_hash: string;
140
+ };
141
+
142
+ export type GetTransactionResponse = {
143
+ status: Status;
144
+ transaction: Transaction;
145
+ block_hash: string;
146
+ block_number: BlockNumber;
147
+ transaction_index: number;
148
+ transaction_hash: string;
149
+ };
150
+
151
+ export type AddTransactionResponse = {
152
+ code: TransactionStatus;
153
+ transaction_hash: string;
154
+ address?: string;
155
+ };
156
+
157
+ export type TransactionReceipt = {
158
+ status: Status;
159
+ transaction_hash: string;
160
+ transaction_index: number;
161
+ block_hash: string;
162
+ block_number: BlockNumber;
163
+ l2_to_l1_messages: string[];
164
+ events: string[];
165
+ };
166
+
167
+ export type RawArgs = {
168
+ [inputName: string]: string | string[] | { type: 'struct'; [k: string]: BigNumberish };
169
+ };
170
+
171
+ export type Calldata = string[];
@@ -0,0 +1,3 @@
1
+ export * from './lib';
2
+ export * from './api';
3
+ export * from './signer';
@@ -0,0 +1,73 @@
1
+ import type { ec as EC } from 'elliptic';
2
+
3
+ import type { BigNumberish } from '../utils/number';
4
+
5
+ export type KeyPair = EC.KeyPair;
6
+ export type Signature = BigNumberish[];
7
+ export type RawCalldata = BigNumberish[];
8
+
9
+ export type DeployContractPayload = {
10
+ contract: CompiledContract | string;
11
+ constructorCalldata?: RawCalldata;
12
+ addressSalt?: BigNumberish;
13
+ };
14
+
15
+ export type Invocation = {
16
+ contractAddress: string;
17
+ entrypoint: string;
18
+ calldata?: RawCalldata;
19
+ signature?: Signature;
20
+ };
21
+
22
+ export type ExecuteInvocation = Omit<Invocation, 'signature'>;
23
+
24
+ export type InvocationsDetails = {
25
+ nonce?: BigNumberish;
26
+ };
27
+
28
+ export type Call = Omit<Invocation, 'signature' | 'nonce'>;
29
+
30
+ export type Status =
31
+ | 'NOT_RECEIVED'
32
+ | 'RECEIVED'
33
+ | 'PENDING'
34
+ | 'ACCEPTED_ON_L2'
35
+ | 'ACCEPTED_ON_L1'
36
+ | 'REJECTED';
37
+ export type TransactionStatus = 'TRANSACTION_RECEIVED';
38
+ export type Type = 'DEPLOY' | 'INVOKE_FUNCTION';
39
+ export type EntryPointType = 'EXTERNAL';
40
+ export type CompressedProgram = string;
41
+
42
+ export type AbiEntry = { name: string; type: 'felt' | 'felt*' | string };
43
+
44
+ export type FunctionAbi = {
45
+ inputs: AbiEntry[];
46
+ name: string;
47
+ outputs: AbiEntry[];
48
+ stateMutability?: 'view';
49
+ type: 'function';
50
+ };
51
+
52
+ export type StructAbi = {
53
+ members: (AbiEntry & { offset: number })[];
54
+ name: string;
55
+ size: number;
56
+ type: 'struct';
57
+ };
58
+
59
+ export type Abi = Array<FunctionAbi | StructAbi>;
60
+
61
+ export type EntryPointsByType = object;
62
+ export type Program = Record<any, any>;
63
+ export type BlockNumber = 'pending' | null | number;
64
+
65
+ export type CompiledContract = {
66
+ abi: Abi;
67
+ entry_points_by_type: EntryPointsByType;
68
+ program: Program;
69
+ };
70
+
71
+ export type CompressedCompiledContract = Omit<CompiledContract, 'program'> & {
72
+ program: CompressedProgram;
73
+ };
@@ -0,0 +1,5 @@
1
+ import { InvocationsDetails } from './lib';
2
+
3
+ export interface InvocationsSignerDetails extends Required<InvocationsDetails> {
4
+ walletAddress: string;
5
+ }
@@ -0,0 +1,23 @@
1
+ import { MASK_251, ZERO } from '../constants';
2
+ import { addHexPrefix, removeHexPrefix } from './encode';
3
+ import { assertInRange } from './number';
4
+
5
+ export function addAddressPadding(address: string): string {
6
+ return addHexPrefix(removeHexPrefix(address).padStart(64, '0'));
7
+ }
8
+
9
+ export function validateAndParseAddress(address: string): string {
10
+ if (typeof address !== 'string') {
11
+ throw new Error('Invalid Address Type');
12
+ }
13
+
14
+ assertInRange(address, ZERO, MASK_251, 'Starknet Address');
15
+
16
+ const result = addAddressPadding(address);
17
+
18
+ if (!result.match(/^(0x)?[0-9a-fA-F]{64}$/)) {
19
+ throw new Error('Invalid Address Format');
20
+ }
21
+
22
+ return result;
23
+ }
@@ -1,4 +1,4 @@
1
- import BN from 'bn.js';
1
+ import BN, { isBN } from 'bn.js';
2
2
  import assert from 'minimalistic-assert';
3
3
 
4
4
  import { addHexPrefix, removeHexPrefix } from './encode';
@@ -23,6 +23,13 @@ export function hexToDecimalString(hex: string): string {
23
23
  return toBN(`0x${hex.replace(/^0x/, '')}`).toString();
24
24
  }
25
25
 
26
+ export function toFelt(num: BigNumberish): string {
27
+ if (isBN(num)) {
28
+ return num.toString();
29
+ }
30
+ return toBN(num).toString();
31
+ }
32
+
26
33
  /*
27
34
  Asserts input is equal to or greater then lowerBound and lower then upperBound.
28
35
  Assert message specifies inputName.
@@ -42,3 +49,7 @@ export function assertInRange(
42
49
  `Message not signable, ${messageSuffix}.`
43
50
  );
44
51
  }
52
+
53
+ export function bigNumberishArrayToDecimalStringArray(rawCalldata: BigNumberish[]): string[] {
54
+ return rawCalldata.map((x) => toBN(x).toString(10));
55
+ }
@@ -1,6 +1,6 @@
1
1
  import { gzip } from 'pako';
2
2
 
3
- import { CompressedProgram, Program, Signature } from '../types';
3
+ import { Calldata, CompressedProgram, Program, RawArgs, Signature } from '../types';
4
4
  import { genKeyPair, getStarkKey } from './ellipticCurve';
5
5
  import { addHexPrefix, btoaUniversal } from './encode';
6
6
  import { starknetKeccak } from './hash';
@@ -49,3 +49,15 @@ export function formatSignature(sig?: Signature): string[] {
49
49
  return [];
50
50
  }
51
51
  }
52
+
53
+ export function compileCalldata(args: RawArgs): Calldata {
54
+ return Object.values(args).flatMap((value) => {
55
+ if (Array.isArray(value))
56
+ return [toBN(value.length).toString(), ...value.map((x) => toBN(x).toString())];
57
+ if (typeof value === 'object' && 'type' in value)
58
+ return Object.entries(value)
59
+ .filter(([k]) => k !== 'type')
60
+ .map(([, v]) => toBN(v).toString());
61
+ return toBN(value).toString();
62
+ });
63
+ }
package/types/api.d.ts ADDED
@@ -0,0 +1,162 @@
1
+ import { BlockIdentifier } from '../provider/utils';
2
+ import { BigNumberish } from '../utils/number';
3
+ import {
4
+ Abi,
5
+ BlockNumber,
6
+ CompressedCompiledContract,
7
+ EntryPointType,
8
+ RawCalldata,
9
+ Signature,
10
+ Status,
11
+ TransactionStatus,
12
+ } from './lib';
13
+ export declare type Endpoints = {
14
+ get_contract_addresses: {
15
+ QUERY: never;
16
+ REQUEST: never;
17
+ RESPONSE: GetContractAddressesResponse;
18
+ };
19
+ add_transaction: {
20
+ QUERY: never;
21
+ REQUEST: Transaction;
22
+ RESPONSE: AddTransactionResponse;
23
+ };
24
+ get_transaction: {
25
+ QUERY: {
26
+ transactionHash: string;
27
+ };
28
+ REQUEST: never;
29
+ RESPONSE: GetTransactionResponse;
30
+ };
31
+ get_transaction_status: {
32
+ QUERY: {
33
+ transactionHash: string;
34
+ };
35
+ REQUEST: never;
36
+ RESPONSE: GetTransactionStatusResponse;
37
+ };
38
+ get_storage_at: {
39
+ QUERY: {
40
+ contractAddress: string;
41
+ key: number;
42
+ blockIdentifier: BlockIdentifier;
43
+ };
44
+ REQUEST: never;
45
+ RESPONSE: object;
46
+ };
47
+ get_code: {
48
+ QUERY: {
49
+ contractAddress: string;
50
+ blockIdentifier: BlockIdentifier;
51
+ };
52
+ REQUEST: never;
53
+ RESPONSE: GetCodeResponse;
54
+ };
55
+ get_block: {
56
+ QUERY: {
57
+ blockIdentifier: BlockIdentifier;
58
+ };
59
+ REQUEST: never;
60
+ RESPONSE: GetBlockResponse;
61
+ };
62
+ call_contract: {
63
+ QUERY: {
64
+ blockIdentifier: BlockIdentifier;
65
+ };
66
+ REQUEST: CallContractTransaction;
67
+ RESPONSE: CallContractResponse;
68
+ };
69
+ };
70
+ export declare type GetContractAddressesResponse = {
71
+ Starknet: string;
72
+ GpsStatementVerifier: string;
73
+ };
74
+ export declare type DeployTransaction = {
75
+ type: 'DEPLOY';
76
+ contract_definition: CompressedCompiledContract;
77
+ contract_address_salt: BigNumberish;
78
+ constructor_calldata: string[];
79
+ nonce?: BigNumberish;
80
+ };
81
+ export declare type InvokeFunctionTransaction = {
82
+ type: 'INVOKE_FUNCTION';
83
+ contract_address: string;
84
+ signature?: Signature;
85
+ entry_point_type?: EntryPointType;
86
+ entry_point_selector: string;
87
+ calldata?: RawCalldata;
88
+ nonce?: BigNumberish;
89
+ };
90
+ export declare type CallContractTransaction = Omit<
91
+ InvokeFunctionTransaction,
92
+ 'type' | 'entry_point_type' | 'nonce'
93
+ >;
94
+ export declare type Transaction = DeployTransaction | InvokeFunctionTransaction;
95
+ export declare type CallContractResponse = {
96
+ result: string[];
97
+ };
98
+ export declare type GetBlockResponse = {
99
+ block_number: number;
100
+ state_root: string;
101
+ block_hash: string;
102
+ transactions: {
103
+ [txHash: string]: Transaction;
104
+ };
105
+ timestamp: number;
106
+ transaction_receipts: {
107
+ [txHash: string]: {
108
+ block_hash: string;
109
+ transaction_hash: string;
110
+ l2_to_l1_messages: {
111
+ to_address: string;
112
+ payload: string[];
113
+ from_address: string;
114
+ }[];
115
+ block_number: BlockNumber;
116
+ status: Status;
117
+ transaction_index: number;
118
+ };
119
+ };
120
+ previous_block_hash: string;
121
+ status: Status;
122
+ };
123
+ export declare type GetCodeResponse = {
124
+ bytecode: string[];
125
+ abi: Abi;
126
+ };
127
+ export declare type GetTransactionStatusResponse = {
128
+ tx_status: Status;
129
+ block_hash: string;
130
+ };
131
+ export declare type GetTransactionResponse = {
132
+ status: Status;
133
+ transaction: Transaction;
134
+ block_hash: string;
135
+ block_number: BlockNumber;
136
+ transaction_index: number;
137
+ transaction_hash: string;
138
+ };
139
+ export declare type AddTransactionResponse = {
140
+ code: TransactionStatus;
141
+ transaction_hash: string;
142
+ address?: string;
143
+ };
144
+ export declare type TransactionReceipt = {
145
+ status: Status;
146
+ transaction_hash: string;
147
+ transaction_index: number;
148
+ block_hash: string;
149
+ block_number: BlockNumber;
150
+ l2_to_l1_messages: string[];
151
+ events: string[];
152
+ };
153
+ export declare type RawArgs = {
154
+ [inputName: string]:
155
+ | string
156
+ | string[]
157
+ | {
158
+ type: 'struct';
159
+ [k: string]: BigNumberish;
160
+ };
161
+ };
162
+ export declare type Calldata = string[];
File without changes