@vbyte/btc-dev 1.0.10 → 1.0.12
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/index.d.ts +0 -1
- package/dist/index.js +0 -1
- package/dist/lib/sighash/segwit.d.ts +4 -1
- package/dist/lib/sighash/segwit.js +6 -6
- package/dist/lib/sighash/taproot.d.ts +7 -6
- package/dist/lib/sighash/taproot.js +24 -53
- package/dist/lib/sighash/util.d.ts +4 -2
- package/dist/lib/sighash/util.js +16 -2
- package/dist/lib/signer/sign.js +8 -5
- package/dist/lib/signer/verify.d.ts +1 -2
- package/dist/lib/signer/verify.js +1 -5
- package/dist/lib/tx/create.d.ts +7 -6
- package/dist/lib/tx/create.js +25 -27
- package/dist/lib/tx/decode.d.ts +4 -9
- package/dist/lib/tx/decode.js +16 -28
- package/dist/lib/tx/encode.d.ts +1 -1
- package/dist/lib/tx/encode.js +6 -6
- package/dist/lib/tx/parse.d.ts +3 -2
- package/dist/lib/tx/parse.js +38 -5
- package/dist/lib/tx/util.d.ts +8 -4
- package/dist/lib/tx/util.js +39 -15
- package/dist/lib/tx/validate.d.ts +4 -2
- package/dist/lib/tx/validate.js +9 -2
- package/dist/main.cjs +4049 -4269
- package/dist/main.cjs.map +1 -1
- package/dist/module.mjs +4040 -4255
- package/dist/module.mjs.map +1 -1
- package/dist/package.json +2 -2
- package/dist/schema/taproot.d.ts +2 -2
- package/dist/schema/tx.d.ts +53 -40
- package/dist/schema/tx.js +9 -6
- package/dist/script.js +8 -8
- package/dist/script.js.map +1 -1
- package/dist/types/txdata.d.ts +28 -5
- package/package.json +2 -2
- package/src/index.ts +0 -2
- package/src/lib/sighash/segwit.ts +6 -6
- package/src/lib/sighash/taproot.ts +40 -70
- package/src/lib/sighash/util.ts +25 -3
- package/src/lib/signer/sign.ts +9 -5
- package/src/lib/signer/verify.ts +1 -18
- package/src/lib/tx/create.ts +44 -36
- package/src/lib/tx/decode.ts +23 -45
- package/src/lib/tx/encode.ts +10 -9
- package/src/lib/tx/parse.ts +61 -8
- package/src/lib/tx/util.ts +57 -20
- package/src/lib/tx/validate.ts +15 -2
- package/src/schema/tx.ts +10 -6
- package/src/types/txdata.ts +32 -5
- package/dist/class/index.d.ts +0 -5
- package/dist/class/index.js +0 -5
- package/dist/class/signer.d.ts +0 -18
- package/dist/class/signer.js +0 -34
- package/dist/class/tx.d.ts +0 -51
- package/dist/class/tx.js +0 -122
- package/dist/class/txin.d.ts +0 -27
- package/dist/class/txin.js +0 -59
- package/dist/class/txout.d.ts +0 -16
- package/dist/class/txout.js +0 -31
- package/dist/class/witness.d.ts +0 -23
- package/dist/class/witness.js +0 -68
- package/src/class/index.ts +0 -5
- package/src/class/signer.ts +0 -49
- package/src/class/tx.ts +0 -175
- package/src/class/txin.ts +0 -81
- package/src/class/txout.ts +0 -50
- package/src/class/witness.ts +0 -99
package/dist/class/tx.js
DELETED
|
@@ -1,122 +0,0 @@
|
|
|
1
|
-
import { Assert } from '@vbyte/micro-lib';
|
|
2
|
-
import { LocktimeUtil } from '../lib/meta/index.js';
|
|
3
|
-
import { TransactionInput } from './txin.js';
|
|
4
|
-
import { TransactionOutput } from './txout.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
|
-
export class Transaction {
|
|
7
|
-
constructor(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
|
-
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
|
-
}
|
|
16
|
-
get data() {
|
|
17
|
-
return this._tx;
|
|
18
|
-
}
|
|
19
|
-
get hash() {
|
|
20
|
-
return this._hash;
|
|
21
|
-
}
|
|
22
|
-
get locktime() {
|
|
23
|
-
return {
|
|
24
|
-
hex: encode_tx_locktime(this._tx.locktime).hex,
|
|
25
|
-
data: LocktimeUtil.decode(this._tx.locktime),
|
|
26
|
-
value: this._tx.locktime
|
|
27
|
-
};
|
|
28
|
-
}
|
|
29
|
-
get return() {
|
|
30
|
-
return this._tx.vout.find(txout => is_return_script(txout.script_pk)) || null;
|
|
31
|
-
}
|
|
32
|
-
get size() {
|
|
33
|
-
return this._size;
|
|
34
|
-
}
|
|
35
|
-
get spends() {
|
|
36
|
-
return this._tx.vin
|
|
37
|
-
.filter(txin => txin.prevout !== null)
|
|
38
|
-
.map(txin => new TransactionOutput(txin.prevout));
|
|
39
|
-
}
|
|
40
|
-
get txid() {
|
|
41
|
-
return this._txid;
|
|
42
|
-
}
|
|
43
|
-
get value() {
|
|
44
|
-
return this._value;
|
|
45
|
-
}
|
|
46
|
-
get version() {
|
|
47
|
-
return this._tx.version;
|
|
48
|
-
}
|
|
49
|
-
get vin() {
|
|
50
|
-
return this._vin;
|
|
51
|
-
}
|
|
52
|
-
get vout() {
|
|
53
|
-
return this._vout;
|
|
54
|
-
}
|
|
55
|
-
add_vin(tx_input) {
|
|
56
|
-
const txin = create_tx_input(tx_input);
|
|
57
|
-
this._tx.vin.push(txin);
|
|
58
|
-
this._update_vin();
|
|
59
|
-
}
|
|
60
|
-
add_vout(tx_output) {
|
|
61
|
-
const txout = create_tx_output(tx_output);
|
|
62
|
-
this._tx.vout.push(txout);
|
|
63
|
-
this._update_vout();
|
|
64
|
-
}
|
|
65
|
-
insert_vin(index, tx_input) {
|
|
66
|
-
Assert.ok(index >= 0 && index <= this._tx.vin.length, 'input goes out of bounds');
|
|
67
|
-
const txin = create_tx_input(tx_input);
|
|
68
|
-
if (index === this._tx.vin.length) {
|
|
69
|
-
this._tx.vin.push(txin);
|
|
70
|
-
}
|
|
71
|
-
else {
|
|
72
|
-
this._tx.vin.splice(index, 0, txin);
|
|
73
|
-
}
|
|
74
|
-
this._update_vin();
|
|
75
|
-
}
|
|
76
|
-
insert_vout(index, tx_output) {
|
|
77
|
-
Assert.ok(index >= 0 && index <= this._tx.vout.length, 'output goes out of bounds');
|
|
78
|
-
const txout = create_tx_output(tx_output);
|
|
79
|
-
if (index === this._tx.vout.length) {
|
|
80
|
-
this._tx.vout.push(txout);
|
|
81
|
-
}
|
|
82
|
-
else {
|
|
83
|
-
this._tx.vout.splice(index, 0, txout);
|
|
84
|
-
}
|
|
85
|
-
this._update_vout();
|
|
86
|
-
}
|
|
87
|
-
remove_vin(index) {
|
|
88
|
-
Assert.ok(this._tx.vin.at(index) !== undefined, 'input does not exist at index');
|
|
89
|
-
this._tx.vin.splice(index, 1);
|
|
90
|
-
this._update_vin();
|
|
91
|
-
}
|
|
92
|
-
remove_vout(index) {
|
|
93
|
-
Assert.ok(this._tx.vout.at(index) !== undefined, 'output does not exist at index');
|
|
94
|
-
this._tx.vout.splice(index, 1);
|
|
95
|
-
this._update_vout();
|
|
96
|
-
}
|
|
97
|
-
_get_size() {
|
|
98
|
-
return {
|
|
99
|
-
...get_txsize(this._tx),
|
|
100
|
-
segwit: this.vin.reduce((acc, txin) => acc + (txin.witness?.size.vsize ?? 0), 0),
|
|
101
|
-
vin: this.vin.reduce((acc, txin) => acc + txin.size, 0),
|
|
102
|
-
vout: this.vout.reduce((acc, txout) => acc + txout.size, 0),
|
|
103
|
-
witness: this.vin.reduce((acc, txin) => acc + (txin.witness?.size.total ?? 0), 0)
|
|
104
|
-
};
|
|
105
|
-
}
|
|
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
|
-
toJSON() { return this.data; }
|
|
121
|
-
toString() { return JSON.stringify(this.data); }
|
|
122
|
-
}
|
package/dist/class/txin.d.ts
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import { TransactionOutput } from './txout.js';
|
|
2
|
-
import { TransactionWitness } from './witness.js';
|
|
3
|
-
import type { TxInput } from '../types/index.js';
|
|
4
|
-
export declare class TransactionInput {
|
|
5
|
-
private readonly _txin;
|
|
6
|
-
constructor(txin: TxInput);
|
|
7
|
-
get coinbase(): string | null;
|
|
8
|
-
get data(): TxInput;
|
|
9
|
-
get has_prevout(): boolean;
|
|
10
|
-
get is_coinbase(): boolean;
|
|
11
|
-
get prevout(): TransactionOutput | null;
|
|
12
|
-
get script_sig(): {
|
|
13
|
-
asm: string[];
|
|
14
|
-
hex: string;
|
|
15
|
-
} | null;
|
|
16
|
-
get sequence(): {
|
|
17
|
-
hex: string;
|
|
18
|
-
data: import("../types/index.js").SequenceData | null;
|
|
19
|
-
value: number;
|
|
20
|
-
};
|
|
21
|
-
get size(): number;
|
|
22
|
-
get txid(): string;
|
|
23
|
-
get vout(): number;
|
|
24
|
-
get witness(): TransactionWitness | null;
|
|
25
|
-
toJSON(): TxInput;
|
|
26
|
-
toString(): string;
|
|
27
|
-
}
|
package/dist/class/txin.js
DELETED
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
import { decode_script } from '../lib/script/index.js';
|
|
2
|
-
import { SequenceUtil } from '../lib/meta/index.js';
|
|
3
|
-
import { TransactionOutput } from './txout.js';
|
|
4
|
-
import { TransactionWitness } from './witness.js';
|
|
5
|
-
import { assert_tx_input, encode_txin_sequence, get_txin_size, } from '../lib/tx/index.js';
|
|
6
|
-
export class TransactionInput {
|
|
7
|
-
constructor(txin) {
|
|
8
|
-
assert_tx_input(txin);
|
|
9
|
-
this._txin = txin;
|
|
10
|
-
}
|
|
11
|
-
get coinbase() {
|
|
12
|
-
return this._txin.coinbase;
|
|
13
|
-
}
|
|
14
|
-
get data() {
|
|
15
|
-
return this._txin;
|
|
16
|
-
}
|
|
17
|
-
get has_prevout() {
|
|
18
|
-
return this._txin.prevout !== null;
|
|
19
|
-
}
|
|
20
|
-
get is_coinbase() {
|
|
21
|
-
return this._txin.coinbase !== null;
|
|
22
|
-
}
|
|
23
|
-
get prevout() {
|
|
24
|
-
return this._txin.prevout
|
|
25
|
-
? new TransactionOutput(this._txin.prevout)
|
|
26
|
-
: null;
|
|
27
|
-
}
|
|
28
|
-
get script_sig() {
|
|
29
|
-
if (this._txin.script_sig === null)
|
|
30
|
-
return null;
|
|
31
|
-
return {
|
|
32
|
-
asm: decode_script(this._txin.script_sig),
|
|
33
|
-
hex: this._txin.script_sig
|
|
34
|
-
};
|
|
35
|
-
}
|
|
36
|
-
get sequence() {
|
|
37
|
-
return {
|
|
38
|
-
hex: encode_txin_sequence(this._txin.sequence).hex,
|
|
39
|
-
data: SequenceUtil.decode(this._txin.sequence),
|
|
40
|
-
value: this._txin.sequence
|
|
41
|
-
};
|
|
42
|
-
}
|
|
43
|
-
get size() {
|
|
44
|
-
return get_txin_size(this._txin);
|
|
45
|
-
}
|
|
46
|
-
get txid() {
|
|
47
|
-
return this._txin.txid;
|
|
48
|
-
}
|
|
49
|
-
get vout() {
|
|
50
|
-
return this._txin.vout;
|
|
51
|
-
}
|
|
52
|
-
get witness() {
|
|
53
|
-
return this._txin.witness.length > 0
|
|
54
|
-
? new TransactionWitness(this._txin.witness)
|
|
55
|
-
: null;
|
|
56
|
-
}
|
|
57
|
-
toJSON() { return this.data; }
|
|
58
|
-
toString() { return JSON.stringify(this.data); }
|
|
59
|
-
}
|
package/dist/class/txout.d.ts
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import type { TxOutput } from '../types/index.js';
|
|
2
|
-
export declare class TransactionOutput {
|
|
3
|
-
private readonly _txout;
|
|
4
|
-
constructor(txout: TxOutput);
|
|
5
|
-
get data(): TxOutput;
|
|
6
|
-
get script_pk(): {
|
|
7
|
-
hex: string;
|
|
8
|
-
asm: string[];
|
|
9
|
-
};
|
|
10
|
-
get size(): number;
|
|
11
|
-
get type(): import("../types/index.js").TxOutputType;
|
|
12
|
-
get value(): bigint;
|
|
13
|
-
get version(): import("../types/index.js").WitnessVersion;
|
|
14
|
-
toJSON(): TxOutput;
|
|
15
|
-
toString(): string;
|
|
16
|
-
}
|
package/dist/class/txout.js
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
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
|
-
export class TransactionOutput {
|
|
4
|
-
constructor(txout) {
|
|
5
|
-
assert_tx_output(txout);
|
|
6
|
-
this._txout = txout;
|
|
7
|
-
}
|
|
8
|
-
get data() {
|
|
9
|
-
return this._txout;
|
|
10
|
-
}
|
|
11
|
-
get script_pk() {
|
|
12
|
-
return {
|
|
13
|
-
hex: this._txout.script_pk,
|
|
14
|
-
asm: decode_script(this._txout.script_pk)
|
|
15
|
-
};
|
|
16
|
-
}
|
|
17
|
-
get size() {
|
|
18
|
-
return get_txout_size(this._txout);
|
|
19
|
-
}
|
|
20
|
-
get type() {
|
|
21
|
-
return get_vout_type(this._txout.script_pk);
|
|
22
|
-
}
|
|
23
|
-
get value() {
|
|
24
|
-
return this._txout.value;
|
|
25
|
-
}
|
|
26
|
-
get version() {
|
|
27
|
-
return get_vout_version(this._txout.script_pk);
|
|
28
|
-
}
|
|
29
|
-
toJSON() { return this.data; }
|
|
30
|
-
toString() { return JSON.stringify(this.data); }
|
|
31
|
-
}
|
package/dist/class/witness.d.ts
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import { Bytes } from '@vbyte/buff';
|
|
2
|
-
import type { ScriptField, WitnessData, WitnessSize, WitnessType } from '../types/index.js';
|
|
3
|
-
export declare class TransactionWitness {
|
|
4
|
-
private readonly _elems;
|
|
5
|
-
private _data;
|
|
6
|
-
private _size;
|
|
7
|
-
constructor(witness: Bytes[]);
|
|
8
|
-
get annex(): string | null;
|
|
9
|
-
get cblock(): string | null;
|
|
10
|
-
get data(): WitnessData;
|
|
11
|
-
get params(): string[];
|
|
12
|
-
get script(): ScriptField | null;
|
|
13
|
-
get size(): WitnessSize;
|
|
14
|
-
get stack(): string[];
|
|
15
|
-
get type(): WitnessType;
|
|
16
|
-
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
|
-
toJSON(): WitnessData;
|
|
22
|
-
toString(): string;
|
|
23
|
-
}
|
package/dist/class/witness.js
DELETED
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
import { Buff } from '@vbyte/buff';
|
|
2
|
-
import { Assert } from '@vbyte/micro-lib';
|
|
3
|
-
import { decode_script } from '../lib/script/index.js';
|
|
4
|
-
import { parse_witness, get_witness_size, assert_witness, } from '../lib/witness/index.js';
|
|
5
|
-
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);
|
|
11
|
-
}
|
|
12
|
-
get annex() {
|
|
13
|
-
return this._data.annex;
|
|
14
|
-
}
|
|
15
|
-
get cblock() {
|
|
16
|
-
return this._data.cblock;
|
|
17
|
-
}
|
|
18
|
-
get data() {
|
|
19
|
-
return this._data;
|
|
20
|
-
}
|
|
21
|
-
get params() {
|
|
22
|
-
return this._data.params;
|
|
23
|
-
}
|
|
24
|
-
get script() {
|
|
25
|
-
if (this._data.script === null)
|
|
26
|
-
return null;
|
|
27
|
-
return {
|
|
28
|
-
hex: this._data.script,
|
|
29
|
-
asm: decode_script(this._data.script)
|
|
30
|
-
};
|
|
31
|
-
}
|
|
32
|
-
get size() {
|
|
33
|
-
return this._size;
|
|
34
|
-
}
|
|
35
|
-
get stack() {
|
|
36
|
-
return this._elems.map(e => e.hex);
|
|
37
|
-
}
|
|
38
|
-
get type() {
|
|
39
|
-
return this._data.type;
|
|
40
|
-
}
|
|
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();
|
|
65
|
-
}
|
|
66
|
-
toJSON() { return this.data; }
|
|
67
|
-
toString() { return JSON.stringify(this.data); }
|
|
68
|
-
}
|
package/src/class/index.ts
DELETED
package/src/class/signer.ts
DELETED
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
import { Buff, Bytes } from '@vbyte/buff'
|
|
2
|
-
import { Assert, ECC } from '@vbyte/micro-lib'
|
|
3
|
-
|
|
4
|
-
import {
|
|
5
|
-
sign_segwit_tx,
|
|
6
|
-
sign_taproot_tx
|
|
7
|
-
} from '@/lib/signer/sign.js'
|
|
8
|
-
|
|
9
|
-
import type {
|
|
10
|
-
SigHashOptions,
|
|
11
|
-
TxData
|
|
12
|
-
} from '@/types/index.js'
|
|
13
|
-
|
|
14
|
-
export class TxSigner {
|
|
15
|
-
private readonly _seckey : string
|
|
16
|
-
|
|
17
|
-
constructor (seckey : Bytes) {
|
|
18
|
-
Assert.ok(Buff.is_bytes(seckey), 'seckey must be a string or bytes')
|
|
19
|
-
Assert.size(seckey, 32, 'seckey must be 32 bytes')
|
|
20
|
-
this._seckey = Buff.bytes(seckey).hex
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
get pubkey () {
|
|
24
|
-
return {
|
|
25
|
-
segwit : ECC.get_pubkey(this._seckey, 'ecdsa'),
|
|
26
|
-
taproot : ECC.get_pubkey(this._seckey, 'bip340')
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
get sign_msg () {
|
|
31
|
-
return {
|
|
32
|
-
ecdsa : (msg : Bytes) => {
|
|
33
|
-
const bytes = Buff.bytes(msg)
|
|
34
|
-
return ECC.sign_ecdsa(this._seckey, bytes)
|
|
35
|
-
},
|
|
36
|
-
bip340 : (msg : Bytes) => {
|
|
37
|
-
const bytes = Buff.bytes(msg)
|
|
38
|
-
return ECC.sign_bip340(this._seckey, bytes)
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
get sign_tx () {
|
|
44
|
-
return {
|
|
45
|
-
segwit : (tx : TxData, options : SigHashOptions) => sign_segwit_tx(this._seckey, tx, options),
|
|
46
|
-
taproot : (tx : TxData, options : SigHashOptions) => sign_taproot_tx(this._seckey, tx, options)
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
}
|
package/src/class/tx.ts
DELETED
|
@@ -1,175 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
import { Assert } from '@vbyte/micro-lib'
|
|
3
|
-
import { LocktimeUtil } from '@/lib/meta/index.js'
|
|
4
|
-
import { TransactionInput } from './txin.js'
|
|
5
|
-
import { TransactionOutput } from './txout.js'
|
|
6
|
-
|
|
7
|
-
import {
|
|
8
|
-
get_txid,
|
|
9
|
-
is_return_script,
|
|
10
|
-
parse_tx,
|
|
11
|
-
get_txsize,
|
|
12
|
-
get_tx_value,
|
|
13
|
-
get_txhash,
|
|
14
|
-
encode_tx_locktime,
|
|
15
|
-
create_tx_input,
|
|
16
|
-
create_tx_output,
|
|
17
|
-
} from '@/lib/tx/index.js'
|
|
18
|
-
|
|
19
|
-
import type {
|
|
20
|
-
TxData,
|
|
21
|
-
TxTemplate,
|
|
22
|
-
TxOutput,
|
|
23
|
-
TxSize,
|
|
24
|
-
TxValue,
|
|
25
|
-
TxInputTemplate
|
|
26
|
-
} from '@/types/index.js'
|
|
27
|
-
|
|
28
|
-
export class Transaction {
|
|
29
|
-
|
|
30
|
-
private readonly _tx : TxData
|
|
31
|
-
|
|
32
|
-
private _size : TxSize & { segwit : number }
|
|
33
|
-
private _hash : string
|
|
34
|
-
private _txid : string
|
|
35
|
-
private _value : TxValue
|
|
36
|
-
private _vin : TransactionInput[]
|
|
37
|
-
private _vout : TransactionOutput[]
|
|
38
|
-
|
|
39
|
-
constructor (txdata : string | TxData | TxTemplate = {}) {
|
|
40
|
-
this._tx = parse_tx(txdata)
|
|
41
|
-
this._vin = this._tx.vin.map(txin => new TransactionInput(txin))
|
|
42
|
-
this._vout = this._tx.vout.map(txout => new TransactionOutput(txout))
|
|
43
|
-
|
|
44
|
-
this._size = this._get_size()
|
|
45
|
-
this._hash = get_txhash(this._tx)
|
|
46
|
-
this._txid = get_txid(this._tx)
|
|
47
|
-
this._value = get_tx_value(this._tx)
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
get data () : TxData {
|
|
51
|
-
return this._tx
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
get hash () : string {
|
|
55
|
-
return this._hash
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
get locktime () {
|
|
59
|
-
return {
|
|
60
|
-
hex : encode_tx_locktime(this._tx.locktime).hex,
|
|
61
|
-
data : LocktimeUtil.decode(this._tx.locktime),
|
|
62
|
-
value : this._tx.locktime
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
get return () {
|
|
67
|
-
return this._tx.vout.find(txout => is_return_script(txout.script_pk)) || null
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
get size () {
|
|
71
|
-
return this._size
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
get spends () : TransactionOutput[] {
|
|
75
|
-
return this._tx.vin
|
|
76
|
-
.filter(txin => txin.prevout !== null)
|
|
77
|
-
.map(txin => new TransactionOutput(txin.prevout!))
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
get txid () : string {
|
|
81
|
-
return this._txid
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
get value () {
|
|
85
|
-
return this._value
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
get version () : number {
|
|
89
|
-
return this._tx.version
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
get vin () : TransactionInput[] {
|
|
93
|
-
return this._vin
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
get vout () : TransactionOutput[] {
|
|
97
|
-
return this._vout
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
add_vin (tx_input : TxInputTemplate) {
|
|
101
|
-
const txin = create_tx_input(tx_input)
|
|
102
|
-
this._tx.vin.push(txin)
|
|
103
|
-
this._update_vin()
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
add_vout (tx_output : TxOutput) {
|
|
107
|
-
const txout = create_tx_output(tx_output)
|
|
108
|
-
this._tx.vout.push(txout)
|
|
109
|
-
this._update_vout()
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
insert_vin (index : number, tx_input : TxInputTemplate) {
|
|
113
|
-
Assert.ok(index >= 0 && index <= this._tx.vin.length, 'input goes out of bounds')
|
|
114
|
-
const txin = create_tx_input(tx_input)
|
|
115
|
-
if (index === this._tx.vin.length) {
|
|
116
|
-
this._tx.vin.push(txin)
|
|
117
|
-
} else {
|
|
118
|
-
this._tx.vin.splice(index, 0, txin)
|
|
119
|
-
}
|
|
120
|
-
this._update_vin()
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
insert_vout (index : number, tx_output : TxOutput) {
|
|
124
|
-
Assert.ok(index >= 0 && index <= this._tx.vout.length, 'output goes out of bounds')
|
|
125
|
-
const txout = create_tx_output(tx_output)
|
|
126
|
-
if (index === this._tx.vout.length) {
|
|
127
|
-
this._tx.vout.push(txout)
|
|
128
|
-
} else {
|
|
129
|
-
this._tx.vout.splice(index, 0, txout)
|
|
130
|
-
}
|
|
131
|
-
this._update_vout()
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
remove_vin (index : number) {
|
|
135
|
-
Assert.ok(this._tx.vin.at(index) !== undefined, 'input does not exist at index')
|
|
136
|
-
this._tx.vin.splice(index, 1)
|
|
137
|
-
this._update_vin()
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
remove_vout (index : number) {
|
|
141
|
-
Assert.ok(this._tx.vout.at(index) !== undefined, 'output does not exist at index')
|
|
142
|
-
this._tx.vout.splice(index, 1)
|
|
143
|
-
this._update_vout()
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
_get_size () {
|
|
147
|
-
return {
|
|
148
|
-
...get_txsize(this._tx),
|
|
149
|
-
segwit : this.vin.reduce((acc, txin) => acc + (txin.witness?.size.vsize ?? 0), 0),
|
|
150
|
-
vin : this.vin.reduce((acc, txin) => acc + txin.size, 0),
|
|
151
|
-
vout : this.vout.reduce((acc, txout) => acc + txout.size, 0),
|
|
152
|
-
witness : this.vin.reduce((acc, txin) => acc + (txin.witness?.size.total ?? 0), 0)
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
_update_tx () {
|
|
157
|
-
this._size = this._get_size()
|
|
158
|
-
this._hash = get_txhash(this._tx)
|
|
159
|
-
this._txid = get_txid(this._tx)
|
|
160
|
-
this._value = get_tx_value(this._tx)
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
_update_vin () {
|
|
164
|
-
this._vin = this._tx.vin.map(txin => new TransactionInput(txin))
|
|
165
|
-
this._update_tx()
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
_update_vout () {
|
|
169
|
-
this._vout = this._tx.vout.map(txout => new TransactionOutput(txout))
|
|
170
|
-
this._update_tx()
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
toJSON () { return this.data }
|
|
174
|
-
toString () { return JSON.stringify(this.data) }
|
|
175
|
-
}
|
package/src/class/txin.ts
DELETED
|
@@ -1,81 +0,0 @@
|
|
|
1
|
-
import { decode_script } from '@/lib/script/index.js'
|
|
2
|
-
import { SequenceUtil } from '@/lib/meta/index.js'
|
|
3
|
-
import { TransactionOutput } from './txout.js'
|
|
4
|
-
import { TransactionWitness } from './witness.js'
|
|
5
|
-
|
|
6
|
-
import {
|
|
7
|
-
assert_tx_input,
|
|
8
|
-
encode_txin_sequence,
|
|
9
|
-
get_txin_size,
|
|
10
|
-
} from '@/lib/tx/index.js'
|
|
11
|
-
|
|
12
|
-
import type { TxInput } from '@/types/index.js'
|
|
13
|
-
|
|
14
|
-
export class TransactionInput {
|
|
15
|
-
|
|
16
|
-
private readonly _txin : TxInput
|
|
17
|
-
|
|
18
|
-
constructor (txin : TxInput) {
|
|
19
|
-
assert_tx_input(txin)
|
|
20
|
-
this._txin = txin
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
get coinbase () : string | null {
|
|
24
|
-
return this._txin.coinbase
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
get data () : TxInput {
|
|
28
|
-
return this._txin
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
get has_prevout () : boolean {
|
|
32
|
-
return this._txin.prevout !== null
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
get is_coinbase () : boolean {
|
|
36
|
-
return this._txin.coinbase !== null
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
get prevout () : TransactionOutput | null {
|
|
40
|
-
return this._txin.prevout
|
|
41
|
-
? new TransactionOutput(this._txin.prevout)
|
|
42
|
-
: null
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
get script_sig () {
|
|
46
|
-
if (this._txin.script_sig === null) return null
|
|
47
|
-
return {
|
|
48
|
-
asm : decode_script(this._txin.script_sig),
|
|
49
|
-
hex : this._txin.script_sig
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
get sequence () {
|
|
54
|
-
return {
|
|
55
|
-
hex : encode_txin_sequence(this._txin.sequence).hex,
|
|
56
|
-
data : SequenceUtil.decode(this._txin.sequence),
|
|
57
|
-
value : this._txin.sequence
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
get size () {
|
|
62
|
-
return get_txin_size(this._txin)
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
get txid () : string {
|
|
66
|
-
return this._txin.txid
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
get vout () : number {
|
|
70
|
-
return this._txin.vout
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
get witness () {
|
|
74
|
-
return this._txin.witness.length > 0
|
|
75
|
-
? new TransactionWitness(this._txin.witness)
|
|
76
|
-
: null
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
toJSON () { return this.data }
|
|
80
|
-
toString () { return JSON.stringify(this.data) }
|
|
81
|
-
}
|