@vbyte/btc-dev 1.0.6 → 1.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/module.mjs CHANGED
@@ -9157,11 +9157,11 @@ function get_vsize$1(bytes) {
9157
9157
  function get_txsize(txdata) {
9158
9158
  const json = parse_tx(txdata);
9159
9159
  const base = encode_tx(json, false).length;
9160
- const size = encode_tx(json, true).length;
9161
- const weight = base * 3 + size;
9160
+ const total = encode_tx(json, true).length;
9161
+ const weight = base * 3 + total;
9162
9162
  const remain = (weight % 4 > 0) ? 1 : 0;
9163
9163
  const vsize = Math.floor(weight / 4) + remain;
9164
- return { base, real: size, vsize, weight };
9164
+ return { base, total, vsize, weight };
9165
9165
  }
9166
9166
  function get_vin_size(vin) {
9167
9167
  const bytes = encode_tx_inputs(vin);
@@ -9877,8 +9877,6 @@ var index$6 = /*#__PURE__*/Object.freeze({
9877
9877
 
9878
9878
  class TransactionOutput {
9879
9879
  constructor(txout) {
9880
- this._info = get_vout_info(txout);
9881
- this._size = get_txout_size(txout);
9882
9880
  this._txout = txout;
9883
9881
  }
9884
9882
  get data() {
@@ -9897,23 +9895,24 @@ class TransactionOutput {
9897
9895
  };
9898
9896
  }
9899
9897
  get size() {
9900
- return this._size;
9898
+ return get_txout_size(this._txout);
9901
9899
  }
9902
9900
  get type() {
9903
- return this._info.type;
9901
+ return get_vout_type(this._txout.script_pk);
9904
9902
  }
9905
9903
  get value() {
9906
9904
  return this._txout.value;
9907
9905
  }
9908
9906
  get version() {
9909
- return this._info.version;
9907
+ return get_vout_version(this._txout.script_pk);
9910
9908
  }
9911
9909
  toJSON() { return this.data; }
9912
9910
  toString() { return JSON.stringify(this.data); }
9913
9911
  }
9914
9912
 
9915
- function parse_witness_data(witness) {
9913
+ function parse_witness(witness) {
9916
9914
  const elems = witness.map(e => Buff.bytes(e));
9915
+ const stack = witness.map(e => Buff.bytes(e).hex);
9917
9916
  const annex = parse_annex_data(elems);
9918
9917
  if (annex !== null)
9919
9918
  elems.pop();
@@ -9926,7 +9925,7 @@ function parse_witness_data(witness) {
9926
9925
  if (script !== null)
9927
9926
  elems.pop();
9928
9927
  const params = elems.map(e => e.hex);
9929
- return { annex, cblock, params, script, type, version };
9928
+ return { annex, cblock, params, script, stack, type, version };
9930
9929
  }
9931
9930
  function parse_annex_data(data) {
9932
9931
  let elem = data.at(-1);
@@ -10000,65 +9999,74 @@ function get_witness_size(witness) {
10000
9999
  const stack = witness.map(e => Buff.bytes(e));
10001
10000
  const size = stack.reduce((prev, next) => prev + next.length, 0);
10002
10001
  const vsize = Math.ceil(WIT_LENGTH_BYTE + size / 4);
10003
- return { size, vsize };
10002
+ return { total: size, vsize };
10004
10003
  }
10005
10004
 
10006
10005
  var index$5 = /*#__PURE__*/Object.freeze({
10007
10006
  __proto__: null,
10008
10007
  get_witness_size: get_witness_size,
10009
- parse_witness_data: parse_witness_data
10008
+ parse_witness: parse_witness
10010
10009
  });
10011
10010
 
10012
10011
  class TransactionWitness {
10013
10012
  constructor(witness) {
10014
- this._data = witness.map(e => Buff.bytes(e));
10015
- this._meta = parse_witness_data(witness);
10016
- this._size = get_witness_size(witness);
10013
+ this._elems = witness.map(e => Buff.bytes(e));
10014
+ this._data = parse_witness(this._elems);
10015
+ this._size = get_witness_size(this._elems);
10017
10016
  }
10018
10017
  get annex() {
10019
- return this._meta.annex;
10018
+ return this._data.annex;
10020
10019
  }
10021
10020
  get cblock() {
10022
- return this._meta.cblock;
10021
+ return this._data.cblock;
10023
10022
  }
10024
10023
  get data() {
10025
- return {
10026
- annex: this.annex,
10027
- cblock: this.cblock,
10028
- params: this.params,
10029
- script: this.script,
10030
- size: this.size,
10031
- stack: this.stack,
10032
- type: this.type,
10033
- version: this.version,
10034
- vsize: this.vsize
10035
- };
10024
+ return this._data;
10036
10025
  }
10037
10026
  get params() {
10038
- return this._meta.params;
10027
+ return this._data.params;
10039
10028
  }
10040
10029
  get script() {
10041
- if (this._meta.script === null)
10030
+ if (this._data.script === null)
10042
10031
  return null;
10043
10032
  return {
10044
- hex: this._meta.script,
10045
- asm: decode_script(this._meta.script)
10033
+ hex: this._data.script,
10034
+ asm: decode_script(this._data.script)
10046
10035
  };
10047
10036
  }
10048
10037
  get size() {
10049
- return this._size.size;
10038
+ return this._size;
10050
10039
  }
10051
10040
  get stack() {
10052
- return this._data.map(e => e.hex);
10041
+ return this._elems.map(e => e.hex);
10053
10042
  }
10054
10043
  get type() {
10055
- return this._meta.type;
10044
+ return this._data.type;
10056
10045
  }
10057
10046
  get version() {
10058
- return this._meta.version;
10047
+ return this._data.version;
10059
10048
  }
10060
- get vsize() {
10061
- return this._size.vsize;
10049
+ _update() {
10050
+ this._data = parse_witness(this._elems);
10051
+ this._size = get_witness_size(this._elems);
10052
+ }
10053
+ add(elem) {
10054
+ this._elems.push(Buff.bytes(elem));
10055
+ this._update();
10056
+ }
10057
+ insert(index, elem) {
10058
+ Assert.ok(index >= 0 && index <= this._elems.length, 'index out of bounds');
10059
+ if (index === this._elems.length) {
10060
+ this._elems.push(Buff.bytes(elem));
10061
+ }
10062
+ else {
10063
+ this._elems.splice(index, 0, Buff.bytes(elem));
10064
+ }
10065
+ this._update();
10066
+ }
10067
+ remove(index) {
10068
+ this._elems.splice(index, 1);
10069
+ this._update();
10062
10070
  }
10063
10071
  toJSON() { return this.data; }
10064
10072
  toString() { return JSON.stringify(this.data); }
@@ -10066,11 +10074,7 @@ class TransactionWitness {
10066
10074
 
10067
10075
  class TransactionInput {
10068
10076
  constructor(txin) {
10069
- this._size = get_txin_size(txin);
10070
10077
  this._txin = txin;
10071
- this._witness = txin.witness.length > 0
10072
- ? new TransactionWitness(txin.witness)
10073
- : null;
10074
10078
  }
10075
10079
  get coinbase() {
10076
10080
  return this._txin.coinbase;
@@ -10114,7 +10118,7 @@ class TransactionInput {
10114
10118
  };
10115
10119
  }
10116
10120
  get size() {
10117
- return this._size;
10121
+ return get_txin_size(this._txin);
10118
10122
  }
10119
10123
  get txid() {
10120
10124
  return this._txin.txid;
@@ -10123,22 +10127,25 @@ class TransactionInput {
10123
10127
  return this._txin.vout;
10124
10128
  }
10125
10129
  get witness() {
10126
- return this._witness;
10130
+ return this._txin.witness.length > 0
10131
+ ? new TransactionWitness(this._txin.witness)
10132
+ : null;
10127
10133
  }
10128
10134
  toJSON() { return this.data; }
10129
10135
  toString() { return JSON.stringify(this.data); }
10130
10136
  }
10131
10137
 
10132
10138
  let Transaction$1 = class Transaction {
10133
- constructor(txdata) {
10139
+ constructor(txdata = {}) {
10134
10140
  this._tx = (typeof txdata !== 'string')
10135
10141
  ? parse_tx(txdata)
10136
10142
  : decode_tx(txdata);
10137
- this._vin = this._tx.vin.map(txin => new TransactionInput(txin));
10138
- this._vout = this._tx.vout.map(txout => new TransactionOutput(txout));
10139
- this._size = get_txsize(this._tx);
10143
+ this._size = this._get_size();
10140
10144
  this._hash = get_txhash(this._tx);
10145
+ this._txid = get_txid(this._tx);
10141
10146
  this._value = get_tx_value(this._tx);
10147
+ this._vin = this._tx.vin.map(txin => new TransactionInput(txin));
10148
+ this._vout = this._tx.vout.map(txout => new TransactionOutput(txout));
10142
10149
  }
10143
10150
  get data() {
10144
10151
  return {
@@ -10168,13 +10175,7 @@ let Transaction$1 = class Transaction {
10168
10175
  return this._tx.vout.find(txout => is_return_script(txout.script_pk)) || null;
10169
10176
  }
10170
10177
  get size() {
10171
- return {
10172
- ...this._size,
10173
- segwit: this._vin.reduce((acc, txin) => acc + (txin.witness?.vsize ?? 0), 0),
10174
- vin: this._vin.reduce((acc, txin) => acc + txin.size, 0),
10175
- vout: this._vout.reduce((acc, txout) => acc + txout.size, 0),
10176
- witness: this._vin.reduce((acc, txin) => acc + (txin.witness?.size ?? 0), 0)
10177
- };
10178
+ return this._size;
10178
10179
  }
10179
10180
  get spends() {
10180
10181
  return this._tx.vin
@@ -10182,7 +10183,7 @@ let Transaction$1 = class Transaction {
10182
10183
  .map(txin => new TransactionOutput(txin.prevout));
10183
10184
  }
10184
10185
  get txid() {
10185
- return get_txid(this._tx);
10186
+ return this._txid;
10186
10187
  }
10187
10188
  get value() {
10188
10189
  return this._value;
@@ -10196,6 +10197,71 @@ let Transaction$1 = class Transaction {
10196
10197
  get vout() {
10197
10198
  return this._vout;
10198
10199
  }
10200
+ add_vin(tx_input) {
10201
+ const txin = create_tx_input(tx_input);
10202
+ this._tx.vin.push(txin);
10203
+ this._update_vin();
10204
+ }
10205
+ add_vout(tx_output) {
10206
+ const txout = create_tx_output(tx_output);
10207
+ this._tx.vout.push(txout);
10208
+ this._update_vout();
10209
+ }
10210
+ insert_vin(index, tx_input) {
10211
+ Assert.ok(index >= 0 && index <= this._tx.vin.length, 'input goes out of bounds');
10212
+ const txin = create_tx_input(tx_input);
10213
+ if (index === this._tx.vin.length) {
10214
+ this._tx.vin.push(txin);
10215
+ }
10216
+ else {
10217
+ this._tx.vin.splice(index, 0, txin);
10218
+ }
10219
+ this._update_vin();
10220
+ }
10221
+ insert_vout(index, tx_output) {
10222
+ Assert.ok(index >= 0 && index <= this._tx.vout.length, 'output goes out of bounds');
10223
+ const txout = create_tx_output(tx_output);
10224
+ if (index === this._tx.vout.length) {
10225
+ this._tx.vout.push(txout);
10226
+ }
10227
+ else {
10228
+ this._tx.vout.splice(index, 0, txout);
10229
+ }
10230
+ this._update_vout();
10231
+ }
10232
+ remove_vin(index) {
10233
+ Assert.ok(this._tx.vin.at(index) !== undefined, 'input does not exist at index');
10234
+ this._tx.vin.splice(index, 1);
10235
+ this._update_vin();
10236
+ }
10237
+ remove_vout(index) {
10238
+ Assert.ok(this._tx.vout.at(index) !== undefined, 'output does not exist at index');
10239
+ this._tx.vout.splice(index, 1);
10240
+ this._update_vout();
10241
+ }
10242
+ _get_size() {
10243
+ return {
10244
+ ...get_txsize(this._tx),
10245
+ segwit: this.vin.reduce((acc, txin) => acc + (txin.witness?.size.vsize ?? 0), 0),
10246
+ vin: this.vin.reduce((acc, txin) => acc + txin.size, 0),
10247
+ vout: this.vout.reduce((acc, txout) => acc + txout.size, 0),
10248
+ witness: this.vin.reduce((acc, txin) => acc + (txin.witness?.size.total ?? 0), 0)
10249
+ };
10250
+ }
10251
+ _update_tx() {
10252
+ this._size = this._get_size();
10253
+ this._hash = get_txhash(this._tx);
10254
+ this._txid = get_txid(this._tx);
10255
+ this._value = get_tx_value(this._tx);
10256
+ }
10257
+ _update_vin() {
10258
+ this._vin = this._tx.vin.map(txin => new TransactionInput(txin));
10259
+ this._update_tx();
10260
+ }
10261
+ _update_vout() {
10262
+ this._vout = this._tx.vout.map(txout => new TransactionOutput(txout));
10263
+ this._update_tx();
10264
+ }
10199
10265
  toJSON() { return this.data; }
10200
10266
  toString() { return JSON.stringify(this.data); }
10201
10267
  };
@@ -14517,7 +14583,7 @@ function merkleize(taptree, target, path = []) {
14517
14583
  }
14518
14584
 
14519
14585
  function parse_taproot_witness(witness) {
14520
- const { cblock, params, script } = parse_witness_data(witness);
14586
+ const { cblock, params, script } = parse_witness(witness);
14521
14587
  Assert.exists(cblock, 'cblock is null');
14522
14588
  Assert.exists(script, 'script is null');
14523
14589
  const cblk = parse_cblock(cblock);