Skip to content

Commit dc88afa

Browse files
committed
ref!: make .toUint32LE(n) and .toUint64LE(n) public
r: u64
1 parent 9fca0d2 commit dc88afa

File tree

1 file changed

+40
-12
lines changed

1 file changed

+40
-12
lines changed

dashtx.js

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@
6262
* @prop {TxHexToBytes} hexToBytes
6363
* @prop {TxBytesToHex} bytesToHex
6464
* @prop {TxStringToHex} strToHex
65+
* @prop {TxToUint32LE} toUint32LE
66+
* @prop {TxToUint64LE} toUint64LE
6567
*/
6668

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

1156-
let locktimeHex = TxUtils._toUint32LE(locktime);
1158+
let locktimeHex = TxUtils.toUint32LE(locktime);
11571159
tx.push(locktimeHex);
11581160

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

11671169
if (sigHashType) {
1168-
let sigHashTypeHex = TxUtils._toUint32LE(sigHashType);
1170+
let sigHashTypeHex = TxUtils.toUint32LE(sigHashType);
11691171
txHex = `${txHex}${sigHashTypeHex}`;
11701172
}
11711173

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

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

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

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

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

18231825
//@ts-ignore
18241826
if (n <= MAX_U32) {
1825-
return "fe" + TxUtils._toUint32LE(n);
1827+
return "fe" + TxUtils.toUint32LE(n);
18261828
}
18271829

18281830
//@ts-ignore
18291831
if (n <= MAX_U53) {
1830-
return "ff" + TxUtils._toUint64LE(n);
1832+
return "ff" + TxUtils.toUint64LE(n);
18311833
}
18321834

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

18391841
if (n <= MAX_U64) {
1840-
return "ff" + TxUtils._toUint64LE(n);
1842+
return "ff" + TxUtils.toUint64LE(n);
18411843
}
18421844

18431845
let err = new Error(E_TOO_BIG_INT);
@@ -1851,7 +1853,7 @@ var DashTx = ("object" === typeof module && exports) || {};
18511853
* @param {BigInt|Number} n - 16-bit positive int to encode
18521854
*/
18531855
TxUtils._toUint16LE = function (n) {
1854-
let hexLE = TxUtils._toUint32LE(n);
1856+
let hexLE = TxUtils.toUint32LE(n);
18551857
// ex: 03000800 => 0300
18561858
hexLE = hexLE.slice(0, 4);
18571859
return hexLE;
@@ -1862,7 +1864,7 @@ var DashTx = ("object" === typeof module && exports) || {};
18621864
* which is true in practice, and much simpler.
18631865
* @param {BigInt|Number} n - 32-bit positive int to encode
18641866
*/
1865-
TxUtils._toUint32LE = function (n) {
1867+
TxUtils.toUint32LE = function (n) {
18661868
// make sure n is uint32/int53, not int32
18671869
//n = n >>> 0;
18681870

@@ -1872,14 +1874,21 @@ var DashTx = ("object" === typeof module && exports) || {};
18721874
let hexLE = Tx.utils.reverseHex(hex);
18731875
return hexLE;
18741876
};
1877+
//@ts-ignore
1878+
TxUtils._toUint32LE = function (n) {
1879+
console.warn(
1880+
"warn: use public TxUtils.toUint32LE() instead of internal TxUtils._toUint32LE()",
1881+
);
1882+
return TxUtils.toUint32LE(n);
1883+
};
18751884

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

19051914
return hex;
19061915
};
1916+
//@ts-ignore
1917+
TxUtils._toUint64LE = function (n) {
1918+
console.warn(
1919+
"warn: use public TxUtils.toUint64LE() instead of internal TxUtils._toUint64LE()",
1920+
);
1921+
return TxUtils.toUint64LE(n);
1922+
};
19071923

19081924
/** @type TxToVarIntSize */
19091925
TxUtils.toVarIntSize = function (n) {
@@ -2446,3 +2462,15 @@ if ("object" === typeof module) {
24462462
* @param {String} utf8
24472463
* @returns {String} - encoded bytes as hex
24482464
*/
2465+
2466+
/**
2467+
* @callback TxToUint32LE
2468+
* @param {Uint32} n
2469+
* @returns {Hex}
2470+
*/
2471+
2472+
/**
2473+
* @callback TxToUint64LE
2474+
* @param {Uint32} n
2475+
* @returns {Hex}
2476+
*/

0 commit comments

Comments
 (0)