diff --git a/package-lock.json b/package-lock.json index d021ca3..6512208 100644 --- a/package-lock.json +++ b/package-lock.json @@ -427,6 +427,15 @@ "integrity": "sha512-SQaNzWQ2YZSr7FqAyPPiA3FYpux2Lqh3HWMZQk47x3xbMCqgC/w0dY3dw9rGqlweDDkrySQBcaScXWeR+Yb11Q==", "dev": true }, + "@types/randombytes": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/@types/randombytes/-/randombytes-2.0.0.tgz", + "integrity": "sha512-bz8PhAVlwN72vqefzxa14DKNT8jK/mV66CSjwdVQM/k3Th3EPKfUtdMniwZgMedQTFuywAsfjnZsg+pEnltaMA==", + "dev": true, + "requires": { + "@types/node": "*" + } + }, "@types/wif": { "version": "2.0.2", "resolved": "https://registry.npmjs.org/@types/wif/-/wif-2.0.2.tgz", diff --git a/package.json b/package.json index 10b0d76..0b49862 100644 --- a/package.json +++ b/package.json @@ -65,6 +65,7 @@ "@types/mocha": "^5.2.7", "@types/node": "^16.11.1", "@types/proxyquire": "^1.3.28", + "@types/randombytes": "^2.0.0", "@types/wif": "^2.0.2", "bip39": "^3.0.2", "bip65": "^1.0.1", @@ -80,6 +81,7 @@ "nyc": "^15.1.0", "prettier": "1.16.4", "proxyquire": "^2.0.1", + "randombytes": "^2.1.0", "regtest-client": "0.2.0", "rimraf": "^2.6.3", "ts-node": "^8.3.0", diff --git a/src/block.js b/src/block.js index 4e6ca84..2c130c3 100644 --- a/src/block.js +++ b/src/block.js @@ -14,6 +14,16 @@ const errorWitnessNotSegwit = new TypeError( 'Cannot compute witness commit for non-segwit block', ); class Block { + constructor() { + this.version = 1; + this.prevHash = undefined; + this.merkleRoot = undefined; + this.timestamp = 0; + this.witnessCommit = undefined; + this.bits = 0; + this.nonce = 0; + this.transactions = undefined; + } static fromBuffer(buffer) { if (buffer.length < 80) throw new Error('Buffer too small (< 80 bytes)'); const bufferReader = new bufferutils_1.BufferReader(buffer); @@ -69,14 +79,6 @@ class Block { ) : rootHash; } - version = 1; - prevHash = undefined; - merkleRoot = undefined; - timestamp = 0; - witnessCommit = undefined; - bits = 0; - nonce = 0; - transactions = undefined; getWitnessCommit() { if (!txesHaveWitnessCommit(this.transactions)) return null; // The merkle root for the witness data is in an OP_RETURN output. diff --git a/src/bufferutils.js b/src/bufferutils.js index 933bebc..fcab0b7 100644 --- a/src/bufferutils.js +++ b/src/bufferutils.js @@ -53,8 +53,6 @@ exports.cloneBuffer = cloneBuffer; * Helper class for serialization of bitcoin data types into a pre-allocated buffer. */ class BufferWriter { - buffer; - offset; constructor(buffer, offset = 0) { this.buffer = buffer; this.offset = offset; @@ -96,8 +94,6 @@ exports.BufferWriter = BufferWriter; * Helper class for reading of bitcoin data types from a buffer. */ class BufferReader { - buffer; - offset; constructor(buffer, offset = 0) { this.buffer = buffer; this.offset = offset; diff --git a/src/psbt.js b/src/psbt.js index 84aa5b8..6162195 100644 --- a/src/psbt.js +++ b/src/psbt.js @@ -60,23 +60,6 @@ const DEFAULT_OPTS = { * Transaction object. Such as fee rate not being larger than maximumFeeRate etc. */ class Psbt { - data; - static fromBase64(data, opts = {}) { - const buffer = Buffer.from(data, 'base64'); - return this.fromBuffer(buffer, opts); - } - static fromHex(data, opts = {}) { - const buffer = Buffer.from(data, 'hex'); - return this.fromBuffer(buffer, opts); - } - static fromBuffer(buffer, opts = {}) { - const psbtBase = bip174_1.Psbt.fromBuffer(buffer, transactionFromBuffer); - const psbt = new Psbt(opts, psbtBase); - checkTxForDupeIns(psbt.__CACHE.__TX, psbt.__CACHE); - return psbt; - } - __CACHE; - opts; constructor(opts = {}, data = new bip174_1.Psbt(new PsbtTransaction())) { this.data = data; // set defaults @@ -106,6 +89,20 @@ class Psbt { dpew(this, '__CACHE', false, true); dpew(this, 'opts', false, true); } + static fromBase64(data, opts = {}) { + const buffer = Buffer.from(data, 'base64'); + return this.fromBuffer(buffer, opts); + } + static fromHex(data, opts = {}) { + const buffer = Buffer.from(data, 'hex'); + return this.fromBuffer(buffer, opts); + } + static fromBuffer(buffer, opts = {}) { + const psbtBase = bip174_1.Psbt.fromBuffer(buffer, transactionFromBuffer); + const psbt = new Psbt(opts, psbtBase); + checkTxForDupeIns(psbt.__CACHE.__TX, psbt.__CACHE); + return psbt; + } get inputCount() { return this.data.inputs.length; } @@ -625,7 +622,6 @@ const transactionFromBuffer = buffer => new PsbtTransaction(buffer); * It contains a bitcoinjs-lib Transaction object. */ class PsbtTransaction { - tx; constructor(buffer = Buffer.from([2, 0, 0, 0, 0, 0, 0, 0, 0, 0])) { this.tx = transaction_1.Transaction.fromBuffer(buffer); checkTxEmpty(this.tx); diff --git a/src/transaction.js b/src/transaction.js index e4ebf6f..5a29569 100644 --- a/src/transaction.js +++ b/src/transaction.js @@ -39,13 +39,12 @@ function isOutput(out) { return out.value !== undefined; } class Transaction { - static DEFAULT_SEQUENCE = 0xffffffff; - static SIGHASH_ALL = 0x01; - static SIGHASH_NONE = 0x02; - static SIGHASH_SINGLE = 0x03; - static SIGHASH_ANYONECANPAY = 0x80; - static ADVANCED_TRANSACTION_MARKER = 0x00; - static ADVANCED_TRANSACTION_FLAG = 0x01; + constructor() { + this.version = 1; + this.locktime = 0; + this.ins = []; + this.outs = []; + } static fromBuffer(buffer, _NO_STRICT) { const bufferReader = new bufferutils_1.BufferReader(buffer); const tx = new Transaction(); @@ -102,10 +101,6 @@ class Transaction { } return true; } - version = 1; - locktime = 0; - ins = []; - outs = []; isCoinbase() { return ( this.ins.length === 1 && Transaction.isCoinbaseHash(this.ins[0].hash) @@ -400,3 +395,10 @@ class Transaction { } } exports.Transaction = Transaction; +Transaction.DEFAULT_SEQUENCE = 0xffffffff; +Transaction.SIGHASH_ALL = 0x01; +Transaction.SIGHASH_NONE = 0x02; +Transaction.SIGHASH_SINGLE = 0x03; +Transaction.SIGHASH_ANYONECANPAY = 0x80; +Transaction.ADVANCED_TRANSACTION_MARKER = 0x00; +Transaction.ADVANCED_TRANSACTION_FLAG = 0x01; diff --git a/test/tsconfig.json b/test/tsconfig.json index 4e4f529..6752ec5 100644 --- a/test/tsconfig.json +++ b/test/tsconfig.json @@ -1,6 +1,6 @@ { "compilerOptions": { - "target": "ESNEXT", + "target": "ES2020", "module": "commonjs", "outDir": "../", "declaration": false, diff --git a/tsconfig.json b/tsconfig.json index 3c74061..25f9d61 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,6 +1,6 @@ { "compilerOptions": { - "target": "ESNEXT", + "target": "ES2020", "module": "commonjs", "outDir": "./src", "declaration": true,