@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/main.cjs CHANGED
@@ -9159,11 +9159,11 @@ function get_vsize$1(bytes) {
9159
9159
  function get_txsize(txdata) {
9160
9160
  const json = parse_tx(txdata);
9161
9161
  const base = encode_tx(json, false).length;
9162
- const size = encode_tx(json, true).length;
9163
- const weight = base * 3 + size;
9162
+ const total = encode_tx(json, true).length;
9163
+ const weight = base * 3 + total;
9164
9164
  const remain = (weight % 4 > 0) ? 1 : 0;
9165
9165
  const vsize = Math.floor(weight / 4) + remain;
9166
- return { base, real: size, vsize, weight };
9166
+ return { base, total, vsize, weight };
9167
9167
  }
9168
9168
  function get_vin_size(vin) {
9169
9169
  const bytes = encode_tx_inputs(vin);
@@ -9879,8 +9879,6 @@ var index$6 = /*#__PURE__*/Object.freeze({
9879
9879
 
9880
9880
  class TransactionOutput {
9881
9881
  constructor(txout) {
9882
- this._info = get_vout_info(txout);
9883
- this._size = get_txout_size(txout);
9884
9882
  this._txout = txout;
9885
9883
  }
9886
9884
  get data() {
@@ -9899,22 +9897,22 @@ class TransactionOutput {
9899
9897
  };
9900
9898
  }
9901
9899
  get size() {
9902
- return this._size;
9900
+ return get_txout_size(this._txout);
9903
9901
  }
9904
9902
  get type() {
9905
- return this._info.type;
9903
+ return get_vout_type(this._txout.script_pk);
9906
9904
  }
9907
9905
  get value() {
9908
9906
  return this._txout.value;
9909
9907
  }
9910
9908
  get version() {
9911
- return this._info.version;
9909
+ return get_vout_version(this._txout.script_pk);
9912
9910
  }
9913
9911
  toJSON() { return this.data; }
9914
9912
  toString() { return JSON.stringify(this.data); }
9915
9913
  }
9916
9914
 
9917
- function parse_witness_data(witness) {
9915
+ function parse_witness(witness) {
9918
9916
  const elems = witness.map(e => Buff.bytes(e));
9919
9917
  const annex = parse_annex_data(elems);
9920
9918
  if (annex !== null)
@@ -10002,26 +10000,26 @@ function get_witness_size(witness) {
10002
10000
  const stack = witness.map(e => Buff.bytes(e));
10003
10001
  const size = stack.reduce((prev, next) => prev + next.length, 0);
10004
10002
  const vsize = Math.ceil(WIT_LENGTH_BYTE + size / 4);
10005
- return { size, vsize };
10003
+ return { total: size, vsize };
10006
10004
  }
10007
10005
 
10008
10006
  var index$5 = /*#__PURE__*/Object.freeze({
10009
10007
  __proto__: null,
10010
10008
  get_witness_size: get_witness_size,
10011
- parse_witness_data: parse_witness_data
10009
+ parse_witness: parse_witness
10012
10010
  });
10013
10011
 
10014
10012
  class TransactionWitness {
10015
10013
  constructor(witness) {
10016
- this._data = witness.map(e => Buff.bytes(e));
10017
- this._meta = parse_witness_data(witness);
10018
- this._size = get_witness_size(witness);
10014
+ this._elems = witness.map(e => Buff.bytes(e));
10015
+ this._data = parse_witness(this._elems);
10016
+ this._size = get_witness_size(this._elems);
10019
10017
  }
10020
10018
  get annex() {
10021
- return this._meta.annex;
10019
+ return this._data.annex;
10022
10020
  }
10023
10021
  get cblock() {
10024
- return this._meta.cblock;
10022
+ return this._data.cblock;
10025
10023
  }
10026
10024
  get data() {
10027
10025
  return {
@@ -10032,35 +10030,53 @@ class TransactionWitness {
10032
10030
  size: this.size,
10033
10031
  stack: this.stack,
10034
10032
  type: this.type,
10035
- version: this.version,
10036
- vsize: this.vsize
10033
+ version: this.version
10037
10034
  };
10038
10035
  }
10039
10036
  get params() {
10040
- return this._meta.params;
10037
+ return this._data.params;
10041
10038
  }
10042
10039
  get script() {
10043
- if (this._meta.script === null)
10040
+ if (this._data.script === null)
10044
10041
  return null;
10045
10042
  return {
10046
- hex: this._meta.script,
10047
- asm: decode_script(this._meta.script)
10043
+ hex: this._data.script,
10044
+ asm: decode_script(this._data.script)
10048
10045
  };
10049
10046
  }
10050
10047
  get size() {
10051
- return this._size.size;
10048
+ return this._size;
10052
10049
  }
10053
10050
  get stack() {
10054
- return this._data.map(e => e.hex);
10051
+ return this._elems.map(e => e.hex);
10055
10052
  }
10056
10053
  get type() {
10057
- return this._meta.type;
10054
+ return this._data.type;
10058
10055
  }
10059
10056
  get version() {
10060
- return this._meta.version;
10057
+ return this._data.version;
10061
10058
  }
10062
- get vsize() {
10063
- return this._size.vsize;
10059
+ _update() {
10060
+ this._data = parse_witness(this._elems);
10061
+ this._size = get_witness_size(this._elems);
10062
+ }
10063
+ add(elem) {
10064
+ this._elems.push(Buff.bytes(elem));
10065
+ this._update();
10066
+ }
10067
+ insert(index, elem) {
10068
+ Assert.ok(index >= 0 && index <= this._elems.length, 'index out of bounds');
10069
+ if (index === this._elems.length) {
10070
+ this._elems.push(Buff.bytes(elem));
10071
+ }
10072
+ else {
10073
+ this._elems.splice(index, 0, Buff.bytes(elem));
10074
+ }
10075
+ this._update();
10076
+ }
10077
+ remove(index) {
10078
+ this._elems.splice(index, 1);
10079
+ this._update();
10064
10080
  }
10065
10081
  toJSON() { return this.data; }
10066
10082
  toString() { return JSON.stringify(this.data); }
@@ -10068,11 +10084,7 @@ class TransactionWitness {
10068
10084
 
10069
10085
  class TransactionInput {
10070
10086
  constructor(txin) {
10071
- this._size = get_txin_size(txin);
10072
10087
  this._txin = txin;
10073
- this._witness = txin.witness.length > 0
10074
- ? new TransactionWitness(txin.witness)
10075
- : null;
10076
10088
  }
10077
10089
  get coinbase() {
10078
10090
  return this._txin.coinbase;
@@ -10116,7 +10128,7 @@ class TransactionInput {
10116
10128
  };
10117
10129
  }
10118
10130
  get size() {
10119
- return this._size;
10131
+ return get_txin_size(this._txin);
10120
10132
  }
10121
10133
  get txid() {
10122
10134
  return this._txin.txid;
@@ -10125,7 +10137,9 @@ class TransactionInput {
10125
10137
  return this._txin.vout;
10126
10138
  }
10127
10139
  get witness() {
10128
- return this._witness;
10140
+ return this._txin.witness.length > 0
10141
+ ? new TransactionWitness(this._txin.witness)
10142
+ : null;
10129
10143
  }
10130
10144
  toJSON() { return this.data; }
10131
10145
  toString() { return JSON.stringify(this.data); }
@@ -10136,11 +10150,12 @@ let Transaction$1 = class Transaction {
10136
10150
  this._tx = (typeof txdata !== 'string')
10137
10151
  ? parse_tx(txdata)
10138
10152
  : decode_tx(txdata);
10139
- this._vin = this._tx.vin.map(txin => new TransactionInput(txin));
10140
- this._vout = this._tx.vout.map(txout => new TransactionOutput(txout));
10141
- this._size = get_txsize(this._tx);
10153
+ this._size = this._get_size();
10142
10154
  this._hash = get_txhash(this._tx);
10155
+ this._txid = get_txid(this._tx);
10143
10156
  this._value = get_tx_value(this._tx);
10157
+ this._vin = this._tx.vin.map(txin => new TransactionInput(txin));
10158
+ this._vout = this._tx.vout.map(txout => new TransactionOutput(txout));
10144
10159
  }
10145
10160
  get data() {
10146
10161
  return {
@@ -10170,13 +10185,7 @@ let Transaction$1 = class Transaction {
10170
10185
  return this._tx.vout.find(txout => is_return_script(txout.script_pk)) || null;
10171
10186
  }
10172
10187
  get size() {
10173
- return {
10174
- ...this._size,
10175
- segwit: this._vin.reduce((acc, txin) => acc + (txin.witness?.vsize ?? 0), 0),
10176
- vin: this._vin.reduce((acc, txin) => acc + txin.size, 0),
10177
- vout: this._vout.reduce((acc, txout) => acc + txout.size, 0),
10178
- witness: this._vin.reduce((acc, txin) => acc + (txin.witness?.size ?? 0), 0)
10179
- };
10188
+ return this._size;
10180
10189
  }
10181
10190
  get spends() {
10182
10191
  return this._tx.vin
@@ -10184,7 +10193,7 @@ let Transaction$1 = class Transaction {
10184
10193
  .map(txin => new TransactionOutput(txin.prevout));
10185
10194
  }
10186
10195
  get txid() {
10187
- return get_txid(this._tx);
10196
+ return this._txid;
10188
10197
  }
10189
10198
  get value() {
10190
10199
  return this._value;
@@ -10198,6 +10207,67 @@ let Transaction$1 = class Transaction {
10198
10207
  get vout() {
10199
10208
  return this._vout;
10200
10209
  }
10210
+ add_vin(txin) {
10211
+ this._tx.vin.push(txin);
10212
+ this._update_vin();
10213
+ }
10214
+ add_vout(txout) {
10215
+ this._tx.vout.push(txout);
10216
+ this._update_vout();
10217
+ }
10218
+ insert_vin(index, txin) {
10219
+ Assert.ok(index >= 0 && index <= this._tx.vin.length, 'input goes out of bounds');
10220
+ if (index === this._tx.vin.length) {
10221
+ this._tx.vin.push(txin);
10222
+ }
10223
+ else {
10224
+ this._tx.vin.splice(index, 0, txin);
10225
+ }
10226
+ this._update_vin();
10227
+ }
10228
+ insert_vout(index, txout) {
10229
+ Assert.ok(index >= 0 && index <= this._tx.vout.length, 'output goes out of bounds');
10230
+ if (index === this._tx.vout.length) {
10231
+ this._tx.vout.push(txout);
10232
+ }
10233
+ else {
10234
+ this._tx.vout.splice(index, 0, txout);
10235
+ }
10236
+ this._update_vout();
10237
+ }
10238
+ remove_vin(index) {
10239
+ Assert.ok(this._tx.vin.at(index) !== undefined, 'input does not exist at index');
10240
+ this._tx.vin.splice(index, 1);
10241
+ this._update_vin();
10242
+ }
10243
+ remove_vout(index) {
10244
+ Assert.ok(this._tx.vout.at(index) !== undefined, 'output does not exist at index');
10245
+ this._tx.vout.splice(index, 1);
10246
+ this._update_vout();
10247
+ }
10248
+ _get_size() {
10249
+ return {
10250
+ ...get_txsize(this._tx),
10251
+ segwit: this.vin.reduce((acc, txin) => acc + (txin.witness?.size.vsize ?? 0), 0),
10252
+ vin: this.vin.reduce((acc, txin) => acc + txin.size, 0),
10253
+ vout: this.vout.reduce((acc, txout) => acc + txout.size, 0),
10254
+ witness: this.vin.reduce((acc, txin) => acc + (txin.witness?.size.total ?? 0), 0)
10255
+ };
10256
+ }
10257
+ _update_tx() {
10258
+ this._size = this._get_size();
10259
+ this._hash = get_txhash(this._tx);
10260
+ this._txid = get_txid(this._tx);
10261
+ this._value = get_tx_value(this._tx);
10262
+ }
10263
+ _update_vin() {
10264
+ this._vin = this._tx.vin.map(txin => new TransactionInput(txin));
10265
+ this._update_tx();
10266
+ }
10267
+ _update_vout() {
10268
+ this._vout = this._tx.vout.map(txout => new TransactionOutput(txout));
10269
+ this._update_tx();
10270
+ }
10201
10271
  toJSON() { return this.data; }
10202
10272
  toString() { return JSON.stringify(this.data); }
10203
10273
  };
@@ -14519,7 +14589,7 @@ function merkleize(taptree, target, path = []) {
14519
14589
  }
14520
14590
 
14521
14591
  function parse_taproot_witness(witness) {
14522
- const { cblock, params, script } = parse_witness_data(witness);
14592
+ const { cblock, params, script } = parse_witness(witness);
14523
14593
  Assert.exists(cblock, 'cblock is null');
14524
14594
  Assert.exists(script, 'script is null');
14525
14595
  const cblk = parse_cblock(cblock);