Skip to content

Commit 85d7f8c

Browse files
committed
doc+ref: use appropriately-sized type aliases for all numbers, fix typos
1 parent 5ef4102 commit 85d7f8c

File tree

1 file changed

+57
-63
lines changed

1 file changed

+57
-63
lines changed

dashtx.js

+57-63
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
/**
22
* @typedef Tx
3-
* @prop {Number} SATOSHIS
4-
* @prop {Number} _HEADER_ONLY_SIZE
5-
* @prop {Number} HEADER_SIZE
6-
* @prop {Number} LEGACY_DUST
7-
* @prop {Number} MIN_INPUT_SIZE - 147 each
8-
* @prop {Number} MAX_INPUT_PAD - 2 (possible ASN.1 BigInt padding)
9-
* @prop {Number} MAX_INPUT_SIZE - 149 each (with padding)
10-
* @prop {Number} OUTPUT_SIZE - 34 each
3+
* @prop {Uint53} SATOSHIS
4+
* @prop {Uint32} _HEADER_ONLY_SIZE
5+
* @prop {Uint32} HEADER_SIZE
6+
* @prop {Uint32} LEGACY_DUST
7+
* @prop {Uint32} MIN_INPUT_SIZE - 147 each
8+
* @prop {Uint32} MAX_INPUT_PAD - 2 (possible ASN.1 BigInt padding)
9+
* @prop {Uint32} MAX_INPUT_SIZE - 149 each (with padding)
10+
* @prop {Uint32} OUTPUT_SIZE - 34 each
1111
* @prop {TxAppraise} appraise
1212
* @prop {TxToDash} toDash
1313
* @prop {TxToSats} toSats
@@ -71,7 +71,7 @@ var DashTx = ("object" === typeof module && exports) || {};
7171
const MAX_U8 = Math.pow(2, 8) - 1;
7272
const MAX_U16 = Math.pow(2, 16) - 1;
7373
const MAX_U32 = Math.pow(2, 32) - 1;
74-
const MAX_U52 = Number.MAX_SAFE_INTEGER;
74+
const MAX_U53 = Number.MAX_SAFE_INTEGER; // Math.pow(2, 53) - 1
7575
const MAX_U64 = 2n ** 64n - 1n;
7676

7777
const CH_0 = 48;
@@ -95,7 +95,7 @@ var DashTx = ("object" === typeof module && exports) || {};
9595
const PKH_SCRIPT_SIZE = (25).toString(16); // 0x19
9696

9797
const E_LITTLE_INT =
98-
"JavaScript 'Number's only go up to uint51, you must use 'BigInt' (ex: `let amount = 18014398509481984n`) for larger values";
98+
"JavaScript 'Number's only go up to uint53, you must use 'BigInt' (ex: `let amount = 18014398509481984n`) for larger values";
9999
const E_TOO_BIG_INT =
100100
"JavaScript 'BigInt's are arbitrarily large, but you may only use up to UINT64 for transactions";
101101

@@ -378,7 +378,7 @@ var DashTx = ("object" === typeof module && exports) || {};
378378
*
379379
* @param {TxOutputSortable} a
380380
* @param {TxOutputSortable} b
381-
* @returns {Number}
381+
* @returns {Uint32}
382382
*/
383383
Tx.sortOutputs = function (a, b) {
384384
// the complexity is inherent, and doesn't seem to be reasonable to break out
@@ -501,7 +501,7 @@ var DashTx = ("object" === typeof module && exports) || {};
501501
* @param {TxInfo} txInfo
502502
* TODO _param {Array<TxInputRaw>} txInfo.inputs - needs type narrowing check
503503
* TODO _param {Array<TxOutput>} txInfo.outputs
504-
* TODO _param {Number} [txInfo.version]
504+
* TODO _param {Uint32} [txInfo.version]
505505
* TODO _param {Boolean} [txInfo._debug] - bespoke debug output
506506
* @param {TxDeps} myUtils
507507
* @returns {Promise<TxInfoSigned>}
@@ -679,9 +679,9 @@ var DashTx = ("object" === typeof module && exports) || {};
679679
/**
680680
* @param {Object} opts
681681
* @param {Array<TxInputRaw|TxInputHashable|TxInputSigned>} opts.inputs
682-
* @param {Number} [opts.locktime]
682+
* @param {Uint32} [opts.locktime]
683683
* @param {Array<TxOutput>} opts.outputs
684-
* @param {Number} [opts.version]
684+
* @param {Uint32} [opts.version]
685685
* @param {Boolean} [opts._debug] - bespoke debug output
686686
*/
687687
Tx._create = function ({
@@ -936,7 +936,7 @@ var DashTx = ("object" === typeof module && exports) || {};
936936
let reverseU8 = new Uint8Array(hashU8.length);
937937
let reverseIndex = reverseU8.length - 1;
938938
hashU8.forEach(
939-
/** @param {Number} b */
939+
/** @param {Uint8} b */
940940
function (b) {
941941
reverseU8[reverseIndex] = b;
942942
reverseIndex -= 1;
@@ -1093,10 +1093,6 @@ var DashTx = ("object" === typeof module && exports) || {};
10931093
return pubKeyBuf;
10941094
};
10951095

1096-
/**
1097-
* Caution: JS can't handle 64-bit ints
1098-
* @param {BigInt|Number} n - 64-bit BigInt or < 52-bit Number
1099-
*/
11001096
TxUtils.toVarInt = function (n) {
11011097
//@ts-ignore - see https://github.com/microsoft/TypeScript/issues/57953
11021098
if (n < 253) {
@@ -1117,7 +1113,7 @@ var DashTx = ("object" === typeof module && exports) || {};
11171113
}
11181114

11191115
//@ts-ignore
1120-
if (n <= MAX_U52) {
1116+
if (n <= MAX_U53) {
11211117
return "ff" + toUint64LE(n);
11221118
}
11231119

@@ -1144,7 +1140,7 @@ var DashTx = ("object" === typeof module && exports) || {};
11441140
* @param {BigInt|Number} n - 32-bit positive int to encode
11451141
*/
11461142
function toUint32LE(n) {
1147-
// make sure n is uint32/int52, not int32
1143+
// make sure n is uint32/int53, not int32
11481144
//n = n >>> 0;
11491145

11501146
// 0x00000003
@@ -1157,7 +1153,7 @@ var DashTx = ("object" === typeof module && exports) || {};
11571153
/**
11581154
* This can handle Big-Endian CPUs, which don't exist,
11591155
* and looks too complicated.
1160-
* @param {BigInt|Number} n - 64-bit BigInt or <= 51-bit Number to encode
1156+
* @param {BigInt|Number} n - 64-bit BigInt or <= 53-bit Number to encode
11611157
* @returns {String} - 8 Little-Endian bytes
11621158
*/
11631159
function toUint64LE(n) {
@@ -1186,10 +1182,6 @@ var DashTx = ("object" === typeof module && exports) || {};
11861182
return hex;
11871183
}
11881184

1189-
/**
1190-
* @param {BigInt|Number} n
1191-
* @returns {Number}
1192-
*/
11931185
TxUtils.toVarIntSize = function (n) {
11941186
//@ts-ignore - see https://github.com/microsoft/TypeScript/issues/57953
11951187
if (n < 253) {
@@ -1257,6 +1249,7 @@ if ("object" === typeof module) {
12571249
/** @typedef {Number} Float64 */
12581250
/** @typedef {Number} Uint53 */
12591251
/** @typedef {Number} Uint32 */
1252+
/** @typedef {Number} Uint8 */
12601253
/** @typedef {Uint8Array} TxPrivateKey */
12611254
/** @typedef {Uint8Array} TxPublicKey */
12621255
/** @typedef {Uint8Array} TxSignature */
@@ -1273,27 +1266,27 @@ if ("object" === typeof module) {
12731266

12741267
/**
12751268
* @typedef TxFees
1276-
* @prop {Number} max
1277-
* @prop {Number} mid
1278-
* @prop {Number} min
1269+
* @prop {Uint53} max
1270+
* @prop {Uint53} mid
1271+
* @prop {Uint53} min
12791272
*/
12801273

12811274
/**
12821275
* @typedef TxInfo
12831276
* @prop {Array<TxInputHashable>} inputs
1284-
* @prop {Number} [locktime] - 0 by default
1277+
* @prop {Uint32} [locktime] - 0 by default
12851278
* @prop {Array<TxOutput>} outputs
1286-
* @prop {Number} [version]
1279+
* @prop {Uint32} [version]
12871280
* @prop {String} [transaction] - signed transaction hex
12881281
* @prop {Boolean} [_debug] - bespoke debug output
12891282
*/
12901283

12911284
/**
12921285
* @typedef TxInfoSigned
12931286
* @prop {Array<TxInputSigned>} inputs
1294-
* @prop {Number} locktime - 0 by default
1287+
* @prop {Uint32} locktime - 0 by default
12951288
* @prop {Array<TxOutput>} outputs
1296-
* @prop {Number} version
1289+
* @prop {Uint32} version
12971290
* @prop {String} transaction - signed transaction hex
12981291
* @prop {Boolean} [_debug] - bespoke debug output
12991292
*/
@@ -1302,52 +1295,52 @@ if ("object" === typeof module) {
13021295
* @typedef TxInput
13031296
* @prop {String} [address] - BaseCheck58-encoded pubKeyHash
13041297
* @prop {String} txId - hex (not pre-reversed)
1305-
* @prop {Number} outputIndex - index in previous tx's output (vout index)
1298+
* @prop {Uint32} outputIndex - index in previous tx's output (vout index)
13061299
* @prop {String} signature - hex-encoded ASN.1 (DER) signature (starts with 0x30440220 or 0x30440221)
13071300
* @prop {String} [script] - the previous lock script (default: derived from public key as p2pkh)
13081301
* @prop {String} publicKey - hex-encoded public key (typically starts with a 0x02 or 0x03 prefix)
13091302
* @prop {String} [pubKeyHash] - the 20-byte pubKeyHash (address without magic byte or checksum)
1310-
* @prop {Number} sigHashType - typically 0x01 (SIGHASH_ALL)
1303+
* @prop {Uint32} sigHashType - typically 0x01 (SIGHASH_ALL)
13111304
*/
13121305

13131306
/**
13141307
* @typedef TxInputHashable
13151308
* @prop {String} [address] - BaseCheck58-encoded pubKeyHash
13161309
* @prop {String} txId - hex (not pre-reversed)
1317-
* @prop {Number} outputIndex - index in previous tx's output (vout index)
1318-
* @prop {Number} [satoshis] - (included for convenience as type hack)
1310+
* @prop {Uint32} outputIndex - index in previous tx's output (vout index)
1311+
* @prop {Uint53} [satoshis] - (included for convenience as type hack)
13191312
* @prop {String} [signature] - (included as type hack)
13201313
* @prop {String} [script] - the previous lock script (default: derived from public key as p2pkh)
13211314
* @prop {String} [publicKey] - hex-encoded public key (typically starts with a 0x02 or 0x03 prefix)
13221315
* @prop {String} [pubKeyHash] - the 20-byte pubKeyHash (address without magic byte or checksum)
1323-
* @prop {Number} [sigHashType] - typically 0x01 (SIGHASH_ALL)
1316+
* @prop {Uint32} [sigHashType] - typically 0x01 (SIGHASH_ALL)
13241317
*/
13251318

13261319
/**
13271320
* @typedef TxInputRaw
13281321
* @prop {String} [address] - BaseCheck58-encoded pubKeyHash
1329-
* @prop {Number} [satoshis] - for convenience
1322+
* @prop {Uint53} [satoshis] - for convenience
13301323
* @prop {String} txId - hex (not pre-reversed)
1331-
* @prop {Number} outputIndex - index in previous tx's output (vout index)
1324+
* @prop {Uint32} outputIndex - index in previous tx's output (vout index)
13321325
*/
13331326

13341327
/**
13351328
* @typedef TxInputUnspent
13361329
* @prop {String} [address] - BaseCheck58-encoded pubKeyHash
1337-
* @prop {Number} satoshis
1330+
* @prop {Uint53} satoshis
13381331
* @prop {String} txId - hex (not pre-reversed)
1339-
* @prop {Number} outputIndex - index in previous tx's output (vout index)
1332+
* @prop {Uint32} outputIndex - index in previous tx's output (vout index)
13401333
*/
13411334

13421335
/**
13431336
* @typedef TxInputSortable
13441337
* @prop {String} txId
1345-
* @prop {Number} outputIndex
1338+
* @prop {Uint32} outputIndex
13461339
*/
13471340

13481341
/**
13491342
* @typedef TxHasSats
1350-
* @prop {Number} satoshis
1343+
* @prop {Uint53} satoshis
13511344
*/
13521345

13531346
/**
@@ -1359,12 +1352,12 @@ if ("object" === typeof module) {
13591352
* @prop {String} [memo] - hex bytes of a memo (incompatible with pubKeyHash / address)
13601353
* @prop {String} [address] - payAddr as Base58Check (human-friendly)
13611354
* @prop {String} [pubKeyHash] - payAddr's raw hex value (decoded, not Base58Check)
1362-
* @prop {Number} satoshis - the number of smallest units of the currency
1355+
* @prop {Uint53} satoshis - the number of smallest units of the currency
13631356
*/
13641357

13651358
/**
13661359
* @typedef TxOutputSortable
1367-
* @prop {Number} satoshis
1360+
* @prop {Uint53} satoshis
13681361
* @prop {String} [script] - hex bytes in wire order
13691362
* @prop {String} [memo] - 0x6a, hex bytes
13701363
* @prop {String} [pubKeyHash] - 0x76, 0xa9, hex bytes
@@ -1398,7 +1391,7 @@ if ("object" === typeof module) {
13981391
/**
13991392
* @callback TxCreateHashable
14001393
* @param {TxInfo} txInfo
1401-
* @param {Number} inputIndex - create hashable tx for this input
1394+
* @param {Uint32} inputIndex - create hashable tx for this input
14021395
* @returns {String} - hashable tx hex
14031396
*/
14041397

@@ -1407,7 +1400,7 @@ if ("object" === typeof module) {
14071400
* @param {Object} opts
14081401
* @param {Array<TxInputRaw>} opts.inputs
14091402
* @param {Array<TxOutput>} opts.outputs
1410-
* @param {Number} [opts.version]
1403+
* @param {Uint32} [opts.version]
14111404
* @param {Boolean} [opts._debug] - bespoke debug output
14121405
*/
14131406

@@ -1416,7 +1409,7 @@ if ("object" === typeof module) {
14161409
* @param {Object} opts
14171410
* @param {Array<TxInputSigned>} opts.inputs
14181411
* @param {Array<TxOutput>} opts.outputs
1419-
* @param {Number} [opts.version]
1412+
* @param {Uint32} [opts.version]
14201413
* @param {Boolean} [opts._debug] - bespoke debug output
14211414
* xparam {String} [opts.sigHashType] - hex, typically 01 (ALL)
14221415
*/
@@ -1430,15 +1423,15 @@ if ("object" === typeof module) {
14301423
/**
14311424
* @callback TxGetPrivateKey
14321425
* @param {TxInputHashable} txInput
1433-
* @param {Number} i
1426+
* @param {Uint53} i
14341427
* @param {Array<TxInputRaw|TxInputHashable>} txInputs
14351428
* @returns {Uint8Array} - private key Uint8Array
14361429
*/
14371430

14381431
/**
14391432
* @callback TxGetPublicKey
14401433
* @param {TxInputHashable} txInput
1441-
* @param {Number} i
1434+
* @param {Uint53} i
14421435
* @param {Array<TxInputRaw|TxInputHashable>} txInputs
14431436
* @returns {Uint8Array} - public key Uint8Array
14441437
*/
@@ -1452,7 +1445,7 @@ if ("object" === typeof module) {
14521445
/**
14531446
* @callback TxHashPartial
14541447
* @param {String} txHex - signable tx hex (like raw tx, but with (lock)script)
1455-
* @param {Number} sigHashType - typically 0x01 (SIGHASH_ALL) for signable
1448+
* @param {Uint32} sigHashType - typically 0x01 (SIGHASH_ALL) for signable
14561449
* @returns {Promise<Uint8Array>}
14571450
*/
14581451

@@ -1487,39 +1480,39 @@ if ("object" === typeof module) {
14871480
* @callback TxSortBySats
14881481
* @param {TxHasSats} a
14891482
* @param {TxHasSats} b
1490-
* @returns {Number}
1483+
* @returns {Uint8}
14911484
*/
14921485

14931486
/**
14941487
* @callback TxSortInputs
14951488
* @param {TxInputSortable} a
14961489
* @param {TxInputSortable} b
1497-
* @returns {Number}
1490+
* @returns {Uint8}
14981491
*/
14991492

15001493
/**
15011494
* @callback TxSortOutputs
15021495
* @param {TxOutputSortable} a
15031496
* @param {TxOutputSortable} b
1504-
* @returns {Number}
1497+
* @returns {Uint8}
15051498
*/
15061499

15071500
/**
15081501
* @callback TxSum
15091502
* @param {Array<TxHasSats>} coins
1510-
* @returns {Number}
1503+
* @returns {Uint53}
15111504
*/
15121505

15131506
/**
15141507
* @callback TxToDash
1515-
* @param {Number} satoshis
1516-
* @returns {Number} - float
1508+
* @param {Uint53} satoshis
1509+
* @returns {Float64} - float
15171510
*/
15181511

15191512
/**
15201513
* @callback TxToSats
1521-
* @param {Number} dash - as float (decimal) DASH, not uint satoshis
1522-
* @returns {Number} - duffs
1514+
* @param {Float64} dash - as float (decimal) DASH, not uint satoshis
1515+
* @returns {Uint53} - duffs
15231516
*/
15241517

15251518
/**
@@ -1529,15 +1522,16 @@ if ("object" === typeof module) {
15291522
*/
15301523

15311524
/**
1525+
* Caution: JS can't handle 64-bit ints
15321526
* @callback TxToVarInt
1533-
* @param {Number} n
1527+
* @param {BigInt|Uint53} n - 64-bit BigInt or < 53-bit Number
15341528
* @returns {String} - hex
15351529
*/
15361530

15371531
/**
15381532
* @callback TxToVarIntSize
1539-
* @param {Number} n
1540-
* @returns {Number} - byte size of n
1533+
* @param {BigInt|Uint53} n
1534+
* @returns {Uint8} - byte size of n
15411535
*/
15421536

15431537
/**

0 commit comments

Comments
 (0)