@piprail/sdk 2.3.0 → 2.4.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 +99 -0
- package/dist/algorand-TMA62DN2.cjs +678 -0
- package/dist/algorand-WB6PBJU4.js +678 -0
- package/dist/aptos-JQMZNTMD.cjs +673 -0
- package/dist/aptos-QAAXIUY3.js +673 -0
- package/dist/index.cjs +161 -17
- package/dist/index.d.cts +73 -23
- package/dist/index.d.ts +73 -23
- package/dist/index.js +158 -14
- package/dist/{solana-E4MD6JJ6.js → solana-3FMCWSEE.js} +20 -0
- package/dist/{solana-TLHL2KNY.cjs → solana-M3VOHCMO.cjs} +20 -0
- package/package.json +1 -1
- package/dist/algorand-GSFVZTBF.js +0 -389
- package/dist/algorand-HZS43N4P.cjs +0 -389
- package/dist/aptos-RIL56C7L.js +0 -352
- package/dist/aptos-TRCCJRZA.cjs +0 -352
package/dist/aptos-RIL56C7L.js
DELETED
|
@@ -1,352 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
ConfirmationTimeoutError,
|
|
3
|
-
InsufficientFundsError,
|
|
4
|
-
UnknownTokenError,
|
|
5
|
-
WrongFamilyError,
|
|
6
|
-
assertNoLegacyWalletKey,
|
|
7
|
-
nativeCost,
|
|
8
|
-
rejectForeignToken,
|
|
9
|
-
toInsufficientFundsError
|
|
10
|
-
} from "./chunk-7XK22JSQ.js";
|
|
11
|
-
|
|
12
|
-
// src/drivers/aptos/index.ts
|
|
13
|
-
import { Aptos, AptosConfig, Network, AccountAddress } from "@aptos-labs/ts-sdk";
|
|
14
|
-
|
|
15
|
-
// src/drivers/aptos/chains.ts
|
|
16
|
-
var APT_DECIMALS = 8;
|
|
17
|
-
var APT_SYMBOL = "APT";
|
|
18
|
-
var APT_FA_METADATA = "0xa";
|
|
19
|
-
var APTOS_MAINNET = {
|
|
20
|
-
caip2: "aptos:1",
|
|
21
|
-
defaultRpc: "https://fullnode.mainnet.aptoslabs.com/v1",
|
|
22
|
-
tokens: {
|
|
23
|
-
// Circle USDC — FA metadata + 6 decimals verified live on mainnet
|
|
24
|
-
// (0x1::fungible_asset::Metadata → symbol "USDC", decimals 6) before shipping.
|
|
25
|
-
USDC: {
|
|
26
|
-
metadata: "0xbae207659db88bea0cbead6da0ed00aac12edcdda169e591cd41c94180b46f3b",
|
|
27
|
-
decimals: 6,
|
|
28
|
-
symbol: "USDC"
|
|
29
|
-
},
|
|
30
|
-
// Tether USD₮ — use the FA *metadata* address (NOT the issuer/creator address
|
|
31
|
-
// 0xf73e…73cb). Verified live (Metadata → name "Tether USD", symbol "USDt", decimals 6).
|
|
32
|
-
USDT: {
|
|
33
|
-
metadata: "0x357b0b74bc833e95a115ad22604854d6b0fca151cecd94111770e5d6ffc9dc2b",
|
|
34
|
-
decimals: 6,
|
|
35
|
-
symbol: "USDT"
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
};
|
|
39
|
-
|
|
40
|
-
// src/drivers/aptos/pay.ts
|
|
41
|
-
var MAX_GAS_AMOUNT = 5e4;
|
|
42
|
-
async function payAptos(params) {
|
|
43
|
-
const { client, signer, sender, accept } = params;
|
|
44
|
-
const metadata = accept.asset === "native" ? APT_FA_METADATA : accept.asset;
|
|
45
|
-
try {
|
|
46
|
-
const transaction = await client.build({
|
|
47
|
-
sender,
|
|
48
|
-
data: {
|
|
49
|
-
function: "0x1::primary_fungible_store::transfer",
|
|
50
|
-
typeArguments: ["0x1::fungible_asset::Metadata"],
|
|
51
|
-
// u64 amount goes as a base-unit string; the FA metadata object + recipient are addresses.
|
|
52
|
-
functionArguments: [metadata, accept.payTo, accept.amount]
|
|
53
|
-
},
|
|
54
|
-
options: { maxGasAmount: MAX_GAS_AMOUNT }
|
|
55
|
-
});
|
|
56
|
-
const res = await client.signSubmit({ signer, transaction });
|
|
57
|
-
return res.hash;
|
|
58
|
-
} catch (err) {
|
|
59
|
-
if (err instanceof InsufficientFundsError) throw err;
|
|
60
|
-
if (isAptosAffordability(err)) {
|
|
61
|
-
throw new InsufficientFundsError(
|
|
62
|
-
err instanceof Error ? err.message : "Insufficient APT/token balance for the payment.",
|
|
63
|
-
{ cause: err }
|
|
64
|
-
);
|
|
65
|
-
}
|
|
66
|
-
throw toInsufficientFundsError(err) ?? err;
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
function isAptosAffordability(err) {
|
|
70
|
-
const m = err instanceof Error ? err.message : String(err);
|
|
71
|
-
return /INSUFFICIENT_BALANCE|EINSUFFICIENT_BALANCE|insufficient.*(balance|gas|funds)|coin store|balance is too low|EINSUFFICIENT/i.test(
|
|
72
|
-
m
|
|
73
|
-
);
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
// src/drivers/aptos/verify.ts
|
|
77
|
-
async function verifyAptos(params) {
|
|
78
|
-
const { reader, hash, accept } = params;
|
|
79
|
-
const required = BigInt(accept.amount);
|
|
80
|
-
const wantMetadata = accept.asset === "native" ? APT_FA_METADATA : accept.asset;
|
|
81
|
-
let tx;
|
|
82
|
-
try {
|
|
83
|
-
tx = await reader.getTransaction(hash);
|
|
84
|
-
} catch {
|
|
85
|
-
return txNotFound(hash);
|
|
86
|
-
}
|
|
87
|
-
if (!tx) return txNotFound(hash);
|
|
88
|
-
if (!tx.success) {
|
|
89
|
-
return { ok: false, error: "tx_reverted", detail: `Aptos tx ${hash} did not succeed.` };
|
|
90
|
-
}
|
|
91
|
-
if (typeof tx.timestampSeconds === "number") {
|
|
92
|
-
const ageSeconds = Math.floor(Date.now() / 1e3) - tx.timestampSeconds;
|
|
93
|
-
if (ageSeconds > accept.maxTimeoutSeconds) {
|
|
94
|
-
return {
|
|
95
|
-
ok: false,
|
|
96
|
-
error: "payment_expired",
|
|
97
|
-
detail: `Payment is ${ageSeconds}s old; max allowed is ${accept.maxTimeoutSeconds}s.`
|
|
98
|
-
};
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
let wantStore;
|
|
102
|
-
try {
|
|
103
|
-
wantStore = await reader.primaryStore(accept.payTo, wantMetadata);
|
|
104
|
-
} catch {
|
|
105
|
-
return txNotFound(hash);
|
|
106
|
-
}
|
|
107
|
-
let paid = 0n;
|
|
108
|
-
for (const d of tx.deposits) {
|
|
109
|
-
if (d.store !== wantStore) continue;
|
|
110
|
-
try {
|
|
111
|
-
paid += BigInt(d.amount);
|
|
112
|
-
} catch {
|
|
113
|
-
continue;
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
if (paid < required) {
|
|
117
|
-
return {
|
|
118
|
-
ok: false,
|
|
119
|
-
error: "transfer_not_found",
|
|
120
|
-
detail: `No Aptos transfer of >= ${required} (${wantMetadata}) to ${accept.payTo} in ${hash}.`
|
|
121
|
-
};
|
|
122
|
-
}
|
|
123
|
-
return {
|
|
124
|
-
ok: true,
|
|
125
|
-
receipt: {
|
|
126
|
-
scheme: "onchain-proof",
|
|
127
|
-
success: true,
|
|
128
|
-
network: accept.network,
|
|
129
|
-
transaction: hash,
|
|
130
|
-
asset: accept.asset,
|
|
131
|
-
amount: accept.amount,
|
|
132
|
-
payer: tx.sender,
|
|
133
|
-
payTo: accept.payTo,
|
|
134
|
-
verifiedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
135
|
-
}
|
|
136
|
-
};
|
|
137
|
-
}
|
|
138
|
-
function txNotFound(hash) {
|
|
139
|
-
return {
|
|
140
|
-
ok: false,
|
|
141
|
-
error: "tx_not_found",
|
|
142
|
-
detail: `Aptos tx ${hash} not found or not yet propagated \u2014 retry.`
|
|
143
|
-
};
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
// src/drivers/aptos/wallet.ts
|
|
147
|
-
import { Account, Ed25519PrivateKey } from "@aptos-labs/ts-sdk";
|
|
148
|
-
function assertAptosWallet(wallet, network) {
|
|
149
|
-
if (typeof wallet !== "object" || wallet === null) {
|
|
150
|
-
throw new WrongFamilyError(
|
|
151
|
-
`chain ${network} is Aptos; wallet must be { key } (ed25519-priv-0x\u2026) or { account }.`
|
|
152
|
-
);
|
|
153
|
-
}
|
|
154
|
-
assertNoLegacyWalletKey(wallet, "Aptos");
|
|
155
|
-
if (!("key" in wallet) && !("account" in wallet)) {
|
|
156
|
-
throw new WrongFamilyError(
|
|
157
|
-
`chain ${network} is Aptos; wallet must be { key } (ed25519-priv-0x\u2026) or { account }.`
|
|
158
|
-
);
|
|
159
|
-
}
|
|
160
|
-
return wallet;
|
|
161
|
-
}
|
|
162
|
-
function resolveAptosAccount(config) {
|
|
163
|
-
if (config.account) return config.account;
|
|
164
|
-
if (config.key != null) {
|
|
165
|
-
try {
|
|
166
|
-
return Account.fromPrivateKey({ privateKey: new Ed25519PrivateKey(config.key) });
|
|
167
|
-
} catch (cause) {
|
|
168
|
-
throw new WrongFamilyError(
|
|
169
|
-
"Aptos wallet { key } is not a valid ed25519 secret (ed25519-priv-0x\u2026 or 0x\u2026 hex).",
|
|
170
|
-
{ cause }
|
|
171
|
-
);
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
throw new WrongFamilyError("Aptos wallet needs { key } (ed25519-priv-0x\u2026) or { account }.");
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
// src/drivers/aptos/index.ts
|
|
178
|
-
var aptosDriver = {
|
|
179
|
-
family: "aptos",
|
|
180
|
-
resolve(opts) {
|
|
181
|
-
if (opts.chain !== "aptos") return null;
|
|
182
|
-
const rpcUrl = opts.rpcUrl ?? APTOS_MAINNET.defaultRpc;
|
|
183
|
-
return makeAptosNetwork(APTOS_MAINNET, rpcUrl);
|
|
184
|
-
}
|
|
185
|
-
};
|
|
186
|
-
function norm(addr) {
|
|
187
|
-
try {
|
|
188
|
-
return AccountAddress.from(addr).toString();
|
|
189
|
-
} catch {
|
|
190
|
-
return addr;
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
function makeAptosNetwork(preset, rpcUrl) {
|
|
194
|
-
const aptos = new Aptos(new AptosConfig({ network: Network.MAINNET, fullnode: rpcUrl }));
|
|
195
|
-
const network = preset.caip2;
|
|
196
|
-
const reader = {
|
|
197
|
-
async getTransaction(hash) {
|
|
198
|
-
const tx = await aptos.getTransactionByHash({ transactionHash: hash });
|
|
199
|
-
if (!tx || tx.type !== "user_transaction") return null;
|
|
200
|
-
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) }));
|
|
201
|
-
return {
|
|
202
|
-
success: tx.success === true,
|
|
203
|
-
// Aptos timestamps are microseconds since epoch.
|
|
204
|
-
timestampSeconds: tx.timestamp != null ? Math.floor(Number(tx.timestamp) / 1e6) : void 0,
|
|
205
|
-
sender: tx.sender ? norm(String(tx.sender)) : "",
|
|
206
|
-
deposits
|
|
207
|
-
};
|
|
208
|
-
},
|
|
209
|
-
async primaryStore(owner, metadata) {
|
|
210
|
-
const [addr] = await aptos.view({
|
|
211
|
-
payload: {
|
|
212
|
-
function: "0x1::primary_fungible_store::primary_store_address",
|
|
213
|
-
typeArguments: ["0x1::fungible_asset::Metadata"],
|
|
214
|
-
functionArguments: [owner, metadata]
|
|
215
|
-
}
|
|
216
|
-
});
|
|
217
|
-
return norm(String(addr));
|
|
218
|
-
}
|
|
219
|
-
};
|
|
220
|
-
const payClient = {
|
|
221
|
-
build: (input) => aptos.transaction.build.simple({
|
|
222
|
-
sender: input.sender,
|
|
223
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
224
|
-
data: input.data,
|
|
225
|
-
...input.options ? { options: input.options } : {}
|
|
226
|
-
}),
|
|
227
|
-
signSubmit: (input) => aptos.signAndSubmitTransaction({
|
|
228
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
229
|
-
signer: input.signer,
|
|
230
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
231
|
-
transaction: input.transaction
|
|
232
|
-
})
|
|
233
|
-
};
|
|
234
|
-
return {
|
|
235
|
-
family: "aptos",
|
|
236
|
-
network,
|
|
237
|
-
supports: (n) => n === network,
|
|
238
|
-
resolveToken(token) {
|
|
239
|
-
if (token === "native") {
|
|
240
|
-
return { asset: "native", decimals: APT_DECIMALS, symbol: APT_SYMBOL };
|
|
241
|
-
}
|
|
242
|
-
if (typeof token === "string") {
|
|
243
|
-
const info = preset.tokens[token.toUpperCase()];
|
|
244
|
-
if (!info) {
|
|
245
|
-
const known = Object.keys(preset.tokens).join(", ") || "(none built in)";
|
|
246
|
-
throw new UnknownTokenError(
|
|
247
|
-
`token "${token}" isn't built in for Aptos (known: ${known}). Pass { metadata, decimals } for a custom Fungible Asset, or use 'native'.`
|
|
248
|
-
);
|
|
249
|
-
}
|
|
250
|
-
return { asset: info.metadata, decimals: info.decimals, symbol: info.symbol };
|
|
251
|
-
}
|
|
252
|
-
rejectForeignToken(token, "aptos", network);
|
|
253
|
-
const t = token;
|
|
254
|
-
if (!t.metadata || typeof t.decimals !== "number") {
|
|
255
|
-
throw new WrongFamilyError(
|
|
256
|
-
`chain ${network} is Aptos; a custom token must be { metadata, decimals }.`
|
|
257
|
-
);
|
|
258
|
-
}
|
|
259
|
-
return {
|
|
260
|
-
asset: t.metadata,
|
|
261
|
-
decimals: t.decimals,
|
|
262
|
-
...t.symbol ? { symbol: t.symbol } : {}
|
|
263
|
-
};
|
|
264
|
-
},
|
|
265
|
-
describeAsset(asset) {
|
|
266
|
-
if (asset === "native") return { symbol: APT_SYMBOL, decimals: APT_DECIMALS };
|
|
267
|
-
for (const info of Object.values(preset.tokens)) {
|
|
268
|
-
if (info.metadata === asset) return { symbol: info.symbol, decimals: info.decimals };
|
|
269
|
-
}
|
|
270
|
-
return null;
|
|
271
|
-
},
|
|
272
|
-
assertValidPayTo(payTo) {
|
|
273
|
-
const evmLike = /^0x[0-9a-fA-F]{40}$/.test(payTo);
|
|
274
|
-
let valid = false;
|
|
275
|
-
try {
|
|
276
|
-
AccountAddress.from(payTo);
|
|
277
|
-
valid = true;
|
|
278
|
-
} catch {
|
|
279
|
-
valid = false;
|
|
280
|
-
}
|
|
281
|
-
if (!valid || evmLike) {
|
|
282
|
-
throw new WrongFamilyError(
|
|
283
|
-
`chain ${network} is Aptos, but payTo "${payTo}" is not a valid Aptos address (0x + 32 bytes).`
|
|
284
|
-
);
|
|
285
|
-
}
|
|
286
|
-
},
|
|
287
|
-
bindWallet(wallet) {
|
|
288
|
-
return { _native: assertAptosWallet(wallet, network) };
|
|
289
|
-
},
|
|
290
|
-
async send(wallet, accept) {
|
|
291
|
-
const account = resolveAptosAccount(wallet._native);
|
|
292
|
-
return payAptos({
|
|
293
|
-
client: payClient,
|
|
294
|
-
signer: account,
|
|
295
|
-
sender: account.accountAddress.toString(),
|
|
296
|
-
accept
|
|
297
|
-
});
|
|
298
|
-
},
|
|
299
|
-
async confirm(ref) {
|
|
300
|
-
try {
|
|
301
|
-
const tx = await aptos.waitForTransaction({ transactionHash: ref });
|
|
302
|
-
return { height: String(tx.version ?? "0") };
|
|
303
|
-
} catch (err) {
|
|
304
|
-
throw new ConfirmationTimeoutError(`Aptos tx ${ref} did not finalize in time.`, { cause: err });
|
|
305
|
-
}
|
|
306
|
-
},
|
|
307
|
-
async estimateCost() {
|
|
308
|
-
return nativeCost({
|
|
309
|
-
symbol: APT_SYMBOL,
|
|
310
|
-
decimals: APT_DECIMALS,
|
|
311
|
-
fee: 100000n,
|
|
312
|
-
// ~0.001 APT
|
|
313
|
-
basis: "heuristic",
|
|
314
|
-
detail: "\u22480.001 APT (a simple Fungible-Asset transfer; Aptos gas is sub-cent)"
|
|
315
|
-
});
|
|
316
|
-
},
|
|
317
|
-
async balanceOf(wallet, asset) {
|
|
318
|
-
let owner;
|
|
319
|
-
try {
|
|
320
|
-
owner = resolveAptosAccount(wallet._native).accountAddress.toString();
|
|
321
|
-
} catch {
|
|
322
|
-
return { token: null, native: null };
|
|
323
|
-
}
|
|
324
|
-
const native = await aptos.getAccountAPTAmount({ accountAddress: owner }).then((n) => BigInt(n)).catch(() => null);
|
|
325
|
-
if (asset === "native") return { token: native, native };
|
|
326
|
-
let token = null;
|
|
327
|
-
try {
|
|
328
|
-
const [bal] = await aptos.view({
|
|
329
|
-
payload: {
|
|
330
|
-
function: "0x1::primary_fungible_store::balance",
|
|
331
|
-
typeArguments: ["0x1::fungible_asset::Metadata"],
|
|
332
|
-
functionArguments: [owner, asset]
|
|
333
|
-
}
|
|
334
|
-
});
|
|
335
|
-
token = BigInt(String(bal));
|
|
336
|
-
} catch {
|
|
337
|
-
token = null;
|
|
338
|
-
}
|
|
339
|
-
return { token, native };
|
|
340
|
-
},
|
|
341
|
-
// No receive prerequisite — the recipient's primary FA store auto-creates on receipt.
|
|
342
|
-
async recipientReady() {
|
|
343
|
-
return { ready: "n/a" };
|
|
344
|
-
},
|
|
345
|
-
async verify(ref, accept) {
|
|
346
|
-
return verifyAptos({ reader, hash: ref, accept });
|
|
347
|
-
}
|
|
348
|
-
};
|
|
349
|
-
}
|
|
350
|
-
export {
|
|
351
|
-
aptosDriver
|
|
352
|
-
};
|
package/dist/aptos-TRCCJRZA.cjs
DELETED
|
@@ -1,352 +0,0 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } }
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
var _chunkJG6KRAW6cjs = require('./chunk-JG6KRAW6.cjs');
|
|
11
|
-
|
|
12
|
-
// src/drivers/aptos/index.ts
|
|
13
|
-
var _tssdk = require('@aptos-labs/ts-sdk');
|
|
14
|
-
|
|
15
|
-
// src/drivers/aptos/chains.ts
|
|
16
|
-
var APT_DECIMALS = 8;
|
|
17
|
-
var APT_SYMBOL = "APT";
|
|
18
|
-
var APT_FA_METADATA = "0xa";
|
|
19
|
-
var APTOS_MAINNET = {
|
|
20
|
-
caip2: "aptos:1",
|
|
21
|
-
defaultRpc: "https://fullnode.mainnet.aptoslabs.com/v1",
|
|
22
|
-
tokens: {
|
|
23
|
-
// Circle USDC — FA metadata + 6 decimals verified live on mainnet
|
|
24
|
-
// (0x1::fungible_asset::Metadata → symbol "USDC", decimals 6) before shipping.
|
|
25
|
-
USDC: {
|
|
26
|
-
metadata: "0xbae207659db88bea0cbead6da0ed00aac12edcdda169e591cd41c94180b46f3b",
|
|
27
|
-
decimals: 6,
|
|
28
|
-
symbol: "USDC"
|
|
29
|
-
},
|
|
30
|
-
// Tether USD₮ — use the FA *metadata* address (NOT the issuer/creator address
|
|
31
|
-
// 0xf73e…73cb). Verified live (Metadata → name "Tether USD", symbol "USDt", decimals 6).
|
|
32
|
-
USDT: {
|
|
33
|
-
metadata: "0x357b0b74bc833e95a115ad22604854d6b0fca151cecd94111770e5d6ffc9dc2b",
|
|
34
|
-
decimals: 6,
|
|
35
|
-
symbol: "USDT"
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
};
|
|
39
|
-
|
|
40
|
-
// src/drivers/aptos/pay.ts
|
|
41
|
-
var MAX_GAS_AMOUNT = 5e4;
|
|
42
|
-
async function payAptos(params) {
|
|
43
|
-
const { client, signer, sender, accept } = params;
|
|
44
|
-
const metadata = accept.asset === "native" ? APT_FA_METADATA : accept.asset;
|
|
45
|
-
try {
|
|
46
|
-
const transaction = await client.build({
|
|
47
|
-
sender,
|
|
48
|
-
data: {
|
|
49
|
-
function: "0x1::primary_fungible_store::transfer",
|
|
50
|
-
typeArguments: ["0x1::fungible_asset::Metadata"],
|
|
51
|
-
// u64 amount goes as a base-unit string; the FA metadata object + recipient are addresses.
|
|
52
|
-
functionArguments: [metadata, accept.payTo, accept.amount]
|
|
53
|
-
},
|
|
54
|
-
options: { maxGasAmount: MAX_GAS_AMOUNT }
|
|
55
|
-
});
|
|
56
|
-
const res = await client.signSubmit({ signer, transaction });
|
|
57
|
-
return res.hash;
|
|
58
|
-
} catch (err) {
|
|
59
|
-
if (err instanceof _chunkJG6KRAW6cjs.InsufficientFundsError) throw err;
|
|
60
|
-
if (isAptosAffordability(err)) {
|
|
61
|
-
throw new (0, _chunkJG6KRAW6cjs.InsufficientFundsError)(
|
|
62
|
-
err instanceof Error ? err.message : "Insufficient APT/token balance for the payment.",
|
|
63
|
-
{ cause: err }
|
|
64
|
-
);
|
|
65
|
-
}
|
|
66
|
-
throw _nullishCoalesce(_chunkJG6KRAW6cjs.toInsufficientFundsError.call(void 0, err), () => ( err));
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
function isAptosAffordability(err) {
|
|
70
|
-
const m = err instanceof Error ? err.message : String(err);
|
|
71
|
-
return /INSUFFICIENT_BALANCE|EINSUFFICIENT_BALANCE|insufficient.*(balance|gas|funds)|coin store|balance is too low|EINSUFFICIENT/i.test(
|
|
72
|
-
m
|
|
73
|
-
);
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
// src/drivers/aptos/verify.ts
|
|
77
|
-
async function verifyAptos(params) {
|
|
78
|
-
const { reader, hash, accept } = params;
|
|
79
|
-
const required = BigInt(accept.amount);
|
|
80
|
-
const wantMetadata = accept.asset === "native" ? APT_FA_METADATA : accept.asset;
|
|
81
|
-
let tx;
|
|
82
|
-
try {
|
|
83
|
-
tx = await reader.getTransaction(hash);
|
|
84
|
-
} catch (e2) {
|
|
85
|
-
return txNotFound(hash);
|
|
86
|
-
}
|
|
87
|
-
if (!tx) return txNotFound(hash);
|
|
88
|
-
if (!tx.success) {
|
|
89
|
-
return { ok: false, error: "tx_reverted", detail: `Aptos tx ${hash} did not succeed.` };
|
|
90
|
-
}
|
|
91
|
-
if (typeof tx.timestampSeconds === "number") {
|
|
92
|
-
const ageSeconds = Math.floor(Date.now() / 1e3) - tx.timestampSeconds;
|
|
93
|
-
if (ageSeconds > accept.maxTimeoutSeconds) {
|
|
94
|
-
return {
|
|
95
|
-
ok: false,
|
|
96
|
-
error: "payment_expired",
|
|
97
|
-
detail: `Payment is ${ageSeconds}s old; max allowed is ${accept.maxTimeoutSeconds}s.`
|
|
98
|
-
};
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
let wantStore;
|
|
102
|
-
try {
|
|
103
|
-
wantStore = await reader.primaryStore(accept.payTo, wantMetadata);
|
|
104
|
-
} catch (e3) {
|
|
105
|
-
return txNotFound(hash);
|
|
106
|
-
}
|
|
107
|
-
let paid = 0n;
|
|
108
|
-
for (const d of tx.deposits) {
|
|
109
|
-
if (d.store !== wantStore) continue;
|
|
110
|
-
try {
|
|
111
|
-
paid += BigInt(d.amount);
|
|
112
|
-
} catch (e4) {
|
|
113
|
-
continue;
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
if (paid < required) {
|
|
117
|
-
return {
|
|
118
|
-
ok: false,
|
|
119
|
-
error: "transfer_not_found",
|
|
120
|
-
detail: `No Aptos transfer of >= ${required} (${wantMetadata}) to ${accept.payTo} in ${hash}.`
|
|
121
|
-
};
|
|
122
|
-
}
|
|
123
|
-
return {
|
|
124
|
-
ok: true,
|
|
125
|
-
receipt: {
|
|
126
|
-
scheme: "onchain-proof",
|
|
127
|
-
success: true,
|
|
128
|
-
network: accept.network,
|
|
129
|
-
transaction: hash,
|
|
130
|
-
asset: accept.asset,
|
|
131
|
-
amount: accept.amount,
|
|
132
|
-
payer: tx.sender,
|
|
133
|
-
payTo: accept.payTo,
|
|
134
|
-
verifiedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
135
|
-
}
|
|
136
|
-
};
|
|
137
|
-
}
|
|
138
|
-
function txNotFound(hash) {
|
|
139
|
-
return {
|
|
140
|
-
ok: false,
|
|
141
|
-
error: "tx_not_found",
|
|
142
|
-
detail: `Aptos tx ${hash} not found or not yet propagated \u2014 retry.`
|
|
143
|
-
};
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
// src/drivers/aptos/wallet.ts
|
|
147
|
-
|
|
148
|
-
function assertAptosWallet(wallet, network) {
|
|
149
|
-
if (typeof wallet !== "object" || wallet === null) {
|
|
150
|
-
throw new (0, _chunkJG6KRAW6cjs.WrongFamilyError)(
|
|
151
|
-
`chain ${network} is Aptos; wallet must be { key } (ed25519-priv-0x\u2026) or { account }.`
|
|
152
|
-
);
|
|
153
|
-
}
|
|
154
|
-
_chunkJG6KRAW6cjs.assertNoLegacyWalletKey.call(void 0, wallet, "Aptos");
|
|
155
|
-
if (!("key" in wallet) && !("account" in wallet)) {
|
|
156
|
-
throw new (0, _chunkJG6KRAW6cjs.WrongFamilyError)(
|
|
157
|
-
`chain ${network} is Aptos; wallet must be { key } (ed25519-priv-0x\u2026) or { account }.`
|
|
158
|
-
);
|
|
159
|
-
}
|
|
160
|
-
return wallet;
|
|
161
|
-
}
|
|
162
|
-
function resolveAptosAccount(config) {
|
|
163
|
-
if (config.account) return config.account;
|
|
164
|
-
if (config.key != null) {
|
|
165
|
-
try {
|
|
166
|
-
return _tssdk.Account.fromPrivateKey({ privateKey: new (0, _tssdk.Ed25519PrivateKey)(config.key) });
|
|
167
|
-
} catch (cause) {
|
|
168
|
-
throw new (0, _chunkJG6KRAW6cjs.WrongFamilyError)(
|
|
169
|
-
"Aptos wallet { key } is not a valid ed25519 secret (ed25519-priv-0x\u2026 or 0x\u2026 hex).",
|
|
170
|
-
{ cause }
|
|
171
|
-
);
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
throw new (0, _chunkJG6KRAW6cjs.WrongFamilyError)("Aptos wallet needs { key } (ed25519-priv-0x\u2026) or { account }.");
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
// src/drivers/aptos/index.ts
|
|
178
|
-
var aptosDriver = {
|
|
179
|
-
family: "aptos",
|
|
180
|
-
resolve(opts) {
|
|
181
|
-
if (opts.chain !== "aptos") return null;
|
|
182
|
-
const rpcUrl = _nullishCoalesce(opts.rpcUrl, () => ( APTOS_MAINNET.defaultRpc));
|
|
183
|
-
return makeAptosNetwork(APTOS_MAINNET, rpcUrl);
|
|
184
|
-
}
|
|
185
|
-
};
|
|
186
|
-
function norm(addr) {
|
|
187
|
-
try {
|
|
188
|
-
return _tssdk.AccountAddress.from(addr).toString();
|
|
189
|
-
} catch (e5) {
|
|
190
|
-
return addr;
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
function makeAptosNetwork(preset, rpcUrl) {
|
|
194
|
-
const aptos = new (0, _tssdk.Aptos)(new (0, _tssdk.AptosConfig)({ network: _tssdk.Network.MAINNET, fullnode: rpcUrl }));
|
|
195
|
-
const network = preset.caip2;
|
|
196
|
-
const reader = {
|
|
197
|
-
async getTransaction(hash) {
|
|
198
|
-
const tx = await aptos.getTransactionByHash({ transactionHash: hash });
|
|
199
|
-
if (!tx || tx.type !== "user_transaction") return null;
|
|
200
|
-
const deposits = (_nullishCoalesce(tx.events, () => ( []))).filter((e) => e.type === "0x1::fungible_asset::Deposit").map((e) => ({ store: norm(String(e.data.store)), amount: String(e.data.amount) }));
|
|
201
|
-
return {
|
|
202
|
-
success: tx.success === true,
|
|
203
|
-
// Aptos timestamps are microseconds since epoch.
|
|
204
|
-
timestampSeconds: tx.timestamp != null ? Math.floor(Number(tx.timestamp) / 1e6) : void 0,
|
|
205
|
-
sender: tx.sender ? norm(String(tx.sender)) : "",
|
|
206
|
-
deposits
|
|
207
|
-
};
|
|
208
|
-
},
|
|
209
|
-
async primaryStore(owner, metadata) {
|
|
210
|
-
const [addr] = await aptos.view({
|
|
211
|
-
payload: {
|
|
212
|
-
function: "0x1::primary_fungible_store::primary_store_address",
|
|
213
|
-
typeArguments: ["0x1::fungible_asset::Metadata"],
|
|
214
|
-
functionArguments: [owner, metadata]
|
|
215
|
-
}
|
|
216
|
-
});
|
|
217
|
-
return norm(String(addr));
|
|
218
|
-
}
|
|
219
|
-
};
|
|
220
|
-
const payClient = {
|
|
221
|
-
build: (input) => aptos.transaction.build.simple({
|
|
222
|
-
sender: input.sender,
|
|
223
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
224
|
-
data: input.data,
|
|
225
|
-
...input.options ? { options: input.options } : {}
|
|
226
|
-
}),
|
|
227
|
-
signSubmit: (input) => aptos.signAndSubmitTransaction({
|
|
228
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
229
|
-
signer: input.signer,
|
|
230
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
231
|
-
transaction: input.transaction
|
|
232
|
-
})
|
|
233
|
-
};
|
|
234
|
-
return {
|
|
235
|
-
family: "aptos",
|
|
236
|
-
network,
|
|
237
|
-
supports: (n) => n === network,
|
|
238
|
-
resolveToken(token) {
|
|
239
|
-
if (token === "native") {
|
|
240
|
-
return { asset: "native", decimals: APT_DECIMALS, symbol: APT_SYMBOL };
|
|
241
|
-
}
|
|
242
|
-
if (typeof token === "string") {
|
|
243
|
-
const info = preset.tokens[token.toUpperCase()];
|
|
244
|
-
if (!info) {
|
|
245
|
-
const known = Object.keys(preset.tokens).join(", ") || "(none built in)";
|
|
246
|
-
throw new (0, _chunkJG6KRAW6cjs.UnknownTokenError)(
|
|
247
|
-
`token "${token}" isn't built in for Aptos (known: ${known}). Pass { metadata, decimals } for a custom Fungible Asset, or use 'native'.`
|
|
248
|
-
);
|
|
249
|
-
}
|
|
250
|
-
return { asset: info.metadata, decimals: info.decimals, symbol: info.symbol };
|
|
251
|
-
}
|
|
252
|
-
_chunkJG6KRAW6cjs.rejectForeignToken.call(void 0, token, "aptos", network);
|
|
253
|
-
const t = token;
|
|
254
|
-
if (!t.metadata || typeof t.decimals !== "number") {
|
|
255
|
-
throw new (0, _chunkJG6KRAW6cjs.WrongFamilyError)(
|
|
256
|
-
`chain ${network} is Aptos; a custom token must be { metadata, decimals }.`
|
|
257
|
-
);
|
|
258
|
-
}
|
|
259
|
-
return {
|
|
260
|
-
asset: t.metadata,
|
|
261
|
-
decimals: t.decimals,
|
|
262
|
-
...t.symbol ? { symbol: t.symbol } : {}
|
|
263
|
-
};
|
|
264
|
-
},
|
|
265
|
-
describeAsset(asset) {
|
|
266
|
-
if (asset === "native") return { symbol: APT_SYMBOL, decimals: APT_DECIMALS };
|
|
267
|
-
for (const info of Object.values(preset.tokens)) {
|
|
268
|
-
if (info.metadata === asset) return { symbol: info.symbol, decimals: info.decimals };
|
|
269
|
-
}
|
|
270
|
-
return null;
|
|
271
|
-
},
|
|
272
|
-
assertValidPayTo(payTo) {
|
|
273
|
-
const evmLike = /^0x[0-9a-fA-F]{40}$/.test(payTo);
|
|
274
|
-
let valid = false;
|
|
275
|
-
try {
|
|
276
|
-
_tssdk.AccountAddress.from(payTo);
|
|
277
|
-
valid = true;
|
|
278
|
-
} catch (e6) {
|
|
279
|
-
valid = false;
|
|
280
|
-
}
|
|
281
|
-
if (!valid || evmLike) {
|
|
282
|
-
throw new (0, _chunkJG6KRAW6cjs.WrongFamilyError)(
|
|
283
|
-
`chain ${network} is Aptos, but payTo "${payTo}" is not a valid Aptos address (0x + 32 bytes).`
|
|
284
|
-
);
|
|
285
|
-
}
|
|
286
|
-
},
|
|
287
|
-
bindWallet(wallet) {
|
|
288
|
-
return { _native: assertAptosWallet(wallet, network) };
|
|
289
|
-
},
|
|
290
|
-
async send(wallet, accept) {
|
|
291
|
-
const account = resolveAptosAccount(wallet._native);
|
|
292
|
-
return payAptos({
|
|
293
|
-
client: payClient,
|
|
294
|
-
signer: account,
|
|
295
|
-
sender: account.accountAddress.toString(),
|
|
296
|
-
accept
|
|
297
|
-
});
|
|
298
|
-
},
|
|
299
|
-
async confirm(ref) {
|
|
300
|
-
try {
|
|
301
|
-
const tx = await aptos.waitForTransaction({ transactionHash: ref });
|
|
302
|
-
return { height: String(_nullishCoalesce(tx.version, () => ( "0"))) };
|
|
303
|
-
} catch (err) {
|
|
304
|
-
throw new (0, _chunkJG6KRAW6cjs.ConfirmationTimeoutError)(`Aptos tx ${ref} did not finalize in time.`, { cause: err });
|
|
305
|
-
}
|
|
306
|
-
},
|
|
307
|
-
async estimateCost() {
|
|
308
|
-
return _chunkJG6KRAW6cjs.nativeCost.call(void 0, {
|
|
309
|
-
symbol: APT_SYMBOL,
|
|
310
|
-
decimals: APT_DECIMALS,
|
|
311
|
-
fee: 100000n,
|
|
312
|
-
// ~0.001 APT
|
|
313
|
-
basis: "heuristic",
|
|
314
|
-
detail: "\u22480.001 APT (a simple Fungible-Asset transfer; Aptos gas is sub-cent)"
|
|
315
|
-
});
|
|
316
|
-
},
|
|
317
|
-
async balanceOf(wallet, asset) {
|
|
318
|
-
let owner;
|
|
319
|
-
try {
|
|
320
|
-
owner = resolveAptosAccount(wallet._native).accountAddress.toString();
|
|
321
|
-
} catch (e7) {
|
|
322
|
-
return { token: null, native: null };
|
|
323
|
-
}
|
|
324
|
-
const native = await aptos.getAccountAPTAmount({ accountAddress: owner }).then((n) => BigInt(n)).catch(() => null);
|
|
325
|
-
if (asset === "native") return { token: native, native };
|
|
326
|
-
let token = null;
|
|
327
|
-
try {
|
|
328
|
-
const [bal] = await aptos.view({
|
|
329
|
-
payload: {
|
|
330
|
-
function: "0x1::primary_fungible_store::balance",
|
|
331
|
-
typeArguments: ["0x1::fungible_asset::Metadata"],
|
|
332
|
-
functionArguments: [owner, asset]
|
|
333
|
-
}
|
|
334
|
-
});
|
|
335
|
-
token = BigInt(String(bal));
|
|
336
|
-
} catch (e8) {
|
|
337
|
-
token = null;
|
|
338
|
-
}
|
|
339
|
-
return { token, native };
|
|
340
|
-
},
|
|
341
|
-
// No receive prerequisite — the recipient's primary FA store auto-creates on receipt.
|
|
342
|
-
async recipientReady() {
|
|
343
|
-
return { ready: "n/a" };
|
|
344
|
-
},
|
|
345
|
-
async verify(ref, accept) {
|
|
346
|
-
return verifyAptos({ reader, hash: ref, accept });
|
|
347
|
-
}
|
|
348
|
-
};
|
|
349
|
-
}
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
exports.aptosDriver = aptosDriver;
|