闪光 SDK
Spark是最快,最便宜,而且最UX友好的方式,在Bitcoin上建立金融应用程序和推出本土资产. 比特币(Bitcoin L2)让开发者以近乎零的成本立即移动比特币和比特币本土资产(包括稳定币),同时保持与比特币基础设施的完全连接.
完整文件,访问 https://docs.spark.money (英语)
安装
npm install @buildonspark/spark-sdk
# or
yarn add @buildonspark/spark-sdk
# or
pnpm add @buildonspark/spark-sdk
快速启动
初始化钱包
import { SparkWallet } from "@buildonspark/spark-sdk";
// Create a new wallet (generates a new mnemonic)
const { wallet, mnemonic } = await SparkWallet.initialize({
options: {
network: "MAINNET", // or "REGTEST" for testing
},
});
// Or initialize with an existing mnemonic
const { wallet } = await SparkWallet.initialize({
mnemonicOrSeed: "your twelve word mnemonic phrase here ...",
options: {
network: "MAINNET",
},
});
检查余额
const balance = await wallet.getBalance();
console.log(`Bitcoin balance: ${balance.balance} sats`);
console.log(`Token balances:`, balance.tokenBalances);
静态保存地址
静态存款地址可以重新使用,并允许您收到同一地址的多笔存款.
// Get a reusable static deposit address
const staticAddress = await wallet.getStaticDepositAddress();
console.log(`Static deposit address: ${staticAddress}`);
// After sending Bitcoin to the address from an external wallet,
// get a quote for claiming the deposit
const quote = await wallet.getClaimStaticDepositQuote(transactionId);
console.log(`Credit amount: ${quote.creditAmountSats} sats`);
console.log(`Fee: ${quote.feeSats} sats`);
// Claim the deposit with the quote
await wallet.claimStaticDeposit({
transactionId,
creditAmountSats: quote.creditAmountSats,
sspSignature: quote.signature,
});
// Or use the convenience method with a max fee
await wallet.claimStaticDepositWithMaxFee({
transactionId,
maxFee: 500, // Will throw if fee exceeds this amount
});
// Query all your static deposit addresses
const addresses = await wallet.queryStaticDepositAddresses();
// Get confirmed UTXOs for a deposit address
const utxos = await wallet.getUtxosForDepositAddress(staticAddress);
for (const utxo of utxos) {
console.log(`UTXO: ${utxo.txid}:${utxo.vout}`);
}
发送比特币
// Transfer to another Spark address
const transfer = await wallet.transfer({
receiverSparkAddress: "sp1q...",
amountSats: 10000,
});
// Withdraw to on-chain Bitcoin address
const withdrawal = await wallet.withdraw({
onchainAddress: "bc1q...",
amountSats: 50000,
exitSpeed: "FAST", // or "MEDIUM", "SLOW"
});
闪电付款
// Create a Lightning invoice
const invoice = await wallet.createLightningInvoice({
amountSats: 1000,
memo: "Payment for services",
});
console.log(`Invoice: ${invoice.invoice.encodedInvoice}`);
// Pay a Lightning invoice
const payment = await wallet.payLightningInvoice({
invoice: "lnbc...",
maxFeeSats: 100,
});
调试操作
// Get token balances
const { tokenBalances } = await wallet.getBalance();
for (const [tokenId, info] of tokenBalances) {
console.log(`${info.tokenMetadata.tokenName}: ${info.balance}`);
}
// Transfer tokens
const tokenTransfer = await wallet.transferTokens({
tokenIdentifier: "spark1...",
receiverSparkAddress: "sp1q...",
tokenAmount: 100n,
});
事件处理
import { SparkWalletEvent } from "@buildonspark/spark-sdk";
// Listen for incoming transfers
wallet.on(SparkWalletEvent.TransferClaimed, (transferId, newBalance) => {
console.log(`Received transfer ${transferId}, new balance: ${newBalance}`);
});
// Listen for confirmed deposits
wallet.on(SparkWalletEvent.DepositConfirmed, (depositId, newBalance) => {
console.log(`Deposit ${depositId} confirmed, new balance: ${newBalance}`);
});
平台支持
SDK支持多个 JavaScript 运行时间 :
- 浏览器
- 节点.js
- 原生反应