跳到主要内容

btcSignTransaction

Bitcoin:签名交易

对预构建交易中的指定输入和输出进行签名。签名前,用户需要在 Ukey Wallet 上核对收款地址、金额、找零和费用等。

const result = await HardwareSDK.btcSignTransaction(
connectId,
deviceId,
params,
);

参数

可选通用参数

  • coin - 必需 string,用于指定 bitcoin.json 文件中的网络定义。可以使用币种的 shortcutnamelabel
  • inputs - 必需 Array,由 TxInputType 组成。
  • outputs - 必需 Array,由 TxOutputType 组成。
  • refTxs - 必需 Array,由 RefTransaction 组成。Zcash 和 Komodo 的 refTxs 还应包含 expiryversion_group_idextra_data 字段。
  • locktime - 可选 number,交易锁定时间。
  • version - 可选 number,交易版本。
  • expiry - 可选 number,仅用于 Decred 和 Zcash。
  • versionGroupId - 可选 number,仅用于 Zcash;设置 overwintered 时对应 nVersionGroupId
  • overwintered - 可选 boolean,仅用于 Zcash。
  • timestamp - 可选 number,仅用于 Capricoin,表示交易时间戳。
  • branchId - 可选 number,仅用于 Zcash;设置 overwintered 时对应 BRANCH_ID

辅助工具

构建输入

private buildHardwareInput = async (
txInput: TxInput,
derivationPath: string,
): Promise<Messages.TxInputType> => {
const { getHDPath, getScriptType } = await CoreSDKLoader();
const hdPath = getHDPath(derivationPath);
const inputScriptType = getScriptType(hdPath);
const utxo = txInput.utxo as UTXO;

// @ts-expect-error
return {
prev_index: utxo.vout,
prev_hash: utxo.txid,
amount: utxo.value.integerValue().toString(),
address_n: hdPath,
script_type: inputScriptType,
};
};

构建输出

private buildHardwareOutput = async (
txOutput: TxOutput,
): Promise<Messages.TxOutputType> => {
const { isCharge, bip44Path } = txOutput.payload || {};

if (isCharge && bip44Path) {
const { getHDPath, getOutputScriptType } = await CoreSDKLoader();
const hdPath = getHDPath(bip44Path);
const outputScriptType = getOutputScriptType(hdPath);
return {
script_type: outputScriptType,
address_n: hdPath,
amount: txOutput.value.integerValue().toString(),
};
}

return {
script_type: 'PAYTOADDRESS',
address: txOutput.address,
amount: txOutput.value.integerValue().toString(),
};
};

构建前序交易

import * as BitcoinJS from 'bitcoinjs-lib';
private buildPrevTx = (rawTransaction: string): RefTransaction => {
const tx = BitcoinJS.Transaction.fromHex(rawTransaction);

return {
hash: tx.getId(),
version: tx.version,
inputs: tx.ins.map((inputItem) => ({
prev_hash: inputItem.hash.reverse().toString('hex'),
prev_index: inputItem.index,
script_sig: inputItem.script.toString('hex'),
sequence: inputItem.sequence,
})),
bin_outputs: tx.outs.map((outputItem) => ({
amount: outputItem.value,
script_pubkey: outputItem.script.toString('hex'),
})),
lock_time: tx.locktime,
};
};

示例

PAYTOADDRESS

HardwareSDK.btcSignTransaction(connectId, deviceId, {
inputs: [
{
address_n: [
(44 | 0x80000000) >>> 0,
(0 | 0x80000000) >>> 0,
(3 | 0x80000000) >>> 0,
1,
0,
],
prev_index: 0,
prev_hash:
"b035d89d4543ce5713c553d69431698116a822c57c03ddacf3f04b763d1999ac",
},
],
outputs: [
{
address_n: [
(44 | 0x80000000) >>> 0,
(0 | 0x80000000) >>> 0,
(3 | 0x80000000) >>> 0,
1,
1,
],
amount: "2981747",
script_type: "PAYTOADDRESS",
},
{
address: "1BoatSLRHtKNngkdXEeobR76b53LETtpyT",
amount: "400000",
script_type: "PAYTOADDRESS",
},
],
coin: "btc",
});

SPENDP2SHWITNESS

HardwareSDK.btcSignTransaction({
inputs: [
{
address_n: [
(49 | 0x80000000) >>> 0,
(0 | 0x80000000) >>> 0,
(3 | 0x80000000) >>> 0,
1,
0,
],
prev_index: 0,
prev_hash:
"b035d89d4543ce5713c553d69431698116a822c57c03ddacf3f04b763d1999ac",
amount: "3382047",
script_type: "SPENDP2SHWITNESS",
},
],
outputs: [
{
address_n: [
(49 | 0x80000000) >>> 0,
(0 | 0x80000000) >>> 0,
(3 | 0x80000000) >>> 0,
1,
1,
],
amount: "2981747",
script_type: "PAYTOP2SHWITNESS",
},
{
address: "1BoatSLRHtKNngkdXEeobR76b53LETtpyT",
amount: "400000",
script_type: "PAYTOADDRESS",
},
],
coin: "btc",
});

带 refTxs 的 PAYTOADDRESS

HardwareSDK.btcSignTransaction({
inputs: [
{
address_n: [
(44 | 0x80000000) >>> 0,
(0 | 0x80000000) >>> 0,
(3 | 0x80000000) >>> 0,
1,
0,
],
prev_index: 0,
prev_hash:
"b035d89d4543ce5713c553d69431698116a822c57c03ddacf3f04b763d1999ac",
},
],
outputs: [
{
address_n: [
(44 | 0x80000000) >>> 0,
(0 | 0x80000000) >>> 0,
(3 | 0x80000000) >>> 0,
1,
1,
],
amount: "2981747",
script_type: "PAYTOADDRESS",
},
{
address: "1BoatSLRHtKNngkdXEeobR76b53LETtpyT",
amount: "400000",
script_type: "PAYTOADDRESS",
},
],
refTxs: [
{
hash: "b035d89d4543ce5713c553d69431698116a822c57c03ddacf3f04b763d1999ac",
inputs: [
{
prev_hash:
"448946a44f1ef514601ccf9b22cc3e638c69ea3900b67b87517ea673eb0293dc",
prev_index: 0,
script_sig:
"47304402202872cb8459eed053dcec0f353c7e293611fe77615862bfadb4d35a5d8807a4cf022015057aa0aaf72ab342b5f8939f86f193ad87b539931911a72e77148a1233e022012103f66bbe3c721f119bb4b8a1e6c1832b98f2cf625d9f59242008411dd92aab8d94",
sequence: 4294967295,
},
],
bin_outputs: [
{
amount: 3431747,
script_pubkey: "76a91441352a84436847a7b660d5e76518f6ebb718dedc88ac",
},
{
amount: 10000,
script_pubkey: "76a9141403b451c79d34e6a7f6e36806683308085467ac88ac",
},
],
lock_time: 0,
version: 1,
},
],
coin: "btc",
});

返回结果

{
success: true,
payload: {
signatures: Array<string>, // 签名结果数组
serializedTx: string, // 序列化后的交易
txid?: string, // 广播后的交易 ID
}
}

错误

{
success: false,
payload: {
error: string, // 错误消息
code: number // 错误码
}
}