Skip to main content

Bitcoin Cash

Keystone is supporting BitcoinCash via OKX Wallet.

Connect with Keystone

For Bitcoin Cash, Keystone defines the new UR type crypto-multi-accounts to expose the public keys. Software can utilize these data to generate the desired addresses. Developers can use the SDK to retrieve and parse this data from the QR Code displayed on the Keystone device.

Here is a sample code snippet to scan the animated QR code and parse the data:

import KeystoneSDK, {UR, URType} from "@keystonehq/keystone-sdk"
import {AnimatedQRScanner} from "@keystonehq/animated-qr"

/**
* Represents a component that handles the scanning of an animated QR code to retrieve
* the crypto hdkey information from a Keystone hardware wallet.
*
* The component uses the `AnimatedQRScanner` from `@keystonehq/animated-qr` to scan the QR code,
* and the `KeystoneSDK` to parse the scanned data into a human-readable account information format.
*/

const Account = () => {

/**
* Callback function to handle successful QR code scans.
*
* @param {Object} data - The data object containing the type and cbor encoded string.
* @param {string} data.type - The type of the scanned data.
* @param {string} data.cbor - The cbor encoded string representing the account information.
*/
const onSucceed = ({type, cbor}) => {
// Parses the crypto multi accounts from the scanned QR code data.
const account = KeystoneSDK.parseMultiAccounts(new UR(Buffer.from(cbor, "hex"), type))
console.log("multiAccounts: ", multiAccounts);
}

/**
* Callback function to handle errors during QR code scanning.
*
* @param {string} errorMessage - The error message describing what went wrong during scanning.
*/
const onError = (errorMessage) => {
console.log("error: ", errorMessage);
}

// Renders the AnimatedQRScanner component with the specified handlers for success and error events.
return <AnimatedQRScanner handleScan={onSucceed} handleError={onError} urTypes={[URType.CryptoMultiAccounts]} />
}

Here is an example of the resulting data:

{
"masterFingerprint": "f23f9fd2",
"keys": [
{
"chain": "BCH",
"path": "m/86'/0'/0'",
"publicKey": "02629d...",
"name": "BCH-0",
"chainCode": "4d58...",
"extendedPublicKey": "xpub6CAS..."
}
],
"device": "Keystone"
}

Here is the type defination of the CryptoMutliAccounts:

interface MultiAccounts {
masterFingerprint: string // A 4 bytes hex string indicates the current mnemonic, e.g. 'f23f9fd2'
keys: Account[] // An array of public keys
device?: string // The device name, e.g. 'Keystone'
deviceId?: string // The device id, e.g. '28475c8d80f6c06bafbe46a7d1750f3fcf2565f7'
deviceVersion?: String // The device firmware version, e.g. '1.0.2'
}

interface Account {
chain: string // The symbol of the coin this key belongs to, e.g. 'SOL'
path: string // The full derivation path of current key
publicKey: string // Public key in hex string
name?: string // The address name in hardware wallet
chainCode: string // The chain code if exist
extendedPublicKey?: string // The bip32 extended public key, e.g. xpub...
note?: string // The note for current account
}

Keystone will provide the master fingerprint and the public keys, allowing software wallets to select the necessary data to generate the desired addresses.

Genereate the sign request

For BitcoinCash, Keystone introdue the new UR type keystone-sign-request to encode the bch transaction data or message. Here is the sample data structure for keystone-sign-request for bch transaction:

requestId: String // UUID for current request
signData: Object(BchTransaction) // the bch transaction
xfp: String // master fingerprint provided by Keystone when getting accounts
origin: Optional(String) // source of the request, wallet name etc

// BchTransaction
Interface BchTransaction {
fee: Number // transaction fee
inputs: Array (
hash: String // UTXO hash in hex string
index: Number // UTXO index
value: Number // the amount of the UTXO
pubKey: String // the public key which the UTXO locking on, in hex string
ownerKeyPath: String // the HD path of the public key
)
outputs: Array (
address: String // the receive address
value: Number // send amount
isChange: Optional(Boolean) // is this receive address a change address
changeAddressPath: Optional(String) // the change address HD path if given isChange as true
)
dustThreshold: Optional(Number) // the dust threshold for transaction
}

Here is a sample code snippet demonstrating how to use the SDK to generate the sign request :

 import KeystoneSDK from "@keystonehq/keystone-sdk"
import {AnimatedQRCode} from "@keystonehq/animated-qr"

const bchTransaction = {
inputs: [{
hash: 'a59bcbaaae11ba5938434e2d4348243e5e392551156c4a3e88e7bdc0b2a8f663',
index: 1,
pubkey: '025ad49879cc8f1f91a210c6a2762fe4904ef0d4f17fd124b11b86135e4cb9143d',
value: 18519750n,
ownerKeyPath: "m/44'/145'/0'/0/0"
}],
outputs: [
{address: 'qzrxqxsx0lfzyk4ht60a5hwwtr2xjvtxmu0qhkusnx', value: 10000n, isChange: false, changeAddressPath: ''},
{address: 'qpgw8p85ysnjutpsk6u490ytydmgdlmc6vzxu680su', value: 18507500n, isChange: true, changeAddressPath: "M/44'/145'/0'/0/0"}
],
fee: "2250"
}

const bchSignRequest = {
requestId: "cc946be2-8e4c-42be-a321-56a53a8cf516",
signData: bchTransaction,
xfp: "F23F9FD2"
}

const bch = () => {
const keystoneSDK = new KeystoneSDK();
const ur = keystoneSDK.bch.generateSignRequest(bchSignRequest);

return <AnimatedQRCode type={ur.type} cbor={ur.cbor.toString("hex")}/>
}
options={{
size: number, // optional, QR code width and length in UI, default 180px
capacity: number, // optional, the capacity of a single QR code, default 400 bytes per image
interval: number // optional, the QR code change time interval in mill seconds for animated QR code, default 100ms
}}

Here is a javascript sample code snippet demonstrating how to use the Keystone SDK to encode a bch transaction into the UR type keystone-sign-result and embed it into QR codes.

AnimatedQRCode will decide whether the animated QR codes are needed, the option props of AnimatedQRCode component can be used to control the size, capacity and the update interval of QR code. Please avoid setting the capacity too high, as larger value can make it more difficult for Keystone to scan.

Extract signature

After Keystone scans the QR Codes, it will verify and display the transaction details for user confirmation. Once Keystone signs the data, it generates a signature and encodes it into a QR Code. An new UR type keystone-sign-result is introduced, After the signing is completed, a software wallet can scan the QR Code to retrieve the signature. The signature is a 64-byte hex string.

SignResult (
requestId: String // the requestId from sign request
rawData: String // the signed transaction raw format in hex string
)

Here are some code samples demonstrating how to use the SDK to achieve this.

import KeystoneSDK, {UR, URType} from "@keystonehq/keystone-sdk"
import {AnimatedQRScanner} from "@keystonehq/animated-qr"

const BitcoinCashScanner = () => {
const keystoneSDK = new KeystoneSDK();

const onSucceed = ({type, cbor}) => {
const signature = keystoneSDK.bch.parseSignResult(new UR(Buffer.from(cbor, "hex"), type))
console.log("signature: ", signature);
}
const onError = (errorMessage) => {
console.log("error: ", errorMessage);
}

return <AnimatedQRScanner handleScan={onSucceed} handleError={onError} urTypes={[URType.KeystoneSignResult]} />
}

AnimatedQRScanner helps scan the QR code on Keystone hardware wallet and returns signature which can be parsed by KeystoneSDK.

After getting the signature, software wallet can get the it and construct the transaction, then broadcast it.