Tron
Keystone is supporting TRON via OKX Wallet.
Connect with Keystone
For TRON, 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": "TRX",
"path": "m/44'/195'/0'",
"publicKey": "02629d...",
"name": "Keystone",
"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 TRON, Keystone introdue the new UR type keystone-sign-request
to encode the tron transaction data or message. Here is the sample data structure for keystone-sign-request
for tron transaction:
requestId: String // UUID for current request
signData: String // the serialized unsigned transaction data, in hex string
path: String // the HD path to tell which private key should be used to sign the data
xfp: String // master fingerprint provided by Keystone when getting accounts
origin: Optional(String) // source of the request, wallet name etc
tokenInfo: Optional ( // The token information is required for TRC10 and TRC20 token transfer
name: String // the token name, will be shown on Keystone if token symbol is not provided
symbol: String // the token symbol, will be shown on Keystone
decimals: Number // the decimal of token
)
Here is a sample code snippet demonstrating how to use the SDK to generate the sign request :
- Javascript
- Swift
- Kotlin
import KeystoneSDK from "@keystonehq/keystone-sdk"
import {AnimatedQRCode} from "@keystonehq/animated-qr"
let tronSignRequest = {
requestId: '9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d',
signData: '0a0207902208e1b9de559665c6714080c49789bb2c5aae01081f12a9010a31747970652e676f6f676c65617069732e636f6d2f70726f746f636f6c2e54726967676572536d617274436f6e747261637412740a15418dfec1cde1fe6a9ec38a16c7d67073e3020851c0121541a614f803b6fd780986a42c78ec9c7f77e6ded13c2244a9059cbb0000000000000000000000009c0279f1bda9fc40a85f1b53c306602864533e7300000000000000000000000000000000000000000000000000000000000f424070c0b6e087bb2c90018094ebdc03',
path: "m/44'/195'/0'/0/0",
xfp: 'F23F9FD2',
tokenInfo: {
name: 'TRON_USDT',
symbol: 'USDT',
decimals: 6
}
}
const Tron = () => {
const keystoneSDK = new KeystoneSDK();
const ur = keystoneSDK.tron.generateSignRequest(tronSignRequest);
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 trc20 transfer 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.
import KeystoneSDK
let tronSignRequest = TronSignRequest(
requestId: "9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d",
signData: "0a0207902208e1b9de559665c6714080c49789bb2c5aae01081f12a9010a31747970652e676f6f676c65617069732e636f6d2f70726f746f636f6c2e54726967676572536d617274436f6e747261637412740a15418dfec1cde1fe6a9ec38a16c7d67073e3020851c0121541a614f803b6fd780986a42c78ec9c7f77e6ded13c2244a9059cbb0000000000000000000000009c0279f1bda9fc40a85f1b53c306602864533e7300000000000000000000000000000000000000000000000000000000000f424070c0b6e087bb2c90018094ebdc03",
path: "m/44'/195'/0'/0/0",
xfp: "F23F9FD2",
tokenInfo: TokenInfo(name: "TRON_USDT", symbol: "USDT", decimals: 6)
)
let keystoneSDK = KeystoneSDK()
let qrCode = try keystoneSDK.tron.generateSignRequest(tronSignRequest: tronSignRequest)
// Check if a single QR code can contain all the transaction information
let isSingleQRCode = qrCode.isSinglePart()
if isSingleQRCode {
// Return the content that should be shown in QR code
qrCode.nextPart()
} else {
while true {
// generate the data for anmiated QR Code
let qr = qrCode.nextPart()
// render the each QR Code
render(qr)
}
}
Here is a Swift sample code snippet demonstrating how to use the Keystone SDK to encode a trc20 transaction into the UR type keystone-sign-request and embed it into QR codes.
The value of KeystoneSDK.maxFragmentLen
can be modified to adjust the capacity of a single QR code. The default length is 400. Please avoid setting this value too high, as larger fragment lengths can make it more difficult for Keystone to scan.
import com.keystone.sdk.KeystoneSDK
val tronSignRequest = TronSignRequest(
requestId = "9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d",
signData = "0a0207902208e1b9de559665c6714080c49789bb2c5aae01081f12a9010a31747970652e676f6f676c65617069732e636f6d2f70726f746f636f6c2e54726967676572536d617274436f6e747261637412740a15418dfec1cde1fe6a9ec38a16c7d67073e3020851c0121541a614f803b6fd780986a42c78ec9c7f77e6ded13c2244a9059cbb0000000000000000000000009c0279f1bda9fc40a85f1b53c306602864533e7300000000000000000000000000000000000000000000000000000000000f424070c0b6e087bb2c90018094ebdc03",
path = "m/44'/195'/0'/0/0",
xfp = "F23F9FD2",
tokenInfo = TokenInfo("USDT", "TRON_USDT", 6)
)
val keystoneSDK = KeystoneSDK()
val qrCode = keystoneSDK.tron.generateSignRequest(tronSignRequest)
// Check if a single QR code can contain all the transaction information
val isSingleQRCode = qrCode.isSinglePart()
if (isSingleQRCode) {
// Return the content that should be shown in QR code
qrCode.nextPart()
} else {
while(true) {
// generate the data for anmiated QR Code
val qr = qrCode.nextPart()
// re-render each data for QR Code
render(qr)
}
}
Here is a Kotlin sample code snippet demonstrating how to use the Keystone SDK to encode a trc20 transaction into the UR type keystone-sign-request and embed it into QR codes.
The value of KeystoneSDK.maxFragmentLen
can be modified to adjust the capacity of a single QR code. The default length is 400. Please avoid setting this value too high, as larger fragment lengths 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.
Signature (
requestId: String // the requestId from sign request
signature: String // the serialized signature in hex string
)
Here are some code samples demonstrating how to use the SDK to achieve this.
- Javascript
- Swift
- Kotlin
import KeystoneSDK, {UR, URType} from "@keystonehq/keystone-sdk"
import {AnimatedQRScanner} from "@keystonehq/animated-qr"
const TronScanner = () => {
const keystoneSDK = new KeystoneSDK();
const onSucceed = ({type, cbor}) => {
const signature = keystoneSDK.tron.parseSignature(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.TronSignature]} />
}
AnimatedQRScanner
helps scan the QR code on Keystone hardware wallet and returns signature which can be parsed by KeystoneSDK
.
import KeystoneSDK
let keystoneSDK = KeystoneSDK()
let decodedResult = try keystoneSDK.decodeQR(qrCode: qrCodeString)
if decodedResult.progress == 100 {
let signature = try keystoneSDK.tron.parseSignature(ur: decodedResult.ur!)
}
import com.keystone.sdk.KeystoneSDK
val keystoneSDK = KeystoneSDK()
val decodedResult = keystoneSDK.decodeQR(qrCodeString)
if (decodedResult.progress == 100) {
val signature = keystoneSDK.tron.parseSignature(decodedResult.ur!!)
}
After getting the signature, software wallet can get the it and construct the transaction, then broadcast it.