@vbyte/btc-dev 1.0.11 → 1.0.13

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.
Files changed (67) hide show
  1. package/dist/index.d.ts +0 -1
  2. package/dist/index.js +0 -1
  3. package/dist/lib/sighash/segwit.d.ts +4 -1
  4. package/dist/lib/sighash/segwit.js +6 -6
  5. package/dist/lib/sighash/taproot.d.ts +7 -6
  6. package/dist/lib/sighash/taproot.js +24 -53
  7. package/dist/lib/sighash/util.d.ts +4 -2
  8. package/dist/lib/sighash/util.js +16 -2
  9. package/dist/lib/signer/sign.js +8 -5
  10. package/dist/lib/signer/verify.d.ts +1 -2
  11. package/dist/lib/signer/verify.js +1 -5
  12. package/dist/lib/tx/create.d.ts +7 -6
  13. package/dist/lib/tx/create.js +25 -27
  14. package/dist/lib/tx/decode.d.ts +4 -9
  15. package/dist/lib/tx/decode.js +16 -28
  16. package/dist/lib/tx/encode.d.ts +1 -1
  17. package/dist/lib/tx/encode.js +6 -6
  18. package/dist/lib/tx/parse.d.ts +3 -2
  19. package/dist/lib/tx/parse.js +38 -5
  20. package/dist/lib/tx/util.d.ts +8 -4
  21. package/dist/lib/tx/util.js +39 -15
  22. package/dist/lib/tx/validate.d.ts +4 -2
  23. package/dist/lib/tx/validate.js +9 -2
  24. package/dist/main.cjs +4049 -4230
  25. package/dist/main.cjs.map +1 -1
  26. package/dist/module.mjs +4040 -4216
  27. package/dist/module.mjs.map +1 -1
  28. package/dist/package.json +2 -2
  29. package/dist/schema/taproot.d.ts +2 -2
  30. package/dist/schema/tx.d.ts +53 -40
  31. package/dist/schema/tx.js +9 -6
  32. package/dist/script.js +8 -8
  33. package/dist/script.js.map +1 -1
  34. package/dist/types/txdata.d.ts +17 -1
  35. package/package.json +2 -2
  36. package/src/index.ts +0 -2
  37. package/src/lib/sighash/segwit.ts +6 -6
  38. package/src/lib/sighash/taproot.ts +40 -70
  39. package/src/lib/sighash/util.ts +25 -3
  40. package/src/lib/signer/sign.ts +9 -5
  41. package/src/lib/signer/verify.ts +1 -18
  42. package/src/lib/tx/create.ts +46 -38
  43. package/src/lib/tx/decode.ts +23 -45
  44. package/src/lib/tx/encode.ts +10 -9
  45. package/src/lib/tx/parse.ts +61 -8
  46. package/src/lib/tx/util.ts +57 -20
  47. package/src/lib/tx/validate.ts +15 -2
  48. package/src/schema/tx.ts +10 -6
  49. package/src/types/txdata.ts +20 -1
  50. package/dist/class/index.d.ts +0 -5
  51. package/dist/class/index.js +0 -5
  52. package/dist/class/signer.d.ts +0 -18
  53. package/dist/class/signer.js +0 -34
  54. package/dist/class/tx.d.ts +0 -40
  55. package/dist/class/tx.js +0 -96
  56. package/dist/class/txin.d.ts +0 -29
  57. package/dist/class/txin.js +0 -62
  58. package/dist/class/txout.d.ts +0 -19
  59. package/dist/class/txout.js +0 -37
  60. package/dist/class/witness.d.ts +0 -18
  61. package/dist/class/witness.js +0 -46
  62. package/src/class/index.ts +0 -5
  63. package/src/class/signer.ts +0 -49
  64. package/src/class/tx.ts +0 -136
  65. package/src/class/txin.ts +0 -89
  66. package/src/class/txout.ts +0 -61
  67. package/src/class/witness.ts +0 -75
@@ -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
- }
@@ -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
- }
@@ -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
- }
@@ -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
- }
@@ -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
- }
@@ -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
- }
@@ -1,5 +0,0 @@
1
- export * from './signer.js'
2
- export * from './tx.js'
3
- export * from './txin.js'
4
- export * from './txout.js'
5
- export * from './witness.js'
@@ -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
- }
@@ -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
- }
@@ -1,75 +0,0 @@
1
- import { Transaction } from './tx.js'
2
- import { Assert } from '@vbyte/micro-lib'
3
- import { decode_script } from '@/lib/script/index.js'
4
-
5
- import {
6
- parse_witness,
7
- get_witness_size
8
- } from '@/lib/witness/index.js'
9
-
10
- import type {
11
- ScriptField,
12
- WitnessData,
13
- WitnessSize,
14
- WitnessType
15
- } from '@/types/index.js'
16
-
17
- export class TransactionWitness {
18
-
19
- private readonly _tx : Transaction
20
- private readonly _index : number
21
-
22
- constructor (
23
- transaction : Transaction,
24
- index : number
25
- ) {
26
- this._tx = transaction
27
- this._index = index
28
- }
29
-
30
- get annex () : string | null {
31
- return this.data.annex
32
- }
33
-
34
- get cblock () : string | null {
35
- return this.data.cblock
36
- }
37
-
38
- get data () : WitnessData {
39
- return parse_witness(this.stack)
40
- }
41
-
42
- get params () : string[] {
43
- return this.data.params
44
- }
45
-
46
- get script () : ScriptField | null {
47
- if (this.data.script === null) return null
48
- return {
49
- hex : this.data.script,
50
- asm : decode_script(this.data.script)
51
- }
52
- }
53
-
54
- get size () : WitnessSize {
55
- return get_witness_size(this.stack)
56
- }
57
-
58
- get stack () : string[] {
59
- const txin = this._tx.data.vin.at(this._index)
60
- Assert.exists(txin, 'txin not found at index ' + this._index)
61
- Assert.exists(txin.witness, 'witness not found at index ' + this._index)
62
- return txin.witness
63
- }
64
-
65
- get type () : WitnessType {
66
- return this.data.type
67
- }
68
-
69
- get version () : number | null {
70
- return this.data.version
71
- }
72
-
73
- toJSON () { return this.data }
74
- toString () { return JSON.stringify(this.data) }
75
- }