@vbyte/btc-dev 1.0.5 → 1.0.7
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/class/tx.d.ts +28 -15
- package/dist/class/tx.js +68 -11
- package/dist/class/txin.d.ts +0 -2
- package/dist/class/txin.js +4 -6
- package/dist/class/txout.d.ts +0 -2
- package/dist/class/txout.js +4 -6
- package/dist/class/witness.d.ts +9 -6
- package/dist/class/witness.js +37 -18
- package/dist/lib/taproot/parse.js +2 -2
- package/dist/lib/tx/size.js +3 -3
- package/dist/lib/witness/parse.d.ts +2 -2
- package/dist/lib/witness/parse.js +1 -1
- package/dist/lib/witness/util.js +1 -1
- package/dist/main.cjs +116 -46
- package/dist/main.cjs.map +1 -1
- package/dist/module.mjs +116 -46
- package/dist/module.mjs.map +1 -1
- package/dist/package.json +3 -3
- package/dist/script.js +5 -5
- package/dist/script.js.map +1 -1
- package/dist/types/class.d.ts +2 -3
- package/dist/types/txdata.d.ts +1 -1
- package/dist/types/witness.d.ts +7 -7
- package/package.json +3 -3
- package/src/class/tx.ts +89 -24
- package/src/class/txin.ts +6 -10
- package/src/class/txout.ts +6 -9
- package/src/class/witness.ts +45 -23
- package/src/lib/taproot/parse.ts +2 -2
- package/src/lib/tx/size.ts +3 -3
- package/src/lib/witness/parse.ts +3 -3
- package/src/lib/witness/util.ts +1 -1
- package/src/types/class.ts +4 -5
- package/src/types/txdata.ts +1 -1
- package/src/types/witness.ts +7 -7
package/dist/class/tx.d.ts
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
import { TransactionInput } from './txin.js';
|
|
2
2
|
import { TransactionOutput } from './txout.js';
|
|
3
|
-
import type { TxData, TxTemplate,
|
|
3
|
+
import type { TxData, TxTemplate, TransactionData, TxInput, TxOutput, TxSize, TxValue } from '../types/index.js';
|
|
4
4
|
export declare class Transaction {
|
|
5
|
-
private readonly _size;
|
|
6
5
|
private readonly _tx;
|
|
7
|
-
private
|
|
8
|
-
private
|
|
9
|
-
private
|
|
10
|
-
private
|
|
6
|
+
private _size;
|
|
7
|
+
private _hash;
|
|
8
|
+
private _txid;
|
|
9
|
+
private _value;
|
|
10
|
+
private _vin;
|
|
11
|
+
private _vout;
|
|
11
12
|
constructor(txdata: string | TxData | TxTemplate);
|
|
12
13
|
get data(): TransactionData;
|
|
13
14
|
get hash(): string;
|
|
@@ -16,16 +17,9 @@ export declare class Transaction {
|
|
|
16
17
|
data: import("../types/index.js").LocktimeData | null;
|
|
17
18
|
value: number;
|
|
18
19
|
};
|
|
19
|
-
get return():
|
|
20
|
-
get size(): {
|
|
20
|
+
get return(): TxOutput | null;
|
|
21
|
+
get size(): TxSize & {
|
|
21
22
|
segwit: number;
|
|
22
|
-
vin: number;
|
|
23
|
-
vout: number;
|
|
24
|
-
witness: number;
|
|
25
|
-
base: number;
|
|
26
|
-
real: number;
|
|
27
|
-
weight: number;
|
|
28
|
-
vsize: number;
|
|
29
23
|
};
|
|
30
24
|
get spends(): TransactionOutput[];
|
|
31
25
|
get txid(): string;
|
|
@@ -33,6 +27,25 @@ export declare class Transaction {
|
|
|
33
27
|
get version(): number;
|
|
34
28
|
get vin(): TransactionInput[];
|
|
35
29
|
get vout(): TransactionOutput[];
|
|
30
|
+
add_vin(txin: TxInput): void;
|
|
31
|
+
add_vout(txout: TxOutput): void;
|
|
32
|
+
insert_vin(index: number, txin: TxInput): void;
|
|
33
|
+
insert_vout(index: number, txout: TxOutput): void;
|
|
34
|
+
remove_vin(index: number): void;
|
|
35
|
+
remove_vout(index: number): void;
|
|
36
|
+
_get_size(): {
|
|
37
|
+
segwit: number;
|
|
38
|
+
vin: number;
|
|
39
|
+
vout: number;
|
|
40
|
+
witness: number;
|
|
41
|
+
base: number;
|
|
42
|
+
total: number;
|
|
43
|
+
weight: number;
|
|
44
|
+
vsize: number;
|
|
45
|
+
};
|
|
46
|
+
_update_tx(): void;
|
|
47
|
+
_update_vin(): void;
|
|
48
|
+
_update_vout(): void;
|
|
36
49
|
toJSON(): TransactionData;
|
|
37
50
|
toString(): string;
|
|
38
51
|
}
|
package/dist/class/tx.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { Assert } from '@vbyte/micro-lib';
|
|
1
2
|
import { LocktimeUtil } from '../lib/meta/index.js';
|
|
2
3
|
import { TransactionInput } from './txin.js';
|
|
3
4
|
import { TransactionOutput } from './txout.js';
|
|
@@ -7,11 +8,12 @@ export class Transaction {
|
|
|
7
8
|
this._tx = (typeof txdata !== 'string')
|
|
8
9
|
? parse_tx(txdata)
|
|
9
10
|
: decode_tx(txdata);
|
|
10
|
-
this.
|
|
11
|
-
this._vout = this._tx.vout.map(txout => new TransactionOutput(txout));
|
|
12
|
-
this._size = get_txsize(this._tx);
|
|
11
|
+
this._size = this._get_size();
|
|
13
12
|
this._hash = get_txhash(this._tx);
|
|
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));
|
|
15
17
|
}
|
|
16
18
|
get data() {
|
|
17
19
|
return {
|
|
@@ -41,13 +43,7 @@ export class Transaction {
|
|
|
41
43
|
return this._tx.vout.find(txout => is_return_script(txout.script_pk)) || null;
|
|
42
44
|
}
|
|
43
45
|
get size() {
|
|
44
|
-
return
|
|
45
|
-
...this._size,
|
|
46
|
-
segwit: this._vin.reduce((acc, txin) => acc + (txin.witness?.vsize ?? 0), 0),
|
|
47
|
-
vin: this._vin.reduce((acc, txin) => acc + txin.size, 0),
|
|
48
|
-
vout: this._vout.reduce((acc, txout) => acc + txout.size, 0),
|
|
49
|
-
witness: this._vin.reduce((acc, txin) => acc + (txin.witness?.size ?? 0), 0)
|
|
50
|
-
};
|
|
46
|
+
return this._size;
|
|
51
47
|
}
|
|
52
48
|
get spends() {
|
|
53
49
|
return this._tx.vin
|
|
@@ -55,7 +51,7 @@ export class Transaction {
|
|
|
55
51
|
.map(txin => new TransactionOutput(txin.prevout));
|
|
56
52
|
}
|
|
57
53
|
get txid() {
|
|
58
|
-
return
|
|
54
|
+
return this._txid;
|
|
59
55
|
}
|
|
60
56
|
get value() {
|
|
61
57
|
return this._value;
|
|
@@ -69,6 +65,67 @@ export class Transaction {
|
|
|
69
65
|
get vout() {
|
|
70
66
|
return this._vout;
|
|
71
67
|
}
|
|
68
|
+
add_vin(txin) {
|
|
69
|
+
this._tx.vin.push(txin);
|
|
70
|
+
this._update_vin();
|
|
71
|
+
}
|
|
72
|
+
add_vout(txout) {
|
|
73
|
+
this._tx.vout.push(txout);
|
|
74
|
+
this._update_vout();
|
|
75
|
+
}
|
|
76
|
+
insert_vin(index, txin) {
|
|
77
|
+
Assert.ok(index >= 0 && index <= this._tx.vin.length, 'input goes out of bounds');
|
|
78
|
+
if (index === this._tx.vin.length) {
|
|
79
|
+
this._tx.vin.push(txin);
|
|
80
|
+
}
|
|
81
|
+
else {
|
|
82
|
+
this._tx.vin.splice(index, 0, txin);
|
|
83
|
+
}
|
|
84
|
+
this._update_vin();
|
|
85
|
+
}
|
|
86
|
+
insert_vout(index, txout) {
|
|
87
|
+
Assert.ok(index >= 0 && index <= this._tx.vout.length, 'output goes out of bounds');
|
|
88
|
+
if (index === this._tx.vout.length) {
|
|
89
|
+
this._tx.vout.push(txout);
|
|
90
|
+
}
|
|
91
|
+
else {
|
|
92
|
+
this._tx.vout.splice(index, 0, txout);
|
|
93
|
+
}
|
|
94
|
+
this._update_vout();
|
|
95
|
+
}
|
|
96
|
+
remove_vin(index) {
|
|
97
|
+
Assert.ok(this._tx.vin.at(index) !== undefined, 'input does not exist at index');
|
|
98
|
+
this._tx.vin.splice(index, 1);
|
|
99
|
+
this._update_vin();
|
|
100
|
+
}
|
|
101
|
+
remove_vout(index) {
|
|
102
|
+
Assert.ok(this._tx.vout.at(index) !== undefined, 'output does not exist at index');
|
|
103
|
+
this._tx.vout.splice(index, 1);
|
|
104
|
+
this._update_vout();
|
|
105
|
+
}
|
|
106
|
+
_get_size() {
|
|
107
|
+
return {
|
|
108
|
+
...get_txsize(this._tx),
|
|
109
|
+
segwit: this.vin.reduce((acc, txin) => acc + (txin.witness?.size.vsize ?? 0), 0),
|
|
110
|
+
vin: this.vin.reduce((acc, txin) => acc + txin.size, 0),
|
|
111
|
+
vout: this.vout.reduce((acc, txout) => acc + txout.size, 0),
|
|
112
|
+
witness: this.vin.reduce((acc, txin) => acc + (txin.witness?.size.total ?? 0), 0)
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
_update_tx() {
|
|
116
|
+
this._size = this._get_size();
|
|
117
|
+
this._hash = get_txhash(this._tx);
|
|
118
|
+
this._txid = get_txid(this._tx);
|
|
119
|
+
this._value = get_tx_value(this._tx);
|
|
120
|
+
}
|
|
121
|
+
_update_vin() {
|
|
122
|
+
this._vin = this._tx.vin.map(txin => new TransactionInput(txin));
|
|
123
|
+
this._update_tx();
|
|
124
|
+
}
|
|
125
|
+
_update_vout() {
|
|
126
|
+
this._vout = this._tx.vout.map(txout => new TransactionOutput(txout));
|
|
127
|
+
this._update_tx();
|
|
128
|
+
}
|
|
72
129
|
toJSON() { return this.data; }
|
|
73
130
|
toString() { return JSON.stringify(this.data); }
|
|
74
131
|
}
|
package/dist/class/txin.d.ts
CHANGED
|
@@ -2,9 +2,7 @@ import { TransactionOutput } from './txout.js';
|
|
|
2
2
|
import { TransactionWitness } from './witness.js';
|
|
3
3
|
import type { TxInput, TransactionInputData } from '../types/index.js';
|
|
4
4
|
export declare class TransactionInput {
|
|
5
|
-
private readonly _size;
|
|
6
5
|
private readonly _txin;
|
|
7
|
-
private readonly _witness;
|
|
8
6
|
constructor(txin: TxInput);
|
|
9
7
|
get coinbase(): string | null;
|
|
10
8
|
get data(): TransactionInputData;
|
package/dist/class/txin.js
CHANGED
|
@@ -5,11 +5,7 @@ import { TransactionWitness } from './witness.js';
|
|
|
5
5
|
import { encode_txin_sequence, get_txin_size, } from '../lib/tx/index.js';
|
|
6
6
|
export class TransactionInput {
|
|
7
7
|
constructor(txin) {
|
|
8
|
-
this._size = get_txin_size(txin);
|
|
9
8
|
this._txin = txin;
|
|
10
|
-
this._witness = txin.witness.length > 0
|
|
11
|
-
? new TransactionWitness(txin.witness)
|
|
12
|
-
: null;
|
|
13
9
|
}
|
|
14
10
|
get coinbase() {
|
|
15
11
|
return this._txin.coinbase;
|
|
@@ -53,7 +49,7 @@ export class TransactionInput {
|
|
|
53
49
|
};
|
|
54
50
|
}
|
|
55
51
|
get size() {
|
|
56
|
-
return this.
|
|
52
|
+
return get_txin_size(this._txin);
|
|
57
53
|
}
|
|
58
54
|
get txid() {
|
|
59
55
|
return this._txin.txid;
|
|
@@ -62,7 +58,9 @@ export class TransactionInput {
|
|
|
62
58
|
return this._txin.vout;
|
|
63
59
|
}
|
|
64
60
|
get witness() {
|
|
65
|
-
return this.
|
|
61
|
+
return this._txin.witness.length > 0
|
|
62
|
+
? new TransactionWitness(this._txin.witness)
|
|
63
|
+
: null;
|
|
66
64
|
}
|
|
67
65
|
toJSON() { return this.data; }
|
|
68
66
|
toString() { return JSON.stringify(this.data); }
|
package/dist/class/txout.d.ts
CHANGED
package/dist/class/txout.js
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
import { decode_script } from '../lib/script/index.js';
|
|
2
|
-
import { get_txout_size,
|
|
2
|
+
import { get_txout_size, get_vout_type, get_vout_version } from '../lib/tx/index.js';
|
|
3
3
|
export class TransactionOutput {
|
|
4
4
|
constructor(txout) {
|
|
5
|
-
this._info = get_vout_info(txout);
|
|
6
|
-
this._size = get_txout_size(txout);
|
|
7
5
|
this._txout = txout;
|
|
8
6
|
}
|
|
9
7
|
get data() {
|
|
@@ -22,16 +20,16 @@ export class TransactionOutput {
|
|
|
22
20
|
};
|
|
23
21
|
}
|
|
24
22
|
get size() {
|
|
25
|
-
return this.
|
|
23
|
+
return get_txout_size(this._txout);
|
|
26
24
|
}
|
|
27
25
|
get type() {
|
|
28
|
-
return this.
|
|
26
|
+
return get_vout_type(this._txout.script_pk);
|
|
29
27
|
}
|
|
30
28
|
get value() {
|
|
31
29
|
return this._txout.value;
|
|
32
30
|
}
|
|
33
31
|
get version() {
|
|
34
|
-
return this.
|
|
32
|
+
return get_vout_version(this._txout.script_pk);
|
|
35
33
|
}
|
|
36
34
|
toJSON() { return this.data; }
|
|
37
35
|
toString() { return JSON.stringify(this.data); }
|
package/dist/class/witness.d.ts
CHANGED
|
@@ -1,20 +1,23 @@
|
|
|
1
1
|
import { Bytes } from '@vbyte/buff';
|
|
2
|
-
import type { ScriptField, WitnessField, WitnessType } from '../types/index.js';
|
|
2
|
+
import type { ScriptField, WitnessField, WitnessSize, WitnessType } from '../types/index.js';
|
|
3
3
|
export declare class TransactionWitness {
|
|
4
|
-
private readonly
|
|
5
|
-
private
|
|
6
|
-
private
|
|
4
|
+
private readonly _elems;
|
|
5
|
+
private _data;
|
|
6
|
+
private _size;
|
|
7
7
|
constructor(witness: Bytes[]);
|
|
8
8
|
get annex(): string | null;
|
|
9
9
|
get cblock(): string | null;
|
|
10
10
|
get data(): WitnessField;
|
|
11
11
|
get params(): string[];
|
|
12
12
|
get script(): ScriptField | null;
|
|
13
|
-
get size():
|
|
13
|
+
get size(): WitnessSize;
|
|
14
14
|
get stack(): string[];
|
|
15
15
|
get type(): WitnessType;
|
|
16
16
|
get version(): number | null;
|
|
17
|
-
|
|
17
|
+
_update(): void;
|
|
18
|
+
add(elem: Bytes): void;
|
|
19
|
+
insert(index: number, elem: Bytes): void;
|
|
20
|
+
remove(index: number): void;
|
|
18
21
|
toJSON(): WitnessField;
|
|
19
22
|
toString(): string;
|
|
20
23
|
}
|
package/dist/class/witness.js
CHANGED
|
@@ -1,17 +1,18 @@
|
|
|
1
1
|
import { Buff } from '@vbyte/buff';
|
|
2
|
+
import { Assert } from '@vbyte/micro-lib';
|
|
2
3
|
import { decode_script } from '../lib/script/index.js';
|
|
3
|
-
import {
|
|
4
|
+
import { parse_witness, get_witness_size, } from '../lib/witness/index.js';
|
|
4
5
|
export class TransactionWitness {
|
|
5
6
|
constructor(witness) {
|
|
6
|
-
this.
|
|
7
|
-
this.
|
|
8
|
-
this._size = get_witness_size(
|
|
7
|
+
this._elems = witness.map(e => Buff.bytes(e));
|
|
8
|
+
this._data = parse_witness(this._elems);
|
|
9
|
+
this._size = get_witness_size(this._elems);
|
|
9
10
|
}
|
|
10
11
|
get annex() {
|
|
11
|
-
return this.
|
|
12
|
+
return this._data.annex;
|
|
12
13
|
}
|
|
13
14
|
get cblock() {
|
|
14
|
-
return this.
|
|
15
|
+
return this._data.cblock;
|
|
15
16
|
}
|
|
16
17
|
get data() {
|
|
17
18
|
return {
|
|
@@ -22,35 +23,53 @@ export class TransactionWitness {
|
|
|
22
23
|
size: this.size,
|
|
23
24
|
stack: this.stack,
|
|
24
25
|
type: this.type,
|
|
25
|
-
version: this.version
|
|
26
|
-
vsize: this.vsize
|
|
26
|
+
version: this.version
|
|
27
27
|
};
|
|
28
28
|
}
|
|
29
29
|
get params() {
|
|
30
|
-
return this.
|
|
30
|
+
return this._data.params;
|
|
31
31
|
}
|
|
32
32
|
get script() {
|
|
33
|
-
if (this.
|
|
33
|
+
if (this._data.script === null)
|
|
34
34
|
return null;
|
|
35
35
|
return {
|
|
36
|
-
hex: this.
|
|
37
|
-
asm: decode_script(this.
|
|
36
|
+
hex: this._data.script,
|
|
37
|
+
asm: decode_script(this._data.script)
|
|
38
38
|
};
|
|
39
39
|
}
|
|
40
40
|
get size() {
|
|
41
|
-
return this._size
|
|
41
|
+
return this._size;
|
|
42
42
|
}
|
|
43
43
|
get stack() {
|
|
44
|
-
return this.
|
|
44
|
+
return this._elems.map(e => e.hex);
|
|
45
45
|
}
|
|
46
46
|
get type() {
|
|
47
|
-
return this.
|
|
47
|
+
return this._data.type;
|
|
48
48
|
}
|
|
49
49
|
get version() {
|
|
50
|
-
return this.
|
|
50
|
+
return this._data.version;
|
|
51
51
|
}
|
|
52
|
-
|
|
53
|
-
|
|
52
|
+
_update() {
|
|
53
|
+
this._data = parse_witness(this._elems);
|
|
54
|
+
this._size = get_witness_size(this._elems);
|
|
55
|
+
}
|
|
56
|
+
add(elem) {
|
|
57
|
+
this._elems.push(Buff.bytes(elem));
|
|
58
|
+
this._update();
|
|
59
|
+
}
|
|
60
|
+
insert(index, elem) {
|
|
61
|
+
Assert.ok(index >= 0 && index <= this._elems.length, 'index out of bounds');
|
|
62
|
+
if (index === this._elems.length) {
|
|
63
|
+
this._elems.push(Buff.bytes(elem));
|
|
64
|
+
}
|
|
65
|
+
else {
|
|
66
|
+
this._elems.splice(index, 0, Buff.bytes(elem));
|
|
67
|
+
}
|
|
68
|
+
this._update();
|
|
69
|
+
}
|
|
70
|
+
remove(index) {
|
|
71
|
+
this._elems.splice(index, 1);
|
|
72
|
+
this._update();
|
|
54
73
|
}
|
|
55
74
|
toJSON() { return this.data; }
|
|
56
75
|
toString() { return JSON.stringify(this.data); }
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { Buff, Stream } from '@vbyte/buff';
|
|
2
2
|
import { Assert, ECC } from '@vbyte/micro-lib';
|
|
3
|
-
import {
|
|
3
|
+
import { parse_witness } from '../../lib/witness/parse.js';
|
|
4
4
|
import { encode_tapbranch, encode_tapscript, encode_taptweak, } from './encode.js';
|
|
5
5
|
export function parse_taproot_witness(witness) {
|
|
6
|
-
const { cblock, params, script } =
|
|
6
|
+
const { cblock, params, script } = parse_witness(witness);
|
|
7
7
|
Assert.exists(cblock, 'cblock is null');
|
|
8
8
|
Assert.exists(script, 'script is null');
|
|
9
9
|
const cblk = parse_cblock(cblock);
|
package/dist/lib/tx/size.js
CHANGED
|
@@ -10,11 +10,11 @@ export function get_vsize(bytes) {
|
|
|
10
10
|
export function get_txsize(txdata) {
|
|
11
11
|
const json = parse_tx(txdata);
|
|
12
12
|
const base = encode_tx(json, false).length;
|
|
13
|
-
const
|
|
14
|
-
const weight = base * 3 +
|
|
13
|
+
const total = encode_tx(json, true).length;
|
|
14
|
+
const weight = base * 3 + total;
|
|
15
15
|
const remain = (weight % 4 > 0) ? 1 : 0;
|
|
16
16
|
const vsize = Math.floor(weight / 4) + remain;
|
|
17
|
-
return { base,
|
|
17
|
+
return { base, total, vsize, weight };
|
|
18
18
|
}
|
|
19
19
|
export function get_vin_size(vin) {
|
|
20
20
|
const bytes = encode_tx_inputs(vin);
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import { Bytes } from '@vbyte/buff';
|
|
2
|
-
import type {
|
|
3
|
-
export declare function
|
|
2
|
+
import type { WitnessData } from '../../types/index.js';
|
|
3
|
+
export declare function parse_witness(witness: Bytes[]): WitnessData;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Buff } from '@vbyte/buff';
|
|
2
2
|
import { is_valid_script } from '../../lib/script/decode.js';
|
|
3
3
|
import { TAPLEAF_VERSIONS } from '../../const.js';
|
|
4
|
-
export function
|
|
4
|
+
export function parse_witness(witness) {
|
|
5
5
|
const elems = witness.map(e => Buff.bytes(e));
|
|
6
6
|
const annex = parse_annex_data(elems);
|
|
7
7
|
if (annex !== null)
|
package/dist/lib/witness/util.js
CHANGED
|
@@ -4,5 +4,5 @@ export function get_witness_size(witness) {
|
|
|
4
4
|
const stack = witness.map(e => Buff.bytes(e));
|
|
5
5
|
const size = stack.reduce((prev, next) => prev + next.length, 0);
|
|
6
6
|
const vsize = Math.ceil(WIT_LENGTH_BYTE + size / 4);
|
|
7
|
-
return { size, vsize };
|
|
7
|
+
return { total: size, vsize };
|
|
8
8
|
}
|