Trezor钱包集成JS开发文档:比特币/比特币现金/比特币黄金/莱特币/

getAddress - 导出地址

显示并返回Trezor钱包指定BIP32路径的地址。要求用户 在Trezor钱包上确认导出操作。

调用方法

ES6:

const result = await TrezorConnect.getAddress(params);
CommonJS:

TrezorConnect.getAddress(params).then(function(result) {

});

调用参数:

可选的公共参数。

导出单个地址:

  • path:BIP32路径,必须
  • address:验证用的地址,可选
  • showOnTrezor:是否在Trezor钱包上显示,布尔值,默认:true
  • coin:数字货币标识字符串,可选,未设置的话则从path推导
  • crossChain:是否跨链,布尔值,可选 批量导出地址:

  • bundle:上述对象数组

示例代码

显示第一个比特币账户的第三个地址:

TrezorConnect.getAddress({
    path: "m/49'/0'/0'/0/2",
    coin: "btc"
});

返回第一个比特币账户的一组地址,不显示在Trezor设备上:

TrezorConnect.getAddress({
    bundle: [
        { path: "m/49'/0'/0'/0/0", showOnTrezor: false }, // address 1
        { path: "m/49'/0'/0'/0/1", showOnTrezor: false }, // address 2
        { path: "m/49'/0'/0'/0/2", showOnTrezor: false }  // address 3
    ]
});

使用应用中的自定义UI验证地址:

import TrezorConnect, { UI } from 'trezor-connect';

TrezorConnect.on(UI.ADDRESS_VALIDATION, data => {
    console.log("Handle button request", data.address, data.serializedPath);
    // here you can display custom UI inside of your app
});

const result = await TrezorConnect.getAddress({
    path: "m/49'/0'/0'/0/0",
    address: "3L6TyTisPBmrDAj6RoKmDzNnj4eQi54gD2",
});
// dont forget to hide your custom UI after you get the result!

返回结果

返回单个地址:

{
    success: true,
    payload: {
        address: string,     // displayed address
        path: Array<number>, // hardended path
        serializedPath: string,
    }
}

批量返回地址:

{
    success: true,
    payload: [
        { address: string, path: Array<number>, serializedPath: string }, // address 1
        { address: string, path: Array<number>, serializedPath: string }, // address 2
        { address: string, path: Array<number>, serializedPath: string }, // address 3
    ]
}

错误信息:

{
    success: false,
    payload: {
        error: string // error message
    }
}

从之前版本迁移

V4或更早版本:

TrezorConnect.getAddress("m/49'/0'/4'/0/0", function(result) {
    // added "serializedPath" field
});

应当是:

// params are key-value pairs inside Object
TrezorConnect.getAddress({ 
    path: "m/49'/0'/4'/0/0" 
}).then(function(result) {
    ...
})

getAccountInfo - 获取账户信息

获取指定账户的信息。

调用方法

ES6:

const result = await TrezorConnect.getAccountInfo(params);
CommonJS:

TrezorConnect.getAccountInfo(params).then(function(result) {

});

参数:

可选的公共参数。

使用路径:

  • path:BIP32路径,必须
  • coin:数字货币标识,字符串,必须 使用公钥:

  • xpub:账户的扩展公钥,字符串,必须

  • coin:数字货币标识,字符串,必须 使用自动发现:

执行BIP-0044账户自动发现,提交给用户一组账户进行选择。

  • coin:数字货币标识

示例代码

获取第一个比特币账户的信息:

TrezorConnect.getAccountInfo({
    path: "m/49'/0'/0'",
    coin: "btc",
});

使用公钥获取账户信息:

TrezorConnect.getAccountInfo({
    xpub: "xpub6CVKsQYXc9awxgV1tWbG4foDvdcnieK2JkbpPEBKB5WwAPKBZ1mstLbKVB4ov7QzxzjaxNK6EfmNY5Jsk2cG26EVcEkycGW4tchT2dyUhrx",
    coin: "btc",
});

使用BIP-0044账户发现机制获取账户信息:

TrezorConnect.getAccountInfo({
    coin: "btc",
});

返回结果

{
    success: true,
    payload: {
        id: number,                           // account id
        path: Array<number>,                  // hardended path
        serializedPath: string,               // serialized path
        xpub: string,                         // public key
        address: string,                      // current address
        addressIndex: number,                 // current address index
        addressPath: Array<number>,           // hardended address path
        addressSerializedPath: Array<number>, // serialized address path
        balance: number,                      // account balance (including unconfirmed transactions)
        confirmed: number,                    // account confirmed balance
        transactions: number,                 // transactions count
        utxo: Array<Utxo>,                    // unspent outputs [detail](../../src/js/types/response.js#L57)
        usedAddresses: Array<{ address: string, received: number }>, // used addresses with received amount
        unusedAddresses: Array<string>,       // unused addresses
    }
}

错误信息:

{
    success: false,
    payload: {
        error: string // error message
    }
}

从之前版本迁移

v4及更早版本:

TrezorConnect.getAccountInfo("m/49'/0'/0'", function(result) {
    result.id               // not changed
    result.serializedPath   // not changed
    result.path             // not changed
    result.xpub             // not changed
    result.freshAddress     // renamed to "address"
    result.freshAddressPath // renamed to "addressPath"
    result.freshAddressId   // renamed to "addressIndex"
    result.serializedFreshAddressPath // renamed to "addressSerializedPath"
    result.balance   // not changed
    result.confirmed // not changed

});

应当是:

// params are key-value pairs inside Object
TrezorConnect.composeTransaction({ 
    path: "m/49'/0'/0'",
    coin: "btc"
}).then(function(result) {
    ...
})

composeTransaction -

请求Trezor钱包向一组指定输出进行支付。在内部执行BIP-0044 账户发现并提交给用户一组可选账户。在用户选择账户后,继续提示 用户选择手续费。确定手续费之后,钱包签名交易并返回16进制 字符串格式的裸交易。如果需要的话,找零输出会自动添加。

调用方法

ES6:

const result = await TrezorConnect.composeTransaction(params);
CommonJS:

TrezorConnect.composeTransaction(params).then(function(result) {

});

参数:

可选的公共参数。

  • outputs:支付接受对象数组,必须
  • coin:数字货币标识,必须
  • push:是否广播到区块链网络中,布尔值,可选
  • Output对象:

常规输出:

  • amount:发送数量,单位:satoshi,必须
  • address:接收地址,必须 send-max - 消费指定账户的所有可用输入:

  • type: 必须,send-max

  • address: 接收地址,必须 opreturn:

  • type:必须,opreturn

  • dataHex:16进制字符串,可选

示例代码

发送 0.002 BTC 给 "18WL2iZKmpDYWk1oFavJapdLALxwSjcSk2":

TrezorConnect.composeTransaction({
    outputs: [
        { amount: "200000", address: "18WL2iZKmpDYWk1oFavJapdLALxwSjcSk2" }
    ]
    coin: "btc",
    push: true
});

返回结果

{
    success: true,
    payload: {
        signatures: Array<string>, // signer signatures
        serializedTx: string,        // serialized transaction 
        txid?: string,             // blockchain transaction id
    }
}

错误信息:

{
    success: false,
    payload: {
        error: string // error message
    }
}

从之前版本迁移

V4及更早版本:

var recipients = [{
    amount: 200000,
    address: "18WL2iZKmpDYWk1oFavJapdLALxwSjcSk2"
}];

TrezorConnect.setCurrency("btc");
TrezorConnect.composeAndSignTx(recipients, function(result) {
    result.signatures    // not changed
    result.serialized_tx // renamed to "serializedTx"
    // added "txid" field if "push" is set to true
});

V5:

var recipients = [{
    amount: "200000",
    address: "18WL2iZKmpDYWk1oFavJapdLALxwSjcSk2"
}];
// params are key-value pairs inside Object
TrezorConnect.composeTransaction({ 
    outputs: recipients,
    coin: "btc"
}).then(function(result) {
    ...
})

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) {
    ...
})

pushTransaction - 广播交易

将交易广播到所选的区块链网络中。

调用方法

ES6:

const result = await TrezorConnect.pushTransaction(params);
CommonJS:

TrezorConnect.pushTransaction(params).then(function(result) {

});

参数:

可选的公共参数。

flowtype:

  • tx:序列化交易,必须
  • coin:数字货币网络标识,必须

示例代码

TrezorConnect.pushTransaction({
    tx: "010000000182488650ef25a58fef6788bd71b8212038d7f2bbe4750bc7bcb44701e85ef6d5000000006b4830450221009a0b7be0d4ed3146ee262b42202841834698bb3ee39c24e7437df208b8b7077102202b79ab1e7736219387dffe8d615bbdba87e11477104b867ef47afed1a5ede7810121023230848585885f63803a0a8aecdd6538792d5c539215c91698e315bf0253b43dffffffff0160cc0500000000001976a914de9b2a8da088824e8fe51debea566617d851537888ac00000000",
    coin: "btc"
});

返回结果

flowtype:

{
    success: true,
    payload: {
        txid: string, // transaction id
    }
}

错误信息:

{
    success: false,
    payload: {
        error: string // error message
    }
}

从之前版本迁移

V4及更早版本:

var tx = "010000000182488650ef25a58fef6788bd71b8212038d7f2bbe4750bc7bcb44701e85ef6d5000000006b4830450221009a0b7be0d4ed3146ee262b42202841834698bb3ee39c24e7437df208b8b7077102202b79ab1e7736219387dffe8d615bbdba87e11477104b867ef47afed1a5ede7810121023230848585885f63803a0a8aecdd6538792d5c539215c91698e315bf0253b43dffffffff0160cc0500000000001976a914de9b2a8da088824e8fe51debea566617d851537888ac00000000";
TrezorConnect.pushTransaction(tx, function(result) {
    ...
});
V5:

// params are key-value pairs inside Object
TrezorConnect.pushTransaction({ 
    tx: "010000000182488650ef25a58fef6788bd71b8212038d7f2bbe4750bc7bcb44701e85ef6d5000000006b4830450221009a0b7be0d4ed3146ee262b42202841834698bb3ee39c24e7437df208b8b7077102202b79ab1e7736219387dffe8d615bbdba87e11477104b867ef47afed1a5ede7810121023230848585885f63803a0a8aecdd6538792d5c539215c91698e315bf0253b43dffffffff0160cc0500000000001976a914de9b2a8da088824e8fe51debea566617d851537888ac00000000",
    coin: "btc"
}).then(function(result) {
    ...
})

signMessage - 签名消息

请求Trezor钱包使用指定BIP32路径对应的私钥签名消息。

调用方法

ES6:

const result = await TrezorConnect.signMessage(params);
CommonJS:

TrezorConnect.signMessage(params).then(function(result) {

});

参数:

可选的公共参数

flowtype:

  • path:BIP32路径,必须
  • message:要签名的消息,必须
  • coin:数字货币标识,可选

示例代码

TrezorConnect.signMessage({
    path: "m/44'/0'/0'",
    message: "example message"
});

返回结果

flowtype:

{
    success: true,
    payload: {
        address: string,   // signer address
        signature: string, // signature in base64 format
    }
}

错误信息

{
    success: false,
    payload: {
        error: string // error message
    }
}

从早期版本迁移

V4及更早版本:

TrezorConnect.signMessage("m/44'/0'/0'", "example message", function(result) {
    ...
}, "bitcoin");

V5:

// params are key-value pairs inside Object
TrezorConnect.signMessage({ 
    path: "m/44'/0'/0'",
    message: "example message"
}).then(function(result) {
    ...
})

verifyMessagee - 验证消息

请求设备使用签名地址和签名验证消息。

调用方法

ES6:

const result = await TrezorConnect.verifyMessage(params);
CommonJS:

TrezorConnect.verifyMessage(params).then(function(result) {

});

参数:

可选的公共参数。

flowtype:

  • address:签名地址,必须
  • message:已签名消息,必须
  • signature:签名,base64格式,必须
  • coin:数字货币标识

示例代码

TrezorConnect.verifyMessage({
    address: "3BD8TL6iShVzizQzvo789SuynEKGpLTms9",
    message: "example message",
    signature: "JO7vL3tOB1qQyfSeIVLvdEw9G1tCvL+lNj78XDAVM4t6UptADs3kXDTO2+2ZeEOLFL4/+wm+BBdSpo3kb3Cnsas=",
    coin: "btc"
});

返回结果

flowtype:

{
    success: true,
    payload: {
        message: "Message verified"
    }
}

错误信息:

{
    success: false,
    payload: {
        error: string // error message
    }
}

从早期版本迁移

V4及更早版本:

var address = "3BD8TL6iShVzizQzvo789SuynEKGpLTms9";
var signature = "JO7vL3tOB1qQyfSeIVLvdEw9G1tCvL+lNj78XDAVM4t6UptADs3kXDTO2+2ZeEOLFL4/+wm+BBdSpo3kb3Cnsas=";
TrezorConnect.verifyMessage(
    address,
    signature,
    "example message",
    function(result) {
    ...
}, "bitcoin");

V5:

// params are key-value pairs inside Object
TrezorConnect.verifyMessage({ 
    address: "3BD8TL6iShVzizQzvo789SuynEKGpLTms9",
    signature: "JO7vL3tOB1qQyfSeIVLvdEw9G1tCvL+lNj78XDAVM4t6UptADs3kXDTO2+2ZeEOLFL4/+wm+BBdSpo3kb3Cnsas=",
    message: "example message",
    coin: "btc"
}).then(function(result) {
    ...
})
全部评论(0)
给作者留言