@vbyte/btc-dev 1.0.8 → 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() {
@@ -1,6 +1,6 @@
1
1
  import { TransactionInput } from './txin.js';
2
2
  import { TransactionOutput } from './txout.js';
3
- import type { TxData, TxTemplate, TransactionData, TxOutput, TxSize, TxValue, TxInputTemplate } from '../types/index.js';
3
+ import type { TxData, TxTemplate, TxOutput, TxSize, TxValue, TxInputTemplate } from '../types/index.js';
4
4
  export declare class Transaction {
5
5
  private readonly _tx;
6
6
  private _size;
@@ -10,7 +10,7 @@ export declare class Transaction {
10
10
  private _vin;
11
11
  private _vout;
12
12
  constructor(txdata?: string | TxData | TxTemplate);
13
- get data(): TransactionData;
13
+ get data(): TxData;
14
14
  get hash(): string;
15
15
  get locktime(): {
16
16
  hex: string;
@@ -40,12 +40,12 @@ export declare class Transaction {
40
40
  witness: number;
41
41
  base: number;
42
42
  total: number;
43
- weight: number;
44
43
  vsize: number;
44
+ weight: number;
45
45
  };
46
46
  _update_tx(): void;
47
47
  _update_vin(): void;
48
48
  _update_vout(): void;
49
- toJSON(): TransactionData;
49
+ toJSON(): TxData;
50
50
  toString(): string;
51
51
  }
package/dist/class/tx.js CHANGED
@@ -2,32 +2,19 @@ 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
- return {
20
- hash: this.hash,
21
- locktime: this.locktime,
22
- return: this.return,
23
- size: this.size,
24
- spends: this.spends,
25
- txid: this.txid,
26
- value: this.value,
27
- version: this.version,
28
- vin: this.vin.map(txin => txin.data),
29
- vout: this.vout.map(txout => txout.data)
30
- };
17
+ return this._tx;
31
18
  }
32
19
  get hash() {
33
20
  return this._hash;
@@ -1,11 +1,11 @@
1
1
  import { TransactionOutput } from './txout.js';
2
2
  import { TransactionWitness } from './witness.js';
3
- import type { TxInput, TransactionInputData } from '../types/index.js';
3
+ import type { TxInput } from '../types/index.js';
4
4
  export declare class TransactionInput {
5
5
  private readonly _txin;
6
6
  constructor(txin: TxInput);
7
7
  get coinbase(): string | null;
8
- get data(): TransactionInputData;
8
+ get data(): TxInput;
9
9
  get has_prevout(): boolean;
10
10
  get is_coinbase(): boolean;
11
11
  get prevout(): TransactionOutput | null;
@@ -22,6 +22,6 @@ export declare class TransactionInput {
22
22
  get txid(): string;
23
23
  get vout(): number;
24
24
  get witness(): TransactionWitness | null;
25
- toJSON(): TransactionInputData;
25
+ toJSON(): TxInput;
26
26
  toString(): string;
27
27
  }
@@ -2,25 +2,17 @@ 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() {
11
12
  return this._txin.coinbase;
12
13
  }
13
14
  get data() {
14
- return {
15
- coinbase: this.coinbase,
16
- prevout: this.prevout?.data ?? null,
17
- script_sig: this.script_sig,
18
- sequence: this.sequence,
19
- size: this.size,
20
- txid: this.txid,
21
- vout: this.vout,
22
- witness: this.witness?.data ?? null
23
- };
15
+ return this._txin;
24
16
  }
25
17
  get has_prevout() {
26
18
  return this._txin.prevout !== null;
@@ -1,8 +1,8 @@
1
- import type { TxOutput, TransactionOutputData } from '../types/index.js';
1
+ import type { TxOutput } from '../types/index.js';
2
2
  export declare class TransactionOutput {
3
3
  private readonly _txout;
4
4
  constructor(txout: TxOutput);
5
- get data(): TransactionOutputData;
5
+ get data(): TxOutput;
6
6
  get script_pk(): {
7
7
  hex: string;
8
8
  asm: string[];
@@ -11,6 +11,6 @@ export declare class TransactionOutput {
11
11
  get type(): import("../types/index.js").TxOutputType;
12
12
  get value(): bigint;
13
13
  get version(): import("../types/index.js").WitnessVersion;
14
- toJSON(): TransactionOutputData;
14
+ toJSON(): TxOutput;
15
15
  toString(): string;
16
16
  }
@@ -1,17 +1,12 @@
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() {
8
- return {
9
- script_pk: this.script_pk,
10
- size: this.size,
11
- type: this.type,
12
- value: this.value,
13
- version: this.version
14
- };
9
+ return this._txout;
15
10
  }
16
11
  get script_pk() {
17
12
  return {
@@ -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,16 +9881,11 @@ 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() {
9885
- return {
9886
- script_pk: this.script_pk,
9887
- size: this.size,
9888
- type: this.type,
9889
- value: this.value,
9890
- version: this.version
9891
- };
9888
+ return this._txout;
9892
9889
  }
9893
9890
  get script_pk() {
9894
9891
  return {
@@ -10003,15 +10000,21 @@ function get_witness_size(witness) {
10003
10000
  const vsize = Math.ceil(WIT_LENGTH_BYTE + size / 4);
10004
10001
  return { total: size, vsize };
10005
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
+ }
10006
10007
 
10007
10008
  var index$5 = /*#__PURE__*/Object.freeze({
10008
10009
  __proto__: null,
10010
+ assert_witness: assert_witness,
10009
10011
  get_witness_size: get_witness_size,
10010
10012
  parse_witness: parse_witness
10011
10013
  });
10012
10014
 
10013
10015
  class TransactionWitness {
10014
10016
  constructor(witness) {
10017
+ assert_witness(witness);
10015
10018
  this._elems = witness.map(e => Buff.bytes(e));
10016
10019
  this._data = parse_witness(this._elems);
10017
10020
  this._size = get_witness_size(this._elems);
@@ -10076,22 +10079,14 @@ class TransactionWitness {
10076
10079
 
10077
10080
  class TransactionInput {
10078
10081
  constructor(txin) {
10082
+ assert_tx_input(txin);
10079
10083
  this._txin = txin;
10080
10084
  }
10081
10085
  get coinbase() {
10082
10086
  return this._txin.coinbase;
10083
10087
  }
10084
10088
  get data() {
10085
- return {
10086
- coinbase: this.coinbase,
10087
- prevout: this.prevout?.data ?? null,
10088
- script_sig: this.script_sig,
10089
- sequence: this.sequence,
10090
- size: this.size,
10091
- txid: this.txid,
10092
- vout: this.vout,
10093
- witness: this.witness?.data ?? null
10094
- };
10089
+ return this._txin;
10095
10090
  }
10096
10091
  get has_prevout() {
10097
10092
  return this._txin.prevout !== null;
@@ -10139,29 +10134,16 @@ class TransactionInput {
10139
10134
 
10140
10135
  let Transaction$1 = class Transaction {
10141
10136
  constructor(txdata = {}) {
10142
- this._tx = (typeof txdata !== 'string')
10143
- ? parse_tx(txdata)
10144
- : 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));
10145
10140
  this._size = this._get_size();
10146
10141
  this._hash = get_txhash(this._tx);
10147
10142
  this._txid = get_txid(this._tx);
10148
10143
  this._value = get_tx_value(this._tx);
10149
- this._vin = this._tx.vin.map(txin => new TransactionInput(txin));
10150
- this._vout = this._tx.vout.map(txout => new TransactionOutput(txout));
10151
10144
  }
10152
10145
  get data() {
10153
- return {
10154
- hash: this.hash,
10155
- locktime: this.locktime,
10156
- return: this.return,
10157
- size: this.size,
10158
- spends: this.spends,
10159
- txid: this.txid,
10160
- value: this.value,
10161
- version: this.version,
10162
- vin: this.vin.map(txin => txin.data),
10163
- vout: this.vout.map(txout => txout.data)
10164
- };
10146
+ return this._tx;
10165
10147
  }
10166
10148
  get hash() {
10167
10149
  return this._hash;