mppx 0.18.1 → 0.19.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +24 -0
- package/dist/Errors.d.ts +11 -4
- package/dist/Errors.js +13 -4
- package/dist/cli/cli.js +25 -25
- 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/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 +6 -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 +32 -12
- 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/package.json +9 -1
- /package/dist/extensions/identity/{web-bot-auth/internal → internal}/JwkThumbprint.d.ts +0 -0
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import type * as Identity from '../../../../identity/Types.js';
|
|
2
|
+
import * as Attestation from '../../attestation/server/index.js';
|
|
3
|
+
import type * as AttestationTypes from '../../attestation/Types.js';
|
|
4
|
+
import type * as Types from '../Types.js';
|
|
5
|
+
/**
|
|
6
|
+
* Creates a request-stage verifier that requires a human verified with World ID: an
|
|
7
|
+
* `Attestation.Server.verifier` of type `world-id` from a shared World ID issuer.
|
|
8
|
+
*
|
|
9
|
+
* A request without one gets 401 with `Identity-Attestation-Required: world-id;issuer="…"`. The
|
|
10
|
+
* client runs World IdP's device flow through the issuer, which holds the World IdP client, and
|
|
11
|
+
* sends the attestation in `Identity-Attestation`. This server never calls World IdP and hosts no
|
|
12
|
+
* routes. An attestation whose `acr` is not in `acr` is `forbidden` (403).
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```ts
|
|
16
|
+
* const human = World.Server.verifier({
|
|
17
|
+
* audience: 'https://api.example',
|
|
18
|
+
* issuer: 'https://world-id.issuer.example',
|
|
19
|
+
* })
|
|
20
|
+
* const result = await mppx.charge({ amount: '1', identity: [human] })(request)
|
|
21
|
+
* if (result.status === 200) result.identity?.get(human)?.subject
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
export declare function verifier(config: verifier.Config): Identity.Verifier<Types.VerifiedHuman>;
|
|
25
|
+
export declare namespace verifier {
|
|
26
|
+
type Config = {
|
|
27
|
+
/** Trusted World ID issuer URL. */
|
|
28
|
+
readonly issuer: string;
|
|
29
|
+
/**
|
|
30
|
+
* This server's public origin, such as `https://api.example`, which attestations must be
|
|
31
|
+
* bound to. The function form is deprecated: see `Attestation.Server.verifier.AudienceFunction`.
|
|
32
|
+
*/
|
|
33
|
+
readonly audience: string | Attestation.verifier.AudienceFunction;
|
|
34
|
+
/**
|
|
35
|
+
* Accepted World IdP authentication classes (`Constants.acr`). Defaults to any the issuer
|
|
36
|
+
* attests.
|
|
37
|
+
*/
|
|
38
|
+
readonly acr?: readonly string[] | undefined;
|
|
39
|
+
/** Resolves issuer keys. Defaults to the issuer's JSON Web Key Set. */
|
|
40
|
+
readonly keyResolver?: AttestationTypes.KeyResolver | undefined;
|
|
41
|
+
/** Fetch used for the default JSON Web Key Set resolver. */
|
|
42
|
+
readonly fetch?: typeof globalThis.fetch | undefined;
|
|
43
|
+
/** Oldest accepted attestation, in seconds since it was issued. Defaults to its own expiry. */
|
|
44
|
+
readonly maxAge?: number | undefined;
|
|
45
|
+
/** Clock tolerance in seconds. @default 60 */
|
|
46
|
+
readonly clockTolerance?: number | undefined;
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import * as Attestation from '../../attestation/server/index.js';
|
|
2
|
+
import { Constants } from '../Constants.js';
|
|
3
|
+
/**
|
|
4
|
+
* Creates a request-stage verifier that requires a human verified with World ID: an
|
|
5
|
+
* `Attestation.Server.verifier` of type `world-id` from a shared World ID issuer.
|
|
6
|
+
*
|
|
7
|
+
* A request without one gets 401 with `Identity-Attestation-Required: world-id;issuer="…"`. The
|
|
8
|
+
* client runs World IdP's device flow through the issuer, which holds the World IdP client, and
|
|
9
|
+
* sends the attestation in `Identity-Attestation`. This server never calls World IdP and hosts no
|
|
10
|
+
* routes. An attestation whose `acr` is not in `acr` is `forbidden` (403).
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
* const human = World.Server.verifier({
|
|
15
|
+
* audience: 'https://api.example',
|
|
16
|
+
* issuer: 'https://world-id.issuer.example',
|
|
17
|
+
* })
|
|
18
|
+
* const result = await mppx.charge({ amount: '1', identity: [human] })(request)
|
|
19
|
+
* if (result.status === 200) result.identity?.get(human)?.subject
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
export function verifier(config) {
|
|
23
|
+
const acr = config.acr;
|
|
24
|
+
if (acr && acr.length === 0)
|
|
25
|
+
throw new TypeError('World ID verifier requires at least one acr.');
|
|
26
|
+
const inner = Attestation.verifier({
|
|
27
|
+
audience: config.audience,
|
|
28
|
+
clockTolerance: config.clockTolerance,
|
|
29
|
+
fetch: config.fetch,
|
|
30
|
+
issuer: config.issuer,
|
|
31
|
+
keyResolver: config.keyResolver,
|
|
32
|
+
maxAge: config.maxAge,
|
|
33
|
+
type: Constants.type,
|
|
34
|
+
...(acr && {
|
|
35
|
+
validate(claims) {
|
|
36
|
+
const value = claims.acr;
|
|
37
|
+
if (typeof value === 'string' && acr.includes(value))
|
|
38
|
+
return undefined;
|
|
39
|
+
return 'This World ID verification level is not accepted.';
|
|
40
|
+
},
|
|
41
|
+
}),
|
|
42
|
+
});
|
|
43
|
+
return {
|
|
44
|
+
async verify(request) {
|
|
45
|
+
const result = await inner.verify(request);
|
|
46
|
+
if (result.status !== 'valid')
|
|
47
|
+
return result;
|
|
48
|
+
const { claims } = result.value;
|
|
49
|
+
const value = claims.acr;
|
|
50
|
+
return {
|
|
51
|
+
status: 'valid',
|
|
52
|
+
value: Object.freeze({
|
|
53
|
+
...(typeof value === 'string' && { acr: value }),
|
|
54
|
+
expiresAt: new Date(claims.exp * 1_000),
|
|
55
|
+
id: claims.jti,
|
|
56
|
+
issuer: claims.iss,
|
|
57
|
+
subject: claims.sub,
|
|
58
|
+
}),
|
|
59
|
+
};
|
|
60
|
+
},
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
* never sent across origins on a redirect or forwarded upstream by a proxy. Provers declare
|
|
8
8
|
* their own extra sensitive headers in `Prover.headers`.
|
|
9
9
|
*/
|
|
10
|
-
export declare const request: readonly ["identity-attestation", "identity-presentation"];
|
|
10
|
+
export declare const request: readonly ["dpop", "identity-attestation", "identity-presentation", "tempo-id-token"];
|
|
11
11
|
/**
|
|
12
12
|
* `WWW-Authenticate` schemes that ask for an identity, lowercased. A proxy removes them from
|
|
13
13
|
* upstream responses: the proxy, not the upstream, decides which identity the client presents.
|
package/dist/identity/Headers.js
CHANGED
|
@@ -7,7 +7,12 @@
|
|
|
7
7
|
* never sent across origins on a redirect or forwarded upstream by a proxy. Provers declare
|
|
8
8
|
* their own extra sensitive headers in `Prover.headers`.
|
|
9
9
|
*/
|
|
10
|
-
export const request = Object.freeze([
|
|
10
|
+
export const request = Object.freeze([
|
|
11
|
+
'dpop',
|
|
12
|
+
'identity-attestation',
|
|
13
|
+
'identity-presentation',
|
|
14
|
+
'tempo-id-token',
|
|
15
|
+
]);
|
|
11
16
|
/**
|
|
12
17
|
* `WWW-Authenticate` schemes that ask for an identity, lowercased. A proxy removes them from
|
|
13
18
|
* upstream responses: the proxy, not the upstream, decides which identity the client presents.
|
package/dist/identity/Types.d.ts
CHANGED
|
@@ -112,6 +112,11 @@ export type Requirement = {
|
|
|
112
112
|
* @experimental Unstable: may change or be removed in any release.
|
|
113
113
|
*/
|
|
114
114
|
export type PaymentCheck<value = unknown> = {
|
|
115
|
+
/**
|
|
116
|
+
* The check reads its credential from `Authorization`. A route that uses it needs a server
|
|
117
|
+
* created with `requiresAuth: true`, so Payment Credentials travel in `Payment-Authorization`.
|
|
118
|
+
*/
|
|
119
|
+
readonly authorization?: boolean | undefined;
|
|
115
120
|
/** Identities the check requires, advertised in every 402 of the route. */
|
|
116
121
|
readonly requires: readonly Requirement[];
|
|
117
122
|
/** Returns the check's outcome for the paid retry (or, without a Credential, for `authorize`). */
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/** Current mppx package version. */
|
|
2
|
-
export declare const version = "0.
|
|
2
|
+
export declare const version = "0.19.0";
|
|
3
3
|
/** Canonical SDK identifier for this mppx release. */
|
|
4
|
-
export declare const sdkIdentifier = "mppx/0.
|
|
4
|
+
export declare const sdkIdentifier = "mppx/0.19.0";
|
|
5
5
|
//# sourceMappingURL=version.d.ts.map
|
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.0';
|
|
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
|
|
@@ -432,12 +432,20 @@ async function createZoneCredential(options) {
|
|
|
432
432
|
pending.delete(key);
|
|
433
433
|
});
|
|
434
434
|
}
|
|
435
|
-
const
|
|
436
|
-
|
|
435
|
+
const withdrawal = await withdrawalRequest;
|
|
436
|
+
// Derived on every attempt, so a failed read on the Zone is retried without withdrawing again.
|
|
437
|
+
const senderTags = withdrawal.senderTags ??
|
|
438
|
+
(await Zone.senderTagCandidates(zone.client, withdrawal).catch((cause) => {
|
|
439
|
+
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.`);
|
|
440
|
+
// Defined manually (non-enumerable, like native `cause`) so consumers on pre-ES2022 libs still typecheck.
|
|
441
|
+
Object.defineProperty(error, 'cause', { configurable: true, value: cause, writable: true });
|
|
442
|
+
throw error;
|
|
443
|
+
}));
|
|
444
|
+
let settled;
|
|
437
445
|
try {
|
|
438
|
-
|
|
446
|
+
settled = await Zone.waitForSettledWithdrawal(client, {
|
|
439
447
|
portal: Zone.portalAddress(zoneId),
|
|
440
|
-
|
|
448
|
+
senderTags,
|
|
441
449
|
timeoutMs: settlementTimeoutMs(challenge.expires, zone.settlementTimeoutMs),
|
|
442
450
|
to: recipient,
|
|
443
451
|
});
|
|
@@ -446,11 +454,12 @@ async function createZoneCredential(options) {
|
|
|
446
454
|
throw new Errors.ZoneSettlementPendingError({
|
|
447
455
|
cause,
|
|
448
456
|
challengeId: challenge.id,
|
|
449
|
-
|
|
457
|
+
senderTags,
|
|
450
458
|
zoneId,
|
|
451
459
|
});
|
|
452
460
|
}
|
|
453
461
|
pending.delete(key);
|
|
462
|
+
const { hash, senderTag } = settled;
|
|
454
463
|
return Credential.serialize({
|
|
455
464
|
challenge,
|
|
456
465
|
payload: { hash, senderTag, type: 'zone', zoneId },
|
|
@@ -500,7 +509,10 @@ function zoneWithdrawalKey(challenge) {
|
|
|
500
509
|
}
|
|
501
510
|
/** How long to look for a withdrawal's receipt on the Zone after its send response fails. */
|
|
502
511
|
const zoneReceiptRecoveryTimeoutMs = 15_000;
|
|
503
|
-
/**
|
|
512
|
+
/**
|
|
513
|
+
* Requests a Zone withdrawal of the challenge amount to its recipient, bound to the challenge by its memo.
|
|
514
|
+
* Returns its sender tag, or leaves it to be derived when the Zone RPC hides it from the receipt.
|
|
515
|
+
*/
|
|
504
516
|
async function requestZoneWithdrawal(options, state) {
|
|
505
517
|
const { challenge, clientId, zone } = options;
|
|
506
518
|
const request = challenge.request;
|
|
@@ -560,13 +572,21 @@ async function requestZoneWithdrawal(options, state) {
|
|
|
560
572
|
eventName: 'WithdrawalRequested',
|
|
561
573
|
logs: receipt.logs,
|
|
562
574
|
});
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
575
|
+
return {
|
|
576
|
+
blockNumber: receipt.blockNumber,
|
|
577
|
+
sender: account.address,
|
|
578
|
+
// A Zone RPC that redacts logs leaves the outbox's event out of the payer's receipt.
|
|
579
|
+
senderTags: requested
|
|
580
|
+
? [
|
|
581
|
+
Zone.senderTag({
|
|
582
|
+
fallbackNonce: requested.args.fallbackNonce,
|
|
583
|
+
sender: requested.args.sender,
|
|
584
|
+
transactionHash: receipt.transactionHash,
|
|
585
|
+
}),
|
|
586
|
+
]
|
|
587
|
+
: undefined,
|
|
568
588
|
transactionHash: receipt.transactionHash,
|
|
569
|
-
}
|
|
589
|
+
};
|
|
570
590
|
}
|
|
571
591
|
/** Parses `maxAmount` to base units, rejecting values that cannot be represented exactly. */
|
|
572
592
|
function parseMaxAmount(maxAmount, decimals) {
|
|
@@ -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
|
}
|