@bitgo/sdk-coin-kaspa (英语)
BitGo 的 SDK 模块 卡斯帕(KASAP) 块链.
概览
Kaspa是一个基于GHOSTDAG协议(中本共识的概括)的工作证明区块链,使得高区块率成为可能. 此模块执行 Kaspa 的 UTXO 模型 秒256k1 坐标 Schnor 签名 (BIP-143类似叹息)和习俗 喀斯巴/喀斯巴东 bech32 地址格式 .
支持的硬币标识符 :
kaspa——卡斯帕主网tkaspa- 卡斯帕测试网
特征
- 键对生成( sep 256k1)
- 地址衍生( kaspa bech32 P2PK Schnor)
- UTXO 交易大楼
- Schnorr 签名和核查(Blake2b-256 sighash)
- TSS/ MPC 支持( ECDSA 算法, 每输入 DKLS 会话)
- 全面连锁往返(hex/JSON)
安装
yarn add @bitgo/sdk-coin-kaspa
使用量
1. 用 BitGo SDK 注册
import { BitGo } from 'bitgo';
import { register } from '@bitgo/sdk-coin-kaspa';
const bitgo = new BitGo({ env: 'prod' });
register(bitgo);
const kaspa = bitgo.coin('kaspa');
const tkaspa = bitgo.coin('tkaspa'); // testnet
或者直接即时处理(在测试或脚本中有用):
import { Kaspa, Tkaspa } from '@bitgo/sdk-coin-kaspa';
const kaspa = Kaspa.createInstance(bitgo);
const tkaspa = Tkaspa.createInstance(bitgo);
2. 钥匙生成
// Random key pair
const kp = kaspa.generateKeyPair();
console.log(kp.pub); // 66-char hex compressed secp256k1 public key
console.log(kp.prv); // 64-char hex private key
// Deterministic from a 32-byte seed
const seed = Buffer.from('...32 bytes...', 'hex');
const kpFromSeed = kaspa.generateKeyPair(seed);
// Validation
kaspa.isValidPub(kp.pub); // true
kaspa.isValidPrv(kp.prv); // true
使用 KeyPair 直接 :
import { KeyPair } from '@bitgo/sdk-coin-kaspa';
const keyPair = new KeyPair(); // random
const { pub, prv } = keyPair.getKeys();
3. 地址生成
import { KeyPair } from '@bitgo/sdk-coin-kaspa';
const keyPair = new KeyPair({ prv: '<64-char-hex-private-key>' });
const mainnetAddress = keyPair.getAddress('mainnet'); // kaspa:qq...
const testnetAddress = keyPair.getAddress('testnet'); // kaspatest:qq...
kaspa.isValidAddress(mainnetAddress); // true
计算 P2PK scriptPublicKey 用于键(在构建 UTXO 输入时需要):
import { compressedToXOnly, buildP2PKScriptPublicKey } from '@bitgo/sdk-coin-kaspa';
const xOnlyPub = compressedToXOnly(Buffer.from(pub, 'hex'));
const scriptPublicKey = buildP2PKScriptPublicKey(xOnlyPub).toString('hex');
4. 建立交易
import { TransactionBuilderFactory, Transaction } from '@bitgo/sdk-coin-kaspa';
import { coins } from '@bitgo/statics';
import type { KaspaUtxoInput } from '@bitgo/sdk-coin-kaspa';
const utxo: KaspaUtxoInput = {
transactionId: '<64-char-hex-prev-tx-id>',
transactionIndex: 0,
amount: '100000000', // 1 KASPA in sompi (1e8)
scriptPublicKey: '<hex>', // P2PK script of the sender's key
sequence: '0',
sigOpCount: 1,
};
const factory = new TransactionBuilderFactory(coins.get('kaspa'));
const builder = factory.getBuilder();
builder
.addInput(utxo)
.to('kaspa:qq...recipient...', '99998000') // amount in sompi
.fee('2000');
const tx = (await builder.build()) as Transaction;
多个输入 :
builder
.addInput(utxo1)
.addInput(utxo2)
.to(recipientAddress, '299996000')
.fee('4000');
5. 签名-路径A:直接私人密钥(非TSS)
// `tx.sign` takes a 32-byte Buffer (raw private key)
tx.sign(Buffer.from(privateKeyHex, 'hex'));
// Signs every input at once. Fully signed → txHex; partial → halfSigned
const signedTxHex = tx.toHex(); // SDK-internal format for round-trips
// Or via the coin interface:
const result = await kaspa.signTransaction({
txPrebuild: { txHex: unsignedTxHex },
prv: privateKeyHex,
} as any) as { txHex: string };
6. 签名 — 路径 B: TSS / MPC (按输入 DKLS 会话)
卡斯帕基于UTXO:每个输入都有自有的叹息(Blake2b-256,BIP-143类似). 每项投入 需要独立的 DKLS 会话 ——没有生产N有效的方法 施诺尔从单一的签名操作中签名.
const unsignedTx = (await builder.build()) as Transaction;
const txHex = unsignedTx.toHex();
// Step 1: one sighash Buffer per input — the messages for each DKLS session
const sighashes: Buffer[] = unsignedTx.signablePayloads; // Buffer[N]
// Step 2: run N DKLS sessions in parallel (one per sighash)
// Each session produces a 64-byte raw Schnorr signature
// Step 3: collect results and call signTransaction
const signatures = sighashes.map((hash, inputIndex) => ({
inputIndex,
pubKey: compressedPubKeyHex, // 33-byte hex
signature: dklsSession(hash), // 64-byte hex Schnorr sig
}));
const result = await kaspa.signTransaction({
txPrebuild: { txHex },
signatures,
} as any) as { txHex: string } | { halfSigned: { txHex: string } };
// result.txHex → all inputs signed
// result.halfSigned → some inputs still unsigned (partial TSS round)
7. 广播
// toBroadcastFormat() returns the Kaspa RPC-compatible JSON string
const broadcastPayload = tx.toBroadcastFormat();
// toHex() is the SDK-internal round-trip format (preserves amount + scriptPublicKey
// on inputs for sighash recomputation). Do NOT send this to the Kaspa node directly.
const internalHex = tx.toHex();
8. 解释/分析交易
// Human-readable breakdown
const explained = await kaspa.explainTransaction({ txHex });
console.log(explained.outputs); // [{ address, amount }]
console.log(explained.outputAmount); // total sent (sompi)
console.log(explained.fee); // fee (sompi)
// Structured parse — inputs and outputs tagged with coin name
const parsed = await kaspa.parseTransaction({ txHex } as any);
// { inputs: [{ amount, coin: 'kaspa' }], outputs: [{ address, amount, coin: 'kaspa' }] }
9. 核查交易
const valid = await kaspa.verifyTransaction({
txPrebuild: { txHex },
txParams: {
recipients: [{ address: 'kaspa:qq...', amount: '99998000' }],
},
} as any);
console.log(valid); // true
10. 铜属性
kaspa.getChain(); // 'kaspa'
kaspa.getFamily(); // 'kaspa'
kaspa.getFullName(); // 'Kaspa'
kaspa.getBaseFactor(); // 100_000_000 (sompi per KASPA)
kaspa.supportsTss(); // true
kaspa.getMPCAlgorithm(); // 'ecdsa'
tkaspa.getChain(); // 'tkaspa'
tkaspa.getFullName(); // 'Testnet Kaspa'
密钥常数
| 财产 | 数值 |
|---|---|
| 1个KSPA | 100_000_000 索姆皮语 |
getBaseFactor() | 1e8 |
| 主网地址前缀 | kaspa: |
| 测试网地址前缀 | kaspatest: |
| 地址类型 | P2PK 施诺尔(x-只分秒256k1) |
| 签名算法 | 施诺尔语(Blake2b-256 sighash) |
| TSS 算法 | ecdsa (DKLS) (韩语) |
| 多西格类型 | onchain |
模块结构
src/
├── kaspa.ts # AbstractKaspaLikeCoin, Kaspa, Tkaspa classes
├── register.ts # SDK registration helper
├── index.ts
└── lib/
├── constants.ts # Chain constants (prefixes, decimals, default fee)
├── iface.ts # TypeScript interfaces
├── keyPair.ts # secp256k1 key pair + address derivation
├── sighash.ts # Blake2b-256 Schnorr sighash + script utilities
├── transaction.ts # Transaction class (sign / verify / explain / serialize)
├── transactionBuilder.ts # UTXO transaction builder
├── transactionBuilderFactory.ts
├── utils.ts # Address validation and encoding
└── index.ts
test/
├── fixtures/
│ └── kaspa.fixtures.ts # Deterministic test vectors
└── unit/
├── coin.test.ts
├── keyPair.test.ts
├── transaction.test.ts
├── transactionBuilder.test.ts
└── utils.test.ts
地址格式
Kaspa 使用自定义的 cashaddr- like bech32 编码 :
- 主网:
kaspa:<bech32-encoded-data> - 测试网 :
kaspatest:<bech32-encoded-data> - 版本字节
0= Schnorr P2PK (x-只为secp 256k1 pubkey)
签名
卡斯帕的使用 秒256k1以上的Schnor签名 带有 布莱克2b-256 叹息。 sighash的预画遵循了Kaspa BIP-143等规格. 每个输入独立签名,产生65字节签名:64字节 Schnor + 1字节 sighash type(0x01 {\FN黑体\FS22\BORD1\SHAD0\3AHBE\4AH00\FSCX67\FSCY66\2CHFFFFFF\3CH808080}请说.