@piprail/sdk 1.1.1 → 1.3.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 CHANGED
@@ -4,6 +4,50 @@ All notable changes to `@piprail/sdk` are documented here. The format
4
4
  follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/) and the
5
5
  versions follow [Semantic Versioning](https://semver.org/).
6
6
 
7
+ ## [1.3.0] — 2026-06-04
8
+
9
+ A new chain **family** — **Aptos** — the **9th driver family** and the only Move L1 with BOTH
10
+ canonical native stablecoins. Brings the built-in count to **27 chains across 9 families (19 EVM)**.
11
+ Aptos has an official `exact` scheme merged into the canonical `coinbase/x402` repo and is a
12
+ first-class x402 / agent-payments network. Fully backward-compatible; `@aptos-labs/ts-sdk` is a
13
+ lazy-loaded optional peer, so pure-EVM (and other) installs never download it.
14
+
15
+ ### Added
16
+ - **Aptos (`chain: 'aptos'`, CAIP-2 `aptos:1`)** — native Circle **USDC**
17
+ (`0xbae207659db88bea0cbead6da0ed00aac12edcdda169e591cd41c94180b46f3b`) + native Tether **USD₮**
18
+ (`0x357b0b74bc833e95a115ad22604854d6b0fca151cecd94111770e5d6ffc9dc2b`), both 6 dp, plus native
19
+ **APT** (8 dp). Both Fungible-Asset metadata addresses were verified on-chain
20
+ (`0x1::fungible_asset::Metadata` → matching symbol + decimals) before shipping.
21
+ - **Template B (digest-bound, like Sui/Tron):** the proof ref is the tx hash; `verify()` re-derives
22
+ payTo's primary store for the required FA metadata from the **trusted accept** (never the client
23
+ ref) and matches `0x1::fungible_asset::Deposit` events to it (+ recency window + single-use proof
24
+ set). Every asset — native APT and both stablecoins — transfers via
25
+ `0x1::primary_fungible_store::transfer` (native = the APT FA at `0xa`), which auto-creates the
26
+ recipient's primary store, so there's **no opt-in / coin-store registration to receive** — even a
27
+ fresh recipient works. `@aptos-labs/ts-sdk` is an **optional peer (`>=2 <8`)**, lazy-loaded on
28
+ first use; the built EVM bundle stays free of any static `@aptos-labs/ts-sdk` import (its own chunk).
29
+
30
+ Live mainnet smoke (a real APT + USDC/USDT round-trip) is the separate ship-gate, pending wallet
31
+ funding; the driver is verified against the test contract (typecheck + 416 tests + build).
32
+
33
+ ## [1.2.0] — 2026-06-04
34
+
35
+ Two new EVM presets — **HyperEVM (Hyperliquid)** and **Monad** — bringing the built-in count to
36
+ **26 chains across 8 families (19 EVM)**. Both reuse the existing EVM driver: one row of
37
+ on-chain-verified data each, no new code path and no new peer dep. Fully backward-compatible.
38
+
39
+ ### Added
40
+ - **HyperEVM (Hyperliquid), `chain: 'hyperevm'`, chainId 999** — native Circle USDC
41
+ (`0xb88339CB7199b77E23DB6E890353E22632Ba630f`, 6 dp; CCTP V2). The highest-activity EVM venue
42
+ of 2025–26 (perps DEX + on-chain agent vaults). Pay in USDC or native HYPE. HyperEVM's USDT is
43
+ USDT0 (LayerZero), not Tether-native, so it's omitted (pass it as a custom `{ address, decimals }`).
44
+ - **Monad, `chain: 'monad'`, chainId 143** — native Circle USDC
45
+ (`0x754704Bc059F8C67012fEd69BC8A327a5aafb603`, 6 dp; CCTP V2). The biggest new EVM L1 of 2025
46
+ (parallel EVM, ~10k TPS). Pay in USDC or native MON. USDT0 omitted, as above.
47
+
48
+ Both addresses were verified on-chain (live `eth_chainId` + `symbol()`/`decimals()`) before
49
+ shipping; `chain: 'hyperevm'` / `chain: 'monad'` work with no setup call.
50
+
7
51
  ## [1.1.1] — 2026-06-03
8
52
 
9
53
  Docs + examples only — **no code change**; the API and every chain behave exactly as 1.1.0.
package/ERRORS.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  This is the **single source of truth** for how `@piprail/sdk` reports errors. It is
4
4
  deliberately small and uniform: every module — the client, the server gate, the registry,
5
- and every chain driver (all eight families: EVM, Solana, TON, Tron, NEAR, Sui, Stellar, XRPL,
5
+ and every chain driver (all nine families: EVM, Solana, TON, Tron, NEAR, Sui, Stellar, XRPL, Aptos,
6
6
  and any future one) — follows it
7
7
  *exactly*, so a human developer, a merchant server, or an AI agent always gets a **typed,
8
8
  understandable** reason, never an opaque chain-library blob.
package/README.md CHANGED
@@ -90,7 +90,7 @@ See [`examples/agent-tools.mjs`](../examples/agent-tools.mjs) for MCP / AI-SDK w
90
90
 
91
91
  ### Accept several chains at once
92
92
 
93
- `requirePayment` (and `createPaymentGate`) take an **`accept: [...]`** array — one challenge that's payable on **any** of several chains/tokens, across **all eight families** (EVM, Solana, TON, Tron, Stellar, XRPL, NEAR, Sui). The agent pays with whatever it holds:
93
+ `requirePayment` (and `createPaymentGate`) take an **`accept: [...]`** array — one challenge that's payable on **any** of several chains/tokens, across **all nine families** (EVM, Solana, TON, Tron, Stellar, XRPL, NEAR, Sui, Aptos). The agent pays with whatever it holds:
94
94
 
95
95
  ```ts
96
96
  requirePayment({
@@ -133,7 +133,7 @@ requirePayment({ chain: 'ton', token: 'native', amount: '1', payTo }) /
133
133
  requirePayment({ chain: 'xrpl', token: 'native', amount: '1', payTo }) // XRP
134
134
  ```
135
135
 
136
- **Native or stablecoin — your choice, on every chain.** Every gate accepts the chain's native coin (ETH, BNB, POL, AVAX, SOL, TON, XLM, XRP, SUI, NEAR, **TRX**, …) just as readily as a stablecoin — set `token: 'native'` and the SDK fills in the right decimals (18 on EVM, 9 on Solana/TON/Sui, 7 on Stellar, 6 on XRPL/Tron, 24 on NEAR). Verification, replay protection, and self-custody are identical to the stablecoin path — across **all eight families, no exceptions**. (On **NEAR**, native is the zero-setup path — no `storage_deposit` — while the NEP-141 token path needs registration; see the NEAR note. On **Tron**, USD₮ is the default since TRX is volatile gas, but native TRX works too.)
136
+ **Native or stablecoin — your choice, on every chain.** Every gate accepts the chain's native coin (ETH, BNB, POL, AVAX, SOL, TON, XLM, XRP, SUI, NEAR, **TRX**, …) just as readily as a stablecoin — set `token: 'native'` and the SDK fills in the right decimals (18 on EVM, 9 on Solana/TON/Sui, 7 on Stellar, 6 on XRPL/Tron, 24 on NEAR). Verification, replay protection, and self-custody are identical to the stablecoin path — across **all nine families, no exceptions**. (On **NEAR**, native is the zero-setup path — no `storage_deposit` — while the NEP-141 token path needs registration; see the NEAR note. On **Tron**, USD₮ is the default since TRX is volatile gas, but native TRX works too.)
137
137
 
138
138
  `token` is **required** — every gate states exactly what it accepts, so there's never any doubt whether a route takes USDC, USDT, or the native coin. Name a built-in symbol (`'USDC'`, `'USDT'`), use `'native'` for the chain's own coin (ETH, BNB, SOL, TON, XLM, …), or pass a custom token by address. The symbol is all you write — the SDK fills in the contract + decimals.
139
139
 
@@ -160,11 +160,14 @@ Every token address below was verified on-chain (symbol + decimals) before shipp
160
160
  | `'worldchain'` | World Chain | USDC |
161
161
  | `'sei'` | Sei | USDC |
162
162
  | `'injective'` | Injective | USDC, USDT |
163
+ | `'hyperevm'` | HyperEVM (Hyperliquid) | USDC |
164
+ | `'monad'` | Monad | USDC |
163
165
  | `'solana'` | Solana | USDC, USDT |
164
166
  | `'ton'` | TON | USDT |
165
167
  | `'tron'` | Tron | USDT |
166
168
  | `'near'` | NEAR | USDC, USDT |
167
169
  | `'sui'` | Sui | USDC |
170
+ | `'aptos'` | Aptos | USDC, USDT |
168
171
  | `'stellar'` | Stellar | USDC, EURC |
169
172
  | `'xrpl'` | XRP Ledger | USDC, RLUSD |
170
173
 
@@ -0,0 +1,329 @@
1
+ import {
2
+ ConfirmationTimeoutError,
3
+ InsufficientFundsError,
4
+ UnknownTokenError,
5
+ WrongFamilyError,
6
+ nativeCost,
7
+ rejectForeignToken,
8
+ toInsufficientFundsError
9
+ } from "./chunk-AGKC3C7Y.js";
10
+
11
+ // src/drivers/aptos/index.ts
12
+ import { Aptos, AptosConfig, Network, AccountAddress } from "@aptos-labs/ts-sdk";
13
+
14
+ // src/drivers/aptos/chains.ts
15
+ var APT_DECIMALS = 8;
16
+ var APT_SYMBOL = "APT";
17
+ var APT_FA_METADATA = "0xa";
18
+ var APTOS_MAINNET = {
19
+ caip2: "aptos:1",
20
+ defaultRpc: "https://fullnode.mainnet.aptoslabs.com/v1",
21
+ tokens: {
22
+ // Circle USDC — FA metadata + 6 decimals verified live on mainnet
23
+ // (0x1::fungible_asset::Metadata → symbol "USDC", decimals 6) before shipping.
24
+ USDC: {
25
+ metadata: "0xbae207659db88bea0cbead6da0ed00aac12edcdda169e591cd41c94180b46f3b",
26
+ decimals: 6,
27
+ symbol: "USDC"
28
+ },
29
+ // Tether USD₮ — use the FA *metadata* address (NOT the issuer/creator address
30
+ // 0xf73e…73cb). Verified live (Metadata → name "Tether USD", symbol "USDt", decimals 6).
31
+ USDT: {
32
+ metadata: "0x357b0b74bc833e95a115ad22604854d6b0fca151cecd94111770e5d6ffc9dc2b",
33
+ decimals: 6,
34
+ symbol: "USDT"
35
+ }
36
+ }
37
+ };
38
+
39
+ // src/drivers/aptos/pay.ts
40
+ async function payAptos(params) {
41
+ const { client, signer, sender, accept } = params;
42
+ const metadata = accept.asset === "native" ? APT_FA_METADATA : accept.asset;
43
+ try {
44
+ const transaction = await client.build({
45
+ sender,
46
+ data: {
47
+ function: "0x1::primary_fungible_store::transfer",
48
+ typeArguments: ["0x1::fungible_asset::Metadata"],
49
+ // u64 amount goes as a base-unit string; the FA metadata object + recipient are addresses.
50
+ functionArguments: [metadata, accept.payTo, accept.amount]
51
+ }
52
+ });
53
+ const res = await client.signSubmit({ signer, transaction });
54
+ return res.hash;
55
+ } catch (err) {
56
+ if (err instanceof InsufficientFundsError) throw err;
57
+ if (isAptosAffordability(err)) {
58
+ throw new InsufficientFundsError(
59
+ err instanceof Error ? err.message : "Insufficient APT/token balance for the payment.",
60
+ { cause: err }
61
+ );
62
+ }
63
+ throw toInsufficientFundsError(err) ?? err;
64
+ }
65
+ }
66
+ function isAptosAffordability(err) {
67
+ const m = err instanceof Error ? err.message : String(err);
68
+ return /INSUFFICIENT_BALANCE|EINSUFFICIENT_BALANCE|insufficient.*(balance|gas|funds)|coin store|balance is too low|EINSUFFICIENT/i.test(
69
+ m
70
+ );
71
+ }
72
+
73
+ // src/drivers/aptos/verify.ts
74
+ async function verifyAptos(params) {
75
+ const { reader, hash, accept } = params;
76
+ const required = BigInt(accept.amount);
77
+ const wantMetadata = accept.asset === "native" ? APT_FA_METADATA : accept.asset;
78
+ let tx;
79
+ try {
80
+ tx = await reader.getTransaction(hash);
81
+ } catch {
82
+ return txNotFound(hash);
83
+ }
84
+ if (!tx) return txNotFound(hash);
85
+ if (!tx.success) {
86
+ return { ok: false, error: "tx_reverted", detail: `Aptos tx ${hash} did not succeed.` };
87
+ }
88
+ if (typeof tx.timestampSeconds === "number") {
89
+ const ageSeconds = Math.floor(Date.now() / 1e3) - tx.timestampSeconds;
90
+ if (ageSeconds > accept.maxTimeoutSeconds) {
91
+ return {
92
+ ok: false,
93
+ error: "payment_expired",
94
+ detail: `Payment is ${ageSeconds}s old; max allowed is ${accept.maxTimeoutSeconds}s.`
95
+ };
96
+ }
97
+ }
98
+ let wantStore;
99
+ try {
100
+ wantStore = await reader.primaryStore(accept.payTo, wantMetadata);
101
+ } catch {
102
+ return txNotFound(hash);
103
+ }
104
+ let paid = 0n;
105
+ for (const d of tx.deposits) {
106
+ if (d.store !== wantStore) continue;
107
+ try {
108
+ paid += BigInt(d.amount);
109
+ } catch {
110
+ continue;
111
+ }
112
+ }
113
+ if (paid < required) {
114
+ return {
115
+ ok: false,
116
+ error: "transfer_not_found",
117
+ detail: `No Aptos transfer of >= ${required} (${wantMetadata}) to ${accept.payTo} in ${hash}.`
118
+ };
119
+ }
120
+ return {
121
+ ok: true,
122
+ receipt: {
123
+ scheme: "onchain-proof",
124
+ success: true,
125
+ network: accept.network,
126
+ transaction: hash,
127
+ asset: accept.asset,
128
+ amount: accept.amount,
129
+ payer: tx.sender,
130
+ payTo: accept.payTo,
131
+ verifiedAt: (/* @__PURE__ */ new Date()).toISOString()
132
+ }
133
+ };
134
+ }
135
+ function txNotFound(hash) {
136
+ return {
137
+ ok: false,
138
+ error: "tx_not_found",
139
+ detail: `Aptos tx ${hash} not found or not yet propagated \u2014 retry.`
140
+ };
141
+ }
142
+
143
+ // src/drivers/aptos/wallet.ts
144
+ import { Account, Ed25519PrivateKey } from "@aptos-labs/ts-sdk";
145
+ function assertAptosWallet(wallet, network) {
146
+ if (typeof wallet !== "object" || wallet === null) {
147
+ throw new WrongFamilyError(
148
+ `chain ${network} is Aptos; wallet must be { privateKey } (ed25519-priv-0x\u2026) or { account }.`
149
+ );
150
+ }
151
+ if ("walletClient" in wallet) {
152
+ throw new WrongFamilyError(
153
+ `chain ${network} is Aptos; a viem { walletClient } can't be used \u2014 pass { privateKey } (ed25519-priv-0x\u2026) or { account }.`
154
+ );
155
+ }
156
+ if ("secretKey" in wallet || "signer" in wallet || "mnemonic" in wallet || "keypair" in wallet || "keyPair" in wallet || "secret" in wallet || "seed" in wallet || "accountId" in wallet) {
157
+ throw new WrongFamilyError(
158
+ `chain ${network} is Aptos; that looks like another family's wallet \u2014 pass { privateKey } (ed25519-priv-0x\u2026) or { account }.`
159
+ );
160
+ }
161
+ if (!("privateKey" in wallet) && !("account" in wallet)) {
162
+ throw new WrongFamilyError(
163
+ `chain ${network} is Aptos; wallet must be { privateKey } (ed25519-priv-0x\u2026) or { account }.`
164
+ );
165
+ }
166
+ return wallet;
167
+ }
168
+ function resolveAptosAccount(config) {
169
+ if (config.account) return config.account;
170
+ if (config.privateKey != null) {
171
+ try {
172
+ return Account.fromPrivateKey({ privateKey: new Ed25519PrivateKey(config.privateKey) });
173
+ } catch (cause) {
174
+ throw new WrongFamilyError(
175
+ "Aptos wallet { privateKey } is not a valid ed25519 secret (ed25519-priv-0x\u2026 or 0x\u2026 hex).",
176
+ { cause }
177
+ );
178
+ }
179
+ }
180
+ throw new WrongFamilyError("Aptos wallet needs { privateKey } (ed25519-priv-0x\u2026) or { account }.");
181
+ }
182
+
183
+ // src/drivers/aptos/index.ts
184
+ var aptosDriver = {
185
+ family: "aptos",
186
+ resolve(opts) {
187
+ if (opts.chain !== "aptos") return null;
188
+ const rpcUrl = opts.rpcUrl ?? APTOS_MAINNET.defaultRpc;
189
+ return makeAptosNetwork(APTOS_MAINNET, rpcUrl);
190
+ }
191
+ };
192
+ function norm(addr) {
193
+ try {
194
+ return AccountAddress.from(addr).toString();
195
+ } catch {
196
+ return addr;
197
+ }
198
+ }
199
+ function makeAptosNetwork(preset, rpcUrl) {
200
+ const aptos = new Aptos(new AptosConfig({ network: Network.MAINNET, fullnode: rpcUrl }));
201
+ const network = preset.caip2;
202
+ const reader = {
203
+ async getTransaction(hash) {
204
+ const tx = await aptos.getTransactionByHash({ transactionHash: hash });
205
+ if (!tx || tx.type !== "user_transaction") return null;
206
+ const deposits = (tx.events ?? []).filter((e) => e.type === "0x1::fungible_asset::Deposit").map((e) => ({ store: norm(String(e.data.store)), amount: String(e.data.amount) }));
207
+ return {
208
+ success: tx.success === true,
209
+ // Aptos timestamps are microseconds since epoch.
210
+ timestampSeconds: tx.timestamp != null ? Math.floor(Number(tx.timestamp) / 1e6) : void 0,
211
+ sender: tx.sender ? norm(String(tx.sender)) : "",
212
+ deposits
213
+ };
214
+ },
215
+ async primaryStore(owner, metadata) {
216
+ const [addr] = await aptos.view({
217
+ payload: {
218
+ function: "0x1::primary_fungible_store::primary_store_address",
219
+ typeArguments: ["0x1::fungible_asset::Metadata"],
220
+ functionArguments: [owner, metadata]
221
+ }
222
+ });
223
+ return norm(String(addr));
224
+ }
225
+ };
226
+ const payClient = {
227
+ build: (input) => aptos.transaction.build.simple({
228
+ sender: input.sender,
229
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
230
+ data: input.data
231
+ }),
232
+ signSubmit: (input) => aptos.signAndSubmitTransaction({
233
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
234
+ signer: input.signer,
235
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
236
+ transaction: input.transaction
237
+ })
238
+ };
239
+ return {
240
+ family: "aptos",
241
+ network,
242
+ supports: (n) => n === network,
243
+ resolveToken(token) {
244
+ if (token === "native") {
245
+ return { asset: "native", decimals: APT_DECIMALS, symbol: APT_SYMBOL };
246
+ }
247
+ if (typeof token === "string") {
248
+ const info = preset.tokens[token.toUpperCase()];
249
+ if (!info) {
250
+ const known = Object.keys(preset.tokens).join(", ") || "(none built in)";
251
+ throw new UnknownTokenError(
252
+ `token "${token}" isn't built in for Aptos (known: ${known}). Pass { metadata, decimals } for a custom Fungible Asset, or use 'native'.`
253
+ );
254
+ }
255
+ return { asset: info.metadata, decimals: info.decimals, symbol: info.symbol };
256
+ }
257
+ rejectForeignToken(token, "aptos", network);
258
+ const t = token;
259
+ if (!t.metadata || typeof t.decimals !== "number") {
260
+ throw new WrongFamilyError(
261
+ `chain ${network} is Aptos; a custom token must be { metadata, decimals }.`
262
+ );
263
+ }
264
+ return {
265
+ asset: t.metadata,
266
+ decimals: t.decimals,
267
+ ...t.symbol ? { symbol: t.symbol } : {}
268
+ };
269
+ },
270
+ describeAsset(asset) {
271
+ if (asset === "native") return { symbol: APT_SYMBOL, decimals: APT_DECIMALS };
272
+ for (const info of Object.values(preset.tokens)) {
273
+ if (info.metadata === asset) return { symbol: info.symbol, decimals: info.decimals };
274
+ }
275
+ return null;
276
+ },
277
+ assertValidPayTo(payTo) {
278
+ const evmLike = /^0x[0-9a-fA-F]{40}$/.test(payTo);
279
+ let valid = false;
280
+ try {
281
+ AccountAddress.from(payTo);
282
+ valid = true;
283
+ } catch {
284
+ valid = false;
285
+ }
286
+ if (!valid || evmLike) {
287
+ throw new WrongFamilyError(
288
+ `chain ${network} is Aptos, but payTo "${payTo}" is not a valid Aptos address (0x + 32 bytes).`
289
+ );
290
+ }
291
+ },
292
+ bindWallet(wallet) {
293
+ return { _native: assertAptosWallet(wallet, network) };
294
+ },
295
+ async send(wallet, accept) {
296
+ const account = resolveAptosAccount(wallet._native);
297
+ return payAptos({
298
+ client: payClient,
299
+ signer: account,
300
+ sender: account.accountAddress.toString(),
301
+ accept
302
+ });
303
+ },
304
+ async confirm(ref) {
305
+ try {
306
+ const tx = await aptos.waitForTransaction({ transactionHash: ref });
307
+ return { height: String(tx.version ?? "0") };
308
+ } catch (err) {
309
+ throw new ConfirmationTimeoutError(`Aptos tx ${ref} did not finalize in time.`, { cause: err });
310
+ }
311
+ },
312
+ async estimateCost() {
313
+ return nativeCost({
314
+ symbol: APT_SYMBOL,
315
+ decimals: APT_DECIMALS,
316
+ fee: 100000n,
317
+ // ~0.001 APT
318
+ basis: "heuristic",
319
+ detail: "\u22480.001 APT (a simple Fungible-Asset transfer; Aptos gas is sub-cent)"
320
+ });
321
+ },
322
+ async verify(ref, accept) {
323
+ return verifyAptos({ reader, hash: ref, accept });
324
+ }
325
+ };
326
+ }
327
+ export {
328
+ aptosDriver
329
+ };