@vbyte/btc-dev 1.1.7 → 2.0.0
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/CHANGELOG.md +94 -0
- package/README.md +260 -3
- package/dist/const.d.ts +3 -0
- package/dist/const.js +23 -22
- package/dist/index.d.ts +11 -11
- package/dist/index.js +10 -10
- package/dist/lib/address/api.d.ts +2 -2
- package/dist/lib/address/api.js +12 -12
- package/dist/lib/address/encode.d.ts +1 -1
- package/dist/lib/address/encode.js +24 -24
- package/dist/lib/address/index.d.ts +6 -6
- package/dist/lib/address/index.js +6 -6
- package/dist/lib/address/p2pkh.d.ts +2 -2
- package/dist/lib/address/p2pkh.js +14 -14
- package/dist/lib/address/p2sh.d.ts +2 -2
- package/dist/lib/address/p2sh.js +13 -13
- package/dist/lib/address/p2tr.d.ts +2 -2
- package/dist/lib/address/p2tr.js +13 -13
- package/dist/lib/address/p2wpkh.d.ts +2 -2
- package/dist/lib/address/p2wpkh.js +14 -14
- package/dist/lib/address/p2wsh.d.ts +2 -2
- package/dist/lib/address/p2wsh.js +13 -13
- package/dist/lib/address/script.d.ts +1 -1
- package/dist/lib/address/script.js +16 -16
- package/dist/lib/address/util.d.ts +1 -1
- package/dist/lib/address/util.js +22 -22
- package/dist/lib/meta/index.d.ts +4 -4
- package/dist/lib/meta/index.js +4 -4
- package/dist/lib/meta/locktime.d.ts +1 -1
- package/dist/lib/meta/locktime.js +12 -12
- package/dist/lib/meta/ref.js +9 -6
- package/dist/lib/meta/scribe.d.ts +2 -2
- package/dist/lib/meta/scribe.js +48 -53
- package/dist/lib/meta/sequence.d.ts +1 -1
- package/dist/lib/meta/sequence.js +16 -15
- package/dist/lib/script/decode.d.ts +2 -2
- package/dist/lib/script/decode.js +50 -15
- package/dist/lib/script/encode.d.ts +1 -1
- package/dist/lib/script/encode.js +20 -16
- package/dist/lib/script/index.d.ts +5 -13
- package/dist/lib/script/index.js +5 -14
- package/dist/lib/script/lock.d.ts +2 -2
- package/dist/lib/script/lock.js +15 -12
- package/dist/lib/script/util.js +4 -4
- package/dist/lib/script/words.js +129 -129
- package/dist/lib/sighash/index.d.ts +3 -3
- package/dist/lib/sighash/index.js +3 -3
- package/dist/lib/sighash/segwit.d.ts +2 -2
- package/dist/lib/sighash/segwit.js +15 -14
- package/dist/lib/sighash/taproot.d.ts +2 -2
- package/dist/lib/sighash/taproot.js +24 -23
- package/dist/lib/sighash/util.d.ts +2 -2
- package/dist/lib/sighash/util.js +7 -7
- package/dist/lib/signer/index.d.ts +2 -2
- package/dist/lib/signer/index.js +2 -2
- package/dist/lib/signer/sign.d.ts +1 -1
- package/dist/lib/signer/sign.js +42 -7
- package/dist/lib/signer/verify.d.ts +17 -3
- package/dist/lib/signer/verify.js +233 -3
- package/dist/lib/taproot/cblock.d.ts +1 -1
- package/dist/lib/taproot/cblock.js +14 -16
- package/dist/lib/taproot/encode.d.ts +1 -1
- package/dist/lib/taproot/encode.js +7 -7
- package/dist/lib/taproot/index.d.ts +4 -4
- package/dist/lib/taproot/index.js +4 -4
- package/dist/lib/taproot/parse.d.ts +1 -1
- package/dist/lib/taproot/parse.js +12 -14
- package/dist/lib/taproot/tree.d.ts +2 -2
- package/dist/lib/taproot/tree.js +11 -7
- package/dist/lib/tx/create.d.ts +1 -1
- package/dist/lib/tx/create.js +28 -12
- package/dist/lib/tx/decode.d.ts +2 -2
- package/dist/lib/tx/decode.js +50 -17
- package/dist/lib/tx/encode.d.ts +2 -2
- package/dist/lib/tx/encode.js +13 -16
- package/dist/lib/tx/index.d.ts +7 -7
- package/dist/lib/tx/index.js +7 -7
- package/dist/lib/tx/parse.d.ts +1 -1
- package/dist/lib/tx/parse.js +9 -9
- package/dist/lib/tx/size.d.ts +2 -2
- package/dist/lib/tx/size.js +9 -9
- package/dist/lib/tx/util.d.ts +2 -2
- package/dist/lib/tx/util.js +23 -22
- package/dist/lib/tx/validate.d.ts +1 -1
- package/dist/lib/tx/validate.js +3 -3
- package/dist/lib/witness/index.d.ts +2 -2
- package/dist/lib/witness/index.js +2 -2
- package/dist/lib/witness/parse.d.ts +2 -2
- package/dist/lib/witness/parse.js +24 -23
- package/dist/lib/witness/util.d.ts +2 -2
- package/dist/lib/witness/util.js +5 -5
- package/dist/main.cjs +2308 -1007
- package/dist/main.cjs.map +1 -1
- package/dist/module.mjs +2308 -1007
- package/dist/module.mjs.map +1 -1
- package/dist/package.json +20 -17
- package/dist/schema/base.d.ts +1 -1
- package/dist/schema/base.js +17 -13
- package/dist/schema/index.d.ts +2 -2
- package/dist/schema/index.js +2 -2
- package/dist/schema/taproot.d.ts +1 -1
- package/dist/schema/taproot.js +2 -2
- package/dist/schema/tx.d.ts +1 -1
- package/dist/schema/tx.js +4 -4
- package/dist/script.js +8 -8
- package/dist/script.js.map +1 -1
- package/dist/types/address.d.ts +4 -4
- package/dist/types/index.d.ts +8 -8
- package/dist/types/index.js +8 -8
- package/dist/types/meta.d.ts +4 -4
- package/dist/types/psbt.d.ts +2 -2
- package/dist/types/script.d.ts +2 -2
- package/dist/types/sighash.d.ts +2 -2
- package/dist/types/witness.d.ts +5 -5
- package/package.json +20 -17
- package/src/const.ts +0 -61
- package/src/index.ts +0 -13
- package/src/lib/address/api.ts +0 -50
- package/src/lib/address/encode.ts +0 -183
- package/src/lib/address/index.ts +0 -7
- package/src/lib/address/p2pkh.ts +0 -94
- package/src/lib/address/p2sh.ts +0 -96
- package/src/lib/address/p2tr.ts +0 -91
- package/src/lib/address/p2wpkh.ts +0 -94
- package/src/lib/address/p2wsh.ts +0 -92
- package/src/lib/address/script.ts +0 -63
- package/src/lib/address/util.ts +0 -87
- package/src/lib/meta/index.ts +0 -4
- package/src/lib/meta/locktime.ts +0 -57
- package/src/lib/meta/ref.ts +0 -107
- package/src/lib/meta/scribe.ts +0 -256
- package/src/lib/meta/sequence.ts +0 -146
- package/src/lib/script/decode.ts +0 -85
- package/src/lib/script/encode.ts +0 -129
- package/src/lib/script/index.ts +0 -20
- package/src/lib/script/lock.ts +0 -73
- package/src/lib/script/util.ts +0 -78
- package/src/lib/script/words.ts +0 -182
- package/src/lib/sighash/index.ts +0 -3
- package/src/lib/sighash/segwit.ts +0 -152
- package/src/lib/sighash/taproot.ts +0 -206
- package/src/lib/sighash/util.ts +0 -51
- package/src/lib/signer/index.ts +0 -2
- package/src/lib/signer/sign.ts +0 -39
- package/src/lib/signer/verify.ts +0 -88
- package/src/lib/taproot/cblock.ts +0 -96
- package/src/lib/taproot/encode.ts +0 -49
- package/src/lib/taproot/index.ts +0 -4
- package/src/lib/taproot/parse.ts +0 -65
- package/src/lib/taproot/tree.ts +0 -94
- package/src/lib/tx/create.ts +0 -90
- package/src/lib/tx/decode.ts +0 -123
- package/src/lib/tx/encode.ts +0 -155
- package/src/lib/tx/index.ts +0 -7
- package/src/lib/tx/parse.ts +0 -69
- package/src/lib/tx/size.ts +0 -68
- package/src/lib/tx/util.ts +0 -113
- package/src/lib/tx/validate.ts +0 -49
- package/src/lib/witness/index.ts +0 -2
- package/src/lib/witness/parse.ts +0 -127
- package/src/lib/witness/util.ts +0 -18
- package/src/schema/base.ts +0 -57
- package/src/schema/index.ts +0 -2
- package/src/schema/taproot.ts +0 -12
- package/src/schema/tx.ts +0 -48
- package/src/types/address.ts +0 -35
- package/src/types/index.ts +0 -8
- package/src/types/meta.ts +0 -48
- package/src/types/psbt.ts +0 -15
- package/src/types/script.ts +0 -18
- package/src/types/sighash.ts +0 -16
- package/src/types/taproot.ts +0 -41
- package/src/types/txdata.ts +0 -85
- package/src/types/witness.ts +0 -42
package/src/lib/address/p2sh.ts
DELETED
|
@@ -1,96 +0,0 @@
|
|
|
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_p2sh_script } from '@/lib/script/lock.js'
|
|
6
|
-
import { LOCK_SCRIPT_TYPE } from '@/const.js'
|
|
7
|
-
|
|
8
|
-
import {
|
|
9
|
-
get_address_config,
|
|
10
|
-
get_address_info
|
|
11
|
-
} from './util.js'
|
|
12
|
-
|
|
13
|
-
import type {
|
|
14
|
-
AddressInfo,
|
|
15
|
-
ChainNetwork
|
|
16
|
-
} from '@/types/index.js'
|
|
17
|
-
|
|
18
|
-
const ADDRESS_TYPE = LOCK_SCRIPT_TYPE.P2SH
|
|
19
|
-
|
|
20
|
-
export namespace P2SH {
|
|
21
|
-
export const create_address = create_p2sh_address
|
|
22
|
-
export const create_script = create_p2sh_script
|
|
23
|
-
export const encode_address = encode_p2sh_address
|
|
24
|
-
export const encode_script = encode_p2sh_script
|
|
25
|
-
export const decode_address = decode_p2sh_address
|
|
26
|
-
export const decode_script = decode_p2sh_script
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
function create_p2sh_address (
|
|
30
|
-
script : Bytes,
|
|
31
|
-
network : ChainNetwork = 'main',
|
|
32
|
-
) : string {
|
|
33
|
-
// Convert the script into bytes.
|
|
34
|
-
const bytes = Buff.bytes(script)
|
|
35
|
-
// Convert the bytes into a hash.
|
|
36
|
-
const hash = hash160(bytes)
|
|
37
|
-
// Create the p2sh script.
|
|
38
|
-
const p2sh_script = encode_p2sh_script(hash)
|
|
39
|
-
// Encode the address.
|
|
40
|
-
return encode_p2sh_address(p2sh_script, network)
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
function create_p2sh_script (script : Bytes) : Buff {
|
|
44
|
-
// Convert the script into bytes.
|
|
45
|
-
const bytes = Buff.bytes(script)
|
|
46
|
-
// Convert the bytes into a hash.
|
|
47
|
-
const hash = hash160(bytes)
|
|
48
|
-
// Return the script.
|
|
49
|
-
return encode_p2sh_script(hash)
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
function encode_p2sh_script (script_hash : Bytes) : Buff {
|
|
53
|
-
return Buff.join([ 'a914', script_hash, '87' ])
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
function encode_p2sh_address (
|
|
57
|
-
script_pk : Bytes,
|
|
58
|
-
network : ChainNetwork = 'main',
|
|
59
|
-
) : string {
|
|
60
|
-
// Get the script hash from the script.
|
|
61
|
-
const script_hash = decode_p2sh_script(script_pk)
|
|
62
|
-
// Get the address configuration.
|
|
63
|
-
const config = get_address_config(network, ADDRESS_TYPE)
|
|
64
|
-
// Assert the configuration exists.
|
|
65
|
-
Assert.exists(config, `unrecognized address config: ${ADDRESS_TYPE} on ${network}` )
|
|
66
|
-
// Assert the payload size is correct.
|
|
67
|
-
Assert.size(script_hash, config.size, `invalid payload size: ${script_hash.length} !== ${config.size}` )
|
|
68
|
-
// Encode the address.
|
|
69
|
-
return encode_address({
|
|
70
|
-
data : script_hash,
|
|
71
|
-
format : 'base58',
|
|
72
|
-
version : config.version
|
|
73
|
-
})
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
function decode_p2sh_address (
|
|
77
|
-
address : string
|
|
78
|
-
) : AddressInfo {
|
|
79
|
-
// Parse the address.
|
|
80
|
-
const parsed = get_address_info(address)
|
|
81
|
-
// Assert the address type is correct.
|
|
82
|
-
Assert.ok(parsed.type === 'p2sh', `address type mismatch: ${parsed.type} !== ${ADDRESS_TYPE}`)
|
|
83
|
-
// Return the parsed address.
|
|
84
|
-
return parsed
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
function decode_p2sh_script (
|
|
88
|
-
script : Bytes,
|
|
89
|
-
) : Buff {
|
|
90
|
-
// Assert the script is a p2sh script.
|
|
91
|
-
Assert.ok(is_p2sh_script(script), `invalid p2sh script`)
|
|
92
|
-
// Convert the script into bytes.
|
|
93
|
-
const bytes = Buff.bytes(script)
|
|
94
|
-
// Return the script hash from the script.
|
|
95
|
-
return bytes.slice(2, 22)
|
|
96
|
-
}
|
package/src/lib/address/p2tr.ts
DELETED
|
@@ -1,91 +0,0 @@
|
|
|
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'
|
|
6
|
-
|
|
7
|
-
import {
|
|
8
|
-
get_address_config,
|
|
9
|
-
get_address_info
|
|
10
|
-
} from './util.js'
|
|
11
|
-
|
|
12
|
-
import type {
|
|
13
|
-
AddressInfo,
|
|
14
|
-
ChainNetwork
|
|
15
|
-
} from '@/types/index.js'
|
|
16
|
-
|
|
17
|
-
const ADDRESS_TYPE = LOCK_SCRIPT_TYPE.P2TR
|
|
18
|
-
|
|
19
|
-
export namespace P2TR {
|
|
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
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
function create_p2tr_address (
|
|
29
|
-
pubkey : Bytes,
|
|
30
|
-
network : ChainNetwork = 'main',
|
|
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 {
|
|
39
|
-
// Convert the public key into bytes.
|
|
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)
|
|
57
|
-
// Get the address configuration.
|
|
58
|
-
const config = get_address_config(network, ADDRESS_TYPE)
|
|
59
|
-
// Assert the configuration exists.
|
|
60
|
-
Assert.exists(config, `unrecognized address config: ${ADDRESS_TYPE} on ${network}` )
|
|
61
|
-
// Assert the payload size is correct.
|
|
62
|
-
Assert.size(pubkey, config.size, `invalid payload size: ${pubkey.length} !== ${config.size}` )
|
|
63
|
-
// Encode the address.
|
|
64
|
-
return encode_address({
|
|
65
|
-
data : pubkey,
|
|
66
|
-
format : 'bech32m',
|
|
67
|
-
prefix : config.prefix
|
|
68
|
-
})
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
function decode_p2tr_address (
|
|
72
|
-
address : string
|
|
73
|
-
) : AddressInfo {
|
|
74
|
-
// Parse the address.
|
|
75
|
-
const parsed = get_address_info(address)
|
|
76
|
-
// Assert the address type is correct.
|
|
77
|
-
Assert.ok(parsed.type === 'p2tr', `address type mismatch: ${parsed.type} !== ${ADDRESS_TYPE}`)
|
|
78
|
-
// Return the parsed address.
|
|
79
|
-
return parsed
|
|
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,94 +0,0 @@
|
|
|
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'
|
|
7
|
-
|
|
8
|
-
import {
|
|
9
|
-
get_address_config,
|
|
10
|
-
get_address_info
|
|
11
|
-
} from './util.js'
|
|
12
|
-
|
|
13
|
-
import type {
|
|
14
|
-
AddressInfo,
|
|
15
|
-
ChainNetwork
|
|
16
|
-
} from '@/types/index.js'
|
|
17
|
-
|
|
18
|
-
const ADDRESS_TYPE = LOCK_SCRIPT_TYPE.P2WPKH
|
|
19
|
-
|
|
20
|
-
export namespace P2WPKH {
|
|
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
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
function create_p2wpkh_address (
|
|
30
|
-
pubkey : Bytes,
|
|
31
|
-
network : ChainNetwork = 'main',
|
|
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 {
|
|
40
|
-
// Convert the public key into bytes.
|
|
41
|
-
const bytes = Buff.bytes(pubkey)
|
|
42
|
-
// Assert the public key is 33 bytes.
|
|
43
|
-
Assert.size(bytes, 33, 'invalid pubkey size')
|
|
44
|
-
// Convert the bytes into a hash.
|
|
45
|
-
const hash = hash160(bytes)
|
|
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 ])
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
function encode_p2wpkh_address (
|
|
55
|
-
script_pk : Bytes,
|
|
56
|
-
network : ChainNetwork = 'main',
|
|
57
|
-
) : string {
|
|
58
|
-
// Get the public key hash from the script.
|
|
59
|
-
const pk_hash = decode_p2wpkh_script(script_pk)
|
|
60
|
-
// Get the address configuration.
|
|
61
|
-
const config = get_address_config(network, ADDRESS_TYPE)
|
|
62
|
-
// Assert the configuration exists.
|
|
63
|
-
Assert.exists(config, `unrecognized address config: ${ADDRESS_TYPE} on ${network}` )
|
|
64
|
-
// Assert the payload size is correct.
|
|
65
|
-
Assert.size(pk_hash, config.size, `invalid payload size: ${pk_hash.length} !== ${config.size}` )
|
|
66
|
-
// Encode the address.
|
|
67
|
-
return encode_address({
|
|
68
|
-
data : pk_hash,
|
|
69
|
-
format : 'bech32',
|
|
70
|
-
prefix : config.prefix
|
|
71
|
-
})
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
function decode_p2wpkh_address (
|
|
75
|
-
address : string
|
|
76
|
-
) : AddressInfo {
|
|
77
|
-
// Parse the address.
|
|
78
|
-
const parsed = get_address_info(address)
|
|
79
|
-
// Assert the address type is correct.
|
|
80
|
-
Assert.ok(parsed.type === 'p2wpkh', `address type mismatch: ${parsed.type} !== ${ADDRESS_TYPE}`)
|
|
81
|
-
// Return the parsed address.
|
|
82
|
-
return parsed
|
|
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
DELETED
|
@@ -1,92 +0,0 @@
|
|
|
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'
|
|
7
|
-
|
|
8
|
-
import {
|
|
9
|
-
get_address_config,
|
|
10
|
-
get_address_info
|
|
11
|
-
} from './util.js'
|
|
12
|
-
|
|
13
|
-
import type {
|
|
14
|
-
AddressInfo,
|
|
15
|
-
ChainNetwork
|
|
16
|
-
} from '@/types/index.js'
|
|
17
|
-
|
|
18
|
-
const ADDRESS_TYPE = LOCK_SCRIPT_TYPE.P2WSH
|
|
19
|
-
|
|
20
|
-
export namespace P2WSH {
|
|
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
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
function create_p2wsh_address (
|
|
30
|
-
script : Bytes,
|
|
31
|
-
network : ChainNetwork = 'main',
|
|
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 {
|
|
40
|
-
// Convert the script into bytes.
|
|
41
|
-
const bytes = Buff.bytes(script)
|
|
42
|
-
// Convert the bytes into a hash.
|
|
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 ])
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
function encode_p2wsh_address (
|
|
53
|
-
script_pk : Bytes,
|
|
54
|
-
network : ChainNetwork = 'main',
|
|
55
|
-
) : string {
|
|
56
|
-
// Get the script hash from the script.
|
|
57
|
-
const script_hash = decode_p2wsh_script(script_pk)
|
|
58
|
-
// Get the address configuration.
|
|
59
|
-
const config = get_address_config(network, ADDRESS_TYPE)
|
|
60
|
-
// Assert the configuration exists.
|
|
61
|
-
Assert.exists(config, `unrecognized address config: ${ADDRESS_TYPE} on ${network}` )
|
|
62
|
-
// Assert the payload size is correct.
|
|
63
|
-
Assert.size(script_hash, config.size, `invalid payload size: ${script_hash.length} !== ${config.size}` )
|
|
64
|
-
// Encode the address.
|
|
65
|
-
return encode_address({
|
|
66
|
-
data : script_hash,
|
|
67
|
-
format : 'bech32',
|
|
68
|
-
prefix : config.prefix
|
|
69
|
-
})
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
function decode_p2wsh_address (
|
|
73
|
-
address : string
|
|
74
|
-
) : AddressInfo {
|
|
75
|
-
// Parse the address.
|
|
76
|
-
const parsed = get_address_info(address)
|
|
77
|
-
// Assert the address type is correct.
|
|
78
|
-
Assert.ok(parsed.type === 'p2wsh', `address type mismatch: ${parsed.type} !== ${ADDRESS_TYPE}`)
|
|
79
|
-
// Return the parsed address.
|
|
80
|
-
return parsed
|
|
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,63 +0,0 @@
|
|
|
1
|
-
import type { AddressType, ScriptInfo } from '@/types/index.js'
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Get the address script.
|
|
5
|
-
*
|
|
6
|
-
* @param script_key - The script key.
|
|
7
|
-
* @param script_type - The script type.
|
|
8
|
-
* @returns The address script.
|
|
9
|
-
*/
|
|
10
|
-
export function get_address_script (
|
|
11
|
-
script_key : string,
|
|
12
|
-
script_type : AddressType
|
|
13
|
-
) : ScriptInfo {
|
|
14
|
-
switch (script_type) {
|
|
15
|
-
case 'p2pkh':
|
|
16
|
-
return get_p2pkh_script(script_key)
|
|
17
|
-
case 'p2sh':
|
|
18
|
-
return get_p2sh_script(script_key)
|
|
19
|
-
case 'p2wpkh':
|
|
20
|
-
return get_p2w_pkh_script(script_key)
|
|
21
|
-
case 'p2wsh':
|
|
22
|
-
return get_p2w_sh_script(script_key)
|
|
23
|
-
case 'p2tr':
|
|
24
|
-
return get_p2tr_script(script_key)
|
|
25
|
-
default:
|
|
26
|
-
throw new Error('unrecognized script type: ' + script_type)
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
function get_p2pkh_script (script_key : string) {
|
|
31
|
-
return {
|
|
32
|
-
hex : '76a914' + script_key + '88ac',
|
|
33
|
-
asm : [ 'OP_DUP', 'OP_HASH160', script_key, 'OP_EQUALVERIFY', 'OP_CHECKSIG' ]
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
function get_p2sh_script (script_key : string) {
|
|
38
|
-
return {
|
|
39
|
-
hex : 'a914' + script_key + '87',
|
|
40
|
-
asm : [ 'OP_HASH160', script_key, 'OP_EQUAL' ]
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
function get_p2w_pkh_script (script_key : string) {
|
|
45
|
-
return {
|
|
46
|
-
hex : '0014' + script_key,
|
|
47
|
-
asm : [ 'OP_0', script_key ]
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
function get_p2w_sh_script (script_key : string) {
|
|
52
|
-
return {
|
|
53
|
-
hex : '0020' + script_key,
|
|
54
|
-
asm : [ 'OP_0', script_key ]
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
function get_p2tr_script (script_key : string) {
|
|
59
|
-
return {
|
|
60
|
-
hex : '5120' + script_key,
|
|
61
|
-
asm : [ 'OP_1', script_key ]
|
|
62
|
-
}
|
|
63
|
-
}
|
package/src/lib/address/util.ts
DELETED
|
@@ -1,87 +0,0 @@
|
|
|
1
|
-
import { Buff } from '@vbyte/buff'
|
|
2
|
-
import { decode_address } from './encode.js'
|
|
3
|
-
import { get_address_script } from './script.js'
|
|
4
|
-
|
|
5
|
-
import type {
|
|
6
|
-
AddressConfig,
|
|
7
|
-
AddressConfigEntry,
|
|
8
|
-
AddressInfo,
|
|
9
|
-
ChainNetwork,
|
|
10
|
-
LockScriptType,
|
|
11
|
-
} from '@/types/index.js'
|
|
12
|
-
|
|
13
|
-
const CONFIG_TABLE : AddressConfigEntry[] = [
|
|
14
|
-
[ '1', 'p2pkh', 'main', 20, 'base58', 0x00 ],
|
|
15
|
-
[ '3', 'p2sh', 'main', 20, 'base58', 0x05 ],
|
|
16
|
-
[ 'm', 'p2pkh', 'testnet', 20, 'base58', 0x6F ],
|
|
17
|
-
[ 'n', 'p2pkh', 'testnet', 20, 'base58', 0x6F ],
|
|
18
|
-
[ '2', 'p2sh', 'testnet', 20, 'base58', 0xC4 ],
|
|
19
|
-
[ 'm', 'p2pkh', 'regtest', 20, 'base58', 0x6F ],
|
|
20
|
-
[ 'n', 'p2pkh', 'regtest', 20, 'base58', 0x6F ],
|
|
21
|
-
[ '2', 'p2sh', 'regtest', 20, 'base58', 0xC4 ],
|
|
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 ],
|
|
28
|
-
[ 'bc', 'p2tr', 'main', 32, 'bech32m', 1 ],
|
|
29
|
-
[ 'tb', 'p2tr', 'testnet', 32, 'bech32m', 1 ],
|
|
30
|
-
[ 'bcrt', 'p2tr', 'regtest', 32, 'bech32m', 1 ]
|
|
31
|
-
]
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Lookup an address configuration by its type and network.
|
|
35
|
-
*
|
|
36
|
-
* @param address_network - The network of the address.
|
|
37
|
-
* @param address_type - The type of the address.
|
|
38
|
-
* @returns The address information, or null if the address is not recognized.
|
|
39
|
-
*/
|
|
40
|
-
export function get_address_config (
|
|
41
|
-
address_network : ChainNetwork,
|
|
42
|
-
address_type : LockScriptType
|
|
43
|
-
) : AddressConfig | null {
|
|
44
|
-
// For each configuration in the table,
|
|
45
|
-
for (const [ prefix, type, network, size, format, version ] of CONFIG_TABLE) {
|
|
46
|
-
// Check if the address matches the configuration
|
|
47
|
-
if (type === address_type && network === address_network) {
|
|
48
|
-
// Return the address configuration.
|
|
49
|
-
return { type, prefix, network, size, format, version }
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
// If no configuration matches the address, return null.
|
|
53
|
-
return null
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* Parse an address into its data and script.
|
|
58
|
-
*
|
|
59
|
-
* @param address - The address to parse.
|
|
60
|
-
* @returns The address data and script.
|
|
61
|
-
*/
|
|
62
|
-
export function get_address_info (address : string) : AddressInfo {
|
|
63
|
-
// Decode the address.
|
|
64
|
-
const dec = decode_address(address)
|
|
65
|
-
// For each configuration in the table,
|
|
66
|
-
for (const [ prefix, type, network, size, format, version ] of CONFIG_TABLE) {
|
|
67
|
-
|
|
68
|
-
// Check if the address matches the configuration
|
|
69
|
-
if (format !== dec.format) continue
|
|
70
|
-
if (size !== dec.data.length) continue
|
|
71
|
-
if (version !== dec.version) continue
|
|
72
|
-
|
|
73
|
-
if (dec.prefix) {
|
|
74
|
-
if (prefix !== dec.prefix) continue
|
|
75
|
-
} else {
|
|
76
|
-
if (!address.startsWith(prefix)) continue
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
// Convert the decoded data into a hex string.
|
|
80
|
-
const data = Buff.uint(dec.data).hex
|
|
81
|
-
const script = get_address_script(data, type)
|
|
82
|
-
// Return the address configuration and data.
|
|
83
|
-
return { data, script,type, prefix, network, size, format, version }
|
|
84
|
-
}
|
|
85
|
-
// Otherwise, throw an error
|
|
86
|
-
throw new Error('address configuration is invalid')
|
|
87
|
-
}
|
package/src/lib/meta/index.ts
DELETED
package/src/lib/meta/locktime.ts
DELETED
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
import { Assert } from '@vbyte/micro-lib'
|
|
2
|
-
|
|
3
|
-
import type { LocktimeData } from '@/types/index.js'
|
|
4
|
-
|
|
5
|
-
// The threshold between block height and timestamp.
|
|
6
|
-
const LOCKTIME_THRESHOLD = 500000000
|
|
7
|
-
|
|
8
|
-
export namespace LocktimeField {
|
|
9
|
-
export const encode = encode_locktime
|
|
10
|
-
export const decode = decode_locktime
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Encodes a LockTimeData object into a string representation.
|
|
15
|
-
* According to BIP-65, the value is simply the numeric value as a string.
|
|
16
|
-
*/
|
|
17
|
-
export function encode_locktime (
|
|
18
|
-
locktime : LocktimeData
|
|
19
|
-
) : number {
|
|
20
|
-
switch (locktime.type) {
|
|
21
|
-
case 'timelock':
|
|
22
|
-
Assert.ok(locktime.stamp >= LOCKTIME_THRESHOLD, 'Invalid timestamp')
|
|
23
|
-
return locktime.stamp
|
|
24
|
-
case 'heightlock':
|
|
25
|
-
Assert.ok(locktime.height > 0, 'height must be greater than 0')
|
|
26
|
-
Assert.ok(locktime.height < LOCKTIME_THRESHOLD, 'invalid block height')
|
|
27
|
-
return locktime.height
|
|
28
|
-
default:
|
|
29
|
-
throw new Error('Invalid locktime type')
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Parses a string or number into a LockTimeData object.
|
|
35
|
-
* According to BIP-65, values below LOCKTIME_THRESHOLD are interpreted as block heights,
|
|
36
|
-
* while values at or above this threshold are interpreted as timestamps.
|
|
37
|
-
*/
|
|
38
|
-
export function decode_locktime (
|
|
39
|
-
locktime : number
|
|
40
|
-
) : LocktimeData | null {
|
|
41
|
-
// Check if the value is valid (non-negative)
|
|
42
|
-
if (isNaN(locktime) || locktime <= 0) {
|
|
43
|
-
return null
|
|
44
|
-
}
|
|
45
|
-
// Return the appropriate locktime type.
|
|
46
|
-
if (locktime < LOCKTIME_THRESHOLD) {
|
|
47
|
-
return {
|
|
48
|
-
type : 'heightlock',
|
|
49
|
-
height : locktime
|
|
50
|
-
}
|
|
51
|
-
} else {
|
|
52
|
-
return {
|
|
53
|
-
type : 'timelock',
|
|
54
|
-
stamp : locktime
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
}
|
package/src/lib/meta/ref.ts
DELETED
|
@@ -1,107 +0,0 @@
|
|
|
1
|
-
export namespace RefPointer {
|
|
2
|
-
export const outpoint = {
|
|
3
|
-
encode : encode_outpoint,
|
|
4
|
-
decode : decode_outpoint,
|
|
5
|
-
verify : verify_outpoint,
|
|
6
|
-
assert : assert_outpoint,
|
|
7
|
-
}
|
|
8
|
-
export const record_id = {
|
|
9
|
-
encode : encode_inscription_id,
|
|
10
|
-
decode : decode_inscription_id,
|
|
11
|
-
verify : verify_inscription_id,
|
|
12
|
-
assert : assert_inscription_id,
|
|
13
|
-
}
|
|
14
|
-
export const rune_id = {
|
|
15
|
-
encode : encode_rune_id,
|
|
16
|
-
decode : decode_rune_id,
|
|
17
|
-
verify : verify_rune_id,
|
|
18
|
-
assert : assert_rune_id,
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
function encode_inscription_id (
|
|
23
|
-
txid : string,
|
|
24
|
-
order : number = 0
|
|
25
|
-
) : string {
|
|
26
|
-
return `${txid}i${order}`
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
function decode_inscription_id (
|
|
30
|
-
inscription_id : string
|
|
31
|
-
) : { txid : string, order : number } {
|
|
32
|
-
assert_inscription_id(inscription_id)
|
|
33
|
-
const [ txid, order ] = inscription_id.split('i')
|
|
34
|
-
return { txid, order : parseInt(order) }
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
function verify_inscription_id (
|
|
38
|
-
inscription_id : string
|
|
39
|
-
) : boolean {
|
|
40
|
-
return inscription_id.match(/^[a-f0-9]{64}i\d+$/) !== null
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
function assert_inscription_id (
|
|
44
|
-
inscription_id : string
|
|
45
|
-
) : void {
|
|
46
|
-
if (!verify_inscription_id(inscription_id)) {
|
|
47
|
-
throw new Error(`invalid inscription id: ${inscription_id}`)
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
function encode_rune_id (
|
|
52
|
-
block_height : number,
|
|
53
|
-
block_index : number
|
|
54
|
-
) : string {
|
|
55
|
-
return `${block_height}:${block_index}`
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
function decode_rune_id (
|
|
59
|
-
rune_id : string
|
|
60
|
-
) : { block_height : number, block_index : number } {
|
|
61
|
-
assert_rune_id(rune_id)
|
|
62
|
-
const [ block_height, block_index ] = rune_id.split(':')
|
|
63
|
-
return { block_height : parseInt(block_height), block_index : parseInt(block_index) }
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
function verify_rune_id (
|
|
67
|
-
rune_id : string
|
|
68
|
-
) : boolean {
|
|
69
|
-
return rune_id.match(/^\d+:\d+$/) !== null
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
function assert_rune_id (
|
|
73
|
-
rune_id : string
|
|
74
|
-
) : void {
|
|
75
|
-
if (!verify_rune_id(rune_id)) {
|
|
76
|
-
throw new Error(`invalid rune id: ${rune_id}`)
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
function encode_outpoint (
|
|
81
|
-
txid : string,
|
|
82
|
-
vout : number
|
|
83
|
-
) : string {
|
|
84
|
-
return `${txid}:${vout}`
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
function decode_outpoint (
|
|
88
|
-
outpoint : string
|
|
89
|
-
) : { txid : string, vout : number } {
|
|
90
|
-
assert_outpoint(outpoint)
|
|
91
|
-
const [ txid, vout ] = outpoint.split(':')
|
|
92
|
-
return { txid, vout : parseInt(vout) }
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
function verify_outpoint (
|
|
96
|
-
outpoint : string
|
|
97
|
-
) : boolean {
|
|
98
|
-
return outpoint.match(/^[a-f0-9]{64}:[0-9]+$/) !== null
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
function assert_outpoint (
|
|
102
|
-
outpoint : string
|
|
103
|
-
) : void {
|
|
104
|
-
if (!verify_outpoint(outpoint)) {
|
|
105
|
-
throw new Error(`invalid outpoint: ${outpoint}`)
|
|
106
|
-
}
|
|
107
|
-
}
|