@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.
@@ -1,14 +1,8 @@
1
1
  import { TransactionInput } from './txin.js';
2
2
  import { TransactionOutput } from './txout.js';
3
- import type { TxData, TxTemplate, TxOutput, TxSize, TxValue, TxInputTemplate } from '../types/index.js';
3
+ import type { TxData, TxTemplate, TxOutput, TxInputTemplate } from '../types/index.js';
4
4
  export declare class Transaction {
5
5
  private readonly _tx;
6
- private _size;
7
- private _hash;
8
- private _txid;
9
- private _value;
10
- private _vin;
11
- private _vout;
12
6
  constructor(txdata?: string | TxData | TxTemplate);
13
7
  get data(): TxData;
14
8
  get hash(): string;
@@ -18,12 +12,10 @@ export declare class Transaction {
18
12
  value: number;
19
13
  };
20
14
  get return(): TxOutput | null;
21
- get size(): TxSize & {
22
- segwit: number;
23
- };
24
- get spends(): TransactionOutput[];
15
+ get size(): import("../types/index.js").TxSize;
16
+ get spends(): TxOutput[];
25
17
  get txid(): string;
26
- get value(): TxValue;
18
+ get value(): import("../types/index.js").TxValue;
27
19
  get version(): number;
28
20
  get vin(): TransactionInput[];
29
21
  get vout(): TransactionOutput[];
@@ -43,9 +35,6 @@ export declare class Transaction {
43
35
  vsize: number;
44
36
  weight: number;
45
37
  };
46
- _update_tx(): void;
47
- _update_vin(): void;
48
- _update_vout(): void;
49
38
  toJSON(): TxData;
50
39
  toString(): string;
51
40
  }
package/dist/class/tx.js CHANGED
@@ -6,18 +6,12 @@ import { get_txid, is_return_script, parse_tx, get_txsize, get_tx_value, get_txh
6
6
  export class Transaction {
7
7
  constructor(txdata = {}) {
8
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
- this._size = this._get_size();
12
- this._hash = get_txhash(this._tx);
13
- this._txid = get_txid(this._tx);
14
- this._value = get_tx_value(this._tx);
15
9
  }
16
10
  get data() {
17
11
  return this._tx;
18
12
  }
19
13
  get hash() {
20
- return this._hash;
14
+ return get_txhash(this._tx);
21
15
  }
22
16
  get locktime() {
23
17
  return {
@@ -30,37 +24,35 @@ export class Transaction {
30
24
  return this._tx.vout.find(txout => is_return_script(txout.script_pk)) || null;
31
25
  }
32
26
  get size() {
33
- return this._size;
27
+ return get_txsize(this._tx);
34
28
  }
35
29
  get spends() {
36
30
  return this._tx.vin
37
31
  .filter(txin => txin.prevout !== null)
38
- .map(txin => new TransactionOutput(txin.prevout));
32
+ .map(txin => txin.prevout);
39
33
  }
40
34
  get txid() {
41
- return this._txid;
35
+ return get_txid(this._tx);
42
36
  }
43
37
  get value() {
44
- return this._value;
38
+ return get_tx_value(this._tx);
45
39
  }
46
40
  get version() {
47
41
  return this._tx.version;
48
42
  }
49
43
  get vin() {
50
- return this._vin;
44
+ return this._tx.vin.map((_, idx) => new TransactionInput(this, idx));
51
45
  }
52
46
  get vout() {
53
- return this._vout;
47
+ return this._tx.vout.map((_, idx) => new TransactionOutput(this, idx));
54
48
  }
55
49
  add_vin(tx_input) {
56
50
  const txin = create_tx_input(tx_input);
57
51
  this._tx.vin.push(txin);
58
- this._update_vin();
59
52
  }
60
53
  add_vout(tx_output) {
61
54
  const txout = create_tx_output(tx_output);
62
55
  this._tx.vout.push(txout);
63
- this._update_vout();
64
56
  }
65
57
  insert_vin(index, tx_input) {
66
58
  Assert.ok(index >= 0 && index <= this._tx.vin.length, 'input goes out of bounds');
@@ -71,7 +63,6 @@ export class Transaction {
71
63
  else {
72
64
  this._tx.vin.splice(index, 0, txin);
73
65
  }
74
- this._update_vin();
75
66
  }
76
67
  insert_vout(index, tx_output) {
77
68
  Assert.ok(index >= 0 && index <= this._tx.vout.length, 'output goes out of bounds');
@@ -82,17 +73,14 @@ export class Transaction {
82
73
  else {
83
74
  this._tx.vout.splice(index, 0, txout);
84
75
  }
85
- this._update_vout();
86
76
  }
87
77
  remove_vin(index) {
88
78
  Assert.ok(this._tx.vin.at(index) !== undefined, 'input does not exist at index');
89
79
  this._tx.vin.splice(index, 1);
90
- this._update_vin();
91
80
  }
92
81
  remove_vout(index) {
93
82
  Assert.ok(this._tx.vout.at(index) !== undefined, 'output does not exist at index');
94
83
  this._tx.vout.splice(index, 1);
95
- this._update_vout();
96
84
  }
97
85
  _get_size() {
98
86
  return {
@@ -103,20 +91,6 @@ export class Transaction {
103
91
  witness: this.vin.reduce((acc, txin) => acc + (txin.witness?.size.total ?? 0), 0)
104
92
  };
105
93
  }
106
- _update_tx() {
107
- this._size = this._get_size();
108
- this._hash = get_txhash(this._tx);
109
- this._txid = get_txid(this._tx);
110
- this._value = get_tx_value(this._tx);
111
- }
112
- _update_vin() {
113
- this._vin = this._tx.vin.map(txin => new TransactionInput(txin));
114
- this._update_tx();
115
- }
116
- _update_vout() {
117
- this._vout = this._tx.vout.map(txout => new TransactionOutput(txout));
118
- this._update_tx();
119
- }
120
94
  toJSON() { return this.data; }
121
95
  toString() { return JSON.stringify(this.data); }
122
96
  }
@@ -1,14 +1,16 @@
1
- import { TransactionOutput } from './txout.js';
1
+ import { Transaction } from './tx.js';
2
2
  import { TransactionWitness } from './witness.js';
3
- import type { TxInput } from '../types/index.js';
3
+ import type { TxInput, TxOutput } from '../types/index.js';
4
4
  export declare class TransactionInput {
5
- private readonly _txin;
6
- constructor(txin: TxInput);
5
+ private readonly _tx;
6
+ private readonly _index;
7
+ constructor(transaction: Transaction, index: number);
7
8
  get coinbase(): string | null;
8
9
  get data(): TxInput;
9
10
  get has_prevout(): boolean;
11
+ get index(): number;
10
12
  get is_coinbase(): boolean;
11
- get prevout(): TransactionOutput | null;
13
+ get prevout(): TxOutput | null;
12
14
  get script_sig(): {
13
15
  asm: string[];
14
16
  hex: string;
@@ -1,57 +1,60 @@
1
+ import { Assert } from '@vbyte/micro-lib';
1
2
  import { decode_script } from '../lib/script/index.js';
2
3
  import { SequenceUtil } from '../lib/meta/index.js';
3
- import { TransactionOutput } from './txout.js';
4
4
  import { TransactionWitness } from './witness.js';
5
- import { assert_tx_input, encode_txin_sequence, get_txin_size, } from '../lib/tx/index.js';
5
+ import { encode_txin_sequence, get_txin_size, } from '../lib/tx/index.js';
6
6
  export class TransactionInput {
7
- constructor(txin) {
8
- assert_tx_input(txin);
9
- this._txin = txin;
7
+ constructor(transaction, index) {
8
+ this._tx = transaction;
9
+ this._index = index;
10
10
  }
11
11
  get coinbase() {
12
- return this._txin.coinbase;
12
+ return this.data.coinbase;
13
13
  }
14
14
  get data() {
15
- return this._txin;
15
+ const txin = this._tx.data.vin.at(this.index);
16
+ Assert.exists(txin, 'txin not found');
17
+ return txin;
16
18
  }
17
19
  get has_prevout() {
18
- return this._txin.prevout !== null;
20
+ return this.data.prevout !== null;
21
+ }
22
+ get index() {
23
+ return this._index;
19
24
  }
20
25
  get is_coinbase() {
21
- return this._txin.coinbase !== null;
26
+ return this.data.coinbase !== null;
22
27
  }
23
28
  get prevout() {
24
- return this._txin.prevout
25
- ? new TransactionOutput(this._txin.prevout)
26
- : null;
29
+ return this.data.prevout;
27
30
  }
28
31
  get script_sig() {
29
- if (this._txin.script_sig === null)
32
+ if (this.data.script_sig === null)
30
33
  return null;
31
34
  return {
32
- asm: decode_script(this._txin.script_sig),
33
- hex: this._txin.script_sig
35
+ asm: decode_script(this.data.script_sig),
36
+ hex: this.data.script_sig
34
37
  };
35
38
  }
36
39
  get sequence() {
37
40
  return {
38
- hex: encode_txin_sequence(this._txin.sequence).hex,
39
- data: SequenceUtil.decode(this._txin.sequence),
40
- value: this._txin.sequence
41
+ hex: encode_txin_sequence(this.data.sequence).hex,
42
+ data: SequenceUtil.decode(this.data.sequence),
43
+ value: this.data.sequence
41
44
  };
42
45
  }
43
46
  get size() {
44
- return get_txin_size(this._txin);
47
+ return get_txin_size(this.data);
45
48
  }
46
49
  get txid() {
47
- return this._txin.txid;
50
+ return this.data.txid;
48
51
  }
49
52
  get vout() {
50
- return this._txin.vout;
53
+ return this.data.vout;
51
54
  }
52
55
  get witness() {
53
- return this._txin.witness.length > 0
54
- ? new TransactionWitness(this._txin.witness)
56
+ return this.data.witness.length > 0
57
+ ? new TransactionWitness(this._tx, this.index)
55
58
  : null;
56
59
  }
57
60
  toJSON() { return this.data; }
@@ -1,8 +1,11 @@
1
+ import { Transaction } from './tx.js';
1
2
  import type { TxOutput } from '../types/index.js';
2
3
  export declare class TransactionOutput {
3
- private readonly _txout;
4
- constructor(txout: TxOutput);
4
+ private readonly _tx;
5
+ private readonly _index;
6
+ constructor(transaction: Transaction, index: number);
5
7
  get data(): TxOutput;
8
+ get index(): number;
6
9
  get script_pk(): {
7
10
  hex: string;
8
11
  asm: string[];
@@ -1,30 +1,36 @@
1
+ import { Assert } from '@vbyte/micro-lib';
1
2
  import { decode_script } from '../lib/script/index.js';
2
- import { assert_tx_output, get_txout_size, get_vout_type, get_vout_version } from '../lib/tx/index.js';
3
+ import { get_txout_size, get_vout_type, get_vout_version } from '../lib/tx/index.js';
3
4
  export class TransactionOutput {
4
- constructor(txout) {
5
- assert_tx_output(txout);
6
- this._txout = txout;
5
+ constructor(transaction, index) {
6
+ this._tx = transaction;
7
+ this._index = index;
7
8
  }
8
9
  get data() {
9
- return this._txout;
10
+ const txout = this._tx.data.vout.at(this.index);
11
+ Assert.exists(txout, 'txout not found');
12
+ return txout;
13
+ }
14
+ get index() {
15
+ return this._index;
10
16
  }
11
17
  get script_pk() {
12
18
  return {
13
- hex: this._txout.script_pk,
14
- asm: decode_script(this._txout.script_pk)
19
+ hex: this.data.script_pk,
20
+ asm: decode_script(this.data.script_pk)
15
21
  };
16
22
  }
17
23
  get size() {
18
- return get_txout_size(this._txout);
24
+ return get_txout_size(this.data);
19
25
  }
20
26
  get type() {
21
- return get_vout_type(this._txout.script_pk);
27
+ return get_vout_type(this.data.script_pk);
22
28
  }
23
29
  get value() {
24
- return this._txout.value;
30
+ return this.data.value;
25
31
  }
26
32
  get version() {
27
- return get_vout_version(this._txout.script_pk);
33
+ return get_vout_version(this.data.script_pk);
28
34
  }
29
35
  toJSON() { return this.data; }
30
36
  toString() { return JSON.stringify(this.data); }
@@ -1,10 +1,9 @@
1
- import { Bytes } from '@vbyte/buff';
1
+ import { Transaction } from './tx.js';
2
2
  import type { ScriptField, WitnessData, WitnessSize, WitnessType } from '../types/index.js';
3
3
  export declare class TransactionWitness {
4
- private readonly _elems;
5
- private _data;
6
- private _size;
7
- constructor(witness: Bytes[]);
4
+ private readonly _tx;
5
+ private readonly _index;
6
+ constructor(transaction: Transaction, index: number);
8
7
  get annex(): string | null;
9
8
  get cblock(): string | null;
10
9
  get data(): WitnessData;
@@ -14,10 +13,6 @@ export declare class TransactionWitness {
14
13
  get stack(): string[];
15
14
  get type(): WitnessType;
16
15
  get version(): number | null;
17
- _update(): void;
18
- add(elem: Bytes): void;
19
- insert(index: number, elem: Bytes): void;
20
- remove(index: number): void;
21
16
  toJSON(): WitnessData;
22
17
  toString(): string;
23
18
  }
@@ -1,67 +1,45 @@
1
- import { Buff } from '@vbyte/buff';
2
1
  import { Assert } from '@vbyte/micro-lib';
3
2
  import { decode_script } from '../lib/script/index.js';
4
- import { parse_witness, get_witness_size, assert_witness, } from '../lib/witness/index.js';
3
+ import { parse_witness, get_witness_size } from '../lib/witness/index.js';
5
4
  export class TransactionWitness {
6
- constructor(witness) {
7
- assert_witness(witness);
8
- this._elems = witness.map(e => Buff.bytes(e));
9
- this._data = parse_witness(this._elems);
10
- this._size = get_witness_size(this._elems);
5
+ constructor(transaction, index) {
6
+ this._tx = transaction;
7
+ this._index = index;
11
8
  }
12
9
  get annex() {
13
- return this._data.annex;
10
+ return this.data.annex;
14
11
  }
15
12
  get cblock() {
16
- return this._data.cblock;
13
+ return this.data.cblock;
17
14
  }
18
15
  get data() {
19
- return this._data;
16
+ return parse_witness(this.stack);
20
17
  }
21
18
  get params() {
22
- return this._data.params;
19
+ return this.data.params;
23
20
  }
24
21
  get script() {
25
- if (this._data.script === null)
22
+ if (this.data.script === null)
26
23
  return null;
27
24
  return {
28
- hex: this._data.script,
29
- asm: decode_script(this._data.script)
25
+ hex: this.data.script,
26
+ asm: decode_script(this.data.script)
30
27
  };
31
28
  }
32
29
  get size() {
33
- return this._size;
30
+ return get_witness_size(this.stack);
34
31
  }
35
32
  get stack() {
36
- return this._elems.map(e => e.hex);
33
+ const txin = this._tx.data.vin.at(this._index);
34
+ Assert.exists(txin, 'txin not found at index ' + this._index);
35
+ Assert.exists(txin.witness, 'witness not found at index ' + this._index);
36
+ return txin.witness;
37
37
  }
38
38
  get type() {
39
- return this._data.type;
39
+ return this.data.type;
40
40
  }
41
41
  get version() {
42
- return this._data.version;
43
- }
44
- _update() {
45
- this._data = parse_witness(this._elems);
46
- this._size = get_witness_size(this._elems);
47
- }
48
- add(elem) {
49
- this._elems.push(Buff.bytes(elem));
50
- this._update();
51
- }
52
- insert(index, elem) {
53
- Assert.ok(index >= 0 && index <= this._elems.length, 'index out of bounds');
54
- if (index === this._elems.length) {
55
- this._elems.push(Buff.bytes(elem));
56
- }
57
- else {
58
- this._elems.splice(index, 0, Buff.bytes(elem));
59
- }
60
- this._update();
61
- }
62
- remove(index) {
63
- this._elems.splice(index, 1);
64
- this._update();
42
+ return this.data.version;
65
43
  }
66
44
  toJSON() { return this.data; }
67
45
  toString() { return JSON.stringify(this.data); }