@vbyte/btc-dev 1.0.6 → 1.0.7

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,22 +9895,22 @@ 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));
9917
9915
  const annex = parse_annex_data(elems);
9918
9916
  if (annex !== null)
@@ -10000,26 +9998,26 @@ function get_witness_size(witness) {
10000
9998
  const stack = witness.map(e => Buff.bytes(e));
10001
9999
  const size = stack.reduce((prev, next) => prev + next.length, 0);
10002
10000
  const vsize = Math.ceil(WIT_LENGTH_BYTE + size / 4);
10003
- return { size, vsize };
10001
+ return { total: size, vsize };
10004
10002
  }
10005
10003
 
10006
10004
  var index$5 = /*#__PURE__*/Object.freeze({
10007
10005
  __proto__: null,
10008
10006
  get_witness_size: get_witness_size,
10009
- parse_witness_data: parse_witness_data
10007
+ parse_witness: parse_witness
10010
10008
  });
10011
10009
 
10012
10010
  class TransactionWitness {
10013
10011
  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);
10012
+ this._elems = witness.map(e => Buff.bytes(e));
10013
+ this._data = parse_witness(this._elems);
10014
+ this._size = get_witness_size(this._elems);
10017
10015
  }
10018
10016
  get annex() {
10019
- return this._meta.annex;
10017
+ return this._data.annex;
10020
10018
  }
10021
10019
  get cblock() {
10022
- return this._meta.cblock;
10020
+ return this._data.cblock;
10023
10021
  }
10024
10022
  get data() {
10025
10023
  return {
@@ -10030,35 +10028,53 @@ class TransactionWitness {
10030
10028
  size: this.size,
10031
10029
  stack: this.stack,
10032
10030
  type: this.type,
10033
- version: this.version,
10034
- vsize: this.vsize
10031
+ version: this.version
10035
10032
  };
10036
10033
  }
10037
10034
  get params() {
10038
- return this._meta.params;
10035
+ return this._data.params;
10039
10036
  }
10040
10037
  get script() {
10041
- if (this._meta.script === null)
10038
+ if (this._data.script === null)
10042
10039
  return null;
10043
10040
  return {
10044
- hex: this._meta.script,
10045
- asm: decode_script(this._meta.script)
10041
+ hex: this._data.script,
10042
+ asm: decode_script(this._data.script)
10046
10043
  };
10047
10044
  }
10048
10045
  get size() {
10049
- return this._size.size;
10046
+ return this._size;
10050
10047
  }
10051
10048
  get stack() {
10052
- return this._data.map(e => e.hex);
10049
+ return this._elems.map(e => e.hex);
10053
10050
  }
10054
10051
  get type() {
10055
- return this._meta.type;
10052
+ return this._data.type;
10056
10053
  }
10057
10054
  get version() {
10058
- return this._meta.version;
10055
+ return this._data.version;
10059
10056
  }
10060
- get vsize() {
10061
- return this._size.vsize;
10057
+ _update() {
10058
+ this._data = parse_witness(this._elems);
10059
+ this._size = get_witness_size(this._elems);
10060
+ }
10061
+ add(elem) {
10062
+ this._elems.push(Buff.bytes(elem));
10063
+ this._update();
10064
+ }
10065
+ insert(index, elem) {
10066
+ Assert.ok(index >= 0 && index <= this._elems.length, 'index out of bounds');
10067
+ if (index === this._elems.length) {
10068
+ this._elems.push(Buff.bytes(elem));
10069
+ }
10070
+ else {
10071
+ this._elems.splice(index, 0, Buff.bytes(elem));
10072
+ }
10073
+ this._update();
10074
+ }
10075
+ remove(index) {
10076
+ this._elems.splice(index, 1);
10077
+ this._update();
10062
10078
  }
10063
10079
  toJSON() { return this.data; }
10064
10080
  toString() { return JSON.stringify(this.data); }
@@ -10066,11 +10082,7 @@ class TransactionWitness {
10066
10082
 
10067
10083
  class TransactionInput {
10068
10084
  constructor(txin) {
10069
- this._size = get_txin_size(txin);
10070
10085
  this._txin = txin;
10071
- this._witness = txin.witness.length > 0
10072
- ? new TransactionWitness(txin.witness)
10073
- : null;
10074
10086
  }
10075
10087
  get coinbase() {
10076
10088
  return this._txin.coinbase;
@@ -10114,7 +10126,7 @@ class TransactionInput {
10114
10126
  };
10115
10127
  }
10116
10128
  get size() {
10117
- return this._size;
10129
+ return get_txin_size(this._txin);
10118
10130
  }
10119
10131
  get txid() {
10120
10132
  return this._txin.txid;
@@ -10123,7 +10135,9 @@ class TransactionInput {
10123
10135
  return this._txin.vout;
10124
10136
  }
10125
10137
  get witness() {
10126
- return this._witness;
10138
+ return this._txin.witness.length > 0
10139
+ ? new TransactionWitness(this._txin.witness)
10140
+ : null;
10127
10141
  }
10128
10142
  toJSON() { return this.data; }
10129
10143
  toString() { return JSON.stringify(this.data); }
@@ -10134,11 +10148,12 @@ let Transaction$1 = class Transaction {
10134
10148
  this._tx = (typeof txdata !== 'string')
10135
10149
  ? parse_tx(txdata)
10136
10150
  : 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);
10151
+ this._size = this._get_size();
10140
10152
  this._hash = get_txhash(this._tx);
10153
+ this._txid = get_txid(this._tx);
10141
10154
  this._value = get_tx_value(this._tx);
10155
+ this._vin = this._tx.vin.map(txin => new TransactionInput(txin));
10156
+ this._vout = this._tx.vout.map(txout => new TransactionOutput(txout));
10142
10157
  }
10143
10158
  get data() {
10144
10159
  return {
@@ -10168,13 +10183,7 @@ let Transaction$1 = class Transaction {
10168
10183
  return this._tx.vout.find(txout => is_return_script(txout.script_pk)) || null;
10169
10184
  }
10170
10185
  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
- };
10186
+ return this._size;
10178
10187
  }
10179
10188
  get spends() {
10180
10189
  return this._tx.vin
@@ -10182,7 +10191,7 @@ let Transaction$1 = class Transaction {
10182
10191
  .map(txin => new TransactionOutput(txin.prevout));
10183
10192
  }
10184
10193
  get txid() {
10185
- return get_txid(this._tx);
10194
+ return this._txid;
10186
10195
  }
10187
10196
  get value() {
10188
10197
  return this._value;
@@ -10196,6 +10205,67 @@ let Transaction$1 = class Transaction {
10196
10205
  get vout() {
10197
10206
  return this._vout;
10198
10207
  }
10208
+ add_vin(txin) {
10209
+ this._tx.vin.push(txin);
10210
+ this._update_vin();
10211
+ }
10212
+ add_vout(txout) {
10213
+ this._tx.vout.push(txout);
10214
+ this._update_vout();
10215
+ }
10216
+ insert_vin(index, txin) {
10217
+ Assert.ok(index >= 0 && index <= this._tx.vin.length, 'input goes out of bounds');
10218
+ if (index === this._tx.vin.length) {
10219
+ this._tx.vin.push(txin);
10220
+ }
10221
+ else {
10222
+ this._tx.vin.splice(index, 0, txin);
10223
+ }
10224
+ this._update_vin();
10225
+ }
10226
+ insert_vout(index, txout) {
10227
+ Assert.ok(index >= 0 && index <= this._tx.vout.length, 'output goes out of bounds');
10228
+ if (index === this._tx.vout.length) {
10229
+ this._tx.vout.push(txout);
10230
+ }
10231
+ else {
10232
+ this._tx.vout.splice(index, 0, txout);
10233
+ }
10234
+ this._update_vout();
10235
+ }
10236
+ remove_vin(index) {
10237
+ Assert.ok(this._tx.vin.at(index) !== undefined, 'input does not exist at index');
10238
+ this._tx.vin.splice(index, 1);
10239
+ this._update_vin();
10240
+ }
10241
+ remove_vout(index) {
10242
+ Assert.ok(this._tx.vout.at(index) !== undefined, 'output does not exist at index');
10243
+ this._tx.vout.splice(index, 1);
10244
+ this._update_vout();
10245
+ }
10246
+ _get_size() {
10247
+ return {
10248
+ ...get_txsize(this._tx),
10249
+ segwit: this.vin.reduce((acc, txin) => acc + (txin.witness?.size.vsize ?? 0), 0),
10250
+ vin: this.vin.reduce((acc, txin) => acc + txin.size, 0),
10251
+ vout: this.vout.reduce((acc, txout) => acc + txout.size, 0),
10252
+ witness: this.vin.reduce((acc, txin) => acc + (txin.witness?.size.total ?? 0), 0)
10253
+ };
10254
+ }
10255
+ _update_tx() {
10256
+ this._size = this._get_size();
10257
+ this._hash = get_txhash(this._tx);
10258
+ this._txid = get_txid(this._tx);
10259
+ this._value = get_tx_value(this._tx);
10260
+ }
10261
+ _update_vin() {
10262
+ this._vin = this._tx.vin.map(txin => new TransactionInput(txin));
10263
+ this._update_tx();
10264
+ }
10265
+ _update_vout() {
10266
+ this._vout = this._tx.vout.map(txout => new TransactionOutput(txout));
10267
+ this._update_tx();
10268
+ }
10199
10269
  toJSON() { return this.data; }
10200
10270
  toString() { return JSON.stringify(this.data); }
10201
10271
  };
@@ -14517,7 +14587,7 @@ function merkleize(taptree, target, path = []) {
14517
14587
  }
14518
14588
 
14519
14589
  function parse_taproot_witness(witness) {
14520
- const { cblock, params, script } = parse_witness_data(witness);
14590
+ const { cblock, params, script } = parse_witness(witness);
14521
14591
  Assert.exists(cblock, 'cblock is null');
14522
14592
  Assert.exists(script, 'script is null');
14523
14593
  const cblk = parse_cblock(cblock);