@vbyte/btc-dev 1.0.9 → 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
@@ -8413,7 +8413,7 @@ const DEFAULT_CONFIG = {
8413
8413
  };
8414
8414
  function decode_tx(txbytes, options = {}) {
8415
8415
  const config = { ...DEFAULT_CONFIG, ...options };
8416
- Assert.is_bytes(txbytes, 'txbytes must be hex or a unit array');
8416
+ Assert.is_bytes(txbytes, 'transaction must be hex or a byte-array');
8417
8417
  const stream = new Stream(txbytes);
8418
8418
  const version = read_version(stream);
8419
8419
  const has_witness = (config.segwit)
@@ -9152,7 +9152,7 @@ const WIT_FLAG_BYTES = 2;
9152
9152
  function get_vsize$1(bytes) {
9153
9153
  const weight = Buff.bytes(bytes).length;
9154
9154
  const remain = (weight % 4 > 0) ? 1 : 0;
9155
- return Math.floor(weight / 4) + remain;
9155
+ return Math.ceil(weight / 4) + remain;
9156
9156
  }
9157
9157
  function get_txsize(txdata) {
9158
9158
  const json = parse_tx(txdata);
@@ -9160,7 +9160,7 @@ function get_txsize(txdata) {
9160
9160
  const total = encode_tx(json, true).length;
9161
9161
  const weight = base * 3 + total;
9162
9162
  const remain = (weight % 4 > 0) ? 1 : 0;
9163
- const vsize = Math.floor(weight / 4) + remain;
9163
+ const vsize = Math.ceil(weight / 4) + remain;
9164
9164
  return { base, total, vsize, weight };
9165
9165
  }
9166
9166
  function get_vin_size(vin) {
@@ -9471,6 +9471,8 @@ function sign_taproot_tx(seckey, txdata, options) {
9471
9471
 
9472
9472
  class TxSigner {
9473
9473
  constructor(seckey) {
9474
+ Assert.ok(Buff.is_bytes(seckey), 'seckey must be a string or bytes');
9475
+ Assert.size(seckey, 32, 'seckey must be 32 bytes');
9474
9476
  this._seckey = Buff.bytes(seckey).hex;
9475
9477
  }
9476
9478
  get pubkey() {
@@ -9875,35 +9877,6 @@ var index$6 = /*#__PURE__*/Object.freeze({
9875
9877
  encode_sequence: encode_sequence
9876
9878
  });
9877
9879
 
9878
- class TransactionOutput {
9879
- constructor(txout) {
9880
- this._txout = txout;
9881
- }
9882
- get data() {
9883
- return this._txout;
9884
- }
9885
- get script_pk() {
9886
- return {
9887
- hex: this._txout.script_pk,
9888
- asm: decode_script(this._txout.script_pk)
9889
- };
9890
- }
9891
- get size() {
9892
- return get_txout_size(this._txout);
9893
- }
9894
- get type() {
9895
- return get_vout_type(this._txout.script_pk);
9896
- }
9897
- get value() {
9898
- return this._txout.value;
9899
- }
9900
- get version() {
9901
- return get_vout_version(this._txout.script_pk);
9902
- }
9903
- toJSON() { return this.data; }
9904
- toString() { return JSON.stringify(this.data); }
9905
- }
9906
-
9907
9880
  function parse_witness(witness) {
9908
9881
  const elems = witness.map(e => Buff.bytes(e));
9909
9882
  const stack = witness.map(e => Buff.bytes(e).hex);
@@ -9995,148 +9968,164 @@ function get_witness_size(witness) {
9995
9968
  const vsize = Math.ceil(WIT_LENGTH_BYTE + size / 4);
9996
9969
  return { total: size, vsize };
9997
9970
  }
9971
+ function assert_witness(witness) {
9972
+ Assert.ok(Array.isArray(witness), 'witness must be an array');
9973
+ Assert.ok(witness.every(e => Buff.is_bytes(e)), 'witness must be an array of strings or bytes');
9974
+ }
9998
9975
 
9999
9976
  var index$5 = /*#__PURE__*/Object.freeze({
10000
9977
  __proto__: null,
9978
+ assert_witness: assert_witness,
10001
9979
  get_witness_size: get_witness_size,
10002
9980
  parse_witness: parse_witness
10003
9981
  });
10004
9982
 
10005
9983
  class TransactionWitness {
10006
- constructor(witness) {
10007
- this._elems = witness.map(e => Buff.bytes(e));
10008
- this._data = parse_witness(this._elems);
10009
- this._size = get_witness_size(this._elems);
9984
+ constructor(transaction, index) {
9985
+ this._tx = transaction;
9986
+ this._index = index;
10010
9987
  }
10011
9988
  get annex() {
10012
- return this._data.annex;
9989
+ return this.data.annex;
10013
9990
  }
10014
9991
  get cblock() {
10015
- return this._data.cblock;
9992
+ return this.data.cblock;
10016
9993
  }
10017
9994
  get data() {
10018
- return this._data;
9995
+ return parse_witness(this.stack);
10019
9996
  }
10020
9997
  get params() {
10021
- return this._data.params;
9998
+ return this.data.params;
10022
9999
  }
10023
10000
  get script() {
10024
- if (this._data.script === null)
10001
+ if (this.data.script === null)
10025
10002
  return null;
10026
10003
  return {
10027
- hex: this._data.script,
10028
- asm: decode_script(this._data.script)
10004
+ hex: this.data.script,
10005
+ asm: decode_script(this.data.script)
10029
10006
  };
10030
10007
  }
10031
10008
  get size() {
10032
- return this._size;
10009
+ return get_witness_size(this.stack);
10033
10010
  }
10034
10011
  get stack() {
10035
- 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;
10036
10016
  }
10037
10017
  get type() {
10038
- return this._data.type;
10018
+ return this.data.type;
10039
10019
  }
10040
10020
  get version() {
10041
- return this._data.version;
10042
- }
10043
- _update() {
10044
- this._data = parse_witness(this._elems);
10045
- this._size = get_witness_size(this._elems);
10046
- }
10047
- add(elem) {
10048
- this._elems.push(Buff.bytes(elem));
10049
- this._update();
10050
- }
10051
- insert(index, elem) {
10052
- Assert.ok(index >= 0 && index <= this._elems.length, 'index out of bounds');
10053
- if (index === this._elems.length) {
10054
- this._elems.push(Buff.bytes(elem));
10055
- }
10056
- else {
10057
- this._elems.splice(index, 0, Buff.bytes(elem));
10058
- }
10059
- this._update();
10060
- }
10061
- remove(index) {
10062
- this._elems.splice(index, 1);
10063
- this._update();
10021
+ return this.data.version;
10064
10022
  }
10065
10023
  toJSON() { return this.data; }
10066
10024
  toString() { return JSON.stringify(this.data); }
10067
10025
  }
10068
10026
 
10069
10027
  class TransactionInput {
10070
- constructor(txin) {
10071
- this._txin = txin;
10028
+ constructor(transaction, index) {
10029
+ this._tx = transaction;
10030
+ this._index = index;
10072
10031
  }
10073
10032
  get coinbase() {
10074
- return this._txin.coinbase;
10033
+ return this.data.coinbase;
10075
10034
  }
10076
10035
  get data() {
10077
- return this._txin;
10036
+ const txin = this._tx.data.vin.at(this.index);
10037
+ Assert.exists(txin, 'txin not found');
10038
+ return txin;
10078
10039
  }
10079
10040
  get has_prevout() {
10080
- return this._txin.prevout !== null;
10041
+ return this.data.prevout !== null;
10042
+ }
10043
+ get index() {
10044
+ return this._index;
10081
10045
  }
10082
10046
  get is_coinbase() {
10083
- return this._txin.coinbase !== null;
10047
+ return this.data.coinbase !== null;
10084
10048
  }
10085
10049
  get prevout() {
10086
- return this._txin.prevout
10087
- ? new TransactionOutput(this._txin.prevout)
10088
- : null;
10050
+ return this.data.prevout;
10089
10051
  }
10090
10052
  get script_sig() {
10091
- if (this._txin.script_sig === null)
10053
+ if (this.data.script_sig === null)
10092
10054
  return null;
10093
10055
  return {
10094
- asm: decode_script(this._txin.script_sig),
10095
- hex: this._txin.script_sig
10056
+ asm: decode_script(this.data.script_sig),
10057
+ hex: this.data.script_sig
10096
10058
  };
10097
10059
  }
10098
10060
  get sequence() {
10099
10061
  return {
10100
- hex: encode_txin_sequence(this._txin.sequence).hex,
10101
- data: SequenceUtil.decode(this._txin.sequence),
10102
- 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
10103
10065
  };
10104
10066
  }
10105
10067
  get size() {
10106
- return get_txin_size(this._txin);
10068
+ return get_txin_size(this.data);
10107
10069
  }
10108
10070
  get txid() {
10109
- return this._txin.txid;
10071
+ return this.data.txid;
10110
10072
  }
10111
10073
  get vout() {
10112
- return this._txin.vout;
10074
+ return this.data.vout;
10113
10075
  }
10114
10076
  get witness() {
10115
- return this._txin.witness.length > 0
10116
- ? new TransactionWitness(this._txin.witness)
10077
+ return this.data.witness.length > 0
10078
+ ? new TransactionWitness(this._tx, this.index)
10117
10079
  : null;
10118
10080
  }
10119
10081
  toJSON() { return this.data; }
10120
10082
  toString() { return JSON.stringify(this.data); }
10121
10083
  }
10122
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
+
10123
10120
  let Transaction$1 = class Transaction {
10124
10121
  constructor(txdata = {}) {
10125
- this._tx = (typeof txdata !== 'string')
10126
- ? parse_tx(txdata)
10127
- : decode_tx(txdata);
10128
- this._size = this._get_size();
10129
- this._hash = get_txhash(this._tx);
10130
- this._txid = get_txid(this._tx);
10131
- this._value = get_tx_value(this._tx);
10132
- this._vin = this._tx.vin.map(txin => new TransactionInput(txin));
10133
- this._vout = this._tx.vout.map(txout => new TransactionOutput(txout));
10122
+ this._tx = parse_tx(txdata);
10134
10123
  }
10135
10124
  get data() {
10136
10125
  return this._tx;
10137
10126
  }
10138
10127
  get hash() {
10139
- return this._hash;
10128
+ return get_txhash(this._tx);
10140
10129
  }
10141
10130
  get locktime() {
10142
10131
  return {
@@ -10149,37 +10138,35 @@ let Transaction$1 = class Transaction {
10149
10138
  return this._tx.vout.find(txout => is_return_script(txout.script_pk)) || null;
10150
10139
  }
10151
10140
  get size() {
10152
- return this._size;
10141
+ return get_txsize(this._tx);
10153
10142
  }
10154
10143
  get spends() {
10155
10144
  return this._tx.vin
10156
10145
  .filter(txin => txin.prevout !== null)
10157
- .map(txin => new TransactionOutput(txin.prevout));
10146
+ .map(txin => txin.prevout);
10158
10147
  }
10159
10148
  get txid() {
10160
- return this._txid;
10149
+ return get_txid(this._tx);
10161
10150
  }
10162
10151
  get value() {
10163
- return this._value;
10152
+ return get_tx_value(this._tx);
10164
10153
  }
10165
10154
  get version() {
10166
10155
  return this._tx.version;
10167
10156
  }
10168
10157
  get vin() {
10169
- return this._vin;
10158
+ return this._tx.vin.map((_, idx) => new TransactionInput(this, idx));
10170
10159
  }
10171
10160
  get vout() {
10172
- return this._vout;
10161
+ return this._tx.vout.map((_, idx) => new TransactionOutput(this, idx));
10173
10162
  }
10174
10163
  add_vin(tx_input) {
10175
10164
  const txin = create_tx_input(tx_input);
10176
10165
  this._tx.vin.push(txin);
10177
- this._update_vin();
10178
10166
  }
10179
10167
  add_vout(tx_output) {
10180
10168
  const txout = create_tx_output(tx_output);
10181
10169
  this._tx.vout.push(txout);
10182
- this._update_vout();
10183
10170
  }
10184
10171
  insert_vin(index, tx_input) {
10185
10172
  Assert.ok(index >= 0 && index <= this._tx.vin.length, 'input goes out of bounds');
@@ -10190,7 +10177,6 @@ let Transaction$1 = class Transaction {
10190
10177
  else {
10191
10178
  this._tx.vin.splice(index, 0, txin);
10192
10179
  }
10193
- this._update_vin();
10194
10180
  }
10195
10181
  insert_vout(index, tx_output) {
10196
10182
  Assert.ok(index >= 0 && index <= this._tx.vout.length, 'output goes out of bounds');
@@ -10201,17 +10187,14 @@ let Transaction$1 = class Transaction {
10201
10187
  else {
10202
10188
  this._tx.vout.splice(index, 0, txout);
10203
10189
  }
10204
- this._update_vout();
10205
10190
  }
10206
10191
  remove_vin(index) {
10207
10192
  Assert.ok(this._tx.vin.at(index) !== undefined, 'input does not exist at index');
10208
10193
  this._tx.vin.splice(index, 1);
10209
- this._update_vin();
10210
10194
  }
10211
10195
  remove_vout(index) {
10212
10196
  Assert.ok(this._tx.vout.at(index) !== undefined, 'output does not exist at index');
10213
10197
  this._tx.vout.splice(index, 1);
10214
- this._update_vout();
10215
10198
  }
10216
10199
  _get_size() {
10217
10200
  return {
@@ -10222,20 +10205,6 @@ let Transaction$1 = class Transaction {
10222
10205
  witness: this.vin.reduce((acc, txin) => acc + (txin.witness?.size.total ?? 0), 0)
10223
10206
  };
10224
10207
  }
10225
- _update_tx() {
10226
- this._size = this._get_size();
10227
- this._hash = get_txhash(this._tx);
10228
- this._txid = get_txid(this._tx);
10229
- this._value = get_tx_value(this._tx);
10230
- }
10231
- _update_vin() {
10232
- this._vin = this._tx.vin.map(txin => new TransactionInput(txin));
10233
- this._update_tx();
10234
- }
10235
- _update_vout() {
10236
- this._vout = this._tx.vout.map(txout => new TransactionOutput(txout));
10237
- this._update_tx();
10238
- }
10239
10208
  toJSON() { return this.data; }
10240
10209
  toString() { return JSON.stringify(this.data); }
10241
10210
  };