@vbyte/btc-dev 1.1.2 → 1.1.4
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/const.d.ts +16 -0
- package/dist/const.js +22 -6
- package/dist/lib/address/api.d.ts +4 -0
- package/dist/lib/address/api.js +32 -0
- package/dist/lib/address/encode.d.ts +3 -3
- package/dist/lib/address/index.d.ts +1 -15
- package/dist/lib/address/index.js +1 -16
- package/dist/lib/address/p2pkh.d.ts +14 -7
- package/dist/lib/address/p2pkh.js +34 -16
- package/dist/lib/address/p2sh.d.ts +14 -7
- package/dist/lib/address/p2sh.js +33 -14
- package/dist/lib/address/p2tr.d.ts +14 -5
- package/dist/lib/address/p2tr.js +34 -11
- package/dist/lib/address/p2wpkh.d.ts +14 -7
- package/dist/lib/address/p2wpkh.js +32 -15
- package/dist/lib/address/p2wsh.d.ts +14 -7
- package/dist/lib/address/p2wsh.js +31 -14
- package/dist/lib/address/script.d.ts +2 -5
- package/dist/lib/address/script.js +12 -12
- package/dist/lib/address/util.d.ts +3 -4
- package/dist/lib/address/util.js +10 -14
- package/dist/lib/script/decode.d.ts +2 -0
- package/dist/lib/script/decode.js +8 -1
- package/dist/lib/script/index.d.ts +2 -1
- package/dist/lib/script/index.js +2 -1
- package/dist/lib/script/lock.d.ts +12 -0
- package/dist/lib/script/lock.js +52 -0
- package/dist/lib/signer/sign.js +2 -2
- package/dist/lib/tx/util.d.ts +4 -9
- package/dist/lib/tx/util.js +16 -35
- package/dist/lib/witness/parse.js +11 -9
- package/dist/main.cjs +4419 -4481
- package/dist/main.cjs.map +1 -1
- package/dist/module.mjs +4419 -4481
- package/dist/module.mjs.map +1 -1
- package/dist/package.json +2 -2
- package/dist/schema/taproot.d.ts +5 -15
- package/dist/schema/tx.d.ts +44 -262
- package/dist/script.js +8 -9
- package/dist/script.js.map +1 -1
- package/dist/types/address.d.ts +6 -10
- package/dist/types/index.d.ts +1 -0
- package/dist/types/index.js +1 -0
- package/dist/types/meta.d.ts +0 -14
- package/dist/types/script.d.ts +15 -0
- package/dist/types/script.js +1 -0
- package/dist/types/witness.d.ts +7 -8
- package/package.json +2 -2
- package/src/const.ts +25 -7
- package/src/lib/address/api.ts +50 -0
- package/src/lib/address/encode.ts +9 -9
- package/src/lib/address/index.ts +1 -18
- package/src/lib/address/p2pkh.ts +54 -25
- package/src/lib/address/p2sh.ts +55 -24
- package/src/lib/address/p2tr.ts +59 -19
- package/src/lib/address/p2wpkh.ts +53 -26
- package/src/lib/address/p2wsh.ts +53 -26
- package/src/lib/address/script.ts +14 -14
- package/src/lib/address/util.ts +16 -31
- package/src/lib/script/decode.ts +11 -1
- package/src/lib/script/index.ts +3 -1
- package/src/lib/script/lock.ts +73 -0
- package/src/lib/signer/sign.ts +2 -2
- package/src/lib/tx/util.ts +31 -54
- package/src/lib/witness/parse.ts +17 -13
- package/src/types/address.ts +7 -11
- package/src/types/index.ts +1 -0
- package/src/types/meta.ts +0 -18
- package/src/types/script.ts +18 -0
- package/src/types/witness.ts +8 -8
package/src/lib/tx/util.ts
CHANGED
|
@@ -1,77 +1,54 @@
|
|
|
1
|
-
import { Buff
|
|
1
|
+
import { Buff } from '@vbyte/buff'
|
|
2
2
|
import { Test } from '@vbyte/micro-lib'
|
|
3
3
|
import { Assert } from '@vbyte/micro-lib/assert'
|
|
4
4
|
import { hash256 } from '@vbyte/micro-lib/hash'
|
|
5
5
|
import { encode_tx } from './encode.js'
|
|
6
|
+
import { parse_tx } from './parse.js'
|
|
6
7
|
import { assert_tx_template } from './validate.js'
|
|
7
8
|
|
|
8
|
-
import { DEFAULT
|
|
9
|
+
import { DEFAULT } from '@/const.js'
|
|
9
10
|
|
|
10
11
|
import type {
|
|
11
12
|
TxData,
|
|
12
13
|
TxOutput,
|
|
13
|
-
TxOutputInfo,
|
|
14
14
|
TxOutputTemplate,
|
|
15
|
-
|
|
16
|
-
TxValue,
|
|
17
|
-
WitnessVersion
|
|
15
|
+
TxValue
|
|
18
16
|
} from '@/types/index.js'
|
|
19
17
|
|
|
20
|
-
export function
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
type : get_vout_script_type(script),
|
|
28
|
-
version : get_vout_script_version(script)
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export function get_vout_script_type (
|
|
33
|
-
script : Bytes
|
|
34
|
-
) : TxOutputType | null {
|
|
35
|
-
const hex = Buff.bytes(script).hex
|
|
36
|
-
for (const [ type, regex ] of Object.entries(LOCK_SCRIPT_REGEX)) {
|
|
37
|
-
if (regex.test(hex)) return type as TxOutputType
|
|
18
|
+
export function get_txid (txdata : string | Uint8Array | TxData) : string {
|
|
19
|
+
// If the transaction data is an object,
|
|
20
|
+
if (typeof txdata === 'object') {
|
|
21
|
+
// Assert the structure of the transaction data is valid.
|
|
22
|
+
assert_tx_template(txdata)
|
|
23
|
+
// Encode the transaction data.
|
|
24
|
+
txdata = encode_tx(txdata, false)
|
|
38
25
|
}
|
|
39
|
-
|
|
26
|
+
// Return the txid of the transaction data.
|
|
27
|
+
return hash256(txdata).reverse().hex
|
|
40
28
|
}
|
|
41
29
|
|
|
42
|
-
export function
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
default : return null
|
|
30
|
+
export function get_txhash (txdata : string | Uint8Array | TxData) : string {
|
|
31
|
+
// If the transaction data is an object,
|
|
32
|
+
if (typeof txdata === 'object') {
|
|
33
|
+
// Assert the structure of the transaction data is valid.
|
|
34
|
+
assert_tx_template(txdata)
|
|
35
|
+
// Encode the transaction data.
|
|
36
|
+
txdata = encode_tx(txdata, true)
|
|
50
37
|
}
|
|
38
|
+
// Return the txhash of the transaction data.
|
|
39
|
+
return hash256(txdata).reverse().hex
|
|
51
40
|
}
|
|
52
41
|
|
|
53
|
-
export function
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
export function get_txhash (
|
|
62
|
-
txdata : TxData
|
|
63
|
-
) : string {
|
|
64
|
-
assert_tx_template(txdata)
|
|
65
|
-
const data = encode_tx(txdata, true)
|
|
66
|
-
return hash256(data).reverse().hex
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
export function get_tx_value (
|
|
70
|
-
txdata : TxData
|
|
71
|
-
) : TxValue {
|
|
72
|
-
const vin = txdata.vin.reduce((acc, txin) => acc + (txin.prevout?.value ?? 0n), 0n)
|
|
73
|
-
const vout = txdata.vout.reduce((acc, txout) => acc + txout.value, 0n)
|
|
42
|
+
export function get_tx_value (txdata : string | Uint8Array | TxData) : TxValue {
|
|
43
|
+
// Parse the transaction data.
|
|
44
|
+
const tx = parse_tx(txdata)
|
|
45
|
+
// Assert the structure of the transaction data is valid.
|
|
46
|
+
assert_tx_template(tx)
|
|
47
|
+
// Calculate the value of the transaction.
|
|
48
|
+
const vin = tx.vin.reduce((acc, txin) => acc + (txin.prevout?.value ?? 0n), 0n)
|
|
49
|
+
const vout = tx.vout.reduce((acc, txout) => acc + txout.value, 0n)
|
|
74
50
|
const fees = (vin > vout) ? (vin - vout) : 0n
|
|
51
|
+
// Return the value of the transaction.
|
|
75
52
|
return { fees, vin, vout }
|
|
76
53
|
}
|
|
77
54
|
|
package/src/lib/witness/parse.ts
CHANGED
|
@@ -4,7 +4,8 @@ import { TAPLEAF_VERSIONS } from '@/const.js'
|
|
|
4
4
|
|
|
5
5
|
import type {
|
|
6
6
|
WitnessData,
|
|
7
|
-
|
|
7
|
+
SpendScriptType,
|
|
8
|
+
WitnessVersion
|
|
8
9
|
} from '@/types/index.js'
|
|
9
10
|
|
|
10
11
|
export function parse_witness (
|
|
@@ -64,13 +65,13 @@ function parse_cblock_data (
|
|
|
64
65
|
|
|
65
66
|
function parse_witness_script (
|
|
66
67
|
elems : Uint8Array[],
|
|
67
|
-
type :
|
|
68
|
+
type : SpendScriptType | null
|
|
68
69
|
) {
|
|
69
70
|
let script : Uint8Array | undefined
|
|
70
71
|
switch (type) {
|
|
71
|
-
case '
|
|
72
|
+
case 'p2ts':
|
|
72
73
|
script = elems.at(-1)
|
|
73
|
-
case '
|
|
74
|
+
case 'p2wsh':
|
|
74
75
|
script = elems.at(-1)
|
|
75
76
|
}
|
|
76
77
|
return (script !== undefined) ? new Buff(script).hex : null
|
|
@@ -79,14 +80,14 @@ function parse_witness_script (
|
|
|
79
80
|
function parse_witness_type (
|
|
80
81
|
elems : Uint8Array[],
|
|
81
82
|
cblock : string | null
|
|
82
|
-
) :
|
|
83
|
+
) : SpendScriptType | null{
|
|
83
84
|
// Get the important elements of the witness.
|
|
84
85
|
let param_0 = elems.at(0),
|
|
85
86
|
param_1 = elems.at(1),
|
|
86
87
|
param_x = elems.at(-1)
|
|
87
88
|
// If the cblock is present and the last element exists:
|
|
88
89
|
if (cblock !== null && param_x !== undefined) {
|
|
89
|
-
return '
|
|
90
|
+
return 'p2ts'
|
|
90
91
|
// If the witness elements match the profile of a p2w-pkh:
|
|
91
92
|
} else if (
|
|
92
93
|
elems.length === 2 &&
|
|
@@ -95,29 +96,32 @@ function parse_witness_type (
|
|
|
95
96
|
param_0.length >= 64 &&
|
|
96
97
|
param_1.length === 33
|
|
97
98
|
) {
|
|
98
|
-
return '
|
|
99
|
+
return 'p2wpkh'
|
|
99
100
|
// If the witness elements match the profile of a p2tr-pk:
|
|
100
101
|
} else if (
|
|
101
102
|
elems.length === 1 &&
|
|
102
103
|
param_0 !== undefined &&
|
|
103
104
|
param_0.length === 64
|
|
104
105
|
) {
|
|
105
|
-
return 'p2tr
|
|
106
|
+
return 'p2tr'
|
|
106
107
|
// If there is at least two witness elements:
|
|
107
108
|
} else if (
|
|
108
109
|
elems.length > 1 &&
|
|
109
110
|
param_x !== undefined &&
|
|
110
111
|
is_valid_script(param_x)
|
|
111
112
|
) {
|
|
112
|
-
return '
|
|
113
|
+
return 'p2wsh'
|
|
113
114
|
// If the witness elements don't match any known profile:
|
|
114
115
|
} else {
|
|
115
|
-
return
|
|
116
|
+
return null
|
|
116
117
|
}
|
|
117
118
|
}
|
|
118
119
|
|
|
119
|
-
function parse_witness_version (
|
|
120
|
-
|
|
121
|
-
|
|
120
|
+
function parse_witness_version (
|
|
121
|
+
type : SpendScriptType | null
|
|
122
|
+
) : WitnessVersion | null {
|
|
123
|
+
if (type === null) return null
|
|
124
|
+
if (type.startsWith('p2w')) return 0
|
|
125
|
+
if (type.startsWith('p2t')) return 1
|
|
122
126
|
return null
|
|
123
127
|
}
|
package/src/types/address.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
+
import type { ScriptInfo } from './script.js'
|
|
2
|
+
|
|
1
3
|
export type AddressFormat = 'base58' | 'bech32' | 'bech32m'
|
|
2
|
-
export type AddressType = 'p2pkh' | 'p2sh' | '
|
|
4
|
+
export type AddressType = 'p2pkh' | 'p2sh' | 'p2wpkh' | 'p2wsh' | 'p2tr'
|
|
3
5
|
export type ChainNetwork = 'main' | 'testnet' | 'regtest' | string
|
|
4
|
-
export type AddressData = AddressContext & ScriptData
|
|
5
6
|
|
|
6
7
|
export type AddressConfigEntry = [
|
|
7
8
|
prefix : string,
|
|
@@ -12,7 +13,7 @@ export type AddressConfigEntry = [
|
|
|
12
13
|
version : number
|
|
13
14
|
]
|
|
14
15
|
|
|
15
|
-
export interface
|
|
16
|
+
export interface EncoderConfig {
|
|
16
17
|
format : AddressFormat
|
|
17
18
|
data : Uint8Array
|
|
18
19
|
prefix? : string
|
|
@@ -28,12 +29,7 @@ export interface AddressConfig {
|
|
|
28
29
|
version : number
|
|
29
30
|
}
|
|
30
31
|
|
|
31
|
-
export interface
|
|
32
|
-
data
|
|
33
|
-
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
export interface ScriptData {
|
|
37
|
-
script_asm : string[]
|
|
38
|
-
script_hex : string
|
|
32
|
+
export interface AddressInfo extends AddressConfig {
|
|
33
|
+
data : string
|
|
34
|
+
script : ScriptInfo
|
|
39
35
|
}
|
package/src/types/index.ts
CHANGED
package/src/types/meta.ts
CHANGED
|
@@ -1,19 +1,6 @@
|
|
|
1
|
-
import type{ WitnessType, WitnessVersion } from './witness.js'
|
|
2
|
-
|
|
3
1
|
export type LocktimeData = LocktimeStamp | LocktimeHeight
|
|
4
2
|
export type SequenceConfig = Partial<SequenceData>
|
|
5
3
|
export type SequenceData = SequenceHeightLock | SequenceStampLock
|
|
6
|
-
export type TxOutputType = 'p2pkh' | 'p2sh' | 'p2w-pkh' | 'p2w-sh' | 'p2tr' | 'opreturn'
|
|
7
|
-
|
|
8
|
-
export interface TxOutputInfo {
|
|
9
|
-
type : TxOutputType | null
|
|
10
|
-
version : WitnessVersion | null
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export interface TxInputInfo {
|
|
14
|
-
type : WitnessType
|
|
15
|
-
version : WitnessVersion
|
|
16
|
-
}
|
|
17
4
|
|
|
18
5
|
export interface LocktimeStamp {
|
|
19
6
|
type : 'timelock' // Discriminator for timelock type
|
|
@@ -49,11 +36,6 @@ export interface SequenceField {
|
|
|
49
36
|
value : number
|
|
50
37
|
}
|
|
51
38
|
|
|
52
|
-
export interface ScriptField {
|
|
53
|
-
asm : string[]
|
|
54
|
-
hex : string
|
|
55
|
-
}
|
|
56
|
-
|
|
57
39
|
export interface InscriptionData {
|
|
58
40
|
content ?: string
|
|
59
41
|
delegate ?: string
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export type LockScriptType = 'p2pkh' | 'p2sh' | 'p2wpkh' | 'p2wsh' | 'p2tr' | 'opreturn'
|
|
2
|
+
export type SpendScriptType = 'p2pkh' | 'p2sh' | 'p2wpkh' | 'p2wsh' | 'p2tr' | 'p2ts'
|
|
3
|
+
export type WitnessVersion = 0 | 1 | null
|
|
4
|
+
|
|
5
|
+
export interface ScriptInfo {
|
|
6
|
+
asm : string[]
|
|
7
|
+
hex : string
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export interface LockScriptInfo {
|
|
11
|
+
type : LockScriptType | null
|
|
12
|
+
version : WitnessVersion | null
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface SpendScriptInfo {
|
|
16
|
+
type : SpendScriptType
|
|
17
|
+
version : WitnessVersion
|
|
18
|
+
}
|
package/src/types/witness.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
+
import type { SpendScriptType, WitnessVersion } from './script.js'
|
|
2
|
+
|
|
1
3
|
export type WitnessContext = WitnessData | TaprootScript | SegwitScript | TaprootSpend | SegwitSpend
|
|
2
|
-
export type WitnessVersion = number | null
|
|
3
|
-
export type WitnessType = 'p2w-pkh' | 'p2w-sh' | 'p2tr-pk' | 'p2tr-ts' | 'unknown'
|
|
4
4
|
|
|
5
5
|
export interface WitnessSize {
|
|
6
6
|
total : number
|
|
@@ -13,30 +13,30 @@ export interface WitnessData {
|
|
|
13
13
|
params : string[]
|
|
14
14
|
script : string | null
|
|
15
15
|
stack : string[]
|
|
16
|
-
type :
|
|
17
|
-
version : WitnessVersion
|
|
16
|
+
type : SpendScriptType | null
|
|
17
|
+
version : WitnessVersion | null
|
|
18
18
|
}
|
|
19
19
|
|
|
20
20
|
export interface TaprootScript extends WitnessData {
|
|
21
21
|
cblock : string
|
|
22
22
|
script : string
|
|
23
|
-
type : '
|
|
23
|
+
type : 'p2ts'
|
|
24
24
|
version : 1
|
|
25
25
|
}
|
|
26
26
|
|
|
27
27
|
export interface SegwitScript extends WitnessData {
|
|
28
28
|
script : string
|
|
29
|
-
type : '
|
|
29
|
+
type : 'p2wsh'
|
|
30
30
|
version : 0
|
|
31
31
|
}
|
|
32
32
|
|
|
33
33
|
export interface TaprootSpend extends WitnessData {
|
|
34
|
-
type : 'p2tr
|
|
34
|
+
type : 'p2tr'
|
|
35
35
|
version : 1
|
|
36
36
|
}
|
|
37
37
|
|
|
38
38
|
export interface SegwitSpend extends WitnessData {
|
|
39
|
-
type : '
|
|
39
|
+
type : 'p2wpkh'
|
|
40
40
|
version : 0
|
|
41
41
|
}
|
|
42
42
|
|