signTransaction - 签名交易
请求Trezor钱包签名之前构造的交易的指定输入输出。需要用户在Trezor 钱包上确认所有的交易详情。
调用方法
ES6:
const result = await TrezorConnect.signTransaction(params);
CommonJS:
TrezorConnect.signTransaction(params).then(function(result) {
});
参数:
可选的公共参数。
flowtype:
- coin:数字货币标识,必须
- inputs:交易输入对象数组,必须
- outputs:交易输出对象数组,必须
- refTxs:引用交易数组,可选
- locktime:可选数值
- version: 交易版本,数值,可选
- expiry:超时,数值,可选,仅用于Decred和Zcash
- versionGroupId:版本组ID,数值,可选,仅用于Zcash
- overwintered:可选布尔值,仅用于ZCash
- timestamp:交易时间戳,可选数值,仅用于Capricoin
- branchId:分支ID,可选数值,仅用于ZCash
- push:是否广播到区块链网络,布尔值,可选,默认:true
示例代码
PAYTOADDRESS
TrezorConnect.signTransaction({
inputs: [
{
address_n: [(44 | 0x80000000) >>> 0, (0 | 0x80000000) >>> 0, (2 | 0x80000000) >>> 0, 1, 0],
prev_index: 0,
prev_hash: 'b035d89d4543ce5713c553d69431698116a822c57c03ddacf3f04b763d1999ac',
}
],
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',
}
],
coin: 'btc'
});
SPENDP2SHWITNESS
TrezorConnect.signTransaction({
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',
}
],
coin: 'btc'
});
使用refTxs的PAYTOADDRESS:
TrezorConnect.signTransaction({
inputs: [
{
address_n: [(44 | 0x80000000) >>> 0, (0 | 0x80000000) >>> 0, (2 | 0x80000000) >>> 0, 1, 0],
prev_index: 0,
prev_hash: 'b035d89d4543ce5713c553d69431698116a822c57c03ddacf3f04b763d1999ac',
}
],
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'
});
返回结果
flowtype:
{
success: true,
payload: {
signatures: Array<string>, // Array of signer signatures
serializedTx: string, // serialized transaction
txid?: string, // broadcasted transaction id
}
}
错误信息
{
success: false,
payload: {
error: string // error message
}
}
从之前版本迁移
V4及更早版本:
var inputs = [{
address_n: [44 | 0x80000000, 0 | 0x80000000, 2 | 0x80000000, 1, 0],
prev_index: 0,
prev_hash: 'b035d89d4543ce5713c553d69431698116a822c57c03ddacf3f04b763d1999ac'
}];
var outputs = [{
address_n: [44 | 0x80000000, 0 | 0x80000000, 2 | 0x80000000, 1, 1],
amount: 3181747,
script_type: 'PAYTOADDRESS'
}, {
address: '18WL2iZKmpDYWk1oFavJapdLALxwSjcSk2',
amount: 200000,
script_type: 'PAYTOADDRESS'
}];
TrezorConnect.setCurrency('BTC');
TrezorConnect.signTx(
inputs, // amount field retyped to a string
outputs, // amount field retyped to a string
"example message",
function(result) {
result.signatures // not changed
result.serialized_tx // renamed to "serializedTx"
// added "txid" field if "push" is set to true
},
"bitcoin"
);
V5:
// params are key-value pairs inside Object
TrezorConnect.signTransaction({
inputs: [{
address_n: [44 | 0x80000000, 0 | 0x80000000, 2 | 0x80000000, 1, 0],
prev_index: 0,
prev_hash: 'b035d89d4543ce5713c553d69431698116a822c57c03ddacf3f04b763d1999ac'
}],
outputs: [{
address_n: [44 | 0x80000000, 0 | 0x80000000, 2 | 0x80000000, 1, 1],
amount: '3181747',
script_type: 'PAYTOADDRESS'
}, {
address: '18WL2iZKmpDYWk1oFavJapdLALxwSjcSk2',
amount: '200000',
script_type: 'PAYTOADDRESS'
}],
coin: "btc"
}).then(function(result) {
...
})