starknet 3.0.0 → 3.3.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.
- package/CHANGELOG.md +26 -0
- package/__mocks__/ArgentAccount.json +68548 -51944
- package/__mocks__/TestDapp.json +12962 -0
- package/__mocks__/contract.json +33191 -0
- package/__mocks__/multicall.json +8139 -0
- package/__tests__/account.test.ts +63 -49
- package/__tests__/accountContract.test.ts +51 -70
- package/__tests__/contract.test.ts +182 -35
- package/__tests__/fixtures.ts +13 -0
- package/__tests__/provider.test.ts +4 -14
- package/__tests__/utils/__snapshots__/utils.browser.test.ts.snap +2 -2
- package/__tests__/utils/__snapshots__/utils.test.ts.snap +2 -2
- package/__tests__/utils/ellipticalCurve.test.ts +20 -13
- package/__tests__/utils/utils.test.ts +3 -3
- package/account/default.d.ts +4 -4
- package/account/default.js +43 -92
- package/account/interface.d.ts +2 -2
- package/contract.d.ts +68 -9
- package/contract.js +229 -77
- package/dist/account/default.d.ts +3 -3
- package/dist/account/default.js +31 -57
- package/dist/account/interface.d.ts +2 -2
- package/dist/contract.d.ts +68 -6
- package/dist/contract.js +207 -55
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/provider/default.d.ts +15 -2
- package/dist/provider/default.js +61 -17
- package/dist/provider/interface.d.ts +5 -1
- package/dist/signer/default.d.ts +1 -1
- package/dist/signer/default.js +6 -18
- package/dist/signer/interface.d.ts +3 -2
- package/dist/types/api.d.ts +12 -0
- package/dist/types/lib.d.ts +3 -3
- package/dist/utils/ellipticCurve.js +1 -1
- package/dist/utils/hash.d.ts +12 -2
- package/dist/utils/hash.js +37 -9
- package/dist/utils/number.d.ts +1 -0
- package/dist/utils/number.js +28 -2
- package/dist/utils/stark.d.ts +2 -9
- package/dist/utils/stark.js +44 -14
- package/dist/utils/transaction.d.ts +19 -0
- package/dist/utils/transaction.js +75 -0
- package/dist/utils/typedData/index.d.ts +1 -1
- package/dist/utils/typedData/index.js +2 -3
- package/index.d.ts +1 -0
- package/index.js +1 -0
- package/package.json +2 -2
- package/provider/default.d.ts +20 -1
- package/provider/default.js +83 -19
- package/provider/interface.d.ts +5 -1
- package/signer/default.d.ts +1 -1
- package/signer/default.js +10 -44
- package/signer/interface.d.ts +3 -2
- package/src/account/default.ts +21 -43
- package/src/account/interface.ts +2 -2
- package/src/contract.ts +232 -62
- package/src/index.ts +1 -0
- package/src/provider/default.ts +58 -22
- package/src/provider/interface.ts +6 -1
- package/src/signer/default.ts +10 -26
- package/src/signer/interface.ts +3 -2
- package/src/types/api.ts +11 -0
- package/src/types/lib.ts +3 -4
- package/src/utils/ellipticCurve.ts +1 -1
- package/src/utils/hash.ts +39 -12
- package/src/utils/number.ts +8 -1
- package/src/utils/stark.ts +14 -15
- package/src/utils/transaction.ts +50 -0
- package/src/utils/typedData/index.ts +2 -3
- package/types/api.d.ts +15 -0
- package/types/lib.d.ts +3 -3
- package/utils/ellipticCurve.js +1 -1
- package/utils/hash.d.ts +15 -6
- package/utils/hash.js +42 -10
- package/utils/number.d.ts +1 -0
- package/utils/number.js +46 -1
- package/utils/stark.d.ts +2 -9
- package/utils/stark.js +64 -15
- package/utils/transaction.d.ts +19 -0
- package/utils/transaction.js +99 -0
- package/utils/typedData/index.d.ts +1 -1
- package/utils/typedData/index.js +2 -3
package/src/utils/hash.ts
CHANGED
|
@@ -3,9 +3,14 @@ import { keccak256 } from 'ethereum-cryptography/keccak';
|
|
|
3
3
|
import assert from 'minimalistic-assert';
|
|
4
4
|
|
|
5
5
|
import { CONSTANT_POINTS, FIELD_PRIME, MASK_250, ONE, ZERO } from '../constants';
|
|
6
|
+
import { Call } from '../types';
|
|
6
7
|
import { ec } from './ellipticCurve';
|
|
7
8
|
import { addHexPrefix, buf2hex, utf8ToArray } from './encode';
|
|
8
|
-
import { BigNumberish, toBN } from './number';
|
|
9
|
+
import { BigNumberish, bigNumberishArrayToDecimalStringArray, toBN, toHex } from './number';
|
|
10
|
+
import { encodeShortString } from './shortString';
|
|
11
|
+
|
|
12
|
+
export const transactionPrefix = encodeShortString('StarkNet Transaction');
|
|
13
|
+
export const transactionVersion = 0;
|
|
9
14
|
|
|
10
15
|
function keccakHex(value: string): string {
|
|
11
16
|
return addHexPrefix(buf2hex(keccak256(utf8ToArray(value))));
|
|
@@ -22,6 +27,18 @@ export function starknetKeccak(value: string): BN {
|
|
|
22
27
|
return toBN(keccakHex(value)).and(MASK_250);
|
|
23
28
|
}
|
|
24
29
|
|
|
30
|
+
/**
|
|
31
|
+
* Function to get the hex selector from a given function name
|
|
32
|
+
*
|
|
33
|
+
* [Reference](https://github.com/starkware-libs/cairo-lang/blob/master/src/starkware/starknet/public/abi.py#L25-L26)
|
|
34
|
+
* @param funcName - selectors abi function name
|
|
35
|
+
* @returns hex selector of given abi function name
|
|
36
|
+
*/
|
|
37
|
+
export function getSelectorFromName(funcName: string) {
|
|
38
|
+
// sometimes BigInteger pads the hex string with zeros, which isnt allowed in the starknet api
|
|
39
|
+
return toHex(starknetKeccak(funcName));
|
|
40
|
+
}
|
|
41
|
+
|
|
25
42
|
const constantPoints = CONSTANT_POINTS.map((coords: string[]) =>
|
|
26
43
|
ec.curve.point(coords[0], coords[1])
|
|
27
44
|
);
|
|
@@ -47,17 +64,27 @@ export function computeHashOnElements(data: BigNumberish[]) {
|
|
|
47
64
|
return [...data, data.length].reduce((x, y) => pedersen([x, y]), 0).toString();
|
|
48
65
|
}
|
|
49
66
|
|
|
50
|
-
export function
|
|
51
|
-
return computeHashOnElements(calldata);
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
export function hashMessage(
|
|
67
|
+
export function hashMulticall(
|
|
55
68
|
account: string,
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
nonce: string
|
|
69
|
+
transactions: Call[],
|
|
70
|
+
nonce: string,
|
|
71
|
+
maxFee: string
|
|
60
72
|
) {
|
|
61
|
-
const
|
|
62
|
-
|
|
73
|
+
const hashArray = transactions
|
|
74
|
+
.map(({ contractAddress, entrypoint, calldata }) => [
|
|
75
|
+
contractAddress,
|
|
76
|
+
getSelectorFromName(entrypoint),
|
|
77
|
+
computeHashOnElements(calldata || []),
|
|
78
|
+
])
|
|
79
|
+
.map(bigNumberishArrayToDecimalStringArray)
|
|
80
|
+
.map(computeHashOnElements);
|
|
81
|
+
|
|
82
|
+
return computeHashOnElements([
|
|
83
|
+
transactionPrefix,
|
|
84
|
+
account,
|
|
85
|
+
computeHashOnElements(hashArray),
|
|
86
|
+
nonce,
|
|
87
|
+
maxFee,
|
|
88
|
+
transactionVersion,
|
|
89
|
+
]);
|
|
63
90
|
}
|
package/src/utils/number.ts
CHANGED
|
@@ -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.
|
package/src/utils/stark.ts
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
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
|
-
import { starknetKeccak } from './hash';
|
|
7
6
|
import { stringify } from './json';
|
|
8
|
-
import { toBN
|
|
7
|
+
import { toBN } from './number';
|
|
9
8
|
|
|
10
9
|
/**
|
|
11
10
|
* Function to compress compiled cairo program
|
|
@@ -20,18 +19,6 @@ export function compressProgram(jsonProgram: Program | string): CompressedProgra
|
|
|
20
19
|
return btoaUniversal(compressedProgram);
|
|
21
20
|
}
|
|
22
21
|
|
|
23
|
-
/**
|
|
24
|
-
* Function to get the hex selector from a given function name
|
|
25
|
-
*
|
|
26
|
-
* [Reference](https://github.com/starkware-libs/cairo-lang/blob/master/src/starkware/starknet/public/abi.py#L25-L26)
|
|
27
|
-
* @param funcName - selectors abi function name
|
|
28
|
-
* @returns hex selector of given abi function name
|
|
29
|
-
*/
|
|
30
|
-
export function getSelectorFromName(funcName: string) {
|
|
31
|
-
// sometimes BigInteger pads the hex string with zeros, which isnt allowed in the starknet api
|
|
32
|
-
return toHex(starknetKeccak(funcName));
|
|
33
|
-
}
|
|
34
|
-
|
|
35
22
|
export function randomAddress(): string {
|
|
36
23
|
const randomKeyPair = genKeyPair();
|
|
37
24
|
return getStarkKey(randomKeyPair);
|
|
@@ -49,3 +36,15 @@ export function formatSignature(sig?: Signature): string[] {
|
|
|
49
36
|
return [];
|
|
50
37
|
}
|
|
51
38
|
}
|
|
39
|
+
|
|
40
|
+
export function compileCalldata(args: RawArgs): Calldata {
|
|
41
|
+
return Object.values(args).flatMap((value) => {
|
|
42
|
+
if (Array.isArray(value))
|
|
43
|
+
return [toBN(value.length).toString(), ...value.map((x) => toBN(x).toString())];
|
|
44
|
+
if (typeof value === 'object' && 'type' in value)
|
|
45
|
+
return Object.entries(value)
|
|
46
|
+
.filter(([k]) => k !== 'type')
|
|
47
|
+
.map(([, v]) => toBN(v).toString());
|
|
48
|
+
return toBN(value).toString();
|
|
49
|
+
});
|
|
50
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { ParsedStruct } from '../contract';
|
|
2
|
+
import { Call } from '../types';
|
|
3
|
+
import { getSelectorFromName } from './hash';
|
|
4
|
+
import { BigNumberish, bigNumberishArrayToDecimalStringArray, toBN } from './number';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Transforms a list of Calls, each with their own calldata, into
|
|
8
|
+
* two arrays: one with the entrypoints, and one with the concatenated calldata.
|
|
9
|
+
* @param calls
|
|
10
|
+
* @returns
|
|
11
|
+
*/
|
|
12
|
+
export const transformCallsToMulticallArrays = (calls: Call[]) => {
|
|
13
|
+
const callArray: ParsedStruct[] = [];
|
|
14
|
+
const calldata: BigNumberish[] = [];
|
|
15
|
+
calls.forEach((call) => {
|
|
16
|
+
const data = call.calldata || [];
|
|
17
|
+
callArray.push({
|
|
18
|
+
to: toBN(call.contractAddress).toString(10),
|
|
19
|
+
selector: toBN(getSelectorFromName(call.entrypoint)).toString(10),
|
|
20
|
+
data_offset: calldata.length.toString(),
|
|
21
|
+
data_len: data.length.toString(),
|
|
22
|
+
});
|
|
23
|
+
calldata.push(...data);
|
|
24
|
+
});
|
|
25
|
+
return {
|
|
26
|
+
callArray,
|
|
27
|
+
calldata: bigNumberishArrayToDecimalStringArray(calldata),
|
|
28
|
+
};
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Transforms a list of calls in the full flattened calldata expected
|
|
33
|
+
* by the __execute__ protocol.
|
|
34
|
+
* @param calls
|
|
35
|
+
* @returns
|
|
36
|
+
*/
|
|
37
|
+
export const fromCallsToExecuteCalldata = (calls: Call[]): string[] => {
|
|
38
|
+
const { callArray, calldata } = transformCallsToMulticallArrays(calls);
|
|
39
|
+
return [
|
|
40
|
+
callArray.length.toString(),
|
|
41
|
+
...callArray
|
|
42
|
+
.map(
|
|
43
|
+
({ to, selector, data_offset, data_len }) =>
|
|
44
|
+
[to, selector, data_offset, data_len] as string[]
|
|
45
|
+
)
|
|
46
|
+
.flat(),
|
|
47
|
+
calldata.length.toString(),
|
|
48
|
+
...calldata,
|
|
49
|
+
];
|
|
50
|
+
};
|
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import { computeHashOnElements } from '../hash';
|
|
1
|
+
import { computeHashOnElements, getSelectorFromName } from '../hash';
|
|
2
2
|
import { BigNumberish, toBN, toHex } from '../number';
|
|
3
3
|
import { encodeShortString } from '../shortString';
|
|
4
|
-
import { getSelectorFromName } from '../stark';
|
|
5
4
|
import { TypedData } from './types';
|
|
6
5
|
import { validateTypedData } from './utils';
|
|
7
6
|
|
|
@@ -161,7 +160,7 @@ export const getStructHash = <T extends TypedData>(
|
|
|
161
160
|
* with Keccak256.
|
|
162
161
|
*
|
|
163
162
|
* @param {TypedData} typedData
|
|
164
|
-
* @param {
|
|
163
|
+
* @param {BigNumberish} account
|
|
165
164
|
* @return {string}
|
|
166
165
|
*/
|
|
167
166
|
export const getMessageHash = (typedData: TypedData, account: BigNumberish): string => {
|
package/types/api.d.ts
CHANGED
|
@@ -127,6 +127,11 @@ export declare type GetCodeResponse = {
|
|
|
127
127
|
export declare type GetTransactionStatusResponse = {
|
|
128
128
|
tx_status: Status;
|
|
129
129
|
block_hash: string;
|
|
130
|
+
tx_failure_reason?: {
|
|
131
|
+
tx_id: number;
|
|
132
|
+
code: string;
|
|
133
|
+
error_message: string;
|
|
134
|
+
};
|
|
130
135
|
};
|
|
131
136
|
export declare type GetTransactionResponse = {
|
|
132
137
|
status: Status;
|
|
@@ -150,3 +155,13 @@ export declare type TransactionReceipt = {
|
|
|
150
155
|
l2_to_l1_messages: string[];
|
|
151
156
|
events: string[];
|
|
152
157
|
};
|
|
158
|
+
export declare type RawArgs = {
|
|
159
|
+
[inputName: string]:
|
|
160
|
+
| string
|
|
161
|
+
| string[]
|
|
162
|
+
| {
|
|
163
|
+
type: 'struct';
|
|
164
|
+
[k: string]: BigNumberish;
|
|
165
|
+
};
|
|
166
|
+
};
|
|
167
|
+
export declare type Calldata = string[];
|
package/types/lib.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ import type { ec as EC } from 'elliptic';
|
|
|
2
2
|
|
|
3
3
|
import type { BigNumberish } from '../utils/number';
|
|
4
4
|
export declare type KeyPair = EC.KeyPair;
|
|
5
|
-
export declare type Signature =
|
|
5
|
+
export declare type Signature = string[];
|
|
6
6
|
export declare type RawCalldata = BigNumberish[];
|
|
7
7
|
export declare type DeployContractPayload = {
|
|
8
8
|
contract: CompiledContract | string;
|
|
@@ -15,11 +15,11 @@ export declare type Invocation = {
|
|
|
15
15
|
calldata?: RawCalldata;
|
|
16
16
|
signature?: Signature;
|
|
17
17
|
};
|
|
18
|
-
export declare type
|
|
18
|
+
export declare type Call = Omit<Invocation, 'signature'>;
|
|
19
19
|
export declare type InvocationsDetails = {
|
|
20
20
|
nonce?: BigNumberish;
|
|
21
|
+
maxFee?: BigNumberish;
|
|
21
22
|
};
|
|
22
|
-
export declare type Call = Omit<Invocation, 'signature' | 'nonce'>;
|
|
23
23
|
export declare type Status =
|
|
24
24
|
| 'NOT_RECEIVED'
|
|
25
25
|
| 'RECEIVED'
|
package/utils/ellipticCurve.js
CHANGED
|
@@ -136,7 +136,7 @@ function sign(keyPair, msgHash) {
|
|
|
136
136
|
(0, number_1.toBN)((0, encode_1.addHexPrefix)(constants_1.MAX_ECDSA_VAL)),
|
|
137
137
|
'w'
|
|
138
138
|
);
|
|
139
|
-
return [r, s];
|
|
139
|
+
return [r.toString(), s.toString()];
|
|
140
140
|
}
|
|
141
141
|
exports.sign = sign;
|
|
142
142
|
function chunkArray(arr, n) {
|
package/utils/hash.d.ts
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import BN from 'bn.js';
|
|
2
2
|
|
|
3
|
+
import { Call } from '../types';
|
|
3
4
|
import { BigNumberish } from './number';
|
|
5
|
+
export declare const transactionPrefix: string;
|
|
6
|
+
export declare const transactionVersion = 0;
|
|
4
7
|
/**
|
|
5
8
|
* Function to get the starknet keccak hash from a string
|
|
6
9
|
*
|
|
@@ -9,13 +12,19 @@ import { BigNumberish } from './number';
|
|
|
9
12
|
* @returns starknet keccak hash as BigNumber
|
|
10
13
|
*/
|
|
11
14
|
export declare function starknetKeccak(value: string): BN;
|
|
15
|
+
/**
|
|
16
|
+
* Function to get the hex selector from a given function name
|
|
17
|
+
*
|
|
18
|
+
* [Reference](https://github.com/starkware-libs/cairo-lang/blob/master/src/starkware/starknet/public/abi.py#L25-L26)
|
|
19
|
+
* @param funcName - selectors abi function name
|
|
20
|
+
* @returns hex selector of given abi function name
|
|
21
|
+
*/
|
|
22
|
+
export declare function getSelectorFromName(funcName: string): string;
|
|
12
23
|
export declare function pedersen(input: [BigNumberish, BigNumberish]): string;
|
|
13
24
|
export declare function computeHashOnElements(data: BigNumberish[]): string;
|
|
14
|
-
export declare function
|
|
15
|
-
export declare function hashMessage(
|
|
25
|
+
export declare function hashMulticall(
|
|
16
26
|
account: string,
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
nonce: string
|
|
27
|
+
transactions: Call[],
|
|
28
|
+
nonce: string,
|
|
29
|
+
maxFee: string
|
|
21
30
|
): string;
|
package/utils/hash.js
CHANGED
|
@@ -39,11 +39,13 @@ var __importDefault =
|
|
|
39
39
|
return mod && mod.__esModule ? mod : { default: mod };
|
|
40
40
|
};
|
|
41
41
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
42
|
-
exports.
|
|
43
|
-
exports.hashCalldata =
|
|
42
|
+
exports.hashMulticall =
|
|
44
43
|
exports.computeHashOnElements =
|
|
45
44
|
exports.pedersen =
|
|
45
|
+
exports.getSelectorFromName =
|
|
46
46
|
exports.starknetKeccak =
|
|
47
|
+
exports.transactionVersion =
|
|
48
|
+
exports.transactionPrefix =
|
|
47
49
|
void 0;
|
|
48
50
|
var keccak_1 = require('ethereum-cryptography/keccak');
|
|
49
51
|
var minimalistic_assert_1 = __importDefault(require('minimalistic-assert'));
|
|
@@ -51,6 +53,9 @@ var constants_1 = require('../constants');
|
|
|
51
53
|
var ellipticCurve_1 = require('./ellipticCurve');
|
|
52
54
|
var encode_1 = require('./encode');
|
|
53
55
|
var number_1 = require('./number');
|
|
56
|
+
var shortString_1 = require('./shortString');
|
|
57
|
+
exports.transactionPrefix = (0, shortString_1.encodeShortString)('StarkNet Transaction');
|
|
58
|
+
exports.transactionVersion = 0;
|
|
54
59
|
function keccakHex(value) {
|
|
55
60
|
return (0, encode_1.addHexPrefix)(
|
|
56
61
|
(0, encode_1.buf2hex)((0, keccak_1.keccak256)((0, encode_1.utf8ToArray)(value)))
|
|
@@ -67,6 +72,18 @@ function starknetKeccak(value) {
|
|
|
67
72
|
return (0, number_1.toBN)(keccakHex(value)).and(constants_1.MASK_250);
|
|
68
73
|
}
|
|
69
74
|
exports.starknetKeccak = starknetKeccak;
|
|
75
|
+
/**
|
|
76
|
+
* Function to get the hex selector from a given function name
|
|
77
|
+
*
|
|
78
|
+
* [Reference](https://github.com/starkware-libs/cairo-lang/blob/master/src/starkware/starknet/public/abi.py#L25-L26)
|
|
79
|
+
* @param funcName - selectors abi function name
|
|
80
|
+
* @returns hex selector of given abi function name
|
|
81
|
+
*/
|
|
82
|
+
function getSelectorFromName(funcName) {
|
|
83
|
+
// sometimes BigInteger pads the hex string with zeros, which isnt allowed in the starknet api
|
|
84
|
+
return (0, number_1.toHex)(starknetKeccak(funcName));
|
|
85
|
+
}
|
|
86
|
+
exports.getSelectorFromName = getSelectorFromName;
|
|
70
87
|
var constantPoints = constants_1.CONSTANT_POINTS.map(function (coords) {
|
|
71
88
|
return ellipticCurve_1.ec.curve.point(coords[0], coords[1]);
|
|
72
89
|
});
|
|
@@ -99,12 +116,27 @@ function computeHashOnElements(data) {
|
|
|
99
116
|
.toString();
|
|
100
117
|
}
|
|
101
118
|
exports.computeHashOnElements = computeHashOnElements;
|
|
102
|
-
function
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
119
|
+
function hashMulticall(account, transactions, nonce, maxFee) {
|
|
120
|
+
var hashArray = transactions
|
|
121
|
+
.map(function (_a) {
|
|
122
|
+
var contractAddress = _a.contractAddress,
|
|
123
|
+
entrypoint = _a.entrypoint,
|
|
124
|
+
calldata = _a.calldata;
|
|
125
|
+
return [
|
|
126
|
+
contractAddress,
|
|
127
|
+
getSelectorFromName(entrypoint),
|
|
128
|
+
computeHashOnElements(calldata || []),
|
|
129
|
+
];
|
|
130
|
+
})
|
|
131
|
+
.map(number_1.bigNumberishArrayToDecimalStringArray)
|
|
132
|
+
.map(computeHashOnElements);
|
|
133
|
+
return computeHashOnElements([
|
|
134
|
+
exports.transactionPrefix,
|
|
135
|
+
account,
|
|
136
|
+
computeHashOnElements(hashArray),
|
|
137
|
+
nonce,
|
|
138
|
+
maxFee,
|
|
139
|
+
exports.transactionVersion,
|
|
140
|
+
]);
|
|
109
141
|
}
|
|
110
|
-
exports.
|
|
142
|
+
exports.hashMulticall = hashMulticall;
|
package/utils/number.d.ts
CHANGED
|
@@ -4,6 +4,7 @@ export declare function isHex(hex: string): boolean;
|
|
|
4
4
|
export declare function toBN(number: BigNumberish, base?: number | 'hex'): BN;
|
|
5
5
|
export declare function toHex(number: BN): string;
|
|
6
6
|
export declare function hexToDecimalString(hex: string): string;
|
|
7
|
+
export declare function toFelt(num: BigNumberish): string;
|
|
7
8
|
export declare function assertInRange(
|
|
8
9
|
input: BigNumberish,
|
|
9
10
|
lowerBound: BigNumberish,
|
package/utils/number.js
CHANGED
|
@@ -1,4 +1,41 @@
|
|
|
1
1
|
'use strict';
|
|
2
|
+
var __createBinding =
|
|
3
|
+
(this && this.__createBinding) ||
|
|
4
|
+
(Object.create
|
|
5
|
+
? function (o, m, k, k2) {
|
|
6
|
+
if (k2 === undefined) k2 = k;
|
|
7
|
+
Object.defineProperty(o, k2, {
|
|
8
|
+
enumerable: true,
|
|
9
|
+
get: function () {
|
|
10
|
+
return m[k];
|
|
11
|
+
},
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
: function (o, m, k, k2) {
|
|
15
|
+
if (k2 === undefined) k2 = k;
|
|
16
|
+
o[k2] = m[k];
|
|
17
|
+
});
|
|
18
|
+
var __setModuleDefault =
|
|
19
|
+
(this && this.__setModuleDefault) ||
|
|
20
|
+
(Object.create
|
|
21
|
+
? function (o, v) {
|
|
22
|
+
Object.defineProperty(o, 'default', { enumerable: true, value: v });
|
|
23
|
+
}
|
|
24
|
+
: function (o, v) {
|
|
25
|
+
o['default'] = v;
|
|
26
|
+
});
|
|
27
|
+
var __importStar =
|
|
28
|
+
(this && this.__importStar) ||
|
|
29
|
+
function (mod) {
|
|
30
|
+
if (mod && mod.__esModule) return mod;
|
|
31
|
+
var result = {};
|
|
32
|
+
if (mod != null)
|
|
33
|
+
for (var k in mod)
|
|
34
|
+
if (k !== 'default' && Object.prototype.hasOwnProperty.call(mod, k))
|
|
35
|
+
__createBinding(result, mod, k);
|
|
36
|
+
__setModuleDefault(result, mod);
|
|
37
|
+
return result;
|
|
38
|
+
};
|
|
2
39
|
var __importDefault =
|
|
3
40
|
(this && this.__importDefault) ||
|
|
4
41
|
function (mod) {
|
|
@@ -7,12 +44,13 @@ var __importDefault =
|
|
|
7
44
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
8
45
|
exports.bigNumberishArrayToDecimalStringArray =
|
|
9
46
|
exports.assertInRange =
|
|
47
|
+
exports.toFelt =
|
|
10
48
|
exports.hexToDecimalString =
|
|
11
49
|
exports.toHex =
|
|
12
50
|
exports.toBN =
|
|
13
51
|
exports.isHex =
|
|
14
52
|
void 0;
|
|
15
|
-
var bn_js_1 =
|
|
53
|
+
var bn_js_1 = __importStar(require('bn.js'));
|
|
16
54
|
var minimalistic_assert_1 = __importDefault(require('minimalistic-assert'));
|
|
17
55
|
var encode_1 = require('./encode');
|
|
18
56
|
function isHex(hex) {
|
|
@@ -33,6 +71,13 @@ function hexToDecimalString(hex) {
|
|
|
33
71
|
return toBN('0x' + hex.replace(/^0x/, '')).toString();
|
|
34
72
|
}
|
|
35
73
|
exports.hexToDecimalString = hexToDecimalString;
|
|
74
|
+
function toFelt(num) {
|
|
75
|
+
if ((0, bn_js_1.isBN)(num)) {
|
|
76
|
+
return num.toString();
|
|
77
|
+
}
|
|
78
|
+
return toBN(num).toString();
|
|
79
|
+
}
|
|
80
|
+
exports.toFelt = toFelt;
|
|
36
81
|
/*
|
|
37
82
|
Asserts input is equal to or greater then lowerBound and lower then upperBound.
|
|
38
83
|
Assert message specifies inputName.
|
package/utils/stark.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { CompressedProgram, Program, Signature } from '../types';
|
|
1
|
+
import { Calldata, CompressedProgram, Program, RawArgs, Signature } from '../types';
|
|
2
2
|
/**
|
|
3
3
|
* Function to compress compiled cairo program
|
|
4
4
|
*
|
|
@@ -7,14 +7,7 @@ import { CompressedProgram, Program, Signature } from '../types';
|
|
|
7
7
|
* @returns Compressed cairo program
|
|
8
8
|
*/
|
|
9
9
|
export declare function compressProgram(jsonProgram: Program | string): CompressedProgram;
|
|
10
|
-
/**
|
|
11
|
-
* Function to get the hex selector from a given function name
|
|
12
|
-
*
|
|
13
|
-
* [Reference](https://github.com/starkware-libs/cairo-lang/blob/master/src/starkware/starknet/public/abi.py#L25-L26)
|
|
14
|
-
* @param funcName - selectors abi function name
|
|
15
|
-
* @returns hex selector of given abi function name
|
|
16
|
-
*/
|
|
17
|
-
export declare function getSelectorFromName(funcName: string): string;
|
|
18
10
|
export declare function randomAddress(): string;
|
|
19
11
|
export declare function makeAddress(input: string): string;
|
|
20
12
|
export declare function formatSignature(sig?: Signature): string[];
|
|
13
|
+
export declare function compileCalldata(args: RawArgs): Calldata;
|
package/utils/stark.js
CHANGED
|
@@ -1,15 +1,48 @@
|
|
|
1
1
|
'use strict';
|
|
2
|
+
var __read =
|
|
3
|
+
(this && this.__read) ||
|
|
4
|
+
function (o, n) {
|
|
5
|
+
var m = typeof Symbol === 'function' && o[Symbol.iterator];
|
|
6
|
+
if (!m) return o;
|
|
7
|
+
var i = m.call(o),
|
|
8
|
+
r,
|
|
9
|
+
ar = [],
|
|
10
|
+
e;
|
|
11
|
+
try {
|
|
12
|
+
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
13
|
+
} catch (error) {
|
|
14
|
+
e = { error: error };
|
|
15
|
+
} finally {
|
|
16
|
+
try {
|
|
17
|
+
if (r && !r.done && (m = i['return'])) m.call(i);
|
|
18
|
+
} finally {
|
|
19
|
+
if (e) throw e.error;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
return ar;
|
|
23
|
+
};
|
|
24
|
+
var __spreadArray =
|
|
25
|
+
(this && this.__spreadArray) ||
|
|
26
|
+
function (to, from, pack) {
|
|
27
|
+
if (pack || arguments.length === 2)
|
|
28
|
+
for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
29
|
+
if (ar || !(i in from)) {
|
|
30
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
31
|
+
ar[i] = from[i];
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
35
|
+
};
|
|
2
36
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
3
|
-
exports.
|
|
37
|
+
exports.compileCalldata =
|
|
38
|
+
exports.formatSignature =
|
|
4
39
|
exports.makeAddress =
|
|
5
40
|
exports.randomAddress =
|
|
6
|
-
exports.getSelectorFromName =
|
|
7
41
|
exports.compressProgram =
|
|
8
42
|
void 0;
|
|
9
43
|
var pako_1 = require('pako');
|
|
10
44
|
var ellipticCurve_1 = require('./ellipticCurve');
|
|
11
45
|
var encode_1 = require('./encode');
|
|
12
|
-
var hash_1 = require('./hash');
|
|
13
46
|
var json_1 = require('./json');
|
|
14
47
|
var number_1 = require('./number');
|
|
15
48
|
/**
|
|
@@ -26,18 +59,6 @@ function compressProgram(jsonProgram) {
|
|
|
26
59
|
return (0, encode_1.btoaUniversal)(compressedProgram);
|
|
27
60
|
}
|
|
28
61
|
exports.compressProgram = compressProgram;
|
|
29
|
-
/**
|
|
30
|
-
* Function to get the hex selector from a given function name
|
|
31
|
-
*
|
|
32
|
-
* [Reference](https://github.com/starkware-libs/cairo-lang/blob/master/src/starkware/starknet/public/abi.py#L25-L26)
|
|
33
|
-
* @param funcName - selectors abi function name
|
|
34
|
-
* @returns hex selector of given abi function name
|
|
35
|
-
*/
|
|
36
|
-
function getSelectorFromName(funcName) {
|
|
37
|
-
// sometimes BigInteger pads the hex string with zeros, which isnt allowed in the starknet api
|
|
38
|
-
return (0, number_1.toHex)((0, hash_1.starknetKeccak)(funcName));
|
|
39
|
-
}
|
|
40
|
-
exports.getSelectorFromName = getSelectorFromName;
|
|
41
62
|
function randomAddress() {
|
|
42
63
|
var randomKeyPair = (0, ellipticCurve_1.genKeyPair)();
|
|
43
64
|
return (0, ellipticCurve_1.getStarkKey)(randomKeyPair);
|
|
@@ -62,3 +83,31 @@ function formatSignature(sig) {
|
|
|
62
83
|
}
|
|
63
84
|
}
|
|
64
85
|
exports.formatSignature = formatSignature;
|
|
86
|
+
function compileCalldata(args) {
|
|
87
|
+
return Object.values(args).flatMap(function (value) {
|
|
88
|
+
if (Array.isArray(value))
|
|
89
|
+
return __spreadArray(
|
|
90
|
+
[(0, number_1.toBN)(value.length).toString()],
|
|
91
|
+
__read(
|
|
92
|
+
value.map(function (x) {
|
|
93
|
+
return (0, number_1.toBN)(x).toString();
|
|
94
|
+
})
|
|
95
|
+
),
|
|
96
|
+
false
|
|
97
|
+
);
|
|
98
|
+
if (typeof value === 'object' && 'type' in value)
|
|
99
|
+
return Object.entries(value)
|
|
100
|
+
.filter(function (_a) {
|
|
101
|
+
var _b = __read(_a, 1),
|
|
102
|
+
k = _b[0];
|
|
103
|
+
return k !== 'type';
|
|
104
|
+
})
|
|
105
|
+
.map(function (_a) {
|
|
106
|
+
var _b = __read(_a, 2),
|
|
107
|
+
v = _b[1];
|
|
108
|
+
return (0, number_1.toBN)(v).toString();
|
|
109
|
+
});
|
|
110
|
+
return (0, number_1.toBN)(value).toString();
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
exports.compileCalldata = compileCalldata;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { ParsedStruct } from '../contract';
|
|
2
|
+
import { Call } from '../types';
|
|
3
|
+
/**
|
|
4
|
+
* Transforms a list of Calls, each with their own calldata, into
|
|
5
|
+
* two arrays: one with the entrypoints, and one with the concatenated calldata.
|
|
6
|
+
* @param calls
|
|
7
|
+
* @returns
|
|
8
|
+
*/
|
|
9
|
+
export declare const transformCallsToMulticallArrays: (calls: Call[]) => {
|
|
10
|
+
callArray: ParsedStruct[];
|
|
11
|
+
calldata: string[];
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* Transforms a list of calls in the full flattened calldata expected
|
|
15
|
+
* by the __execute__ protocol.
|
|
16
|
+
* @param calls
|
|
17
|
+
* @returns
|
|
18
|
+
*/
|
|
19
|
+
export declare const fromCallsToExecuteCalldata: (calls: Call[]) => string[];
|