mppx 0.18.1 → 0.19.1
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 +33 -0
- package/dist/Errors.d.ts +40 -4
- package/dist/Errors.js +40 -6
- package/dist/cli/cli.js +96 -96
- package/dist/cli/plugins/index.js +12 -12
- package/dist/extensions/identity/Types.d.ts +26 -0
- package/dist/extensions/identity/attestation/Token.d.ts +6 -4
- package/dist/extensions/identity/attestation/Token.js +11 -5
- package/dist/extensions/identity/index.d.ts +2 -0
- package/dist/extensions/identity/index.js +2 -0
- package/dist/extensions/identity/internal/HttpMessageSignature.d.ts +2 -2
- package/dist/extensions/identity/internal/HttpMessageSignature.js +13 -10
- package/dist/extensions/identity/{web-bot-auth/internal → internal}/JwkThumbprint.js +1 -1
- package/dist/extensions/identity/internal/Signer.d.ts +20 -0
- package/dist/extensions/identity/internal/Signer.js +94 -0
- package/dist/extensions/identity/poppy/Constants.d.ts +26 -0
- package/dist/extensions/identity/poppy/Constants.js +26 -0
- package/dist/extensions/identity/poppy/Types.d.ts +19 -0
- package/dist/extensions/identity/poppy/Types.js +2 -0
- package/dist/extensions/identity/poppy/client/index.d.ts +69 -0
- package/dist/extensions/identity/poppy/client/index.js +81 -0
- package/dist/extensions/identity/poppy/index.d.ts +7 -0
- package/dist/extensions/identity/poppy/index.js +7 -0
- package/dist/extensions/identity/poppy/internal/Jws.d.ts +12 -0
- package/dist/extensions/identity/poppy/internal/Jws.js +78 -0
- package/dist/extensions/identity/poppy/server/index.d.ts +40 -0
- package/dist/extensions/identity/poppy/server/index.js +181 -0
- package/dist/extensions/identity/tap/client/index.d.ts +6 -2
- package/dist/extensions/identity/tap/client/index.js +2 -0
- package/dist/extensions/identity/tempo/Constants.d.ts +14 -0
- package/dist/extensions/identity/tempo/Constants.js +14 -0
- package/dist/extensions/identity/tempo/Types.d.ts +31 -0
- package/dist/extensions/identity/tempo/Types.js +2 -0
- package/dist/extensions/identity/tempo/client/index.d.ts +34 -0
- package/dist/extensions/identity/tempo/client/index.js +60 -0
- package/dist/extensions/identity/tempo/index.d.ts +5 -0
- package/dist/extensions/identity/tempo/index.js +4 -0
- package/dist/extensions/identity/tempo/internal/Jwt.d.ts +36 -0
- package/dist/extensions/identity/tempo/internal/Jwt.js +114 -0
- package/dist/extensions/identity/tempo/server/index.d.ts +65 -0
- package/dist/extensions/identity/tempo/server/index.js +247 -0
- package/dist/extensions/identity/web-bot-auth/client/index.d.ts +6 -4
- package/dist/extensions/identity/web-bot-auth/client/index.js +7 -5
- package/dist/extensions/identity/web-bot-auth/server/index.js +1 -1
- package/dist/extensions/identity/world/Constants.d.ts +50 -0
- package/dist/extensions/identity/world/Constants.js +50 -0
- package/dist/extensions/identity/world/Types.d.ts +52 -0
- package/dist/extensions/identity/world/Types.js +2 -0
- package/dist/extensions/identity/world/client/index.d.ts +103 -0
- package/dist/extensions/identity/world/client/index.js +208 -0
- package/dist/extensions/identity/world/index.d.ts +6 -0
- package/dist/extensions/identity/world/index.js +5 -0
- package/dist/extensions/identity/world/internal/Idp.d.ts +50 -0
- package/dist/extensions/identity/world/internal/Idp.js +265 -0
- package/dist/extensions/identity/world/issuer/index.d.ts +120 -0
- package/dist/extensions/identity/world/issuer/index.js +520 -0
- package/dist/extensions/identity/world/server/index.d.ts +49 -0
- package/dist/extensions/identity/world/server/index.js +63 -0
- package/dist/identity/Headers.d.ts +1 -1
- package/dist/identity/Headers.js +7 -1
- package/dist/identity/Types.d.ts +5 -0
- package/dist/internal/version.d.ts +2 -2
- package/dist/internal/version.js +1 -1
- package/dist/server/Mppx.js +2 -1
- package/dist/tempo/client/Charge.js +39 -16
- package/dist/tempo/internal/simulation.d.ts +33 -0
- package/dist/tempo/internal/simulation.js +80 -0
- package/dist/tempo/internal/zone.d.ts +27 -0
- package/dist/tempo/internal/zone.js +54 -7
- package/dist/tempo/server/internal/html.gen.js +1 -1
- package/dist/tempo/session/client/ChannelOps.js +5 -2
- package/package.json +13 -1
- /package/dist/extensions/identity/{web-bot-auth/internal → internal}/JwkThumbprint.d.ts +0 -0
package/dist/internal/version.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// Generated by scripts/sync-version.ts.
|
|
2
2
|
/** Current mppx package version. */
|
|
3
|
-
export const version = '0.
|
|
3
|
+
export const version = '0.19.1';
|
|
4
4
|
/** Canonical SDK identifier for this mppx release. */
|
|
5
5
|
export const sdkIdentifier = `mppx/${version}`;
|
|
6
6
|
//# sourceMappingURL=version.js.map
|
package/dist/server/Mppx.js
CHANGED
|
@@ -446,7 +446,8 @@ function createMethodFn(parameters) {
|
|
|
446
446
|
const identityRequirements = paymentCheck?.requires ?? [];
|
|
447
447
|
const hasIdentity = identityRoute.request.length > 0 || paymentCheck !== undefined;
|
|
448
448
|
// A verifier that reads Authorization would lose its credential to the Payment Credential.
|
|
449
|
-
if (identityRoute.request.some((verifier) => verifier.authorization)
|
|
449
|
+
if ((identityRoute.request.some((verifier) => verifier.authorization) ||
|
|
450
|
+
paymentCheck?.authorization) &&
|
|
450
451
|
credentialHeader !== Constants.Headers.paymentAuthorization)
|
|
451
452
|
throw new TypeError('An identity verifier on this route reads Authorization: create the server with requiresAuth: true.');
|
|
452
453
|
// Preflight hooks (e.g. session bootstrap) issue and settle their own challenges, which
|
|
@@ -18,6 +18,7 @@ import * as defaults from '../internal/defaults.js';
|
|
|
18
18
|
import { defaultFeeTokens, resolveFeeToken } from '../internal/fee-token.js';
|
|
19
19
|
import * as MachineTokenCharge from '../internal/machine-token-charge.js';
|
|
20
20
|
import * as Proof from '../internal/proof.js';
|
|
21
|
+
import * as Simulation from '../internal/simulation.js';
|
|
21
22
|
import * as Zone from '../internal/zone.js';
|
|
22
23
|
import * as Methods from '../Methods.js';
|
|
23
24
|
/**
|
|
@@ -283,13 +284,13 @@ export function charge(parameters = {}) {
|
|
|
283
284
|
const { receipts } = account.type === 'local'
|
|
284
285
|
? {
|
|
285
286
|
receipts: [
|
|
286
|
-
await sendTransactionSync(client, {
|
|
287
|
+
await Simulation.run({ account }, () => sendTransactionSync(client, {
|
|
287
288
|
account,
|
|
288
289
|
calls,
|
|
289
290
|
...(feeToken ? { feeToken } : {}),
|
|
290
291
|
nonceKey: 'expiring',
|
|
291
292
|
validBefore,
|
|
292
|
-
}),
|
|
293
|
+
})),
|
|
293
294
|
],
|
|
294
295
|
}
|
|
295
296
|
: await sendCallsSync(client, {
|
|
@@ -324,13 +325,15 @@ export function charge(parameters = {}) {
|
|
|
324
325
|
throw new Error(`Sponsored transaction preparation took too long: less than ${defaults.minRemainingValiditySeconds} seconds of its validity window remain, so the fee payer would reject it.`);
|
|
325
326
|
};
|
|
326
327
|
assertSponsorable();
|
|
327
|
-
|
|
328
|
+
// Preparation estimates gas, which simulates the calls as the signing key. A spending-limit
|
|
329
|
+
// or balance revert there would also revert on chain, at the fee payer's cost, so stop here.
|
|
330
|
+
const prepared = await Simulation.run({ account }, () => prepareTransactionRequest(client, {
|
|
328
331
|
account,
|
|
329
332
|
calls,
|
|
330
333
|
...(feeToken ? { feeToken } : {}),
|
|
331
334
|
nonceKey: 'expiring',
|
|
332
335
|
validBefore,
|
|
333
|
-
});
|
|
336
|
+
}));
|
|
334
337
|
// Estimate before enabling fee-payer mode so Tempo includes sender
|
|
335
338
|
// signature and access-key verification costs in the gas budget.
|
|
336
339
|
prepared.gas = (prepared.gas ?? 0n) + 5000n;
|
|
@@ -432,12 +435,20 @@ async function createZoneCredential(options) {
|
|
|
432
435
|
pending.delete(key);
|
|
433
436
|
});
|
|
434
437
|
}
|
|
435
|
-
const
|
|
436
|
-
|
|
438
|
+
const withdrawal = await withdrawalRequest;
|
|
439
|
+
// Derived on every attempt, so a failed read on the Zone is retried without withdrawing again.
|
|
440
|
+
const senderTags = withdrawal.senderTags ??
|
|
441
|
+
(await Zone.senderTagCandidates(zone.client, withdrawal).catch((cause) => {
|
|
442
|
+
const error = new Error(`Zone withdrawal transaction ${withdrawal.transactionHash} was sent, but its sender tag could not be read from the Zone. Retry the challenge to resume without withdrawing again.`);
|
|
443
|
+
// Defined manually (non-enumerable, like native `cause`) so consumers on pre-ES2022 libs still typecheck.
|
|
444
|
+
Object.defineProperty(error, 'cause', { configurable: true, value: cause, writable: true });
|
|
445
|
+
throw error;
|
|
446
|
+
}));
|
|
447
|
+
let settled;
|
|
437
448
|
try {
|
|
438
|
-
|
|
449
|
+
settled = await Zone.waitForSettledWithdrawal(client, {
|
|
439
450
|
portal: Zone.portalAddress(zoneId),
|
|
440
|
-
|
|
451
|
+
senderTags,
|
|
441
452
|
timeoutMs: settlementTimeoutMs(challenge.expires, zone.settlementTimeoutMs),
|
|
442
453
|
to: recipient,
|
|
443
454
|
});
|
|
@@ -446,11 +457,12 @@ async function createZoneCredential(options) {
|
|
|
446
457
|
throw new Errors.ZoneSettlementPendingError({
|
|
447
458
|
cause,
|
|
448
459
|
challengeId: challenge.id,
|
|
449
|
-
|
|
460
|
+
senderTags,
|
|
450
461
|
zoneId,
|
|
451
462
|
});
|
|
452
463
|
}
|
|
453
464
|
pending.delete(key);
|
|
465
|
+
const { hash, senderTag } = settled;
|
|
454
466
|
return Credential.serialize({
|
|
455
467
|
challenge,
|
|
456
468
|
payload: { hash, senderTag, type: 'zone', zoneId },
|
|
@@ -500,7 +512,10 @@ function zoneWithdrawalKey(challenge) {
|
|
|
500
512
|
}
|
|
501
513
|
/** How long to look for a withdrawal's receipt on the Zone after its send response fails. */
|
|
502
514
|
const zoneReceiptRecoveryTimeoutMs = 15_000;
|
|
503
|
-
/**
|
|
515
|
+
/**
|
|
516
|
+
* Requests a Zone withdrawal of the challenge amount to its recipient, bound to the challenge by its memo.
|
|
517
|
+
* Returns its sender tag, or leaves it to be derived when the Zone RPC hides it from the receipt.
|
|
518
|
+
*/
|
|
504
519
|
async function requestZoneWithdrawal(options, state) {
|
|
505
520
|
const { challenge, clientId, zone } = options;
|
|
506
521
|
const request = challenge.request;
|
|
@@ -560,13 +575,21 @@ async function requestZoneWithdrawal(options, state) {
|
|
|
560
575
|
eventName: 'WithdrawalRequested',
|
|
561
576
|
logs: receipt.logs,
|
|
562
577
|
});
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
578
|
+
return {
|
|
579
|
+
blockNumber: receipt.blockNumber,
|
|
580
|
+
sender: account.address,
|
|
581
|
+
// A Zone RPC that redacts logs leaves the outbox's event out of the payer's receipt.
|
|
582
|
+
senderTags: requested
|
|
583
|
+
? [
|
|
584
|
+
Zone.senderTag({
|
|
585
|
+
fallbackNonce: requested.args.fallbackNonce,
|
|
586
|
+
sender: requested.args.sender,
|
|
587
|
+
transactionHash: receipt.transactionHash,
|
|
588
|
+
}),
|
|
589
|
+
]
|
|
590
|
+
: undefined,
|
|
568
591
|
transactionHash: receipt.transactionHash,
|
|
569
|
-
}
|
|
592
|
+
};
|
|
570
593
|
}
|
|
571
594
|
/** Parses `maxAmount` to base units, rejecting values that cannot be represented exactly. */
|
|
572
595
|
function parseMaxAmount(maxAmount, decimals) {
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { type Address } from 'viem';
|
|
2
|
+
import * as Errors from '../../Errors.js';
|
|
3
|
+
/**
|
|
4
|
+
* Returns the revert data of a failed simulation (`eth_estimateGas` or `eth_call`), found anywhere
|
|
5
|
+
* in the error's `cause` chain.
|
|
6
|
+
*/
|
|
7
|
+
export declare function getRevertData(error: unknown): `0x${string}` | undefined;
|
|
8
|
+
/**
|
|
9
|
+
* Maps a failed pre-signing simulation to a typed client error.
|
|
10
|
+
*
|
|
11
|
+
* Tempo reverts with `SpendingLimitExceeded()` when the signing access key's remaining limit is
|
|
12
|
+
* below the payment, and with `InsufficientBalance(available, required, token)` when the account
|
|
13
|
+
* holds too little of a token. Both would fail on chain, and a fee payer would pay for the failed
|
|
14
|
+
* transaction, so the client stops before it signs. Returns `undefined` for any other error.
|
|
15
|
+
*/
|
|
16
|
+
export declare function toClientError(error: unknown, parameters: {
|
|
17
|
+
account: {
|
|
18
|
+
address: Address;
|
|
19
|
+
accessKeyAddress?: unknown;
|
|
20
|
+
};
|
|
21
|
+
}): Errors.SpendingLimitExceededError | Errors.InsufficientBalanceError | undefined;
|
|
22
|
+
/**
|
|
23
|
+
* Runs a step that simulates the transaction before it is signed, such as
|
|
24
|
+
* `prepareTransactionRequest` (which estimates gas), and rethrows a spending-limit or balance
|
|
25
|
+
* revert as a typed client error.
|
|
26
|
+
*/
|
|
27
|
+
export declare function run<value>(parameters: {
|
|
28
|
+
account: {
|
|
29
|
+
address: Address;
|
|
30
|
+
accessKeyAddress?: unknown;
|
|
31
|
+
};
|
|
32
|
+
}, fn: () => Promise<value>): Promise<value>;
|
|
33
|
+
//# sourceMappingURL=simulation.d.ts.map
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { decodeErrorResult } from 'viem';
|
|
2
|
+
import { Abis } from 'viem/tempo';
|
|
3
|
+
import * as Errors from '../../Errors.js';
|
|
4
|
+
const abi = [
|
|
5
|
+
...Abis.accountKeychain.filter((item) => item.type === 'error'),
|
|
6
|
+
...Abis.tip20.filter((item) => item.type === 'error'),
|
|
7
|
+
];
|
|
8
|
+
/**
|
|
9
|
+
* Returns the revert data of a failed simulation (`eth_estimateGas` or `eth_call`), found anywhere
|
|
10
|
+
* in the error's `cause` chain.
|
|
11
|
+
*/
|
|
12
|
+
export function getRevertData(error) {
|
|
13
|
+
const seen = new Set();
|
|
14
|
+
let current = error;
|
|
15
|
+
while (typeof current === 'object' && current !== null && !seen.has(current)) {
|
|
16
|
+
seen.add(current);
|
|
17
|
+
const data = current.data;
|
|
18
|
+
if (isRevertData(data))
|
|
19
|
+
return data;
|
|
20
|
+
const nested = data?.data;
|
|
21
|
+
if (isRevertData(nested))
|
|
22
|
+
return nested;
|
|
23
|
+
current = current.cause;
|
|
24
|
+
}
|
|
25
|
+
return undefined;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Maps a failed pre-signing simulation to a typed client error.
|
|
29
|
+
*
|
|
30
|
+
* Tempo reverts with `SpendingLimitExceeded()` when the signing access key's remaining limit is
|
|
31
|
+
* below the payment, and with `InsufficientBalance(available, required, token)` when the account
|
|
32
|
+
* holds too little of a token. Both would fail on chain, and a fee payer would pay for the failed
|
|
33
|
+
* transaction, so the client stops before it signs. Returns `undefined` for any other error.
|
|
34
|
+
*/
|
|
35
|
+
export function toClientError(error, parameters) {
|
|
36
|
+
const data = getRevertData(error);
|
|
37
|
+
if (!data)
|
|
38
|
+
return undefined;
|
|
39
|
+
const decoded = (() => {
|
|
40
|
+
try {
|
|
41
|
+
return decodeErrorResult({ abi, data });
|
|
42
|
+
}
|
|
43
|
+
catch {
|
|
44
|
+
return undefined;
|
|
45
|
+
}
|
|
46
|
+
})();
|
|
47
|
+
if (decoded?.errorName === 'SpendingLimitExceeded') {
|
|
48
|
+
const accessKey = parameters.account.accessKeyAddress;
|
|
49
|
+
return new Errors.SpendingLimitExceededError({
|
|
50
|
+
accessKey: typeof accessKey === 'string' ? accessKey : undefined,
|
|
51
|
+
account: parameters.account.address,
|
|
52
|
+
cause: error,
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
if (decoded?.errorName === 'InsufficientBalance') {
|
|
56
|
+
const [available, required, token] = decoded.args;
|
|
57
|
+
return new Errors.InsufficientBalanceError({
|
|
58
|
+
cause: error,
|
|
59
|
+
reason: `${parameters.account.address} holds ${available} of token ${token}, but the payment requires ${required}`,
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
return undefined;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Runs a step that simulates the transaction before it is signed, such as
|
|
66
|
+
* `prepareTransactionRequest` (which estimates gas), and rethrows a spending-limit or balance
|
|
67
|
+
* revert as a typed client error.
|
|
68
|
+
*/
|
|
69
|
+
export async function run(parameters, fn) {
|
|
70
|
+
try {
|
|
71
|
+
return await fn();
|
|
72
|
+
}
|
|
73
|
+
catch (error) {
|
|
74
|
+
throw toClientError(error, parameters) ?? error;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
function isRevertData(value) {
|
|
78
|
+
return typeof value === 'string' && /^0x[0-9a-fA-F]{8}/.test(value);
|
|
79
|
+
}
|
|
80
|
+
//# sourceMappingURL=simulation.js.map
|
|
@@ -66,6 +66,20 @@ export declare function findWithdrawal(calls: readonly {
|
|
|
66
66
|
portal: Address;
|
|
67
67
|
senderTag: Hex;
|
|
68
68
|
}): Withdrawal | undefined;
|
|
69
|
+
/**
|
|
70
|
+
* Derives the sender tags a withdrawal transaction may carry when its receipt
|
|
71
|
+
* has no `WithdrawalRequested` event, as on a Zone RPC that redacts logs.
|
|
72
|
+
*
|
|
73
|
+
* The outbox numbers each withdrawal with the next `lastFallbackNonce`, so the
|
|
74
|
+
* transaction's withdrawal took one of the nonces its block added. Every
|
|
75
|
+
* candidate hashes the payer's address and transaction, so only the payer's
|
|
76
|
+
* withdrawal can match one.
|
|
77
|
+
*/
|
|
78
|
+
export declare function senderTagCandidates(client: Client, parameters: {
|
|
79
|
+
blockNumber: bigint;
|
|
80
|
+
sender: Address;
|
|
81
|
+
transactionHash: Hex;
|
|
82
|
+
}): Promise<readonly Hex[]>;
|
|
69
83
|
/**
|
|
70
84
|
* Waits until a portal emits `WithdrawalProcessed` for a sender tag and
|
|
71
85
|
* returns the Tempo transaction hash that settled it.
|
|
@@ -76,4 +90,17 @@ export declare function waitForSettlement(client: Client, parameters: {
|
|
|
76
90
|
to?: Address | undefined;
|
|
77
91
|
timeoutMs?: number | undefined;
|
|
78
92
|
}): Promise<Hex>;
|
|
93
|
+
/**
|
|
94
|
+
* Waits until a portal emits `WithdrawalProcessed` for any of several sender
|
|
95
|
+
* tags, and returns the tag and the Tempo transaction hash that settled it.
|
|
96
|
+
*/
|
|
97
|
+
export declare function waitForSettledWithdrawal(client: Client, parameters: {
|
|
98
|
+
portal: Address;
|
|
99
|
+
senderTags: readonly Hex[];
|
|
100
|
+
to?: Address | undefined;
|
|
101
|
+
timeoutMs?: number | undefined;
|
|
102
|
+
}): Promise<{
|
|
103
|
+
hash: Hex;
|
|
104
|
+
senderTag: Hex;
|
|
105
|
+
}>;
|
|
79
106
|
//# sourceMappingURL=zone.d.ts.map
|
|
@@ -1,12 +1,17 @@
|
|
|
1
1
|
import { AbiParameters, Hash } from 'ox';
|
|
2
2
|
import { decodeFunctionData, getAbiItem, parseEventLogs, } from 'viem';
|
|
3
|
-
import { getBlockNumber, getLogs } from 'viem/actions';
|
|
3
|
+
import { getBlockNumber, getLogs, readContract } from 'viem/actions';
|
|
4
4
|
import { Abis, Addresses } from 'viem/tempo';
|
|
5
5
|
import * as TempoAddress from './address.js';
|
|
6
6
|
/** Default time a payer waits for a Zone withdrawal to settle on Tempo. */
|
|
7
7
|
export const defaultSettlementTimeoutMs = 60_000;
|
|
8
8
|
/** Number of Tempo blocks searched back for a settled Zone withdrawal. */
|
|
9
9
|
const lookbackBlocks = 7200n;
|
|
10
|
+
/**
|
|
11
|
+
* Most sender tags derived for one withdrawal: the withdrawals its Zone block may hold.
|
|
12
|
+
* Each candidate is a topic in every settlement poll's `eth_getLogs` filter.
|
|
13
|
+
*/
|
|
14
|
+
const maxSenderTagCandidates = 64n;
|
|
10
15
|
/** Time between polls for a settled Zone withdrawal. */
|
|
11
16
|
const pollingIntervalMs = 500;
|
|
12
17
|
const withdrawalProcessed = getAbiItem({ abi: Abis.zonePortal, name: 'WithdrawalProcessed' });
|
|
@@ -95,14 +100,56 @@ export function findWithdrawal(calls, parameters) {
|
|
|
95
100
|
}
|
|
96
101
|
return undefined;
|
|
97
102
|
}
|
|
103
|
+
/**
|
|
104
|
+
* Derives the sender tags a withdrawal transaction may carry when its receipt
|
|
105
|
+
* has no `WithdrawalRequested` event, as on a Zone RPC that redacts logs.
|
|
106
|
+
*
|
|
107
|
+
* The outbox numbers each withdrawal with the next `lastFallbackNonce`, so the
|
|
108
|
+
* transaction's withdrawal took one of the nonces its block added. Every
|
|
109
|
+
* candidate hashes the payer's address and transaction, so only the payer's
|
|
110
|
+
* withdrawal can match one.
|
|
111
|
+
*/
|
|
112
|
+
export async function senderTagCandidates(client, parameters) {
|
|
113
|
+
const { blockNumber, sender, transactionHash } = parameters;
|
|
114
|
+
const lastFallbackNonce = (blockNumber) => readContract(client, {
|
|
115
|
+
abi: Abis.zoneOutbox,
|
|
116
|
+
address: Addresses.zoneOutbox,
|
|
117
|
+
blockNumber,
|
|
118
|
+
functionName: 'lastFallbackNonce',
|
|
119
|
+
});
|
|
120
|
+
const [before, after] = await Promise.all([
|
|
121
|
+
lastFallbackNonce(blockNumber - 1n),
|
|
122
|
+
lastFallbackNonce(blockNumber),
|
|
123
|
+
]);
|
|
124
|
+
if (after <= before)
|
|
125
|
+
throw new Error(`Zone withdrawal transaction ${transactionHash} did not request a withdrawal from the outbox.`);
|
|
126
|
+
// The nonces come from the Zone RPC, so a range no block could hold is refused rather than hashed.
|
|
127
|
+
if (after - before > maxSenderTagCandidates)
|
|
128
|
+
throw new Error(`Zone withdrawal transaction ${transactionHash} has ${after - before} candidate sender tags, more than ${maxSenderTagCandidates}.`);
|
|
129
|
+
const tags = [];
|
|
130
|
+
for (let fallbackNonce = before + 1n; fallbackNonce <= after; fallbackNonce++)
|
|
131
|
+
tags.push(senderTag({ fallbackNonce, sender, transactionHash }));
|
|
132
|
+
return tags;
|
|
133
|
+
}
|
|
98
134
|
/**
|
|
99
135
|
* Waits until a portal emits `WithdrawalProcessed` for a sender tag and
|
|
100
136
|
* returns the Tempo transaction hash that settled it.
|
|
101
137
|
*/
|
|
102
138
|
export async function waitForSettlement(client, parameters) {
|
|
103
|
-
const {
|
|
139
|
+
const { senderTag, ...rest } = parameters;
|
|
140
|
+
const { hash } = await waitForSettledWithdrawal(client, { ...rest, senderTags: [senderTag] });
|
|
141
|
+
return hash;
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Waits until a portal emits `WithdrawalProcessed` for any of several sender
|
|
145
|
+
* tags, and returns the tag and the Tempo transaction hash that settled it.
|
|
146
|
+
*/
|
|
147
|
+
export async function waitForSettledWithdrawal(client, parameters) {
|
|
148
|
+
const { senderTags, timeoutMs = defaultSettlementTimeoutMs } = parameters;
|
|
104
149
|
if (!Number.isFinite(timeoutMs) || timeoutMs < 0)
|
|
105
150
|
throw new Error('Zone settlement timeout must be a finite, non-negative number.');
|
|
151
|
+
if (senderTags.length === 0)
|
|
152
|
+
throw new Error('Zone settlement needs at least one sender tag.');
|
|
106
153
|
let latest = await getBlockNumber(client, { cacheTime: 0 });
|
|
107
154
|
// Scan the lookback window once, then only blocks added since the previous poll.
|
|
108
155
|
let fromBlock = latest > lookbackBlocks ? latest - lookbackBlocks : 0n;
|
|
@@ -113,19 +160,19 @@ export async function waitForSettlement(client, parameters) {
|
|
|
113
160
|
address: parameters.portal,
|
|
114
161
|
event: withdrawalProcessed,
|
|
115
162
|
args: {
|
|
116
|
-
senderTag:
|
|
163
|
+
senderTag: [...senderTags],
|
|
117
164
|
...(parameters.to ? { to: parameters.to } : {}),
|
|
118
165
|
},
|
|
119
166
|
fromBlock,
|
|
120
167
|
toBlock: latest,
|
|
121
168
|
});
|
|
122
|
-
const
|
|
123
|
-
if (
|
|
124
|
-
return hash;
|
|
169
|
+
const [log] = logs;
|
|
170
|
+
if (log?.transactionHash && log.args.senderTag)
|
|
171
|
+
return { hash: log.transactionHash, senderTag: log.args.senderTag };
|
|
125
172
|
fromBlock = latest + 1n;
|
|
126
173
|
}
|
|
127
174
|
if (Date.now() + pollingIntervalMs > deadline)
|
|
128
|
-
throw new Error(`Zone withdrawal ${
|
|
175
|
+
throw new Error(`Zone withdrawal ${senderTags.join(' or ')} did not settle on Tempo within ${timeoutMs}ms.`);
|
|
129
176
|
await new Promise((resolve) => setTimeout(resolve, pollingIntervalMs));
|
|
130
177
|
latest = await getBlockNumber(client, { cacheTime: 0 });
|
|
131
178
|
}
|