@vbyte/btc-dev 1.0.11 → 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 -4230
- package/dist/main.cjs.map +1 -1
- package/dist/module.mjs +4040 -4216
- 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 -40
- package/dist/class/tx.js +0 -96
- package/dist/class/txin.d.ts +0 -29
- package/dist/class/txin.js +0 -62
- package/dist/class/txout.d.ts +0 -19
- package/dist/class/txout.js +0 -37
- package/dist/class/witness.d.ts +0 -18
- package/dist/class/witness.js +0 -46
- package/src/class/index.ts +0 -5
- package/src/class/signer.ts +0 -49
- package/src/class/tx.ts +0 -136
- package/src/class/txin.ts +0 -89
- package/src/class/txout.ts +0 -61
- package/src/class/witness.ts +0 -75
package/dist/class/tx.js
DELETED
|
@@ -1,96 +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
|
-
}
|
|
10
|
-
get data() {
|
|
11
|
-
return this._tx;
|
|
12
|
-
}
|
|
13
|
-
get hash() {
|
|
14
|
-
return get_txhash(this._tx);
|
|
15
|
-
}
|
|
16
|
-
get locktime() {
|
|
17
|
-
return {
|
|
18
|
-
hex: encode_tx_locktime(this._tx.locktime).hex,
|
|
19
|
-
data: LocktimeUtil.decode(this._tx.locktime),
|
|
20
|
-
value: this._tx.locktime
|
|
21
|
-
};
|
|
22
|
-
}
|
|
23
|
-
get return() {
|
|
24
|
-
return this._tx.vout.find(txout => is_return_script(txout.script_pk)) || null;
|
|
25
|
-
}
|
|
26
|
-
get size() {
|
|
27
|
-
return get_txsize(this._tx);
|
|
28
|
-
}
|
|
29
|
-
get spends() {
|
|
30
|
-
return this._tx.vin
|
|
31
|
-
.filter(txin => txin.prevout !== null)
|
|
32
|
-
.map(txin => txin.prevout);
|
|
33
|
-
}
|
|
34
|
-
get txid() {
|
|
35
|
-
return get_txid(this._tx);
|
|
36
|
-
}
|
|
37
|
-
get value() {
|
|
38
|
-
return get_tx_value(this._tx);
|
|
39
|
-
}
|
|
40
|
-
get version() {
|
|
41
|
-
return this._tx.version;
|
|
42
|
-
}
|
|
43
|
-
get vin() {
|
|
44
|
-
return this._tx.vin.map((_, idx) => new TransactionInput(this, idx));
|
|
45
|
-
}
|
|
46
|
-
get vout() {
|
|
47
|
-
return this._tx.vout.map((_, idx) => new TransactionOutput(this, idx));
|
|
48
|
-
}
|
|
49
|
-
add_vin(tx_input) {
|
|
50
|
-
const txin = create_tx_input(tx_input);
|
|
51
|
-
this._tx.vin.push(txin);
|
|
52
|
-
}
|
|
53
|
-
add_vout(tx_output) {
|
|
54
|
-
const txout = create_tx_output(tx_output);
|
|
55
|
-
this._tx.vout.push(txout);
|
|
56
|
-
}
|
|
57
|
-
insert_vin(index, tx_input) {
|
|
58
|
-
Assert.ok(index >= 0 && index <= this._tx.vin.length, 'input goes out of bounds');
|
|
59
|
-
const txin = create_tx_input(tx_input);
|
|
60
|
-
if (index === this._tx.vin.length) {
|
|
61
|
-
this._tx.vin.push(txin);
|
|
62
|
-
}
|
|
63
|
-
else {
|
|
64
|
-
this._tx.vin.splice(index, 0, txin);
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
insert_vout(index, tx_output) {
|
|
68
|
-
Assert.ok(index >= 0 && index <= this._tx.vout.length, 'output goes out of bounds');
|
|
69
|
-
const txout = create_tx_output(tx_output);
|
|
70
|
-
if (index === this._tx.vout.length) {
|
|
71
|
-
this._tx.vout.push(txout);
|
|
72
|
-
}
|
|
73
|
-
else {
|
|
74
|
-
this._tx.vout.splice(index, 0, txout);
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
remove_vin(index) {
|
|
78
|
-
Assert.ok(this._tx.vin.at(index) !== undefined, 'input does not exist at index');
|
|
79
|
-
this._tx.vin.splice(index, 1);
|
|
80
|
-
}
|
|
81
|
-
remove_vout(index) {
|
|
82
|
-
Assert.ok(this._tx.vout.at(index) !== undefined, 'output does not exist at index');
|
|
83
|
-
this._tx.vout.splice(index, 1);
|
|
84
|
-
}
|
|
85
|
-
_get_size() {
|
|
86
|
-
return {
|
|
87
|
-
...get_txsize(this._tx),
|
|
88
|
-
segwit: this.vin.reduce((acc, txin) => acc + (txin.witness?.size.vsize ?? 0), 0),
|
|
89
|
-
vin: this.vin.reduce((acc, txin) => acc + txin.size, 0),
|
|
90
|
-
vout: this.vout.reduce((acc, txout) => acc + txout.size, 0),
|
|
91
|
-
witness: this.vin.reduce((acc, txin) => acc + (txin.witness?.size.total ?? 0), 0)
|
|
92
|
-
};
|
|
93
|
-
}
|
|
94
|
-
toJSON() { return this.data; }
|
|
95
|
-
toString() { return JSON.stringify(this.data); }
|
|
96
|
-
}
|
package/dist/class/txin.d.ts
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import { Transaction } from './tx.js';
|
|
2
|
-
import { TransactionWitness } from './witness.js';
|
|
3
|
-
import type { TxInput, TxOutput } from '../types/index.js';
|
|
4
|
-
export declare class TransactionInput {
|
|
5
|
-
private readonly _tx;
|
|
6
|
-
private readonly _index;
|
|
7
|
-
constructor(transaction: Transaction, index: number);
|
|
8
|
-
get coinbase(): string | null;
|
|
9
|
-
get data(): TxInput;
|
|
10
|
-
get has_prevout(): boolean;
|
|
11
|
-
get index(): number;
|
|
12
|
-
get is_coinbase(): boolean;
|
|
13
|
-
get prevout(): TxOutput | null;
|
|
14
|
-
get script_sig(): {
|
|
15
|
-
asm: string[];
|
|
16
|
-
hex: string;
|
|
17
|
-
} | null;
|
|
18
|
-
get sequence(): {
|
|
19
|
-
hex: string;
|
|
20
|
-
data: import("../types/index.js").SequenceData | null;
|
|
21
|
-
value: number;
|
|
22
|
-
};
|
|
23
|
-
get size(): number;
|
|
24
|
-
get txid(): string;
|
|
25
|
-
get vout(): number;
|
|
26
|
-
get witness(): TransactionWitness | null;
|
|
27
|
-
toJSON(): TxInput;
|
|
28
|
-
toString(): string;
|
|
29
|
-
}
|
package/dist/class/txin.js
DELETED
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
import { Assert } from '@vbyte/micro-lib';
|
|
2
|
-
import { decode_script } from '../lib/script/index.js';
|
|
3
|
-
import { SequenceUtil } from '../lib/meta/index.js';
|
|
4
|
-
import { TransactionWitness } from './witness.js';
|
|
5
|
-
import { encode_txin_sequence, get_txin_size, } from '../lib/tx/index.js';
|
|
6
|
-
export class TransactionInput {
|
|
7
|
-
constructor(transaction, index) {
|
|
8
|
-
this._tx = transaction;
|
|
9
|
-
this._index = index;
|
|
10
|
-
}
|
|
11
|
-
get coinbase() {
|
|
12
|
-
return this.data.coinbase;
|
|
13
|
-
}
|
|
14
|
-
get data() {
|
|
15
|
-
const txin = this._tx.data.vin.at(this.index);
|
|
16
|
-
Assert.exists(txin, 'txin not found');
|
|
17
|
-
return txin;
|
|
18
|
-
}
|
|
19
|
-
get has_prevout() {
|
|
20
|
-
return this.data.prevout !== null;
|
|
21
|
-
}
|
|
22
|
-
get index() {
|
|
23
|
-
return this._index;
|
|
24
|
-
}
|
|
25
|
-
get is_coinbase() {
|
|
26
|
-
return this.data.coinbase !== null;
|
|
27
|
-
}
|
|
28
|
-
get prevout() {
|
|
29
|
-
return this.data.prevout;
|
|
30
|
-
}
|
|
31
|
-
get script_sig() {
|
|
32
|
-
if (this.data.script_sig === null)
|
|
33
|
-
return null;
|
|
34
|
-
return {
|
|
35
|
-
asm: decode_script(this.data.script_sig),
|
|
36
|
-
hex: this.data.script_sig
|
|
37
|
-
};
|
|
38
|
-
}
|
|
39
|
-
get sequence() {
|
|
40
|
-
return {
|
|
41
|
-
hex: encode_txin_sequence(this.data.sequence).hex,
|
|
42
|
-
data: SequenceUtil.decode(this.data.sequence),
|
|
43
|
-
value: this.data.sequence
|
|
44
|
-
};
|
|
45
|
-
}
|
|
46
|
-
get size() {
|
|
47
|
-
return get_txin_size(this.data);
|
|
48
|
-
}
|
|
49
|
-
get txid() {
|
|
50
|
-
return this.data.txid;
|
|
51
|
-
}
|
|
52
|
-
get vout() {
|
|
53
|
-
return this.data.vout;
|
|
54
|
-
}
|
|
55
|
-
get witness() {
|
|
56
|
-
return this.data.witness.length > 0
|
|
57
|
-
? new TransactionWitness(this._tx, this.index)
|
|
58
|
-
: null;
|
|
59
|
-
}
|
|
60
|
-
toJSON() { return this.data; }
|
|
61
|
-
toString() { return JSON.stringify(this.data); }
|
|
62
|
-
}
|
package/dist/class/txout.d.ts
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { Transaction } from './tx.js';
|
|
2
|
-
import type { TxOutput } from '../types/index.js';
|
|
3
|
-
export declare class TransactionOutput {
|
|
4
|
-
private readonly _tx;
|
|
5
|
-
private readonly _index;
|
|
6
|
-
constructor(transaction: Transaction, index: number);
|
|
7
|
-
get data(): TxOutput;
|
|
8
|
-
get index(): number;
|
|
9
|
-
get script_pk(): {
|
|
10
|
-
hex: string;
|
|
11
|
-
asm: string[];
|
|
12
|
-
};
|
|
13
|
-
get size(): number;
|
|
14
|
-
get type(): import("../types/index.js").TxOutputType;
|
|
15
|
-
get value(): bigint;
|
|
16
|
-
get version(): import("../types/index.js").WitnessVersion;
|
|
17
|
-
toJSON(): TxOutput;
|
|
18
|
-
toString(): string;
|
|
19
|
-
}
|
package/dist/class/txout.js
DELETED
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
import { Assert } from '@vbyte/micro-lib';
|
|
2
|
-
import { decode_script } from '../lib/script/index.js';
|
|
3
|
-
import { get_txout_size, get_vout_type, get_vout_version } from '../lib/tx/index.js';
|
|
4
|
-
export class TransactionOutput {
|
|
5
|
-
constructor(transaction, index) {
|
|
6
|
-
this._tx = transaction;
|
|
7
|
-
this._index = index;
|
|
8
|
-
}
|
|
9
|
-
get data() {
|
|
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;
|
|
16
|
-
}
|
|
17
|
-
get script_pk() {
|
|
18
|
-
return {
|
|
19
|
-
hex: this.data.script_pk,
|
|
20
|
-
asm: decode_script(this.data.script_pk)
|
|
21
|
-
};
|
|
22
|
-
}
|
|
23
|
-
get size() {
|
|
24
|
-
return get_txout_size(this.data);
|
|
25
|
-
}
|
|
26
|
-
get type() {
|
|
27
|
-
return get_vout_type(this.data.script_pk);
|
|
28
|
-
}
|
|
29
|
-
get value() {
|
|
30
|
-
return this.data.value;
|
|
31
|
-
}
|
|
32
|
-
get version() {
|
|
33
|
-
return get_vout_version(this.data.script_pk);
|
|
34
|
-
}
|
|
35
|
-
toJSON() { return this.data; }
|
|
36
|
-
toString() { return JSON.stringify(this.data); }
|
|
37
|
-
}
|
package/dist/class/witness.d.ts
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { Transaction } from './tx.js';
|
|
2
|
-
import type { ScriptField, WitnessData, WitnessSize, WitnessType } from '../types/index.js';
|
|
3
|
-
export declare class TransactionWitness {
|
|
4
|
-
private readonly _tx;
|
|
5
|
-
private readonly _index;
|
|
6
|
-
constructor(transaction: Transaction, index: number);
|
|
7
|
-
get annex(): string | null;
|
|
8
|
-
get cblock(): string | null;
|
|
9
|
-
get data(): WitnessData;
|
|
10
|
-
get params(): string[];
|
|
11
|
-
get script(): ScriptField | null;
|
|
12
|
-
get size(): WitnessSize;
|
|
13
|
-
get stack(): string[];
|
|
14
|
-
get type(): WitnessType;
|
|
15
|
-
get version(): number | null;
|
|
16
|
-
toJSON(): WitnessData;
|
|
17
|
-
toString(): string;
|
|
18
|
-
}
|
package/dist/class/witness.js
DELETED
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
import { Assert } from '@vbyte/micro-lib';
|
|
2
|
-
import { decode_script } from '../lib/script/index.js';
|
|
3
|
-
import { parse_witness, get_witness_size } from '../lib/witness/index.js';
|
|
4
|
-
export class TransactionWitness {
|
|
5
|
-
constructor(transaction, index) {
|
|
6
|
-
this._tx = transaction;
|
|
7
|
-
this._index = index;
|
|
8
|
-
}
|
|
9
|
-
get annex() {
|
|
10
|
-
return this.data.annex;
|
|
11
|
-
}
|
|
12
|
-
get cblock() {
|
|
13
|
-
return this.data.cblock;
|
|
14
|
-
}
|
|
15
|
-
get data() {
|
|
16
|
-
return parse_witness(this.stack);
|
|
17
|
-
}
|
|
18
|
-
get params() {
|
|
19
|
-
return this.data.params;
|
|
20
|
-
}
|
|
21
|
-
get script() {
|
|
22
|
-
if (this.data.script === null)
|
|
23
|
-
return null;
|
|
24
|
-
return {
|
|
25
|
-
hex: this.data.script,
|
|
26
|
-
asm: decode_script(this.data.script)
|
|
27
|
-
};
|
|
28
|
-
}
|
|
29
|
-
get size() {
|
|
30
|
-
return get_witness_size(this.stack);
|
|
31
|
-
}
|
|
32
|
-
get stack() {
|
|
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
|
-
}
|
|
38
|
-
get type() {
|
|
39
|
-
return this.data.type;
|
|
40
|
-
}
|
|
41
|
-
get version() {
|
|
42
|
-
return this.data.version;
|
|
43
|
-
}
|
|
44
|
-
toJSON() { return this.data; }
|
|
45
|
-
toString() { return JSON.stringify(this.data); }
|
|
46
|
-
}
|
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,136 +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
|
-
TxInputTemplate
|
|
24
|
-
} from '@/types/index.js'
|
|
25
|
-
|
|
26
|
-
export class Transaction {
|
|
27
|
-
|
|
28
|
-
private readonly _tx : TxData
|
|
29
|
-
|
|
30
|
-
constructor (txdata : string | TxData | TxTemplate = {}) {
|
|
31
|
-
this._tx = parse_tx(txdata)
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
get data () : TxData {
|
|
35
|
-
return this._tx
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
get hash () : string {
|
|
39
|
-
return get_txhash(this._tx)
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
get locktime () {
|
|
43
|
-
return {
|
|
44
|
-
hex : encode_tx_locktime(this._tx.locktime).hex,
|
|
45
|
-
data : LocktimeUtil.decode(this._tx.locktime),
|
|
46
|
-
value : this._tx.locktime
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
get return () {
|
|
51
|
-
return this._tx.vout.find(txout => is_return_script(txout.script_pk)) || null
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
get size () {
|
|
55
|
-
return get_txsize(this._tx)
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
get spends () : TxOutput[] {
|
|
59
|
-
return this._tx.vin
|
|
60
|
-
.filter(txin => txin.prevout !== null)
|
|
61
|
-
.map(txin => txin.prevout!)
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
get txid () : string {
|
|
65
|
-
return get_txid(this._tx)
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
get value () {
|
|
69
|
-
return get_tx_value(this._tx)
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
get version () : number {
|
|
73
|
-
return this._tx.version
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
get vin () : TransactionInput[] {
|
|
77
|
-
return this._tx.vin.map((_, idx) => new TransactionInput(this, idx))
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
get vout () : TransactionOutput[] {
|
|
81
|
-
return this._tx.vout.map((_, idx) => new TransactionOutput(this, idx))
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
add_vin (tx_input : TxInputTemplate) {
|
|
85
|
-
const txin = create_tx_input(tx_input)
|
|
86
|
-
this._tx.vin.push(txin)
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
add_vout (tx_output : TxOutput) {
|
|
90
|
-
const txout = create_tx_output(tx_output)
|
|
91
|
-
this._tx.vout.push(txout)
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
insert_vin (index : number, tx_input : TxInputTemplate) {
|
|
95
|
-
Assert.ok(index >= 0 && index <= this._tx.vin.length, 'input goes out of bounds')
|
|
96
|
-
const txin = create_tx_input(tx_input)
|
|
97
|
-
if (index === this._tx.vin.length) {
|
|
98
|
-
this._tx.vin.push(txin)
|
|
99
|
-
} else {
|
|
100
|
-
this._tx.vin.splice(index, 0, txin)
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
insert_vout (index : number, tx_output : TxOutput) {
|
|
105
|
-
Assert.ok(index >= 0 && index <= this._tx.vout.length, 'output goes out of bounds')
|
|
106
|
-
const txout = create_tx_output(tx_output)
|
|
107
|
-
if (index === this._tx.vout.length) {
|
|
108
|
-
this._tx.vout.push(txout)
|
|
109
|
-
} else {
|
|
110
|
-
this._tx.vout.splice(index, 0, txout)
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
remove_vin (index : number) {
|
|
115
|
-
Assert.ok(this._tx.vin.at(index) !== undefined, 'input does not exist at index')
|
|
116
|
-
this._tx.vin.splice(index, 1)
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
remove_vout (index : number) {
|
|
120
|
-
Assert.ok(this._tx.vout.at(index) !== undefined, 'output does not exist at index')
|
|
121
|
-
this._tx.vout.splice(index, 1)
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
_get_size () {
|
|
125
|
-
return {
|
|
126
|
-
...get_txsize(this._tx),
|
|
127
|
-
segwit : this.vin.reduce((acc, txin) => acc + (txin.witness?.size.vsize ?? 0), 0),
|
|
128
|
-
vin : this.vin.reduce((acc, txin) => acc + txin.size, 0),
|
|
129
|
-
vout : this.vout.reduce((acc, txout) => acc + txout.size, 0),
|
|
130
|
-
witness : this.vin.reduce((acc, txin) => acc + (txin.witness?.size.total ?? 0), 0)
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
toJSON () { return this.data }
|
|
135
|
-
toString () { return JSON.stringify(this.data) }
|
|
136
|
-
}
|
package/src/class/txin.ts
DELETED
|
@@ -1,89 +0,0 @@
|
|
|
1
|
-
import { Assert } from '@vbyte/micro-lib'
|
|
2
|
-
import { Transaction } from './tx.js'
|
|
3
|
-
import { decode_script } from '@/lib/script/index.js'
|
|
4
|
-
import { SequenceUtil } from '@/lib/meta/index.js'
|
|
5
|
-
import { TransactionWitness } from './witness.js'
|
|
6
|
-
|
|
7
|
-
import {
|
|
8
|
-
encode_txin_sequence,
|
|
9
|
-
get_txin_size,
|
|
10
|
-
} from '@/lib/tx/index.js'
|
|
11
|
-
|
|
12
|
-
import type { TxInput, TxOutput } from '@/types/index.js'
|
|
13
|
-
|
|
14
|
-
export class TransactionInput {
|
|
15
|
-
|
|
16
|
-
private readonly _tx : Transaction
|
|
17
|
-
private readonly _index : number
|
|
18
|
-
|
|
19
|
-
constructor (
|
|
20
|
-
transaction : Transaction,
|
|
21
|
-
index : number
|
|
22
|
-
) {
|
|
23
|
-
this._tx = transaction
|
|
24
|
-
this._index = index
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
get coinbase () : string | null {
|
|
28
|
-
return this.data.coinbase
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
get data () : TxInput {
|
|
32
|
-
const txin = this._tx.data.vin.at(this.index)
|
|
33
|
-
Assert.exists(txin, 'txin not found')
|
|
34
|
-
return txin
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
get has_prevout () : boolean {
|
|
38
|
-
return this.data.prevout !== null
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
get index () : number {
|
|
42
|
-
return this._index
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
get is_coinbase () : boolean {
|
|
46
|
-
return this.data.coinbase !== null
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
get prevout () : TxOutput | null {
|
|
50
|
-
return this.data.prevout
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
get script_sig () {
|
|
54
|
-
if (this.data.script_sig === null) return null
|
|
55
|
-
return {
|
|
56
|
-
asm : decode_script(this.data.script_sig),
|
|
57
|
-
hex : this.data.script_sig
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
get sequence () {
|
|
62
|
-
return {
|
|
63
|
-
hex : encode_txin_sequence(this.data.sequence).hex,
|
|
64
|
-
data : SequenceUtil.decode(this.data.sequence),
|
|
65
|
-
value : this.data.sequence
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
get size () {
|
|
70
|
-
return get_txin_size(this.data)
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
get txid () : string {
|
|
74
|
-
return this.data.txid
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
get vout () : number {
|
|
78
|
-
return this.data.vout
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
get witness () {
|
|
82
|
-
return this.data.witness.length > 0
|
|
83
|
-
? new TransactionWitness(this._tx, this.index)
|
|
84
|
-
: null
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
toJSON () { return this.data }
|
|
88
|
-
toString () { return JSON.stringify(this.data) }
|
|
89
|
-
}
|
package/src/class/txout.ts
DELETED
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
import { Assert } from '@vbyte/micro-lib'
|
|
2
|
-
import { Transaction } from './tx.js'
|
|
3
|
-
import { decode_script } from '@/lib/script/index.js'
|
|
4
|
-
|
|
5
|
-
import {
|
|
6
|
-
get_txout_size,
|
|
7
|
-
get_vout_type,
|
|
8
|
-
get_vout_version
|
|
9
|
-
} from '@/lib/tx/index.js'
|
|
10
|
-
|
|
11
|
-
import type { TxOutput } from '@/types/index.js'
|
|
12
|
-
|
|
13
|
-
export class TransactionOutput {
|
|
14
|
-
|
|
15
|
-
private readonly _tx : Transaction
|
|
16
|
-
private readonly _index : number
|
|
17
|
-
|
|
18
|
-
constructor (
|
|
19
|
-
transaction : Transaction,
|
|
20
|
-
index : number
|
|
21
|
-
) {
|
|
22
|
-
this._tx = transaction
|
|
23
|
-
this._index = index
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
get data () : TxOutput {
|
|
27
|
-
const txout = this._tx.data.vout.at(this.index)
|
|
28
|
-
Assert.exists(txout, 'txout not found')
|
|
29
|
-
return txout
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
get index () {
|
|
33
|
-
return this._index
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
get script_pk () {
|
|
37
|
-
return {
|
|
38
|
-
hex : this.data.script_pk,
|
|
39
|
-
asm : decode_script(this.data.script_pk)
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
get size () {
|
|
44
|
-
return get_txout_size(this.data)
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
get type () {
|
|
48
|
-
return get_vout_type(this.data.script_pk)
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
get value () : bigint {
|
|
52
|
-
return this.data.value
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
get version () {
|
|
56
|
-
return get_vout_version(this.data.script_pk)
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
toJSON () { return this.data }
|
|
60
|
-
toString () { return JSON.stringify(this.data) }
|
|
61
|
-
}
|