@integraledger/profile-facilitator 0.3.1 → 0.5.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/README.md +642 -456
- package/dist/answers.d.ts +11 -4
- package/dist/answers.js +24 -0
- package/dist/core.d.ts +51 -0
- package/dist/core.js +758 -0
- package/dist/http.d.ts +20 -3
- package/dist/http.js +32 -7
- package/dist/index.d.ts +12 -36
- package/dist/index.js +9 -162
- package/dist/module.d.ts +372 -0
- package/dist/module.js +13 -0
- package/dist/modules/algorand/index.d.ts +22 -0
- package/dist/modules/algorand/index.js +247 -0
- package/dist/modules/algorand/own.d.ts +26 -0
- package/dist/modules/algorand/own.js +231 -0
- package/dist/modules/aptos/index.d.ts +23 -0
- package/dist/modules/aptos/index.js +232 -0
- package/dist/modules/cardano/index.d.ts +18 -0
- package/dist/modules/cardano/index.js +124 -0
- package/dist/modules/casper/index.d.ts +120 -0
- package/dist/modules/casper/index.js +442 -0
- package/dist/modules/concordium/index.d.ts +16 -0
- package/dist/modules/concordium/index.js +77 -0
- package/dist/modules/erc7710/index.d.ts +26 -0
- package/dist/modules/erc7710/index.js +220 -0
- package/dist/modules/evm/forms.d.ts +74 -0
- package/dist/modules/evm/forms.js +182 -0
- package/dist/modules/evm/index.d.ts +40 -0
- package/dist/modules/evm/index.js +148 -0
- package/dist/modules/evm/node.d.ts +29 -0
- package/dist/modules/evm/node.js +222 -0
- package/dist/modules/hedera/executor.d.ts +63 -0
- package/dist/modules/hedera/executor.js +343 -0
- package/dist/modules/hedera/index.d.ts +23 -0
- package/dist/modules/hedera/index.js +136 -0
- package/dist/modules/hedera/mirror.d.ts +32 -0
- package/dist/modules/hedera/mirror.js +111 -0
- package/dist/modules/index.d.ts +23 -0
- package/dist/modules/index.js +24 -0
- package/dist/modules/lnbtc/index.d.ts +10 -0
- package/dist/modules/lnbtc/index.js +111 -0
- package/dist/modules/near/index.d.ts +18 -0
- package/dist/modules/near/index.js +246 -0
- package/dist/modules/polkadot/index.d.ts +12 -0
- package/dist/modules/polkadot/index.js +149 -0
- package/dist/modules/polkadot/rail.d.ts +40 -0
- package/dist/{polkadot.js → modules/polkadot/rail.js} +214 -288
- package/dist/modules/solana/index.d.ts +60 -0
- package/dist/modules/solana/index.js +304 -0
- package/dist/modules/solana/reader.d.ts +25 -0
- package/dist/modules/solana/reader.js +194 -0
- package/dist/modules/solana/state.d.ts +6 -0
- package/dist/modules/solana/state.js +103 -0
- package/dist/modules/starknet/index.d.ts +35 -0
- package/dist/modules/starknet/index.js +541 -0
- package/dist/modules/starknet/node.d.ts +78 -0
- package/dist/modules/starknet/node.js +339 -0
- package/dist/modules/stellar/index.d.ts +44 -0
- package/dist/modules/stellar/index.js +481 -0
- package/dist/modules/stellar/node.d.ts +57 -0
- package/dist/modules/stellar/node.js +151 -0
- package/dist/modules/sui/index.d.ts +16 -0
- package/dist/modules/sui/index.js +309 -0
- package/dist/modules/sui/node.d.ts +49 -0
- package/dist/modules/sui/node.js +197 -0
- package/dist/modules/ton/index.d.ts +16 -0
- package/dist/modules/ton/index.js +73 -0
- package/dist/modules/tron/index.d.ts +12 -0
- package/dist/modules/tron/index.js +120 -0
- package/dist/modules/tron/rail.d.ts +32 -0
- package/dist/{tron.js → modules/tron/rail.js} +206 -195
- package/dist/modules/xrpl/index.d.ts +28 -0
- package/dist/modules/xrpl/index.js +299 -0
- package/dist/modules/xrpl/node.d.ts +52 -0
- package/dist/modules/xrpl/node.js +231 -0
- package/dist/modules/xrpl/upto.d.ts +5 -0
- package/dist/modules/xrpl/upto.js +269 -0
- package/dist/record.d.ts +131 -0
- package/dist/record.js +330 -0
- package/dist/unexpected.d.ts +3 -2
- package/dist/unexpected.js +2 -1
- package/dist/wrapper.d.ts +68 -0
- package/dist/wrapper.js +126 -0
- package/package.json +47 -12
- package/dist/polkadot.d.ts +0 -21
- package/dist/store.d.ts +0 -59
- package/dist/store.js +0 -103
- package/dist/tron.d.ts +0 -12
- /package/dist/{base58check.d.ts → modules/tron/base58check.d.ts} +0 -0
- /package/dist/{base58check.js → modules/tron/base58check.js} +0 -0
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The seller's Sui node, over Sui's GraphQL RPC (the schema `@mysten/sui` 2.35.0 ships,
|
|
3
|
+
* `src/graphql/generated/schema.graphql`): the reads, the simulation and the execution the module makes, and the reader
|
|
4
|
+
* lcp's `x402/exact/sui` read takes. Every call is bounded by the node bound of `../../http.js` and by its deadline.
|
|
5
|
+
*/
|
|
6
|
+
import { fromBase64, toBase64 } from "@mysten/sui/utils";
|
|
7
|
+
import { isObject } from "../../answers.js";
|
|
8
|
+
import { NodeError, postJson } from "../../http.js";
|
|
9
|
+
/** The node answered the request with GraphQL errors: it reached the node, which refused it. */
|
|
10
|
+
export class GraphqlFault extends Error {
|
|
11
|
+
messages;
|
|
12
|
+
constructor(messages) {
|
|
13
|
+
super(messages.join("; "));
|
|
14
|
+
this.messages = messages;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
const EFFECTS = `status checkpoint { sequenceNumber } balanceChangesJson
|
|
18
|
+
gasEffects { gasSummary { computationCost storageCost storageRebate } }`;
|
|
19
|
+
const LATEST = `checkpoint { sequenceNumber epoch { epochId referenceGasPrice } }`;
|
|
20
|
+
export function suiNode(network, url) {
|
|
21
|
+
async function query(text, variables, deadline) {
|
|
22
|
+
const answer = await postJson(url, { query: text, variables }, deadline - performance.now());
|
|
23
|
+
if (!isObject(answer))
|
|
24
|
+
throw new NodeError("malformed", true, "not an object");
|
|
25
|
+
const errors = answer["errors"];
|
|
26
|
+
if (Array.isArray(errors) && errors.length > 0) {
|
|
27
|
+
throw new GraphqlFault(errors.map((e) => (isObject(e) && typeof e["message"] === "string" ? e["message"] : "error")));
|
|
28
|
+
}
|
|
29
|
+
const data = answer["data"];
|
|
30
|
+
if (!isObject(data))
|
|
31
|
+
throw new NodeError("malformed", true, "no data");
|
|
32
|
+
return data;
|
|
33
|
+
}
|
|
34
|
+
const malformed = (what) => new NodeError("malformed", true, what);
|
|
35
|
+
const u53 = (v, what) => {
|
|
36
|
+
if (typeof v === "number" && Number.isSafeInteger(v) && v >= 0)
|
|
37
|
+
return BigInt(v);
|
|
38
|
+
if (typeof v === "string" && /^[0-9]{1,20}$/.test(v))
|
|
39
|
+
return BigInt(v);
|
|
40
|
+
throw malformed(what);
|
|
41
|
+
};
|
|
42
|
+
function changesOf(json) {
|
|
43
|
+
if (!Array.isArray(json))
|
|
44
|
+
throw malformed("balanceChangesJson");
|
|
45
|
+
return json.map((c) => {
|
|
46
|
+
if (!isObject(c) || typeof c["address"] !== "string" || typeof c["coinType"] !== "string")
|
|
47
|
+
throw malformed("balance change");
|
|
48
|
+
const amount = c["amount"];
|
|
49
|
+
if (typeof amount !== "string" || !/^-?[0-9]{1,40}$/.test(amount))
|
|
50
|
+
throw malformed("balance change amount");
|
|
51
|
+
return { owner: c["address"].toLowerCase(), coinType: c["coinType"], amount: BigInt(amount) };
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
function effectsOf(e) {
|
|
55
|
+
if (!isObject(e) || (e["status"] !== "SUCCESS" && e["status"] !== "FAILURE"))
|
|
56
|
+
throw malformed("effects");
|
|
57
|
+
const cp = e["checkpoint"];
|
|
58
|
+
const gas = isObject(e["gasEffects"]) ? e["gasEffects"]["gasSummary"] : undefined;
|
|
59
|
+
if (!isObject(gas))
|
|
60
|
+
throw malformed("gasSummary");
|
|
61
|
+
const fee = u53(gas["computationCost"], "computationCost") + u53(gas["storageCost"], "storageCost") - u53(gas["storageRebate"], "storageRebate");
|
|
62
|
+
return {
|
|
63
|
+
status: e["status"],
|
|
64
|
+
checkpoint: isObject(cp) ? u53(cp["sequenceNumber"], "sequenceNumber") : null,
|
|
65
|
+
balanceChanges: changesOf(e["balanceChangesJson"]),
|
|
66
|
+
fee: fee > 0n ? fee : 0n,
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
function latestOf(data) {
|
|
70
|
+
const cp = data["checkpoint"];
|
|
71
|
+
const epoch = isObject(cp) ? cp["epoch"] : undefined;
|
|
72
|
+
if (!isObject(cp) || !isObject(epoch))
|
|
73
|
+
throw malformed("checkpoint");
|
|
74
|
+
const price = epoch["referenceGasPrice"];
|
|
75
|
+
return {
|
|
76
|
+
checkpoint: u53(cp["sequenceNumber"], "sequenceNumber"),
|
|
77
|
+
epoch: u53(epoch["epochId"], "epochId"),
|
|
78
|
+
referenceGasPrice: u53(price, "referenceGasPrice"),
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
/** A reader answer's transaction, its members renamed and left as the node gave them for lcp to judge. */
|
|
82
|
+
function looseExecuted(t) {
|
|
83
|
+
const effects = isObject(t["effects"]) ? t["effects"] : {};
|
|
84
|
+
const cp = effects["checkpoint"];
|
|
85
|
+
const json = effects["balanceChangesJson"];
|
|
86
|
+
let bcs = t["transactionBcs"];
|
|
87
|
+
if (typeof bcs === "string") {
|
|
88
|
+
try {
|
|
89
|
+
bcs = fromBase64(bcs);
|
|
90
|
+
}
|
|
91
|
+
catch {
|
|
92
|
+
// Left as given: lcp reads it as out of the reader's form.
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
return {
|
|
96
|
+
transactionBcs: bcs,
|
|
97
|
+
status: effects["status"],
|
|
98
|
+
checkpoint: isObject(cp) ? looseBig(cp["sequenceNumber"]) : cp === null || cp === undefined ? null : cp,
|
|
99
|
+
balanceChanges: Array.isArray(json)
|
|
100
|
+
? json.map((c) => isObject(c)
|
|
101
|
+
? { owner: c["address"], coinType: c["coinType"], amount: typeof c["amount"] === "string" && /^-?[0-9]{1,40}$/.test(c["amount"]) ? BigInt(c["amount"]) : c["amount"] }
|
|
102
|
+
: c)
|
|
103
|
+
: json,
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
const node = {
|
|
107
|
+
network,
|
|
108
|
+
async transaction(digest, deadline) {
|
|
109
|
+
const data = await query(`query($d: String!) { transaction(digest: $d) { transactionBcs effects { ${EFFECTS} } } ${LATEST} }`, { d: digest }, deadline);
|
|
110
|
+
const t = data["transaction"];
|
|
111
|
+
let executed = null;
|
|
112
|
+
if (isObject(t) && isObject(t["effects"])) {
|
|
113
|
+
if (typeof t["transactionBcs"] !== "string")
|
|
114
|
+
throw malformed("transactionBcs");
|
|
115
|
+
executed = { bytes: fromBase64(t["transactionBcs"]), effects: effectsOf(t["effects"]) };
|
|
116
|
+
}
|
|
117
|
+
return { executed, latest: latestOf(data) };
|
|
118
|
+
},
|
|
119
|
+
async simulate(bytes, deadline) {
|
|
120
|
+
const data = await query(`query($t: JSON!) { simulateTransaction(transaction: $t, checksEnabled: true) { effects { ${EFFECTS} } } }`, { t: { bcs: { value: toBase64(bytes) } } }, deadline);
|
|
121
|
+
const r = data["simulateTransaction"];
|
|
122
|
+
return effectsOf(isObject(r) ? r["effects"] : undefined);
|
|
123
|
+
},
|
|
124
|
+
async execute(bytes, signatures, deadline) {
|
|
125
|
+
const data = await query(`mutation($b: Base64!, $s: [Base64!]!) { executeTransaction(transactionDataBcs: $b, signatures: $s) { effects { ${EFFECTS} } } }`, { b: toBase64(bytes), s: signatures }, deadline);
|
|
126
|
+
const r = data["executeTransaction"];
|
|
127
|
+
return effectsOf(isObject(r) ? r["effects"] : undefined);
|
|
128
|
+
},
|
|
129
|
+
async verifySignature(bytes, signature, author, deadline) {
|
|
130
|
+
try {
|
|
131
|
+
const data = await query(`query($m: Base64!, $s: Base64!, $a: SuiAddress!) { verifySignature(message: $m, signature: $s, intentScope: TRANSACTION_DATA, author: $a) { success } }`, { m: toBase64(bytes), s: signature, a: author }, deadline);
|
|
132
|
+
const r = data["verifySignature"];
|
|
133
|
+
return isObject(r) && r["success"] === true;
|
|
134
|
+
}
|
|
135
|
+
catch (e) {
|
|
136
|
+
// The schema answers an invalid signature with an error naming why: no valid signature.
|
|
137
|
+
if (e instanceof GraphqlFault)
|
|
138
|
+
return false;
|
|
139
|
+
throw e;
|
|
140
|
+
}
|
|
141
|
+
},
|
|
142
|
+
reader(deadline) {
|
|
143
|
+
return {
|
|
144
|
+
network,
|
|
145
|
+
async read(digest) {
|
|
146
|
+
const data = await query(`query($d: String!) { transaction(digest: $d) { transactionBcs effects { status checkpoint { sequenceNumber } balanceChangesJson } }
|
|
147
|
+
checkpoint { epoch { epochId } }
|
|
148
|
+
serviceConfig { availableRange(type: "Query", field: "transaction") { first { sequenceNumber } }
|
|
149
|
+
sender: availableRange(type: "Query", field: "transactions", filters: ["sentAddress"]) { first { sequenceNumber } last { sequenceNumber epoch { epochId } } } } }`, { d: digest }, deadline);
|
|
150
|
+
const t = data["transaction"];
|
|
151
|
+
const cp = data["checkpoint"];
|
|
152
|
+
const config = isObject(data["serviceConfig"]) ? data["serviceConfig"] : {};
|
|
153
|
+
const range = isObject(config["availableRange"]) ? config["availableRange"]["first"] : undefined;
|
|
154
|
+
const sender = isObject(config["sender"]) ? config["sender"] : undefined;
|
|
155
|
+
const first = isObject(sender) && isObject(sender["first"]) ? sender["first"]["sequenceNumber"] : undefined;
|
|
156
|
+
const last = isObject(sender) && isObject(sender["last"]) ? sender["last"] : undefined;
|
|
157
|
+
return {
|
|
158
|
+
tx: isObject(t) ? looseExecuted(t) : null,
|
|
159
|
+
epoch: looseBig(isObject(cp) && isObject(cp["epoch"]) ? cp["epoch"]["epochId"] : undefined),
|
|
160
|
+
firstCheckpoint: (isObject(range) ? looseBig(range["sequenceNumber"]) : null),
|
|
161
|
+
searchRange: first === undefined || last === undefined
|
|
162
|
+
? null
|
|
163
|
+
: { first: looseBig(first), last: looseBig(last["sequenceNumber"]), epoch: looseBig(isObject(last["epoch"]) ? last["epoch"]["epochId"] : undefined) },
|
|
164
|
+
};
|
|
165
|
+
},
|
|
166
|
+
async search(sender, after, before, cursor) {
|
|
167
|
+
const data = await query(`query($s: SuiAddress!, $a: UInt53!, $b: UInt53!, $c: String) {
|
|
168
|
+
transactions(first: 50, after: $c, filter: { sentAddress: $s, afterCheckpoint: $a, beforeCheckpoint: $b }) {
|
|
169
|
+
pageInfo { hasNextPage endCursor }
|
|
170
|
+
nodes { digest transactionBcs effects { status checkpoint { sequenceNumber } balanceChangesJson } } } }`, { s: sender, a: Number(after), b: Number(before), c: cursor }, deadline);
|
|
171
|
+
const c = data["transactions"];
|
|
172
|
+
if (!isObject(c) || !isObject(c["pageInfo"]) || !Array.isArray(c["nodes"]))
|
|
173
|
+
throw malformed("transactions");
|
|
174
|
+
return {
|
|
175
|
+
transactions: c["nodes"].map((n) => {
|
|
176
|
+
if (!isObject(n))
|
|
177
|
+
return n;
|
|
178
|
+
const t = looseExecuted(n);
|
|
179
|
+
return { ...t, digest: n["digest"], checkpoint: t["checkpoint"] };
|
|
180
|
+
}),
|
|
181
|
+
hasNextPage: c["pageInfo"]["hasNextPage"],
|
|
182
|
+
endCursor: c["pageInfo"]["endCursor"],
|
|
183
|
+
};
|
|
184
|
+
},
|
|
185
|
+
};
|
|
186
|
+
},
|
|
187
|
+
};
|
|
188
|
+
return node;
|
|
189
|
+
}
|
|
190
|
+
/** A whole number as a bigint where it is one, else the value as given. */
|
|
191
|
+
function looseBig(v) {
|
|
192
|
+
if (typeof v === "number" && Number.isSafeInteger(v) && v >= 0)
|
|
193
|
+
return BigInt(v);
|
|
194
|
+
if (typeof v === "string" && /^[0-9]{1,20}$/.test(v))
|
|
195
|
+
return BigInt(v);
|
|
196
|
+
return v;
|
|
197
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { type TvmNetwork, type TvmReader } from "@integraledger/lcp/tvm";
|
|
2
|
+
import type { FacilitatorTvmSigner } from "@x402/tvm";
|
|
3
|
+
import { type Module, type ModuleStarter, type Registration } from "../../module.js";
|
|
4
|
+
export interface TonConfig {
|
|
5
|
+
/** The network, exactly one. */
|
|
6
|
+
readonly network: TvmNetwork;
|
|
7
|
+
/** The seller's highload-v3 wallet and its node, as `@x402/tvm`'s facilitator class takes them. */
|
|
8
|
+
readonly signer: FacilitatorTvmSigner;
|
|
9
|
+
/** lcp's reader over the seller's TON Center v3 index for the network (`TvmReader` of `@integraledger/lcp/tvm`). */
|
|
10
|
+
readonly reader: TvmReader;
|
|
11
|
+
}
|
|
12
|
+
/** The module of `x402/exact/tvm` on the configured network. */
|
|
13
|
+
export declare function tonModule(c: TonConfig): Module;
|
|
14
|
+
export declare function tonRegistration(c: TonConfig): Registration;
|
|
15
|
+
/** The module's start: one registration of `exact` per configured network. */
|
|
16
|
+
export declare const starter: ModuleStarter<readonly TonConfig[]>;
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The TON module for `x402/exact/tvm` on each configured network: `@x402/tvm` 2.31.0's facilitator class, registered
|
|
3
|
+
* unchanged. The payer signs a W5 request; the seller's highload-v3 wallet relays it in a message of its own, which it
|
|
4
|
+
* signs and whose gas it pays (`extra.key` `relayer`). The wallet reaches the module as the `FacilitatorTvmSigner` the
|
|
5
|
+
* class takes, its address in `signers`. lcp's checks name the instrument, the payer's wallet and the sequence number
|
|
6
|
+
* the request was signed against; lcp's read goes through the reader the configuration gives over the seller's TON
|
|
7
|
+
* Center v3 index, as the class's signer is given over its node.
|
|
8
|
+
*/
|
|
9
|
+
import { isRefusal } from "@integraledger/lcp";
|
|
10
|
+
import { exactTvm } from "@integraledger/lcp/tvm";
|
|
11
|
+
import { ExactTvmScheme } from "@x402/tvm/exact/facilitator";
|
|
12
|
+
import { outcomeOf } from "../../module.js";
|
|
13
|
+
function reasonOf(r) {
|
|
14
|
+
if (r.code === "x402/not-v2")
|
|
15
|
+
return "invalid_x402_version";
|
|
16
|
+
if (r.code.startsWith("x402/option") || r.code === "tvm/option-malformed")
|
|
17
|
+
return "invalid_payment_requirements";
|
|
18
|
+
return "invalid_payload";
|
|
19
|
+
}
|
|
20
|
+
async function checked(p) {
|
|
21
|
+
const bound = await exactTvm.bound(p.payload);
|
|
22
|
+
if (isRefusal(bound))
|
|
23
|
+
return bound;
|
|
24
|
+
// The requirements arrive as JSON, which lcp's option type names.
|
|
25
|
+
return exactTvm.reference(p.payload, p.requirements);
|
|
26
|
+
}
|
|
27
|
+
/** The module of `x402/exact/tvm` on the configured network. */
|
|
28
|
+
export function tonModule(c) {
|
|
29
|
+
const network = c.network;
|
|
30
|
+
if (c.reader.network !== c.network)
|
|
31
|
+
throw new TypeError(`the TON reader serves ${c.reader.network}, not ${c.network}`);
|
|
32
|
+
return {
|
|
33
|
+
descriptor: { name: "ton", key: "relayer", pairings: [exactTvm.id], acceptsZero: true },
|
|
34
|
+
network,
|
|
35
|
+
facilitator: new ExactTvmScheme(c.signer),
|
|
36
|
+
record: {
|
|
37
|
+
async instrument(p) {
|
|
38
|
+
const ref = await checked(p);
|
|
39
|
+
if (isRefusal(ref))
|
|
40
|
+
return { refused: reasonOf(ref) };
|
|
41
|
+
return {
|
|
42
|
+
pairing: exactTvm.id,
|
|
43
|
+
// The W5 wallet takes one request at each sequence number.
|
|
44
|
+
instrument: { by: "single-use", network, signer: ref.wallet, value: String(ref.seqno) },
|
|
45
|
+
nonce: exactTvm.data.nonce,
|
|
46
|
+
keep: { until: "lapse", at: ref.validUntil * 1000 },
|
|
47
|
+
};
|
|
48
|
+
},
|
|
49
|
+
async read(p, at) {
|
|
50
|
+
const ref = await checked(p);
|
|
51
|
+
if (isRefusal(ref))
|
|
52
|
+
return outcomeOf({ state: "unreadable", reason: "unparseable", foreign: [] });
|
|
53
|
+
const search = at.search;
|
|
54
|
+
return outcomeOf(await exactTvm.status(ref, c.reader, { act: "payment", finality: "finalized", ...(search === undefined ? {} : { search }) }));
|
|
55
|
+
},
|
|
56
|
+
},
|
|
57
|
+
fees: {
|
|
58
|
+
// The highload wallet pays the relay's gas and the value it attaches to the payer's wallet.
|
|
59
|
+
paysFee: () => true,
|
|
60
|
+
payerBroadcast: () => false,
|
|
61
|
+
},
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
export function tonRegistration(c) {
|
|
65
|
+
return { network: c.network, module: tonModule(c), own: [] };
|
|
66
|
+
}
|
|
67
|
+
/** The module's start: one registration of `exact` per configured network. */
|
|
68
|
+
export const starter = {
|
|
69
|
+
name: "ton",
|
|
70
|
+
async start(config) {
|
|
71
|
+
return { registrations: config.map(tonRegistration) };
|
|
72
|
+
},
|
|
73
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { type OperatorLog } from "../../answers.js";
|
|
2
|
+
import { type Module, type ModuleStarter } from "../../module.js";
|
|
3
|
+
import { createTron, type TronNode } from "./rail.js";
|
|
4
|
+
export { createTron, type TronNode } from "./rail.js";
|
|
5
|
+
type TronRail = ReturnType<typeof createTron>;
|
|
6
|
+
/** One module per network the rail serves. `log` is the operator log the rail was made with. */
|
|
7
|
+
export declare function tronModules(rail: TronRail, log: OperatorLog): Module[];
|
|
8
|
+
/**
|
|
9
|
+
* The Tron module, started from the configuration's `tron` member: one FullNode and its Solidity node per network.
|
|
10
|
+
* The FullNode keeps the whole block history, as lcp's read of a payment requires of it.
|
|
11
|
+
*/
|
|
12
|
+
export declare const starter: ModuleStarter<readonly TronNode[]>;
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The Tron rail as the settler's modules: one for each configured network, for the scheme `exact` and the pairing
|
|
3
|
+
* `x402/exact/tron/lcp-trc20-memo`. Its checks, broadcast, wait and read are the rail's; the payer signs and pays every
|
|
4
|
+
* fee, so the module holds no key. The record of which payments are settled is the settler's, keyed by this module's
|
|
5
|
+
* instrument, the transaction id, and holding where lcp's read of the payment searches from: the last solidified block
|
|
6
|
+
* read before the broadcast.
|
|
7
|
+
*/
|
|
8
|
+
import { exactTronMemo, LCP_TRC20_MEMO } from "@integraledger/lcp/tron";
|
|
9
|
+
import { failed, invalid, readRequest } from "../../answers.js";
|
|
10
|
+
import { outcomeOf } from "../../module.js";
|
|
11
|
+
import { unexpectedSettle, unexpectedVerify } from "../../unexpected.js";
|
|
12
|
+
import { createTron } from "./rail.js";
|
|
13
|
+
export { createTron } from "./rail.js";
|
|
14
|
+
/** The x402 facilitator request that carries `payload` and `requirements`, read by the rail's shared shape checks. */
|
|
15
|
+
function requestOf(payload, requirements) {
|
|
16
|
+
return readRequest({ x402Version: 2, paymentPayload: payload, paymentRequirements: requirements });
|
|
17
|
+
}
|
|
18
|
+
function settled(answer) {
|
|
19
|
+
return { ...answer, network: answer.network };
|
|
20
|
+
}
|
|
21
|
+
/** A search position as the record gives it back: the block number and its `blockID`. */
|
|
22
|
+
function cursorOf(v) {
|
|
23
|
+
if (typeof v !== "object" || v === null)
|
|
24
|
+
return undefined;
|
|
25
|
+
const { number, blockId } = v;
|
|
26
|
+
if (typeof number !== "bigint" || typeof blockId !== "string" || !/^0x[0-9a-f]{64}$/.test(blockId))
|
|
27
|
+
return undefined;
|
|
28
|
+
return { number, blockId: blockId };
|
|
29
|
+
}
|
|
30
|
+
/** One module per network the rail serves. `log` is the operator log the rail was made with. */
|
|
31
|
+
export function tronModules(rail, log) {
|
|
32
|
+
return rail.networks.map((name) => {
|
|
33
|
+
const network = name;
|
|
34
|
+
const facilitator = {
|
|
35
|
+
scheme: "exact",
|
|
36
|
+
caipFamily: "tron:*",
|
|
37
|
+
getExtra: () => ({ assetTransferMethod: LCP_TRC20_MEMO }),
|
|
38
|
+
getSigners: () => [],
|
|
39
|
+
async verify(payload, requirements) {
|
|
40
|
+
const r = requestOf(payload, requirements);
|
|
41
|
+
if (typeof r === "string")
|
|
42
|
+
return invalid(r);
|
|
43
|
+
try {
|
|
44
|
+
return await rail.verify(r);
|
|
45
|
+
}
|
|
46
|
+
catch (e) {
|
|
47
|
+
return invalid(unexpectedVerify(log, { event: "verify-error", network: r.paymentRequirements.network }, e));
|
|
48
|
+
}
|
|
49
|
+
},
|
|
50
|
+
async settle(payload, requirements) {
|
|
51
|
+
const r = requestOf(payload, requirements);
|
|
52
|
+
if (typeof r === "string")
|
|
53
|
+
return settled(failed(r, "", requirements.network));
|
|
54
|
+
try {
|
|
55
|
+
return settled(await rail.settle(r));
|
|
56
|
+
}
|
|
57
|
+
catch (e) {
|
|
58
|
+
return settled(unexpectedSettle(log, { event: "settle-error", network: r.paymentRequirements.network }, e));
|
|
59
|
+
}
|
|
60
|
+
},
|
|
61
|
+
};
|
|
62
|
+
const request = (p) => requestOf(p.payload, p.requirements);
|
|
63
|
+
return {
|
|
64
|
+
descriptor: {
|
|
65
|
+
name: "tron",
|
|
66
|
+
key: "none",
|
|
67
|
+
pairings: [exactTronMemo.id],
|
|
68
|
+
// The profile's floor is 1 base unit until a run shows a contract's transfer(to, 0) succeeding with a Transfer
|
|
69
|
+
// log (rule 1); no contract is so shown, so no payment of zero is settled.
|
|
70
|
+
acceptsZero: false,
|
|
71
|
+
},
|
|
72
|
+
network,
|
|
73
|
+
facilitator,
|
|
74
|
+
record: {
|
|
75
|
+
async instrument(p) {
|
|
76
|
+
const r = request(p);
|
|
77
|
+
const c = typeof r === "string" ? r : await rail.check(r);
|
|
78
|
+
if (typeof c === "string")
|
|
79
|
+
return { refused: c };
|
|
80
|
+
return {
|
|
81
|
+
pairing: exactTronMemo.id,
|
|
82
|
+
// The id is the SHA-256 of the signed raw_data, which holds no signature (profile rule 7: the replay
|
|
83
|
+
// primitive), so a payment signed by other keys of the same permission is the same instrument.
|
|
84
|
+
instrument: { by: "transaction", network: c.network, transaction: c.txid },
|
|
85
|
+
nonce: exactTronMemo.data.nonce,
|
|
86
|
+
keep: { until: "lapse", at: Number(c.raw.expiration) },
|
|
87
|
+
};
|
|
88
|
+
},
|
|
89
|
+
async read(p, at) {
|
|
90
|
+
const r = request(p);
|
|
91
|
+
if (typeof r === "string")
|
|
92
|
+
return outcomeOf({ state: "unreadable", reason: "unparseable", foreign: [] });
|
|
93
|
+
return outcomeOf(await rail.status(r, at.deadline, cursorOf(at.from), at.search));
|
|
94
|
+
},
|
|
95
|
+
async position(p, at) {
|
|
96
|
+
const r = request(p);
|
|
97
|
+
return typeof r === "string" ? undefined : rail.position(r, at.deadline);
|
|
98
|
+
},
|
|
99
|
+
},
|
|
100
|
+
fees: {
|
|
101
|
+
paysFee: () => false,
|
|
102
|
+
payerBroadcast: () => false,
|
|
103
|
+
},
|
|
104
|
+
};
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
function trimmed(url) {
|
|
108
|
+
return url.replace(/\/+$/, "");
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* The Tron module, started from the configuration's `tron` member: one FullNode and its Solidity node per network.
|
|
112
|
+
* The FullNode keeps the whole block history, as lcp's read of a payment requires of it.
|
|
113
|
+
*/
|
|
114
|
+
export const starter = {
|
|
115
|
+
name: "tron",
|
|
116
|
+
async start(nodes, core) {
|
|
117
|
+
const rail = createTron(nodes.map((n) => ({ ...n, fullNode: trimmed(n.fullNode), solidityNode: trimmed(n.solidityNode) })), core.settleWaitMs, core.log);
|
|
118
|
+
return { registrations: tronModules(rail, core.log).map((module) => ({ network: module.network, module, own: [] })) };
|
|
119
|
+
},
|
|
120
|
+
};
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { type TronCursor, type TronRaw, type TronStatus } from "@integraledger/lcp/tron";
|
|
2
|
+
import { type FacilitatorRequest, type InvalidReason, type OperatorLog, type SettleAnswer, type VerifyAnswer } from "../../answers.js";
|
|
3
|
+
export interface TronNode {
|
|
4
|
+
network: `tron:${number}`;
|
|
5
|
+
fullNode: string;
|
|
6
|
+
solidityNode: string;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Where the settler's read of a payment starts its search for H: the last solidified block before the broadcast, by
|
|
10
|
+
* its number and `blockID`, which lcp's read takes as its search position.
|
|
11
|
+
*/
|
|
12
|
+
export type TronPosition = TronCursor;
|
|
13
|
+
/** A transaction that passed every check that does not depend on time or chain state. */
|
|
14
|
+
interface Checked {
|
|
15
|
+
network: string;
|
|
16
|
+
hex: string;
|
|
17
|
+
raw: TronRaw;
|
|
18
|
+
txid: string;
|
|
19
|
+
payer: string;
|
|
20
|
+
/** The addresses (0x41 ‖ 20 bytes, hex) whose keys made the signatures, in the order signed, each once. */
|
|
21
|
+
signers: string[];
|
|
22
|
+
maxTimeoutSeconds: number;
|
|
23
|
+
}
|
|
24
|
+
export declare function createTron(nodes: readonly TronNode[], settleWaitMs: number, log: OperatorLog): {
|
|
25
|
+
verify: (r: FacilitatorRequest) => Promise<VerifyAnswer>;
|
|
26
|
+
settle: (r: FacilitatorRequest) => Promise<SettleAnswer>;
|
|
27
|
+
check: (r: FacilitatorRequest) => Promise<Checked | InvalidReason>;
|
|
28
|
+
status: (r: FacilitatorRequest, deadline: number, from?: TronPosition, search?: unknown) => Promise<TronStatus>;
|
|
29
|
+
position: (r: FacilitatorRequest, deadline: number) => Promise<TronPosition | undefined>;
|
|
30
|
+
networks: string[];
|
|
31
|
+
};
|
|
32
|
+
export {};
|