starknet 3.1.0 → 3.2.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 +7 -0
- package/__mocks__/ArgentAccount.json +68548 -51944
- package/__mocks__/TestDapp.json +12962 -0
- package/__tests__/account.test.ts +61 -48
- package/__tests__/accountContract.test.ts +50 -70
- package/__tests__/contract.test.ts +28 -16
- package/__tests__/fixtures.ts +13 -0
- package/__tests__/provider.test.ts +3 -15
- 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 +42 -90
- package/account/interface.d.ts +2 -2
- package/dist/account/default.d.ts +3 -3
- package/dist/account/default.js +30 -55
- package/dist/account/interface.d.ts +2 -2
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/provider/default.js +28 -12
- 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 +5 -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/stark.d.ts +0 -8
- package/dist/utils/stark.js +1 -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 +1 -1
- package/provider/default.js +39 -14
- 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 -42
- package/src/account/interface.ts +2 -2
- package/src/index.ts +1 -0
- package/src/provider/default.ts +22 -17
- package/src/signer/default.ts +10 -26
- package/src/signer/interface.ts +3 -2
- package/src/types/api.ts +5 -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/stark.ts +1 -14
- package/src/utils/transaction.ts +50 -0
- package/src/utils/typedData/index.ts +2 -3
- package/types/api.d.ts +5 -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/stark.d.ts +0 -8
- package/utils/stark.js +0 -14
- 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/dist/signer/default.js
CHANGED
|
@@ -38,10 +38,7 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
|
38
38
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
39
|
exports.Signer = void 0;
|
|
40
40
|
var ellipticCurve_1 = require("../utils/ellipticCurve");
|
|
41
|
-
var encode_1 = require("../utils/encode");
|
|
42
41
|
var hash_1 = require("../utils/hash");
|
|
43
|
-
var number_1 = require("../utils/number");
|
|
44
|
-
var stark_1 = require("../utils/stark");
|
|
45
42
|
var typedData_1 = require("../utils/typedData");
|
|
46
43
|
var Signer = /** @class */ (function () {
|
|
47
44
|
function Signer(keyPair) {
|
|
@@ -55,31 +52,22 @@ var Signer = /** @class */ (function () {
|
|
|
55
52
|
});
|
|
56
53
|
};
|
|
57
54
|
Signer.prototype.signTransaction = function (transactions, transactionsDetail, abis) {
|
|
58
|
-
if (abis === void 0) { abis = []; }
|
|
59
55
|
return __awaiter(this, void 0, void 0, function () {
|
|
60
|
-
var
|
|
61
|
-
return __generator(this, function (
|
|
62
|
-
if (
|
|
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) {
|
|
56
|
+
var msgHash;
|
|
57
|
+
return __generator(this, function (_a) {
|
|
58
|
+
if (abis && abis.length !== transactions.length) {
|
|
66
59
|
throw new Error('ABI must be provided for each transaction or no transaction');
|
|
67
60
|
}
|
|
68
|
-
|
|
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()));
|
|
61
|
+
msgHash = (0, hash_1.hashMulticall)(transactionsDetail.walletAddress, transactions, transactionsDetail.nonce.toString(), transactionsDetail.maxFee.toString());
|
|
74
62
|
return [2 /*return*/, (0, ellipticCurve_1.sign)(this.keyPair, msgHash)];
|
|
75
63
|
});
|
|
76
64
|
});
|
|
77
65
|
};
|
|
78
|
-
Signer.prototype.signMessage = function (typedData,
|
|
66
|
+
Signer.prototype.signMessage = function (typedData, accountAddress) {
|
|
79
67
|
return __awaiter(this, void 0, void 0, function () {
|
|
80
68
|
var msgHash;
|
|
81
69
|
return __generator(this, function (_a) {
|
|
82
|
-
msgHash = (0, typedData_1.getMessageHash)(typedData,
|
|
70
|
+
msgHash = (0, typedData_1.getMessageHash)(typedData, accountAddress);
|
|
83
71
|
return [2 /*return*/, (0, ellipticCurve_1.sign)(this.keyPair, msgHash)];
|
|
84
72
|
});
|
|
85
73
|
});
|
|
@@ -11,11 +11,12 @@ export declare abstract class SignerInterface {
|
|
|
11
11
|
* Sign an JSON object for off-chain usage with the starknet private key and return the signature
|
|
12
12
|
* This adds a message prefix so it cant be interchanged with transactions
|
|
13
13
|
*
|
|
14
|
-
* @param
|
|
14
|
+
* @param typedData - JSON object to be signed
|
|
15
|
+
* @param accountAddress - account
|
|
15
16
|
* @returns the signature of the JSON object
|
|
16
17
|
* @throws {Error} if the JSON object is not a valid JSON
|
|
17
18
|
*/
|
|
18
|
-
abstract signMessage(typedData: TypedData,
|
|
19
|
+
abstract signMessage(typedData: TypedData, accountAddress: string): Promise<Signature>;
|
|
19
20
|
/**
|
|
20
21
|
* Signs a transaction with the starknet private key and returns the signature
|
|
21
22
|
*
|
package/dist/types/api.d.ts
CHANGED
|
@@ -115,6 +115,11 @@ export declare type GetCodeResponse = {
|
|
|
115
115
|
export declare type GetTransactionStatusResponse = {
|
|
116
116
|
tx_status: Status;
|
|
117
117
|
block_hash: string;
|
|
118
|
+
tx_failure_reason?: {
|
|
119
|
+
tx_id: number;
|
|
120
|
+
code: string;
|
|
121
|
+
error_message: string;
|
|
122
|
+
};
|
|
118
123
|
};
|
|
119
124
|
export declare type GetTransactionResponse = {
|
|
120
125
|
status: Status;
|
package/dist/types/lib.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { ec as EC } from 'elliptic';
|
|
2
2
|
import type { BigNumberish } from '../utils/number';
|
|
3
3
|
export declare type KeyPair = EC.KeyPair;
|
|
4
|
-
export declare type Signature =
|
|
4
|
+
export declare type Signature = string[];
|
|
5
5
|
export declare type RawCalldata = BigNumberish[];
|
|
6
6
|
export declare type DeployContractPayload = {
|
|
7
7
|
contract: CompiledContract | string;
|
|
@@ -14,11 +14,11 @@ export declare type Invocation = {
|
|
|
14
14
|
calldata?: RawCalldata;
|
|
15
15
|
signature?: Signature;
|
|
16
16
|
};
|
|
17
|
-
export declare type
|
|
17
|
+
export declare type Call = Omit<Invocation, 'signature'>;
|
|
18
18
|
export declare type InvocationsDetails = {
|
|
19
19
|
nonce?: BigNumberish;
|
|
20
|
+
maxFee?: BigNumberish;
|
|
20
21
|
};
|
|
21
|
-
export declare type Call = Omit<Invocation, 'signature' | 'nonce'>;
|
|
22
22
|
export declare type Status = 'NOT_RECEIVED' | 'RECEIVED' | 'PENDING' | 'ACCEPTED_ON_L2' | 'ACCEPTED_ON_L1' | 'REJECTED';
|
|
23
23
|
export declare type TransactionStatus = 'TRANSACTION_RECEIVED';
|
|
24
24
|
export declare type Type = 'DEPLOY' | 'INVOKE_FUNCTION';
|
|
@@ -93,7 +93,7 @@ function sign(keyPair, msgHash) {
|
|
|
93
93
|
(0, number_1.assertInRange)(r, constants_1.ONE, (0, number_1.toBN)((0, encode_1.addHexPrefix)(constants_1.MAX_ECDSA_VAL)), 'r');
|
|
94
94
|
(0, number_1.assertInRange)(s, constants_1.ONE, (0, number_1.toBN)((0, encode_1.addHexPrefix)(constants_1.EC_ORDER)), 's');
|
|
95
95
|
(0, number_1.assertInRange)(w, constants_1.ONE, (0, number_1.toBN)((0, encode_1.addHexPrefix)(constants_1.MAX_ECDSA_VAL)), 'w');
|
|
96
|
-
return [r, s];
|
|
96
|
+
return [r.toString(), s.toString()];
|
|
97
97
|
}
|
|
98
98
|
exports.sign = sign;
|
|
99
99
|
function chunkArray(arr, n) {
|
package/dist/utils/hash.d.ts
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import BN from 'bn.js';
|
|
2
|
+
import { Call } from '../types';
|
|
2
3
|
import { BigNumberish } from './number';
|
|
4
|
+
export declare const transactionPrefix: string;
|
|
5
|
+
export declare const transactionVersion = 0;
|
|
3
6
|
/**
|
|
4
7
|
* Function to get the starknet keccak hash from a string
|
|
5
8
|
*
|
|
@@ -8,7 +11,14 @@ import { BigNumberish } from './number';
|
|
|
8
11
|
* @returns starknet keccak hash as BigNumber
|
|
9
12
|
*/
|
|
10
13
|
export declare function starknetKeccak(value: string): BN;
|
|
14
|
+
/**
|
|
15
|
+
* Function to get the hex selector from a given function name
|
|
16
|
+
*
|
|
17
|
+
* [Reference](https://github.com/starkware-libs/cairo-lang/blob/master/src/starkware/starknet/public/abi.py#L25-L26)
|
|
18
|
+
* @param funcName - selectors abi function name
|
|
19
|
+
* @returns hex selector of given abi function name
|
|
20
|
+
*/
|
|
21
|
+
export declare function getSelectorFromName(funcName: string): string;
|
|
11
22
|
export declare function pedersen(input: [BigNumberish, BigNumberish]): string;
|
|
12
23
|
export declare function computeHashOnElements(data: BigNumberish[]): string;
|
|
13
|
-
export declare function
|
|
14
|
-
export declare function hashMessage(account: string, to: string, selector: string, calldata: string[], nonce: string): string;
|
|
24
|
+
export declare function hashMulticall(account: string, transactions: Call[], nonce: string, maxFee: string): string;
|
package/dist/utils/hash.js
CHANGED
|
@@ -28,13 +28,16 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
28
28
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
29
29
|
};
|
|
30
30
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
31
|
-
exports.
|
|
31
|
+
exports.hashMulticall = exports.computeHashOnElements = exports.pedersen = exports.getSelectorFromName = exports.starknetKeccak = exports.transactionVersion = exports.transactionPrefix = void 0;
|
|
32
32
|
var keccak_1 = require("ethereum-cryptography/keccak");
|
|
33
33
|
var minimalistic_assert_1 = __importDefault(require("minimalistic-assert"));
|
|
34
34
|
var constants_1 = require("../constants");
|
|
35
35
|
var ellipticCurve_1 = require("./ellipticCurve");
|
|
36
36
|
var encode_1 = require("./encode");
|
|
37
37
|
var number_1 = require("./number");
|
|
38
|
+
var shortString_1 = require("./shortString");
|
|
39
|
+
exports.transactionPrefix = (0, shortString_1.encodeShortString)('StarkNet Transaction');
|
|
40
|
+
exports.transactionVersion = 0;
|
|
38
41
|
function keccakHex(value) {
|
|
39
42
|
return (0, encode_1.addHexPrefix)((0, encode_1.buf2hex)((0, keccak_1.keccak256)((0, encode_1.utf8ToArray)(value))));
|
|
40
43
|
}
|
|
@@ -49,6 +52,18 @@ function starknetKeccak(value) {
|
|
|
49
52
|
return (0, number_1.toBN)(keccakHex(value)).and(constants_1.MASK_250);
|
|
50
53
|
}
|
|
51
54
|
exports.starknetKeccak = starknetKeccak;
|
|
55
|
+
/**
|
|
56
|
+
* Function to get the hex selector from a given function name
|
|
57
|
+
*
|
|
58
|
+
* [Reference](https://github.com/starkware-libs/cairo-lang/blob/master/src/starkware/starknet/public/abi.py#L25-L26)
|
|
59
|
+
* @param funcName - selectors abi function name
|
|
60
|
+
* @returns hex selector of given abi function name
|
|
61
|
+
*/
|
|
62
|
+
function getSelectorFromName(funcName) {
|
|
63
|
+
// sometimes BigInteger pads the hex string with zeros, which isnt allowed in the starknet api
|
|
64
|
+
return (0, number_1.toHex)(starknetKeccak(funcName));
|
|
65
|
+
}
|
|
66
|
+
exports.getSelectorFromName = getSelectorFromName;
|
|
52
67
|
var constantPoints = constants_1.CONSTANT_POINTS.map(function (coords) {
|
|
53
68
|
return ellipticCurve_1.ec.curve.point(coords[0], coords[1]);
|
|
54
69
|
});
|
|
@@ -73,12 +88,25 @@ function computeHashOnElements(data) {
|
|
|
73
88
|
return __spreadArray(__spreadArray([], __read(data), false), [data.length], false).reduce(function (x, y) { return pedersen([x, y]); }, 0).toString();
|
|
74
89
|
}
|
|
75
90
|
exports.computeHashOnElements = computeHashOnElements;
|
|
76
|
-
function
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
91
|
+
function hashMulticall(account, transactions, nonce, maxFee) {
|
|
92
|
+
var hashArray = transactions
|
|
93
|
+
.map(function (_a) {
|
|
94
|
+
var contractAddress = _a.contractAddress, entrypoint = _a.entrypoint, calldata = _a.calldata;
|
|
95
|
+
return [
|
|
96
|
+
contractAddress,
|
|
97
|
+
getSelectorFromName(entrypoint),
|
|
98
|
+
computeHashOnElements(calldata || []),
|
|
99
|
+
];
|
|
100
|
+
})
|
|
101
|
+
.map(number_1.bigNumberishArrayToDecimalStringArray)
|
|
102
|
+
.map(computeHashOnElements);
|
|
103
|
+
return computeHashOnElements([
|
|
104
|
+
exports.transactionPrefix,
|
|
105
|
+
account,
|
|
106
|
+
computeHashOnElements(hashArray),
|
|
107
|
+
nonce,
|
|
108
|
+
maxFee,
|
|
109
|
+
exports.transactionVersion,
|
|
110
|
+
]);
|
|
83
111
|
}
|
|
84
|
-
exports.
|
|
112
|
+
exports.hashMulticall = hashMulticall;
|
package/dist/utils/stark.d.ts
CHANGED
|
@@ -7,14 +7,6 @@ import { Calldata, CompressedProgram, Program, RawArgs, Signature } from '../typ
|
|
|
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[];
|
package/dist/utils/stark.js
CHANGED
|
@@ -25,11 +25,10 @@ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
|
25
25
|
return to.concat(ar || Array.prototype.slice.call(from));
|
|
26
26
|
};
|
|
27
27
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
28
|
-
exports.compileCalldata = exports.formatSignature = exports.makeAddress = exports.randomAddress = exports.
|
|
28
|
+
exports.compileCalldata = exports.formatSignature = exports.makeAddress = exports.randomAddress = exports.compressProgram = void 0;
|
|
29
29
|
var pako_1 = require("pako");
|
|
30
30
|
var ellipticCurve_1 = require("./ellipticCurve");
|
|
31
31
|
var encode_1 = require("./encode");
|
|
32
|
-
var hash_1 = require("./hash");
|
|
33
32
|
var json_1 = require("./json");
|
|
34
33
|
var number_1 = require("./number");
|
|
35
34
|
/**
|
|
@@ -45,18 +44,6 @@ function compressProgram(jsonProgram) {
|
|
|
45
44
|
return (0, encode_1.btoaUniversal)(compressedProgram);
|
|
46
45
|
}
|
|
47
46
|
exports.compressProgram = compressProgram;
|
|
48
|
-
/**
|
|
49
|
-
* Function to get the hex selector from a given function name
|
|
50
|
-
*
|
|
51
|
-
* [Reference](https://github.com/starkware-libs/cairo-lang/blob/master/src/starkware/starknet/public/abi.py#L25-L26)
|
|
52
|
-
* @param funcName - selectors abi function name
|
|
53
|
-
* @returns hex selector of given abi function name
|
|
54
|
-
*/
|
|
55
|
-
function getSelectorFromName(funcName) {
|
|
56
|
-
// sometimes BigInteger pads the hex string with zeros, which isnt allowed in the starknet api
|
|
57
|
-
return (0, number_1.toHex)((0, hash_1.starknetKeccak)(funcName));
|
|
58
|
-
}
|
|
59
|
-
exports.getSelectorFromName = getSelectorFromName;
|
|
60
47
|
function randomAddress() {
|
|
61
48
|
var randomKeyPair = (0, ellipticCurve_1.genKeyPair)();
|
|
62
49
|
return (0, ellipticCurve_1.getStarkKey)(randomKeyPair);
|
|
@@ -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[];
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __read = (this && this.__read) || function (o, n) {
|
|
3
|
+
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
4
|
+
if (!m) return o;
|
|
5
|
+
var i = m.call(o), r, ar = [], e;
|
|
6
|
+
try {
|
|
7
|
+
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
8
|
+
}
|
|
9
|
+
catch (error) { e = { error: error }; }
|
|
10
|
+
finally {
|
|
11
|
+
try {
|
|
12
|
+
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
13
|
+
}
|
|
14
|
+
finally { if (e) throw e.error; }
|
|
15
|
+
}
|
|
16
|
+
return ar;
|
|
17
|
+
};
|
|
18
|
+
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
19
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
20
|
+
if (ar || !(i in from)) {
|
|
21
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
22
|
+
ar[i] = from[i];
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
26
|
+
};
|
|
27
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
28
|
+
exports.fromCallsToExecuteCalldata = exports.transformCallsToMulticallArrays = void 0;
|
|
29
|
+
var hash_1 = require("./hash");
|
|
30
|
+
var number_1 = require("./number");
|
|
31
|
+
/**
|
|
32
|
+
* Transforms a list of Calls, each with their own calldata, into
|
|
33
|
+
* two arrays: one with the entrypoints, and one with the concatenated calldata.
|
|
34
|
+
* @param calls
|
|
35
|
+
* @returns
|
|
36
|
+
*/
|
|
37
|
+
var transformCallsToMulticallArrays = function (calls) {
|
|
38
|
+
var callArray = [];
|
|
39
|
+
var calldata = [];
|
|
40
|
+
calls.forEach(function (call) {
|
|
41
|
+
var data = call.calldata || [];
|
|
42
|
+
callArray.push({
|
|
43
|
+
to: (0, number_1.toBN)(call.contractAddress).toString(10),
|
|
44
|
+
selector: (0, number_1.toBN)((0, hash_1.getSelectorFromName)(call.entrypoint)).toString(10),
|
|
45
|
+
data_offset: calldata.length.toString(),
|
|
46
|
+
data_len: data.length.toString(),
|
|
47
|
+
});
|
|
48
|
+
calldata.push.apply(calldata, __spreadArray([], __read(data), false));
|
|
49
|
+
});
|
|
50
|
+
return {
|
|
51
|
+
callArray: callArray,
|
|
52
|
+
calldata: (0, number_1.bigNumberishArrayToDecimalStringArray)(calldata),
|
|
53
|
+
};
|
|
54
|
+
};
|
|
55
|
+
exports.transformCallsToMulticallArrays = transformCallsToMulticallArrays;
|
|
56
|
+
/**
|
|
57
|
+
* Transforms a list of calls in the full flattened calldata expected
|
|
58
|
+
* by the __execute__ protocol.
|
|
59
|
+
* @param calls
|
|
60
|
+
* @returns
|
|
61
|
+
*/
|
|
62
|
+
var fromCallsToExecuteCalldata = function (calls) {
|
|
63
|
+
var _a = (0, exports.transformCallsToMulticallArrays)(calls), callArray = _a.callArray, calldata = _a.calldata;
|
|
64
|
+
return __spreadArray(__spreadArray(__spreadArray([
|
|
65
|
+
callArray.length.toString()
|
|
66
|
+
], __read(callArray
|
|
67
|
+
.map(function (_a) {
|
|
68
|
+
var to = _a.to, selector = _a.selector, data_offset = _a.data_offset, data_len = _a.data_len;
|
|
69
|
+
return [to, selector, data_offset, data_len];
|
|
70
|
+
})
|
|
71
|
+
.flat()), false), [
|
|
72
|
+
calldata.length.toString()
|
|
73
|
+
], false), __read(calldata), false);
|
|
74
|
+
};
|
|
75
|
+
exports.fromCallsToExecuteCalldata = fromCallsToExecuteCalldata;
|
|
@@ -85,7 +85,7 @@ export declare const getStructHash: <T extends {
|
|
|
85
85
|
* with Keccak256.
|
|
86
86
|
*
|
|
87
87
|
* @param {TypedData} typedData
|
|
88
|
-
* @param {
|
|
88
|
+
* @param {BigNumberish} account
|
|
89
89
|
* @return {string}
|
|
90
90
|
*/
|
|
91
91
|
export declare const getMessageHash: (typedData: TypedData, account: BigNumberish) => string;
|
|
@@ -39,7 +39,6 @@ exports.getMessageHash = exports.getStructHash = exports.encodeData = exports.ge
|
|
|
39
39
|
var hash_1 = require("../hash");
|
|
40
40
|
var number_1 = require("../number");
|
|
41
41
|
var shortString_1 = require("../shortString");
|
|
42
|
-
var stark_1 = require("../stark");
|
|
43
42
|
var utils_1 = require("./utils");
|
|
44
43
|
__exportStar(require("./types"), exports);
|
|
45
44
|
function getHex(value) {
|
|
@@ -104,7 +103,7 @@ exports.encodeType = encodeType;
|
|
|
104
103
|
* @return {string}
|
|
105
104
|
*/
|
|
106
105
|
var getTypeHash = function (typedData, type) {
|
|
107
|
-
return (0,
|
|
106
|
+
return (0, hash_1.getSelectorFromName)((0, exports.encodeType)(typedData, type));
|
|
108
107
|
};
|
|
109
108
|
exports.getTypeHash = getTypeHash;
|
|
110
109
|
/**
|
|
@@ -168,7 +167,7 @@ exports.getStructHash = getStructHash;
|
|
|
168
167
|
* with Keccak256.
|
|
169
168
|
*
|
|
170
169
|
* @param {TypedData} typedData
|
|
171
|
-
* @param {
|
|
170
|
+
* @param {BigNumberish} account
|
|
172
171
|
* @return {string}
|
|
173
172
|
*/
|
|
174
173
|
var getMessageHash = function (typedData, account) {
|
package/index.d.ts
CHANGED
package/index.js
CHANGED
package/package.json
CHANGED
package/provider/default.js
CHANGED
|
@@ -175,6 +175,7 @@ Object.defineProperty(exports, '__esModule', { value: true });
|
|
|
175
175
|
exports.Provider = void 0;
|
|
176
176
|
var axios_1 = __importDefault(require('axios'));
|
|
177
177
|
var url_join_1 = __importDefault(require('url-join'));
|
|
178
|
+
var hash_1 = require('../utils/hash');
|
|
178
179
|
var json_1 = require('../utils/json');
|
|
179
180
|
var number_1 = require('../utils/number');
|
|
180
181
|
var stark_1 = require('../utils/stark');
|
|
@@ -260,8 +261,9 @@ var Provider = /** @class */ (function () {
|
|
|
260
261
|
};
|
|
261
262
|
// typesafe fetch
|
|
262
263
|
Provider.prototype.fetchEndpoint = function (endpoint) {
|
|
264
|
+
var _a;
|
|
263
265
|
// typescript type magiuc to create a nice fitting function interface
|
|
264
|
-
var
|
|
266
|
+
var _b = []; // when both query and request are needed, we cant omit anything
|
|
265
267
|
for (
|
|
266
268
|
// typescript type magiuc to create a nice fitting function interface
|
|
267
269
|
var _i = 1; // when both query and request are needed, we cant omit anything
|
|
@@ -271,21 +273,24 @@ var Provider = /** @class */ (function () {
|
|
|
271
273
|
_i++ // when both query and request are needed, we cant omit anything
|
|
272
274
|
) {
|
|
273
275
|
// typescript type magiuc to create a nice fitting function interface
|
|
274
|
-
|
|
276
|
+
_b[_i - 1] = arguments[_i]; // when both query and request are needed, we cant omit anything
|
|
275
277
|
}
|
|
276
278
|
// typescript type magiuc to create a nice fitting function interface
|
|
277
|
-
var
|
|
278
|
-
query =
|
|
279
|
-
request =
|
|
279
|
+
var _c = __read(_b, 2),
|
|
280
|
+
query = _c[0],
|
|
281
|
+
request = _c[1]; // when both query and request are needed, we cant omit anything
|
|
280
282
|
return __awaiter(this, void 0, void 0, function () {
|
|
281
|
-
var baseUrl, method, queryString, headers, data;
|
|
282
|
-
return __generator(this, function (
|
|
283
|
-
switch (
|
|
283
|
+
var baseUrl, method, queryString, headers, data, error_1, data;
|
|
284
|
+
return __generator(this, function (_d) {
|
|
285
|
+
switch (_d.label) {
|
|
284
286
|
case 0:
|
|
285
287
|
baseUrl = this.getFetchUrl(endpoint);
|
|
286
288
|
method = this.getFetchMethod(endpoint);
|
|
287
289
|
queryString = this.getQueryString(query);
|
|
288
290
|
headers = this.getHeaders(method);
|
|
291
|
+
_d.label = 1;
|
|
292
|
+
case 1:
|
|
293
|
+
_d.trys.push([1, 3, , 4]);
|
|
289
294
|
return [
|
|
290
295
|
4 /*yield*/,
|
|
291
296
|
axios_1.default.request({
|
|
@@ -295,9 +300,22 @@ var Provider = /** @class */ (function () {
|
|
|
295
300
|
headers: headers,
|
|
296
301
|
}),
|
|
297
302
|
];
|
|
298
|
-
case
|
|
299
|
-
data =
|
|
303
|
+
case 2:
|
|
304
|
+
data = _d.sent().data;
|
|
300
305
|
return [2 /*return*/, data];
|
|
306
|
+
case 3:
|
|
307
|
+
error_1 = _d.sent();
|
|
308
|
+
data =
|
|
309
|
+
(_a = error_1 === null || error_1 === void 0 ? void 0 : error_1.response) === null ||
|
|
310
|
+
_a === void 0
|
|
311
|
+
? void 0
|
|
312
|
+
: _a.data;
|
|
313
|
+
if (data === null || data === void 0 ? void 0 : data.message) {
|
|
314
|
+
throw new Error(data.code + ': ' + data.message);
|
|
315
|
+
}
|
|
316
|
+
throw error_1;
|
|
317
|
+
case 4:
|
|
318
|
+
return [2 /*return*/];
|
|
301
319
|
}
|
|
302
320
|
});
|
|
303
321
|
});
|
|
@@ -345,7 +363,7 @@ var Provider = /** @class */ (function () {
|
|
|
345
363
|
{
|
|
346
364
|
signature: [],
|
|
347
365
|
contract_address: contractAddress,
|
|
348
|
-
entry_point_selector: (0,
|
|
366
|
+
entry_point_selector: (0, hash_1.getSelectorFromName)(entrypoint),
|
|
349
367
|
calldata: calldata,
|
|
350
368
|
}
|
|
351
369
|
),
|
|
@@ -543,7 +561,7 @@ var Provider = /** @class */ (function () {
|
|
|
543
561
|
return this.fetchEndpoint('add_transaction', undefined, {
|
|
544
562
|
type: 'INVOKE_FUNCTION',
|
|
545
563
|
contract_address: invocation.contractAddress,
|
|
546
|
-
entry_point_selector: (0,
|
|
564
|
+
entry_point_selector: (0, hash_1.getSelectorFromName)(invocation.entrypoint),
|
|
547
565
|
calldata: (0, number_1.bigNumberishArrayToDecimalStringArray)(
|
|
548
566
|
(_a = invocation.calldata) !== null && _a !== void 0 ? _a : []
|
|
549
567
|
),
|
|
@@ -557,7 +575,7 @@ var Provider = /** @class */ (function () {
|
|
|
557
575
|
retryInterval = 8000;
|
|
558
576
|
}
|
|
559
577
|
return __awaiter(this, void 0, void 0, function () {
|
|
560
|
-
var onchain, res, error;
|
|
578
|
+
var onchain, res, message, error;
|
|
561
579
|
return __generator(this, function (_a) {
|
|
562
580
|
switch (_a.label) {
|
|
563
581
|
case 0:
|
|
@@ -579,7 +597,14 @@ var Provider = /** @class */ (function () {
|
|
|
579
597
|
if (res.tx_status === 'ACCEPTED_ON_L1' || res.tx_status === 'ACCEPTED_ON_L2') {
|
|
580
598
|
onchain = true;
|
|
581
599
|
} else if (res.tx_status === 'REJECTED' || res.tx_status === 'NOT_RECEIVED') {
|
|
582
|
-
|
|
600
|
+
message = res.tx_failure_reason
|
|
601
|
+
? res.tx_status +
|
|
602
|
+
': ' +
|
|
603
|
+
res.tx_failure_reason.code +
|
|
604
|
+
'\n' +
|
|
605
|
+
res.tx_failure_reason.error_message
|
|
606
|
+
: res.tx_status;
|
|
607
|
+
error = new Error(message);
|
|
583
608
|
error.response = res;
|
|
584
609
|
throw error;
|
|
585
610
|
}
|
package/signer/default.d.ts
CHANGED
|
@@ -10,5 +10,5 @@ export declare class Signer implements SignerInterface {
|
|
|
10
10
|
transactionsDetail: InvocationsSignerDetails,
|
|
11
11
|
abis?: Abi[]
|
|
12
12
|
): Promise<Signature>;
|
|
13
|
-
signMessage(typedData: TypedData,
|
|
13
|
+
signMessage(typedData: TypedData, accountAddress: string): Promise<Signature>;
|
|
14
14
|
}
|
package/signer/default.js
CHANGED
|
@@ -133,10 +133,7 @@ var __generator =
|
|
|
133
133
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
134
134
|
exports.Signer = void 0;
|
|
135
135
|
var ellipticCurve_1 = require('../utils/ellipticCurve');
|
|
136
|
-
var encode_1 = require('../utils/encode');
|
|
137
136
|
var hash_1 = require('../utils/hash');
|
|
138
|
-
var number_1 = require('../utils/number');
|
|
139
|
-
var stark_1 = require('../utils/stark');
|
|
140
137
|
var typedData_1 = require('../utils/typedData');
|
|
141
138
|
var Signer = /** @class */ (function () {
|
|
142
139
|
function Signer(keyPair) {
|
|
@@ -150,58 +147,27 @@ var Signer = /** @class */ (function () {
|
|
|
150
147
|
});
|
|
151
148
|
};
|
|
152
149
|
Signer.prototype.signTransaction = function (transactions, transactionsDetail, abis) {
|
|
153
|
-
if (abis === void 0) {
|
|
154
|
-
abis = [];
|
|
155
|
-
}
|
|
156
150
|
return __awaiter(this, void 0, void 0, function () {
|
|
157
|
-
var
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
_b,
|
|
161
|
-
calldata,
|
|
162
|
-
nonce,
|
|
163
|
-
walletAddress,
|
|
164
|
-
nonceBn,
|
|
165
|
-
entrypointSelector,
|
|
166
|
-
calldataDecimal,
|
|
167
|
-
msgHash;
|
|
168
|
-
return __generator(this, function (_c) {
|
|
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
|
-
) {
|
|
151
|
+
var msgHash;
|
|
152
|
+
return __generator(this, function (_a) {
|
|
153
|
+
if (abis && abis.length !== transactions.length) {
|
|
176
154
|
throw new Error('ABI must be provided for each transaction or no transaction');
|
|
177
155
|
}
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
(
|
|
182
|
-
(
|
|
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
|
-
)
|
|
156
|
+
msgHash = (0, hash_1.hashMulticall)(
|
|
157
|
+
transactionsDetail.walletAddress,
|
|
158
|
+
transactions,
|
|
159
|
+
transactionsDetail.nonce.toString(),
|
|
160
|
+
transactionsDetail.maxFee.toString()
|
|
195
161
|
);
|
|
196
162
|
return [2 /*return*/, (0, ellipticCurve_1.sign)(this.keyPair, msgHash)];
|
|
197
163
|
});
|
|
198
164
|
});
|
|
199
165
|
};
|
|
200
|
-
Signer.prototype.signMessage = function (typedData,
|
|
166
|
+
Signer.prototype.signMessage = function (typedData, accountAddress) {
|
|
201
167
|
return __awaiter(this, void 0, void 0, function () {
|
|
202
168
|
var msgHash;
|
|
203
169
|
return __generator(this, function (_a) {
|
|
204
|
-
msgHash = (0, typedData_1.getMessageHash)(typedData,
|
|
170
|
+
msgHash = (0, typedData_1.getMessageHash)(typedData, accountAddress);
|
|
205
171
|
return [2 /*return*/, (0, ellipticCurve_1.sign)(this.keyPair, msgHash)];
|
|
206
172
|
});
|
|
207
173
|
});
|
package/signer/interface.d.ts
CHANGED
|
@@ -11,11 +11,12 @@ export declare abstract class SignerInterface {
|
|
|
11
11
|
* Sign an JSON object for off-chain usage with the starknet private key and return the signature
|
|
12
12
|
* This adds a message prefix so it cant be interchanged with transactions
|
|
13
13
|
*
|
|
14
|
-
* @param
|
|
14
|
+
* @param typedData - JSON object to be signed
|
|
15
|
+
* @param accountAddress - account
|
|
15
16
|
* @returns the signature of the JSON object
|
|
16
17
|
* @throws {Error} if the JSON object is not a valid JSON
|
|
17
18
|
*/
|
|
18
|
-
abstract signMessage(typedData: TypedData,
|
|
19
|
+
abstract signMessage(typedData: TypedData, accountAddress: string): Promise<Signature>;
|
|
19
20
|
/**
|
|
20
21
|
* Signs a transaction with the starknet private key and returns the signature
|
|
21
22
|
*
|