nansen-cli 2.5.0 → 2.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +30 -0
- package/README.md +18 -12
- package/docs/error-codes.md +1 -0
- package/package.json +1 -1
- package/scripts/postinstall.js +36 -11
- package/skills/nansen-limit-orders/SKILL.md +6 -3
- package/skills/nansen-token-research/SKILL.md +1 -1
- package/skills/nansen-trading/SKILL.md +14 -11
- package/skills/nansen-web-searcher/SKILL.md +1 -1
- package/src/api.js +0 -14
- package/src/cli.js +150 -116
- package/src/commands/alerts.js +13 -4
- package/src/commands/completion.js +0 -1
- package/src/commands/research.js +35 -35
- package/src/error-codes.js +1 -0
- package/src/execute-guard.js +43 -3
- package/src/input-validation.js +32 -3
- package/src/limit-order.js +12 -4
- package/src/perp.js +14 -9
- package/src/privy.js +145 -103
- package/src/schema.json +152 -88
- package/skills/nansen-web-fetcher/SKILL.md +0 -53
package/src/privy.js
CHANGED
|
@@ -8,13 +8,12 @@
|
|
|
8
8
|
|
|
9
9
|
import fs from "fs";
|
|
10
10
|
import path from "path";
|
|
11
|
-
import { parsePaymentRequirements } from "./x402.js";
|
|
11
|
+
import { parsePaymentRequirements, rankRequirements } from "./x402.js";
|
|
12
12
|
import { hasSufficientEvmPaymentBalance, hasSufficientSvmPaymentBalance } from "./x402-payment-balance.js";
|
|
13
13
|
import { isEvmNetwork } from "./x402-evm.js";
|
|
14
14
|
import { mkdirPrivateSync, readWalletJson, validateWalletName, withWalletLock, writeWalletJsonAtomic } from "./wallet.js";
|
|
15
15
|
import { evaluatePaymentRequirement, resolvePaymentAmount, resolvePayTo, signWithSessionCharge } from "./x402-policy.js";
|
|
16
16
|
import {
|
|
17
|
-
isSvmNetwork,
|
|
18
17
|
getSolanaRpcUrl,
|
|
19
18
|
fetchRecentBlockhash,
|
|
20
19
|
buildUnsignedSvmTransaction,
|
|
@@ -359,6 +358,11 @@ async function getPrivySolanaWallet(client) {
|
|
|
359
358
|
* Same yield contract as createPaymentSignatures() in x402.js:
|
|
360
359
|
* { signature, network, asset, payment, releaseCharge }
|
|
361
360
|
*
|
|
361
|
+
* Options are tried cheapest first, in the order rankRequirements gives the
|
|
362
|
+
* local and WalletConnect signers. The caller stops at the first payment the
|
|
363
|
+
* server accepts, so server order would let a pricier option be paid while a
|
|
364
|
+
* cheaper one sat in the same response.
|
|
365
|
+
*
|
|
362
366
|
* @param {Response} response - The 402 HTTP response
|
|
363
367
|
* @param {string} url - The original request URL
|
|
364
368
|
* @param {object} [options] - { onCapRefusal }, called with the reason when the session cap refuses an option
|
|
@@ -370,113 +374,151 @@ export async function* createPrivyPaymentSignatures(response, url, options = {})
|
|
|
370
374
|
|
|
371
375
|
const client = getClient();
|
|
372
376
|
|
|
373
|
-
// EVM
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
const typedData = buildEIP712TypedData({
|
|
387
|
-
fromAddress: evmWallet.address,
|
|
388
|
-
requirement,
|
|
389
|
-
});
|
|
390
|
-
|
|
391
|
-
const signed = await signWithSessionCharge(decision, async () => {
|
|
392
|
-
const signResult = await client.ethSignTypedDataV4(evmWallet.id, typedData);
|
|
393
|
-
const signature = signResult.data?.signature || signResult.signature;
|
|
394
|
-
if (!signature) throw new Error('Privy returned no signature');
|
|
395
|
-
|
|
396
|
-
const authorization = {
|
|
397
|
-
from: evmWallet.address,
|
|
398
|
-
to: resolvePayTo(requirement),
|
|
399
|
-
value: resolvePaymentAmount(requirement).toString(),
|
|
400
|
-
validAfter: typedData.message.validAfter.toString(),
|
|
401
|
-
validBefore: typedData.message.validBefore.toString(),
|
|
402
|
-
nonce: typedData.message.nonce,
|
|
403
|
-
};
|
|
404
|
-
|
|
405
|
-
return buildPaymentSignatureHeader({
|
|
406
|
-
signature,
|
|
407
|
-
authorization,
|
|
408
|
-
resource: { url, description: "", mimeType: "" },
|
|
409
|
-
accepted: requirement,
|
|
410
|
-
});
|
|
411
|
-
});
|
|
412
|
-
if (!signed.ok) {
|
|
413
|
-
console.error(`[x402] ${signed.reason}`);
|
|
414
|
-
options.onCapRefusal?.(signed.reason);
|
|
415
|
-
continue;
|
|
416
|
-
}
|
|
417
|
-
|
|
418
|
-
yield { signature: signed.value, network: requirement.network, asset: requirement.asset, payment: decision, releaseCharge: signed.release };
|
|
419
|
-
} catch (err) {
|
|
420
|
-
console.error(`[x402] Privy EVM signing failed for ${requirement.network}: ${err.message}`);
|
|
421
|
-
continue;
|
|
422
|
-
}
|
|
423
|
-
}
|
|
377
|
+
// rankRequirements keeps only EVM and Solana options. Each wallet is looked
|
|
378
|
+
// up once, on the first option for its chain.
|
|
379
|
+
let evmWallet;
|
|
380
|
+
let solWallet;
|
|
381
|
+
let lookupError;
|
|
382
|
+
let signedAny = false;
|
|
383
|
+
const onLookupError = (err) => { if (!lookupError) lookupError = err; };
|
|
384
|
+
for (const requirement of rankRequirements(requirements)) {
|
|
385
|
+
if (isEvmNetwork(requirement.network)) {
|
|
386
|
+
if (evmWallet === undefined) evmWallet = await lookupPrivyWallet('EVM', () => getPrivyEvmWallet(client), onLookupError);
|
|
387
|
+
if (!evmWallet) continue;
|
|
388
|
+
const item = await signPrivyEvmRequirement(client, evmWallet, requirement, url, options);
|
|
389
|
+
if (item) { signedAny = true; yield item; }
|
|
424
390
|
} else {
|
|
425
|
-
|
|
391
|
+
if (solWallet === undefined) solWallet = await lookupPrivyWallet('Solana', () => getPrivySolanaWallet(client), onLookupError);
|
|
392
|
+
if (!solWallet) continue;
|
|
393
|
+
const item = await signPrivySolanaRequirement(client, solWallet, requirement, url, options);
|
|
394
|
+
if (item) { signedAny = true; yield item; }
|
|
426
395
|
}
|
|
427
396
|
}
|
|
397
|
+
// A failed lookup only skips its own chain. When nothing was signed at all,
|
|
398
|
+
// surface the lookup error so a Privy setup problem keeps its reason.
|
|
399
|
+
if (!signedAny && lookupError) throw lookupError;
|
|
400
|
+
}
|
|
428
401
|
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
requirement,
|
|
447
|
-
solWallet.address,
|
|
448
|
-
recentBlockhash,
|
|
449
|
-
);
|
|
450
|
-
|
|
451
|
-
const signed = await signWithSessionCharge(svmDecision, async () => {
|
|
452
|
-
const signResult = await client.signSolanaTransaction(solWallet.id, txBase64);
|
|
453
|
-
const signedTx = signResult.data?.signed_transaction || signResult.signed_transaction;
|
|
454
|
-
if (!signedTx) throw new Error('Privy returned no signed transaction');
|
|
455
|
-
|
|
456
|
-
const payload = {
|
|
457
|
-
x402Version: 2,
|
|
458
|
-
payload: { transaction: signedTx },
|
|
459
|
-
accepted: requirement,
|
|
460
|
-
};
|
|
461
|
-
if (url) {
|
|
462
|
-
payload.resource = { url };
|
|
463
|
-
}
|
|
464
|
-
return Buffer.from(JSON.stringify(payload)).toString("base64");
|
|
465
|
-
});
|
|
466
|
-
if (!signed.ok) {
|
|
467
|
-
console.error(`[x402] ${signed.reason}`);
|
|
468
|
-
options.onCapRefusal?.(signed.reason);
|
|
469
|
-
continue;
|
|
470
|
-
}
|
|
402
|
+
/**
|
|
403
|
+
* Look up the Privy wallet for one chain. A failed lookup only skips that
|
|
404
|
+
* chain's options, so the remaining ranked options on the other chain are
|
|
405
|
+
* still tried. The error is passed to onError so the caller can report it
|
|
406
|
+
* if no option gets signed.
|
|
407
|
+
*/
|
|
408
|
+
async function lookupPrivyWallet(chain, lookup, onError) {
|
|
409
|
+
try {
|
|
410
|
+
const wallet = await lookup();
|
|
411
|
+
if (!wallet) console.error(`[x402] No Privy ${chain} wallet found for payment signing`);
|
|
412
|
+
return wallet || null;
|
|
413
|
+
} catch (err) {
|
|
414
|
+
console.error(`[x402] Privy ${chain} wallet lookup failed: ${err.message}`);
|
|
415
|
+
onError(err);
|
|
416
|
+
return null;
|
|
417
|
+
}
|
|
418
|
+
}
|
|
471
419
|
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
420
|
+
/**
|
|
421
|
+
* Sign one EVM option with the Privy wallet, or return null to skip it.
|
|
422
|
+
*/
|
|
423
|
+
async function signPrivyEvmRequirement(client, evmWallet, requirement, url, options) {
|
|
424
|
+
const decision = evaluatePaymentRequirement(requirement);
|
|
425
|
+
if (!decision.ok) {
|
|
426
|
+
console.error(`[x402] ${decision.reason}`);
|
|
427
|
+
return null;
|
|
428
|
+
}
|
|
429
|
+
// Privy signs an EIP-3009 TransferWithAuthorization. A Permit2 or unknown
|
|
430
|
+
// transfer method needs a different signature, which the server rejects as
|
|
431
|
+
// EIP-3009, so skip it like the WalletConnect signer does.
|
|
432
|
+
const method = requirement.extra?.assetTransferMethod;
|
|
433
|
+
if (method && method !== 'eip3009') {
|
|
434
|
+
console.error(`[x402] Skipping ${requirement.network} option: Privy wallets sign only EIP-3009 payments, not assetTransferMethod "${method}".`);
|
|
435
|
+
return null;
|
|
436
|
+
}
|
|
437
|
+
try {
|
|
438
|
+
if (!await hasSufficientEvmPaymentBalance(requirement, evmWallet.address)) return null;
|
|
439
|
+
const typedData = buildEIP712TypedData({
|
|
440
|
+
fromAddress: evmWallet.address,
|
|
441
|
+
requirement,
|
|
442
|
+
});
|
|
443
|
+
|
|
444
|
+
const signed = await signWithSessionCharge(decision, async () => {
|
|
445
|
+
const signResult = await client.ethSignTypedDataV4(evmWallet.id, typedData);
|
|
446
|
+
const signature = signResult.data?.signature || signResult.signature;
|
|
447
|
+
if (!signature) throw new Error('Privy returned no signature');
|
|
448
|
+
|
|
449
|
+
const authorization = {
|
|
450
|
+
from: evmWallet.address,
|
|
451
|
+
to: resolvePayTo(requirement),
|
|
452
|
+
value: resolvePaymentAmount(requirement).toString(),
|
|
453
|
+
validAfter: typedData.message.validAfter.toString(),
|
|
454
|
+
validBefore: typedData.message.validBefore.toString(),
|
|
455
|
+
nonce: typedData.message.nonce,
|
|
456
|
+
};
|
|
457
|
+
|
|
458
|
+
return buildPaymentSignatureHeader({
|
|
459
|
+
signature,
|
|
460
|
+
authorization,
|
|
461
|
+
resource: { url, description: "", mimeType: "" },
|
|
462
|
+
accepted: requirement,
|
|
463
|
+
});
|
|
464
|
+
});
|
|
465
|
+
if (!signed.ok) {
|
|
466
|
+
console.error(`[x402] ${signed.reason}`);
|
|
467
|
+
options.onCapRefusal?.(signed.reason);
|
|
468
|
+
return null;
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
return { signature: signed.value, network: requirement.network, asset: requirement.asset, payment: decision, releaseCharge: signed.release };
|
|
472
|
+
} catch (err) {
|
|
473
|
+
console.error(`[x402] Privy EVM signing failed for ${requirement.network}: ${err.message}`);
|
|
474
|
+
return null;
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
/**
|
|
479
|
+
* Sign one Solana option with the Privy wallet, or return null to skip it.
|
|
480
|
+
*/
|
|
481
|
+
async function signPrivySolanaRequirement(client, solWallet, requirement, url, options) {
|
|
482
|
+
const svmDecision = evaluatePaymentRequirement(requirement);
|
|
483
|
+
if (!svmDecision.ok) {
|
|
484
|
+
console.error(`[x402] ${svmDecision.reason}`);
|
|
485
|
+
return null;
|
|
486
|
+
}
|
|
487
|
+
try {
|
|
488
|
+
if (!await hasSufficientSvmPaymentBalance(requirement, solWallet.address)) return null;
|
|
489
|
+
const rpcUrl = getSolanaRpcUrl(requirement.network);
|
|
490
|
+
const recentBlockhash = await fetchRecentBlockhash(rpcUrl);
|
|
491
|
+
|
|
492
|
+
const { txBase64 } = buildUnsignedSvmTransaction(
|
|
493
|
+
requirement,
|
|
494
|
+
solWallet.address,
|
|
495
|
+
recentBlockhash,
|
|
496
|
+
);
|
|
497
|
+
|
|
498
|
+
const signed = await signWithSessionCharge(svmDecision, async () => {
|
|
499
|
+
const signResult = await client.signSolanaTransaction(solWallet.id, txBase64);
|
|
500
|
+
const signedTx = signResult.data?.signed_transaction || signResult.signed_transaction;
|
|
501
|
+
if (!signedTx) throw new Error('Privy returned no signed transaction');
|
|
502
|
+
|
|
503
|
+
const payload = {
|
|
504
|
+
x402Version: 2,
|
|
505
|
+
payload: { transaction: signedTx },
|
|
506
|
+
accepted: requirement,
|
|
507
|
+
};
|
|
508
|
+
if (url) {
|
|
509
|
+
payload.resource = { url };
|
|
477
510
|
}
|
|
478
|
-
|
|
479
|
-
|
|
511
|
+
return Buffer.from(JSON.stringify(payload)).toString("base64");
|
|
512
|
+
});
|
|
513
|
+
if (!signed.ok) {
|
|
514
|
+
console.error(`[x402] ${signed.reason}`);
|
|
515
|
+
options.onCapRefusal?.(signed.reason);
|
|
516
|
+
return null;
|
|
480
517
|
}
|
|
518
|
+
|
|
519
|
+
return { signature: signed.value, network: requirement.network, asset: requirement.asset, payment: svmDecision, releaseCharge: signed.release };
|
|
520
|
+
} catch (err) {
|
|
521
|
+
console.error(`[x402] Privy Solana signing failed for ${requirement.network}: ${err.message}`);
|
|
522
|
+
return null;
|
|
481
523
|
}
|
|
482
524
|
}
|