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