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