@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.
Files changed (90) hide show
  1. package/README.md +642 -456
  2. package/dist/answers.d.ts +11 -4
  3. package/dist/answers.js +24 -0
  4. package/dist/core.d.ts +51 -0
  5. package/dist/core.js +758 -0
  6. package/dist/http.d.ts +20 -3
  7. package/dist/http.js +32 -7
  8. package/dist/index.d.ts +12 -36
  9. package/dist/index.js +9 -162
  10. package/dist/module.d.ts +372 -0
  11. package/dist/module.js +13 -0
  12. package/dist/modules/algorand/index.d.ts +22 -0
  13. package/dist/modules/algorand/index.js +247 -0
  14. package/dist/modules/algorand/own.d.ts +26 -0
  15. package/dist/modules/algorand/own.js +231 -0
  16. package/dist/modules/aptos/index.d.ts +23 -0
  17. package/dist/modules/aptos/index.js +232 -0
  18. package/dist/modules/cardano/index.d.ts +18 -0
  19. package/dist/modules/cardano/index.js +124 -0
  20. package/dist/modules/casper/index.d.ts +120 -0
  21. package/dist/modules/casper/index.js +442 -0
  22. package/dist/modules/concordium/index.d.ts +16 -0
  23. package/dist/modules/concordium/index.js +77 -0
  24. package/dist/modules/erc7710/index.d.ts +26 -0
  25. package/dist/modules/erc7710/index.js +220 -0
  26. package/dist/modules/evm/forms.d.ts +74 -0
  27. package/dist/modules/evm/forms.js +182 -0
  28. package/dist/modules/evm/index.d.ts +40 -0
  29. package/dist/modules/evm/index.js +148 -0
  30. package/dist/modules/evm/node.d.ts +29 -0
  31. package/dist/modules/evm/node.js +222 -0
  32. package/dist/modules/hedera/executor.d.ts +63 -0
  33. package/dist/modules/hedera/executor.js +343 -0
  34. package/dist/modules/hedera/index.d.ts +23 -0
  35. package/dist/modules/hedera/index.js +136 -0
  36. package/dist/modules/hedera/mirror.d.ts +32 -0
  37. package/dist/modules/hedera/mirror.js +111 -0
  38. package/dist/modules/index.d.ts +23 -0
  39. package/dist/modules/index.js +24 -0
  40. package/dist/modules/lnbtc/index.d.ts +10 -0
  41. package/dist/modules/lnbtc/index.js +111 -0
  42. package/dist/modules/near/index.d.ts +18 -0
  43. package/dist/modules/near/index.js +246 -0
  44. package/dist/modules/polkadot/index.d.ts +12 -0
  45. package/dist/modules/polkadot/index.js +149 -0
  46. package/dist/modules/polkadot/rail.d.ts +40 -0
  47. package/dist/{polkadot.js → modules/polkadot/rail.js} +214 -288
  48. package/dist/modules/solana/index.d.ts +60 -0
  49. package/dist/modules/solana/index.js +304 -0
  50. package/dist/modules/solana/reader.d.ts +25 -0
  51. package/dist/modules/solana/reader.js +194 -0
  52. package/dist/modules/solana/state.d.ts +6 -0
  53. package/dist/modules/solana/state.js +103 -0
  54. package/dist/modules/starknet/index.d.ts +35 -0
  55. package/dist/modules/starknet/index.js +541 -0
  56. package/dist/modules/starknet/node.d.ts +78 -0
  57. package/dist/modules/starknet/node.js +339 -0
  58. package/dist/modules/stellar/index.d.ts +44 -0
  59. package/dist/modules/stellar/index.js +481 -0
  60. package/dist/modules/stellar/node.d.ts +57 -0
  61. package/dist/modules/stellar/node.js +151 -0
  62. package/dist/modules/sui/index.d.ts +16 -0
  63. package/dist/modules/sui/index.js +309 -0
  64. package/dist/modules/sui/node.d.ts +49 -0
  65. package/dist/modules/sui/node.js +197 -0
  66. package/dist/modules/ton/index.d.ts +16 -0
  67. package/dist/modules/ton/index.js +73 -0
  68. package/dist/modules/tron/index.d.ts +12 -0
  69. package/dist/modules/tron/index.js +120 -0
  70. package/dist/modules/tron/rail.d.ts +32 -0
  71. package/dist/{tron.js → modules/tron/rail.js} +206 -195
  72. package/dist/modules/xrpl/index.d.ts +28 -0
  73. package/dist/modules/xrpl/index.js +299 -0
  74. package/dist/modules/xrpl/node.d.ts +52 -0
  75. package/dist/modules/xrpl/node.js +231 -0
  76. package/dist/modules/xrpl/upto.d.ts +5 -0
  77. package/dist/modules/xrpl/upto.js +269 -0
  78. package/dist/record.d.ts +131 -0
  79. package/dist/record.js +330 -0
  80. package/dist/unexpected.d.ts +3 -2
  81. package/dist/unexpected.js +2 -1
  82. package/dist/wrapper.d.ts +68 -0
  83. package/dist/wrapper.js +126 -0
  84. package/package.json +47 -12
  85. package/dist/polkadot.d.ts +0 -21
  86. package/dist/store.d.ts +0 -59
  87. package/dist/store.js +0 -103
  88. package/dist/tron.d.ts +0 -12
  89. /package/dist/{base58check.d.ts → modules/tron/base58check.d.ts} +0 -0
  90. /package/dist/{base58check.js → modules/tron/base58check.js} +0 -0
@@ -0,0 +1,339 @@
1
+ /**
2
+ * The seller's Starknet node, over Starknet's JSON-RPC (specification 0.9 or later, whose block tags are `latest`,
3
+ * `pre_confirmed` and `l1_accepted`): the calls the module's checks and its settlement make, and the reader lcp's
4
+ * `x402/exact/starknet` read takes. Every call is bounded by the node bound of `../../http.js` and by its deadline.
5
+ */
6
+ import { FELT_P, } from "@integraledger/lcp/starknet";
7
+ import { isObject } from "../../answers.js";
8
+ import { NodeError, postJson } from "../../http.js";
9
+ /** A JSON-RPC error the node answered: the request reached it, and it refused it with this code. */
10
+ export class RpcFault extends Error {
11
+ code;
12
+ data;
13
+ constructor(code, message, data) {
14
+ super(`${code}: ${message}`);
15
+ this.code = code;
16
+ this.data = data;
17
+ }
18
+ }
19
+ /** Starknet JSON-RPC error codes the module reads (the specification's `starknet_api_openrpc.json`). */
20
+ export const RPC = {
21
+ CONTRACT_NOT_FOUND: 20,
22
+ ENTRYPOINT_NOT_FOUND: 21,
23
+ BLOCK_NOT_FOUND: 24,
24
+ TXN_HASH_NOT_FOUND: 29,
25
+ CONTRACT_ERROR: 40,
26
+ TRANSACTION_EXECUTION_ERROR: 41,
27
+ };
28
+ const FELT = /^0x[0-9a-fA-F]{1,64}$/;
29
+ /**
30
+ * A felt in its shortest lowercase spelling, or undefined for a value that is not `0x` and 1 to 64 hex digits below the
31
+ * STARK prime.
32
+ */
33
+ export function felt(v) {
34
+ if (typeof v !== "string" || !FELT.test(v))
35
+ return undefined;
36
+ const n = BigInt(v);
37
+ return n < FELT_P ? `0x${n.toString(16)}` : undefined;
38
+ }
39
+ /** A felt in its shortest spelling where it is one, else the value as given. */
40
+ function loose(v) {
41
+ return felt(v) ?? v;
42
+ }
43
+ function felts(v) {
44
+ if (!Array.isArray(v))
45
+ return undefined;
46
+ const out = [];
47
+ for (const x of v) {
48
+ const f = felt(x);
49
+ if (f === undefined)
50
+ return undefined;
51
+ out.push(f);
52
+ }
53
+ return out;
54
+ }
55
+ /** The most events one page of `starknet_getEvents` asks for: lcp reads at most 1024 in a page. */
56
+ const CHUNK = 1_000;
57
+ export function starknetNode(network, url) {
58
+ let id = 0;
59
+ async function rpc(method, params, deadline) {
60
+ id += 1;
61
+ const answer = await postJson(url, { jsonrpc: "2.0", id, method, params }, deadline - performance.now());
62
+ if (!isObject(answer))
63
+ throw new NodeError("malformed", true, `${method}: not an object`);
64
+ const error = answer["error"];
65
+ if (isObject(error)) {
66
+ const code = error["code"];
67
+ throw new RpcFault(typeof code === "number" ? code : -1, String(error["message"] ?? ""), error["data"]);
68
+ }
69
+ if (!("result" in answer))
70
+ throw new NodeError("malformed", true, `${method}: no result`);
71
+ return answer["result"];
72
+ }
73
+ const malformed = (what) => new NodeError("malformed", true, what);
74
+ function receiptOf(r) {
75
+ if (!isObject(r))
76
+ throw malformed("receipt");
77
+ const finality = r["finality_status"];
78
+ const execution = r["execution_status"];
79
+ if (finality !== "PRE_CONFIRMED" && finality !== "ACCEPTED_ON_L2" && finality !== "ACCEPTED_ON_L1")
80
+ throw malformed("finality");
81
+ if (execution !== "SUCCEEDED" && execution !== "REVERTED")
82
+ throw malformed("execution");
83
+ const number = r["block_number"];
84
+ if (number !== undefined && number !== null && !Number.isSafeInteger(number))
85
+ throw malformed("block_number");
86
+ const events = [];
87
+ if (!Array.isArray(r["events"]))
88
+ throw malformed("events");
89
+ for (const e of r["events"])
90
+ events.push(eventOf(e, "from_address"));
91
+ const actual = r["actual_fee"];
92
+ const amount = isObject(actual) ? felt(actual["amount"]) : undefined;
93
+ if (amount === undefined)
94
+ throw malformed("actual_fee");
95
+ return {
96
+ finality,
97
+ execution,
98
+ blockNumber: typeof number === "number" ? BigInt(number) : null,
99
+ events: events.map((e) => ({ fromAddress: e.from, keys: e.keys, data: e.data })),
100
+ fee: BigInt(amount),
101
+ };
102
+ }
103
+ function eventOf(e, emitter, from) {
104
+ if (!isObject(e))
105
+ throw malformed("event");
106
+ const address = from ?? felt(e[emitter]);
107
+ const keys = felts(e["keys"]);
108
+ const data = felts(e["data"]);
109
+ if (address === undefined || keys === undefined || data === undefined)
110
+ throw malformed("event");
111
+ return { from: address, keys, data };
112
+ }
113
+ /**
114
+ * A `FUNCTION_INVOCATION` as lcp's reader gives it, its members renamed and left as the node gave them, so that lcp
115
+ * judges one that is not well formed; a whole execution that reverted is one reverted invocation. Below 32 levels,
116
+ * deeper than lcp reads, its calls are not mapped.
117
+ */
118
+ function invocationOf(v, depth = 1) {
119
+ if (!isObject(v))
120
+ return v;
121
+ if (typeof v["revert_reason"] === "string" && v["contract_address"] === undefined) {
122
+ return { contract: "0x0", selector: "0x0", calldata: [], reverted: true, calls: [] };
123
+ }
124
+ const calls = v["calls"];
125
+ return {
126
+ contract: loose(v["contract_address"]),
127
+ selector: loose(v["entry_point_selector"]),
128
+ calldata: Array.isArray(v["calldata"]) ? v["calldata"].map(loose) : v["calldata"],
129
+ reverted: v["is_reverted"],
130
+ calls: Array.isArray(calls) ? (depth >= 32 ? [] : calls.map((c) => invocationOf(c, depth + 1))) : calls,
131
+ };
132
+ }
133
+ /** A receipt as lcp's reader gives it, renamed and left as the node gave it. */
134
+ function looseReceipt(r) {
135
+ if (!isObject(r))
136
+ return r;
137
+ const number = r["block_number"];
138
+ const events = r["events"];
139
+ return {
140
+ finality: r["finality_status"],
141
+ execution: r["execution_status"],
142
+ blockNumber: typeof number === "number" && Number.isSafeInteger(number) ? BigInt(number) : number === undefined ? null : number,
143
+ events: Array.isArray(events) ? events.map(looseEvent) : events,
144
+ };
145
+ }
146
+ function looseEvent(e) {
147
+ if (!isObject(e))
148
+ return e;
149
+ const keys = e["keys"];
150
+ const data = e["data"];
151
+ return {
152
+ fromAddress: loose(e["from_address"]),
153
+ keys: Array.isArray(keys) ? keys.map(loose) : keys,
154
+ data: Array.isArray(data) ? data.map(loose) : data,
155
+ };
156
+ }
157
+ /** Every event of a simulated execution, each with the contract whose invocation emitted it, in no order. */
158
+ function eventsOf(v, out, depth = 0) {
159
+ if (!isObject(v) || depth > 64)
160
+ throw malformed("invocation");
161
+ const contract = felt(v["contract_address"]);
162
+ if (contract === undefined || !Array.isArray(v["calls"]) || !Array.isArray(v["events"] ?? []))
163
+ throw malformed("invocation");
164
+ if (v["is_reverted"] === true)
165
+ return;
166
+ for (const e of (v["events"] ?? []))
167
+ out.push(eventOf(e, "", contract));
168
+ for (const c of v["calls"])
169
+ eventsOf(c, out, depth + 1);
170
+ }
171
+ async function rawReceipt(tx, deadline) {
172
+ try {
173
+ return await rpc("starknet_getTransactionReceipt", { transaction_hash: tx }, deadline);
174
+ }
175
+ catch (e) {
176
+ if (e instanceof RpcFault && e.code === RPC.TXN_HASH_NOT_FOUND)
177
+ return null;
178
+ throw e;
179
+ }
180
+ }
181
+ const asBig = (v, what) => {
182
+ if (typeof v === "number" && Number.isSafeInteger(v) && v >= 0)
183
+ return BigInt(v);
184
+ const f = felt(v);
185
+ if (f === undefined)
186
+ throw malformed(what);
187
+ return BigInt(f);
188
+ };
189
+ const node = {
190
+ network,
191
+ async call(contract, entrypoint, calldata, block, deadline) {
192
+ const r = await rpc("starknet_call", { request: { contract_address: contract, entry_point_selector: entrypoint, calldata }, block_id: block }, deadline);
193
+ const out = felts(r);
194
+ if (out === undefined)
195
+ throw malformed("starknet_call");
196
+ return out;
197
+ },
198
+ async classHashAt(contract, deadline) {
199
+ const r = felt(await rpc("starknet_getClassHashAt", { block_id: "latest", contract_address: contract }, deadline));
200
+ if (r === undefined)
201
+ throw malformed("starknet_getClassHashAt");
202
+ return r;
203
+ },
204
+ async nonce(account, block, deadline) {
205
+ return asBig(await rpc("starknet_getNonce", { block_id: block, contract_address: account }, deadline), "nonce");
206
+ },
207
+ async head(mark, deadline) {
208
+ const tag = mark === "ACCEPTED_ON_L1" ? "l1_accepted" : "latest";
209
+ const b = await rpc("starknet_getBlockWithTxHashes", { block_id: tag }, deadline);
210
+ if (!isObject(b) || !Number.isSafeInteger(b["block_number"]) || !Number.isSafeInteger(b["timestamp"])) {
211
+ throw malformed("block header");
212
+ }
213
+ return { number: BigInt(b["block_number"]), timestamp: BigInt(b["timestamp"]) };
214
+ },
215
+ async receipt(tx, deadline) {
216
+ const r = await rawReceipt(tx, deadline);
217
+ return r === null ? null : receiptOf(r);
218
+ },
219
+ async trace(tx, deadline) {
220
+ let t;
221
+ try {
222
+ t = await rpc("starknet_traceTransaction", { transaction_hash: tx }, deadline);
223
+ }
224
+ catch (e) {
225
+ if (e instanceof RpcFault && e.code === RPC.TXN_HASH_NOT_FOUND)
226
+ return null;
227
+ throw e;
228
+ }
229
+ if (!isObject(t))
230
+ throw malformed("trace");
231
+ const execute = t["execute_invocation"];
232
+ return execute === undefined || execute === null ? null : invocationOf(execute);
233
+ },
234
+ async events(q, deadline) {
235
+ let r;
236
+ try {
237
+ r = await rpc("starknet_getEvents", {
238
+ filter: {
239
+ from_block: { block_number: Number(q.from) },
240
+ to_block: { block_number: Number(q.to) },
241
+ address: q.address,
242
+ keys: q.keys,
243
+ chunk_size: CHUNK,
244
+ ...(q.continuation === undefined ? {} : { continuation_token: q.continuation }),
245
+ },
246
+ }, deadline);
247
+ }
248
+ catch (e) {
249
+ if (e instanceof RpcFault && e.code === RPC.BLOCK_NOT_FOUND)
250
+ return null;
251
+ throw e;
252
+ }
253
+ if (!isObject(r) || !Array.isArray(r["events"]))
254
+ throw malformed("starknet_getEvents");
255
+ const events = r["events"].map((e) => {
256
+ if (!isObject(e))
257
+ return e;
258
+ const block = e["block_number"];
259
+ return {
260
+ ...looseEvent(e),
261
+ transaction: loose(e["transaction_hash"]),
262
+ blockNumber: typeof block === "number" && Number.isSafeInteger(block) ? BigInt(block) : block === undefined ? null : block,
263
+ };
264
+ });
265
+ const token = r["continuation_token"];
266
+ return { events, continuation: typeof token === "string" && token !== "" ? token : null };
267
+ },
268
+ async simulate(invoke, deadline) {
269
+ const r = await rpc("starknet_simulateTransactions", { block_id: "latest", transactions: [invoke], simulation_flags: ["SKIP_VALIDATE", "SKIP_FEE_CHARGE"] }, deadline);
270
+ const first = Array.isArray(r) ? r[0] : undefined;
271
+ if (!isObject(first) || !isObject(first["transaction_trace"]) || !isObject(first["fee_estimation"])) {
272
+ throw malformed("starknet_simulateTransactions");
273
+ }
274
+ const execute = first["transaction_trace"]["execute_invocation"];
275
+ const fee = first["fee_estimation"];
276
+ const reverted = !isObject(execute) || typeof execute["revert_reason"] === "string" || execute["is_reverted"] === true;
277
+ const events = [];
278
+ if (!reverted)
279
+ eventsOf(execute, events);
280
+ return {
281
+ reverted,
282
+ events,
283
+ fee: {
284
+ l1Gas: asBig(fee["l1_gas_consumed"], "l1_gas_consumed"),
285
+ l1GasPrice: asBig(fee["l1_gas_price"], "l1_gas_price"),
286
+ l2Gas: asBig(fee["l2_gas_consumed"], "l2_gas_consumed"),
287
+ l2GasPrice: asBig(fee["l2_gas_price"], "l2_gas_price"),
288
+ l1DataGas: asBig(fee["l1_data_gas_consumed"], "l1_data_gas_consumed"),
289
+ l1DataGasPrice: asBig(fee["l1_data_gas_price"], "l1_data_gas_price"),
290
+ },
291
+ };
292
+ },
293
+ async add(invoke, deadline) {
294
+ const r = await rpc("starknet_addInvokeTransaction", { invoke_transaction: invoke }, deadline);
295
+ const hash = isObject(r) ? felt(r["transaction_hash"]) : undefined;
296
+ if (hash === undefined)
297
+ throw malformed("starknet_addInvokeTransaction");
298
+ return hash;
299
+ },
300
+ reader(deadline) {
301
+ return {
302
+ network,
303
+ async receipt(tx) {
304
+ const r = await rawReceipt(tx, deadline);
305
+ return r === null ? null : looseReceipt(r);
306
+ },
307
+ trace: (tx) => node.trace(tx, deadline),
308
+ head: (mark) => node.head(mark, deadline),
309
+ async nonceValid(account, nonce, block) {
310
+ let out;
311
+ try {
312
+ out = await node.call(account, IS_VALID_NONCE, [nonce], { block_number: Number(block) }, deadline);
313
+ }
314
+ catch (e) {
315
+ // A block whose state the node does not serve: lcp reads it as a history gap.
316
+ if (e instanceof RpcFault && e.code === RPC.BLOCK_NOT_FOUND)
317
+ return null;
318
+ throw e;
319
+ }
320
+ if (out.length !== 1 || (out[0] !== "0x0" && out[0] !== "0x1"))
321
+ throw malformed("is_valid_outside_execution_nonce");
322
+ return out[0] === "0x1";
323
+ },
324
+ events: (q) => node.events(q, deadline),
325
+ };
326
+ },
327
+ };
328
+ return node;
329
+ }
330
+ /** sn_keccak("is_valid_outside_execution_nonce") */
331
+ export const IS_VALID_NONCE = "0x1e6d35df2b9d989fb4b6bbcebda1314e4254cbe5e589dd94ff4f29ea935e91c";
332
+ /** sn_keccak("is_valid_signature") */
333
+ export const IS_VALID_SIGNATURE = "0x28420862938116cb3bbdbedee07451ccc54d4e9412dbef71142ad1980a30941";
334
+ /** sn_keccak("balance_of") */
335
+ export const BALANCE_OF = "0x35a73cd311a05d46deda634c5ee045db92f811b4e74bca4437fcb5302b7af33";
336
+ /** sn_keccak("balanceOf") */
337
+ export const BALANCE_OF_CAMEL = "0x2e4263afad30923c891518314c3c95dbe830a16874e8abc5777a9a20b54c76e";
338
+ /** SNIP-6's `VALID`, the short string `VALID`. */
339
+ export const VALID = "0x56414c4944";
@@ -0,0 +1,44 @@
1
+ import { type StellarNetwork } from "@integraledger/lcp/stellar";
2
+ import { type Core, type Module, type ModuleStarter } from "../../module.js";
3
+ /**
4
+ * The source account's signer: the account's `G…` address, and SEP-43's `signTransaction`, which returns the envelope
5
+ * it is given with the account's signature added. x402's `FacilitatorStellarSigner` (`createEd25519Signer` of
6
+ * `@x402/stellar`) is one; so is any SEP-43 wallet or signing service.
7
+ */
8
+ export interface StellarSigner {
9
+ readonly address: string;
10
+ signTransaction(xdr: string, opts?: {
11
+ networkPassphrase?: string;
12
+ address?: string;
13
+ }): Promise<{
14
+ signedTxXdr: string;
15
+ signerAddress?: string;
16
+ error?: unknown;
17
+ }>;
18
+ }
19
+ /** The configuration's member `stellar`: one entry per network the settler serves. */
20
+ export interface StellarConfig {
21
+ readonly networks: readonly StellarNetworkConfig[];
22
+ }
23
+ /** One Stellar network. */
24
+ export interface StellarNetworkConfig {
25
+ network: StellarNetwork;
26
+ /** A Stellar RPC endpoint that holds the history the read searches, a few ledgers back from the entry's expiration. */
27
+ rpc: string;
28
+ /** The seller's funded account, the transaction's source, which pays its fee and spends its sequence number. */
29
+ source: StellarSigner;
30
+ /**
31
+ * The most one settlement's fee may be, in stroops: x402's `maxTransactionFeeStroops`, whose default, 50,000, is its
32
+ * Stellar scheme's. A simulation that needs more is refused.
33
+ */
34
+ maxTransactionFeeStroops?: number;
35
+ /** The inclusion fee bid above the simulated resource fee, in stroops: x402's `inclusionFeeStroops`, at least 100, default 100. */
36
+ inclusionFeeStroops?: number;
37
+ }
38
+ /** Starts one module per configured network, each its own registration of `exact`. */
39
+ export declare const starter: ModuleStarter<StellarConfig>;
40
+ /**
41
+ * The module for one network. The settler's wait for the chain is the core's; the fee meter is the network's, which
42
+ * the module tells of each transaction's fee before it sends it and of the fee charged once it is included.
43
+ */
44
+ export declare function stellarModule(c: StellarNetworkConfig, core: Core): Module;