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