Trezor钱包JS开发文档

在线工具推荐: Three.js AI纹理开发包 - YOLO合成数据生成器 - GLTF/GLB在线编辑 - 3D模型格式在线转换 - 可编程3D场景编辑器

getPublicKey - 导出公钥

使用getPublicKey()方法获取指定路径的BIP32扩展公钥。将展示给 用户所请求密钥的描述并要求确认导出操作。

方法调用

ES6:

const result = await TrezorConnect.getPublicKey(params);

CommonJS:

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

});

调用参数:

可选的公共参数

导出单个公钥:

  • path:密钥路径,字符串或数值数组,必须
  • coin:数字货币代码,在coins.json中定义,可选。如果未设置该参数,则利用path参数进行推导
  • crossChain:是否跨链,可选

批量导出公钥:

  • bundle:导出目标对象的数组,每个对象包含path、coin和crossChain字段

示例代码

返回第5个比特币账户的公钥:

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

返回多个比特币账户的公钥:

TrezorConnect.getPublicKey({
    bundle: [
        { path: "m/49'/0'/0'" }, // account 1
        { path: "m/49'/0'/1'" }, // account 2
        { path: "m/49'/0'/2'" }  // account 3
    ]
});

返回结果

单个公钥的返回结果:

{
    success: true,
    payload: {
        path: Array<number>, // hardended path
        serializedPath: string, // serialized path
        xpub: string,        // xpub in legacy format
        xpubSegwit?: string, // optional for segwit accounts: xpub in segwit format
        chainCode: string,   // BIP32 serialization format
        childNum: number,    // BIP32 serialization format
        publicKey: string,   // BIP32 serialization format
        fingerprint: number, // BIP32 serialization format
        depth: number,       // BIP32 serialization format
    }
}

批量导出公钥返回结果:

{
    success: true,
    payload: [
        { path, serializedPath, xpub, xpubSegwit?, chainCode, childNum, publicKey, fingerprint, depth }, // account 1
        { path, serializedPath, xpub, xpubSegwit?, chainCode, childNum, publicKey, fingerprint, depth }, // account 2
        { path, serializedPath, xpub, xpubSegwit?, chainCode, childNum, publicKey, fingerprint, depth }  // account 3
    ]
}

错误信息:

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

从之前版本迁移

version 4及之前版本:

TrezorConnect.getXPubKey("m/49'/0'/0'", function(result) {
    result.path           // not changed
    result.serializedPath // not changed
    result.xpubkey        // renamed to "xpub"
    // added "xpubSegwit" field for segwit accounts
    result.chainCode      // not changed
    result.publicKey      // not changed
    // added "fingerprint" field
    // added "childNum" field
    // added "depth" field
});

version 5:

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