@alchemy/x402 (中文(简体) )
用于Alchemy x402认证和付款的CLI和库. Handles SIWE (Sign-In With Ethereum) 和 SIWS (Sig-In With Solana) 认证, 加上对Alchemy代理网关的每个请求付款x402 .
安装
npm install @alchemy/x402
# or
yarn add @alchemy/x402
# or
pnpm add @alchemy/x402
# or
bun add @alchemy/x402
国 际
所有命令接受一个 --architecture 要在 EVM 和 SVM (索拉纳) 之间切换的旗帜 。 默认为 evm。 。 。 .
EVM (Ethereum, Base等) (英语)
# Generate a new EVM wallet
npx @alchemy/x402 wallet generate
# Import an existing wallet (accepts hex key or path to a key file)
npx @alchemy/x402 wallet import --private-key 0xac09...
npx @alchemy/x402 wallet import --private-key /path/to/keyfile
# Generate a SIWE token
npx @alchemy/x402 sign --private-key /path/to/keyfile --expires-after 1h
# Create an x402 payment from a PAYMENT-REQUIRED header
npx @alchemy/x402 pay --private-key /path/to/keyfile --payment-required <header>
SVM(索拉纳语)
# Generate a new Solana wallet
npx @alchemy/x402 wallet generate --architecture svm
# Import an existing wallet (accepts base58 key, JSON byte array, or path to a key file)
npx @alchemy/x402 wallet import --private-key <base58-key> --architecture svm
npx @alchemy/x402 wallet import --private-key /path/to/keyfile --architecture svm
# Generate a SIWS token
npx @alchemy/x402 sign --private-key /path/to/keyfile --expires-after 1h --architecture svm
# Create an x402 payment from a PAYMENT-REQUIRED header
npx @alchemy/x402 pay --private-key /path/to/keyfile --payment-required <header> --architecture svm
图书馆
import {
// EVM
signSiwe,
generateWallet,
getWalletAddress,
createPayment,
buildX402Client,
// SVM (Solana)
signSiws,
generateSolanaWallet,
getSolanaWalletAddress,
createSolanaPayment,
buildSolanaX402Client,
// Enum
Architecture,
} from "@alchemy/x402";
生成钱包
// EVM
const evmWallet = generateWallet();
// { privateKey: "0x...", address: "0x..." }
const evmAddress = getWalletAddress("0x<private-key>");
// SVM (Solana)
const solWallet = generateSolanaWallet();
// { privateKey: "<base58>", address: "<base58>" }
const solAddress = getSolanaWalletAddress("<base58-secret-key>");
签名认证符
// EVM — SIWE (Sign-In With Ethereum)
const siweToken = await signSiwe({
privateKey: "0x<private-key>",
expiresAfter: "1h", // optional, default "1h"
});
// SVM — SIWS (Sign-In With Solana)
const siwsToken = await signSiws({
privateKey: "<base58-secret-key>",
expiresAfter: "1h", // optional, default "1h"
});
创建一个 x402 付款
// EVM
const evmPayment = await createPayment({
privateKey: "0x<private-key>",
paymentRequiredHeader: "<base64-encoded PAYMENT-REQUIRED header>",
});
// SVM (Solana)
const solPayment = await createSolanaPayment({
privateKey: "<base58-secret-key>",
paymentRequiredHeader: "<base64-encoded PAYMENT-REQUIRED header>",
});
使用@ x402/ fatch
402自动付款处理的全程请求协调,使用 buildX402Client 与 @x402/fetch数字 :
import { buildX402Client, signSiwe } from "@alchemy/x402";
import { wrapFetchWithPayment } from "@x402/fetch";
const privateKey = "0x<private-key>";
const client = buildX402Client(privateKey);
const siweToken = await signSiwe({ privateKey });
// Wrap fetch with SIWE auth
const authedFetch: typeof fetch = async (input, init) => {
const headers = new Headers(init?.headers);
headers.set("Authorization", `SIWE ${siweToken}`);
return fetch(input, { ...init, headers });
};
// Wrap with automatic x402 payment handling
const paymentFetch = wrapFetchWithPayment(authedFetch, client);
const response = await paymentFetch("https://x402.alchemy.com/...");
私钥输入
EVM( 视频)
所有 EVM 命令和函数接受私有密钥为:
- 十六进制字符串为
0x前缀 :0xac09... - 原始十六进制字符串 :
ac09... - 文件路径 :
/path/to/keyfile
SVM(索拉纳语)
所有 SVM 命令和函数接受私有密钥为:
- 密码为64字节的密钥
- JSON 字节数组( Solana CLI 格式):
[1, 2, 3, ...] - 包含任一格式的文件路径 :
/path/to/keyfile
维护者
见 安保人员。 。 。 .