@vbyte/btc-dev 1.0.10 → 1.0.11

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
@@ -9877,36 +9877,6 @@ var index$6 = /*#__PURE__*/Object.freeze({
9877
9877
  encode_sequence: encode_sequence
9878
9878
  });
9879
9879
 
9880
- class TransactionOutput {
9881
- constructor(txout) {
9882
- assert_tx_output(txout);
9883
- this._txout = txout;
9884
- }
9885
- get data() {
9886
- return this._txout;
9887
- }
9888
- get script_pk() {
9889
- return {
9890
- hex: this._txout.script_pk,
9891
- asm: decode_script(this._txout.script_pk)
9892
- };
9893
- }
9894
- get size() {
9895
- return get_txout_size(this._txout);
9896
- }
9897
- get type() {
9898
- return get_vout_type(this._txout.script_pk);
9899
- }
9900
- get value() {
9901
- return this._txout.value;
9902
- }
9903
- get version() {
9904
- return get_vout_version(this._txout.script_pk);
9905
- }
9906
- toJSON() { return this.data; }
9907
- toString() { return JSON.stringify(this.data); }
9908
- }
9909
-
9910
9880
  function parse_witness(witness) {
9911
9881
  const elems = witness.map(e => Buff.bytes(e));
9912
9882
  const stack = witness.map(e => Buff.bytes(e).hex);
@@ -10011,140 +9981,151 @@ var index$5 = /*#__PURE__*/Object.freeze({
10011
9981
  });
10012
9982
 
10013
9983
  class TransactionWitness {
10014
- constructor(witness) {
10015
- assert_witness(witness);
10016
- this._elems = witness.map(e => Buff.bytes(e));
10017
- this._data = parse_witness(this._elems);
10018
- this._size = get_witness_size(this._elems);
9984
+ constructor(transaction, index) {
9985
+ this._tx = transaction;
9986
+ this._index = index;
10019
9987
  }
10020
9988
  get annex() {
10021
- return this._data.annex;
9989
+ return this.data.annex;
10022
9990
  }
10023
9991
  get cblock() {
10024
- return this._data.cblock;
9992
+ return this.data.cblock;
10025
9993
  }
10026
9994
  get data() {
10027
- return this._data;
9995
+ return parse_witness(this.stack);
10028
9996
  }
10029
9997
  get params() {
10030
- return this._data.params;
9998
+ return this.data.params;
10031
9999
  }
10032
10000
  get script() {
10033
- if (this._data.script === null)
10001
+ if (this.data.script === null)
10034
10002
  return null;
10035
10003
  return {
10036
- hex: this._data.script,
10037
- asm: decode_script(this._data.script)
10004
+ hex: this.data.script,
10005
+ asm: decode_script(this.data.script)
10038
10006
  };
10039
10007
  }
10040
10008
  get size() {
10041
- return this._size;
10009
+ return get_witness_size(this.stack);
10042
10010
  }
10043
10011
  get stack() {
10044
- return this._elems.map(e => e.hex);
10012
+ const txin = this._tx.data.vin.at(this._index);
10013
+ Assert.exists(txin, 'txin not found at index ' + this._index);
10014
+ Assert.exists(txin.witness, 'witness not found at index ' + this._index);
10015
+ return txin.witness;
10045
10016
  }
10046
10017
  get type() {
10047
- return this._data.type;
10018
+ return this.data.type;
10048
10019
  }
10049
10020
  get version() {
10050
- return this._data.version;
10051
- }
10052
- _update() {
10053
- this._data = parse_witness(this._elems);
10054
- this._size = get_witness_size(this._elems);
10055
- }
10056
- add(elem) {
10057
- this._elems.push(Buff.bytes(elem));
10058
- this._update();
10059
- }
10060
- insert(index, elem) {
10061
- Assert.ok(index >= 0 && index <= this._elems.length, 'index out of bounds');
10062
- if (index === this._elems.length) {
10063
- this._elems.push(Buff.bytes(elem));
10064
- }
10065
- else {
10066
- this._elems.splice(index, 0, Buff.bytes(elem));
10067
- }
10068
- this._update();
10069
- }
10070
- remove(index) {
10071
- this._elems.splice(index, 1);
10072
- this._update();
10021
+ return this.data.version;
10073
10022
  }
10074
10023
  toJSON() { return this.data; }
10075
10024
  toString() { return JSON.stringify(this.data); }
10076
10025
  }
10077
10026
 
10078
10027
  class TransactionInput {
10079
- constructor(txin) {
10080
- assert_tx_input(txin);
10081
- this._txin = txin;
10028
+ constructor(transaction, index) {
10029
+ this._tx = transaction;
10030
+ this._index = index;
10082
10031
  }
10083
10032
  get coinbase() {
10084
- return this._txin.coinbase;
10033
+ return this.data.coinbase;
10085
10034
  }
10086
10035
  get data() {
10087
- return this._txin;
10036
+ const txin = this._tx.data.vin.at(this.index);
10037
+ Assert.exists(txin, 'txin not found');
10038
+ return txin;
10088
10039
  }
10089
10040
  get has_prevout() {
10090
- return this._txin.prevout !== null;
10041
+ return this.data.prevout !== null;
10042
+ }
10043
+ get index() {
10044
+ return this._index;
10091
10045
  }
10092
10046
  get is_coinbase() {
10093
- return this._txin.coinbase !== null;
10047
+ return this.data.coinbase !== null;
10094
10048
  }
10095
10049
  get prevout() {
10096
- return this._txin.prevout
10097
- ? new TransactionOutput(this._txin.prevout)
10098
- : null;
10050
+ return this.data.prevout;
10099
10051
  }
10100
10052
  get script_sig() {
10101
- if (this._txin.script_sig === null)
10053
+ if (this.data.script_sig === null)
10102
10054
  return null;
10103
10055
  return {
10104
- asm: decode_script(this._txin.script_sig),
10105
- hex: this._txin.script_sig
10056
+ asm: decode_script(this.data.script_sig),
10057
+ hex: this.data.script_sig
10106
10058
  };
10107
10059
  }
10108
10060
  get sequence() {
10109
10061
  return {
10110
- hex: encode_txin_sequence(this._txin.sequence).hex,
10111
- data: SequenceUtil.decode(this._txin.sequence),
10112
- value: this._txin.sequence
10062
+ hex: encode_txin_sequence(this.data.sequence).hex,
10063
+ data: SequenceUtil.decode(this.data.sequence),
10064
+ value: this.data.sequence
10113
10065
  };
10114
10066
  }
10115
10067
  get size() {
10116
- return get_txin_size(this._txin);
10068
+ return get_txin_size(this.data);
10117
10069
  }
10118
10070
  get txid() {
10119
- return this._txin.txid;
10071
+ return this.data.txid;
10120
10072
  }
10121
10073
  get vout() {
10122
- return this._txin.vout;
10074
+ return this.data.vout;
10123
10075
  }
10124
10076
  get witness() {
10125
- return this._txin.witness.length > 0
10126
- ? new TransactionWitness(this._txin.witness)
10077
+ return this.data.witness.length > 0
10078
+ ? new TransactionWitness(this._tx, this.index)
10127
10079
  : null;
10128
10080
  }
10129
10081
  toJSON() { return this.data; }
10130
10082
  toString() { return JSON.stringify(this.data); }
10131
10083
  }
10132
10084
 
10085
+ class TransactionOutput {
10086
+ constructor(transaction, index) {
10087
+ this._tx = transaction;
10088
+ this._index = index;
10089
+ }
10090
+ get data() {
10091
+ const txout = this._tx.data.vout.at(this.index);
10092
+ Assert.exists(txout, 'txout not found');
10093
+ return txout;
10094
+ }
10095
+ get index() {
10096
+ return this._index;
10097
+ }
10098
+ get script_pk() {
10099
+ return {
10100
+ hex: this.data.script_pk,
10101
+ asm: decode_script(this.data.script_pk)
10102
+ };
10103
+ }
10104
+ get size() {
10105
+ return get_txout_size(this.data);
10106
+ }
10107
+ get type() {
10108
+ return get_vout_type(this.data.script_pk);
10109
+ }
10110
+ get value() {
10111
+ return this.data.value;
10112
+ }
10113
+ get version() {
10114
+ return get_vout_version(this.data.script_pk);
10115
+ }
10116
+ toJSON() { return this.data; }
10117
+ toString() { return JSON.stringify(this.data); }
10118
+ }
10119
+
10133
10120
  let Transaction$1 = class Transaction {
10134
10121
  constructor(txdata = {}) {
10135
10122
  this._tx = parse_tx(txdata);
10136
- this._vin = this._tx.vin.map(txin => new TransactionInput(txin));
10137
- this._vout = this._tx.vout.map(txout => new TransactionOutput(txout));
10138
- this._size = this._get_size();
10139
- this._hash = get_txhash(this._tx);
10140
- this._txid = get_txid(this._tx);
10141
- this._value = get_tx_value(this._tx);
10142
10123
  }
10143
10124
  get data() {
10144
10125
  return this._tx;
10145
10126
  }
10146
10127
  get hash() {
10147
- return this._hash;
10128
+ return get_txhash(this._tx);
10148
10129
  }
10149
10130
  get locktime() {
10150
10131
  return {
@@ -10157,37 +10138,35 @@ let Transaction$1 = class Transaction {
10157
10138
  return this._tx.vout.find(txout => is_return_script(txout.script_pk)) || null;
10158
10139
  }
10159
10140
  get size() {
10160
- return this._size;
10141
+ return get_txsize(this._tx);
10161
10142
  }
10162
10143
  get spends() {
10163
10144
  return this._tx.vin
10164
10145
  .filter(txin => txin.prevout !== null)
10165
- .map(txin => new TransactionOutput(txin.prevout));
10146
+ .map(txin => txin.prevout);
10166
10147
  }
10167
10148
  get txid() {
10168
- return this._txid;
10149
+ return get_txid(this._tx);
10169
10150
  }
10170
10151
  get value() {
10171
- return this._value;
10152
+ return get_tx_value(this._tx);
10172
10153
  }
10173
10154
  get version() {
10174
10155
  return this._tx.version;
10175
10156
  }
10176
10157
  get vin() {
10177
- return this._vin;
10158
+ return this._tx.vin.map((_, idx) => new TransactionInput(this, idx));
10178
10159
  }
10179
10160
  get vout() {
10180
- return this._vout;
10161
+ return this._tx.vout.map((_, idx) => new TransactionOutput(this, idx));
10181
10162
  }
10182
10163
  add_vin(tx_input) {
10183
10164
  const txin = create_tx_input(tx_input);
10184
10165
  this._tx.vin.push(txin);
10185
- this._update_vin();
10186
10166
  }
10187
10167
  add_vout(tx_output) {
10188
10168
  const txout = create_tx_output(tx_output);
10189
10169
  this._tx.vout.push(txout);
10190
- this._update_vout();
10191
10170
  }
10192
10171
  insert_vin(index, tx_input) {
10193
10172
  Assert.ok(index >= 0 && index <= this._tx.vin.length, 'input goes out of bounds');
@@ -10198,7 +10177,6 @@ let Transaction$1 = class Transaction {
10198
10177
  else {
10199
10178
  this._tx.vin.splice(index, 0, txin);
10200
10179
  }
10201
- this._update_vin();
10202
10180
  }
10203
10181
  insert_vout(index, tx_output) {
10204
10182
  Assert.ok(index >= 0 && index <= this._tx.vout.length, 'output goes out of bounds');
@@ -10209,17 +10187,14 @@ let Transaction$1 = class Transaction {
10209
10187
  else {
10210
10188
  this._tx.vout.splice(index, 0, txout);
10211
10189
  }
10212
- this._update_vout();
10213
10190
  }
10214
10191
  remove_vin(index) {
10215
10192
  Assert.ok(this._tx.vin.at(index) !== undefined, 'input does not exist at index');
10216
10193
  this._tx.vin.splice(index, 1);
10217
- this._update_vin();
10218
10194
  }
10219
10195
  remove_vout(index) {
10220
10196
  Assert.ok(this._tx.vout.at(index) !== undefined, 'output does not exist at index');
10221
10197
  this._tx.vout.splice(index, 1);
10222
- this._update_vout();
10223
10198
  }
10224
10199
  _get_size() {
10225
10200
  return {
@@ -10230,20 +10205,6 @@ let Transaction$1 = class Transaction {
10230
10205
  witness: this.vin.reduce((acc, txin) => acc + (txin.witness?.size.total ?? 0), 0)
10231
10206
  };
10232
10207
  }
10233
- _update_tx() {
10234
- this._size = this._get_size();
10235
- this._hash = get_txhash(this._tx);
10236
- this._txid = get_txid(this._tx);
10237
- this._value = get_tx_value(this._tx);
10238
- }
10239
- _update_vin() {
10240
- this._vin = this._tx.vin.map(txin => new TransactionInput(txin));
10241
- this._update_tx();
10242
- }
10243
- _update_vout() {
10244
- this._vout = this._tx.vout.map(txout => new TransactionOutput(txout));
10245
- this._update_tx();
10246
- }
10247
10208
  toJSON() { return this.data; }
10248
10209
  toString() { return JSON.stringify(this.data); }
10249
10210
  };