Skip to content

Commit

Permalink
Use ES2020, add dev dep for randombytes
Browse files Browse the repository at this point in the history
  • Loading branch information
junderw committed Oct 20, 2021
1 parent 32cc25d commit b1ff3ce
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 43 deletions.
9 changes: 9 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
18 changes: 10 additions & 8 deletions src/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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.
Expand Down
4 changes: 0 additions & 4 deletions src/bufferutils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
32 changes: 14 additions & 18 deletions src/psbt.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
Expand Down
24 changes: 13 additions & 11 deletions src/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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;
2 changes: 1 addition & 1 deletion test/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"compilerOptions": {
"target": "ESNEXT",
"target": "ES2020",
"module": "commonjs",
"outDir": "../",
"declaration": false,
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"compilerOptions": {
"target": "ESNEXT",
"target": "ES2020",
"module": "commonjs",
"outDir": "./src",
"declaration": true,
Expand Down

0 comments on commit b1ff3ce

Please sign in to comment.