Skip to content

Commit 291244b

Browse files
committed
ref!: make .toUint32LE(n) and .toUint64LE(n) public
1 parent f39eaf1 commit 291244b

File tree

1 file changed

+40
-12
lines changed

1 file changed

+40
-12
lines changed

dashtx.js

+40-12
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@
6363
* @prop {TxHexToBytes} hexToBytes
6464
* @prop {TxBytesToHex} bytesToHex
6565
* @prop {TxStringToHex} strToHex
66+
* @prop {TxToUint32LE} toUint32LE
67+
* @prop {TxToUint64LE} toUint64LE
6668
*/
6769

6870
/**
@@ -1160,7 +1162,7 @@ var DashTx = ("object" === typeof module && exports) || {};
11601162
void Tx.serializeInputs(inputs, { _tx: tx, _sep: _sep });
11611163
void Tx.serializeOutputs(outputs, { _tx: tx, _sep: _sep });
11621164

1163-
let locktimeHex = TxUtils._toUint32LE(locktime);
1165+
let locktimeHex = TxUtils.toUint32LE(locktime);
11641166
tx.push(locktimeHex);
11651167

11661168
if (extraPayload) {
@@ -1170,7 +1172,7 @@ var DashTx = ("object" === typeof module && exports) || {};
11701172
}
11711173

11721174
if (sigHashType) {
1173-
let sigHashTypeHex = TxUtils._toUint32LE(sigHashType);
1175+
let sigHashTypeHex = TxUtils.toUint32LE(sigHashType);
11741176
tx.push(sigHashTypeHex);
11751177
}
11761178

@@ -1221,7 +1223,7 @@ var DashTx = ("object" === typeof module && exports) || {};
12211223
`expected utxo property 'input[${i}]outputIndex' to be an integer representing this input's previous output index`,
12221224
);
12231225
}
1224-
let reverseVout = TxUtils._toUint32LE(voutIndex);
1226+
let reverseVout = TxUtils.toUint32LE(voutIndex);
12251227
tx.push(reverseVout);
12261228

12271229
//@ts-ignore - enum types not handled properly here
@@ -1309,7 +1311,7 @@ var DashTx = ("object" === typeof module && exports) || {};
13091311
if (!output.satoshis) {
13101312
throw new Error(`every output must have 'satoshis'`);
13111313
}
1312-
let satoshis = TxUtils._toUint64LE(output.satoshis);
1314+
let satoshis = TxUtils.toUint64LE(output.satoshis);
13131315
tx.push(satoshis);
13141316

13151317
if (!output.pubKeyHash) {
@@ -1348,7 +1350,7 @@ var DashTx = ("object" === typeof module && exports) || {};
13481350
*/
13491351
Tx._createMemoScript = function (memoHex, sats, i = 0) {
13501352
let outputHex = [];
1351-
let satoshis = TxUtils._toUint64LE(sats);
1353+
let satoshis = TxUtils.toUint64LE(sats);
13521354
outputHex.push(satoshis);
13531355

13541356
assertHex(memoHex, `output[${i}].memo`);
@@ -1816,17 +1818,17 @@ var DashTx = ("object" === typeof module && exports) || {};
18161818

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

18221824
//@ts-ignore
18231825
if (n <= MAX_U32) {
1824-
return "fe" + TxUtils._toUint32LE(n);
1826+
return "fe" + TxUtils.toUint32LE(n);
18251827
}
18261828

18271829
//@ts-ignore
18281830
if (n <= MAX_U53) {
1829-
return "ff" + TxUtils._toUint64LE(n);
1831+
return "ff" + TxUtils.toUint64LE(n);
18301832
}
18311833

18321834
if ("bigint" !== typeof n) {
@@ -1836,7 +1838,7 @@ var DashTx = ("object" === typeof module && exports) || {};
18361838
}
18371839

18381840
if (n <= MAX_U64) {
1839-
return "ff" + TxUtils._toUint64LE(n);
1841+
return "ff" + TxUtils.toUint64LE(n);
18401842
}
18411843

18421844
let err = new Error(E_TOO_BIG_INT);
@@ -1850,7 +1852,7 @@ var DashTx = ("object" === typeof module && exports) || {};
18501852
* @param {BigInt|Number} n - 16-bit positive int to encode
18511853
*/
18521854
TxUtils._toUint16LE = function (n) {
1853-
let hexLE = TxUtils._toUint32LE(n);
1855+
let hexLE = TxUtils.toUint32LE(n);
18541856
// ex: 03000800 => 0300
18551857
hexLE = hexLE.slice(0, 4);
18561858
return hexLE;
@@ -1861,7 +1863,7 @@ var DashTx = ("object" === typeof module && exports) || {};
18611863
* which is true in practice, and much simpler.
18621864
* @param {BigInt|Number} n - 32-bit positive int to encode
18631865
*/
1864-
TxUtils._toUint32LE = function (n) {
1866+
TxUtils.toUint32LE = function (n) {
18651867
// make sure n is uint32/int53, not int32
18661868
//n = n >>> 0;
18671869

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

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

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

19071923
/** @type TxToVarIntSize */
19081924
TxUtils.toVarIntSize = function (n) {
@@ -2456,3 +2472,15 @@ if ("object" === typeof module) {
24562472
* @param {String} utf8
24572473
* @returns {String} - encoded bytes as hex
24582474
*/
2475+
2476+
/**
2477+
* @callback TxToUint32LE
2478+
* @param {Uint32} n
2479+
* @returns {Hex}
2480+
*/
2481+
2482+
/**
2483+
* @callback TxToUint64LE
2484+
* @param {Uint32} n
2485+
* @returns {Hex}
2486+
*/

0 commit comments

Comments
 (0)