Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions packages/hdwallet-ledger/src/tron.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,47 @@ export async function tronGetAddress(transport: LedgerTransport, msg: core.TronG
return res.payload.address;
}

/**
* Detects if a TRON transaction contains a memo in the raw_data.data field
* by checking for protobuf field 10 (wire type 2 for bytes) = 0x52
*/
function transactionHasMemo(rawDataHex: string): boolean {
try {
const buffer = Buffer.from(rawDataHex, "hex");

// Search for field 10 with wire type 2 (bytes)
// Field number 10, wire type 2 (length-delimited) = (10 << 3) | 2 = 0x52
for (let i = 0; i < buffer.length - 1; i++) {
if (buffer[i] === 0x52) {
return true;
}
}

return false;
} catch (err) {
return false;
}
}

Comment thread
gomesalexandre marked this conversation as resolved.
export async function tronSignTx(transport: LedgerTransport, msg: core.TronSignTx): Promise<core.TronSignedTx> {
const bip32path = core.addressNListToBIP32(msg.addressNList);

// Check if transaction has memo and validate Ledger app configuration
const hasMemo = transactionHasMemo(msg.rawDataHex);

if (hasMemo) {
const configRes = await transport.call("Tron", "getAppConfiguration");
handleError(configRes, transport, "Unable to get Tron app configuration from device.");

const { allowData } = configRes.payload;

if (!allowData) {
const error = new Error("Please enable Transactions Data in your Ledger TRON app settings and try again.");
(error as any).name = "LedgerTronAllowDataDisabled";
throw error;
}
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.

const res = await transport.call("Tron", "signTransaction", bip32path, msg.rawDataHex, []);
handleError(res, transport, "Unable to sign Tron transaction.");

Expand Down