@integraledger/profile-facilitator 0.5.0 → 0.6.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 +556 -0
- package/README.md +754 -111
- package/dist/core.d.ts +1 -1
- package/dist/core.js +683 -66
- package/dist/http.d.ts +19 -4
- package/dist/http.js +16 -6
- package/dist/module.d.ts +111 -24
- package/dist/module.js +39 -0
- package/dist/modules/algorand/index.js +1 -1
- package/dist/modules/aptos/index.js +1 -1
- package/dist/modules/cardano/index.js +1 -1
- package/dist/modules/casper/index.js +15 -3
- package/dist/modules/concordium/index.js +1 -1
- package/dist/modules/erc7710/index.d.ts +8 -5
- package/dist/modules/erc7710/index.js +1111 -34
- package/dist/modules/evm/escrow.d.ts +125 -0
- package/dist/modules/evm/escrow.js +619 -0
- package/dist/modules/evm/forms.d.ts +39 -11
- package/dist/modules/evm/forms.js +191 -25
- package/dist/modules/evm/index.d.ts +41 -6
- package/dist/modules/evm/index.js +1135 -20
- package/dist/modules/evm/node.d.ts +16 -1
- package/dist/modules/evm/node.js +181 -26
- package/dist/modules/evm/nonce-proof.d.ts +186 -0
- package/dist/modules/evm/nonce-proof.js +779 -0
- package/dist/modules/hedera/executor.js +1 -1
- package/dist/modules/hedera/index.js +1 -1
- package/dist/modules/lnbtc/index.js +3 -2
- package/dist/modules/near/index.js +1 -1
- package/dist/modules/polkadot/index.js +1 -1
- package/dist/modules/solana/index.d.ts +3 -0
- package/dist/modules/solana/index.js +119 -10
- package/dist/modules/solana/reader.d.ts +2 -1
- package/dist/modules/solana/reader.js +42 -4
- package/dist/modules/solana/upto-claim.d.ts +58 -0
- package/dist/modules/solana/upto-claim.js +269 -0
- package/dist/modules/starknet/index.js +9 -2
- package/dist/modules/stellar/index.d.ts +5 -0
- package/dist/modules/stellar/index.js +18 -8
- package/dist/modules/stellar/node.d.ts +7 -1
- package/dist/modules/stellar/node.js +51 -7
- package/dist/modules/sui/index.js +66 -20
- package/dist/modules/sui/node.d.ts +13 -0
- package/dist/modules/sui/node.js +60 -0
- package/dist/modules/ton/index.js +6 -2
- package/dist/modules/tron/index.js +1 -1
- package/dist/modules/xrpl/index.js +61 -12
- package/dist/modules/xrpl/node.d.ts +31 -2
- package/dist/modules/xrpl/node.js +88 -17
- package/dist/modules/xrpl/upto.js +106 -20
- package/dist/record.d.ts +52 -8
- package/dist/record.js +39 -5
- package/dist/wrapper.d.ts +7 -5
- package/dist/wrapper.js +7 -5
- package/package.json +4 -3
|
@@ -30,6 +30,13 @@ export interface Latest {
|
|
|
30
30
|
epoch: bigint;
|
|
31
31
|
referenceGasPrice: bigint;
|
|
32
32
|
}
|
|
33
|
+
/** An owned or immutable object a transaction names at one version, as its signed bytes give it. */
|
|
34
|
+
export interface ObjectRef {
|
|
35
|
+
objectId: string;
|
|
36
|
+
version: bigint;
|
|
37
|
+
/** Base58, as the bytes and the node state it. */
|
|
38
|
+
digest: string;
|
|
39
|
+
}
|
|
33
40
|
export interface SuiNode {
|
|
34
41
|
readonly network: SuiNetwork;
|
|
35
42
|
/** The transaction with `digest`, or null where the node holds none, and the newest checkpoint. */
|
|
@@ -37,6 +44,12 @@ export interface SuiNode {
|
|
|
37
44
|
executed: Executed | null;
|
|
38
45
|
latest: Latest;
|
|
39
46
|
}>;
|
|
47
|
+
/**
|
|
48
|
+
* The first of `refs` the node can no longer take as an input, or null where it shows none: the node holds the
|
|
49
|
+
* version named and that version's digest is another, or the object is now at a later version or no longer live
|
|
50
|
+
* (deleted or wrapped). An object or version the node's index does not yet hold shows nothing.
|
|
51
|
+
*/
|
|
52
|
+
unavailable(refs: readonly ObjectRef[], deadline: number): Promise<ObjectRef | null>;
|
|
40
53
|
/** Simulates the transaction with Sui's checks, its signatures apart. */
|
|
41
54
|
simulate(bytes: Uint8Array, deadline: number): Promise<Effects>;
|
|
42
55
|
/** Executes the transaction with its signatures; its effects, the checkpoint where the node states one. */
|
package/dist/modules/sui/node.js
CHANGED
|
@@ -14,6 +14,34 @@ export class GraphqlFault extends Error {
|
|
|
14
14
|
this.messages = messages;
|
|
15
15
|
}
|
|
16
16
|
}
|
|
17
|
+
/**
|
|
18
|
+
* The most bytes one objects request's body carries. Sui GraphQL's `maxQueryPayloadSize`, 5000 on the public testnet and
|
|
19
|
+
* mainnet endpoints, counts the whole body, variables included: about 26 references fit, and a wallet may pay gas with up
|
|
20
|
+
* to 256 coins. Under it, each request is far below `maxMultiGetSize` (200).
|
|
21
|
+
*/
|
|
22
|
+
const OBJECTS_PAYLOAD_BYTES = 4800;
|
|
23
|
+
const OBJECTS_QUERY = `query($at: [ObjectKey!]!, $now: [ObjectKey!]!) { at: multiGetObjects(keys: $at) { version digest } now: multiGetObjects(keys: $now) { version } }`;
|
|
24
|
+
const objectsBody = (part) => ({
|
|
25
|
+
at: part.map((r) => ({ address: r.objectId, version: Number(r.version) })),
|
|
26
|
+
now: part.map((r) => ({ address: r.objectId })),
|
|
27
|
+
});
|
|
28
|
+
/** `refs` in order, in parts whose request body stays within `OBJECTS_PAYLOAD_BYTES`. */
|
|
29
|
+
function objectParts(refs) {
|
|
30
|
+
const parts = [];
|
|
31
|
+
let part = [];
|
|
32
|
+
for (const ref of refs) {
|
|
33
|
+
const next = [...part, ref];
|
|
34
|
+
if (part.length > 0 && JSON.stringify({ query: OBJECTS_QUERY, variables: objectsBody(next) }).length > OBJECTS_PAYLOAD_BYTES) {
|
|
35
|
+
parts.push(part);
|
|
36
|
+
part = [ref];
|
|
37
|
+
}
|
|
38
|
+
else
|
|
39
|
+
part = next;
|
|
40
|
+
}
|
|
41
|
+
if (part.length > 0)
|
|
42
|
+
parts.push(part);
|
|
43
|
+
return parts;
|
|
44
|
+
}
|
|
17
45
|
const EFFECTS = `status checkpoint { sequenceNumber } balanceChangesJson
|
|
18
46
|
gasEffects { gasSummary { computationCost storageCost storageRebate } }`;
|
|
19
47
|
const LATEST = `checkpoint { sequenceNumber epoch { epochId referenceGasPrice } }`;
|
|
@@ -116,6 +144,38 @@ export function suiNode(network, url) {
|
|
|
116
144
|
}
|
|
117
145
|
return { executed, latest: latestOf(data) };
|
|
118
146
|
},
|
|
147
|
+
async unavailable(refs, deadline) {
|
|
148
|
+
// A version past 2^53 - 1 is beyond the schema's UInt53: such a reference shows nothing.
|
|
149
|
+
const named = refs.filter((r) => r.version <= BigInt(Number.MAX_SAFE_INTEGER));
|
|
150
|
+
for (const part of objectParts(named)) {
|
|
151
|
+
// One request reads both lists from one view of the index: the version named, and the object's latest.
|
|
152
|
+
const data = await query(OBJECTS_QUERY, objectsBody(part), deadline);
|
|
153
|
+
const at = data["at"], now = data["now"];
|
|
154
|
+
if (!Array.isArray(at) || !Array.isArray(now) || at.length !== part.length || now.length !== part.length)
|
|
155
|
+
throw malformed("multiGetObjects");
|
|
156
|
+
for (const [k, ref] of part.entries()) {
|
|
157
|
+
const held = at[k], latest = now[k];
|
|
158
|
+
// A latest version past the one named is decisive alone: an object's version only increases, so the version
|
|
159
|
+
// named can never be taken, whether or not the index still holds it.
|
|
160
|
+
if (latest !== null) {
|
|
161
|
+
if (!isObject(latest))
|
|
162
|
+
throw malformed("object");
|
|
163
|
+
if (u53(latest["version"], "version") > ref.version)
|
|
164
|
+
return ref;
|
|
165
|
+
}
|
|
166
|
+
// The version named is not in the index (not yet, never or pruned): nothing more is shown.
|
|
167
|
+
if (held === null)
|
|
168
|
+
continue;
|
|
169
|
+
if (!isObject(held) || typeof held["digest"] !== "string")
|
|
170
|
+
throw malformed("object");
|
|
171
|
+
if (held["digest"] !== ref.digest)
|
|
172
|
+
return ref;
|
|
173
|
+
if (latest === null)
|
|
174
|
+
return ref;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
return null;
|
|
178
|
+
},
|
|
119
179
|
async simulate(bytes, deadline) {
|
|
120
180
|
const data = await query(`query($t: JSON!) { simulateTransaction(transaction: $t, checksEnabled: true) { effects { ${EFFECTS} } } }`, { t: { bcs: { value: toBase64(bytes) } } }, deadline);
|
|
121
181
|
const r = data["simulateTransaction"];
|
|
@@ -30,7 +30,7 @@ export function tonModule(c) {
|
|
|
30
30
|
if (c.reader.network !== c.network)
|
|
31
31
|
throw new TypeError(`the TON reader serves ${c.reader.network}, not ${c.network}`);
|
|
32
32
|
return {
|
|
33
|
-
descriptor: { name: "ton", key: "relayer", pairings: [exactTvm.id], acceptsZero:
|
|
33
|
+
descriptor: { name: "ton", key: "relayer", pairings: [exactTvm.id], acceptsZero: [exactTvm.id] },
|
|
34
34
|
network,
|
|
35
35
|
facilitator: new ExactTvmScheme(c.signer),
|
|
36
36
|
record: {
|
|
@@ -46,12 +46,16 @@ export function tonModule(c) {
|
|
|
46
46
|
keep: { until: "lapse", at: ref.validUntil * 1000 },
|
|
47
47
|
};
|
|
48
48
|
},
|
|
49
|
+
/**
|
|
50
|
+
* lcp's read, made by the request the payment presents (`byRequest`): the payer's signed W5 request names it before
|
|
51
|
+
* anything is sent, by its body hash, so the read never settles on another payment of the payer under H.
|
|
52
|
+
*/
|
|
49
53
|
async read(p, at) {
|
|
50
54
|
const ref = await checked(p);
|
|
51
55
|
if (isRefusal(ref))
|
|
52
56
|
return outcomeOf({ state: "unreadable", reason: "unparseable", foreign: [] });
|
|
53
57
|
const search = at.search;
|
|
54
|
-
return outcomeOf(await exactTvm.status(ref, c.reader, { act: "payment", finality: "finalized", ...(search === undefined ? {} : { search }) }));
|
|
58
|
+
return outcomeOf(await exactTvm.status({ ...ref, byRequest: true }, c.reader, { act: "payment", finality: "finalized", ...(search === undefined ? {} : { search }) }));
|
|
55
59
|
},
|
|
56
60
|
},
|
|
57
61
|
fees: {
|
|
@@ -67,7 +67,7 @@ export function tronModules(rail, log) {
|
|
|
67
67
|
pairings: [exactTronMemo.id],
|
|
68
68
|
// The profile's floor is 1 base unit until a run shows a contract's transfer(to, 0) succeeding with a Transfer
|
|
69
69
|
// log (rule 1); no contract is so shown, so no payment of zero is settled.
|
|
70
|
-
acceptsZero:
|
|
70
|
+
acceptsZero: [],
|
|
71
71
|
},
|
|
72
72
|
network,
|
|
73
73
|
facilitator,
|
|
@@ -13,6 +13,7 @@
|
|
|
13
13
|
*/
|
|
14
14
|
import { isRefusal } from "@integraledger/lcp";
|
|
15
15
|
import { exactXrpl } from "@integraledger/lcp/x402-exact-xrpl";
|
|
16
|
+
import { compareDecimalStrings, getMaxLastLedgerSequence, isDecimalString, isIssuedCurrencyAmount, isValidDestinationTag } from "@x402/xrpl";
|
|
16
17
|
import { ExactXrplScheme } from "@x402/xrpl/exact/facilitator";
|
|
17
18
|
import { decode, deriveAddress, encodeForMultiSigning, verifyKeypairSignature } from "xrpl";
|
|
18
19
|
import { isObject } from "../../answers.js";
|
|
@@ -136,10 +137,36 @@ function amountMatches(tx, r) {
|
|
|
136
137
|
amount["value"] === r.amount);
|
|
137
138
|
}
|
|
138
139
|
/**
|
|
139
|
-
* The
|
|
140
|
-
*
|
|
141
|
-
*
|
|
142
|
-
|
|
140
|
+
* The `SendMax` rule the class makes of a payment signed with one key (`@x402/xrpl` 2.31.0, `verifyXrpAmount` and
|
|
141
|
+
* `verifyIouAmount`), with its reasons: none for XRP; for an issued currency, required, of the Amount's currency and
|
|
142
|
+
* issuer, and no less than the Amount, which the payer may spend to deliver it (a transfer fee). Undefined: it holds.
|
|
143
|
+
*/
|
|
144
|
+
function sendMaxFailure(tx, r) {
|
|
145
|
+
const sendMax = tx["SendMax"];
|
|
146
|
+
if (r.asset === "XRP")
|
|
147
|
+
return sendMax === undefined ? undefined : "invalid_exact_xrpl_payload_sendmax_not_allowed";
|
|
148
|
+
const amount = tx["Amount"];
|
|
149
|
+
if (!isIssuedCurrencyAmount(sendMax) || !isIssuedCurrencyAmount(amount))
|
|
150
|
+
return "invalid_exact_xrpl_payload_sendmax_required";
|
|
151
|
+
if (sendMax.currency !== amount.currency || sendMax.issuer !== amount.issuer)
|
|
152
|
+
return "invalid_exact_xrpl_payload_sendmax_iou_mismatch";
|
|
153
|
+
// The class's comparison throws on a value that is no plain decimal, a negative one among them: refused as too low.
|
|
154
|
+
if (!isDecimalString(sendMax.value) || !isDecimalString(amount.value) || compareDecimalStrings(sendMax.value, amount.value) < 0) {
|
|
155
|
+
return "invalid_exact_xrpl_payload_sendmax_too_low";
|
|
156
|
+
}
|
|
157
|
+
return undefined;
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* The checks of a multisigned payment that read the chain, after lcp's reference of it: a `Payment` with an empty
|
|
161
|
+
* `SigningPubKey`; its `Destination`, `DestinationTag` and `Amount` against the requirements, the tag by the class's
|
|
162
|
+
* rule; `SendMax`, then `Paths`, `DeliverMin` and the partial-payment flag, by the class's rules and in its order; the
|
|
163
|
+
* fee against `maxFeeDrops`; each signer's signature over the transaction's multisigning data with its own key, a key
|
|
164
|
+
* that is its master key or its regular key; the payer's signer list met by those signers' weights; its
|
|
165
|
+
* LastLedgerSequence past the validated ledger and within the requirements' timeout, by the class's rule; its Sequence
|
|
166
|
+
* or ticket unconsumed; and the server's simulation. These are not every form check the class makes for a payment
|
|
167
|
+
* signed with one key: its NetworkID, InvoiceID and sequencing-field checks are not made here as the class makes
|
|
168
|
+
* them, so a payment that fails one is refused with another reason (lcp's reference, the ticket or sequence read, or
|
|
169
|
+
* the simulation). Nothing is submitted unless every check passes. The payer, or the refusal.
|
|
143
170
|
*/
|
|
144
171
|
async function multisigChecks(c, node, payload, r) {
|
|
145
172
|
const ref = await pure(c.network, payload, r);
|
|
@@ -153,13 +180,27 @@ async function multisigChecks(c, node, payload, r) {
|
|
|
153
180
|
return { reason: "invalid_exact_xrpl_payload_signing_pub_key" };
|
|
154
181
|
if (tx["Destination"] !== r.payTo)
|
|
155
182
|
return { reason: "invalid_exact_xrpl_payload_destination_mismatch" };
|
|
183
|
+
// The class's destination-tag rule (`@x402/xrpl` 2.31.0, `verifyPaymentTransactionStructure`): where the requirements
|
|
184
|
+
// name a tag, it is a 32-bit unsigned integer and the Payment carries that tag; where they name none, any tag passes.
|
|
185
|
+
const tag = r.extra?.["destinationTag"];
|
|
186
|
+
if (tag !== undefined) {
|
|
187
|
+
if (!isValidDestinationTag(tag))
|
|
188
|
+
return { reason: "invalid_exact_xrpl_destination_tag_malformed" };
|
|
189
|
+
if (tx["DestinationTag"] !== tag)
|
|
190
|
+
return { reason: "invalid_exact_xrpl_payload_destination_tag_mismatch" };
|
|
191
|
+
}
|
|
156
192
|
if (!amountMatches(tx, r))
|
|
157
193
|
return { reason: "invalid_exact_xrpl_payload_amount_mismatch" };
|
|
194
|
+
const sendMax = sendMaxFailure(tx, r);
|
|
195
|
+
if (sendMax !== undefined)
|
|
196
|
+
return { reason: sendMax };
|
|
197
|
+
if (tx["Paths"] !== undefined)
|
|
198
|
+
return { reason: "invalid_exact_xrpl_payload_paths_not_allowed" };
|
|
199
|
+
if (tx["DeliverMin"] !== undefined)
|
|
200
|
+
return { reason: "invalid_exact_xrpl_payload_delivermin_not_allowed" };
|
|
201
|
+
// The class's order: SendMax, Paths, DeliverMin, then the partial-payment flag.
|
|
158
202
|
if (((Number(tx["Flags"] ?? 0) >>> 0) & PARTIAL_PAYMENT) !== 0)
|
|
159
203
|
return { reason: "invalid_exact_xrpl_payload_partial_payment_not_allowed" };
|
|
160
|
-
if (tx["SendMax"] !== undefined || tx["Paths"] !== undefined || tx["DeliverMin"] !== undefined) {
|
|
161
|
-
return { reason: "invalid_exact_xrpl_payload_paths_not_allowed" };
|
|
162
|
-
}
|
|
163
204
|
if (typeof tx["Fee"] !== "string" || BigInt(tx["Fee"]) > BigInt(c.maxFeeDrops ?? DEFAULT_MAX_FEE_DROPS)) {
|
|
164
205
|
return { reason: "invalid_exact_xrpl_payload_fee_too_high" };
|
|
165
206
|
}
|
|
@@ -184,21 +225,29 @@ async function multisigChecks(c, node, payload, r) {
|
|
|
184
225
|
const data = encodeForMultiSigning(tx, who);
|
|
185
226
|
if (!verifyKeypairSignature(data, sig, key))
|
|
186
227
|
return { reason: "invalid_exact_xrpl_payload_signature" };
|
|
228
|
+
// rippled's rule for each signer: a key that is the signer's regular key, or its master key where the master key is
|
|
229
|
+
// not disabled; a signer with no account signs with its master key, the key its address derives from.
|
|
187
230
|
const derived = deriveAddress(key);
|
|
231
|
+
const auth = await node.signerAuthorization(who);
|
|
188
232
|
if (derived !== who) {
|
|
189
|
-
|
|
190
|
-
if (auth.regularKey !== derived)
|
|
233
|
+
if (auth === null || auth.regularKey !== derived)
|
|
191
234
|
return { reason: "invalid_exact_xrpl_payload_signer_not_authorized" };
|
|
192
235
|
}
|
|
193
|
-
else if (
|
|
236
|
+
else if (auth !== null && auth.isMasterKeyDisabled) {
|
|
194
237
|
return { reason: "invalid_exact_xrpl_payload_signer_not_authorized" };
|
|
195
238
|
}
|
|
196
239
|
weight += list.weights.get(who) ?? 0;
|
|
197
240
|
}
|
|
198
241
|
if (weight < list.quorum)
|
|
199
242
|
return { reason: "invalid_exact_xrpl_payload_signer_not_authorized" };
|
|
200
|
-
|
|
243
|
+
// The class's rule for a payment signed with one key (`@x402/xrpl` 2.31.0, `verifyLedgerExpiry`), against the
|
|
244
|
+
// validated ledger: a LastLedgerSequence that ledger has reached has expired, and one past the ledgers the
|
|
245
|
+
// requirements' `maxTimeoutSeconds` spans, at the class's close time and tolerance, is too large.
|
|
246
|
+
const current = await node.validatedLedger();
|
|
247
|
+
if (last <= current)
|
|
201
248
|
return { reason: "invalid_exact_xrpl_payload_expired" };
|
|
249
|
+
if (last > getMaxLastLedgerSequence(current, r))
|
|
250
|
+
return { reason: "invalid_exact_xrpl_payload_lastledgersequence_too_large" };
|
|
202
251
|
const ticket = tx["TicketSequence"];
|
|
203
252
|
if (typeof ticket === "number") {
|
|
204
253
|
if (!(await node.ticket(account, ticket)))
|
|
@@ -240,7 +289,7 @@ function multisignedModule(c, node) {
|
|
|
240
289
|
}
|
|
241
290
|
/** One kind for both paths: x402/exact/xrpl, no key; zero is no Payment (a Payment of zero is malformed). */
|
|
242
291
|
function descriptor() {
|
|
243
|
-
return { name: "xrpl", key: "none", pairings: [exactXrpl.id], acceptsZero:
|
|
292
|
+
return { name: "xrpl", key: "none", pairings: [exactXrpl.id], acceptsZero: [] };
|
|
244
293
|
}
|
|
245
294
|
/** The published class with the module's pure checks before it on both calls. */
|
|
246
295
|
function behind(cls, network) {
|
|
@@ -11,6 +11,17 @@ export declare function xrplRpc(url: string, method: string, params: Record<stri
|
|
|
11
11
|
export declare function xrplReader(network: XrplNetwork, url: string, deadline: number): XrplReader;
|
|
12
12
|
/** The index of the server's latest validated ledger, its call ending by `deadline`. */
|
|
13
13
|
export declare function validatedLedger(url: string, deadline: number): Promise<number>;
|
|
14
|
+
/**
|
|
15
|
+
* What a transaction's metadata shows of each payment channel it modified or deleted, by the channel's id in upper
|
|
16
|
+
* case: its `Balance`, the drops the channel has delivered to its destination, before the transaction and after it. A
|
|
17
|
+
* channel whose `Balance` the transaction left unchanged has no `PreviousFields.Balance`, and reads the same on both.
|
|
18
|
+
*/
|
|
19
|
+
export type ChannelBalances = ReadonlyMap<string, {
|
|
20
|
+
readonly before: bigint;
|
|
21
|
+
readonly after: bigint;
|
|
22
|
+
}>;
|
|
23
|
+
/** The channel balances of a JSON (not binary) transaction answer's `meta`, as `ChannelBalances` states them. */
|
|
24
|
+
export declare function channelBalancesOf(meta: Record<string, unknown>): ChannelBalances;
|
|
14
25
|
/** What the class and the own module read of the payer's account and the ledger, and how they submit. */
|
|
15
26
|
export interface XrplNode {
|
|
16
27
|
validatedLedger(): Promise<number>;
|
|
@@ -19,6 +30,14 @@ export interface XrplNode {
|
|
|
19
30
|
regularKey?: string;
|
|
20
31
|
isMasterKeyDisabled: boolean;
|
|
21
32
|
}>;
|
|
33
|
+
/**
|
|
34
|
+
* A signer's account at the validated ledger as `authorization` reads it, or null where no such account exists: a
|
|
35
|
+
* signer list's entries need not be funded accounts, and such a signer signs with the key its address derives from.
|
|
36
|
+
*/
|
|
37
|
+
signerAuthorization(account: string): Promise<{
|
|
38
|
+
regularKey?: string;
|
|
39
|
+
isMasterKeyDisabled: boolean;
|
|
40
|
+
} | null>;
|
|
22
41
|
ticket(account: string, ticketSequence: number): Promise<boolean>;
|
|
23
42
|
/** The account's signer list at the validated ledger: its quorum and each signer's weight, or none. */
|
|
24
43
|
signerList(account: string): Promise<{
|
|
@@ -29,11 +48,15 @@ export interface XrplNode {
|
|
|
29
48
|
engineResult: string;
|
|
30
49
|
engineResultMessage?: string;
|
|
31
50
|
}>;
|
|
32
|
-
/**
|
|
51
|
+
/**
|
|
52
|
+
* Submits the signed blob and waits up to `waitMs` for it in a validated ledger; once validated, with the channel
|
|
53
|
+
* balances its metadata shows.
|
|
54
|
+
*/
|
|
33
55
|
submit(blob: string): Promise<{
|
|
34
56
|
hash: string;
|
|
35
57
|
validated: boolean;
|
|
36
58
|
resultCode: string;
|
|
59
|
+
channels?: ChannelBalances;
|
|
37
60
|
}>;
|
|
38
61
|
/** The `PayChannel` ledger entry `id` names at the validated ledger, its fields as the server gives them, or none. */
|
|
39
62
|
payChannel(id: string): Promise<Record<string, unknown> | null>;
|
|
@@ -42,10 +65,16 @@ export interface XrplNode {
|
|
|
42
65
|
index: number;
|
|
43
66
|
closeTime: number;
|
|
44
67
|
}>;
|
|
45
|
-
/**
|
|
68
|
+
/**
|
|
69
|
+
* A transaction by its hash: whether a validated ledger holds it, its result, the channel balances its metadata
|
|
70
|
+
* shows, and the `Balance` the transaction itself carries, where it carries one (a channel claim's); none where the
|
|
71
|
+
* server holds none.
|
|
72
|
+
*/
|
|
46
73
|
transaction(hash: string): Promise<{
|
|
47
74
|
validated: boolean;
|
|
48
75
|
resultCode: string;
|
|
76
|
+
channels?: ChannelBalances;
|
|
77
|
+
balance?: string;
|
|
49
78
|
} | null>;
|
|
50
79
|
}
|
|
51
80
|
/** The node calls over the server at `url`, each bounded by the node bounds; a submission waits up to `waitMs`. */
|
|
@@ -12,11 +12,41 @@ function failedOf(method, r) {
|
|
|
12
12
|
throw new NodeError("malformed", true, `${method}: ${String(r["error"] ?? "an answer without its members")}`);
|
|
13
13
|
}
|
|
14
14
|
const int = (v) => (typeof v === "number" && Number.isSafeInteger(v) && v >= 0 ? v : undefined);
|
|
15
|
-
/**
|
|
15
|
+
/**
|
|
16
|
+
* The first of `names` that `t` holds as a string. rippled names a binary answer's members by API version and by method:
|
|
17
|
+
* `tx` answers `tx` and `meta` on API version 1, `tx_blob` and `meta_blob` on version 2; `account_tx` answers `tx_blob`
|
|
18
|
+
* and `meta` on version 1, `tx_blob` and `meta_blob` on version 2 (rippled 3.1.3; xrpld 3.3.0).
|
|
19
|
+
*/
|
|
20
|
+
const hexOf = (t, ...names) => names.map((n) => t[n]).find((v) => typeof v === "string");
|
|
21
|
+
/** A Payment's flag that lets it deliver less than its `Amount` (`tfPartialPayment`). */
|
|
22
|
+
const PARTIAL_PAYMENT = 0x00020000;
|
|
23
|
+
/**
|
|
24
|
+
* What a successful Payment delivered where its metadata names no amount: its `Amount`, unless it is a partial payment.
|
|
25
|
+
* A binary answer's metadata never holds `delivered_amount`, which rippled adds to a JSON answer only, and holds
|
|
26
|
+
* `DeliveredAmount` for a partial payment only; a Payment that is not partial delivers its `Amount` exactly or fails
|
|
27
|
+
* (xrpl.org, "Partial Payments", "The delivered_amount Field"). A partial payment with no `DeliveredAmount` names none.
|
|
28
|
+
*/
|
|
29
|
+
function deliveredOf(blob, result) {
|
|
30
|
+
if (result !== "tesSUCCESS")
|
|
31
|
+
return undefined;
|
|
32
|
+
let tx;
|
|
33
|
+
try {
|
|
34
|
+
tx = decode(blob);
|
|
35
|
+
}
|
|
36
|
+
catch {
|
|
37
|
+
// A transaction the codec cannot decode names nothing delivered here; lcp reads its blob as it reads any other.
|
|
38
|
+
return undefined;
|
|
39
|
+
}
|
|
40
|
+
if (tx["TransactionType"] !== "Payment")
|
|
41
|
+
return undefined;
|
|
42
|
+
const flags = typeof tx["Flags"] === "number" ? tx["Flags"] : 0;
|
|
43
|
+
return (flags & PARTIAL_PAYMENT) === 0 ? tx["Amount"] : undefined;
|
|
44
|
+
}
|
|
45
|
+
/** A landed transaction from its binary transaction and metadata, in any of the forms rippled answers. */
|
|
16
46
|
function landedOf(t, method) {
|
|
17
|
-
const blob = t
|
|
18
|
-
const metaHex = t
|
|
19
|
-
if (
|
|
47
|
+
const blob = hexOf(t, "tx_blob", "tx");
|
|
48
|
+
const metaHex = hexOf(t, "meta_blob", "meta");
|
|
49
|
+
if (blob === undefined || metaHex === undefined)
|
|
20
50
|
return failedOf(method, t);
|
|
21
51
|
const meta = decode(metaHex);
|
|
22
52
|
const result = meta["TransactionResult"];
|
|
@@ -33,7 +63,7 @@ function landedOf(t, method) {
|
|
|
33
63
|
out.push(node["LedgerIndex"]);
|
|
34
64
|
}
|
|
35
65
|
}
|
|
36
|
-
const delivered = meta["delivered_amount"] ?? meta["DeliveredAmount"];
|
|
66
|
+
const delivered = meta["delivered_amount"] ?? meta["DeliveredAmount"] ?? deliveredOf(blob, result);
|
|
37
67
|
const index = int(t["ledger_index"]);
|
|
38
68
|
const parent = meta["ParentBatchID"];
|
|
39
69
|
return {
|
|
@@ -58,12 +88,12 @@ export function xrplReader(network, url, deadline) {
|
|
|
58
88
|
const r = await call("server_info", {});
|
|
59
89
|
const info = isObject(r["info"]) ? r["info"] : failedOf("server_info", r);
|
|
60
90
|
const validated = isObject(info["validated_ledger"]) ? int(info["validated_ledger"]["seq"]) : undefined;
|
|
61
|
-
|
|
62
|
-
const latest = int(current["ledger_current_index"]);
|
|
63
|
-
if (validated === undefined || latest === undefined || typeof info["complete_ledgers"] !== "string")
|
|
91
|
+
if (validated === undefined || typeof info["complete_ledgers"] !== "string")
|
|
64
92
|
return failedOf("server_info", r);
|
|
65
93
|
const id = int(info["network_id"]);
|
|
66
|
-
|
|
94
|
+
// lcp's read takes `validated`: every search of `account_tx` ends there, since `account_tx` reads validated ledgers
|
|
95
|
+
// only.
|
|
96
|
+
return { validated, completeLedgers: info["complete_ledgers"], ...(id === undefined ? {} : { networkId: id }) };
|
|
67
97
|
},
|
|
68
98
|
async tx(hash, range) {
|
|
69
99
|
const r = await call("tx", { transaction: hash, binary: true, ...(range ? { min_ledger: range.min, max_ledger: range.max } : {}) });
|
|
@@ -71,6 +101,11 @@ export function xrplReader(network, url, deadline) {
|
|
|
71
101
|
return { notFound: true, searchedAll: r["searched_all"] === true };
|
|
72
102
|
if (r["error"] !== undefined)
|
|
73
103
|
return failedOf("tx", r);
|
|
104
|
+
// A transaction the server knows that no validated ledger holds, answered with no metadata: a submission rippled
|
|
105
|
+
// refused and keeps in its cache (`tefPAST_SEQ`, `tefMAX_LEDGER`), or one no closed ledger holds yet. lcp's reader
|
|
106
|
+
// contract reads it as not found, its search not whole.
|
|
107
|
+
if (r["validated"] !== true && hexOf(r, "meta_blob", "meta") === undefined)
|
|
108
|
+
return { notFound: true, searchedAll: false };
|
|
74
109
|
return landedOf({ ...r, hash: r["hash"] ?? hash }, "tx");
|
|
75
110
|
},
|
|
76
111
|
async accountTx(account, range, marker) {
|
|
@@ -127,6 +162,32 @@ export async function validatedLedger(url, deadline) {
|
|
|
127
162
|
}
|
|
128
163
|
/** The account's flag that disables its master key (`lsfDisableMaster`). */
|
|
129
164
|
const DISABLE_MASTER = 0x00100000;
|
|
165
|
+
/** An account's regular key and whether its master key is disabled, from its `account_data`. */
|
|
166
|
+
function authorizationOf(data) {
|
|
167
|
+
const flags = int(data["Flags"]) ?? 0;
|
|
168
|
+
return {
|
|
169
|
+
...(typeof data["RegularKey"] === "string" ? { regularKey: data["RegularKey"] } : {}),
|
|
170
|
+
isMasterKeyDisabled: (flags & DISABLE_MASTER) !== 0,
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
const DROPS = /^[0-9]{1,19}$/;
|
|
174
|
+
/** The channel balances of a JSON (not binary) transaction answer's `meta`, as `ChannelBalances` states them. */
|
|
175
|
+
export function channelBalancesOf(meta) {
|
|
176
|
+
const out = new Map();
|
|
177
|
+
for (const n of Array.isArray(meta["AffectedNodes"]) ? meta["AffectedNodes"] : []) {
|
|
178
|
+
if (!isObject(n))
|
|
179
|
+
continue;
|
|
180
|
+
const node = isObject(n["ModifiedNode"]) ? n["ModifiedNode"] : isObject(n["DeletedNode"]) ? n["DeletedNode"] : undefined;
|
|
181
|
+
if (node === undefined || node["LedgerEntryType"] !== "PayChannel" || typeof node["LedgerIndex"] !== "string")
|
|
182
|
+
continue;
|
|
183
|
+
const final = isObject(node["FinalFields"]) ? node["FinalFields"]["Balance"] : undefined;
|
|
184
|
+
const previous = isObject(node["PreviousFields"]) ? (node["PreviousFields"]["Balance"] ?? final) : final;
|
|
185
|
+
if (typeof final !== "string" || typeof previous !== "string" || !DROPS.test(final) || !DROPS.test(previous))
|
|
186
|
+
continue;
|
|
187
|
+
out.set(node["LedgerIndex"].toUpperCase(), { before: BigInt(previous), after: BigInt(final) });
|
|
188
|
+
}
|
|
189
|
+
return out;
|
|
190
|
+
}
|
|
130
191
|
/** The node calls over the server at `url`, each bounded by the node bounds; a submission waits up to `waitMs`. */
|
|
131
192
|
export function xrplNode(url, waitMs) {
|
|
132
193
|
const call = (method, params) => xrplRpc(url, method, params, performance.now() + 5_000);
|
|
@@ -143,12 +204,13 @@ export function xrplNode(url, waitMs) {
|
|
|
143
204
|
return int((await accountData(account, "current"))["Sequence"]) ?? failedOf("account_info", {});
|
|
144
205
|
},
|
|
145
206
|
async authorization(account) {
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
207
|
+
return authorizationOf(await accountData(account, "validated"));
|
|
208
|
+
},
|
|
209
|
+
async signerAuthorization(account) {
|
|
210
|
+
const r = await call("account_info", { account, ledger_index: "validated" });
|
|
211
|
+
if (r["error"] === "actNotFound")
|
|
212
|
+
return null;
|
|
213
|
+
return authorizationOf(isObject(r["account_data"]) ? r["account_data"] : failedOf("account_info", r));
|
|
152
214
|
},
|
|
153
215
|
async ticket(account, ticketSequence) {
|
|
154
216
|
const r = await call("ledger_entry", { ticket: { account, ticket_seq: ticketSequence }, ledger_index: "validated" });
|
|
@@ -210,7 +272,16 @@ export function xrplNode(url, waitMs) {
|
|
|
210
272
|
const meta = r["meta"];
|
|
211
273
|
if (!isObject(meta) || typeof meta["TransactionResult"] !== "string")
|
|
212
274
|
return { validated: false, resultCode: "" };
|
|
213
|
-
|
|
275
|
+
// The transaction's fields: at the answer's top level on API version 1, which a request naming no `api_version`
|
|
276
|
+
// gets, and in `tx_json` on version 2.
|
|
277
|
+
const tx = isObject(r["tx_json"]) ? r["tx_json"] : r;
|
|
278
|
+
const balance = typeof tx["Balance"] === "string" ? tx["Balance"] : undefined;
|
|
279
|
+
return {
|
|
280
|
+
validated: r["validated"] === true,
|
|
281
|
+
resultCode: meta["TransactionResult"],
|
|
282
|
+
channels: channelBalancesOf(meta),
|
|
283
|
+
...(balance === undefined ? {} : { balance }),
|
|
284
|
+
};
|
|
214
285
|
},
|
|
215
286
|
async submit(blob) {
|
|
216
287
|
const sent = await call("submit", { tx_blob: blob });
|
|
@@ -220,7 +291,7 @@ export function xrplNode(url, waitMs) {
|
|
|
220
291
|
for (;;) {
|
|
221
292
|
const r = await call("tx", { transaction: hash });
|
|
222
293
|
if (r["validated"] === true && isObject(r["meta"]) && typeof r["meta"]["TransactionResult"] === "string") {
|
|
223
|
-
return { hash, validated: true, resultCode: r["meta"]["TransactionResult"] };
|
|
294
|
+
return { hash, validated: true, resultCode: r["meta"]["TransactionResult"], channels: channelBalancesOf(r["meta"]) };
|
|
224
295
|
}
|
|
225
296
|
if (performance.now() + 1_000 > until)
|
|
226
297
|
return { hash, validated: false, resultCode: engine };
|