@vbyte/btc-dev 1.1.3 → 1.1.5
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 +3 -116
- package/dist/lib/script/index.js +3 -6
- package/dist/lib/script/lock.d.ts +12 -0
- package/dist/lib/script/lock.js +52 -0
- package/dist/lib/tx/util.d.ts +1 -6
- package/dist/lib/tx/util.js +1 -27
- package/dist/lib/witness/parse.js +11 -9
- package/dist/main.cjs +345 -194
- package/dist/main.cjs.map +1 -1
- package/dist/module.mjs +345 -194
- package/dist/module.mjs.map +1 -1
- package/dist/package.json +1 -1
- package/dist/script.js +8 -8
- 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 +1 -1
- 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 +7 -13
- package/src/lib/script/lock.ts +73 -0
- package/src/lib/tx/util.ts +3 -41
- 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/address/p2tr.ts
CHANGED
|
@@ -1,39 +1,68 @@
|
|
|
1
|
-
import { Buff }
|
|
2
|
-
import { Assert }
|
|
3
|
-
import { encode_address }
|
|
1
|
+
import { Buff, Bytes } from '@vbyte/buff'
|
|
2
|
+
import { Assert } from '@vbyte/micro-lib'
|
|
3
|
+
import { encode_address } from './encode.js'
|
|
4
|
+
import { is_p2tr_script } from '@/lib/script/lock.js'
|
|
5
|
+
import { LOCK_SCRIPT_TYPE } from '@/const.js'
|
|
4
6
|
|
|
5
7
|
import {
|
|
6
8
|
get_address_config,
|
|
7
|
-
|
|
9
|
+
get_address_info
|
|
8
10
|
} from './util.js'
|
|
9
11
|
|
|
10
12
|
import type {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
+
AddressInfo,
|
|
14
|
+
ChainNetwork
|
|
13
15
|
} from '@/types/index.js'
|
|
14
16
|
|
|
15
|
-
const
|
|
17
|
+
const ADDRESS_TYPE = LOCK_SCRIPT_TYPE.P2TR
|
|
16
18
|
|
|
17
19
|
export namespace P2TR {
|
|
18
|
-
export const
|
|
19
|
-
export const
|
|
20
|
+
export const create_address = create_p2tr_address
|
|
21
|
+
export const create_script = create_p2tr_script
|
|
22
|
+
export const encode_address = encode_p2tr_address
|
|
23
|
+
export const encode_script = encode_p2tr_script
|
|
24
|
+
export const decode_address = decode_p2tr_address
|
|
25
|
+
export const decode_script = decode_p2tr_script
|
|
20
26
|
}
|
|
21
27
|
|
|
22
|
-
function
|
|
23
|
-
pubkey :
|
|
24
|
-
network : ChainNetwork = 'main'
|
|
28
|
+
function create_p2tr_address (
|
|
29
|
+
pubkey : Bytes,
|
|
30
|
+
network : ChainNetwork = 'main',
|
|
25
31
|
) : string {
|
|
32
|
+
// Create the p2tr script.
|
|
33
|
+
const script = create_p2tr_script(pubkey)
|
|
34
|
+
// Encode the script as an address.
|
|
35
|
+
return encode_p2tr_address(script, network)
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function create_p2tr_script (pubkey : Bytes) : Buff {
|
|
26
39
|
// Convert the public key into bytes.
|
|
27
40
|
const bytes = Buff.bytes(pubkey)
|
|
41
|
+
// Assert the public key is 32 bytes.
|
|
42
|
+
Assert.size(bytes, 32, 'invalid pubkey size')
|
|
43
|
+
// Return the script.
|
|
44
|
+
return encode_p2tr_script(bytes)
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function encode_p2tr_script (pubkey : Bytes) : Buff {
|
|
48
|
+
return Buff.join([ '5120', pubkey ])
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function encode_p2tr_address (
|
|
52
|
+
script_pk : Bytes,
|
|
53
|
+
network : ChainNetwork = 'main'
|
|
54
|
+
) : string {
|
|
55
|
+
// Get the public key from the script.
|
|
56
|
+
const pubkey = decode_p2tr_script(script_pk)
|
|
28
57
|
// Get the address configuration.
|
|
29
|
-
const config = get_address_config(network,
|
|
58
|
+
const config = get_address_config(network, ADDRESS_TYPE)
|
|
30
59
|
// Assert the configuration exists.
|
|
31
|
-
Assert.exists(config, `unrecognized address config: ${
|
|
60
|
+
Assert.exists(config, `unrecognized address config: ${ADDRESS_TYPE} on ${network}` )
|
|
32
61
|
// Assert the payload size is correct.
|
|
33
|
-
Assert.size(
|
|
62
|
+
Assert.size(pubkey, config.size, `invalid payload size: ${pubkey.length} !== ${config.size}` )
|
|
34
63
|
// Encode the address.
|
|
35
64
|
return encode_address({
|
|
36
|
-
data :
|
|
65
|
+
data : pubkey,
|
|
37
66
|
format : 'bech32m',
|
|
38
67
|
prefix : config.prefix
|
|
39
68
|
})
|
|
@@ -41,11 +70,22 @@ function encode_p2tr_address (
|
|
|
41
70
|
|
|
42
71
|
function decode_p2tr_address (
|
|
43
72
|
address : string
|
|
44
|
-
) :
|
|
73
|
+
) : AddressInfo {
|
|
45
74
|
// Parse the address.
|
|
46
|
-
const parsed =
|
|
75
|
+
const parsed = get_address_info(address)
|
|
47
76
|
// Assert the address type is correct.
|
|
48
|
-
Assert.ok(parsed.type === 'p2tr', `address type mismatch: ${parsed.type} !== ${
|
|
77
|
+
Assert.ok(parsed.type === 'p2tr', `address type mismatch: ${parsed.type} !== ${ADDRESS_TYPE}`)
|
|
49
78
|
// Return the parsed address.
|
|
50
79
|
return parsed
|
|
51
80
|
}
|
|
81
|
+
|
|
82
|
+
function decode_p2tr_script (
|
|
83
|
+
script : Bytes,
|
|
84
|
+
) : Buff {
|
|
85
|
+
// Assert the script is a p2tr script.
|
|
86
|
+
Assert.ok(is_p2tr_script(script), `invalid p2tr script`)
|
|
87
|
+
// Convert the script into bytes.
|
|
88
|
+
const bytes = Buff.bytes(script)
|
|
89
|
+
// Return the public key from the script.
|
|
90
|
+
return bytes.slice(2, 34)
|
|
91
|
+
}
|
|
@@ -1,55 +1,71 @@
|
|
|
1
|
-
import { Buff }
|
|
2
|
-
import { Assert }
|
|
3
|
-
import { hash160 }
|
|
4
|
-
import { encode_address }
|
|
1
|
+
import { Buff, Bytes } from '@vbyte/buff'
|
|
2
|
+
import { Assert } from '@vbyte/micro-lib'
|
|
3
|
+
import { hash160 } from '@vbyte/micro-lib/hash'
|
|
4
|
+
import { encode_address } from './encode.js'
|
|
5
|
+
import { is_p2wpkh_script } from '@/lib/script/lock.js'
|
|
6
|
+
import { LOCK_SCRIPT_TYPE } from '@/const.js'
|
|
5
7
|
|
|
6
8
|
import {
|
|
7
9
|
get_address_config,
|
|
8
|
-
|
|
10
|
+
get_address_info
|
|
9
11
|
} from './util.js'
|
|
10
12
|
|
|
11
13
|
import type {
|
|
12
|
-
|
|
14
|
+
AddressInfo,
|
|
13
15
|
ChainNetwork
|
|
14
16
|
} from '@/types/index.js'
|
|
15
17
|
|
|
16
|
-
const
|
|
18
|
+
const ADDRESS_TYPE = LOCK_SCRIPT_TYPE.P2WPKH
|
|
17
19
|
|
|
18
20
|
export namespace P2WPKH {
|
|
19
|
-
export const
|
|
20
|
-
export const
|
|
21
|
-
export const
|
|
21
|
+
export const create_address = create_p2wpkh_address
|
|
22
|
+
export const create_script = create_p2wpkh_script
|
|
23
|
+
export const encode_address = encode_p2wpkh_address
|
|
24
|
+
export const encode_script = encode_p2wpkh_script
|
|
25
|
+
export const decode_address = decode_p2wpkh_address
|
|
26
|
+
export const decode_script = decode_p2wpkh_script
|
|
22
27
|
}
|
|
23
28
|
|
|
24
29
|
function create_p2wpkh_address (
|
|
25
|
-
pubkey :
|
|
30
|
+
pubkey : Bytes,
|
|
26
31
|
network : ChainNetwork = 'main',
|
|
27
32
|
) : string {
|
|
33
|
+
// Create the p2wpkh script.
|
|
34
|
+
const script = create_p2wpkh_script(pubkey)
|
|
35
|
+
// Encode the script as an address.
|
|
36
|
+
return encode_p2wpkh_address(script, network)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function create_p2wpkh_script (pubkey : Bytes) : Buff {
|
|
28
40
|
// Convert the public key into bytes.
|
|
29
41
|
const bytes = Buff.bytes(pubkey)
|
|
30
|
-
// Assert the
|
|
31
|
-
Assert.size(bytes, 33,
|
|
42
|
+
// Assert the public key is 33 bytes.
|
|
43
|
+
Assert.size(bytes, 33, 'invalid pubkey size')
|
|
32
44
|
// Convert the bytes into a hash.
|
|
33
45
|
const hash = hash160(bytes)
|
|
34
|
-
//
|
|
35
|
-
return
|
|
46
|
+
// Return the script.
|
|
47
|
+
return encode_p2wpkh_script(hash)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function encode_p2wpkh_script (pk_hash : Bytes) : Buff {
|
|
51
|
+
return Buff.join([ '0014', pk_hash ])
|
|
36
52
|
}
|
|
37
53
|
|
|
38
54
|
function encode_p2wpkh_address (
|
|
39
|
-
|
|
40
|
-
network
|
|
55
|
+
script_pk : Bytes,
|
|
56
|
+
network : ChainNetwork = 'main',
|
|
41
57
|
) : string {
|
|
42
|
-
//
|
|
43
|
-
const
|
|
58
|
+
// Get the public key hash from the script.
|
|
59
|
+
const pk_hash = decode_p2wpkh_script(script_pk)
|
|
44
60
|
// Get the address configuration.
|
|
45
|
-
const config = get_address_config(network,
|
|
61
|
+
const config = get_address_config(network, ADDRESS_TYPE)
|
|
46
62
|
// Assert the configuration exists.
|
|
47
|
-
Assert.exists(config, `unrecognized address config: ${
|
|
63
|
+
Assert.exists(config, `unrecognized address config: ${ADDRESS_TYPE} on ${network}` )
|
|
48
64
|
// Assert the payload size is correct.
|
|
49
|
-
Assert.size(
|
|
65
|
+
Assert.size(pk_hash, config.size, `invalid payload size: ${pk_hash.length} !== ${config.size}` )
|
|
50
66
|
// Encode the address.
|
|
51
67
|
return encode_address({
|
|
52
|
-
data :
|
|
68
|
+
data : pk_hash,
|
|
53
69
|
format : 'bech32',
|
|
54
70
|
prefix : config.prefix
|
|
55
71
|
})
|
|
@@ -57,11 +73,22 @@ function encode_p2wpkh_address (
|
|
|
57
73
|
|
|
58
74
|
function decode_p2wpkh_address (
|
|
59
75
|
address : string
|
|
60
|
-
) :
|
|
76
|
+
) : AddressInfo {
|
|
61
77
|
// Parse the address.
|
|
62
|
-
const parsed =
|
|
78
|
+
const parsed = get_address_info(address)
|
|
63
79
|
// Assert the address type is correct.
|
|
64
|
-
Assert.ok(parsed.type === '
|
|
80
|
+
Assert.ok(parsed.type === 'p2wpkh', `address type mismatch: ${parsed.type} !== ${ADDRESS_TYPE}`)
|
|
65
81
|
// Return the parsed address.
|
|
66
82
|
return parsed
|
|
67
83
|
}
|
|
84
|
+
|
|
85
|
+
function decode_p2wpkh_script (
|
|
86
|
+
script : Bytes,
|
|
87
|
+
) : Buff {
|
|
88
|
+
// Assert the script is a p2wpkh script.
|
|
89
|
+
Assert.ok(is_p2wpkh_script(script), `invalid p2wpkh script`)
|
|
90
|
+
// Convert the script into bytes.
|
|
91
|
+
const bytes = Buff.bytes(script)
|
|
92
|
+
// Return the public key hash from the script.
|
|
93
|
+
return bytes.slice(2, 22)
|
|
94
|
+
}
|
package/src/lib/address/p2wsh.ts
CHANGED
|
@@ -1,53 +1,69 @@
|
|
|
1
|
-
import { Buff }
|
|
2
|
-
import { Assert }
|
|
3
|
-
import { sha256 }
|
|
4
|
-
import { encode_address }
|
|
1
|
+
import { Buff, Bytes } from '@vbyte/buff'
|
|
2
|
+
import { Assert } from '@vbyte/micro-lib'
|
|
3
|
+
import { sha256 } from '@vbyte/micro-lib/hash'
|
|
4
|
+
import { encode_address } from './encode.js'
|
|
5
|
+
import { is_p2wsh_script } from '@/lib/script/lock.js'
|
|
6
|
+
import { LOCK_SCRIPT_TYPE } from '@/const.js'
|
|
5
7
|
|
|
6
8
|
import {
|
|
7
9
|
get_address_config,
|
|
8
|
-
|
|
10
|
+
get_address_info
|
|
9
11
|
} from './util.js'
|
|
10
12
|
|
|
11
13
|
import type {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
+
AddressInfo,
|
|
15
|
+
ChainNetwork
|
|
14
16
|
} from '@/types/index.js'
|
|
15
17
|
|
|
16
|
-
const
|
|
18
|
+
const ADDRESS_TYPE = LOCK_SCRIPT_TYPE.P2WSH
|
|
17
19
|
|
|
18
20
|
export namespace P2WSH {
|
|
19
|
-
export const
|
|
20
|
-
export const
|
|
21
|
-
export const
|
|
21
|
+
export const create_address = create_p2wsh_address
|
|
22
|
+
export const create_script = create_p2wsh_script
|
|
23
|
+
export const encode_address = encode_p2wsh_address
|
|
24
|
+
export const encode_script = encode_p2wsh_script
|
|
25
|
+
export const decode_address = decode_p2wsh_address
|
|
26
|
+
export const decode_script = decode_p2wsh_script
|
|
22
27
|
}
|
|
23
28
|
|
|
24
29
|
function create_p2wsh_address (
|
|
25
|
-
script :
|
|
30
|
+
script : Bytes,
|
|
26
31
|
network : ChainNetwork = 'main',
|
|
27
32
|
) : string {
|
|
33
|
+
// Create the p2wsh script.
|
|
34
|
+
const wsh_script = create_p2wsh_script(script)
|
|
35
|
+
// Encode the script as an address.
|
|
36
|
+
return encode_p2wsh_address(wsh_script, network)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function create_p2wsh_script (script : Bytes) : Buff {
|
|
28
40
|
// Convert the script into bytes.
|
|
29
41
|
const bytes = Buff.bytes(script)
|
|
30
42
|
// Convert the bytes into a hash.
|
|
31
|
-
const hash
|
|
32
|
-
//
|
|
33
|
-
return
|
|
43
|
+
const hash = sha256(bytes)
|
|
44
|
+
// Return the script.
|
|
45
|
+
return encode_p2wsh_script(hash)
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function encode_p2wsh_script (script_hash : Bytes) : Buff {
|
|
49
|
+
return Buff.join([ '0020', script_hash ])
|
|
34
50
|
}
|
|
35
51
|
|
|
36
52
|
function encode_p2wsh_address (
|
|
37
|
-
|
|
38
|
-
network
|
|
53
|
+
script_pk : Bytes,
|
|
54
|
+
network : ChainNetwork = 'main',
|
|
39
55
|
) : string {
|
|
40
|
-
//
|
|
41
|
-
const
|
|
56
|
+
// Get the script hash from the script.
|
|
57
|
+
const script_hash = decode_p2wsh_script(script_pk)
|
|
42
58
|
// Get the address configuration.
|
|
43
|
-
const config = get_address_config(network,
|
|
59
|
+
const config = get_address_config(network, ADDRESS_TYPE)
|
|
44
60
|
// Assert the configuration exists.
|
|
45
|
-
Assert.exists(config, `unrecognized address config: ${
|
|
61
|
+
Assert.exists(config, `unrecognized address config: ${ADDRESS_TYPE} on ${network}` )
|
|
46
62
|
// Assert the payload size is correct.
|
|
47
|
-
Assert.size(
|
|
63
|
+
Assert.size(script_hash, config.size, `invalid payload size: ${script_hash.length} !== ${config.size}` )
|
|
48
64
|
// Encode the address.
|
|
49
65
|
return encode_address({
|
|
50
|
-
data :
|
|
66
|
+
data : script_hash,
|
|
51
67
|
format : 'bech32',
|
|
52
68
|
prefix : config.prefix
|
|
53
69
|
})
|
|
@@ -55,11 +71,22 @@ function encode_p2wsh_address (
|
|
|
55
71
|
|
|
56
72
|
function decode_p2wsh_address (
|
|
57
73
|
address : string
|
|
58
|
-
) :
|
|
74
|
+
) : AddressInfo {
|
|
59
75
|
// Parse the address.
|
|
60
|
-
const parsed =
|
|
76
|
+
const parsed = get_address_info(address)
|
|
61
77
|
// Assert the address type is correct.
|
|
62
|
-
Assert.ok(parsed.type === '
|
|
78
|
+
Assert.ok(parsed.type === 'p2wsh', `address type mismatch: ${parsed.type} !== ${ADDRESS_TYPE}`)
|
|
63
79
|
// Return the parsed address.
|
|
64
80
|
return parsed
|
|
65
81
|
}
|
|
82
|
+
|
|
83
|
+
function decode_p2wsh_script (
|
|
84
|
+
script : Bytes,
|
|
85
|
+
) : Buff {
|
|
86
|
+
// Assert the script is a p2wsh script.
|
|
87
|
+
Assert.ok(is_p2wsh_script(script), `invalid p2wsh script`)
|
|
88
|
+
// Convert the script into bytes.
|
|
89
|
+
const bytes = Buff.bytes(script)
|
|
90
|
+
// Return the script hash from the script.
|
|
91
|
+
return bytes.slice(2, 34)
|
|
92
|
+
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { AddressType } from '@/types/index.js'
|
|
1
|
+
import type { AddressType, ScriptInfo } from '@/types/index.js'
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Get the address script.
|
|
@@ -10,15 +10,15 @@ import type { AddressType } from '@/types/index.js'
|
|
|
10
10
|
export function get_address_script (
|
|
11
11
|
script_key : string,
|
|
12
12
|
script_type : AddressType
|
|
13
|
-
) {
|
|
13
|
+
) : ScriptInfo {
|
|
14
14
|
switch (script_type) {
|
|
15
15
|
case 'p2pkh':
|
|
16
16
|
return get_p2pkh_script(script_key)
|
|
17
17
|
case 'p2sh':
|
|
18
18
|
return get_p2sh_script(script_key)
|
|
19
|
-
case '
|
|
19
|
+
case 'p2wpkh':
|
|
20
20
|
return get_p2w_pkh_script(script_key)
|
|
21
|
-
case '
|
|
21
|
+
case 'p2wsh':
|
|
22
22
|
return get_p2w_sh_script(script_key)
|
|
23
23
|
case 'p2tr':
|
|
24
24
|
return get_p2tr_script(script_key)
|
|
@@ -29,35 +29,35 @@ export function get_address_script (
|
|
|
29
29
|
|
|
30
30
|
function get_p2pkh_script (script_key : string) {
|
|
31
31
|
return {
|
|
32
|
-
|
|
33
|
-
|
|
32
|
+
hex : '76a914' + script_key + '88ac',
|
|
33
|
+
asm : [ 'OP_DUP', 'OP_HASH160', script_key, 'OP_EQUALVERIFY', 'OP_CHECKSIG' ]
|
|
34
34
|
}
|
|
35
35
|
}
|
|
36
36
|
|
|
37
37
|
function get_p2sh_script (script_key : string) {
|
|
38
38
|
return {
|
|
39
|
-
|
|
40
|
-
|
|
39
|
+
hex : 'a914' + script_key + '87',
|
|
40
|
+
asm : [ 'OP_HASH160', script_key, 'OP_EQUAL' ]
|
|
41
41
|
}
|
|
42
42
|
}
|
|
43
43
|
|
|
44
44
|
function get_p2w_pkh_script (script_key : string) {
|
|
45
45
|
return {
|
|
46
|
-
|
|
47
|
-
|
|
46
|
+
hex : '0014' + script_key,
|
|
47
|
+
asm : [ 'OP_0', script_key ]
|
|
48
48
|
}
|
|
49
49
|
}
|
|
50
50
|
|
|
51
51
|
function get_p2w_sh_script (script_key : string) {
|
|
52
52
|
return {
|
|
53
|
-
|
|
54
|
-
|
|
53
|
+
hex : '0020' + script_key,
|
|
54
|
+
asm : [ 'OP_0', script_key ]
|
|
55
55
|
}
|
|
56
56
|
}
|
|
57
57
|
|
|
58
58
|
function get_p2tr_script (script_key : string) {
|
|
59
59
|
return {
|
|
60
|
-
|
|
61
|
-
|
|
60
|
+
hex : '5120' + script_key,
|
|
61
|
+
asm : [ 'OP_1', script_key ]
|
|
62
62
|
}
|
|
63
63
|
}
|
package/src/lib/address/util.ts
CHANGED
|
@@ -5,10 +5,9 @@ import { get_address_script } from './script.js'
|
|
|
5
5
|
import type {
|
|
6
6
|
AddressConfig,
|
|
7
7
|
AddressConfigEntry,
|
|
8
|
-
|
|
8
|
+
AddressInfo,
|
|
9
9
|
ChainNetwork,
|
|
10
|
-
|
|
11
|
-
AddressData
|
|
10
|
+
LockScriptType,
|
|
12
11
|
} from '@/types/index.js'
|
|
13
12
|
|
|
14
13
|
const CONFIG_TABLE : AddressConfigEntry[] = [
|
|
@@ -20,12 +19,12 @@ const CONFIG_TABLE : AddressConfigEntry[] = [
|
|
|
20
19
|
[ 'm', 'p2pkh', 'regtest', 20, 'base58', 0x6F ],
|
|
21
20
|
[ 'n', 'p2pkh', 'regtest', 20, 'base58', 0x6F ],
|
|
22
21
|
[ '2', 'p2sh', 'regtest', 20, 'base58', 0xC4 ],
|
|
23
|
-
[ 'bc', '
|
|
24
|
-
[ 'tb', '
|
|
25
|
-
[ 'bcrt', '
|
|
26
|
-
[ 'bc', '
|
|
27
|
-
[ 'tb', '
|
|
28
|
-
[ 'bcrt', '
|
|
22
|
+
[ 'bc', 'p2wpkh', 'main', 20, 'bech32', 0 ],
|
|
23
|
+
[ 'tb', 'p2wpkh', 'testnet', 20, 'bech32', 0 ],
|
|
24
|
+
[ 'bcrt', 'p2wpkh', 'regtest', 20, 'bech32', 0 ],
|
|
25
|
+
[ 'bc', 'p2wsh', 'main', 32, 'bech32', 0 ],
|
|
26
|
+
[ 'tb', 'p2wsh', 'testnet', 32, 'bech32', 0 ],
|
|
27
|
+
[ 'bcrt', 'p2wsh', 'regtest', 32, 'bech32', 0 ],
|
|
29
28
|
[ 'bc', 'p2tr', 'main', 32, 'bech32m', 1 ],
|
|
30
29
|
[ 'tb', 'p2tr', 'testnet', 32, 'bech32m', 1 ],
|
|
31
30
|
[ 'bcrt', 'p2tr', 'regtest', 32, 'bech32m', 1 ]
|
|
@@ -40,7 +39,7 @@ const CONFIG_TABLE : AddressConfigEntry[] = [
|
|
|
40
39
|
*/
|
|
41
40
|
export function get_address_config (
|
|
42
41
|
address_network : ChainNetwork,
|
|
43
|
-
address_type :
|
|
42
|
+
address_type : LockScriptType
|
|
44
43
|
) : AddressConfig | null {
|
|
45
44
|
// For each configuration in the table,
|
|
46
45
|
for (const [ prefix, type, network, size, format, version ] of CONFIG_TABLE) {
|
|
@@ -55,12 +54,12 @@ export function get_address_config (
|
|
|
55
54
|
}
|
|
56
55
|
|
|
57
56
|
/**
|
|
58
|
-
*
|
|
57
|
+
* Parse an address into its data and script.
|
|
59
58
|
*
|
|
60
|
-
* @param address - The address to
|
|
61
|
-
* @returns The address
|
|
59
|
+
* @param address - The address to parse.
|
|
60
|
+
* @returns The address data and script.
|
|
62
61
|
*/
|
|
63
|
-
export function
|
|
62
|
+
export function get_address_info (address : string) : AddressInfo {
|
|
64
63
|
// Decode the address.
|
|
65
64
|
const dec = decode_address(address)
|
|
66
65
|
// For each configuration in the table,
|
|
@@ -78,25 +77,11 @@ export function get_address_ctx (address : string) : AddressContext {
|
|
|
78
77
|
}
|
|
79
78
|
|
|
80
79
|
// Convert the decoded data into a hex string.
|
|
81
|
-
const
|
|
80
|
+
const data = Buff.uint(dec.data).hex
|
|
81
|
+
const script = get_address_script(data, type)
|
|
82
82
|
// Return the address configuration and data.
|
|
83
|
-
return { data
|
|
83
|
+
return { data, script,type, prefix, network, size, format, version }
|
|
84
84
|
}
|
|
85
85
|
// Otherwise, throw an error
|
|
86
86
|
throw new Error('address configuration is invalid')
|
|
87
87
|
}
|
|
88
|
-
|
|
89
|
-
/**
|
|
90
|
-
* Parse an address into its data and script.
|
|
91
|
-
*
|
|
92
|
-
* @param address - The address to parse.
|
|
93
|
-
* @returns The address data and script.
|
|
94
|
-
*/
|
|
95
|
-
export function parse_address (address : string) : AddressData {
|
|
96
|
-
// Get the address context.
|
|
97
|
-
const ctx = get_address_ctx(address)
|
|
98
|
-
// Get the address script.
|
|
99
|
-
const script = get_address_script(ctx.hex, ctx.type)
|
|
100
|
-
// Return the address data.
|
|
101
|
-
return { ...ctx, ...script }
|
|
102
|
-
}
|
package/src/lib/script/decode.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Bytes, Stream } from '@vbyte/buff'
|
|
1
|
+
import { Buff, Bytes, Stream } from '@vbyte/buff'
|
|
2
2
|
|
|
3
3
|
import {
|
|
4
4
|
get_op_code,
|
|
@@ -6,6 +6,16 @@ import {
|
|
|
6
6
|
is_valid_op
|
|
7
7
|
} from './words.js'
|
|
8
8
|
|
|
9
|
+
import type { ScriptInfo } from '@/types/script.js'
|
|
10
|
+
|
|
11
|
+
export function parse_script (script: Bytes): ScriptInfo {
|
|
12
|
+
const bytes = Buff.bytes(script)
|
|
13
|
+
return {
|
|
14
|
+
asm: decode_script(bytes),
|
|
15
|
+
hex: bytes.hex
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
9
19
|
/**
|
|
10
20
|
* Decode a bitcoin script into asm instructions.
|
|
11
21
|
*/
|
package/src/lib/script/index.ts
CHANGED
|
@@ -1,26 +1,20 @@
|
|
|
1
|
-
import { OPCODE_MAP } from './words.js'
|
|
2
1
|
import { encode_script } from './encode.js'
|
|
3
2
|
|
|
4
3
|
import {
|
|
5
4
|
decode_script,
|
|
6
|
-
is_valid_script
|
|
5
|
+
is_valid_script,
|
|
6
|
+
parse_script
|
|
7
7
|
} from './decode.js'
|
|
8
8
|
|
|
9
|
-
import {
|
|
10
|
-
parse_script_pubkeys,
|
|
11
|
-
prefix_script_size
|
|
12
|
-
} from './util.js'
|
|
13
|
-
|
|
14
9
|
export * from './decode.js'
|
|
15
10
|
export * from './encode.js'
|
|
11
|
+
export * from './lock.js'
|
|
16
12
|
export * from './util.js'
|
|
17
13
|
export * from './words.js'
|
|
18
14
|
|
|
19
15
|
export namespace ScriptUtil {
|
|
20
|
-
export const
|
|
21
|
-
export const decode
|
|
22
|
-
export const encode
|
|
23
|
-
export const is_valid
|
|
24
|
-
export const get_pubkeys = parse_script_pubkeys
|
|
25
|
-
export const OPCODES = OPCODE_MAP
|
|
16
|
+
export const parse = parse_script
|
|
17
|
+
export const decode = decode_script
|
|
18
|
+
export const encode = encode_script
|
|
19
|
+
export const is_valid = is_valid_script
|
|
26
20
|
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { Buff, Bytes } from '@vbyte/buff'
|
|
2
|
+
import { LOCK_SCRIPT_REGEX } from '@/const.js'
|
|
3
|
+
|
|
4
|
+
import type {
|
|
5
|
+
LockScriptInfo,
|
|
6
|
+
LockScriptType,
|
|
7
|
+
WitnessVersion
|
|
8
|
+
} from '@/types/index.js'
|
|
9
|
+
|
|
10
|
+
export function is_return_script (script : Bytes) : boolean {
|
|
11
|
+
const bytes = Buff.bytes(script)
|
|
12
|
+
return bytes.at(0) === 0x6a
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function get_lock_script_info (script : Bytes) : LockScriptInfo {
|
|
16
|
+
return {
|
|
17
|
+
type : get_lock_script_type(script),
|
|
18
|
+
version : get_lock_script_version(script)
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function get_lock_script_type (script : Bytes) : LockScriptType | null {
|
|
23
|
+
// Get the hex string of the script.
|
|
24
|
+
const hex = Buff.bytes(script).hex
|
|
25
|
+
// Iterate over the lock script regexes.
|
|
26
|
+
for (const [ type, regex ] of Object.entries(LOCK_SCRIPT_REGEX)) {
|
|
27
|
+
// If the script matches the regex, return the type.
|
|
28
|
+
if (regex.test(hex)) return type as LockScriptType
|
|
29
|
+
}
|
|
30
|
+
// If the script does not match any regex, return null.
|
|
31
|
+
return null
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function get_lock_script_version (script : Bytes) : WitnessVersion | null {
|
|
35
|
+
// Get the version of the script.
|
|
36
|
+
const version = Buff.bytes(script)
|
|
37
|
+
// Return the version of the script.
|
|
38
|
+
switch (version.at(0)) {
|
|
39
|
+
case 0x00 : return 0
|
|
40
|
+
case 0x51 : return 1
|
|
41
|
+
default : return null
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function is_p2pkh_script (script : Bytes) : boolean {
|
|
46
|
+
const hex = Buff.bytes(script).hex
|
|
47
|
+
return LOCK_SCRIPT_REGEX['p2pkh'].test(hex)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function is_p2sh_script (script : Bytes) : boolean {
|
|
51
|
+
const hex = Buff.bytes(script).hex
|
|
52
|
+
return LOCK_SCRIPT_REGEX['p2sh'].test(hex)
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function is_p2wpkh_script (script : Bytes) : boolean {
|
|
56
|
+
const hex = Buff.bytes(script).hex
|
|
57
|
+
return LOCK_SCRIPT_REGEX['p2wpkh'].test(hex)
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function is_p2wsh_script (script : Bytes) : boolean {
|
|
61
|
+
const hex = Buff.bytes(script).hex
|
|
62
|
+
return LOCK_SCRIPT_REGEX['p2wsh'].test(hex)
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export function is_p2tr_script (script : Bytes) : boolean {
|
|
66
|
+
const hex = Buff.bytes(script).hex
|
|
67
|
+
return LOCK_SCRIPT_REGEX['p2tr'].test(hex)
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function is_opreturn_script (script : Bytes) : boolean {
|
|
71
|
+
const hex = Buff.bytes(script).hex
|
|
72
|
+
return LOCK_SCRIPT_REGEX['opreturn'].test(hex)
|
|
73
|
+
}
|