Skip to Content
ハードウェア

btcSignTransaction

Bitcoin: トランザクションに署名

準備済みトランザクションの入力と出力に対する署名を要求します。ユーザーはデバイス上でリクエストを確認して承認します。

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

パラメーター

任意の共通パラメーター

  • coin - 必須 string 次で指定されたネットワーク定義を決定します: bitcoin.json  ファイル。コインの shortcut, name または label を使用できます。
  • inputs - 必須 Array の TxInputType ,
  • outputs - 必須 Array の TxOutputType ,
  • refTxs - 必須 Array の RefTransaction 。Zcash と Komodo の refTxs には次も必要です: expiry, version_group_id および extra_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

トランザクションのパラメーターを構築

入力を構築

const buildHardwareInput = async ( input: TxInput, path: string, ): Promise<Messages.TxInputType> => { const { getHDPath, getScriptType } = await CoreSDKLoader(); const addressN = getHDPath(path); const scriptType = getScriptType(addressN); const utxo = input.utxo as UTXO; return { prev_index: utxo.vout, prev_hash: utxo.txid, amount: utxo.value.integerValue().toString(), address_n: addressN, script_type: scriptType, }; };

出力を構築

const buildHardwareOutput = async ( output: TxOutput, ): Promise<Messages.TxOutputType> => { const { isCharge, bip44Path } = output.payload || {}; if (isCharge && bip44Path) { const { getHDPath, getOutputScriptType } = await CoreSDKLoader(); const addressN = getHDPath(bip44Path); const scriptType = getOutputScriptType(addressN); return { script_type: scriptType, address_n: addressN, amount: output.value.integerValue().toString(), }; } return { script_type: 'PAYTOADDRESS', address: output.address, amount: output.value.integerValue().toString(), }; };

以前のトランザクションを構築

import * as BitcoinJS from 'bitcoinjs-lib'; const buildPrevTx = (rawTx: string): RefTransaction => { const tx = BitcoinJS.Transaction.fromHex(rawTx); return { hash: tx.getId(), version: tx.version, inputs: tx.ins.map((i) => ({ prev_hash: Buffer.from(i.hash).reverse().toString('hex'), prev_index: i.index, script_sig: Buffer.from(i.script).toString('hex'), sequence: i.sequence, })), bin_outputs: tx.outs.map((o) => ({ amount: o.value.toString(), script_pubkey: Buffer.from(o.script).toString('hex'), })), lock_time: tx.locktime, }; };

例

以下のスニペットはパラメーターの形式を示します。実際に使用可能な UTXO、一致する金額、完全な以前のトランザクションを使用してください。この固定データは、そのままブロードキャストできるトランザクションではありません。

PAYTOADDRESS

import type { BTCSignTransactionParams } from "@onekeyfe/hd-core"; // Load the actual previous transactions for the UTXOs below. declare const refTxs: BTCSignTransactionParams["refTxs"]; declare const previousOutputAmount: string; HardwareSDK.btcSignTransaction(connectId, deviceId, { inputs: [ { address_n: [(44 | 0x80000000) >>> 0, (0 | 0x80000000) >>> 0, (2 | 0x80000000) >>> 0, 1, 0], prev_index: 0, prev_hash: 'b035d89d4543ce5713c553d69431698116a822c57c03ddacf3f04b763d1999ac', amount: previousOutputAmount, } ], outputs: [ { address_n: [(44 | 0x80000000) >>> 0, (0 | 0x80000000) >>> 0, (2 | 0x80000000) >>> 0, 1, 1], amount: '3181747', script_type: 'PAYTOADDRESS', }, { address: '18WL2iZKmpDYWk1oFavJapdLALxwSjcSk2', amount: '200000', script_type: 'PAYTOADDRESS', } ], refTxs, coin: 'btc' });

SPENDP2SHWITNESS

import type { BTCSignTransactionParams } from "@onekeyfe/hd-core"; // Load the actual previous transactions for the UTXOs below. declare const refTxs: BTCSignTransactionParams["refTxs"]; declare const previousOutputAmount: string; HardwareSDK.btcSignTransaction(connectId, deviceId, { inputs: [ { address_n: [(49 | 0x80000000) >>> 0, (0 | 0x80000000) >>> 0, (2 | 0x80000000) >>> 0, 1, 0], prev_index: 0, prev_hash: 'b035d89d4543ce5713c553d69431698116a822c57c03ddacf3f04b763d1999ac', amount: '3382047', script_type: 'SPENDP2SHWITNESS', } ], outputs: [ { address_n: [(49 | 0x80000000) >>> 0, (0 | 0x80000000) >>> 0, (2 | 0x80000000) >>> 0, 1, 1], amount: '3181747', script_type: 'PAYTOP2SHWITNESS', }, { address: '18WL2iZKmpDYWk1oFavJapdLALxwSjcSk2', amount: '200000', script_type: 'PAYTOADDRESS', } ], refTxs, coin: 'btc' });

refTxs を使う PAYTOADDRESS (独自のバックエンドからトランザクションデータを提供)

HardwareSDK.btcSignTransaction(connectId, deviceId, { inputs: [ { address_n: [(44 | 0x80000000) >>> 0, (0 | 0x80000000) >>> 0, (2 | 0x80000000) >>> 0, 1, 0], prev_index: 0, prev_hash: 'b035d89d4543ce5713c553d69431698116a822c57c03ddacf3f04b763d1999ac', amount: '3431747', } ], outputs: [ { address_n: [(44 | 0x80000000) >>> 0, (0 | 0x80000000) >>> 0, (2 | 0x80000000) >>> 0, 1, 1], amount: '3181747', script_type: 'PAYTOADDRESS', }, { address: '18WL2iZKmpDYWk1oFavJapdLALxwSjcSk2', amount: '200000', 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>, // Array of signer signatures serializedTx: string, // serialized transaction txid?: string, // transaction ID, if returned; signing does not broadcast } }

エラー

{ success: false, payload: { error: string, // error message code?: string | number // error code } }
最終更新日