Skip to content

Commit

Permalink
ref!: make .toUint32LE(n) and .toUint64LE(n) public
Browse files Browse the repository at this point in the history
r: u64
  • Loading branch information
coolaj86 committed Aug 23, 2024
1 parent 9fca0d2 commit dc88afa
Showing 1 changed file with 40 additions and 12 deletions.
52 changes: 40 additions & 12 deletions dashtx.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@
* @prop {TxHexToBytes} hexToBytes
* @prop {TxBytesToHex} bytesToHex
* @prop {TxStringToHex} strToHex
* @prop {TxToUint32LE} toUint32LE
* @prop {TxToUint64LE} toUint64LE
*/

/**
Expand Down Expand Up @@ -1153,7 +1155,7 @@ var DashTx = ("object" === typeof module && exports) || {};
void Tx.serializeInputs(inputs, { _tx: tx, _sep: _sep });
void Tx.serializeOutputs(outputs, { _tx: tx, _sep: _sep });

let locktimeHex = TxUtils._toUint32LE(locktime);
let locktimeHex = TxUtils.toUint32LE(locktime);
tx.push(locktimeHex);

if (extraPayload) {
Expand All @@ -1165,7 +1167,7 @@ var DashTx = ("object" === typeof module && exports) || {};
let txHex = tx.join(_sep);

if (sigHashType) {
let sigHashTypeHex = TxUtils._toUint32LE(sigHashType);
let sigHashTypeHex = TxUtils.toUint32LE(sigHashType);
txHex = `${txHex}${sigHashTypeHex}`;
}

Expand Down Expand Up @@ -1215,7 +1217,7 @@ var DashTx = ("object" === typeof module && exports) || {};
`expected utxo property 'input[${i}]outputIndex' to be an integer representing this input's previous output index`,
);
}
let reverseVout = TxUtils._toUint32LE(voutIndex);
let reverseVout = TxUtils.toUint32LE(voutIndex);
tx.push(reverseVout);

//@ts-ignore - enum types not handled properly here
Expand Down Expand Up @@ -1303,7 +1305,7 @@ var DashTx = ("object" === typeof module && exports) || {};
if (!output.satoshis) {
throw new Error(`every output must have 'satoshis'`);
}
let satoshis = TxUtils._toUint64LE(output.satoshis);
let satoshis = TxUtils.toUint64LE(output.satoshis);
tx.push(satoshis);

if (!output.pubKeyHash) {
Expand Down Expand Up @@ -1342,7 +1344,7 @@ var DashTx = ("object" === typeof module && exports) || {};
*/
Tx._createMemoScript = function (memoHex, sats, i = 0) {
let outputHex = [];
let satoshis = TxUtils._toUint64LE(sats);
let satoshis = TxUtils.toUint64LE(sats);
outputHex.push(satoshis);

assertHex(memoHex, `output[${i}].memo`);
Expand Down Expand Up @@ -1817,17 +1819,17 @@ var DashTx = ("object" === typeof module && exports) || {};

//@ts-ignore
if (n <= MAX_U16) {
return "fd" + TxUtils._toUint32LE(n).slice(0, 4);
return "fd" + TxUtils.toUint32LE(n).slice(0, 4);
}

//@ts-ignore
if (n <= MAX_U32) {
return "fe" + TxUtils._toUint32LE(n);
return "fe" + TxUtils.toUint32LE(n);
}

//@ts-ignore
if (n <= MAX_U53) {
return "ff" + TxUtils._toUint64LE(n);
return "ff" + TxUtils.toUint64LE(n);
}

if ("bigint" !== typeof n) {
Expand All @@ -1837,7 +1839,7 @@ var DashTx = ("object" === typeof module && exports) || {};
}

if (n <= MAX_U64) {
return "ff" + TxUtils._toUint64LE(n);
return "ff" + TxUtils.toUint64LE(n);
}

let err = new Error(E_TOO_BIG_INT);
Expand All @@ -1851,7 +1853,7 @@ var DashTx = ("object" === typeof module && exports) || {};
* @param {BigInt|Number} n - 16-bit positive int to encode
*/
TxUtils._toUint16LE = function (n) {
let hexLE = TxUtils._toUint32LE(n);
let hexLE = TxUtils.toUint32LE(n);
// ex: 03000800 => 0300
hexLE = hexLE.slice(0, 4);
return hexLE;
Expand All @@ -1862,7 +1864,7 @@ var DashTx = ("object" === typeof module && exports) || {};
* which is true in practice, and much simpler.
* @param {BigInt|Number} n - 32-bit positive int to encode
*/
TxUtils._toUint32LE = function (n) {
TxUtils.toUint32LE = function (n) {
// make sure n is uint32/int53, not int32
//n = n >>> 0;

Expand All @@ -1872,14 +1874,21 @@ var DashTx = ("object" === typeof module && exports) || {};
let hexLE = Tx.utils.reverseHex(hex);
return hexLE;
};
//@ts-ignore
TxUtils._toUint32LE = function (n) {
console.warn(
"warn: use public TxUtils.toUint32LE() instead of internal TxUtils._toUint32LE()",
);
return TxUtils.toUint32LE(n);
};

/**
* This can handle Big-Endian CPUs, which don't exist,
* and looks too complicated.
* @param {BigInt|Number} n - 64-bit BigInt or <= 53-bit Number to encode
* @returns {String} - 8 Little-Endian bytes
*/
TxUtils._toUint64LE = function (n) {
TxUtils.toUint64LE = function (n) {
let bn;
if ("bigint" === typeof n) {
bn = n;
Expand All @@ -1904,6 +1913,13 @@ var DashTx = ("object" === typeof module && exports) || {};

return hex;
};
//@ts-ignore
TxUtils._toUint64LE = function (n) {
console.warn(
"warn: use public TxUtils.toUint64LE() instead of internal TxUtils._toUint64LE()",
);
return TxUtils.toUint64LE(n);
};

/** @type TxToVarIntSize */
TxUtils.toVarIntSize = function (n) {
Expand Down Expand Up @@ -2446,3 +2462,15 @@ if ("object" === typeof module) {
* @param {String} utf8
* @returns {String} - encoded bytes as hex
*/

/**
* @callback TxToUint32LE
* @param {Uint32} n
* @returns {Hex}
*/

/**
* @callback TxToUint64LE
* @param {Uint32} n
* @returns {Hex}
*/

0 comments on commit dc88afa

Please sign in to comment.