Skip to content

Commit

Permalink
ledger: Fix writeUint64LE (#3958)
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeGruffins authored Aug 23, 2024
1 parent e7cd0c6 commit 5227a52
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
14 changes: 10 additions & 4 deletions app/helpers/ledger.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import * as bs58 from "bs58";
import toBuffer from "typedarray-to-buffer";
import { getTxFromInputs } from "../actions/TransactionActions";

const ONE_OVER_MAX_UINT32 = 0x100000000;

export function addressPath(branch, index) {
const prefix = "44'/42'/0'/";
const i = (index || 0).toString();
Expand Down Expand Up @@ -46,12 +48,16 @@ function writeUint32LE(n) {
return buff;
}

function writeUint64LE(n) {
// Exported for testing.
//
// TODO: Electron cannot handle a uint64 with full precision so one cannot be
// passed to this function. Maybe we should find a better way to pass this value.
export function writeUint64LE(n) {
const buff = new Buffer(8);
const lower = 0xffffffff & n;
// bitshift right (>>>) does not seem to throw away the lower half, so
// dividing and throwing away the remainder.
const upper = Math.floor(n / 0xffffffff);
const upper = Math.floor(n / ONE_OVER_MAX_UINT32);
const lower = n % ONE_OVER_MAX_UINT32;
buff.writeUInt32LE(lower, 0);
buff.writeUInt32LE(upper, 4);
return buff;
Expand Down Expand Up @@ -133,7 +139,7 @@ export async function signArg(txHex, chainParams, walletService, dispatch) {
}
}
if (!verboseInp) {
throw "cound not find input";
throw "could not find input";
}
const prevOut = inputToTx(verboseInp);
const idx = inp.outputIndex;
Expand Down
14 changes: 13 additions & 1 deletion test/unit/helpers/ledger.spec.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5227a52

Please sign in to comment.