starknet 11.4.1 → 11.4.3
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 +12 -0
- package/dist/index.d.ts +219 -94
- package/dist/index.global.js +1442 -1353
- package/dist/index.global.js.map +1 -1
- package/dist/index.js +1444 -1356
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1443 -1356
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -204,7 +204,8 @@ __export(index_exports, {
|
|
|
204
204
|
num: () => num_exports,
|
|
205
205
|
outsideExecution: () => outsideExecution_exports,
|
|
206
206
|
parseCalldataField: () => parseCalldataField,
|
|
207
|
-
paymaster: () =>
|
|
207
|
+
paymaster: () => utils_exports,
|
|
208
|
+
paymasterPlugin: () => paymasterPlugin,
|
|
208
209
|
provider: () => provider_exports,
|
|
209
210
|
selector: () => selector_exports,
|
|
210
211
|
shortString: () => shortString_exports,
|
|
@@ -17210,11 +17211,720 @@ function fastExecute() {
|
|
|
17210
17211
|
};
|
|
17211
17212
|
}
|
|
17212
17213
|
|
|
17214
|
+
// src/plugins/paymaster/utils.ts
|
|
17215
|
+
var utils_exports = {};
|
|
17216
|
+
__export(utils_exports, {
|
|
17217
|
+
assertCallsAreStrictlyEqual: () => assertCallsAreStrictlyEqual,
|
|
17218
|
+
assertPaymasterTransactionSafety: () => assertPaymasterTransactionSafety,
|
|
17219
|
+
getDefaultPaymasterNodeUrl: () => getDefaultPaymasterNodeUrl
|
|
17220
|
+
});
|
|
17221
|
+
|
|
17222
|
+
// src/utils/outsideExecution.ts
|
|
17223
|
+
var outsideExecution_exports = {};
|
|
17224
|
+
__export(outsideExecution_exports, {
|
|
17225
|
+
buildExecuteFromOutsideCall: () => buildExecuteFromOutsideCall,
|
|
17226
|
+
buildExecuteFromOutsideCallData: () => buildExecuteFromOutsideCallData,
|
|
17227
|
+
getOutsideCall: () => getOutsideCall,
|
|
17228
|
+
getTypedData: () => getTypedData,
|
|
17229
|
+
toOutsideCallV2: () => toOutsideCallV2
|
|
17230
|
+
});
|
|
17231
|
+
function toOutsideCallV2(call) {
|
|
17232
|
+
if ("calldata_len" in call) {
|
|
17233
|
+
return {
|
|
17234
|
+
To: call.to,
|
|
17235
|
+
Selector: call.selector,
|
|
17236
|
+
Calldata: call.calldata
|
|
17237
|
+
};
|
|
17238
|
+
}
|
|
17239
|
+
return call;
|
|
17240
|
+
}
|
|
17241
|
+
function getOutsideCall(call) {
|
|
17242
|
+
const callData = call.calldata ?? [];
|
|
17243
|
+
const callDataCompiled = Array.isArray(callData) && "__compiled__" in callData ? callData : CallData.compile(callData);
|
|
17244
|
+
return {
|
|
17245
|
+
to: call.contractAddress,
|
|
17246
|
+
selector: getSelectorFromName(call.entrypoint),
|
|
17247
|
+
calldata: callDataCompiled
|
|
17248
|
+
};
|
|
17249
|
+
}
|
|
17250
|
+
function callToTypedData(call, version) {
|
|
17251
|
+
const outsideCall = getOutsideCall(call);
|
|
17252
|
+
if (version === "1") {
|
|
17253
|
+
return {
|
|
17254
|
+
...outsideCall,
|
|
17255
|
+
calldata_len: outsideCall.calldata.length,
|
|
17256
|
+
calldata: outsideCall.calldata
|
|
17257
|
+
};
|
|
17258
|
+
}
|
|
17259
|
+
return {
|
|
17260
|
+
To: outsideCall.to,
|
|
17261
|
+
Selector: outsideCall.selector,
|
|
17262
|
+
Calldata: outsideCall.calldata
|
|
17263
|
+
};
|
|
17264
|
+
}
|
|
17265
|
+
function getDomain(chainId, version) {
|
|
17266
|
+
return {
|
|
17267
|
+
name: "Account.execute_from_outside",
|
|
17268
|
+
version,
|
|
17269
|
+
chainId,
|
|
17270
|
+
...version === "2" ? { revision: "1" } : {}
|
|
17271
|
+
};
|
|
17272
|
+
}
|
|
17273
|
+
function getTypedData(chainId, options, nonce, myCalls, version) {
|
|
17274
|
+
if (version === "1") {
|
|
17275
|
+
return {
|
|
17276
|
+
types: OutsideExecutionTypesV1,
|
|
17277
|
+
primaryType: "OutsideExecution",
|
|
17278
|
+
domain: getDomain(chainId, version),
|
|
17279
|
+
message: {
|
|
17280
|
+
...options,
|
|
17281
|
+
nonce,
|
|
17282
|
+
calls_len: myCalls.length,
|
|
17283
|
+
calls: myCalls.map((call) => callToTypedData(call, version))
|
|
17284
|
+
}
|
|
17285
|
+
};
|
|
17286
|
+
}
|
|
17287
|
+
return {
|
|
17288
|
+
types: OutsideExecutionTypesV2,
|
|
17289
|
+
primaryType: "OutsideExecution",
|
|
17290
|
+
domain: getDomain(chainId, version),
|
|
17291
|
+
message: {
|
|
17292
|
+
Caller: options.caller,
|
|
17293
|
+
Nonce: nonce,
|
|
17294
|
+
"Execute After": options.execute_after,
|
|
17295
|
+
"Execute Before": options.execute_before,
|
|
17296
|
+
Calls: myCalls.map((call) => callToTypedData(call, version))
|
|
17297
|
+
}
|
|
17298
|
+
};
|
|
17299
|
+
}
|
|
17300
|
+
function buildExecuteFromOutsideCallData(outsideTransaction) {
|
|
17301
|
+
const execution2 = outsideTransaction.outsideExecution;
|
|
17302
|
+
const formattedSignature = formatSignature(outsideTransaction.signature);
|
|
17303
|
+
return CallData.compile({
|
|
17304
|
+
outside_execution: execution2,
|
|
17305
|
+
signature: formattedSignature
|
|
17306
|
+
});
|
|
17307
|
+
}
|
|
17308
|
+
function buildExecuteFromOutsideCall(outsideTransaction) {
|
|
17309
|
+
const myOutsideTransactions = Array.isArray(outsideTransaction) ? outsideTransaction : [outsideTransaction];
|
|
17310
|
+
const multiCall = myOutsideTransactions.map((outsideTx) => {
|
|
17311
|
+
let entrypoint;
|
|
17312
|
+
if (outsideTx.version === OutsideExecutionVersion.V1) {
|
|
17313
|
+
entrypoint = "execute_from_outside";
|
|
17314
|
+
} else if (outsideTx.version === OutsideExecutionVersion.V2) {
|
|
17315
|
+
entrypoint = "execute_from_outside_v2";
|
|
17316
|
+
} else {
|
|
17317
|
+
throw new Error("Unsupported OutsideExecution version");
|
|
17318
|
+
}
|
|
17319
|
+
return {
|
|
17320
|
+
contractAddress: toHex(outsideTx.signerAddress),
|
|
17321
|
+
entrypoint,
|
|
17322
|
+
calldata: buildExecuteFromOutsideCallData(outsideTx)
|
|
17323
|
+
};
|
|
17324
|
+
});
|
|
17325
|
+
return multiCall;
|
|
17326
|
+
}
|
|
17327
|
+
|
|
17328
|
+
// src/utils/uint256.ts
|
|
17329
|
+
var uint256_exports = {};
|
|
17330
|
+
__export(uint256_exports, {
|
|
17331
|
+
bnToUint256: () => bnToUint256,
|
|
17332
|
+
isUint256: () => isUint256,
|
|
17333
|
+
uint256ToBN: () => uint256ToBN
|
|
17334
|
+
});
|
|
17335
|
+
function uint256ToBN(uint2562) {
|
|
17336
|
+
return new CairoUint256(uint2562).toBigInt();
|
|
17337
|
+
}
|
|
17338
|
+
function isUint256(bn) {
|
|
17339
|
+
return CairoUint256.is(bn);
|
|
17340
|
+
}
|
|
17341
|
+
function bnToUint256(bn) {
|
|
17342
|
+
return new CairoUint256(bn).toUint256HexString();
|
|
17343
|
+
}
|
|
17344
|
+
|
|
17345
|
+
// src/plugins/paymaster/utils.ts
|
|
17346
|
+
var getDefaultPaymasterNodeUrl = (networkName, mute = false) => {
|
|
17347
|
+
if (!mute) {
|
|
17348
|
+
logger.info("Using default public node url, please provide nodeUrl in provider options!");
|
|
17349
|
+
}
|
|
17350
|
+
const nodes = PAYMASTER_RPC_NODES[networkName ?? _NetworkName.SN_SEPOLIA];
|
|
17351
|
+
const randIdx = Math.floor(Math.random() * nodes.length);
|
|
17352
|
+
return nodes[randIdx];
|
|
17353
|
+
};
|
|
17354
|
+
var assertGasFeeFromUnsafeCalls = (unsafeCalls, fees) => {
|
|
17355
|
+
const unsafeCall = toOutsideCallV2(unsafeCalls[unsafeCalls.length - 1]);
|
|
17356
|
+
const unsafeGasTokenCalldata = CallData.toCalldata(unsafeCall.Calldata);
|
|
17357
|
+
const unsafeGasTokenValue = uint256ToBN({
|
|
17358
|
+
low: unsafeGasTokenCalldata[1],
|
|
17359
|
+
high: unsafeGasTokenCalldata[2]
|
|
17360
|
+
});
|
|
17361
|
+
assert(
|
|
17362
|
+
unsafeGasTokenValue === BigInt(fees),
|
|
17363
|
+
"Gas token value is not equal to the provided gas fees"
|
|
17364
|
+
);
|
|
17365
|
+
};
|
|
17366
|
+
var assertGasTokenFromUnsafeCalls = (unsafeCalls, gasToken) => {
|
|
17367
|
+
const unsafeCall = toOutsideCallV2(unsafeCalls[unsafeCalls.length - 1]);
|
|
17368
|
+
assert(
|
|
17369
|
+
BigInt(unsafeCall.To) === BigInt(gasToken),
|
|
17370
|
+
"Gas token address is not equal to the provided gas token"
|
|
17371
|
+
);
|
|
17372
|
+
assert(
|
|
17373
|
+
unsafeCall.Selector === getSelectorFromName("transfer"),
|
|
17374
|
+
"Gas token call selector is not a transfer"
|
|
17375
|
+
);
|
|
17376
|
+
assert(
|
|
17377
|
+
CallData.toCalldata(unsafeCall.Calldata).length === 3,
|
|
17378
|
+
"Gas token transfer calldata does not match the expected recipient/amount shape"
|
|
17379
|
+
);
|
|
17380
|
+
};
|
|
17381
|
+
function assertCallsAreStrictlyEqual(originalCalls, unsafeCalls, isSponsored = false) {
|
|
17382
|
+
const baseError = "Provided calls are not strictly equal to the returned calls";
|
|
17383
|
+
const expectedExtraCalls = isSponsored ? 0 : 1;
|
|
17384
|
+
assert(
|
|
17385
|
+
unsafeCalls.length - expectedExtraCalls === originalCalls.length,
|
|
17386
|
+
`${baseError}: Expected ${originalCalls.length + expectedExtraCalls} calls, got ${unsafeCalls.length}`
|
|
17387
|
+
);
|
|
17388
|
+
for (let callIndex = 0; callIndex < originalCalls.length; callIndex += 1) {
|
|
17389
|
+
const originalCall = originalCalls[callIndex];
|
|
17390
|
+
const unsafeCall = toOutsideCallV2(unsafeCalls[callIndex]);
|
|
17391
|
+
const normalizeAddress = (address) => {
|
|
17392
|
+
return toBigInt(address).toString(16).toLowerCase();
|
|
17393
|
+
};
|
|
17394
|
+
const originalAddress = normalizeAddress(originalCall.contractAddress);
|
|
17395
|
+
const unsafeAddress = normalizeAddress(unsafeCall.To);
|
|
17396
|
+
assert(
|
|
17397
|
+
originalAddress === unsafeAddress,
|
|
17398
|
+
`${baseError}: Contract address mismatch at call ${callIndex}. Expected: ${originalCall.contractAddress}, Got: ${unsafeCall.To}`
|
|
17399
|
+
);
|
|
17400
|
+
assert(
|
|
17401
|
+
getSelectorFromName(originalCall.entrypoint) === unsafeCall.Selector,
|
|
17402
|
+
`${baseError}: Entrypoint mismatch at call ${callIndex}. Expected: ${originalCall.entrypoint}, Got: ${unsafeCall.Selector}`
|
|
17403
|
+
);
|
|
17404
|
+
const originalCalldata = CallData.toCalldata(originalCall.calldata);
|
|
17405
|
+
const unsafeCalldata = CallData.toCalldata(unsafeCall.Calldata);
|
|
17406
|
+
assert(
|
|
17407
|
+
originalCalldata.length === unsafeCalldata.length,
|
|
17408
|
+
`${baseError}: Calldata length mismatch at call ${callIndex}. Expected length: ${originalCalldata.length}, Got length: ${unsafeCalldata.length}`
|
|
17409
|
+
);
|
|
17410
|
+
for (let dataIndex = 0; dataIndex < originalCalldata.length; dataIndex += 1) {
|
|
17411
|
+
const originalValue = BigInt(originalCalldata[dataIndex]);
|
|
17412
|
+
const unsafeValue = BigInt(unsafeCalldata[dataIndex]);
|
|
17413
|
+
assert(
|
|
17414
|
+
originalValue === unsafeValue,
|
|
17415
|
+
`${baseError}: Calldata value mismatch at call ${callIndex}, parameter ${dataIndex}. Expected: ${originalCalldata[dataIndex]}, Got: ${unsafeCalldata[dataIndex]}`
|
|
17416
|
+
);
|
|
17417
|
+
}
|
|
17418
|
+
}
|
|
17419
|
+
}
|
|
17420
|
+
var domainFieldToFelt = (value) => {
|
|
17421
|
+
try {
|
|
17422
|
+
return toBigInt(value);
|
|
17423
|
+
} catch {
|
|
17424
|
+
return CairoBytes31.fromText(String(value)).toBigInt();
|
|
17425
|
+
}
|
|
17426
|
+
};
|
|
17427
|
+
var assertChainIdFromTypedData = (typedData, chainId) => {
|
|
17428
|
+
assert(
|
|
17429
|
+
typedData.domain.chainId !== void 0 && domainFieldToFelt(typedData.domain.chainId) === BigInt(chainId),
|
|
17430
|
+
"Paymaster typed data domain chainId does not match the account's provider chain"
|
|
17431
|
+
);
|
|
17432
|
+
};
|
|
17433
|
+
var assertPaymasterTransactionSafety = (preparedTransaction, calls, paymasterDetails, chainId, maxFeeInGasToken) => {
|
|
17434
|
+
if (preparedTransaction.type === "invoke" || preparedTransaction.type === "deploy_and_invoke") {
|
|
17435
|
+
assertChainIdFromTypedData(preparedTransaction.typed_data, chainId);
|
|
17436
|
+
const { message } = preparedTransaction.typed_data;
|
|
17437
|
+
const hasV1Calls = "calls" in message;
|
|
17438
|
+
const hasV2Calls = "Calls" in message;
|
|
17439
|
+
assert(
|
|
17440
|
+
hasV1Calls !== hasV2Calls,
|
|
17441
|
+
'Paymaster typed data must declare exactly one of "calls" (SNIP-9 V1) or "Calls" (SNIP-9 V2)'
|
|
17442
|
+
);
|
|
17443
|
+
const unsafeCalls = hasV1Calls ? message.calls : message.Calls;
|
|
17444
|
+
assertCallsAreStrictlyEqual(calls, unsafeCalls, paymasterDetails.feeMode.mode === "sponsored");
|
|
17445
|
+
if (paymasterDetails.feeMode.mode !== "sponsored") {
|
|
17446
|
+
assertGasTokenFromUnsafeCalls(unsafeCalls, paymasterDetails.feeMode.gasToken);
|
|
17447
|
+
assertGasFeeFromUnsafeCalls(
|
|
17448
|
+
unsafeCalls,
|
|
17449
|
+
preparedTransaction.fee.suggested_max_fee_in_gas_token
|
|
17450
|
+
);
|
|
17451
|
+
if (maxFeeInGasToken) {
|
|
17452
|
+
assert(
|
|
17453
|
+
preparedTransaction.fee.suggested_max_fee_in_gas_token <= maxFeeInGasToken,
|
|
17454
|
+
"Gas token price is too high"
|
|
17455
|
+
);
|
|
17456
|
+
}
|
|
17457
|
+
}
|
|
17458
|
+
const msg = message;
|
|
17459
|
+
const version = hasV1Calls ? OutsideExecutionVersion.V1 : OutsideExecutionVersion.V2;
|
|
17460
|
+
const [callerKey, nonceKey, afterKey, beforeKey] = hasV1Calls ? ["caller", "nonce", "execute_after", "execute_before"] : ["Caller", "Nonce", "Execute After", "Execute Before"];
|
|
17461
|
+
assert(
|
|
17462
|
+
[callerKey, nonceKey, afterKey, beforeKey].every((key) => msg[key] !== void 0),
|
|
17463
|
+
"Paymaster typed data does not match the expected outside execution"
|
|
17464
|
+
);
|
|
17465
|
+
const caller = msg[callerKey];
|
|
17466
|
+
const nonce = msg[nonceKey];
|
|
17467
|
+
const executeAfter = msg[afterKey];
|
|
17468
|
+
const executeBefore = msg[beforeKey];
|
|
17469
|
+
if (paymasterDetails.timeBounds) {
|
|
17470
|
+
const requestedAfter = toBigInt(paymasterDetails.timeBounds.executeAfter ?? 1);
|
|
17471
|
+
const requestedBefore = toBigInt(paymasterDetails.timeBounds.executeBefore);
|
|
17472
|
+
assert(
|
|
17473
|
+
toBigInt(executeAfter) >= requestedAfter && toBigInt(executeBefore) <= requestedBefore,
|
|
17474
|
+
"Paymaster typed data execution window is not within the requested time bounds"
|
|
17475
|
+
);
|
|
17476
|
+
}
|
|
17477
|
+
const expectedCalls = [...calls];
|
|
17478
|
+
if (paymasterDetails.feeMode.mode !== "sponsored") {
|
|
17479
|
+
const feeCall = toOutsideCallV2(unsafeCalls[unsafeCalls.length - 1]);
|
|
17480
|
+
const feeRecipient = CallData.toCalldata(feeCall.Calldata)[0];
|
|
17481
|
+
const feeAmount = bnToUint256(preparedTransaction.fee.suggested_max_fee_in_gas_token);
|
|
17482
|
+
expectedCalls.push({
|
|
17483
|
+
contractAddress: paymasterDetails.feeMode.gasToken,
|
|
17484
|
+
entrypoint: "transfer",
|
|
17485
|
+
calldata: [feeRecipient, feeAmount.low, feeAmount.high]
|
|
17486
|
+
});
|
|
17487
|
+
}
|
|
17488
|
+
const expectedTypedData = getTypedData(
|
|
17489
|
+
chainId,
|
|
17490
|
+
{ caller, execute_after: executeAfter, execute_before: executeBefore },
|
|
17491
|
+
nonce,
|
|
17492
|
+
expectedCalls,
|
|
17493
|
+
version
|
|
17494
|
+
);
|
|
17495
|
+
assert(
|
|
17496
|
+
getMessageHash(expectedTypedData, 0) === getMessageHash(preparedTransaction.typed_data, 0),
|
|
17497
|
+
"Paymaster typed data does not match the expected outside execution"
|
|
17498
|
+
);
|
|
17499
|
+
}
|
|
17500
|
+
};
|
|
17501
|
+
|
|
17502
|
+
// src/plugins/paymaster/impl.ts
|
|
17503
|
+
var PaymasterImpl = class {
|
|
17504
|
+
/**
|
|
17505
|
+
* Asks the paymaster to build a transaction for the calls of an account.
|
|
17506
|
+
* @param {Account} account The account that will sign the transaction.
|
|
17507
|
+
* @param {Call[]} calls The calls to execute.
|
|
17508
|
+
* @param {PaymasterDetails} paymasterDetails The fee mode, and optionally the deployment data
|
|
17509
|
+
* and the time bounds.
|
|
17510
|
+
* @returns {Promise<PreparedTransaction>} The transaction prepared by the paymaster, with its
|
|
17511
|
+
* typed data to sign and its fees.
|
|
17512
|
+
* @example
|
|
17513
|
+
* ```typescript
|
|
17514
|
+
* const prepared = await PaymasterImpl.build(myAccount, [myCall], {
|
|
17515
|
+
* feeMode: { mode: 'sponsored' },
|
|
17516
|
+
* });
|
|
17517
|
+
* // prepared.type = 'invoke'
|
|
17518
|
+
* ```
|
|
17519
|
+
*/
|
|
17520
|
+
static async build(account, calls, paymasterDetails) {
|
|
17521
|
+
if (!paymasterDetails.deploymentData) {
|
|
17522
|
+
const snip9Version = await account.getSnip9Version();
|
|
17523
|
+
if (snip9Version === OutsideExecutionVersion.UNSUPPORTED) {
|
|
17524
|
+
throw Error("Account is not compatible with SNIP-9");
|
|
17525
|
+
}
|
|
17526
|
+
}
|
|
17527
|
+
const parameters = {
|
|
17528
|
+
version: "0x1",
|
|
17529
|
+
feeMode: paymasterDetails.feeMode,
|
|
17530
|
+
timeBounds: paymasterDetails.timeBounds
|
|
17531
|
+
};
|
|
17532
|
+
let transaction;
|
|
17533
|
+
if (paymasterDetails.deploymentData) {
|
|
17534
|
+
if (calls.length > 0) {
|
|
17535
|
+
transaction = {
|
|
17536
|
+
type: "deploy_and_invoke",
|
|
17537
|
+
invoke: { userAddress: account.address, calls },
|
|
17538
|
+
deployment: paymasterDetails.deploymentData
|
|
17539
|
+
};
|
|
17540
|
+
} else {
|
|
17541
|
+
transaction = {
|
|
17542
|
+
type: "deploy",
|
|
17543
|
+
deployment: paymasterDetails.deploymentData
|
|
17544
|
+
};
|
|
17545
|
+
}
|
|
17546
|
+
} else {
|
|
17547
|
+
transaction = {
|
|
17548
|
+
type: "invoke",
|
|
17549
|
+
invoke: { userAddress: account.address, calls }
|
|
17550
|
+
};
|
|
17551
|
+
}
|
|
17552
|
+
return account.paymaster.buildTransaction(transaction, parameters);
|
|
17553
|
+
}
|
|
17554
|
+
/**
|
|
17555
|
+
* Asks the paymaster for the fees of the calls of an account.
|
|
17556
|
+
* @param {Account} account The account that will sign the transaction.
|
|
17557
|
+
* @param {Call[]} calls The calls to execute.
|
|
17558
|
+
* @param {PaymasterDetails} paymasterDetails The fee mode, and optionally the deployment data
|
|
17559
|
+
* and the time bounds.
|
|
17560
|
+
* @returns {Promise<PaymasterFeeEstimate>} The fees, in STRK and in the gas token.
|
|
17561
|
+
* @example
|
|
17562
|
+
* ```typescript
|
|
17563
|
+
* const fees = await PaymasterImpl.estimate(myAccount, [myCall], {
|
|
17564
|
+
* feeMode: { mode: 'default', gasToken: usdcAddress },
|
|
17565
|
+
* });
|
|
17566
|
+
* // fees.suggested_max_fee_in_gas_token is the fee to show to the user
|
|
17567
|
+
* ```
|
|
17568
|
+
*/
|
|
17569
|
+
static async estimate(account, calls, paymasterDetails) {
|
|
17570
|
+
const preparedTransaction = await account.buildPaymasterTransaction(calls, paymasterDetails);
|
|
17571
|
+
return preparedTransaction.fee;
|
|
17572
|
+
}
|
|
17573
|
+
/**
|
|
17574
|
+
* Checks that a transaction prepared by the paymaster is safe to sign, then signs it.
|
|
17575
|
+
* @param {Account} account The account that signs the transaction.
|
|
17576
|
+
* @param {PreparedTransaction} preparedTransaction The transaction returned by the paymaster.
|
|
17577
|
+
* @param {Call[]} calls The calls the user requested.
|
|
17578
|
+
* @param {PaymasterDetails} paymasterDetails The fee mode and related paymaster details.
|
|
17579
|
+
* @param {BigNumberish} [maxFeeInGasToken] Optional user-approved ceiling on the gas-token fee.
|
|
17580
|
+
* @returns {Promise<ExecutableUserTransaction>} The transaction to send to the paymaster.
|
|
17581
|
+
* @example
|
|
17582
|
+
* ```typescript
|
|
17583
|
+
* const prepared = await PaymasterImpl.build(myAccount, [myCall], paymasterDetails);
|
|
17584
|
+
* const executable = await PaymasterImpl.prepare(myAccount, prepared, [myCall], paymasterDetails);
|
|
17585
|
+
* // executable.type = 'invoke'
|
|
17586
|
+
* ```
|
|
17587
|
+
*/
|
|
17588
|
+
static async prepare(account, preparedTransaction, calls, paymasterDetails, maxFeeInGasToken) {
|
|
17589
|
+
assertPaymasterTransactionSafety(
|
|
17590
|
+
preparedTransaction,
|
|
17591
|
+
calls,
|
|
17592
|
+
paymasterDetails,
|
|
17593
|
+
await account.provider.getChainId(),
|
|
17594
|
+
maxFeeInGasToken
|
|
17595
|
+
);
|
|
17596
|
+
let transaction;
|
|
17597
|
+
switch (preparedTransaction.type) {
|
|
17598
|
+
case "deploy_and_invoke": {
|
|
17599
|
+
const signature = await account.signMessage(preparedTransaction.typed_data);
|
|
17600
|
+
transaction = {
|
|
17601
|
+
type: "deploy_and_invoke",
|
|
17602
|
+
invoke: {
|
|
17603
|
+
userAddress: account.address,
|
|
17604
|
+
typedData: preparedTransaction.typed_data,
|
|
17605
|
+
signature: signatureToHexArray(signature)
|
|
17606
|
+
},
|
|
17607
|
+
deployment: preparedTransaction.deployment
|
|
17608
|
+
};
|
|
17609
|
+
break;
|
|
17610
|
+
}
|
|
17611
|
+
case "invoke": {
|
|
17612
|
+
const signature = await account.signMessage(preparedTransaction.typed_data);
|
|
17613
|
+
transaction = {
|
|
17614
|
+
type: "invoke",
|
|
17615
|
+
invoke: {
|
|
17616
|
+
userAddress: account.address,
|
|
17617
|
+
typedData: preparedTransaction.typed_data,
|
|
17618
|
+
signature: signatureToHexArray(signature)
|
|
17619
|
+
}
|
|
17620
|
+
};
|
|
17621
|
+
break;
|
|
17622
|
+
}
|
|
17623
|
+
case "deploy": {
|
|
17624
|
+
transaction = {
|
|
17625
|
+
type: "deploy",
|
|
17626
|
+
deployment: preparedTransaction.deployment
|
|
17627
|
+
};
|
|
17628
|
+
break;
|
|
17629
|
+
}
|
|
17630
|
+
default:
|
|
17631
|
+
throw Error("Invalid transaction type");
|
|
17632
|
+
}
|
|
17633
|
+
return transaction;
|
|
17634
|
+
}
|
|
17635
|
+
/**
|
|
17636
|
+
* Builds, checks, signs and sends a paymaster transaction for the calls of an account.
|
|
17637
|
+
* @param {Account} account The account that signs the transaction.
|
|
17638
|
+
* @param {Call[]} calls The calls to execute.
|
|
17639
|
+
* @param {PaymasterDetails} paymasterDetails The fee mode, and optionally the deployment data
|
|
17640
|
+
* and the time bounds.
|
|
17641
|
+
* @param {BigNumberish} [maxFeeInGasToken] Optional user-approved ceiling on the gas-token fee.
|
|
17642
|
+
* @returns {Promise<InvokeFunctionResponse>} The hash of the transaction sent by the paymaster.
|
|
17643
|
+
* @example
|
|
17644
|
+
* ```typescript
|
|
17645
|
+
* const { transaction_hash } = await PaymasterImpl.execute(myAccount, [myCall], {
|
|
17646
|
+
* feeMode: { mode: 'sponsored' },
|
|
17647
|
+
* });
|
|
17648
|
+
* await myAccount.provider.waitForTransaction(transaction_hash);
|
|
17649
|
+
* ```
|
|
17650
|
+
*/
|
|
17651
|
+
static async execute(account, calls, paymasterDetails, maxFeeInGasToken) {
|
|
17652
|
+
const preparedTransaction = await account.buildPaymasterTransaction(calls, paymasterDetails);
|
|
17653
|
+
const transaction = await account.preparePaymasterTransaction(
|
|
17654
|
+
preparedTransaction,
|
|
17655
|
+
calls,
|
|
17656
|
+
paymasterDetails,
|
|
17657
|
+
maxFeeInGasToken
|
|
17658
|
+
);
|
|
17659
|
+
return account.paymaster.executeTransaction(transaction, preparedTransaction.parameters).then((response) => ({ transaction_hash: response.transaction_hash }));
|
|
17660
|
+
}
|
|
17661
|
+
};
|
|
17662
|
+
|
|
17663
|
+
// src/plugins/paymaster/rpc.ts
|
|
17664
|
+
var convertCalls = (calls) => calls.map((call) => ({
|
|
17665
|
+
to: call.contractAddress,
|
|
17666
|
+
selector: getSelectorFromName(call.entrypoint),
|
|
17667
|
+
calldata: CallData.toHex(call.calldata)
|
|
17668
|
+
}));
|
|
17669
|
+
var convertFeeMode = (feeMode) => {
|
|
17670
|
+
if (feeMode.mode === "sponsored") {
|
|
17671
|
+
return { mode: "sponsored" };
|
|
17672
|
+
}
|
|
17673
|
+
return { mode: "default", gas_token: feeMode.gasToken };
|
|
17674
|
+
};
|
|
17675
|
+
var convertFEE_MODE = (feeMode) => {
|
|
17676
|
+
if (feeMode.mode === "sponsored") {
|
|
17677
|
+
return { mode: "sponsored" };
|
|
17678
|
+
}
|
|
17679
|
+
return { mode: "default", gasToken: feeMode.gas_token };
|
|
17680
|
+
};
|
|
17681
|
+
var convertTimeBounds = (timeBounds) => timeBounds ? {
|
|
17682
|
+
execute_after: timeBounds.executeAfter || 1,
|
|
17683
|
+
// If executeAfter is not provided, set it to 1, meaning the transaction can be executed immediately
|
|
17684
|
+
execute_before: timeBounds.executeBefore
|
|
17685
|
+
} : void 0;
|
|
17686
|
+
var convertTIME_BOUNDS = (timeBounds) => timeBounds ? {
|
|
17687
|
+
executeAfter: timeBounds.execute_after,
|
|
17688
|
+
executeBefore: timeBounds.execute_before
|
|
17689
|
+
} : void 0;
|
|
17690
|
+
var convertEXECUTION_PARAMETERS = (parameters) => ({
|
|
17691
|
+
version: parameters.version,
|
|
17692
|
+
feeMode: convertFEE_MODE(parameters.fee_mode),
|
|
17693
|
+
timeBounds: convertTIME_BOUNDS(parameters.time_bounds)
|
|
17694
|
+
});
|
|
17695
|
+
var defaultOptions = {
|
|
17696
|
+
headers: { "Content-Type": "application/json" }
|
|
17697
|
+
};
|
|
17698
|
+
var PaymasterRpc = class _PaymasterRpc {
|
|
17699
|
+
nodeUrl;
|
|
17700
|
+
headers;
|
|
17701
|
+
baseFetch;
|
|
17702
|
+
requestId;
|
|
17703
|
+
constructor(options) {
|
|
17704
|
+
if (options instanceof _PaymasterRpc) {
|
|
17705
|
+
this.nodeUrl = options.nodeUrl;
|
|
17706
|
+
this.headers = { ...defaultOptions.headers, ...options.headers };
|
|
17707
|
+
this.baseFetch = options.baseFetch;
|
|
17708
|
+
this.requestId = options.requestId;
|
|
17709
|
+
return;
|
|
17710
|
+
}
|
|
17711
|
+
if (options && "nodeUrl" in options && "headers" in options && "baseFetch" in options) {
|
|
17712
|
+
this.nodeUrl = options.nodeUrl ?? getDefaultPaymasterNodeUrl(void 0);
|
|
17713
|
+
this.headers = { ...defaultOptions.headers, ...options.headers };
|
|
17714
|
+
this.baseFetch = options.baseFetch ?? fetch_default;
|
|
17715
|
+
this.requestId = 0;
|
|
17716
|
+
return;
|
|
17717
|
+
}
|
|
17718
|
+
const { nodeUrl, headers, baseFetch } = options || {};
|
|
17719
|
+
if (nodeUrl && Object.values(_NetworkName).includes(nodeUrl)) {
|
|
17720
|
+
this.nodeUrl = getDefaultPaymasterNodeUrl(nodeUrl, options?.mute);
|
|
17721
|
+
} else if (nodeUrl) {
|
|
17722
|
+
this.nodeUrl = nodeUrl;
|
|
17723
|
+
} else {
|
|
17724
|
+
this.nodeUrl = getDefaultPaymasterNodeUrl(void 0, options?.mute);
|
|
17725
|
+
}
|
|
17726
|
+
this.baseFetch = baseFetch ?? fetch_default;
|
|
17727
|
+
this.headers = { ...defaultOptions.headers, ...headers };
|
|
17728
|
+
this.requestId = 0;
|
|
17729
|
+
}
|
|
17730
|
+
fetch(method, params, id = 0) {
|
|
17731
|
+
const rpcRequestBody = {
|
|
17732
|
+
id,
|
|
17733
|
+
jsonrpc: "2.0",
|
|
17734
|
+
method,
|
|
17735
|
+
...params && { params }
|
|
17736
|
+
};
|
|
17737
|
+
return this.baseFetch(this.nodeUrl, {
|
|
17738
|
+
method: "POST",
|
|
17739
|
+
body: stringify2(rpcRequestBody),
|
|
17740
|
+
headers: this.headers
|
|
17741
|
+
});
|
|
17742
|
+
}
|
|
17743
|
+
errorHandler(method, params, rpcError, otherError) {
|
|
17744
|
+
if (rpcError) {
|
|
17745
|
+
throw new RpcError(rpcError, method, params);
|
|
17746
|
+
}
|
|
17747
|
+
if (otherError instanceof LibraryError) {
|
|
17748
|
+
throw otherError;
|
|
17749
|
+
}
|
|
17750
|
+
if (otherError) {
|
|
17751
|
+
throw Error(otherError.message);
|
|
17752
|
+
}
|
|
17753
|
+
}
|
|
17754
|
+
async fetchEndpoint(method, params) {
|
|
17755
|
+
try {
|
|
17756
|
+
this.requestId += 1;
|
|
17757
|
+
const rawResult = await this.fetch(method, params, this.requestId);
|
|
17758
|
+
const { error, result } = await rawResult.json();
|
|
17759
|
+
this.errorHandler(method, params, error);
|
|
17760
|
+
return result;
|
|
17761
|
+
} catch (error) {
|
|
17762
|
+
this.errorHandler(method, params, error?.response?.data, error);
|
|
17763
|
+
throw error;
|
|
17764
|
+
}
|
|
17765
|
+
}
|
|
17766
|
+
async isAvailable() {
|
|
17767
|
+
return this.fetchEndpoint("paymaster_isAvailable");
|
|
17768
|
+
}
|
|
17769
|
+
async buildTransaction(transaction, parameters) {
|
|
17770
|
+
let userTransaction;
|
|
17771
|
+
switch (transaction.type) {
|
|
17772
|
+
case "invoke":
|
|
17773
|
+
userTransaction = {
|
|
17774
|
+
...transaction,
|
|
17775
|
+
invoke: {
|
|
17776
|
+
user_address: transaction.invoke.userAddress,
|
|
17777
|
+
calls: convertCalls(transaction.invoke.calls)
|
|
17778
|
+
}
|
|
17779
|
+
};
|
|
17780
|
+
break;
|
|
17781
|
+
case "deploy_and_invoke":
|
|
17782
|
+
userTransaction = {
|
|
17783
|
+
...transaction,
|
|
17784
|
+
invoke: {
|
|
17785
|
+
user_address: transaction.invoke.userAddress,
|
|
17786
|
+
calls: convertCalls(transaction.invoke.calls)
|
|
17787
|
+
}
|
|
17788
|
+
};
|
|
17789
|
+
break;
|
|
17790
|
+
case "deploy":
|
|
17791
|
+
default:
|
|
17792
|
+
userTransaction = transaction;
|
|
17793
|
+
break;
|
|
17794
|
+
}
|
|
17795
|
+
const executionParameters = {
|
|
17796
|
+
version: parameters.version,
|
|
17797
|
+
fee_mode: convertFeeMode(parameters.feeMode),
|
|
17798
|
+
time_bounds: convertTimeBounds(parameters.timeBounds)
|
|
17799
|
+
};
|
|
17800
|
+
const response = await this.fetchEndpoint("paymaster_buildTransaction", {
|
|
17801
|
+
transaction: userTransaction,
|
|
17802
|
+
parameters: executionParameters
|
|
17803
|
+
});
|
|
17804
|
+
const fee = {
|
|
17805
|
+
gas_token_price_in_strk: BigInt(response.fee.gas_token_price_in_strk),
|
|
17806
|
+
estimated_fee_in_strk: BigInt(response.fee.estimated_fee_in_strk),
|
|
17807
|
+
estimated_fee_in_gas_token: BigInt(response.fee.estimated_fee_in_gas_token),
|
|
17808
|
+
suggested_max_fee_in_strk: BigInt(response.fee.suggested_max_fee_in_strk),
|
|
17809
|
+
suggested_max_fee_in_gas_token: BigInt(response.fee.suggested_max_fee_in_gas_token)
|
|
17810
|
+
};
|
|
17811
|
+
switch (response.type) {
|
|
17812
|
+
case "invoke":
|
|
17813
|
+
return {
|
|
17814
|
+
type: "invoke",
|
|
17815
|
+
typed_data: response.typed_data,
|
|
17816
|
+
parameters: convertEXECUTION_PARAMETERS(response.parameters),
|
|
17817
|
+
fee
|
|
17818
|
+
};
|
|
17819
|
+
case "deploy_and_invoke":
|
|
17820
|
+
return {
|
|
17821
|
+
type: "deploy_and_invoke",
|
|
17822
|
+
deployment: response.deployment,
|
|
17823
|
+
typed_data: response.typed_data,
|
|
17824
|
+
parameters: convertEXECUTION_PARAMETERS(response.parameters),
|
|
17825
|
+
fee
|
|
17826
|
+
};
|
|
17827
|
+
case "deploy":
|
|
17828
|
+
default:
|
|
17829
|
+
return {
|
|
17830
|
+
type: "deploy",
|
|
17831
|
+
deployment: response.deployment,
|
|
17832
|
+
parameters: convertEXECUTION_PARAMETERS(response.parameters),
|
|
17833
|
+
fee
|
|
17834
|
+
};
|
|
17835
|
+
}
|
|
17836
|
+
}
|
|
17837
|
+
async executeTransaction(transaction, parameters) {
|
|
17838
|
+
let user_transaction;
|
|
17839
|
+
switch (transaction.type) {
|
|
17840
|
+
case "invoke":
|
|
17841
|
+
user_transaction = {
|
|
17842
|
+
...transaction,
|
|
17843
|
+
invoke: {
|
|
17844
|
+
user_address: transaction.invoke.userAddress,
|
|
17845
|
+
typed_data: transaction.invoke.typedData,
|
|
17846
|
+
signature: signatureToHexArray(transaction.invoke.signature)
|
|
17847
|
+
}
|
|
17848
|
+
};
|
|
17849
|
+
break;
|
|
17850
|
+
case "deploy_and_invoke":
|
|
17851
|
+
user_transaction = {
|
|
17852
|
+
...transaction,
|
|
17853
|
+
invoke: {
|
|
17854
|
+
user_address: transaction.invoke.userAddress,
|
|
17855
|
+
typed_data: transaction.invoke.typedData,
|
|
17856
|
+
signature: signatureToHexArray(transaction.invoke.signature)
|
|
17857
|
+
}
|
|
17858
|
+
};
|
|
17859
|
+
break;
|
|
17860
|
+
case "deploy":
|
|
17861
|
+
default:
|
|
17862
|
+
user_transaction = transaction;
|
|
17863
|
+
break;
|
|
17864
|
+
}
|
|
17865
|
+
const executionParameters = {
|
|
17866
|
+
version: parameters.version,
|
|
17867
|
+
fee_mode: convertFeeMode(parameters.feeMode),
|
|
17868
|
+
time_bounds: convertTimeBounds(parameters.timeBounds)
|
|
17869
|
+
};
|
|
17870
|
+
return this.fetchEndpoint("paymaster_executeTransaction", {
|
|
17871
|
+
transaction: user_transaction,
|
|
17872
|
+
parameters: executionParameters
|
|
17873
|
+
});
|
|
17874
|
+
}
|
|
17875
|
+
async getSupportedTokens() {
|
|
17876
|
+
return this.fetchEndpoint("paymaster_getSupportedTokens").then(
|
|
17877
|
+
(tokens) => tokens.map((token) => ({
|
|
17878
|
+
token_address: token.token_address,
|
|
17879
|
+
decimals: token.decimals,
|
|
17880
|
+
priceInStrk: BigInt(token.price_in_strk)
|
|
17881
|
+
}))
|
|
17882
|
+
);
|
|
17883
|
+
}
|
|
17884
|
+
};
|
|
17885
|
+
|
|
17886
|
+
// src/plugins/paymaster/interface.ts
|
|
17887
|
+
var PaymasterInterface = class {
|
|
17888
|
+
};
|
|
17889
|
+
|
|
17890
|
+
// src/plugins/paymaster/index.ts
|
|
17891
|
+
var PAYMASTER_PLUGIN_NAME = "paymaster";
|
|
17892
|
+
function paymasterPlugin(options) {
|
|
17893
|
+
return {
|
|
17894
|
+
name: PAYMASTER_PLUGIN_NAME,
|
|
17895
|
+
accountExtend(account) {
|
|
17896
|
+
const target = account;
|
|
17897
|
+
const members = {
|
|
17898
|
+
paymaster: options ? new PaymasterRpc(options) : new PaymasterRpc({ mute: true }),
|
|
17899
|
+
buildPaymasterTransaction: (calls, paymasterDetails) => PaymasterImpl.build(target, calls, paymasterDetails),
|
|
17900
|
+
estimatePaymasterTransactionFee: (calls, paymasterDetails) => PaymasterImpl.estimate(target, calls, paymasterDetails),
|
|
17901
|
+
preparePaymasterTransaction: (preparedTransaction, calls, paymasterDetails, maxFeeInGasToken) => PaymasterImpl.prepare(
|
|
17902
|
+
target,
|
|
17903
|
+
preparedTransaction,
|
|
17904
|
+
calls,
|
|
17905
|
+
paymasterDetails,
|
|
17906
|
+
maxFeeInGasToken
|
|
17907
|
+
),
|
|
17908
|
+
executePaymasterTransaction: (calls, paymasterDetails, maxFeeInGasToken) => PaymasterImpl.execute(target, calls, paymasterDetails, maxFeeInGasToken)
|
|
17909
|
+
};
|
|
17910
|
+
return Object.fromEntries(
|
|
17911
|
+
Object.entries(members).filter(([name]) => !(name in account))
|
|
17912
|
+
);
|
|
17913
|
+
}
|
|
17914
|
+
};
|
|
17915
|
+
}
|
|
17916
|
+
function applyPaymasterOption(plugins, paymaster) {
|
|
17917
|
+
if (!paymaster) return plugins;
|
|
17918
|
+
const configured = paymasterPlugin(paymaster);
|
|
17919
|
+
return plugins.some((plugin) => plugin.name === PAYMASTER_PLUGIN_NAME) ? plugins.map((plugin) => plugin.name === PAYMASTER_PLUGIN_NAME ? configured : plugin) : [...plugins, configured];
|
|
17920
|
+
}
|
|
17921
|
+
|
|
17213
17922
|
// src/plugins/defaults.ts
|
|
17214
17923
|
var defaultPlugins = [
|
|
17215
17924
|
starknetId(),
|
|
17216
17925
|
brotherId(),
|
|
17217
|
-
fastExecute()
|
|
17926
|
+
fastExecute(),
|
|
17927
|
+
paymasterPlugin()
|
|
17218
17928
|
];
|
|
17219
17929
|
|
|
17220
17930
|
// src/provider/rpc.ts
|
|
@@ -17787,25 +18497,6 @@ var Signer = class {
|
|
|
17787
18497
|
|
|
17788
18498
|
// src/signer/ethSigner.ts
|
|
17789
18499
|
var import_secp256k12 = require("@noble/curves/secp256k1.js");
|
|
17790
|
-
|
|
17791
|
-
// src/utils/uint256.ts
|
|
17792
|
-
var uint256_exports = {};
|
|
17793
|
-
__export(uint256_exports, {
|
|
17794
|
-
bnToUint256: () => bnToUint256,
|
|
17795
|
-
isUint256: () => isUint256,
|
|
17796
|
-
uint256ToBN: () => uint256ToBN
|
|
17797
|
-
});
|
|
17798
|
-
function uint256ToBN(uint2562) {
|
|
17799
|
-
return new CairoUint256(uint2562).toBigInt();
|
|
17800
|
-
}
|
|
17801
|
-
function isUint256(bn) {
|
|
17802
|
-
return CairoUint256.is(bn);
|
|
17803
|
-
}
|
|
17804
|
-
function bnToUint256(bn) {
|
|
17805
|
-
return new CairoUint256(bn).toUint256HexString();
|
|
17806
|
-
}
|
|
17807
|
-
|
|
17808
|
-
// src/signer/ethSigner.ts
|
|
17809
18500
|
var EthSigner = class {
|
|
17810
18501
|
pk;
|
|
17811
18502
|
// hex string without 0x and with an odd number of characters
|
|
@@ -17956,262 +18647,57 @@ var LedgerSigner111 = class {
|
|
|
17956
18647
|
*/
|
|
17957
18648
|
async getPubKey() {
|
|
17958
18649
|
if (!this.pubKey) await this.getPublicKeys();
|
|
17959
|
-
return this.pubKey;
|
|
17960
|
-
}
|
|
17961
|
-
/**
|
|
17962
|
-
* provides the full public key (with parity prefix)
|
|
17963
|
-
* @returns an hex string : 2 first characters are the parity, the 64 following characters are Point X coordinate. 64 last characters are Point Y coordinate.
|
|
17964
|
-
* @example
|
|
17965
|
-
* ```typescript
|
|
17966
|
-
* const result = await myLedgerSigner.getFullPubKey();
|
|
17967
|
-
* // result= "0x0403681417ba3e1f050dd3ccdceb8d22b5e44fa70ee7844d472c6a768bded5174e03cbc86f805dcfcb0c1922dd4daf181afa289d86223a18bc856276615bcc7787"
|
|
17968
|
-
* ```
|
|
17969
|
-
*/
|
|
17970
|
-
async getFullPubKey() {
|
|
17971
|
-
if (!this.fullPubKey) await this.getPublicKeys();
|
|
17972
|
-
return this.fullPubKey;
|
|
17973
|
-
}
|
|
17974
|
-
/**
|
|
17975
|
-
* Returns the version of the Starknet APP implemented in the Ledger.
|
|
17976
|
-
* @returns {string} version.
|
|
17977
|
-
* @example
|
|
17978
|
-
* ```typescript
|
|
17979
|
-
* const result = await myLedgerSigner.getAppVersion();
|
|
17980
|
-
* // result= "1.1.1"
|
|
17981
|
-
* ```
|
|
17982
|
-
*/
|
|
17983
|
-
async getAppVersion() {
|
|
17984
|
-
if (!this.appVersion) {
|
|
17985
|
-
const resp = await this._transporter.send(Number("0x5a"), 0, 0, 0);
|
|
17986
|
-
this.appVersion = `${resp[0]}.${resp[1]}.${resp[2]}`;
|
|
17987
|
-
}
|
|
17988
|
-
return this.appVersion;
|
|
17989
|
-
}
|
|
17990
|
-
/**
|
|
17991
|
-
* Sign a TypedData message (SNIP-12) in a Ledger.
|
|
17992
|
-
* @param {typedDataToHash} typedDataToHash A TypedData message compatible with SNIP-12.
|
|
17993
|
-
* @param {string} accountAddress Signer account address (Hex or num string)
|
|
17994
|
-
* @returns {Signature} The signed message.
|
|
17995
|
-
* @example
|
|
17996
|
-
* ```typescript
|
|
17997
|
-
* const result = myLedgerSigner.signMessage(snip12Message, account0.address);
|
|
17998
|
-
* // result = Signature { r: 611475243393396148729326917410546146405234155928298353899191529090923298688n,
|
|
17999
|
-
* // s: 798839819213540985856952481651392652149797817551686626114697493101433761982n,
|
|
18000
|
-
* // recovery: 0}
|
|
18001
|
-
* ```
|
|
18002
|
-
*/
|
|
18003
|
-
async signMessage(typedDataToHash, accountAddress) {
|
|
18004
|
-
const msgHash = getMessageHash(typedDataToHash, accountAddress);
|
|
18005
|
-
return this.signRaw(msgHash);
|
|
18006
|
-
}
|
|
18007
|
-
/**
|
|
18008
|
-
* Sign in a Ledger a V3 transaction. This is a blind sign on the Ledger screen.
|
|
18009
|
-
* @param {Call1[]} transactions An array of `Call` transactions (generated for example by `myContract.populate()`).
|
|
18010
|
-
* @param {InvocationsSignerDetails} transactionsDetail An object that includes all the necessary inputs to hash the transaction.
|
|
18011
|
-
* @returns {Signature} The signed transaction.
|
|
18012
|
-
* @example
|
|
18013
|
-
* ```typescript
|
|
18014
|
-
* const txDetailsV3: V3InvocationsSignerDetails = {
|
|
18015
|
-
* chainId: constants.StarknetChainId.SN_MAIN,
|
|
18016
|
-
* nonce: "28",
|
|
18017
|
-
* accountDeploymentData: [],
|
|
18018
|
-
* paymasterData: [],
|
|
18019
|
-
* cairoVersion: "1",
|
|
18020
|
-
* feeDataAvailabilityMode: "L1",
|
|
18021
|
-
* nonceDataAvailabilityMode: "L1",
|
|
18022
|
-
* resourceBounds: {
|
|
18023
|
-
* l1_gas: {
|
|
18024
|
-
* max_amount: "0x2a00",
|
|
18025
|
-
* max_price_per_unit: "0x5c00000"
|
|
18026
|
-
* },
|
|
18027
|
-
* l2_gas: {
|
|
18028
|
-
* max_amount: "0x00",
|
|
18029
|
-
* max_price_per_unit: "0x00"
|
|
18030
|
-
* },
|
|
18031
|
-
* },
|
|
18032
|
-
* tip: 0,
|
|
18033
|
-
* version: "0x3",
|
|
18034
|
-
* walletAddress: account0.address
|
|
18035
|
-
* }
|
|
18036
|
-
* const result = myLedgerSigner.signTransaction([call0, call1], txDetailsV3);
|
|
18037
|
-
* // result = Signature { r: 611475243393396148729326917410546146405234155928298353899191529090923298688n,
|
|
18038
|
-
* // s: 798839819213540985856952481651392652149797817551686626114697493101433761982n,
|
|
18039
|
-
* // recovery: 0}
|
|
18040
|
-
* ```
|
|
18041
|
-
*/
|
|
18042
|
-
async signTransaction(transactions, transactionsDetail) {
|
|
18043
|
-
const compiledCalldata = getExecuteCalldata(transactions, transactionsDetail.cairoVersion);
|
|
18044
|
-
const msgHash = calculateInvokeTransactionHash2({
|
|
18045
|
-
...transactionsDetail,
|
|
18046
|
-
senderAddress: transactionsDetail.walletAddress,
|
|
18047
|
-
compiledCalldata,
|
|
18048
|
-
version: transactionsDetail.version,
|
|
18049
|
-
nonceDataAvailabilityMode: intDAM(transactionsDetail.nonceDataAvailabilityMode),
|
|
18050
|
-
feeDataAvailabilityMode: intDAM(transactionsDetail.feeDataAvailabilityMode)
|
|
18051
|
-
});
|
|
18052
|
-
return this.signRaw(msgHash);
|
|
18053
|
-
}
|
|
18054
|
-
/**
|
|
18055
|
-
* Sign in a Ledger the deployment of a new account. This is a blind sign on the Ledger screen.
|
|
18056
|
-
* @param {DeployAccountSignerDetails} details An object that includes all necessary data to calculate the Hash.
|
|
18057
|
-
* @returns {Signature} The deploy account signature.
|
|
18058
|
-
* @example
|
|
18059
|
-
* ```typescript
|
|
18060
|
-
* const result = myLedgerSigner.signDeployAccountTransaction(details);
|
|
18061
|
-
* // result = Signature { r: 611475243393396148729326917410546146405234155928298353899191529090923298688n,
|
|
18062
|
-
* // s: 798839819213540985856952481651392652149797817551686626114697493101433761982n,
|
|
18063
|
-
* // recovery: 0}
|
|
18064
|
-
* ```
|
|
18065
|
-
*/
|
|
18066
|
-
async signDeployAccountTransaction(details) {
|
|
18067
|
-
const compiledConstructorCalldata = CallData.compile(details.constructorCalldata);
|
|
18068
|
-
const msgHash = calculateDeployAccountTransactionHash2({
|
|
18069
|
-
...details,
|
|
18070
|
-
salt: details.addressSalt,
|
|
18071
|
-
compiledConstructorCalldata,
|
|
18072
|
-
version: details.version,
|
|
18073
|
-
nonceDataAvailabilityMode: intDAM(details.nonceDataAvailabilityMode),
|
|
18074
|
-
feeDataAvailabilityMode: intDAM(details.feeDataAvailabilityMode)
|
|
18075
|
-
});
|
|
18076
|
-
return this.signRaw(msgHash);
|
|
18077
|
-
}
|
|
18078
|
-
/**
|
|
18079
|
-
* Sign in a Ledger the declaration of a new class. This is a blind sign on the Ledger screen.
|
|
18080
|
-
* @param {DeclareSignerDetails} details An object that includes all necessary data to calculate the Hash.
|
|
18081
|
-
* @returns {Signature} The declare Signature.
|
|
18082
|
-
* @example
|
|
18083
|
-
* ```typescript
|
|
18084
|
-
* const result = myLedgerSigner.signDeclareTransaction(details);
|
|
18085
|
-
* // result = Signature { r: 611475243393396148729326917410546146405234155928298353899191529090923298688n,
|
|
18086
|
-
* // s: 798839819213540985856952481651392652149797817551686626114697493101433761982n,
|
|
18087
|
-
* // recovery: 0}
|
|
18088
|
-
* ```
|
|
18089
|
-
*/
|
|
18090
|
-
async signDeclareTransaction(details) {
|
|
18091
|
-
const msgHash = calculateDeclareTransactionHash2({
|
|
18092
|
-
...details,
|
|
18093
|
-
version: details.version,
|
|
18094
|
-
nonceDataAvailabilityMode: intDAM(details.nonceDataAvailabilityMode),
|
|
18095
|
-
feeDataAvailabilityMode: intDAM(details.feeDataAvailabilityMode)
|
|
18096
|
-
});
|
|
18097
|
-
return this.signRaw(msgHash);
|
|
18098
|
-
}
|
|
18099
|
-
/**
|
|
18100
|
-
* Internal function to sign a hash in a Ledger Nano.
|
|
18101
|
-
* This is a blind sign in the Ledger ; no display of what you are signing.
|
|
18102
|
-
*/
|
|
18103
|
-
async signRaw(msgHash) {
|
|
18104
|
-
addHexPrefix(
|
|
18105
|
-
buf2hex(await this._transporter.send(Number("0x5a"), 2, 0, 0, buffer_default.from(this.pathBuffer)))
|
|
18106
|
-
);
|
|
18107
|
-
const shiftedHash = toHex(BigInt(msgHash) << 4n);
|
|
18108
|
-
const buff2 = hexToBytes(shiftedHash);
|
|
18109
|
-
const respSign2 = Uint8Array.from(
|
|
18110
|
-
await this._transporter.send(Number("0x5a"), 2, 1, 0, buffer_default.from(buff2))
|
|
18111
|
-
);
|
|
18112
|
-
const r = BigInt(addHexPrefix(buf2hex(respSign2.subarray(1, 33))));
|
|
18113
|
-
const s = BigInt(addHexPrefix(buf2hex(respSign2.subarray(33, 65))));
|
|
18114
|
-
const v = respSign2[65];
|
|
18115
|
-
const sign0 = new starkCurve.Signature(r, s);
|
|
18116
|
-
const sign1 = sign0.addRecoveryBit(v);
|
|
18117
|
-
return sign1;
|
|
18118
|
-
}
|
|
18119
|
-
/** internal function to get both the Starknet public key and the full public key */
|
|
18120
|
-
async getPublicKeys() {
|
|
18121
|
-
const pathBuff = this.pathBuffer;
|
|
18122
|
-
const respGetPublic = Uint8Array.from(
|
|
18123
|
-
await this._transporter.send(Number("0x5a"), 1, 0, 0, buffer_default.from(pathBuff))
|
|
18124
|
-
);
|
|
18125
|
-
this.pubKey = addHexPrefix(buf2hex(respGetPublic.subarray(1, 33)));
|
|
18126
|
-
this.fullPubKey = addHexPrefix(buf2hex(respGetPublic.subarray(0, 65)));
|
|
18127
|
-
}
|
|
18128
|
-
};
|
|
18129
|
-
function getLedgerPathBuffer111(accountId, applicationName = "LedgerW") {
|
|
18130
|
-
const path0buff = new Uint8Array([128, 0, 10, 85]);
|
|
18131
|
-
const path1buff = new Uint8Array([71, 65, 233, 201]);
|
|
18132
|
-
const path2buff = applicationName === "LedgerW" ? new Uint8Array([43, 206, 231, 219]) : stringToSha256ToArrayBuff4(applicationName);
|
|
18133
|
-
const path3buff = new Uint8Array([0, 0, 0, 0]);
|
|
18134
|
-
const hex = toHex(accountId);
|
|
18135
|
-
const padded = addHexPrefix(removeHexPrefix(hex).padStart(8, "0"));
|
|
18136
|
-
const path4buff = hexToBytes(padded);
|
|
18137
|
-
const path5buff = new Uint8Array([0, 0, 0, 0]);
|
|
18138
|
-
const pathBuff = concatenateArrayBuffer([
|
|
18139
|
-
path0buff,
|
|
18140
|
-
path1buff,
|
|
18141
|
-
path2buff,
|
|
18142
|
-
path3buff,
|
|
18143
|
-
path4buff,
|
|
18144
|
-
path5buff
|
|
18145
|
-
]);
|
|
18146
|
-
return pathBuff;
|
|
18147
|
-
}
|
|
18148
|
-
|
|
18149
|
-
// src/utils/address.ts
|
|
18150
|
-
var import_utils5 = require("@noble/curves/utils.js");
|
|
18151
|
-
function addAddressPadding(address) {
|
|
18152
|
-
const hex = toHex(isString(address) ? addHexPrefix(address) : address);
|
|
18153
|
-
const padded = removeHexPrefix(hex).padStart(64, "0");
|
|
18154
|
-
return addHexPrefix(padded);
|
|
18155
|
-
}
|
|
18156
|
-
function validateAndParseAddress(address) {
|
|
18157
|
-
if (isString(address) && !/^(0x)?[0-9a-f]+$/i.test(address)) {
|
|
18158
|
-
throw new Error("Invalid Address Format");
|
|
18159
|
-
}
|
|
18160
|
-
const result = addAddressPadding(address);
|
|
18161
|
-
if (!result.match(/^(0x)?[0-9a-fA-F]{64}$/)) {
|
|
18162
|
-
throw new Error("Invalid Address Format");
|
|
18163
|
-
}
|
|
18164
|
-
assertInRange(result, ZERO, ADDR_BOUND - 1n, "Starknet Address");
|
|
18165
|
-
return result;
|
|
18166
|
-
}
|
|
18167
|
-
function getChecksumAddress(address) {
|
|
18168
|
-
const chars = removeHexPrefix(validateAndParseAddress(address)).toLowerCase().split("");
|
|
18169
|
-
const hex = removeHexPrefix(keccakBn(address));
|
|
18170
|
-
const hashed = (0, import_utils5.hexToBytes)(hex.padStart(64, "0"));
|
|
18171
|
-
for (let i = 0; i < chars.length; i += 2) {
|
|
18172
|
-
if (hashed[i >> 1] >> 4 >= 8) {
|
|
18173
|
-
chars[i] = chars[i].toUpperCase();
|
|
18174
|
-
}
|
|
18175
|
-
if ((hashed[i >> 1] & 15) >= 8) {
|
|
18176
|
-
chars[i + 1] = chars[i + 1].toUpperCase();
|
|
18177
|
-
}
|
|
18650
|
+
return this.pubKey;
|
|
18178
18651
|
}
|
|
18179
|
-
return addHexPrefix(chars.join(""));
|
|
18180
|
-
}
|
|
18181
|
-
function validateChecksumAddress(address) {
|
|
18182
|
-
return getChecksumAddress(address) === address;
|
|
18183
|
-
}
|
|
18184
|
-
|
|
18185
|
-
// src/signer/ledgerSigner221.ts
|
|
18186
|
-
var LedgerSigner221 = class extends LedgerSigner111 {
|
|
18187
18652
|
/**
|
|
18188
|
-
*
|
|
18189
|
-
* @
|
|
18190
|
-
*
|
|
18191
|
-
* @param {number} accountID ID of Ledger Nano (can handle 2**31 accounts).
|
|
18192
|
-
* @param {string} [eip2645application='LedgerW'] A wallet is defined by an ERC2645 derivation path (6 items).
|
|
18193
|
-
* One item is called `application` and can be customized.
|
|
18194
|
-
* Default value is `LedgerW`.
|
|
18195
|
-
* @param {LedgerPathCalculation} [pathFunction=getLedgerPathBuffer221]
|
|
18196
|
-
* defines the function that will calculate the path. By default `getLedgerPathBuffer221` is selected.
|
|
18197
|
-
*
|
|
18198
|
-
* If you are using APP v2.2.1 with an account created with the v1.1.1, you need to use :
|
|
18653
|
+
* provides the full public key (with parity prefix)
|
|
18654
|
+
* @returns an hex string : 2 first characters are the parity, the 64 following characters are Point X coordinate. 64 last characters are Point Y coordinate.
|
|
18655
|
+
* @example
|
|
18199
18656
|
* ```typescript
|
|
18200
|
-
* const
|
|
18657
|
+
* const result = await myLedgerSigner.getFullPubKey();
|
|
18658
|
+
* // result= "0x0403681417ba3e1f050dd3ccdceb8d22b5e44fa70ee7844d472c6a768bded5174e03cbc86f805dcfcb0c1922dd4daf181afa289d86223a18bc856276615bcc7787"
|
|
18201
18659
|
* ```
|
|
18660
|
+
*/
|
|
18661
|
+
async getFullPubKey() {
|
|
18662
|
+
if (!this.fullPubKey) await this.getPublicKeys();
|
|
18663
|
+
return this.fullPubKey;
|
|
18664
|
+
}
|
|
18665
|
+
/**
|
|
18666
|
+
* Returns the version of the Starknet APP implemented in the Ledger.
|
|
18667
|
+
* @returns {string} version.
|
|
18202
18668
|
* @example
|
|
18203
18669
|
* ```typescript
|
|
18204
|
-
*
|
|
18205
|
-
*
|
|
18206
|
-
* const myLedgerSigner = new LedgerSigner221(myNodeTransport, 0);
|
|
18670
|
+
* const result = await myLedgerSigner.getAppVersion();
|
|
18671
|
+
* // result= "1.1.1"
|
|
18207
18672
|
* ```
|
|
18208
18673
|
*/
|
|
18209
|
-
|
|
18210
|
-
|
|
18674
|
+
async getAppVersion() {
|
|
18675
|
+
if (!this.appVersion) {
|
|
18676
|
+
const resp = await this._transporter.send(Number("0x5a"), 0, 0, 0);
|
|
18677
|
+
this.appVersion = `${resp[0]}.${resp[1]}.${resp[2]}`;
|
|
18678
|
+
}
|
|
18679
|
+
return this.appVersion;
|
|
18211
18680
|
}
|
|
18212
18681
|
/**
|
|
18213
|
-
* Sign
|
|
18214
|
-
* @param {
|
|
18682
|
+
* Sign a TypedData message (SNIP-12) in a Ledger.
|
|
18683
|
+
* @param {typedDataToHash} typedDataToHash A TypedData message compatible with SNIP-12.
|
|
18684
|
+
* @param {string} accountAddress Signer account address (Hex or num string)
|
|
18685
|
+
* @returns {Signature} The signed message.
|
|
18686
|
+
* @example
|
|
18687
|
+
* ```typescript
|
|
18688
|
+
* const result = myLedgerSigner.signMessage(snip12Message, account0.address);
|
|
18689
|
+
* // result = Signature { r: 611475243393396148729326917410546146405234155928298353899191529090923298688n,
|
|
18690
|
+
* // s: 798839819213540985856952481651392652149797817551686626114697493101433761982n,
|
|
18691
|
+
* // recovery: 0}
|
|
18692
|
+
* ```
|
|
18693
|
+
*/
|
|
18694
|
+
async signMessage(typedDataToHash, accountAddress) {
|
|
18695
|
+
const msgHash = getMessageHash(typedDataToHash, accountAddress);
|
|
18696
|
+
return this.signRaw(msgHash);
|
|
18697
|
+
}
|
|
18698
|
+
/**
|
|
18699
|
+
* Sign in a Ledger a V3 transaction. This is a blind sign on the Ledger screen.
|
|
18700
|
+
* @param {Call1[]} transactions An array of `Call` transactions (generated for example by `myContract.populate()`).
|
|
18215
18701
|
* @param {InvocationsSignerDetails} transactionsDetail An object that includes all the necessary inputs to hash the transaction.
|
|
18216
18702
|
* @returns {Signature} The signed transaction.
|
|
18217
18703
|
* @example
|
|
@@ -18233,294 +18719,110 @@ var LedgerSigner221 = class extends LedgerSigner111 {
|
|
|
18233
18719
|
* max_amount: "0x00",
|
|
18234
18720
|
* max_price_per_unit: "0x00"
|
|
18235
18721
|
* },
|
|
18236
|
-
* },
|
|
18237
|
-
* tip: 0,
|
|
18238
|
-
* version: "0x3",
|
|
18239
|
-
* walletAddress: account0.address
|
|
18240
|
-
* }
|
|
18241
|
-
* const result = myLedgerSigner.signTransaction([call0, call1], txDetailsV3);
|
|
18242
|
-
* // result = Signature { r: 611475243393396148729326917410546146405234155928298353899191529090923298688n,
|
|
18243
|
-
* // s: 798839819213540985856952481651392652149797817551686626114697493101433761982n,
|
|
18244
|
-
* // recovery: 0}
|
|
18245
|
-
* ```
|
|
18246
|
-
*/
|
|
18247
|
-
async signTransaction(transactions, transactionsDetail) {
|
|
18248
|
-
const compiledCalldata = getExecuteCalldata(transactions, transactionsDetail.cairoVersion);
|
|
18249
|
-
const msgHash = calculateInvokeTransactionHash2({
|
|
18250
|
-
...transactionsDetail,
|
|
18251
|
-
senderAddress: transactionsDetail.walletAddress,
|
|
18252
|
-
compiledCalldata,
|
|
18253
|
-
version: transactionsDetail.version,
|
|
18254
|
-
nonceDataAvailabilityMode: intDAM(transactionsDetail.nonceDataAvailabilityMode),
|
|
18255
|
-
feeDataAvailabilityMode: intDAM(transactionsDetail.feeDataAvailabilityMode)
|
|
18256
|
-
});
|
|
18257
|
-
|
|
18258
|
-
assert(
|
|
18259
|
-
toBigInt(msgHash) === ledgerResponse.hash,
|
|
18260
|
-
"The V3 transaction hash calculated by Starknet.js is different from the one calculated by the Ledger."
|
|
18261
|
-
);
|
|
18262
|
-
return ledgerResponse.signature;
|
|
18263
|
-
}
|
|
18264
|
-
/**
|
|
18265
|
-
* Sign in a Ledger the deployment of a new account. The details are displayed on the Ledger screen.
|
|
18266
|
-
* @param {DeployAccountSignerDetails} details An object that includes all necessary data to calculate the Hash.
|
|
18267
|
-
* @returns {Signature} The deploy account signature.
|
|
18268
|
-
* @example
|
|
18269
|
-
* ```typescript
|
|
18270
|
-
* const result = myLedgerSigner.signDeployAccountTransaction(details);
|
|
18271
|
-
* // result = Signature { r: 611475243393396148729326917410546146405234155928298353899191529090923298688n,
|
|
18272
|
-
* // s: 798839819213540985856952481651392652149797817551686626114697493101433761982n,
|
|
18273
|
-
* // recovery: 0}
|
|
18274
|
-
* ```
|
|
18275
|
-
*/
|
|
18276
|
-
async signDeployAccountTransaction(details) {
|
|
18277
|
-
const compiledConstructorCalldata = CallData.compile(details.constructorCalldata);
|
|
18278
|
-
const msgHash = calculateDeployAccountTransactionHash2({
|
|
18279
|
-
...details,
|
|
18280
|
-
salt: details.addressSalt,
|
|
18281
|
-
compiledConstructorCalldata,
|
|
18282
|
-
version: details.version,
|
|
18283
|
-
nonceDataAvailabilityMode: intDAM(details.nonceDataAvailabilityMode),
|
|
18284
|
-
feeDataAvailabilityMode: intDAM(details.feeDataAvailabilityMode)
|
|
18285
|
-
});
|
|
18286
|
-
const ledgerResponse = await this.signDeployAccountV3(details);
|
|
18287
|
-
assert(
|
|
18288
|
-
toBigInt(msgHash) === ledgerResponse.hash,
|
|
18289
|
-
"The transaction hash calculated by Starknet.js is different from the one calculated by the Ledger."
|
|
18290
|
-
);
|
|
18291
|
-
return ledgerResponse.signature;
|
|
18292
|
-
}
|
|
18293
|
-
/**
|
|
18294
|
-
* Internal function to convert a bigNumberish to an Uint8array of 256 bits
|
|
18295
|
-
* @param {BigNumberish} input input value
|
|
18296
|
-
* @returns {Uint8Array} a Uint8Array containing 32 bytes.
|
|
18297
|
-
*/
|
|
18298
|
-
convertBnToLedger(input) {
|
|
18299
|
-
return hexToBytes(addAddressPadding(toHex(input)));
|
|
18300
|
-
}
|
|
18301
|
-
/**
|
|
18302
|
-
* Internal function to decode the response of the Ledger signature
|
|
18303
|
-
* @param {Uint8Array} respSign the Buffer response of the Ledger
|
|
18304
|
-
* @returns { hash: bigint; signature: Signature } transaction hash & signature
|
|
18305
|
-
*/
|
|
18306
|
-
decodeSignatureLedger(respSign) {
|
|
18307
|
-
const h = BigInt(addHexPrefix(buf2hex(respSign.subarray(0, 32))));
|
|
18308
|
-
const r = BigInt(addHexPrefix(buf2hex(respSign.subarray(33, 65))));
|
|
18309
|
-
const s = BigInt(addHexPrefix(buf2hex(respSign.subarray(65, 97))));
|
|
18310
|
-
const v = respSign[97];
|
|
18311
|
-
const sign0 = new starkCurve.Signature(r, s);
|
|
18312
|
-
const sign1 = sign0.addRecoveryBit(v);
|
|
18313
|
-
return { hash: h, signature: sign1 };
|
|
18314
|
-
}
|
|
18315
|
-
/** Internal function to convert a Call to an array of Uint8Array.
|
|
18316
|
-
* @param {Call} call A Call to convert.
|
|
18317
|
-
* @return {Uint8Array[]} Call encoded in an array of Uint8Array (each containing 7 u256).
|
|
18318
|
-
*/
|
|
18319
|
-
encodeCall(call) {
|
|
18320
|
-
const toBuf = this.convertBnToLedger(call.contractAddress);
|
|
18321
|
-
const selectorBuf = hexToBytes(addAddressPadding(getSelector(call.entrypoint)));
|
|
18322
|
-
let calldataBuf = new Uint8Array([]);
|
|
18323
|
-
if (call.calldata) {
|
|
18324
|
-
const compiledCalldata = CallData.compile(call.calldata);
|
|
18325
|
-
calldataBuf = concatenateArrayBuffer(
|
|
18326
|
-
compiledCalldata.map((parameter) => {
|
|
18327
|
-
const a = this.convertBnToLedger(parameter);
|
|
18328
|
-
return a;
|
|
18329
|
-
})
|
|
18330
|
-
);
|
|
18331
|
-
}
|
|
18332
|
-
const callBuf = concatenateArrayBuffer([toBuf, selectorBuf, calldataBuf]);
|
|
18333
|
-
const calldatas = [];
|
|
18334
|
-
const chunkSize = 7 * 32;
|
|
18335
|
-
for (let i = 0; i < callBuf.length; i += chunkSize)
|
|
18336
|
-
calldatas.push(callBuf.subarray(i, i + chunkSize));
|
|
18337
|
-
return calldatas;
|
|
18338
|
-
}
|
|
18339
|
-
/**
|
|
18340
|
-
* Ask to the Ledger Nano to display and sign a Starknet V3 transaction.
|
|
18341
|
-
* @param {V3InvocationsSignerDetails} txDetails All the details needed for a txV3.
|
|
18342
|
-
* @param {Call[]} calls array of Starknet invocations
|
|
18343
|
-
* @returns an object including the transaction Hash and the signature
|
|
18344
|
-
* @example
|
|
18345
|
-
* ```typescript
|
|
18346
|
-
* const calls: Call[] = [{contractAddress: "0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7",
|
|
18347
|
-
* entrypoint: "transfer",
|
|
18348
|
-
* calldata:["0x11f5fc2a92ac03434a7937fe982f5e5293b65ad438a989c5b78fb8f04a12016",
|
|
18349
|
-
* "0x9184e72a000", "0x0"]}];
|
|
18350
|
-
* const txDetailsV3: V3InvocationsSignerDetails = {
|
|
18351
|
-
* chainId: constants.StarknetChainId.SN_MAIN,
|
|
18352
|
-
* nonce: "28", accountDeploymentData: [],
|
|
18353
|
-
* paymasterData: [], cairoVersion: "1",
|
|
18354
|
-
* feeDataAvailabilityMode: "L1", nonceDataAvailabilityMode: "L1",
|
|
18355
|
-
* resourceBounds: {
|
|
18356
|
-
* l1_gas: { max_amount: "0x2a00", max_price_per_unit: "0x5c00000"
|
|
18357
|
-
* },
|
|
18358
|
-
* l2_gas: { max_amount: "0x00", max_price_per_unit: "0x00"},
|
|
18359
|
-
* }, tip: 0, version: "0x3", walletAddress: account0.address
|
|
18360
|
-
* };
|
|
18361
|
-
* const res = await myLedgerSigner.signTxV3(txDetailsV3, calls);
|
|
18362
|
-
* // res = {hash:
|
|
18363
|
-
* // signature:
|
|
18364
|
-
* // }
|
|
18365
|
-
* ```
|
|
18366
|
-
*/
|
|
18367
|
-
async signTxV3(txDetails, calls) {
|
|
18368
|
-
assert(txDetails.paymasterData.length <= 7, "Paymaster data includes more than 7 items.");
|
|
18369
|
-
assert(
|
|
18370
|
-
txDetails.accountDeploymentData.length <= 7,
|
|
18371
|
-
"accountDeploymentData includes more than 7 items"
|
|
18372
|
-
);
|
|
18373
|
-
await this._transporter.send(Number("0x5a"), 3, 0, 0, buffer_default.from(this.pathBuffer));
|
|
18374
|
-
const accountAddressBuf = this.convertBnToLedger(txDetails.walletAddress);
|
|
18375
|
-
const tipBuf = this.convertBnToLedger(txDetails.tip);
|
|
18376
|
-
const chainIdBuf = this.convertBnToLedger(txDetails.chainId);
|
|
18377
|
-
const nonceBuf = this.convertBnToLedger(txDetails.nonce);
|
|
18378
|
-
const dAModeHashBuf = this.convertBnToLedger(
|
|
18379
|
-
hashDAMode(
|
|
18380
|
-
txDetails.nonceDataAvailabilityMode === api_exports.EDataAvailabilityMode.L1 ? api_exports.EDAMode.L1 : api_exports.EDAMode.L2,
|
|
18381
|
-
txDetails.feeDataAvailabilityMode === api_exports.EDataAvailabilityMode.L1 ? api_exports.EDAMode.L1 : api_exports.EDAMode.L2
|
|
18382
|
-
)
|
|
18383
|
-
);
|
|
18384
|
-
const l1_gasBuf = this.convertBnToLedger(encodeResourceBoundsL1(txDetails.resourceBounds));
|
|
18385
|
-
const l2_gasBuf = this.convertBnToLedger(encodeResourceBoundsL2(txDetails.resourceBounds));
|
|
18386
|
-
const dataBuf = concatenateArrayBuffer([
|
|
18387
|
-
accountAddressBuf,
|
|
18388
|
-
tipBuf,
|
|
18389
|
-
l1_gasBuf,
|
|
18390
|
-
l2_gasBuf,
|
|
18391
|
-
chainIdBuf,
|
|
18392
|
-
nonceBuf,
|
|
18393
|
-
dAModeHashBuf
|
|
18394
|
-
]);
|
|
18395
|
-
await this._transporter.send(Number("0x5a"), 3, 1, 0, buffer_default.from(dataBuf));
|
|
18396
|
-
const paymasterBuf = concatenateArrayBuffer(
|
|
18397
|
-
txDetails.paymasterData.map((value) => {
|
|
18398
|
-
const a = this.convertBnToLedger(value);
|
|
18399
|
-
return a;
|
|
18400
|
-
})
|
|
18401
|
-
);
|
|
18402
|
-
await this._transporter.send(Number("0x5a"), 3, 2, 0, buffer_default.from(paymasterBuf));
|
|
18403
|
-
const accountDeployDataBuf = concatenateArrayBuffer(
|
|
18404
|
-
txDetails.paymasterData.map((value) => {
|
|
18405
|
-
const a = this.convertBnToLedger(value);
|
|
18406
|
-
return a;
|
|
18407
|
-
})
|
|
18408
|
-
);
|
|
18409
|
-
await this._transporter.send(Number("0x5a"), 3, 3, 0, buffer_default.from(accountDeployDataBuf));
|
|
18410
|
-
const nbCallsBuf = this.convertBnToLedger(calls.length);
|
|
18411
|
-
await this._transporter.send(Number("0x5a"), 3, 4, 0, buffer_default.from(nbCallsBuf));
|
|
18412
|
-
let respSign = new Uint8Array(0);
|
|
18413
|
-
for (const call of calls) {
|
|
18414
|
-
const calldatas = this.encodeCall(call);
|
|
18415
|
-
await this._transporter.send(Number("0x5a"), 3, 5, 0, buffer_default.from(calldatas[0]));
|
|
18416
|
-
if (calldatas.length > 1) {
|
|
18417
|
-
calldatas.slice(1).forEach(async (part) => {
|
|
18418
|
-
await this._transporter.send(Number("0x5a"), 3, 5, 1, buffer_default.from(part));
|
|
18419
|
-
});
|
|
18420
|
-
}
|
|
18421
|
-
respSign = await this._transporter.send(Number("0x5a"), 3, 5, 2);
|
|
18422
|
-
}
|
|
18423
|
-
return this.decodeSignatureLedger(respSign);
|
|
18722
|
+
* },
|
|
18723
|
+
* tip: 0,
|
|
18724
|
+
* version: "0x3",
|
|
18725
|
+
* walletAddress: account0.address
|
|
18726
|
+
* }
|
|
18727
|
+
* const result = myLedgerSigner.signTransaction([call0, call1], txDetailsV3);
|
|
18728
|
+
* // result = Signature { r: 611475243393396148729326917410546146405234155928298353899191529090923298688n,
|
|
18729
|
+
* // s: 798839819213540985856952481651392652149797817551686626114697493101433761982n,
|
|
18730
|
+
* // recovery: 0}
|
|
18731
|
+
* ```
|
|
18732
|
+
*/
|
|
18733
|
+
async signTransaction(transactions, transactionsDetail) {
|
|
18734
|
+
const compiledCalldata = getExecuteCalldata(transactions, transactionsDetail.cairoVersion);
|
|
18735
|
+
const msgHash = calculateInvokeTransactionHash2({
|
|
18736
|
+
...transactionsDetail,
|
|
18737
|
+
senderAddress: transactionsDetail.walletAddress,
|
|
18738
|
+
compiledCalldata,
|
|
18739
|
+
version: transactionsDetail.version,
|
|
18740
|
+
nonceDataAvailabilityMode: intDAM(transactionsDetail.nonceDataAvailabilityMode),
|
|
18741
|
+
feeDataAvailabilityMode: intDAM(transactionsDetail.feeDataAvailabilityMode)
|
|
18742
|
+
});
|
|
18743
|
+
return this.signRaw(msgHash);
|
|
18424
18744
|
}
|
|
18425
18745
|
/**
|
|
18426
|
-
*
|
|
18427
|
-
* @param {
|
|
18428
|
-
* @returns
|
|
18746
|
+
* Sign in a Ledger the deployment of a new account. This is a blind sign on the Ledger screen.
|
|
18747
|
+
* @param {DeployAccountSignerDetails} details An object that includes all necessary data to calculate the Hash.
|
|
18748
|
+
* @returns {Signature} The deploy account signature.
|
|
18429
18749
|
* @example
|
|
18430
18750
|
* ```typescript
|
|
18431
|
-
* const
|
|
18432
|
-
* {
|
|
18433
|
-
*
|
|
18434
|
-
*
|
|
18435
|
-
* resourceBounds: {
|
|
18436
|
-
* l2_gas: { max_amount: '0x0', max_price_per_unit: '0x0' },
|
|
18437
|
-
* l1_gas: { max_amount: '0x226', max_price_per_unit: '0x22ecb25c00' }
|
|
18438
|
-
* },
|
|
18439
|
-
* classHash: '0x540d7f5ec7ecf317e68d48564934cb99259781b1ee3cedbbc37ec5337f8e688',
|
|
18440
|
-
* constructorCalldata: [
|
|
18441
|
-
* '3571125127744830445572285574469842579401255431821644822726857471463672199621'
|
|
18442
|
-
* ],
|
|
18443
|
-
* contractAddress: '0x4ca062add1cf12a107be1107af17981cf6e544a24d987693230ea481d3d5e34',
|
|
18444
|
-
* addressSalt: '0x07e52f68e3160e1ef698211cdf6d3792368fe347e7e2d4a8ace14d9b248f39c5',
|
|
18445
|
-
* chainId: '0x534e5f5345504f4c4941',
|
|
18446
|
-
* version: '0x3', nonce: 0n
|
|
18447
|
-
*}
|
|
18448
|
-
* const res = await myLedgerSigner.signDeployAccountV3(deployData);
|
|
18449
|
-
* // res = {hash:
|
|
18450
|
-
* // signature:
|
|
18451
|
-
* // }
|
|
18751
|
+
* const result = myLedgerSigner.signDeployAccountTransaction(details);
|
|
18752
|
+
* // result = Signature { r: 611475243393396148729326917410546146405234155928298353899191529090923298688n,
|
|
18753
|
+
* // s: 798839819213540985856952481651392652149797817551686626114697493101433761982n,
|
|
18754
|
+
* // recovery: 0}
|
|
18452
18755
|
* ```
|
|
18453
18756
|
*/
|
|
18454
|
-
async
|
|
18455
|
-
|
|
18456
|
-
const
|
|
18457
|
-
|
|
18458
|
-
|
|
18459
|
-
|
|
18460
|
-
|
|
18461
|
-
|
|
18462
|
-
|
|
18463
|
-
|
|
18464
|
-
|
|
18465
|
-
|
|
18466
|
-
|
|
18467
|
-
|
|
18468
|
-
|
|
18469
|
-
|
|
18470
|
-
|
|
18471
|
-
|
|
18472
|
-
|
|
18473
|
-
|
|
18474
|
-
|
|
18475
|
-
|
|
18476
|
-
|
|
18477
|
-
|
|
18478
|
-
|
|
18479
|
-
const
|
|
18480
|
-
|
|
18481
|
-
|
|
18482
|
-
|
|
18483
|
-
|
|
18757
|
+
async signDeployAccountTransaction(details) {
|
|
18758
|
+
const compiledConstructorCalldata = CallData.compile(details.constructorCalldata);
|
|
18759
|
+
const msgHash = calculateDeployAccountTransactionHash2({
|
|
18760
|
+
...details,
|
|
18761
|
+
salt: details.addressSalt,
|
|
18762
|
+
compiledConstructorCalldata,
|
|
18763
|
+
version: details.version,
|
|
18764
|
+
nonceDataAvailabilityMode: intDAM(details.nonceDataAvailabilityMode),
|
|
18765
|
+
feeDataAvailabilityMode: intDAM(details.feeDataAvailabilityMode)
|
|
18766
|
+
});
|
|
18767
|
+
return this.signRaw(msgHash);
|
|
18768
|
+
}
|
|
18769
|
+
/**
|
|
18770
|
+
* Sign in a Ledger the declaration of a new class. This is a blind sign on the Ledger screen.
|
|
18771
|
+
* @param {DeclareSignerDetails} details An object that includes all necessary data to calculate the Hash.
|
|
18772
|
+
* @returns {Signature} The declare Signature.
|
|
18773
|
+
* @example
|
|
18774
|
+
* ```typescript
|
|
18775
|
+
* const result = myLedgerSigner.signDeclareTransaction(details);
|
|
18776
|
+
* // result = Signature { r: 611475243393396148729326917410546146405234155928298353899191529090923298688n,
|
|
18777
|
+
* // s: 798839819213540985856952481651392652149797817551686626114697493101433761982n,
|
|
18778
|
+
* // recovery: 0}
|
|
18779
|
+
* ```
|
|
18780
|
+
*/
|
|
18781
|
+
async signDeclareTransaction(details) {
|
|
18782
|
+
const msgHash = calculateDeclareTransactionHash2({
|
|
18783
|
+
...details,
|
|
18784
|
+
version: details.version,
|
|
18785
|
+
nonceDataAvailabilityMode: intDAM(details.nonceDataAvailabilityMode),
|
|
18786
|
+
feeDataAvailabilityMode: intDAM(details.feeDataAvailabilityMode)
|
|
18787
|
+
});
|
|
18788
|
+
return this.signRaw(msgHash);
|
|
18789
|
+
}
|
|
18790
|
+
/**
|
|
18791
|
+
* Internal function to sign a hash in a Ledger Nano.
|
|
18792
|
+
* This is a blind sign in the Ledger ; no display of what you are signing.
|
|
18793
|
+
*/
|
|
18794
|
+
async signRaw(msgHash) {
|
|
18795
|
+
addHexPrefix(
|
|
18796
|
+
buf2hex(await this._transporter.send(Number("0x5a"), 2, 0, 0, buffer_default.from(this.pathBuffer)))
|
|
18484
18797
|
);
|
|
18485
|
-
const
|
|
18486
|
-
|
|
18487
|
-
const
|
|
18488
|
-
|
|
18489
|
-
const a = this.convertBnToLedger(value);
|
|
18490
|
-
return a;
|
|
18491
|
-
})
|
|
18798
|
+
const shiftedHash = toHex(BigInt(msgHash) << 4n);
|
|
18799
|
+
const buff2 = hexToBytes(shiftedHash);
|
|
18800
|
+
const respSign2 = Uint8Array.from(
|
|
18801
|
+
await this._transporter.send(Number("0x5a"), 2, 1, 0, buffer_default.from(buff2))
|
|
18492
18802
|
);
|
|
18493
|
-
|
|
18494
|
-
const
|
|
18495
|
-
const
|
|
18496
|
-
|
|
18497
|
-
const
|
|
18498
|
-
|
|
18499
|
-
|
|
18500
|
-
|
|
18501
|
-
|
|
18803
|
+
const r = BigInt(addHexPrefix(buf2hex(respSign2.subarray(1, 33))));
|
|
18804
|
+
const s = BigInt(addHexPrefix(buf2hex(respSign2.subarray(33, 65))));
|
|
18805
|
+
const v = respSign2[65];
|
|
18806
|
+
const sign0 = new starkCurve.Signature(r, s);
|
|
18807
|
+
const sign1 = sign0.addRecoveryBit(v);
|
|
18808
|
+
return sign1;
|
|
18809
|
+
}
|
|
18810
|
+
/** internal function to get both the Starknet public key and the full public key */
|
|
18811
|
+
async getPublicKeys() {
|
|
18812
|
+
const pathBuff = this.pathBuffer;
|
|
18813
|
+
const respGetPublic = Uint8Array.from(
|
|
18814
|
+
await this._transporter.send(Number("0x5a"), 1, 0, 0, buffer_default.from(pathBuff))
|
|
18502
18815
|
);
|
|
18503
|
-
|
|
18504
|
-
|
|
18505
|
-
for (let i = 0; i < constructorBuf.length; i += chunkSize)
|
|
18506
|
-
constructorChunks.push(constructorBuf.subarray(i, i + chunkSize));
|
|
18507
|
-
let respSign = new Uint8Array(0);
|
|
18508
|
-
for (const chunk of constructorChunks) {
|
|
18509
|
-
respSign = await this._transporter.send(Number("0x5a"), 5, 5, 0, buffer_default.from(chunk));
|
|
18510
|
-
}
|
|
18511
|
-
return this.decodeSignatureLedger(respSign);
|
|
18816
|
+
this.pubKey = addHexPrefix(buf2hex(respGetPublic.subarray(1, 33)));
|
|
18817
|
+
this.fullPubKey = addHexPrefix(buf2hex(respGetPublic.subarray(0, 65)));
|
|
18512
18818
|
}
|
|
18513
18819
|
};
|
|
18514
|
-
function
|
|
18515
|
-
const path0buff = new Uint8Array([
|
|
18516
|
-
const path1buff = new Uint8Array([71
|
|
18517
|
-
const
|
|
18518
|
-
const
|
|
18519
|
-
|
|
18520
|
-
path2Base.subarray(1)
|
|
18521
|
-
]);
|
|
18522
|
-
const path3buff = new Uint8Array([HARDENING_BYTE, 0, 0, 0]);
|
|
18523
|
-
const hex = toHex(BigInt(accountId) | HARDENING_4BYTES);
|
|
18820
|
+
function getLedgerPathBuffer111(accountId, applicationName = "LedgerW") {
|
|
18821
|
+
const path0buff = new Uint8Array([128, 0, 10, 85]);
|
|
18822
|
+
const path1buff = new Uint8Array([71, 65, 233, 201]);
|
|
18823
|
+
const path2buff = applicationName === "LedgerW" ? new Uint8Array([43, 206, 231, 219]) : stringToSha256ToArrayBuff4(applicationName);
|
|
18824
|
+
const path3buff = new Uint8Array([0, 0, 0, 0]);
|
|
18825
|
+
const hex = toHex(accountId);
|
|
18524
18826
|
const padded = addHexPrefix(removeHexPrefix(hex).padStart(8, "0"));
|
|
18525
18827
|
const path4buff = hexToBytes(padded);
|
|
18526
18828
|
const path5buff = new Uint8Array([0, 0, 0, 0]);
|
|
@@ -18535,224 +18837,171 @@ function getLedgerPathBuffer221(accountId, applicationName = "LedgerW") {
|
|
|
18535
18837
|
return pathBuff;
|
|
18536
18838
|
}
|
|
18537
18839
|
|
|
18538
|
-
// src/
|
|
18539
|
-
var
|
|
18840
|
+
// src/utils/address.ts
|
|
18841
|
+
var import_utils7 = require("@noble/curves/utils.js");
|
|
18842
|
+
function addAddressPadding(address) {
|
|
18843
|
+
const hex = toHex(isString(address) ? addHexPrefix(address) : address);
|
|
18844
|
+
const padded = removeHexPrefix(hex).padStart(64, "0");
|
|
18845
|
+
return addHexPrefix(padded);
|
|
18846
|
+
}
|
|
18847
|
+
function validateAndParseAddress(address) {
|
|
18848
|
+
if (isString(address) && !/^(0x)?[0-9a-f]+$/i.test(address)) {
|
|
18849
|
+
throw new Error("Invalid Address Format");
|
|
18850
|
+
}
|
|
18851
|
+
const result = addAddressPadding(address);
|
|
18852
|
+
if (!result.match(/^(0x)?[0-9a-fA-F]{64}$/)) {
|
|
18853
|
+
throw new Error("Invalid Address Format");
|
|
18854
|
+
}
|
|
18855
|
+
assertInRange(result, ZERO, ADDR_BOUND - 1n, "Starknet Address");
|
|
18856
|
+
return result;
|
|
18857
|
+
}
|
|
18858
|
+
function getChecksumAddress(address) {
|
|
18859
|
+
const chars = removeHexPrefix(validateAndParseAddress(address)).toLowerCase().split("");
|
|
18860
|
+
const hex = removeHexPrefix(keccakBn(address));
|
|
18861
|
+
const hashed = (0, import_utils7.hexToBytes)(hex.padStart(64, "0"));
|
|
18862
|
+
for (let i = 0; i < chars.length; i += 2) {
|
|
18863
|
+
if (hashed[i >> 1] >> 4 >= 8) {
|
|
18864
|
+
chars[i] = chars[i].toUpperCase();
|
|
18865
|
+
}
|
|
18866
|
+
if ((hashed[i >> 1] & 15) >= 8) {
|
|
18867
|
+
chars[i + 1] = chars[i + 1].toUpperCase();
|
|
18868
|
+
}
|
|
18869
|
+
}
|
|
18870
|
+
return addHexPrefix(chars.join(""));
|
|
18871
|
+
}
|
|
18872
|
+
function validateChecksumAddress(address) {
|
|
18873
|
+
return getChecksumAddress(address) === address;
|
|
18874
|
+
}
|
|
18875
|
+
|
|
18876
|
+
// src/signer/ledgerSigner221.ts
|
|
18877
|
+
var LedgerSigner221 = class extends LedgerSigner111 {
|
|
18540
18878
|
/**
|
|
18541
18879
|
* constructor of the LedgerSigner class.
|
|
18542
18880
|
* @param {Transport} transport 5 transports are available to handle USB, bluetooth, Node, Web, Mobile.
|
|
18543
18881
|
* See Guides for more details.
|
|
18544
|
-
* @param {number} accountID ID of Ledger Nano
|
|
18882
|
+
* @param {number} accountID ID of Ledger Nano (can handle 2**31 accounts).
|
|
18545
18883
|
* @param {string} [eip2645application='LedgerW'] A wallet is defined by an ERC2645 derivation path (6 items).
|
|
18546
18884
|
* One item is called `application` and can be customized.
|
|
18547
18885
|
* Default value is `LedgerW`.
|
|
18548
18886
|
* @param {LedgerPathCalculation} [pathFunction=getLedgerPathBuffer221]
|
|
18549
18887
|
* defines the function that will calculate the path. By default `getLedgerPathBuffer221` is selected.
|
|
18550
18888
|
*
|
|
18551
|
-
* If you are using APP v2.
|
|
18889
|
+
* If you are using APP v2.2.1 with an account created with the v1.1.1, you need to use :
|
|
18552
18890
|
* ```typescript
|
|
18553
|
-
* const myLedgerSigner = new
|
|
18891
|
+
* const myLedgerSigner = new LedgerSigner221(myNodeTransport, 0, undefined, getLedgerPathBuffer111);
|
|
18554
18892
|
* ```
|
|
18555
18893
|
* @example
|
|
18556
18894
|
* ```typescript
|
|
18557
18895
|
* import TransportNodeHid from "@ledgerhq/hw-transport-node-hid";
|
|
18558
18896
|
* const myNodeTransport = await TransportNodeHid.create();
|
|
18559
|
-
* const myLedgerSigner = new
|
|
18897
|
+
* const myLedgerSigner = new LedgerSigner221(myNodeTransport, 0);
|
|
18898
|
+
* ```
|
|
18899
|
+
*/
|
|
18900
|
+
constructor(transport, accountID, eip2645application = "LedgerW", pathFunction = getLedgerPathBuffer221) {
|
|
18901
|
+
super(transport, accountID, eip2645application, pathFunction);
|
|
18902
|
+
}
|
|
18903
|
+
/**
|
|
18904
|
+
* Sign in a Ledger a V3 transaction. The details are displayed on the Ledger screen.
|
|
18905
|
+
* @param {Call[]} transactions An array of `Call` transactions (generated for example by `myContract.populate()`).
|
|
18906
|
+
* @param {InvocationsSignerDetails} transactionsDetail An object that includes all the necessary inputs to hash the transaction.
|
|
18907
|
+
* @returns {Signature} The signed transaction.
|
|
18908
|
+
* @example
|
|
18909
|
+
* ```typescript
|
|
18910
|
+
* const txDetailsV3: V3InvocationsSignerDetails = {
|
|
18911
|
+
* chainId: constants.StarknetChainId.SN_MAIN,
|
|
18912
|
+
* nonce: "28",
|
|
18913
|
+
* accountDeploymentData: [],
|
|
18914
|
+
* paymasterData: [],
|
|
18915
|
+
* cairoVersion: "1",
|
|
18916
|
+
* feeDataAvailabilityMode: "L1",
|
|
18917
|
+
* nonceDataAvailabilityMode: "L1",
|
|
18918
|
+
* resourceBounds: {
|
|
18919
|
+
* l1_gas: {
|
|
18920
|
+
* max_amount: "0x2a00",
|
|
18921
|
+
* max_price_per_unit: "0x5c00000"
|
|
18922
|
+
* },
|
|
18923
|
+
* l2_gas: {
|
|
18924
|
+
* max_amount: "0x00",
|
|
18925
|
+
* max_price_per_unit: "0x00"
|
|
18926
|
+
* },
|
|
18927
|
+
* },
|
|
18928
|
+
* tip: 0,
|
|
18929
|
+
* version: "0x3",
|
|
18930
|
+
* walletAddress: account0.address
|
|
18931
|
+
* }
|
|
18932
|
+
* const result = myLedgerSigner.signTransaction([call0, call1], txDetailsV3);
|
|
18933
|
+
* // result = Signature { r: 611475243393396148729326917410546146405234155928298353899191529090923298688n,
|
|
18934
|
+
* // s: 798839819213540985856952481651392652149797817551686626114697493101433761982n,
|
|
18935
|
+
* // recovery: 0}
|
|
18560
18936
|
* ```
|
|
18561
18937
|
*/
|
|
18562
|
-
|
|
18563
|
-
|
|
18938
|
+
async signTransaction(transactions, transactionsDetail) {
|
|
18939
|
+
const compiledCalldata = getExecuteCalldata(transactions, transactionsDetail.cairoVersion);
|
|
18940
|
+
const msgHash = calculateInvokeTransactionHash2({
|
|
18941
|
+
...transactionsDetail,
|
|
18942
|
+
senderAddress: transactionsDetail.walletAddress,
|
|
18943
|
+
compiledCalldata,
|
|
18944
|
+
version: transactionsDetail.version,
|
|
18945
|
+
nonceDataAvailabilityMode: intDAM(transactionsDetail.nonceDataAvailabilityMode),
|
|
18946
|
+
feeDataAvailabilityMode: intDAM(transactionsDetail.feeDataAvailabilityMode)
|
|
18947
|
+
});
|
|
18948
|
+
const ledgerResponse = await this.signTxV3(transactionsDetail, transactions);
|
|
18949
|
+
assert(
|
|
18950
|
+
toBigInt(msgHash) === ledgerResponse.hash,
|
|
18951
|
+
"The V3 transaction hash calculated by Starknet.js is different from the one calculated by the Ledger."
|
|
18952
|
+
);
|
|
18953
|
+
return ledgerResponse.signature;
|
|
18564
18954
|
}
|
|
18565
18955
|
/**
|
|
18566
|
-
*
|
|
18567
|
-
* @param {
|
|
18568
|
-
* @
|
|
18569
|
-
* @returns an object including the transaction Hash and the signature
|
|
18956
|
+
* Sign in a Ledger the deployment of a new account. The details are displayed on the Ledger screen.
|
|
18957
|
+
* @param {DeployAccountSignerDetails} details An object that includes all necessary data to calculate the Hash.
|
|
18958
|
+
* @returns {Signature} The deploy account signature.
|
|
18570
18959
|
* @example
|
|
18571
18960
|
* ```typescript
|
|
18572
|
-
* const
|
|
18573
|
-
*
|
|
18574
|
-
*
|
|
18575
|
-
*
|
|
18576
|
-
* const txDetailsV3: V3InvocationsSignerDetails = {
|
|
18577
|
-
* chainId: constants.StarknetChainId.SN_MAIN,
|
|
18578
|
-
* nonce: "28", accountDeploymentData: [],
|
|
18579
|
-
* paymasterData: [], cairoVersion: "1",
|
|
18580
|
-
* feeDataAvailabilityMode: "L1", nonceDataAvailabilityMode: "L1",
|
|
18581
|
-
* resourceBounds: {
|
|
18582
|
-
* l1_gas: { max_amount: "0x2a00", max_price_per_unit: "0x5c00000"
|
|
18583
|
-
* },
|
|
18584
|
-
* l2_gas: { max_amount: "0x00", max_price_per_unit: "0x00"},
|
|
18585
|
-
* }, tip: 0, version: "0x3", walletAddress: account0.address
|
|
18586
|
-
* };
|
|
18587
|
-
* const res = await myLedgerSigner.signTxV3(txDetailsV3, calls);
|
|
18588
|
-
* // res = {hash:
|
|
18589
|
-
* // signature:
|
|
18590
|
-
* // }
|
|
18961
|
+
* const result = myLedgerSigner.signDeployAccountTransaction(details);
|
|
18962
|
+
* // result = Signature { r: 611475243393396148729326917410546146405234155928298353899191529090923298688n,
|
|
18963
|
+
* // s: 798839819213540985856952481651392652149797817551686626114697493101433761982n,
|
|
18964
|
+
* // recovery: 0}
|
|
18591
18965
|
* ```
|
|
18592
18966
|
*/
|
|
18593
|
-
async
|
|
18594
|
-
|
|
18967
|
+
async signDeployAccountTransaction(details) {
|
|
18968
|
+
const compiledConstructorCalldata = CallData.compile(details.constructorCalldata);
|
|
18969
|
+
const msgHash = calculateDeployAccountTransactionHash2({
|
|
18970
|
+
...details,
|
|
18971
|
+
salt: details.addressSalt,
|
|
18972
|
+
compiledConstructorCalldata,
|
|
18973
|
+
version: details.version,
|
|
18974
|
+
nonceDataAvailabilityMode: intDAM(details.nonceDataAvailabilityMode),
|
|
18975
|
+
feeDataAvailabilityMode: intDAM(details.feeDataAvailabilityMode)
|
|
18976
|
+
});
|
|
18977
|
+
const ledgerResponse = await this.signDeployAccountV3(details);
|
|
18595
18978
|
assert(
|
|
18596
|
-
|
|
18597
|
-
"
|
|
18598
|
-
);
|
|
18599
|
-
await this._transporter.send(Number("0x5a"), 3, 0, 0, buffer_default.from(this.pathBuffer));
|
|
18600
|
-
const accountAddressBuf = this.convertBnToLedger(txDetails.walletAddress);
|
|
18601
|
-
const chainIdBuf = this.convertBnToLedger(txDetails.chainId);
|
|
18602
|
-
const nonceBuf = this.convertBnToLedger(txDetails.nonce);
|
|
18603
|
-
const dAModeHashBuf = this.convertBnToLedger(
|
|
18604
|
-
hashDAMode(
|
|
18605
|
-
intDAM(txDetails.nonceDataAvailabilityMode),
|
|
18606
|
-
intDAM(txDetails.feeDataAvailabilityMode)
|
|
18607
|
-
)
|
|
18608
|
-
);
|
|
18609
|
-
const dataBuf = concatenateArrayBuffer([
|
|
18610
|
-
accountAddressBuf,
|
|
18611
|
-
chainIdBuf,
|
|
18612
|
-
nonceBuf,
|
|
18613
|
-
dAModeHashBuf
|
|
18614
|
-
]);
|
|
18615
|
-
await this._transporter.send(Number("0x5a"), 3, 1, 0, buffer_default.from(dataBuf));
|
|
18616
|
-
if (isRPC08Plus_ResourceBoundsBN(txDetails.resourceBounds)) {
|
|
18617
|
-
const tipBuf = this.convertBnToLedger(txDetails.tip);
|
|
18618
|
-
const l1_gasBuf = this.convertBnToLedger(encodeResourceBoundsL1(txDetails.resourceBounds));
|
|
18619
|
-
const l2_gasBuf = this.convertBnToLedger(encodeResourceBoundsL2(txDetails.resourceBounds));
|
|
18620
|
-
const l1_data_gasBuf = this.convertBnToLedger(
|
|
18621
|
-
encodeDataResourceBoundsL1(txDetails.resourceBounds)
|
|
18622
|
-
);
|
|
18623
|
-
const feeBuf = concatenateArrayBuffer([
|
|
18624
|
-
tipBuf,
|
|
18625
|
-
l1_gasBuf,
|
|
18626
|
-
l2_gasBuf,
|
|
18627
|
-
l1_data_gasBuf
|
|
18628
|
-
]);
|
|
18629
|
-
await this._transporter.send(Number("0x5a"), 3, 2, 0, buffer_default.from(feeBuf));
|
|
18630
|
-
}
|
|
18631
|
-
const paymasterBuf = concatenateArrayBuffer(
|
|
18632
|
-
txDetails.paymasterData.map((value) => {
|
|
18633
|
-
const a = this.convertBnToLedger(value);
|
|
18634
|
-
return a;
|
|
18635
|
-
})
|
|
18636
|
-
);
|
|
18637
|
-
await this._transporter.send(Number("0x5a"), 3, 3, 0, buffer_default.from(paymasterBuf));
|
|
18638
|
-
const accountDeployDataBuf = concatenateArrayBuffer(
|
|
18639
|
-
txDetails.paymasterData.map((value) => {
|
|
18640
|
-
const a = this.convertBnToLedger(value);
|
|
18641
|
-
return a;
|
|
18642
|
-
})
|
|
18979
|
+
toBigInt(msgHash) === ledgerResponse.hash,
|
|
18980
|
+
"The transaction hash calculated by Starknet.js is different from the one calculated by the Ledger."
|
|
18643
18981
|
);
|
|
18644
|
-
|
|
18645
|
-
const nbCallsBuf = this.convertBnToLedger(calls.length);
|
|
18646
|
-
await this._transporter.send(Number("0x5a"), 3, 5, 0, buffer_default.from(nbCallsBuf));
|
|
18647
|
-
let respSign = new Uint8Array(0);
|
|
18648
|
-
for (const call of calls) {
|
|
18649
|
-
const calldatas = this.encodeCall(call);
|
|
18650
|
-
respSign = await this._transporter.send(Number("0x5a"), 3, 6, 0, buffer_default.from(calldatas[0]));
|
|
18651
|
-
if (calldatas.length > 1) {
|
|
18652
|
-
calldatas.slice(1).forEach(async (part) => {
|
|
18653
|
-
respSign = await this._transporter.send(Number("0x5a"), 3, 6, 1, buffer_default.from(part));
|
|
18654
|
-
});
|
|
18655
|
-
}
|
|
18656
|
-
}
|
|
18657
|
-
return this.decodeSignatureLedger(respSign);
|
|
18982
|
+
return ledgerResponse.signature;
|
|
18658
18983
|
}
|
|
18659
18984
|
/**
|
|
18660
|
-
*
|
|
18661
|
-
* @param {
|
|
18662
|
-
* @returns
|
|
18663
|
-
* @example
|
|
18664
|
-
* ```typescript
|
|
18665
|
-
* const deployData: V3DeployAccountSignerDetails =
|
|
18666
|
-
* {
|
|
18667
|
-
* tip: 0, paymasterData: [], accountDeploymentData: [],
|
|
18668
|
-
* nonceDataAvailabilityMode: 'L1', feeDataAvailabilityMode: 'L1',
|
|
18669
|
-
* resourceBounds: {
|
|
18670
|
-
* l2_gas: { max_amount: '0x0', max_price_per_unit: '0x0' },
|
|
18671
|
-
* l1_gas: { max_amount: '0x226', max_price_per_unit: '0x22ecb25c00' }
|
|
18672
|
-
* },
|
|
18673
|
-
* classHash: '0x540d7f5ec7ecf317e68d48564934cb99259781b1ee3cedbbc37ec5337f8e688',
|
|
18674
|
-
* constructorCalldata: [
|
|
18675
|
-
* '3571125127744830445572285574469842579401255431821644822726857471463672199621'
|
|
18676
|
-
* ],
|
|
18677
|
-
* contractAddress: '0x4ca062add1cf12a107be1107af17981cf6e544a24d987693230ea481d3d5e34',
|
|
18678
|
-
* addressSalt: '0x07e52f68e3160e1ef698211cdf6d3792368fe347e7e2d4a8ace14d9b248f39c5',
|
|
18679
|
-
* chainId: '0x534e5f5345504f4c4941',
|
|
18680
|
-
* version: '0x3', nonce: 0n
|
|
18681
|
-
*}
|
|
18682
|
-
* const res = await myLedgerSigner.signDeployAccountV3(deployData);
|
|
18683
|
-
* // res = {hash:
|
|
18684
|
-
* // signature:
|
|
18685
|
-
* // }
|
|
18686
|
-
* ```
|
|
18985
|
+
* Internal function to convert a bigNumberish to an Uint8array of 256 bits
|
|
18986
|
+
* @param {BigNumberish} input input value
|
|
18987
|
+
* @returns {Uint8Array} a Uint8Array containing 32 bytes.
|
|
18687
18988
|
*/
|
|
18688
|
-
|
|
18689
|
-
|
|
18690
|
-
|
|
18691
|
-
|
|
18692
|
-
|
|
18693
|
-
|
|
18694
|
-
|
|
18695
|
-
|
|
18696
|
-
|
|
18697
|
-
|
|
18698
|
-
|
|
18699
|
-
|
|
18700
|
-
|
|
18701
|
-
const
|
|
18702
|
-
const
|
|
18703
|
-
|
|
18704
|
-
accountAddressBuf,
|
|
18705
|
-
chainIdBuf,
|
|
18706
|
-
nonceBuf,
|
|
18707
|
-
dAModeHashBuf,
|
|
18708
|
-
classHashBuf,
|
|
18709
|
-
saltBuf
|
|
18710
|
-
]);
|
|
18711
|
-
await this._transporter.send(Number("0x5a"), 5, 1, 0, buffer_default.from(dataBuf));
|
|
18712
|
-
if (isRPC08Plus_ResourceBoundsBN(deployAccountDetail.resourceBounds)) {
|
|
18713
|
-
const tipBuf = this.convertBnToLedger(deployAccountDetail.tip);
|
|
18714
|
-
const l1_gasBuf = this.convertBnToLedger(
|
|
18715
|
-
encodeResourceBoundsL1(deployAccountDetail.resourceBounds)
|
|
18716
|
-
);
|
|
18717
|
-
const l2_gasBuf = this.convertBnToLedger(
|
|
18718
|
-
encodeResourceBoundsL2(deployAccountDetail.resourceBounds)
|
|
18719
|
-
);
|
|
18720
|
-
const l1_data_gasBuf = this.convertBnToLedger(
|
|
18721
|
-
encodeDataResourceBoundsL1(deployAccountDetail.resourceBounds)
|
|
18722
|
-
);
|
|
18723
|
-
const feeBuf = concatenateArrayBuffer([
|
|
18724
|
-
tipBuf,
|
|
18725
|
-
l1_gasBuf,
|
|
18726
|
-
l2_gasBuf,
|
|
18727
|
-
l1_data_gasBuf
|
|
18728
|
-
]);
|
|
18729
|
-
await this._transporter.send(Number("0x5a"), 5, 2, 0, buffer_default.from(feeBuf));
|
|
18730
|
-
}
|
|
18731
|
-
const paymasterBuf = concatenateArrayBuffer(
|
|
18732
|
-
deployAccountDetail.paymasterData.map((value) => {
|
|
18733
|
-
const a = this.convertBnToLedger(value);
|
|
18734
|
-
return a;
|
|
18735
|
-
})
|
|
18736
|
-
);
|
|
18737
|
-
await this._transporter.send(Number("0x5a"), 5, 3, 0, buffer_default.from(paymasterBuf));
|
|
18738
|
-
const compiledConstructor = CallData.compile(deployAccountDetail.constructorCalldata);
|
|
18739
|
-
const constructorLengthBuf = this.convertBnToLedger(compiledConstructor.length);
|
|
18740
|
-
await this._transporter.send(Number("0x5a"), 5, 4, 0, buffer_default.from(constructorLengthBuf));
|
|
18741
|
-
const constructorBuf = concatenateArrayBuffer(
|
|
18742
|
-
compiledConstructor.map((parameter) => {
|
|
18743
|
-
const a = this.convertBnToLedger(parameter);
|
|
18744
|
-
return a;
|
|
18745
|
-
})
|
|
18746
|
-
);
|
|
18747
|
-
const constructorChunks = [];
|
|
18748
|
-
const chunkSize = 7 * 32;
|
|
18749
|
-
for (let i = 0; i < constructorBuf.length; i += chunkSize)
|
|
18750
|
-
constructorChunks.push(constructorBuf.subarray(i, i + chunkSize));
|
|
18751
|
-
let respSign = new Uint8Array(0);
|
|
18752
|
-
for (const chunk of constructorChunks) {
|
|
18753
|
-
respSign = await this._transporter.send(Number("0x5a"), 5, 5, 0, buffer_default.from(chunk));
|
|
18754
|
-
}
|
|
18755
|
-
return this.decodeSignatureLedger(respSign);
|
|
18989
|
+
convertBnToLedger(input) {
|
|
18990
|
+
return hexToBytes(addAddressPadding(toHex(input)));
|
|
18991
|
+
}
|
|
18992
|
+
/**
|
|
18993
|
+
* Internal function to decode the response of the Ledger signature
|
|
18994
|
+
* @param {Uint8Array} respSign the Buffer response of the Ledger
|
|
18995
|
+
* @returns { hash: bigint; signature: Signature } transaction hash & signature
|
|
18996
|
+
*/
|
|
18997
|
+
decodeSignatureLedger(respSign) {
|
|
18998
|
+
const h = BigInt(addHexPrefix(buf2hex(respSign.subarray(0, 32))));
|
|
18999
|
+
const r = BigInt(addHexPrefix(buf2hex(respSign.subarray(33, 65))));
|
|
19000
|
+
const s = BigInt(addHexPrefix(buf2hex(respSign.subarray(65, 97))));
|
|
19001
|
+
const v = respSign[97];
|
|
19002
|
+
const sign0 = new starkCurve.Signature(r, s);
|
|
19003
|
+
const sign1 = sign0.addRecoveryBit(v);
|
|
19004
|
+
return { hash: h, signature: sign1 };
|
|
18756
19005
|
}
|
|
18757
19006
|
/** Internal function to convert a Call to an array of Uint8Array.
|
|
18758
19007
|
* @param {Call} call A Call to convert.
|
|
@@ -18761,19 +19010,15 @@ var LedgerSigner231 = class extends LedgerSigner221 {
|
|
|
18761
19010
|
encodeCall(call) {
|
|
18762
19011
|
const toBuf = this.convertBnToLedger(call.contractAddress);
|
|
18763
19012
|
const selectorBuf = hexToBytes(addAddressPadding(getSelector(call.entrypoint)));
|
|
18764
|
-
let calldataBuf;
|
|
19013
|
+
let calldataBuf = new Uint8Array([]);
|
|
18765
19014
|
if (call.calldata) {
|
|
18766
19015
|
const compiledCalldata = CallData.compile(call.calldata);
|
|
18767
|
-
|
|
18768
|
-
|
|
18769
|
-
calldataSizeBuf,
|
|
18770
|
-
...compiledCalldata.map((parameter) => {
|
|
19016
|
+
calldataBuf = concatenateArrayBuffer(
|
|
19017
|
+
compiledCalldata.map((parameter) => {
|
|
18771
19018
|
const a = this.convertBnToLedger(parameter);
|
|
18772
19019
|
return a;
|
|
18773
19020
|
})
|
|
18774
|
-
|
|
18775
|
-
} else {
|
|
18776
|
-
calldataBuf = this.convertBnToLedger("0x00");
|
|
19021
|
+
);
|
|
18777
19022
|
}
|
|
18778
19023
|
const callBuf = concatenateArrayBuffer([toBuf, selectorBuf, calldataBuf]);
|
|
18779
19024
|
const calldatas = [];
|
|
@@ -18782,522 +19027,472 @@ var LedgerSigner231 = class extends LedgerSigner221 {
|
|
|
18782
19027
|
calldatas.push(callBuf.subarray(i, i + chunkSize));
|
|
18783
19028
|
return calldatas;
|
|
18784
19029
|
}
|
|
18785
|
-
|
|
18786
|
-
|
|
18787
|
-
|
|
18788
|
-
|
|
18789
|
-
|
|
18790
|
-
|
|
18791
|
-
|
|
18792
|
-
|
|
18793
|
-
|
|
18794
|
-
|
|
18795
|
-
}
|
|
18796
|
-
|
|
18797
|
-
|
|
18798
|
-
|
|
18799
|
-
|
|
18800
|
-
|
|
18801
|
-
|
|
18802
|
-
|
|
18803
|
-
|
|
18804
|
-
|
|
18805
|
-
}
|
|
18806
|
-
|
|
18807
|
-
|
|
18808
|
-
|
|
18809
|
-
|
|
18810
|
-
|
|
18811
|
-
|
|
18812
|
-
|
|
18813
|
-
|
|
18814
|
-
|
|
18815
|
-
function callToTypedData(call, version) {
|
|
18816
|
-
const outsideCall = getOutsideCall(call);
|
|
18817
|
-
if (version === "1") {
|
|
18818
|
-
return {
|
|
18819
|
-
...outsideCall,
|
|
18820
|
-
calldata_len: outsideCall.calldata.length,
|
|
18821
|
-
calldata: outsideCall.calldata
|
|
18822
|
-
};
|
|
18823
|
-
}
|
|
18824
|
-
return {
|
|
18825
|
-
To: outsideCall.to,
|
|
18826
|
-
Selector: outsideCall.selector,
|
|
18827
|
-
Calldata: outsideCall.calldata
|
|
18828
|
-
};
|
|
18829
|
-
}
|
|
18830
|
-
function getDomain(chainId, version) {
|
|
18831
|
-
return {
|
|
18832
|
-
name: "Account.execute_from_outside",
|
|
18833
|
-
version,
|
|
18834
|
-
chainId,
|
|
18835
|
-
...version === "2" ? { revision: "1" } : {}
|
|
18836
|
-
};
|
|
18837
|
-
}
|
|
18838
|
-
function getTypedData(chainId, options, nonce, myCalls, version) {
|
|
18839
|
-
if (version === "1") {
|
|
18840
|
-
return {
|
|
18841
|
-
types: OutsideExecutionTypesV1,
|
|
18842
|
-
primaryType: "OutsideExecution",
|
|
18843
|
-
domain: getDomain(chainId, version),
|
|
18844
|
-
message: {
|
|
18845
|
-
...options,
|
|
18846
|
-
nonce,
|
|
18847
|
-
calls_len: myCalls.length,
|
|
18848
|
-
calls: myCalls.map((call) => callToTypedData(call, version))
|
|
18849
|
-
}
|
|
18850
|
-
};
|
|
18851
|
-
}
|
|
18852
|
-
return {
|
|
18853
|
-
types: OutsideExecutionTypesV2,
|
|
18854
|
-
primaryType: "OutsideExecution",
|
|
18855
|
-
domain: getDomain(chainId, version),
|
|
18856
|
-
message: {
|
|
18857
|
-
Caller: options.caller,
|
|
18858
|
-
Nonce: nonce,
|
|
18859
|
-
"Execute After": options.execute_after,
|
|
18860
|
-
"Execute Before": options.execute_before,
|
|
18861
|
-
Calls: myCalls.map((call) => callToTypedData(call, version))
|
|
18862
|
-
}
|
|
18863
|
-
};
|
|
18864
|
-
}
|
|
18865
|
-
function buildExecuteFromOutsideCallData(outsideTransaction) {
|
|
18866
|
-
const execution2 = outsideTransaction.outsideExecution;
|
|
18867
|
-
const formattedSignature = formatSignature(outsideTransaction.signature);
|
|
18868
|
-
return CallData.compile({
|
|
18869
|
-
outside_execution: execution2,
|
|
18870
|
-
signature: formattedSignature
|
|
18871
|
-
});
|
|
18872
|
-
}
|
|
18873
|
-
function buildExecuteFromOutsideCall(outsideTransaction) {
|
|
18874
|
-
const myOutsideTransactions = Array.isArray(outsideTransaction) ? outsideTransaction : [outsideTransaction];
|
|
18875
|
-
const multiCall = myOutsideTransactions.map((outsideTx) => {
|
|
18876
|
-
let entrypoint;
|
|
18877
|
-
if (outsideTx.version === OutsideExecutionVersion.V1) {
|
|
18878
|
-
entrypoint = "execute_from_outside";
|
|
18879
|
-
} else if (outsideTx.version === OutsideExecutionVersion.V2) {
|
|
18880
|
-
entrypoint = "execute_from_outside_v2";
|
|
18881
|
-
} else {
|
|
18882
|
-
throw new Error("Unsupported OutsideExecution version");
|
|
18883
|
-
}
|
|
18884
|
-
return {
|
|
18885
|
-
contractAddress: toHex(outsideTx.signerAddress),
|
|
18886
|
-
entrypoint,
|
|
18887
|
-
calldata: buildExecuteFromOutsideCallData(outsideTx)
|
|
18888
|
-
};
|
|
18889
|
-
});
|
|
18890
|
-
return multiCall;
|
|
18891
|
-
}
|
|
18892
|
-
|
|
18893
|
-
// src/utils/src5.ts
|
|
18894
|
-
var src5_exports = {};
|
|
18895
|
-
__export(src5_exports, {
|
|
18896
|
-
supportsInterface: () => supportsInterface
|
|
18897
|
-
});
|
|
18898
|
-
async function supportsInterface(provider, contractAddress, interfaceId) {
|
|
18899
|
-
const call = {
|
|
18900
|
-
contractAddress: toHex(contractAddress),
|
|
18901
|
-
entrypoint: "supports_interface",
|
|
18902
|
-
calldata: [toHex(interfaceId)]
|
|
18903
|
-
};
|
|
18904
|
-
try {
|
|
18905
|
-
const resp = await provider.callContract(call);
|
|
18906
|
-
return BigInt(resp[0]) !== 0n;
|
|
18907
|
-
} catch {
|
|
18908
|
-
return false;
|
|
18909
|
-
}
|
|
18910
|
-
}
|
|
18911
|
-
|
|
18912
|
-
// src/utils/paymaster.ts
|
|
18913
|
-
var paymaster_exports = {};
|
|
18914
|
-
__export(paymaster_exports, {
|
|
18915
|
-
assertCallsAreStrictlyEqual: () => assertCallsAreStrictlyEqual,
|
|
18916
|
-
assertPaymasterTransactionSafety: () => assertPaymasterTransactionSafety,
|
|
18917
|
-
getDefaultPaymasterNodeUrl: () => getDefaultPaymasterNodeUrl
|
|
18918
|
-
});
|
|
18919
|
-
var getDefaultPaymasterNodeUrl = (networkName, mute = false) => {
|
|
18920
|
-
if (!mute) {
|
|
18921
|
-
logger.info("Using default public node url, please provide nodeUrl in provider options!");
|
|
18922
|
-
}
|
|
18923
|
-
const nodes = PAYMASTER_RPC_NODES[networkName ?? _NetworkName.SN_SEPOLIA];
|
|
18924
|
-
const randIdx = Math.floor(Math.random() * nodes.length);
|
|
18925
|
-
return nodes[randIdx];
|
|
18926
|
-
};
|
|
18927
|
-
var assertGasFeeFromUnsafeCalls = (unsafeCalls, fees) => {
|
|
18928
|
-
const unsafeCall = toOutsideCallV2(unsafeCalls[unsafeCalls.length - 1]);
|
|
18929
|
-
const unsafeGasTokenCalldata = CallData.toCalldata(unsafeCall.Calldata);
|
|
18930
|
-
const unsafeGasTokenValue = uint256ToBN({
|
|
18931
|
-
low: unsafeGasTokenCalldata[1],
|
|
18932
|
-
high: unsafeGasTokenCalldata[2]
|
|
18933
|
-
});
|
|
18934
|
-
assert(
|
|
18935
|
-
unsafeGasTokenValue === BigInt(fees),
|
|
18936
|
-
"Gas token value is not equal to the provided gas fees"
|
|
18937
|
-
);
|
|
18938
|
-
};
|
|
18939
|
-
var assertGasTokenFromUnsafeCalls = (unsafeCalls, gasToken) => {
|
|
18940
|
-
const unsafeCall = toOutsideCallV2(unsafeCalls[unsafeCalls.length - 1]);
|
|
18941
|
-
assert(
|
|
18942
|
-
BigInt(unsafeCall.To) === BigInt(gasToken),
|
|
18943
|
-
"Gas token address is not equal to the provided gas token"
|
|
18944
|
-
);
|
|
18945
|
-
assert(
|
|
18946
|
-
unsafeCall.Selector === getSelectorFromName("transfer"),
|
|
18947
|
-
"Gas token call selector is not a transfer"
|
|
18948
|
-
);
|
|
18949
|
-
assert(
|
|
18950
|
-
CallData.toCalldata(unsafeCall.Calldata).length === 3,
|
|
18951
|
-
"Gas token transfer calldata does not match the expected recipient/amount shape"
|
|
18952
|
-
);
|
|
18953
|
-
};
|
|
18954
|
-
function assertCallsAreStrictlyEqual(originalCalls, unsafeCalls, isSponsored = false) {
|
|
18955
|
-
const baseError = "Provided calls are not strictly equal to the returned calls";
|
|
18956
|
-
const expectedExtraCalls = isSponsored ? 0 : 1;
|
|
18957
|
-
assert(
|
|
18958
|
-
unsafeCalls.length - expectedExtraCalls === originalCalls.length,
|
|
18959
|
-
`${baseError}: Expected ${originalCalls.length + expectedExtraCalls} calls, got ${unsafeCalls.length}`
|
|
18960
|
-
);
|
|
18961
|
-
for (let callIndex = 0; callIndex < originalCalls.length; callIndex += 1) {
|
|
18962
|
-
const originalCall = originalCalls[callIndex];
|
|
18963
|
-
const unsafeCall = toOutsideCallV2(unsafeCalls[callIndex]);
|
|
18964
|
-
const normalizeAddress = (address) => {
|
|
18965
|
-
return toBigInt(address).toString(16).toLowerCase();
|
|
18966
|
-
};
|
|
18967
|
-
const originalAddress = normalizeAddress(originalCall.contractAddress);
|
|
18968
|
-
const unsafeAddress = normalizeAddress(unsafeCall.To);
|
|
19030
|
+
/**
|
|
19031
|
+
* Ask to the Ledger Nano to display and sign a Starknet V3 transaction.
|
|
19032
|
+
* @param {V3InvocationsSignerDetails} txDetails All the details needed for a txV3.
|
|
19033
|
+
* @param {Call[]} calls array of Starknet invocations
|
|
19034
|
+
* @returns an object including the transaction Hash and the signature
|
|
19035
|
+
* @example
|
|
19036
|
+
* ```typescript
|
|
19037
|
+
* const calls: Call[] = [{contractAddress: "0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7",
|
|
19038
|
+
* entrypoint: "transfer",
|
|
19039
|
+
* calldata:["0x11f5fc2a92ac03434a7937fe982f5e5293b65ad438a989c5b78fb8f04a12016",
|
|
19040
|
+
* "0x9184e72a000", "0x0"]}];
|
|
19041
|
+
* const txDetailsV3: V3InvocationsSignerDetails = {
|
|
19042
|
+
* chainId: constants.StarknetChainId.SN_MAIN,
|
|
19043
|
+
* nonce: "28", accountDeploymentData: [],
|
|
19044
|
+
* paymasterData: [], cairoVersion: "1",
|
|
19045
|
+
* feeDataAvailabilityMode: "L1", nonceDataAvailabilityMode: "L1",
|
|
19046
|
+
* resourceBounds: {
|
|
19047
|
+
* l1_gas: { max_amount: "0x2a00", max_price_per_unit: "0x5c00000"
|
|
19048
|
+
* },
|
|
19049
|
+
* l2_gas: { max_amount: "0x00", max_price_per_unit: "0x00"},
|
|
19050
|
+
* }, tip: 0, version: "0x3", walletAddress: account0.address
|
|
19051
|
+
* };
|
|
19052
|
+
* const res = await myLedgerSigner.signTxV3(txDetailsV3, calls);
|
|
19053
|
+
* // res = {hash:
|
|
19054
|
+
* // signature:
|
|
19055
|
+
* // }
|
|
19056
|
+
* ```
|
|
19057
|
+
*/
|
|
19058
|
+
async signTxV3(txDetails, calls) {
|
|
19059
|
+
assert(txDetails.paymasterData.length <= 7, "Paymaster data includes more than 7 items.");
|
|
18969
19060
|
assert(
|
|
18970
|
-
|
|
18971
|
-
|
|
19061
|
+
txDetails.accountDeploymentData.length <= 7,
|
|
19062
|
+
"accountDeploymentData includes more than 7 items"
|
|
19063
|
+
);
|
|
19064
|
+
await this._transporter.send(Number("0x5a"), 3, 0, 0, buffer_default.from(this.pathBuffer));
|
|
19065
|
+
const accountAddressBuf = this.convertBnToLedger(txDetails.walletAddress);
|
|
19066
|
+
const tipBuf = this.convertBnToLedger(txDetails.tip);
|
|
19067
|
+
const chainIdBuf = this.convertBnToLedger(txDetails.chainId);
|
|
19068
|
+
const nonceBuf = this.convertBnToLedger(txDetails.nonce);
|
|
19069
|
+
const dAModeHashBuf = this.convertBnToLedger(
|
|
19070
|
+
hashDAMode(
|
|
19071
|
+
txDetails.nonceDataAvailabilityMode === api_exports.EDataAvailabilityMode.L1 ? api_exports.EDAMode.L1 : api_exports.EDAMode.L2,
|
|
19072
|
+
txDetails.feeDataAvailabilityMode === api_exports.EDataAvailabilityMode.L1 ? api_exports.EDAMode.L1 : api_exports.EDAMode.L2
|
|
19073
|
+
)
|
|
19074
|
+
);
|
|
19075
|
+
const l1_gasBuf = this.convertBnToLedger(encodeResourceBoundsL1(txDetails.resourceBounds));
|
|
19076
|
+
const l2_gasBuf = this.convertBnToLedger(encodeResourceBoundsL2(txDetails.resourceBounds));
|
|
19077
|
+
const dataBuf = concatenateArrayBuffer([
|
|
19078
|
+
accountAddressBuf,
|
|
19079
|
+
tipBuf,
|
|
19080
|
+
l1_gasBuf,
|
|
19081
|
+
l2_gasBuf,
|
|
19082
|
+
chainIdBuf,
|
|
19083
|
+
nonceBuf,
|
|
19084
|
+
dAModeHashBuf
|
|
19085
|
+
]);
|
|
19086
|
+
await this._transporter.send(Number("0x5a"), 3, 1, 0, buffer_default.from(dataBuf));
|
|
19087
|
+
const paymasterBuf = concatenateArrayBuffer(
|
|
19088
|
+
txDetails.paymasterData.map((value) => {
|
|
19089
|
+
const a = this.convertBnToLedger(value);
|
|
19090
|
+
return a;
|
|
19091
|
+
})
|
|
19092
|
+
);
|
|
19093
|
+
await this._transporter.send(Number("0x5a"), 3, 2, 0, buffer_default.from(paymasterBuf));
|
|
19094
|
+
const accountDeployDataBuf = concatenateArrayBuffer(
|
|
19095
|
+
txDetails.paymasterData.map((value) => {
|
|
19096
|
+
const a = this.convertBnToLedger(value);
|
|
19097
|
+
return a;
|
|
19098
|
+
})
|
|
19099
|
+
);
|
|
19100
|
+
await this._transporter.send(Number("0x5a"), 3, 3, 0, buffer_default.from(accountDeployDataBuf));
|
|
19101
|
+
const nbCallsBuf = this.convertBnToLedger(calls.length);
|
|
19102
|
+
await this._transporter.send(Number("0x5a"), 3, 4, 0, buffer_default.from(nbCallsBuf));
|
|
19103
|
+
let respSign = new Uint8Array(0);
|
|
19104
|
+
for (const call of calls) {
|
|
19105
|
+
const calldatas = this.encodeCall(call);
|
|
19106
|
+
await this._transporter.send(Number("0x5a"), 3, 5, 0, buffer_default.from(calldatas[0]));
|
|
19107
|
+
if (calldatas.length > 1) {
|
|
19108
|
+
calldatas.slice(1).forEach(async (part) => {
|
|
19109
|
+
await this._transporter.send(Number("0x5a"), 3, 5, 1, buffer_default.from(part));
|
|
19110
|
+
});
|
|
19111
|
+
}
|
|
19112
|
+
respSign = await this._transporter.send(Number("0x5a"), 3, 5, 2);
|
|
19113
|
+
}
|
|
19114
|
+
return this.decodeSignatureLedger(respSign);
|
|
19115
|
+
}
|
|
19116
|
+
/**
|
|
19117
|
+
*Ask the Ledger Nano to display and sign a Starknet V3 account deployment.
|
|
19118
|
+
* @param {V3DeployAccountSignerDetails} deployAccountDetail All the details needed for a V3 deploy account.
|
|
19119
|
+
* @returns an object including the transaction Hash and the signature
|
|
19120
|
+
* @example
|
|
19121
|
+
* ```typescript
|
|
19122
|
+
* const deployData: V3DeployAccountSignerDetails =
|
|
19123
|
+
* {
|
|
19124
|
+
* tip: 0, paymasterData: [], accountDeploymentData: [],
|
|
19125
|
+
* nonceDataAvailabilityMode: 'L1', feeDataAvailabilityMode: 'L1',
|
|
19126
|
+
* resourceBounds: {
|
|
19127
|
+
* l2_gas: { max_amount: '0x0', max_price_per_unit: '0x0' },
|
|
19128
|
+
* l1_gas: { max_amount: '0x226', max_price_per_unit: '0x22ecb25c00' }
|
|
19129
|
+
* },
|
|
19130
|
+
* classHash: '0x540d7f5ec7ecf317e68d48564934cb99259781b1ee3cedbbc37ec5337f8e688',
|
|
19131
|
+
* constructorCalldata: [
|
|
19132
|
+
* '3571125127744830445572285574469842579401255431821644822726857471463672199621'
|
|
19133
|
+
* ],
|
|
19134
|
+
* contractAddress: '0x4ca062add1cf12a107be1107af17981cf6e544a24d987693230ea481d3d5e34',
|
|
19135
|
+
* addressSalt: '0x07e52f68e3160e1ef698211cdf6d3792368fe347e7e2d4a8ace14d9b248f39c5',
|
|
19136
|
+
* chainId: '0x534e5f5345504f4c4941',
|
|
19137
|
+
* version: '0x3', nonce: 0n
|
|
19138
|
+
*}
|
|
19139
|
+
* const res = await myLedgerSigner.signDeployAccountV3(deployData);
|
|
19140
|
+
* // res = {hash:
|
|
19141
|
+
* // signature:
|
|
19142
|
+
* // }
|
|
19143
|
+
* ```
|
|
19144
|
+
*/
|
|
19145
|
+
async signDeployAccountV3(deployAccountDetail) {
|
|
19146
|
+
await this._transporter.send(Number("0x5a"), 5, 0, 0, buffer_default.from(this.pathBuffer));
|
|
19147
|
+
const accountAddressBuf = this.convertBnToLedger(
|
|
19148
|
+
deployAccountDetail.contractAddress
|
|
18972
19149
|
);
|
|
18973
|
-
|
|
18974
|
-
|
|
18975
|
-
|
|
19150
|
+
const chainIdBuf = this.convertBnToLedger(deployAccountDetail.chainId);
|
|
19151
|
+
const nonceBuf = this.convertBnToLedger(deployAccountDetail.nonce);
|
|
19152
|
+
const dAModeHashBuf = this.convertBnToLedger(
|
|
19153
|
+
hashDAMode(
|
|
19154
|
+
deployAccountDetail.nonceDataAvailabilityMode === api_exports.EDataAvailabilityMode.L1 ? api_exports.EDAMode.L1 : api_exports.EDAMode.L2,
|
|
19155
|
+
deployAccountDetail.feeDataAvailabilityMode === api_exports.EDataAvailabilityMode.L1 ? api_exports.EDAMode.L1 : api_exports.EDAMode.L2
|
|
19156
|
+
)
|
|
18976
19157
|
);
|
|
18977
|
-
const
|
|
18978
|
-
const
|
|
18979
|
-
|
|
18980
|
-
|
|
18981
|
-
|
|
19158
|
+
const classHashBuf = this.convertBnToLedger(deployAccountDetail.classHash);
|
|
19159
|
+
const saltBuf = this.convertBnToLedger(deployAccountDetail.addressSalt);
|
|
19160
|
+
const dataBuf = concatenateArrayBuffer([
|
|
19161
|
+
accountAddressBuf,
|
|
19162
|
+
chainIdBuf,
|
|
19163
|
+
nonceBuf,
|
|
19164
|
+
dAModeHashBuf,
|
|
19165
|
+
classHashBuf,
|
|
19166
|
+
saltBuf
|
|
19167
|
+
]);
|
|
19168
|
+
await this._transporter.send(Number("0x5a"), 5, 1, 0, buffer_default.from(dataBuf));
|
|
19169
|
+
const tipBuf = this.convertBnToLedger(deployAccountDetail.tip);
|
|
19170
|
+
const l1_gasBuf = this.convertBnToLedger(
|
|
19171
|
+
encodeResourceBoundsL1(deployAccountDetail.resourceBounds)
|
|
18982
19172
|
);
|
|
18983
|
-
|
|
18984
|
-
|
|
18985
|
-
|
|
18986
|
-
|
|
18987
|
-
|
|
18988
|
-
|
|
18989
|
-
)
|
|
19173
|
+
const l2_gasBuf = this.convertBnToLedger(
|
|
19174
|
+
encodeResourceBoundsL2(deployAccountDetail.resourceBounds)
|
|
19175
|
+
);
|
|
19176
|
+
const feeBuf = concatenateArrayBuffer([tipBuf, l1_gasBuf, l2_gasBuf]);
|
|
19177
|
+
await this._transporter.send(Number("0x5a"), 5, 2, 0, buffer_default.from(feeBuf));
|
|
19178
|
+
const paymasterBuf = concatenateArrayBuffer(
|
|
19179
|
+
deployAccountDetail.paymasterData.map((value) => {
|
|
19180
|
+
const a = this.convertBnToLedger(value);
|
|
19181
|
+
return a;
|
|
19182
|
+
})
|
|
19183
|
+
);
|
|
19184
|
+
await this._transporter.send(Number("0x5a"), 5, 3, 0, buffer_default.from(paymasterBuf));
|
|
19185
|
+
const compiledConstructor = CallData.compile(deployAccountDetail.constructorCalldata);
|
|
19186
|
+
const constructorLengthBuf = this.convertBnToLedger(compiledConstructor.length);
|
|
19187
|
+
await this._transporter.send(Number("0x5a"), 5, 4, 0, buffer_default.from(constructorLengthBuf));
|
|
19188
|
+
const constructorBuf = concatenateArrayBuffer(
|
|
19189
|
+
compiledConstructor.map((parameter) => {
|
|
19190
|
+
const a = this.convertBnToLedger(parameter);
|
|
19191
|
+
return a;
|
|
19192
|
+
})
|
|
19193
|
+
);
|
|
19194
|
+
const constructorChunks = [];
|
|
19195
|
+
const chunkSize = 7 * 32;
|
|
19196
|
+
for (let i = 0; i < constructorBuf.length; i += chunkSize)
|
|
19197
|
+
constructorChunks.push(constructorBuf.subarray(i, i + chunkSize));
|
|
19198
|
+
let respSign = new Uint8Array(0);
|
|
19199
|
+
for (const chunk of constructorChunks) {
|
|
19200
|
+
respSign = await this._transporter.send(Number("0x5a"), 5, 5, 0, buffer_default.from(chunk));
|
|
18990
19201
|
}
|
|
19202
|
+
return this.decodeSignatureLedger(respSign);
|
|
18991
19203
|
}
|
|
19204
|
+
};
|
|
19205
|
+
function getLedgerPathBuffer221(accountId, applicationName = "LedgerW") {
|
|
19206
|
+
const path0buff = new Uint8Array([HARDENING_BYTE, 0, 10, 85]);
|
|
19207
|
+
const path1buff = new Uint8Array([71 | HARDENING_BYTE, 65, 233, 201]);
|
|
19208
|
+
const path2Base = applicationName === "LedgerW" ? new Uint8Array([43, 206, 231, 219]) : stringToSha256ToArrayBuff4(applicationName);
|
|
19209
|
+
const path2buff = concatenateArrayBuffer([
|
|
19210
|
+
new Uint8Array([path2Base[0] | HARDENING_BYTE]),
|
|
19211
|
+
path2Base.subarray(1)
|
|
19212
|
+
]);
|
|
19213
|
+
const path3buff = new Uint8Array([HARDENING_BYTE, 0, 0, 0]);
|
|
19214
|
+
const hex = toHex(BigInt(accountId) | HARDENING_4BYTES);
|
|
19215
|
+
const padded = addHexPrefix(removeHexPrefix(hex).padStart(8, "0"));
|
|
19216
|
+
const path4buff = hexToBytes(padded);
|
|
19217
|
+
const path5buff = new Uint8Array([0, 0, 0, 0]);
|
|
19218
|
+
const pathBuff = concatenateArrayBuffer([
|
|
19219
|
+
path0buff,
|
|
19220
|
+
path1buff,
|
|
19221
|
+
path2buff,
|
|
19222
|
+
path3buff,
|
|
19223
|
+
path4buff,
|
|
19224
|
+
path5buff
|
|
19225
|
+
]);
|
|
19226
|
+
return pathBuff;
|
|
18992
19227
|
}
|
|
18993
|
-
|
|
18994
|
-
|
|
18995
|
-
|
|
18996
|
-
|
|
18997
|
-
|
|
19228
|
+
|
|
19229
|
+
// src/signer/ledgerSigner231.ts
|
|
19230
|
+
var LedgerSigner231 = class extends LedgerSigner221 {
|
|
19231
|
+
/**
|
|
19232
|
+
* constructor of the LedgerSigner class.
|
|
19233
|
+
* @param {Transport} transport 5 transports are available to handle USB, bluetooth, Node, Web, Mobile.
|
|
19234
|
+
* See Guides for more details.
|
|
19235
|
+
* @param {number} accountID ID of Ledger Nano account (can handle 2**31 accounts).
|
|
19236
|
+
* @param {string} [eip2645application='LedgerW'] A wallet is defined by an ERC2645 derivation path (6 items).
|
|
19237
|
+
* One item is called `application` and can be customized.
|
|
19238
|
+
* Default value is `LedgerW`.
|
|
19239
|
+
* @param {LedgerPathCalculation} [pathFunction=getLedgerPathBuffer221]
|
|
19240
|
+
* defines the function that will calculate the path. By default `getLedgerPathBuffer221` is selected.
|
|
19241
|
+
*
|
|
19242
|
+
* If you are using APP v2.3.1 with an account created with the v1.1.1, you need to use :
|
|
19243
|
+
* ```typescript
|
|
19244
|
+
* const myLedgerSigner = new LedgerSigner231(myNodeTransport, 0, undefined, getLedgerPathBuffer111);
|
|
19245
|
+
* ```
|
|
19246
|
+
* @example
|
|
19247
|
+
* ```typescript
|
|
19248
|
+
* import TransportNodeHid from "@ledgerhq/hw-transport-node-hid";
|
|
19249
|
+
* const myNodeTransport = await TransportNodeHid.create();
|
|
19250
|
+
* const myLedgerSigner = new LedgerSigner231(myNodeTransport, 0);
|
|
19251
|
+
* ```
|
|
19252
|
+
*/
|
|
19253
|
+
constructor(transport, accountID, eip2645application = "LedgerW", pathFunction = getLedgerPathBuffer221) {
|
|
19254
|
+
super(transport, accountID, eip2645application, pathFunction);
|
|
18998
19255
|
}
|
|
18999
|
-
|
|
19000
|
-
|
|
19001
|
-
|
|
19002
|
-
|
|
19003
|
-
|
|
19004
|
-
|
|
19005
|
-
|
|
19006
|
-
|
|
19007
|
-
|
|
19008
|
-
|
|
19009
|
-
|
|
19010
|
-
|
|
19011
|
-
|
|
19256
|
+
/**
|
|
19257
|
+
* Ask to the Ledger Nano to display and sign a Starknet V3 transaction.
|
|
19258
|
+
* @param {V3InvocationsSignerDetails} txDetails All the details needed for a txV3.
|
|
19259
|
+
* @param {Call[]} calls array of Starknet invocations
|
|
19260
|
+
* @returns an object including the transaction Hash and the signature
|
|
19261
|
+
* @example
|
|
19262
|
+
* ```typescript
|
|
19263
|
+
* const calls: Call[] = [{contractAddress: "0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7",
|
|
19264
|
+
* entrypoint: "transfer",
|
|
19265
|
+
* calldata:["0x11f5fc2a92ac03434a7937fe982f5e5293b65ad438a989c5b78fb8f04a12016",
|
|
19266
|
+
* "0x9184e72a000", "0x0"]}];
|
|
19267
|
+
* const txDetailsV3: V3InvocationsSignerDetails = {
|
|
19268
|
+
* chainId: constants.StarknetChainId.SN_MAIN,
|
|
19269
|
+
* nonce: "28", accountDeploymentData: [],
|
|
19270
|
+
* paymasterData: [], cairoVersion: "1",
|
|
19271
|
+
* feeDataAvailabilityMode: "L1", nonceDataAvailabilityMode: "L1",
|
|
19272
|
+
* resourceBounds: {
|
|
19273
|
+
* l1_gas: { max_amount: "0x2a00", max_price_per_unit: "0x5c00000"
|
|
19274
|
+
* },
|
|
19275
|
+
* l2_gas: { max_amount: "0x00", max_price_per_unit: "0x00"},
|
|
19276
|
+
* }, tip: 0, version: "0x3", walletAddress: account0.address
|
|
19277
|
+
* };
|
|
19278
|
+
* const res = await myLedgerSigner.signTxV3(txDetailsV3, calls);
|
|
19279
|
+
* // res = {hash:
|
|
19280
|
+
* // signature:
|
|
19281
|
+
* // }
|
|
19282
|
+
* ```
|
|
19283
|
+
*/
|
|
19284
|
+
async signTxV3(txDetails, calls) {
|
|
19285
|
+
assert(txDetails.paymasterData.length <= 7, "Paymaster data includes more than 7 items.");
|
|
19012
19286
|
assert(
|
|
19013
|
-
|
|
19014
|
-
|
|
19287
|
+
txDetails.accountDeploymentData.length <= 7,
|
|
19288
|
+
"accountDeploymentData includes more than 7 items"
|
|
19015
19289
|
);
|
|
19016
|
-
|
|
19017
|
-
|
|
19018
|
-
|
|
19019
|
-
|
|
19020
|
-
|
|
19021
|
-
|
|
19022
|
-
|
|
19023
|
-
|
|
19024
|
-
|
|
19025
|
-
assert(
|
|
19026
|
-
preparedTransaction.fee.suggested_max_fee_in_gas_token <= maxFeeInGasToken,
|
|
19027
|
-
"Gas token price is too high"
|
|
19028
|
-
);
|
|
19029
|
-
}
|
|
19030
|
-
}
|
|
19031
|
-
const msg = message;
|
|
19032
|
-
const version = hasV1Calls ? OutsideExecutionVersion.V1 : OutsideExecutionVersion.V2;
|
|
19033
|
-
const [callerKey, nonceKey, afterKey, beforeKey] = hasV1Calls ? ["caller", "nonce", "execute_after", "execute_before"] : ["Caller", "Nonce", "Execute After", "Execute Before"];
|
|
19034
|
-
assert(
|
|
19035
|
-
[callerKey, nonceKey, afterKey, beforeKey].every((key) => msg[key] !== void 0),
|
|
19036
|
-
"Paymaster typed data does not match the expected outside execution"
|
|
19290
|
+
await this._transporter.send(Number("0x5a"), 3, 0, 0, buffer_default.from(this.pathBuffer));
|
|
19291
|
+
const accountAddressBuf = this.convertBnToLedger(txDetails.walletAddress);
|
|
19292
|
+
const chainIdBuf = this.convertBnToLedger(txDetails.chainId);
|
|
19293
|
+
const nonceBuf = this.convertBnToLedger(txDetails.nonce);
|
|
19294
|
+
const dAModeHashBuf = this.convertBnToLedger(
|
|
19295
|
+
hashDAMode(
|
|
19296
|
+
intDAM(txDetails.nonceDataAvailabilityMode),
|
|
19297
|
+
intDAM(txDetails.feeDataAvailabilityMode)
|
|
19298
|
+
)
|
|
19037
19299
|
);
|
|
19038
|
-
const
|
|
19039
|
-
|
|
19040
|
-
|
|
19041
|
-
|
|
19042
|
-
|
|
19043
|
-
|
|
19044
|
-
|
|
19045
|
-
|
|
19046
|
-
|
|
19047
|
-
|
|
19300
|
+
const dataBuf = concatenateArrayBuffer([
|
|
19301
|
+
accountAddressBuf,
|
|
19302
|
+
chainIdBuf,
|
|
19303
|
+
nonceBuf,
|
|
19304
|
+
dAModeHashBuf
|
|
19305
|
+
]);
|
|
19306
|
+
await this._transporter.send(Number("0x5a"), 3, 1, 0, buffer_default.from(dataBuf));
|
|
19307
|
+
if (isRPC08Plus_ResourceBoundsBN(txDetails.resourceBounds)) {
|
|
19308
|
+
const tipBuf = this.convertBnToLedger(txDetails.tip);
|
|
19309
|
+
const l1_gasBuf = this.convertBnToLedger(encodeResourceBoundsL1(txDetails.resourceBounds));
|
|
19310
|
+
const l2_gasBuf = this.convertBnToLedger(encodeResourceBoundsL2(txDetails.resourceBounds));
|
|
19311
|
+
const l1_data_gasBuf = this.convertBnToLedger(
|
|
19312
|
+
encodeDataResourceBoundsL1(txDetails.resourceBounds)
|
|
19048
19313
|
);
|
|
19314
|
+
const feeBuf = concatenateArrayBuffer([
|
|
19315
|
+
tipBuf,
|
|
19316
|
+
l1_gasBuf,
|
|
19317
|
+
l2_gasBuf,
|
|
19318
|
+
l1_data_gasBuf
|
|
19319
|
+
]);
|
|
19320
|
+
await this._transporter.send(Number("0x5a"), 3, 2, 0, buffer_default.from(feeBuf));
|
|
19049
19321
|
}
|
|
19050
|
-
const
|
|
19051
|
-
|
|
19052
|
-
|
|
19053
|
-
|
|
19054
|
-
|
|
19055
|
-
expectedCalls.push({
|
|
19056
|
-
contractAddress: paymasterDetails.feeMode.gasToken,
|
|
19057
|
-
entrypoint: "transfer",
|
|
19058
|
-
calldata: [feeRecipient, feeAmount.low, feeAmount.high]
|
|
19059
|
-
});
|
|
19060
|
-
}
|
|
19061
|
-
const expectedTypedData = getTypedData(
|
|
19062
|
-
chainId,
|
|
19063
|
-
{ caller, execute_after: executeAfter, execute_before: executeBefore },
|
|
19064
|
-
nonce,
|
|
19065
|
-
expectedCalls,
|
|
19066
|
-
version
|
|
19322
|
+
const paymasterBuf = concatenateArrayBuffer(
|
|
19323
|
+
txDetails.paymasterData.map((value) => {
|
|
19324
|
+
const a = this.convertBnToLedger(value);
|
|
19325
|
+
return a;
|
|
19326
|
+
})
|
|
19067
19327
|
);
|
|
19068
|
-
|
|
19069
|
-
|
|
19070
|
-
|
|
19328
|
+
await this._transporter.send(Number("0x5a"), 3, 3, 0, buffer_default.from(paymasterBuf));
|
|
19329
|
+
const accountDeployDataBuf = concatenateArrayBuffer(
|
|
19330
|
+
txDetails.paymasterData.map((value) => {
|
|
19331
|
+
const a = this.convertBnToLedger(value);
|
|
19332
|
+
return a;
|
|
19333
|
+
})
|
|
19071
19334
|
);
|
|
19072
|
-
|
|
19073
|
-
|
|
19074
|
-
|
|
19075
|
-
|
|
19076
|
-
|
|
19077
|
-
|
|
19078
|
-
|
|
19079
|
-
|
|
19080
|
-
|
|
19081
|
-
|
|
19082
|
-
|
|
19083
|
-
|
|
19084
|
-
}
|
|
19085
|
-
return { mode: "default", gas_token: feeMode.gasToken };
|
|
19086
|
-
};
|
|
19087
|
-
var convertFEE_MODE = (feeMode) => {
|
|
19088
|
-
if (feeMode.mode === "sponsored") {
|
|
19089
|
-
return { mode: "sponsored" };
|
|
19090
|
-
}
|
|
19091
|
-
return { mode: "default", gasToken: feeMode.gas_token };
|
|
19092
|
-
};
|
|
19093
|
-
var convertTimeBounds = (timeBounds) => timeBounds ? {
|
|
19094
|
-
execute_after: timeBounds.executeAfter || 1,
|
|
19095
|
-
// If executeAfter is not provided, set it to 1, meaning the transaction can be executed immediately
|
|
19096
|
-
execute_before: timeBounds.executeBefore
|
|
19097
|
-
} : void 0;
|
|
19098
|
-
var convertTIME_BOUNDS = (timeBounds) => timeBounds ? {
|
|
19099
|
-
executeAfter: timeBounds.execute_after,
|
|
19100
|
-
executeBefore: timeBounds.execute_before
|
|
19101
|
-
} : void 0;
|
|
19102
|
-
var convertEXECUTION_PARAMETERS = (parameters) => ({
|
|
19103
|
-
version: parameters.version,
|
|
19104
|
-
feeMode: convertFEE_MODE(parameters.fee_mode),
|
|
19105
|
-
timeBounds: convertTIME_BOUNDS(parameters.time_bounds)
|
|
19106
|
-
});
|
|
19107
|
-
var defaultOptions = {
|
|
19108
|
-
headers: { "Content-Type": "application/json" }
|
|
19109
|
-
};
|
|
19110
|
-
var PaymasterRpc = class _PaymasterRpc {
|
|
19111
|
-
nodeUrl;
|
|
19112
|
-
headers;
|
|
19113
|
-
baseFetch;
|
|
19114
|
-
requestId;
|
|
19115
|
-
constructor(options) {
|
|
19116
|
-
if (options instanceof _PaymasterRpc) {
|
|
19117
|
-
this.nodeUrl = options.nodeUrl;
|
|
19118
|
-
this.headers = { ...defaultOptions.headers, ...options.headers };
|
|
19119
|
-
this.baseFetch = options.baseFetch;
|
|
19120
|
-
this.requestId = options.requestId;
|
|
19121
|
-
return;
|
|
19122
|
-
}
|
|
19123
|
-
if (options && "nodeUrl" in options && "headers" in options && "baseFetch" in options) {
|
|
19124
|
-
this.nodeUrl = options.nodeUrl ?? getDefaultPaymasterNodeUrl(void 0);
|
|
19125
|
-
this.headers = { ...defaultOptions.headers, ...options.headers };
|
|
19126
|
-
this.baseFetch = options.baseFetch ?? fetch_default;
|
|
19127
|
-
this.requestId = 0;
|
|
19128
|
-
return;
|
|
19129
|
-
}
|
|
19130
|
-
const { nodeUrl, headers, baseFetch } = options || {};
|
|
19131
|
-
if (nodeUrl && Object.values(_NetworkName).includes(nodeUrl)) {
|
|
19132
|
-
this.nodeUrl = getDefaultPaymasterNodeUrl(nodeUrl, options?.mute);
|
|
19133
|
-
} else if (nodeUrl) {
|
|
19134
|
-
this.nodeUrl = nodeUrl;
|
|
19135
|
-
} else {
|
|
19136
|
-
this.nodeUrl = getDefaultPaymasterNodeUrl(void 0, options?.mute);
|
|
19137
|
-
}
|
|
19138
|
-
this.baseFetch = baseFetch ?? fetch_default;
|
|
19139
|
-
this.headers = { ...defaultOptions.headers, ...headers };
|
|
19140
|
-
this.requestId = 0;
|
|
19141
|
-
}
|
|
19142
|
-
fetch(method, params, id = 0) {
|
|
19143
|
-
const rpcRequestBody = {
|
|
19144
|
-
id,
|
|
19145
|
-
jsonrpc: "2.0",
|
|
19146
|
-
method,
|
|
19147
|
-
...params && { params }
|
|
19148
|
-
};
|
|
19149
|
-
return this.baseFetch(this.nodeUrl, {
|
|
19150
|
-
method: "POST",
|
|
19151
|
-
body: stringify2(rpcRequestBody),
|
|
19152
|
-
headers: this.headers
|
|
19153
|
-
});
|
|
19154
|
-
}
|
|
19155
|
-
errorHandler(method, params, rpcError, otherError) {
|
|
19156
|
-
if (rpcError) {
|
|
19157
|
-
throw new RpcError(rpcError, method, params);
|
|
19158
|
-
}
|
|
19159
|
-
if (otherError instanceof LibraryError) {
|
|
19160
|
-
throw otherError;
|
|
19161
|
-
}
|
|
19162
|
-
if (otherError) {
|
|
19163
|
-
throw Error(otherError.message);
|
|
19164
|
-
}
|
|
19165
|
-
}
|
|
19166
|
-
async fetchEndpoint(method, params) {
|
|
19167
|
-
try {
|
|
19168
|
-
this.requestId += 1;
|
|
19169
|
-
const rawResult = await this.fetch(method, params, this.requestId);
|
|
19170
|
-
const { error, result } = await rawResult.json();
|
|
19171
|
-
this.errorHandler(method, params, error);
|
|
19172
|
-
return result;
|
|
19173
|
-
} catch (error) {
|
|
19174
|
-
this.errorHandler(method, params, error?.response?.data, error);
|
|
19175
|
-
throw error;
|
|
19335
|
+
await this._transporter.send(Number("0x5a"), 3, 4, 0, buffer_default.from(accountDeployDataBuf));
|
|
19336
|
+
const nbCallsBuf = this.convertBnToLedger(calls.length);
|
|
19337
|
+
await this._transporter.send(Number("0x5a"), 3, 5, 0, buffer_default.from(nbCallsBuf));
|
|
19338
|
+
let respSign = new Uint8Array(0);
|
|
19339
|
+
for (const call of calls) {
|
|
19340
|
+
const calldatas = this.encodeCall(call);
|
|
19341
|
+
respSign = await this._transporter.send(Number("0x5a"), 3, 6, 0, buffer_default.from(calldatas[0]));
|
|
19342
|
+
if (calldatas.length > 1) {
|
|
19343
|
+
calldatas.slice(1).forEach(async (part) => {
|
|
19344
|
+
respSign = await this._transporter.send(Number("0x5a"), 3, 6, 1, buffer_default.from(part));
|
|
19345
|
+
});
|
|
19346
|
+
}
|
|
19176
19347
|
}
|
|
19348
|
+
return this.decodeSignatureLedger(respSign);
|
|
19177
19349
|
}
|
|
19178
|
-
|
|
19179
|
-
|
|
19180
|
-
|
|
19181
|
-
|
|
19182
|
-
|
|
19183
|
-
|
|
19184
|
-
|
|
19185
|
-
|
|
19186
|
-
|
|
19187
|
-
|
|
19188
|
-
|
|
19189
|
-
|
|
19190
|
-
|
|
19191
|
-
|
|
19192
|
-
|
|
19193
|
-
|
|
19194
|
-
|
|
19195
|
-
|
|
19196
|
-
|
|
19197
|
-
|
|
19198
|
-
|
|
19199
|
-
|
|
19200
|
-
|
|
19201
|
-
|
|
19202
|
-
|
|
19203
|
-
|
|
19204
|
-
|
|
19205
|
-
|
|
19350
|
+
/**
|
|
19351
|
+
*Ask the Ledger Nano to display and sign a Starknet V3 account deployment.
|
|
19352
|
+
* @param {V3DeployAccountSignerDetails} deployAccountDetail All the details needed for a V3 deploy account.
|
|
19353
|
+
* @returns an object including the transaction Hash and the signature
|
|
19354
|
+
* @example
|
|
19355
|
+
* ```typescript
|
|
19356
|
+
* const deployData: V3DeployAccountSignerDetails =
|
|
19357
|
+
* {
|
|
19358
|
+
* tip: 0, paymasterData: [], accountDeploymentData: [],
|
|
19359
|
+
* nonceDataAvailabilityMode: 'L1', feeDataAvailabilityMode: 'L1',
|
|
19360
|
+
* resourceBounds: {
|
|
19361
|
+
* l2_gas: { max_amount: '0x0', max_price_per_unit: '0x0' },
|
|
19362
|
+
* l1_gas: { max_amount: '0x226', max_price_per_unit: '0x22ecb25c00' }
|
|
19363
|
+
* },
|
|
19364
|
+
* classHash: '0x540d7f5ec7ecf317e68d48564934cb99259781b1ee3cedbbc37ec5337f8e688',
|
|
19365
|
+
* constructorCalldata: [
|
|
19366
|
+
* '3571125127744830445572285574469842579401255431821644822726857471463672199621'
|
|
19367
|
+
* ],
|
|
19368
|
+
* contractAddress: '0x4ca062add1cf12a107be1107af17981cf6e544a24d987693230ea481d3d5e34',
|
|
19369
|
+
* addressSalt: '0x07e52f68e3160e1ef698211cdf6d3792368fe347e7e2d4a8ace14d9b248f39c5',
|
|
19370
|
+
* chainId: '0x534e5f5345504f4c4941',
|
|
19371
|
+
* version: '0x3', nonce: 0n
|
|
19372
|
+
*}
|
|
19373
|
+
* const res = await myLedgerSigner.signDeployAccountV3(deployData);
|
|
19374
|
+
* // res = {hash:
|
|
19375
|
+
* // signature:
|
|
19376
|
+
* // }
|
|
19377
|
+
* ```
|
|
19378
|
+
*/
|
|
19379
|
+
async signDeployAccountV3(deployAccountDetail) {
|
|
19380
|
+
await this._transporter.send(Number("0x5a"), 5, 0, 0, buffer_default.from(this.pathBuffer));
|
|
19381
|
+
const accountAddressBuf = this.convertBnToLedger(
|
|
19382
|
+
deployAccountDetail.contractAddress
|
|
19383
|
+
);
|
|
19384
|
+
const chainIdBuf = this.convertBnToLedger(deployAccountDetail.chainId);
|
|
19385
|
+
const nonceBuf = this.convertBnToLedger(deployAccountDetail.nonce);
|
|
19386
|
+
const dAModeHashBuf = this.convertBnToLedger(
|
|
19387
|
+
hashDAMode(
|
|
19388
|
+
intDAM(deployAccountDetail.nonceDataAvailabilityMode),
|
|
19389
|
+
intDAM(deployAccountDetail.feeDataAvailabilityMode)
|
|
19390
|
+
)
|
|
19391
|
+
);
|
|
19392
|
+
const classHashBuf = this.convertBnToLedger(deployAccountDetail.classHash);
|
|
19393
|
+
const saltBuf = this.convertBnToLedger(deployAccountDetail.addressSalt);
|
|
19394
|
+
const dataBuf = concatenateArrayBuffer([
|
|
19395
|
+
accountAddressBuf,
|
|
19396
|
+
chainIdBuf,
|
|
19397
|
+
nonceBuf,
|
|
19398
|
+
dAModeHashBuf,
|
|
19399
|
+
classHashBuf,
|
|
19400
|
+
saltBuf
|
|
19401
|
+
]);
|
|
19402
|
+
await this._transporter.send(Number("0x5a"), 5, 1, 0, buffer_default.from(dataBuf));
|
|
19403
|
+
if (isRPC08Plus_ResourceBoundsBN(deployAccountDetail.resourceBounds)) {
|
|
19404
|
+
const tipBuf = this.convertBnToLedger(deployAccountDetail.tip);
|
|
19405
|
+
const l1_gasBuf = this.convertBnToLedger(
|
|
19406
|
+
encodeResourceBoundsL1(deployAccountDetail.resourceBounds)
|
|
19407
|
+
);
|
|
19408
|
+
const l2_gasBuf = this.convertBnToLedger(
|
|
19409
|
+
encodeResourceBoundsL2(deployAccountDetail.resourceBounds)
|
|
19410
|
+
);
|
|
19411
|
+
const l1_data_gasBuf = this.convertBnToLedger(
|
|
19412
|
+
encodeDataResourceBoundsL1(deployAccountDetail.resourceBounds)
|
|
19413
|
+
);
|
|
19414
|
+
const feeBuf = concatenateArrayBuffer([
|
|
19415
|
+
tipBuf,
|
|
19416
|
+
l1_gasBuf,
|
|
19417
|
+
l2_gasBuf,
|
|
19418
|
+
l1_data_gasBuf
|
|
19419
|
+
]);
|
|
19420
|
+
await this._transporter.send(Number("0x5a"), 5, 2, 0, buffer_default.from(feeBuf));
|
|
19206
19421
|
}
|
|
19207
|
-
const
|
|
19208
|
-
|
|
19209
|
-
|
|
19210
|
-
|
|
19211
|
-
|
|
19212
|
-
|
|
19213
|
-
|
|
19214
|
-
|
|
19215
|
-
|
|
19216
|
-
|
|
19217
|
-
|
|
19218
|
-
|
|
19219
|
-
|
|
19220
|
-
|
|
19221
|
-
|
|
19222
|
-
|
|
19223
|
-
|
|
19224
|
-
|
|
19225
|
-
|
|
19226
|
-
|
|
19227
|
-
|
|
19228
|
-
|
|
19229
|
-
|
|
19230
|
-
};
|
|
19231
|
-
case "deploy_and_invoke":
|
|
19232
|
-
return {
|
|
19233
|
-
type: "deploy_and_invoke",
|
|
19234
|
-
deployment: response.deployment,
|
|
19235
|
-
typed_data: response.typed_data,
|
|
19236
|
-
parameters: convertEXECUTION_PARAMETERS(response.parameters),
|
|
19237
|
-
fee
|
|
19238
|
-
};
|
|
19239
|
-
case "deploy":
|
|
19240
|
-
default:
|
|
19241
|
-
return {
|
|
19242
|
-
type: "deploy",
|
|
19243
|
-
deployment: response.deployment,
|
|
19244
|
-
parameters: convertEXECUTION_PARAMETERS(response.parameters),
|
|
19245
|
-
fee
|
|
19246
|
-
};
|
|
19422
|
+
const paymasterBuf = concatenateArrayBuffer(
|
|
19423
|
+
deployAccountDetail.paymasterData.map((value) => {
|
|
19424
|
+
const a = this.convertBnToLedger(value);
|
|
19425
|
+
return a;
|
|
19426
|
+
})
|
|
19427
|
+
);
|
|
19428
|
+
await this._transporter.send(Number("0x5a"), 5, 3, 0, buffer_default.from(paymasterBuf));
|
|
19429
|
+
const compiledConstructor = CallData.compile(deployAccountDetail.constructorCalldata);
|
|
19430
|
+
const constructorLengthBuf = this.convertBnToLedger(compiledConstructor.length);
|
|
19431
|
+
await this._transporter.send(Number("0x5a"), 5, 4, 0, buffer_default.from(constructorLengthBuf));
|
|
19432
|
+
const constructorBuf = concatenateArrayBuffer(
|
|
19433
|
+
compiledConstructor.map((parameter) => {
|
|
19434
|
+
const a = this.convertBnToLedger(parameter);
|
|
19435
|
+
return a;
|
|
19436
|
+
})
|
|
19437
|
+
);
|
|
19438
|
+
const constructorChunks = [];
|
|
19439
|
+
const chunkSize = 7 * 32;
|
|
19440
|
+
for (let i = 0; i < constructorBuf.length; i += chunkSize)
|
|
19441
|
+
constructorChunks.push(constructorBuf.subarray(i, i + chunkSize));
|
|
19442
|
+
let respSign = new Uint8Array(0);
|
|
19443
|
+
for (const chunk of constructorChunks) {
|
|
19444
|
+
respSign = await this._transporter.send(Number("0x5a"), 5, 5, 0, buffer_default.from(chunk));
|
|
19247
19445
|
}
|
|
19446
|
+
return this.decodeSignatureLedger(respSign);
|
|
19248
19447
|
}
|
|
19249
|
-
|
|
19250
|
-
|
|
19251
|
-
|
|
19252
|
-
|
|
19253
|
-
|
|
19254
|
-
|
|
19255
|
-
|
|
19256
|
-
|
|
19257
|
-
|
|
19258
|
-
|
|
19259
|
-
|
|
19260
|
-
|
|
19261
|
-
|
|
19262
|
-
|
|
19263
|
-
|
|
19264
|
-
|
|
19265
|
-
|
|
19266
|
-
|
|
19267
|
-
|
|
19268
|
-
|
|
19269
|
-
}
|
|
19270
|
-
};
|
|
19271
|
-
break;
|
|
19272
|
-
case "deploy":
|
|
19273
|
-
default:
|
|
19274
|
-
user_transaction = transaction;
|
|
19275
|
-
break;
|
|
19448
|
+
/** Internal function to convert a Call to an array of Uint8Array.
|
|
19449
|
+
* @param {Call} call A Call to convert.
|
|
19450
|
+
* @return {Uint8Array[]} Call encoded in an array of Uint8Array (each containing 7 u256).
|
|
19451
|
+
*/
|
|
19452
|
+
encodeCall(call) {
|
|
19453
|
+
const toBuf = this.convertBnToLedger(call.contractAddress);
|
|
19454
|
+
const selectorBuf = hexToBytes(addAddressPadding(getSelector(call.entrypoint)));
|
|
19455
|
+
let calldataBuf;
|
|
19456
|
+
if (call.calldata) {
|
|
19457
|
+
const compiledCalldata = CallData.compile(call.calldata);
|
|
19458
|
+
const calldataSizeBuf = this.convertBnToLedger(compiledCalldata.length);
|
|
19459
|
+
calldataBuf = concatenateArrayBuffer([
|
|
19460
|
+
calldataSizeBuf,
|
|
19461
|
+
...compiledCalldata.map((parameter) => {
|
|
19462
|
+
const a = this.convertBnToLedger(parameter);
|
|
19463
|
+
return a;
|
|
19464
|
+
})
|
|
19465
|
+
]);
|
|
19466
|
+
} else {
|
|
19467
|
+
calldataBuf = this.convertBnToLedger("0x00");
|
|
19276
19468
|
}
|
|
19277
|
-
const
|
|
19278
|
-
|
|
19279
|
-
|
|
19280
|
-
|
|
19281
|
-
|
|
19282
|
-
return
|
|
19283
|
-
transaction: user_transaction,
|
|
19284
|
-
parameters: executionParameters
|
|
19285
|
-
});
|
|
19286
|
-
}
|
|
19287
|
-
async getSupportedTokens() {
|
|
19288
|
-
return this.fetchEndpoint("paymaster_getSupportedTokens").then(
|
|
19289
|
-
(tokens) => tokens.map((token) => ({
|
|
19290
|
-
token_address: token.token_address,
|
|
19291
|
-
decimals: token.decimals,
|
|
19292
|
-
priceInStrk: BigInt(token.price_in_strk)
|
|
19293
|
-
}))
|
|
19294
|
-
);
|
|
19469
|
+
const callBuf = concatenateArrayBuffer([toBuf, selectorBuf, calldataBuf]);
|
|
19470
|
+
const calldatas = [];
|
|
19471
|
+
const chunkSize = 7 * 32;
|
|
19472
|
+
for (let i = 0; i < callBuf.length; i += chunkSize)
|
|
19473
|
+
calldatas.push(callBuf.subarray(i, i + chunkSize));
|
|
19474
|
+
return calldatas;
|
|
19295
19475
|
}
|
|
19296
19476
|
};
|
|
19297
19477
|
|
|
19298
|
-
// src/
|
|
19299
|
-
var
|
|
19300
|
-
|
|
19478
|
+
// src/utils/src5.ts
|
|
19479
|
+
var src5_exports = {};
|
|
19480
|
+
__export(src5_exports, {
|
|
19481
|
+
supportsInterface: () => supportsInterface
|
|
19482
|
+
});
|
|
19483
|
+
async function supportsInterface(provider, contractAddress, interfaceId) {
|
|
19484
|
+
const call = {
|
|
19485
|
+
contractAddress: toHex(contractAddress),
|
|
19486
|
+
entrypoint: "supports_interface",
|
|
19487
|
+
calldata: [toHex(interfaceId)]
|
|
19488
|
+
};
|
|
19489
|
+
try {
|
|
19490
|
+
const resp = await provider.callContract(call);
|
|
19491
|
+
return BigInt(resp[0]) !== 0n;
|
|
19492
|
+
} catch {
|
|
19493
|
+
return false;
|
|
19494
|
+
}
|
|
19495
|
+
}
|
|
19301
19496
|
|
|
19302
19497
|
// src/deployer/compileConstructorCalldata.ts
|
|
19303
19498
|
function checkCompiledConstructorCalldata(calldata, abi) {
|
|
@@ -19454,7 +19649,6 @@ var Account = class {
|
|
|
19454
19649
|
// holds Cairo 0 contracts, to refuse a Cairo 0 account; otherwise remove it.
|
|
19455
19650
|
cairoVersion;
|
|
19456
19651
|
transactionVersion;
|
|
19457
|
-
paymaster;
|
|
19458
19652
|
deployer;
|
|
19459
19653
|
defaultTipType;
|
|
19460
19654
|
/** @internal Account-level plugin management */
|
|
@@ -19476,11 +19670,13 @@ var Account = class {
|
|
|
19476
19670
|
this.cairoVersion = cairoVersion.toString();
|
|
19477
19671
|
}
|
|
19478
19672
|
this.transactionVersion = transactionVersion ?? config.get("transactionVersion");
|
|
19479
|
-
this.paymaster = paymaster ? new PaymasterRpc(paymaster) : new PaymasterRpc({ mute: true });
|
|
19480
19673
|
this.deployer = options.deployer ?? defaultDeployer;
|
|
19481
19674
|
this.defaultTipType = defaultTipType ?? config.get("defaultTipType");
|
|
19482
19675
|
this.accountPluginManager = new PluginManager();
|
|
19483
|
-
const plugins =
|
|
19676
|
+
const plugins = applyPaymasterOption(
|
|
19677
|
+
options.plugins === false ? [] : options.plugins ?? defaultPlugins,
|
|
19678
|
+
paymaster
|
|
19679
|
+
);
|
|
19484
19680
|
plugins.forEach((plugin) => {
|
|
19485
19681
|
this.accountPluginManager.installOnAccount(plugin, this);
|
|
19486
19682
|
});
|
|
@@ -20191,114 +20387,6 @@ var Account = class {
|
|
|
20191
20387
|
})
|
|
20192
20388
|
);
|
|
20193
20389
|
}
|
|
20194
|
-
/*
|
|
20195
|
-
* SNIP-29 Paymaster
|
|
20196
|
-
*/
|
|
20197
|
-
async buildPaymasterTransaction(calls, paymasterDetails) {
|
|
20198
|
-
if (!paymasterDetails.deploymentData) {
|
|
20199
|
-
const snip9Version = await this.getSnip9Version();
|
|
20200
|
-
if (snip9Version === OutsideExecutionVersion.UNSUPPORTED) {
|
|
20201
|
-
throw Error("Account is not compatible with SNIP-9");
|
|
20202
|
-
}
|
|
20203
|
-
}
|
|
20204
|
-
const parameters = {
|
|
20205
|
-
version: "0x1",
|
|
20206
|
-
feeMode: paymasterDetails.feeMode,
|
|
20207
|
-
timeBounds: paymasterDetails.timeBounds
|
|
20208
|
-
};
|
|
20209
|
-
let transaction;
|
|
20210
|
-
if (paymasterDetails.deploymentData) {
|
|
20211
|
-
if (calls.length > 0) {
|
|
20212
|
-
transaction = {
|
|
20213
|
-
type: "deploy_and_invoke",
|
|
20214
|
-
invoke: { userAddress: this.address, calls },
|
|
20215
|
-
deployment: paymasterDetails.deploymentData
|
|
20216
|
-
};
|
|
20217
|
-
} else {
|
|
20218
|
-
transaction = {
|
|
20219
|
-
type: "deploy",
|
|
20220
|
-
deployment: paymasterDetails.deploymentData
|
|
20221
|
-
};
|
|
20222
|
-
}
|
|
20223
|
-
} else {
|
|
20224
|
-
transaction = {
|
|
20225
|
-
type: "invoke",
|
|
20226
|
-
invoke: { userAddress: this.address, calls }
|
|
20227
|
-
};
|
|
20228
|
-
}
|
|
20229
|
-
return this.paymaster.buildTransaction(transaction, parameters);
|
|
20230
|
-
}
|
|
20231
|
-
async estimatePaymasterTransactionFee(calls, paymasterDetails) {
|
|
20232
|
-
const preparedTransaction = await this.buildPaymasterTransaction(calls, paymasterDetails);
|
|
20233
|
-
return preparedTransaction.fee;
|
|
20234
|
-
}
|
|
20235
|
-
/**
|
|
20236
|
-
* Signs a transaction prepared by the paymaster, after checking it is safe to sign. The safety
|
|
20237
|
-
* check needs the calls and the fee details the user actually requested, so a caller that uses
|
|
20238
|
-
* this low-level method directly must pass them, the same way `executePaymasterTransaction`
|
|
20239
|
-
* does.
|
|
20240
|
-
* @param preparedTransaction the transaction returned by the paymaster.
|
|
20241
|
-
* @param calls the calls the user requested.
|
|
20242
|
-
* @param paymasterDetails the fee mode and related paymaster details.
|
|
20243
|
-
* @param maxFeeInGasToken optional user-approved ceiling on the gas-token fee.
|
|
20244
|
-
*/
|
|
20245
|
-
async preparePaymasterTransaction(preparedTransaction, calls, paymasterDetails, maxFeeInGasToken) {
|
|
20246
|
-
assertPaymasterTransactionSafety(
|
|
20247
|
-
preparedTransaction,
|
|
20248
|
-
calls,
|
|
20249
|
-
paymasterDetails,
|
|
20250
|
-
await this.provider.getChainId(),
|
|
20251
|
-
maxFeeInGasToken
|
|
20252
|
-
);
|
|
20253
|
-
let transaction;
|
|
20254
|
-
switch (preparedTransaction.type) {
|
|
20255
|
-
case "deploy_and_invoke": {
|
|
20256
|
-
const signature = await this.signMessage(preparedTransaction.typed_data);
|
|
20257
|
-
transaction = {
|
|
20258
|
-
type: "deploy_and_invoke",
|
|
20259
|
-
invoke: {
|
|
20260
|
-
userAddress: this.address,
|
|
20261
|
-
typedData: preparedTransaction.typed_data,
|
|
20262
|
-
signature: signatureToHexArray(signature)
|
|
20263
|
-
},
|
|
20264
|
-
deployment: preparedTransaction.deployment
|
|
20265
|
-
};
|
|
20266
|
-
break;
|
|
20267
|
-
}
|
|
20268
|
-
case "invoke": {
|
|
20269
|
-
const signature = await this.signMessage(preparedTransaction.typed_data);
|
|
20270
|
-
transaction = {
|
|
20271
|
-
type: "invoke",
|
|
20272
|
-
invoke: {
|
|
20273
|
-
userAddress: this.address,
|
|
20274
|
-
typedData: preparedTransaction.typed_data,
|
|
20275
|
-
signature: signatureToHexArray(signature)
|
|
20276
|
-
}
|
|
20277
|
-
};
|
|
20278
|
-
break;
|
|
20279
|
-
}
|
|
20280
|
-
case "deploy": {
|
|
20281
|
-
transaction = {
|
|
20282
|
-
type: "deploy",
|
|
20283
|
-
deployment: preparedTransaction.deployment
|
|
20284
|
-
};
|
|
20285
|
-
break;
|
|
20286
|
-
}
|
|
20287
|
-
default:
|
|
20288
|
-
throw Error("Invalid transaction type");
|
|
20289
|
-
}
|
|
20290
|
-
return transaction;
|
|
20291
|
-
}
|
|
20292
|
-
async executePaymasterTransaction(calls, paymasterDetails, maxFeeInGasToken) {
|
|
20293
|
-
const preparedTransaction = await this.buildPaymasterTransaction(calls, paymasterDetails);
|
|
20294
|
-
const transaction = await this.preparePaymasterTransaction(
|
|
20295
|
-
preparedTransaction,
|
|
20296
|
-
calls,
|
|
20297
|
-
paymasterDetails,
|
|
20298
|
-
maxFeeInGasToken
|
|
20299
|
-
);
|
|
20300
|
-
return this.paymaster.executeTransaction(transaction, preparedTransaction.parameters).then((response) => ({ transaction_hash: response.transaction_hash }));
|
|
20301
|
-
}
|
|
20302
20390
|
};
|
|
20303
20391
|
|
|
20304
20392
|
// src/account/interface.ts
|
|
@@ -21330,6 +21418,7 @@ function filterEventsByAddress(events, contractAddress) {
|
|
|
21330
21418
|
}
|
|
21331
21419
|
|
|
21332
21420
|
// src/contract/default.ts
|
|
21421
|
+
var NO_PAYMASTER_MESSAGE = "This account has no paymaster methods: add paymasterPlugin() to its plugins, or set its paymaster option.";
|
|
21333
21422
|
function buildCall(contract, functionAbi) {
|
|
21334
21423
|
return async function(...args) {
|
|
21335
21424
|
const options = { ...contract.withOptionsProps };
|
|
@@ -21568,13 +21657,12 @@ var Contract = class _Contract {
|
|
|
21568
21657
|
await this.ensureProvider();
|
|
21569
21658
|
if (isAccount(this.providerOrAccount)) {
|
|
21570
21659
|
if (restInvokeOptions.paymasterDetails) {
|
|
21571
|
-
|
|
21572
|
-
|
|
21573
|
-
|
|
21574
|
-
|
|
21575
|
-
};
|
|
21660
|
+
assert(
|
|
21661
|
+
typeof this.providerOrAccount.executePaymasterTransaction === "function",
|
|
21662
|
+
NO_PAYMASTER_MESSAGE
|
|
21663
|
+
);
|
|
21576
21664
|
return this.providerOrAccount.executePaymasterTransaction(
|
|
21577
|
-
[
|
|
21665
|
+
[invocation],
|
|
21578
21666
|
restInvokeOptions.paymasterDetails,
|
|
21579
21667
|
restInvokeOptions.maxFeeInGasToken
|
|
21580
21668
|
);
|
|
@@ -21618,13 +21706,12 @@ var Contract = class _Contract {
|
|
|
21618
21706
|
await this.ensureProvider();
|
|
21619
21707
|
if (isAccount(this.providerOrAccount)) {
|
|
21620
21708
|
if (estimateDetails.paymasterDetails) {
|
|
21621
|
-
|
|
21622
|
-
|
|
21623
|
-
|
|
21624
|
-
|
|
21625
|
-
};
|
|
21709
|
+
assert(
|
|
21710
|
+
typeof this.providerOrAccount.estimatePaymasterTransactionFee === "function",
|
|
21711
|
+
NO_PAYMASTER_MESSAGE
|
|
21712
|
+
);
|
|
21626
21713
|
return this.providerOrAccount.estimatePaymasterTransactionFee(
|
|
21627
|
-
[
|
|
21714
|
+
[invocation],
|
|
21628
21715
|
estimateDetails.paymasterDetails
|
|
21629
21716
|
);
|
|
21630
21717
|
}
|
|
@@ -21997,6 +22084,7 @@ function units(amount, simbol = "fri") {
|
|
|
21997
22084
|
outsideExecution,
|
|
21998
22085
|
parseCalldataField,
|
|
21999
22086
|
paymaster,
|
|
22087
|
+
paymasterPlugin,
|
|
22000
22088
|
provider,
|
|
22001
22089
|
selector,
|
|
22002
22090
|
shortString,
|