@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/dist/types/address.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
+
import type { ScriptInfo } from './script.js';
|
|
1
2
|
export type AddressFormat = 'base58' | 'bech32' | 'bech32m';
|
|
2
|
-
export type AddressType = 'p2pkh' | 'p2sh' | '
|
|
3
|
+
export type AddressType = 'p2pkh' | 'p2sh' | 'p2wpkh' | 'p2wsh' | 'p2tr';
|
|
3
4
|
export type ChainNetwork = 'main' | 'testnet' | 'regtest' | string;
|
|
4
|
-
export type AddressData = AddressContext & ScriptData;
|
|
5
5
|
export type AddressConfigEntry = [
|
|
6
6
|
prefix: string,
|
|
7
7
|
type: AddressType,
|
|
@@ -10,7 +10,7 @@ export type AddressConfigEntry = [
|
|
|
10
10
|
format: AddressFormat,
|
|
11
11
|
version: number
|
|
12
12
|
];
|
|
13
|
-
export interface
|
|
13
|
+
export interface EncoderConfig {
|
|
14
14
|
format: AddressFormat;
|
|
15
15
|
data: Uint8Array;
|
|
16
16
|
prefix?: string;
|
|
@@ -24,11 +24,7 @@ export interface AddressConfig {
|
|
|
24
24
|
type: AddressType;
|
|
25
25
|
version: number;
|
|
26
26
|
}
|
|
27
|
-
export interface
|
|
28
|
-
data:
|
|
29
|
-
|
|
30
|
-
}
|
|
31
|
-
export interface ScriptData {
|
|
32
|
-
script_asm: string[];
|
|
33
|
-
script_hex: string;
|
|
27
|
+
export interface AddressInfo extends AddressConfig {
|
|
28
|
+
data: string;
|
|
29
|
+
script: ScriptInfo;
|
|
34
30
|
}
|
package/dist/types/index.d.ts
CHANGED
package/dist/types/index.js
CHANGED
package/dist/types/meta.d.ts
CHANGED
|
@@ -1,16 +1,6 @@
|
|
|
1
|
-
import type { WitnessType, WitnessVersion } from './witness.js';
|
|
2
1
|
export type LocktimeData = LocktimeStamp | LocktimeHeight;
|
|
3
2
|
export type SequenceConfig = Partial<SequenceData>;
|
|
4
3
|
export type SequenceData = SequenceHeightLock | SequenceStampLock;
|
|
5
|
-
export type TxOutputType = 'p2pkh' | 'p2sh' | 'p2w-pkh' | 'p2w-sh' | 'p2tr' | 'opreturn';
|
|
6
|
-
export interface TxOutputInfo {
|
|
7
|
-
type: TxOutputType | null;
|
|
8
|
-
version: WitnessVersion | null;
|
|
9
|
-
}
|
|
10
|
-
export interface TxInputInfo {
|
|
11
|
-
type: WitnessType;
|
|
12
|
-
version: WitnessVersion;
|
|
13
|
-
}
|
|
14
4
|
export interface LocktimeStamp {
|
|
15
5
|
type: 'timelock';
|
|
16
6
|
stamp: number;
|
|
@@ -37,10 +27,6 @@ export interface SequenceField {
|
|
|
37
27
|
data: SequenceData | null;
|
|
38
28
|
value: number;
|
|
39
29
|
}
|
|
40
|
-
export interface ScriptField {
|
|
41
|
-
asm: string[];
|
|
42
|
-
hex: string;
|
|
43
|
-
}
|
|
44
30
|
export interface InscriptionData {
|
|
45
31
|
content?: string;
|
|
46
32
|
delegate?: string;
|
|
@@ -0,0 +1,15 @@
|
|
|
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
|
+
export interface ScriptInfo {
|
|
5
|
+
asm: string[];
|
|
6
|
+
hex: string;
|
|
7
|
+
}
|
|
8
|
+
export interface LockScriptInfo {
|
|
9
|
+
type: LockScriptType | null;
|
|
10
|
+
version: WitnessVersion | null;
|
|
11
|
+
}
|
|
12
|
+
export interface SpendScriptInfo {
|
|
13
|
+
type: SpendScriptType;
|
|
14
|
+
version: WitnessVersion;
|
|
15
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/types/witness.d.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
+
import type { SpendScriptType, WitnessVersion } from './script.js';
|
|
1
2
|
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
3
|
export interface WitnessSize {
|
|
5
4
|
total: number;
|
|
6
5
|
vsize: number;
|
|
@@ -11,25 +10,25 @@ export interface WitnessData {
|
|
|
11
10
|
params: string[];
|
|
12
11
|
script: string | null;
|
|
13
12
|
stack: string[];
|
|
14
|
-
type:
|
|
15
|
-
version: WitnessVersion;
|
|
13
|
+
type: SpendScriptType | null;
|
|
14
|
+
version: WitnessVersion | null;
|
|
16
15
|
}
|
|
17
16
|
export interface TaprootScript extends WitnessData {
|
|
18
17
|
cblock: string;
|
|
19
18
|
script: string;
|
|
20
|
-
type: '
|
|
19
|
+
type: 'p2ts';
|
|
21
20
|
version: 1;
|
|
22
21
|
}
|
|
23
22
|
export interface SegwitScript extends WitnessData {
|
|
24
23
|
script: string;
|
|
25
|
-
type: '
|
|
24
|
+
type: 'p2wsh';
|
|
26
25
|
version: 0;
|
|
27
26
|
}
|
|
28
27
|
export interface TaprootSpend extends WitnessData {
|
|
29
|
-
type: 'p2tr
|
|
28
|
+
type: 'p2tr';
|
|
30
29
|
version: 1;
|
|
31
30
|
}
|
|
32
31
|
export interface SegwitSpend extends WitnessData {
|
|
33
|
-
type: '
|
|
32
|
+
type: 'p2wpkh';
|
|
34
33
|
version: 0;
|
|
35
34
|
}
|
package/package.json
CHANGED
package/src/const.ts
CHANGED
|
@@ -20,14 +20,32 @@ export const TAPLEAF_VERSIONS = [
|
|
|
20
20
|
|
|
21
21
|
export const TAPLEAF_DEFAULT_VERSION = 0xc0
|
|
22
22
|
|
|
23
|
+
export const LOCK_SCRIPT_TYPE = {
|
|
24
|
+
P2PKH : 'p2pkh',
|
|
25
|
+
P2SH : 'p2sh',
|
|
26
|
+
P2WPKH : 'p2wpkh',
|
|
27
|
+
P2WSH : 'p2wsh',
|
|
28
|
+
P2TR : 'p2tr',
|
|
29
|
+
OPRETURN : 'opreturn',
|
|
30
|
+
} as const
|
|
31
|
+
|
|
32
|
+
export const SPEND_SCRIPT_TYPE = {
|
|
33
|
+
P2PKH : 'p2pkh',
|
|
34
|
+
P2SH : 'p2sh',
|
|
35
|
+
P2WPKH : 'p2wpkh',
|
|
36
|
+
P2WSH : 'p2wsh',
|
|
37
|
+
P2TR : 'p2tr',
|
|
38
|
+
P2TS : 'p2ts',
|
|
39
|
+
} as const
|
|
40
|
+
|
|
23
41
|
export const LOCK_SCRIPT_REGEX : Record<string, RegExp> = {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
}
|
|
42
|
+
[LOCK_SCRIPT_TYPE.P2PKH] : /^76a914[0-9a-f]{40}88ac$/i,
|
|
43
|
+
[LOCK_SCRIPT_TYPE.P2SH] : /^a914[0-9a-f]{40}87$/i,
|
|
44
|
+
[LOCK_SCRIPT_TYPE.P2WPKH] : /^0014[0-9a-f]{40}$/i,
|
|
45
|
+
[LOCK_SCRIPT_TYPE.P2WSH] : /^0020[0-9a-f]{64}$/i,
|
|
46
|
+
[LOCK_SCRIPT_TYPE.P2TR] : /^5120[0-9a-f]{64}$/i,
|
|
47
|
+
[LOCK_SCRIPT_TYPE.OPRETURN] : /^6a[0-9a-f]{2,}$/i,
|
|
48
|
+
} as const
|
|
31
49
|
|
|
32
50
|
export const SCRIPT_INT_KEY = ''
|
|
33
51
|
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { Buff, Bytes } from '@vbyte/buff'
|
|
2
|
+
import { get_lock_script_type } from '@/lib/script/lock.js'
|
|
3
|
+
import { get_address_info } from './util.js'
|
|
4
|
+
import { LOCK_SCRIPT_TYPE } from '@/const.js'
|
|
5
|
+
|
|
6
|
+
import { P2PKH } from './p2pkh.js'
|
|
7
|
+
import { P2SH } from './p2sh.js'
|
|
8
|
+
import { P2TR } from './p2tr.js'
|
|
9
|
+
import { P2WPKH } from './p2wpkh.js'
|
|
10
|
+
import { P2WSH } from './p2wsh.js'
|
|
11
|
+
|
|
12
|
+
import type { AddressInfo, ChainNetwork } from '@/types/index.js'
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Parse an address into its data and script.
|
|
16
|
+
*
|
|
17
|
+
* @param address - The address to parse.
|
|
18
|
+
* @returns The address data and script.
|
|
19
|
+
*/
|
|
20
|
+
export function create_address (
|
|
21
|
+
script : Bytes,
|
|
22
|
+
network : ChainNetwork = 'main'
|
|
23
|
+
) : string {
|
|
24
|
+
// Convert the script into bytes.
|
|
25
|
+
const bytes = Buff.bytes(script)
|
|
26
|
+
// Get the address configuration.
|
|
27
|
+
const type = get_lock_script_type(bytes)
|
|
28
|
+
// If the script type is not recognized, throw an error.
|
|
29
|
+
if (type === null) throw new Error('unrecognized script type: ' + bytes.hex)
|
|
30
|
+
// Create the address based on the script type.
|
|
31
|
+
switch (type) {
|
|
32
|
+
case LOCK_SCRIPT_TYPE.P2PKH:
|
|
33
|
+
return P2PKH.create_address(script, network)
|
|
34
|
+
case LOCK_SCRIPT_TYPE.P2SH:
|
|
35
|
+
return P2SH.create_address(script, network)
|
|
36
|
+
case LOCK_SCRIPT_TYPE.P2WPKH:
|
|
37
|
+
return P2WPKH.create_address(script, network)
|
|
38
|
+
case LOCK_SCRIPT_TYPE.P2WSH:
|
|
39
|
+
return P2WSH.create_address(script, network)
|
|
40
|
+
case LOCK_SCRIPT_TYPE.P2TR:
|
|
41
|
+
return P2TR.create_address(script, network)
|
|
42
|
+
default:
|
|
43
|
+
// If the script type is not recognized, throw an error.
|
|
44
|
+
throw new Error('unrecognized script type: ' + type)
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function parse_address (address : string) : AddressInfo {
|
|
49
|
+
return get_address_info(address)
|
|
50
|
+
}
|
|
@@ -4,7 +4,7 @@ import { Assert, B58chk, Bech32, Bech32m } from '@vbyte/micro-lib'
|
|
|
4
4
|
|
|
5
5
|
import type {
|
|
6
6
|
AddressFormat,
|
|
7
|
-
|
|
7
|
+
EncoderConfig,
|
|
8
8
|
} from '@/types/address.js'
|
|
9
9
|
|
|
10
10
|
const ENCODING_REGEX = {
|
|
@@ -24,7 +24,7 @@ const VERSION = {
|
|
|
24
24
|
* @param address - The address to decode.
|
|
25
25
|
* @returns The decoded address.
|
|
26
26
|
*/
|
|
27
|
-
export function decode_address (address : string) :
|
|
27
|
+
export function decode_address (address : string) : EncoderConfig {
|
|
28
28
|
// Get the address format.
|
|
29
29
|
const format = get_address_format(address)
|
|
30
30
|
// If the format is not found, throw an error.
|
|
@@ -45,7 +45,7 @@ export function decode_address (address : string) : DecodedAddress {
|
|
|
45
45
|
* recognized.
|
|
46
46
|
*/
|
|
47
47
|
export function encode_address (
|
|
48
|
-
config :
|
|
48
|
+
config : EncoderConfig
|
|
49
49
|
) : string {
|
|
50
50
|
// Encode the address based on the format.
|
|
51
51
|
if (config.format === 'base58') return base58_encode(config)
|
|
@@ -78,7 +78,7 @@ function get_address_format (address : string) : AddressFormat | null {
|
|
|
78
78
|
* @param config - The encoder configuration.
|
|
79
79
|
* @returns The encoded base58 string.
|
|
80
80
|
*/
|
|
81
|
-
function base58_encode (config :
|
|
81
|
+
function base58_encode (config : EncoderConfig) : string {
|
|
82
82
|
// Assert the format is correct.
|
|
83
83
|
Assert.ok(config.format === 'base58', 'encoding mismatch')
|
|
84
84
|
// Assert the version is specified.
|
|
@@ -95,7 +95,7 @@ function base58_encode (config : DecodedAddress) : string {
|
|
|
95
95
|
* @param encoded - The base58 string to decode.
|
|
96
96
|
* @returns The decoded data.
|
|
97
97
|
*/
|
|
98
|
-
function base58_decode (encoded : string) :
|
|
98
|
+
function base58_decode (encoded : string) : EncoderConfig {
|
|
99
99
|
// Decode the encoded data.
|
|
100
100
|
const bytes = B58chk.decode(encoded)
|
|
101
101
|
// Get the data from the decoded bytes.
|
|
@@ -112,7 +112,7 @@ function base58_decode (encoded : string) : DecodedAddress {
|
|
|
112
112
|
* @param config - The encoder configuration.
|
|
113
113
|
* @returns The encoded bech32 string.
|
|
114
114
|
*/
|
|
115
|
-
function bech32_encode (config :
|
|
115
|
+
function bech32_encode (config : EncoderConfig) : string {
|
|
116
116
|
// Assert the format is correct.
|
|
117
117
|
Assert.ok(config.format === 'bech32', 'encoding mismatch')
|
|
118
118
|
// Assert the prefix is specified.
|
|
@@ -131,7 +131,7 @@ function bech32_encode (config : DecodedAddress) : string {
|
|
|
131
131
|
* @param encoded - The bech32 string to decode.
|
|
132
132
|
* @returns The decoded data.
|
|
133
133
|
*/
|
|
134
|
-
function bech32_decode (encoded : string) :
|
|
134
|
+
function bech32_decode (encoded : string) : EncoderConfig {
|
|
135
135
|
// Decode the encoded data.
|
|
136
136
|
const { prefix, words } = Bech32.decode(encoded)
|
|
137
137
|
// Get the version and rest of the words.
|
|
@@ -150,7 +150,7 @@ function bech32_decode (encoded : string) : DecodedAddress {
|
|
|
150
150
|
* @param config - The encoder configuration.
|
|
151
151
|
* @returns The encoded bech32 string.
|
|
152
152
|
*/
|
|
153
|
-
function bech32m_encode (config :
|
|
153
|
+
function bech32m_encode (config : EncoderConfig) : string {
|
|
154
154
|
// Assert the format is correct.
|
|
155
155
|
Assert.ok(config.format === 'bech32m', 'encoding mismatch')
|
|
156
156
|
// Assert the prefix is specified.
|
|
@@ -169,7 +169,7 @@ function bech32m_encode (config : DecodedAddress) : string {
|
|
|
169
169
|
* @param encoded - The bech32 string to decode.
|
|
170
170
|
* @returns The decoded data.
|
|
171
171
|
*/
|
|
172
|
-
function bech32m_decode (encoded : string) :
|
|
172
|
+
function bech32m_decode (encoded : string) : EncoderConfig {
|
|
173
173
|
// Decode the encoded data.
|
|
174
174
|
const { prefix, words } = Bech32m.decode(encoded)
|
|
175
175
|
// Get the version and rest of the words.
|
package/src/lib/address/index.ts
CHANGED
|
@@ -1,24 +1,7 @@
|
|
|
1
|
-
import { P2PKH as P2PKH_TOOL } from './p2pkh.js'
|
|
2
|
-
import { P2SH as P2SH_TOOL } from './p2sh.js'
|
|
3
|
-
import { P2WPKH as P2WPKH_TOOL } from './p2wpkh.js'
|
|
4
|
-
import { P2WSH as P2WSH_TOOL } from './p2wsh.js'
|
|
5
|
-
import { P2TR as P2TR_TOOL } from './p2tr.js'
|
|
6
|
-
|
|
7
|
-
import { parse_address } from './util.js'
|
|
8
|
-
|
|
9
1
|
export { P2PKH } from './p2pkh.js'
|
|
10
2
|
export { P2SH } from './p2sh.js'
|
|
11
3
|
export { P2WPKH } from './p2wpkh.js'
|
|
12
4
|
export { P2WSH } from './p2wsh.js'
|
|
13
5
|
export { P2TR } from './p2tr.js'
|
|
14
6
|
|
|
15
|
-
export
|
|
16
|
-
|
|
17
|
-
export namespace AddressTool {
|
|
18
|
-
export const P2PKH = P2PKH_TOOL
|
|
19
|
-
export const P2SH = P2SH_TOOL
|
|
20
|
-
export const P2WPKH = P2WPKH_TOOL
|
|
21
|
-
export const P2WSH = P2WSH_TOOL
|
|
22
|
-
export const P2TR = P2TR_TOOL
|
|
23
|
-
export const parse = parse_address
|
|
24
|
-
}
|
|
7
|
+
export * from './api.js'
|
package/src/lib/address/p2pkh.ts
CHANGED
|
@@ -1,53 +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_p2pkh_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.P2PKH
|
|
17
19
|
|
|
18
20
|
export namespace P2PKH {
|
|
19
|
-
export const
|
|
20
|
-
export const
|
|
21
|
-
export const
|
|
21
|
+
export const create_address = create_p2pkh_address
|
|
22
|
+
export const create_script = create_p2pkh_script
|
|
23
|
+
export const encode_address = encode_p2pkh_address
|
|
24
|
+
export const encode_script = encode_p2pkh_script
|
|
25
|
+
export const decode_address = decode_p2pkh_address
|
|
26
|
+
export const decode_script = decode_p2pkh_script
|
|
22
27
|
}
|
|
23
28
|
|
|
24
29
|
function create_p2pkh_address (
|
|
25
|
-
|
|
30
|
+
pubkey : Bytes,
|
|
26
31
|
network : ChainNetwork = 'main',
|
|
27
32
|
) : string {
|
|
28
|
-
//
|
|
29
|
-
const
|
|
33
|
+
// Create the p2pkh script.
|
|
34
|
+
const script = create_p2pkh_script(pubkey)
|
|
35
|
+
// Encode the script as an address.
|
|
36
|
+
return encode_p2pkh_address(script, network)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function create_p2pkh_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')
|
|
30
44
|
// Convert the bytes into a hash.
|
|
31
45
|
const hash = hash160(bytes)
|
|
32
|
-
//
|
|
33
|
-
return
|
|
46
|
+
// Return the script.
|
|
47
|
+
return encode_p2pkh_script(hash)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function encode_p2pkh_script (pk_hash : Bytes) : Buff {
|
|
51
|
+
return Buff.join([ '76a914', pk_hash, '88ac' ])
|
|
34
52
|
}
|
|
35
53
|
|
|
36
54
|
function encode_p2pkh_address (
|
|
37
|
-
|
|
55
|
+
script : Bytes,
|
|
38
56
|
network : ChainNetwork = 'main',
|
|
39
57
|
) : string {
|
|
40
|
-
//
|
|
41
|
-
const
|
|
58
|
+
// Get the public key hash from the script.
|
|
59
|
+
const pk_hash = decode_p2pkh_script(script)
|
|
42
60
|
// Get the address configuration.
|
|
43
|
-
const config
|
|
61
|
+
const config = get_address_config(network, ADDRESS_TYPE)
|
|
44
62
|
// Assert the configuration exists.
|
|
45
|
-
Assert.exists(config, `unrecognized address config: ${
|
|
63
|
+
Assert.exists(config, `unrecognized address config: ${ADDRESS_TYPE} on ${network}` )
|
|
46
64
|
// Assert the payload size is correct.
|
|
47
|
-
Assert.size(
|
|
65
|
+
Assert.size(pk_hash, config.size, `invalid payload size: ${pk_hash.length} !== ${config.size}` )
|
|
48
66
|
// Encode the address.
|
|
49
67
|
return encode_address({
|
|
50
|
-
data :
|
|
68
|
+
data : pk_hash,
|
|
51
69
|
format : 'base58',
|
|
52
70
|
version : config.version
|
|
53
71
|
})
|
|
@@ -55,11 +73,22 @@ function encode_p2pkh_address (
|
|
|
55
73
|
|
|
56
74
|
function decode_p2pkh_address (
|
|
57
75
|
address : string
|
|
58
|
-
) :
|
|
76
|
+
) : AddressInfo {
|
|
59
77
|
// Parse the address.
|
|
60
|
-
const parsed =
|
|
78
|
+
const parsed = get_address_info(address)
|
|
61
79
|
// Assert the address type is correct.
|
|
62
|
-
Assert.ok(parsed.type === 'p2pkh', `address type mismatch: ${parsed.type} !== ${
|
|
80
|
+
Assert.ok(parsed.type === 'p2pkh', `address type mismatch: ${parsed.type} !== ${ADDRESS_TYPE}`)
|
|
63
81
|
// Return the parsed address.
|
|
64
82
|
return parsed
|
|
65
83
|
}
|
|
84
|
+
|
|
85
|
+
function decode_p2pkh_script (
|
|
86
|
+
script : Bytes,
|
|
87
|
+
) : Buff {
|
|
88
|
+
// Convert the script into bytes.
|
|
89
|
+
const bytes = Buff.bytes(script)
|
|
90
|
+
// Assert the script is a p2pkh script.
|
|
91
|
+
Assert.ok(is_p2pkh_script(script), `invalid p2pkh script`)
|
|
92
|
+
// Return the public key hash from the script.
|
|
93
|
+
return bytes.slice(3, 23)
|
|
94
|
+
}
|
package/src/lib/address/p2sh.ts
CHANGED
|
@@ -1,53 +1,73 @@
|
|
|
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_p2sh_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.P2SH
|
|
17
19
|
|
|
18
20
|
export namespace P2SH {
|
|
19
|
-
export const
|
|
20
|
-
export const
|
|
21
|
-
export const
|
|
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
|
|
22
27
|
}
|
|
23
28
|
|
|
24
29
|
function create_p2sh_address (
|
|
25
|
-
script :
|
|
30
|
+
script : Bytes,
|
|
26
31
|
network : ChainNetwork = 'main',
|
|
27
32
|
) : string {
|
|
28
33
|
// Convert the script into bytes.
|
|
29
34
|
const bytes = Buff.bytes(script)
|
|
30
35
|
// Convert the bytes into a hash.
|
|
31
36
|
const hash = hash160(bytes)
|
|
37
|
+
// Create the p2sh script.
|
|
38
|
+
const p2sh_script = encode_p2sh_script(hash)
|
|
32
39
|
// Encode the address.
|
|
33
|
-
return encode_p2sh_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' ])
|
|
34
54
|
}
|
|
35
55
|
|
|
36
56
|
function encode_p2sh_address (
|
|
37
|
-
|
|
38
|
-
network
|
|
57
|
+
script_pk : Bytes,
|
|
58
|
+
network : ChainNetwork = 'main',
|
|
39
59
|
) : string {
|
|
40
|
-
//
|
|
41
|
-
const
|
|
60
|
+
// Get the script hash from the script.
|
|
61
|
+
const script_hash = decode_p2sh_script(script_pk)
|
|
42
62
|
// Get the address configuration.
|
|
43
|
-
const config = get_address_config(network,
|
|
63
|
+
const config = get_address_config(network, ADDRESS_TYPE)
|
|
44
64
|
// Assert the configuration exists.
|
|
45
|
-
Assert.exists(config, `unrecognized address config: ${
|
|
65
|
+
Assert.exists(config, `unrecognized address config: ${ADDRESS_TYPE} on ${network}` )
|
|
46
66
|
// Assert the payload size is correct.
|
|
47
|
-
Assert.size(
|
|
67
|
+
Assert.size(script_hash, config.size, `invalid payload size: ${script_hash.length} !== ${config.size}` )
|
|
48
68
|
// Encode the address.
|
|
49
69
|
return encode_address({
|
|
50
|
-
data :
|
|
70
|
+
data : script_hash,
|
|
51
71
|
format : 'base58',
|
|
52
72
|
version : config.version
|
|
53
73
|
})
|
|
@@ -55,11 +75,22 @@ function encode_p2sh_address (
|
|
|
55
75
|
|
|
56
76
|
function decode_p2sh_address (
|
|
57
77
|
address : string
|
|
58
|
-
) :
|
|
78
|
+
) : AddressInfo {
|
|
59
79
|
// Parse the address.
|
|
60
|
-
const parsed =
|
|
80
|
+
const parsed = get_address_info(address)
|
|
61
81
|
// Assert the address type is correct.
|
|
62
|
-
Assert.ok(parsed.type === 'p2sh', `address type mismatch: ${parsed.type} !== ${
|
|
82
|
+
Assert.ok(parsed.type === 'p2sh', `address type mismatch: ${parsed.type} !== ${ADDRESS_TYPE}`)
|
|
63
83
|
// Return the parsed address.
|
|
64
84
|
return parsed
|
|
65
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
|
+
}
|