Skip to content

Commit cc1b3ec

Browse files
committed
feat: working parser (at least for tx requests)
1 parent d2d194b commit cc1b3ec

File tree

3 files changed

+477
-5
lines changed

3 files changed

+477
-5
lines changed

bin/inspect2.js

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/usr/bin/env node
2+
3+
"use strict";
4+
5+
let Fs = require("node:fs");
6+
7+
let Tx = require("../dashtx.js");
8+
require("../txparser.js");
9+
10+
let filepath = process.argv[2];
11+
12+
async function main() {
13+
let hex = Fs.readFileSync(filepath, "utf8");
14+
hex = hex.trim();
15+
16+
let txInfo;
17+
try {
18+
txInfo = Tx.parseUnknown(hex);
19+
} catch (e) {
20+
//@ts-ignore
21+
console.log(e.transaction);
22+
throw e;
23+
}
24+
25+
console.info(txInfo);
26+
let out = Tx._debugPrint(txInfo);
27+
console.info(out);
28+
}
29+
30+
main()
31+
.then(function () {
32+
process.exit(0);
33+
})
34+
.catch(function (err) {
35+
console.error("Fail:");
36+
console.error(err.stack || err);
37+
process.exit(1);
38+
});

dashtx.js

+38-5
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
* @prop {Uint32} MAX_INPUT_PAD - 2 (possible ASN.1 BigInt padding)
99
* @prop {Uint32} MAX_INPUT_SIZE - 149 each (with padding)
1010
* @prop {Uint32} OUTPUT_SIZE - 34 each
11-
* @prop {Uint32} SIGHASH_ALL - 0x01
11+
* @prop {Uint32|0x00000001} SIGHASH_ALL - 0x01
12+
* @prop {Uint32|0x00000080} SIGHASH_ANYONECANPAY - 0x80
1213
* @prop {TxAppraise} appraise
1314
* @prop {TxAppraiseCounts} _appraiseCounts
1415
* @prop {TxAppraiseMemos} _appraiseMemos
@@ -22,6 +23,10 @@
2223
* @prop {TxGetId} getId - only useful for fully signed tx
2324
* @prop {TxHashPartial} hashPartial - useful for computing sigs
2425
* @prop {TxCreateLegacyTx} createLegacyTx
26+
* @prop {TxParseUnknown} parseUnknown
27+
* @prop {TxParseRequest} parseRequest
28+
* @prop {TxParseHashable} parseHashable
29+
* @prop {TxParseSigned} parseSigned
2530
* @prop {TxSortBySats} sortBySatsAsc
2631
* @prop {TxSortBySats} sortBySatsDsc
2732
* @prop {TxSortInputs} sortInputs
@@ -32,12 +37,14 @@
3237
* @prop {Function} _create
3338
* @prop {Function} _createInsufficientFundsError
3439
* @prop {Function} _createMemoScript
40+
* @prop {Function} _debugPrint
3541
* @prop {Function} _hash
3642
* @prop {Function} _hashAndSignAll
3743
* @prop {Function} _legacyMustSelectInputs
3844
* @prop {Function} _legacySelectOptimalUtxos
3945
* @prop {Function} _packInputs
4046
* @prop {Function} _packOutputs
47+
* @prop {Function} _parse
4148
*/
4249

4350
/**
@@ -153,6 +160,7 @@ var DashTx = ("object" === typeof module && exports) || {};
153160
25; // lockscript
154161

155162
Tx.SIGHASH_ALL = 0x01;
163+
Tx.SIGHASH_ANYONECANPAY = 0x80;
156164

157165
Tx.appraise = function (txInfo) {
158166
let extraSize = Tx._appraiseMemos(txInfo.outputs);
@@ -1368,8 +1376,8 @@ var DashTx = ("object" === typeof module && exports) || {};
13681376
let sigHashTypeHex = TxUtils._toUint32LE(sigHashType);
13691377
txSignable = `${txSignable}${sigHashTypeHex}`;
13701378
}
1371-
//console.log("Signable Tx Hex");
1372-
//console.log(txSignable);
1379+
console.log("Signable Tx Hex");
1380+
console.log(txSignable);
13731381

13741382
let u8 = Tx.utils.hexToBytes(txSignable);
13751383
//console.log("Signable Tx Buffer");
@@ -1609,9 +1617,10 @@ if ("object" === typeof module) {
16091617
// Type Aliases
16101618

16111619
/** @typedef {Number} Float64 */
1612-
/** @typedef {Number} Uint53 */
1613-
/** @typedef {Number} Uint32 */
16141620
/** @typedef {Number} Uint8 */
1621+
/** @typedef {Number} Uint32 */
1622+
/** @typedef {Number} Uint53 */
1623+
/** @typedef {String} Hex */
16151624
/** @typedef {Uint8Array} TxPrivateKey */
16161625
/** @typedef {Uint8Array} TxPublicKey */
16171626
/** @typedef {Uint8Array} TxSignature */
@@ -1878,6 +1887,30 @@ if ("object" === typeof module) {
18781887
* @returns {Promise<TxInfo>}
18791888
*/
18801889

1890+
/**
1891+
* @callback TxParseRequest
1892+
* @param {Hex} hex - a tx request with unsigned or partially signed inputs
1893+
* @returns {TxInfo}
1894+
*/
1895+
1896+
/**
1897+
* @callback TxParseHashable
1898+
* @param {Hex} hex - a ready-to-sign tx with input script and trailing sighash byte
1899+
* @returns {TxInfo}
1900+
*/
1901+
1902+
/**
1903+
* @callback TxParseSigned
1904+
* @param {Hex} hex - a fully signed, ready-to-broadcast transaction
1905+
* @returns {TxInfo}
1906+
*/
1907+
1908+
/**
1909+
* @callback TxParseUnknown
1910+
* @param {Hex} hex - a tx request, hashable tx, or signed tx
1911+
* @returns {TxInfo}
1912+
*/
1913+
18811914
/**
18821915
* @callback TxReverseHex
18831916
* @param {String} hex

0 commit comments

Comments
 (0)