@vbyte/btc-dev 1.0.9 → 1.0.10

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.
@@ -1,8 +1,10 @@
1
1
  import { Buff } from '@vbyte/buff';
2
- import { ECC } from '@vbyte/micro-lib';
2
+ import { Assert, ECC } from '@vbyte/micro-lib';
3
3
  import { sign_segwit_tx, sign_taproot_tx } from '../lib/signer/sign.js';
4
4
  export class TxSigner {
5
5
  constructor(seckey) {
6
+ Assert.ok(Buff.is_bytes(seckey), 'seckey must be a string or bytes');
7
+ Assert.size(seckey, 32, 'seckey must be 32 bytes');
6
8
  this._seckey = Buff.bytes(seckey).hex;
7
9
  }
8
10
  get pubkey() {
package/dist/class/tx.js CHANGED
@@ -2,18 +2,16 @@ import { Assert } from '@vbyte/micro-lib';
2
2
  import { LocktimeUtil } from '../lib/meta/index.js';
3
3
  import { TransactionInput } from './txin.js';
4
4
  import { TransactionOutput } from './txout.js';
5
- import { decode_tx, get_txid, is_return_script, parse_tx, get_txsize, get_tx_value, get_txhash, encode_tx_locktime, create_tx_input, create_tx_output, } from '../lib/tx/index.js';
5
+ import { get_txid, is_return_script, parse_tx, get_txsize, get_tx_value, get_txhash, encode_tx_locktime, create_tx_input, create_tx_output, } from '../lib/tx/index.js';
6
6
  export class Transaction {
7
7
  constructor(txdata = {}) {
8
- this._tx = (typeof txdata !== 'string')
9
- ? parse_tx(txdata)
10
- : decode_tx(txdata);
8
+ this._tx = parse_tx(txdata);
9
+ this._vin = this._tx.vin.map(txin => new TransactionInput(txin));
10
+ this._vout = this._tx.vout.map(txout => new TransactionOutput(txout));
11
11
  this._size = this._get_size();
12
12
  this._hash = get_txhash(this._tx);
13
13
  this._txid = get_txid(this._tx);
14
14
  this._value = get_tx_value(this._tx);
15
- this._vin = this._tx.vin.map(txin => new TransactionInput(txin));
16
- this._vout = this._tx.vout.map(txout => new TransactionOutput(txout));
17
15
  }
18
16
  get data() {
19
17
  return this._tx;
@@ -2,9 +2,10 @@ import { decode_script } from '../lib/script/index.js';
2
2
  import { SequenceUtil } from '../lib/meta/index.js';
3
3
  import { TransactionOutput } from './txout.js';
4
4
  import { TransactionWitness } from './witness.js';
5
- import { encode_txin_sequence, get_txin_size, } from '../lib/tx/index.js';
5
+ import { assert_tx_input, encode_txin_sequence, get_txin_size, } from '../lib/tx/index.js';
6
6
  export class TransactionInput {
7
7
  constructor(txin) {
8
+ assert_tx_input(txin);
8
9
  this._txin = txin;
9
10
  }
10
11
  get coinbase() {
@@ -1,7 +1,8 @@
1
1
  import { decode_script } from '../lib/script/index.js';
2
- import { get_txout_size, get_vout_type, get_vout_version } from '../lib/tx/index.js';
2
+ import { assert_tx_output, get_txout_size, get_vout_type, get_vout_version } from '../lib/tx/index.js';
3
3
  export class TransactionOutput {
4
4
  constructor(txout) {
5
+ assert_tx_output(txout);
5
6
  this._txout = txout;
6
7
  }
7
8
  get data() {
@@ -1,9 +1,10 @@
1
1
  import { Buff } from '@vbyte/buff';
2
2
  import { Assert } from '@vbyte/micro-lib';
3
3
  import { decode_script } from '../lib/script/index.js';
4
- import { parse_witness, get_witness_size, } from '../lib/witness/index.js';
4
+ import { parse_witness, get_witness_size, assert_witness, } from '../lib/witness/index.js';
5
5
  export class TransactionWitness {
6
6
  constructor(witness) {
7
+ assert_witness(witness);
7
8
  this._elems = witness.map(e => Buff.bytes(e));
8
9
  this._data = parse_witness(this._elems);
9
10
  this._size = get_witness_size(this._elems);
@@ -8,7 +8,7 @@ const DEFAULT_CONFIG = {
8
8
  };
9
9
  export function decode_tx(txbytes, options = {}) {
10
10
  const config = { ...DEFAULT_CONFIG, ...options };
11
- Assert.is_bytes(txbytes, 'txbytes must be hex or a unit array');
11
+ Assert.is_bytes(txbytes, 'transaction must be hex or a byte-array');
12
12
  const stream = new Stream(txbytes);
13
13
  const version = read_version(stream);
14
14
  const has_witness = (config.segwit)
@@ -5,7 +5,7 @@ const WIT_FLAG_BYTES = 2;
5
5
  export function get_vsize(bytes) {
6
6
  const weight = Buff.bytes(bytes).length;
7
7
  const remain = (weight % 4 > 0) ? 1 : 0;
8
- return Math.floor(weight / 4) + remain;
8
+ return Math.ceil(weight / 4) + remain;
9
9
  }
10
10
  export function get_txsize(txdata) {
11
11
  const json = parse_tx(txdata);
@@ -13,7 +13,7 @@ export function get_txsize(txdata) {
13
13
  const total = encode_tx(json, true).length;
14
14
  const weight = base * 3 + total;
15
15
  const remain = (weight % 4 > 0) ? 1 : 0;
16
- const vsize = Math.floor(weight / 4) + remain;
16
+ const vsize = Math.ceil(weight / 4) + remain;
17
17
  return { base, total, vsize, weight };
18
18
  }
19
19
  export function get_vin_size(vin) {
@@ -1,3 +1,4 @@
1
1
  import { Bytes } from '@vbyte/buff';
2
2
  import type { WitnessSize } from '../../types/index.js';
3
3
  export declare function get_witness_size(witness: Bytes[]): WitnessSize;
4
+ export declare function assert_witness(witness: unknown): asserts witness is Bytes[];
@@ -1,4 +1,5 @@
1
1
  import { Buff } from '@vbyte/buff';
2
+ import { Assert } from '@vbyte/micro-lib';
2
3
  const WIT_LENGTH_BYTE = 1;
3
4
  export function get_witness_size(witness) {
4
5
  const stack = witness.map(e => Buff.bytes(e));
@@ -6,3 +7,7 @@ export function get_witness_size(witness) {
6
7
  const vsize = Math.ceil(WIT_LENGTH_BYTE + size / 4);
7
8
  return { total: size, vsize };
8
9
  }
10
+ export function assert_witness(witness) {
11
+ Assert.ok(Array.isArray(witness), 'witness must be an array');
12
+ Assert.ok(witness.every(e => Buff.is_bytes(e)), 'witness must be an array of strings or bytes');
13
+ }
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() {
@@ -9879,6 +9881,7 @@ var index$6 = /*#__PURE__*/Object.freeze({
9879
9881
 
9880
9882
  class TransactionOutput {
9881
9883
  constructor(txout) {
9884
+ assert_tx_output(txout);
9882
9885
  this._txout = txout;
9883
9886
  }
9884
9887
  get data() {
@@ -9997,15 +10000,21 @@ function get_witness_size(witness) {
9997
10000
  const vsize = Math.ceil(WIT_LENGTH_BYTE + size / 4);
9998
10001
  return { total: size, vsize };
9999
10002
  }
10003
+ function assert_witness(witness) {
10004
+ Assert.ok(Array.isArray(witness), 'witness must be an array');
10005
+ Assert.ok(witness.every(e => Buff.is_bytes(e)), 'witness must be an array of strings or bytes');
10006
+ }
10000
10007
 
10001
10008
  var index$5 = /*#__PURE__*/Object.freeze({
10002
10009
  __proto__: null,
10010
+ assert_witness: assert_witness,
10003
10011
  get_witness_size: get_witness_size,
10004
10012
  parse_witness: parse_witness
10005
10013
  });
10006
10014
 
10007
10015
  class TransactionWitness {
10008
10016
  constructor(witness) {
10017
+ assert_witness(witness);
10009
10018
  this._elems = witness.map(e => Buff.bytes(e));
10010
10019
  this._data = parse_witness(this._elems);
10011
10020
  this._size = get_witness_size(this._elems);
@@ -10070,6 +10079,7 @@ class TransactionWitness {
10070
10079
 
10071
10080
  class TransactionInput {
10072
10081
  constructor(txin) {
10082
+ assert_tx_input(txin);
10073
10083
  this._txin = txin;
10074
10084
  }
10075
10085
  get coinbase() {
@@ -10124,15 +10134,13 @@ class TransactionInput {
10124
10134
 
10125
10135
  let Transaction$1 = class Transaction {
10126
10136
  constructor(txdata = {}) {
10127
- this._tx = (typeof txdata !== 'string')
10128
- ? parse_tx(txdata)
10129
- : decode_tx(txdata);
10137
+ 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));
10130
10140
  this._size = this._get_size();
10131
10141
  this._hash = get_txhash(this._tx);
10132
10142
  this._txid = get_txid(this._tx);
10133
10143
  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));
10136
10144
  }
10137
10145
  get data() {
10138
10146
  return this._tx;