cipherKeyValue - 对称加密键值
cipherKeyValue方法提供Trezor设备的对称加密能力,用户需要确认 钱包显示的加密/解密。用于加密的密钥从BIP地址的私钥构造,密钥 将显示在钱包显示屏上。不同的路径、密钥或确认信息将得到不同的 加密密钥和IV。因此,你不能使用不同的输入来跳过确认步骤。IV 可以手工设置,或者使用密钥算出。值必须16字节块对齐。应用需要 自己补齐块以确保安全性。例如,使用PKCS7。
详细信息可参考 SLIP-0011
调用方法
ES6:
const result = await TrezorConnect.cipherKeyValue(params);
CommonJS:
TrezorConnect.cipherKeyValue(params).then(function(result) {
});
调用参数:
可选的公共参数:
- useEmptyPassphrase:始终设置为true,本方法忽略
加密单个值:
- path:密钥路径,字符串或数值数组,必须
- key:钱包显示的文本,可选
- value:待加密内容,16字节倍长的16进制字符串,可选
- askOnEncrypt:是否需要用户确认加密,布尔值,可选
- askOnDecrypt:是否需要用户确认解密,布尔值,可选
- iv:初始化向量,可选
加密多个值:
- bundle:上述对象数组
示例代码
返回加密数据:
TrezorConnect.cipherKeyValue({
path: "m/49'/0'/0'",
key: "This text is displayed on Trezor during encrypt",
value: "1c0ffeec0ffeec0ffeec0ffeec0ffee1",
encrypt: true,
askOnEncrypt: true,
askOnDecrypt: true
});
返回批量加密数据:
TrezorConnect.cipherKeyValue({
bundle: [
{ path: "m/49'/0'/0'", key: "1 text on Trezor", value: "1c0ffeec0ffeec0ffeec0ffeec0ffee1", encrypt: true },
{ path: "m/49'/0'/1'", key: "2 text on Trezor", value: "1c0ffeec0ffeec0ffeec0ffeec0ffee1", encrypt: false },
{ path: "m/49'/0'/2'", key: "3 text on Trezor", value: "1c0ffeec0ffeec0ffeec0ffeec0ffee1" }
]
});
返回结果
单值返回结果:
{
success: true,
payload: {
value: string
}
}
批量返回结果:
{
success: true,
payload: [
{ value: string },
{ value: string },
{ value: string }
]
}
错误信息
{
success: false,
payload: {
error: string // error message
}
}
从之前版本迁移
版本4及更早:
TrezorConnect.cipherKeyValue(
"m/49'/0'/0'", // path
"This is displayed on Trezor during encrypt", // key
"1c0ffeec0ffeec0ffeec0ffeec0ffee1", // value
true, // encrypt
true, // ask on encrypt
true, // ask on decrypt
function(result) { // callback
// result not changed
}
);
版本5:
// params are key-value pairs inside Object
TrezorConnect.cipherKeyValue({
path: "m/49'/0'/0'",
key: "This is displayed on Trezor during encrypt",
value: "1c0ffeec0ffeec0ffeec0ffeec0ffee1",
encrypt: true,
askOnEncrypt: true,
askOnDecrypt: true
}).then(function(result) {
// result not changed
})