@vbyte/btc-dev 1.1.8 → 2.1.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 +127 -0
- package/LICENSE +21 -121
- package/README.md +69 -3
- package/dist/const.d.ts +3 -0
- package/dist/const.js +23 -22
- package/dist/error.d.ts +11 -0
- package/dist/error.js +20 -0
- package/dist/index.d.ts +12 -11
- package/dist/index.js +11 -10
- package/dist/lib/address/api.d.ts +2 -2
- package/dist/lib/address/api.js +13 -12
- package/dist/lib/address/encode.d.ts +1 -1
- package/dist/lib/address/encode.js +26 -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 +15 -15
- package/dist/lib/address/p2sh.d.ts +2 -2
- package/dist/lib/address/p2sh.js +14 -14
- package/dist/lib/address/p2tr.d.ts +2 -2
- package/dist/lib/address/p2tr.js +14 -14
- package/dist/lib/address/p2wpkh.d.ts +2 -2
- package/dist/lib/address/p2wpkh.js +15 -15
- package/dist/lib/address/p2wsh.d.ts +2 -2
- package/dist/lib/address/p2wsh.js +14 -14
- 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 +24 -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 +13 -12
- package/dist/lib/meta/ref.js +13 -9
- package/dist/lib/meta/scribe.d.ts +2 -2
- package/dist/lib/meta/scribe.js +71 -56
- package/dist/lib/meta/sequence.d.ts +1 -1
- package/dist/lib/meta/sequence.js +21 -19
- package/dist/lib/script/decode.d.ts +2 -2
- package/dist/lib/script/decode.js +53 -17
- package/dist/lib/script/encode.d.ts +1 -1
- package/dist/lib/script/encode.js +21 -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 +131 -130
- 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 +18 -14
- package/dist/lib/sighash/taproot.d.ts +2 -2
- package/dist/lib/sighash/taproot.js +27 -23
- package/dist/lib/sighash/util.d.ts +2 -2
- package/dist/lib/sighash/util.js +8 -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 +43 -7
- package/dist/lib/signer/verify.d.ts +17 -3
- package/dist/lib/signer/verify.js +232 -3
- package/dist/lib/taproot/cblock.d.ts +1 -1
- package/dist/lib/taproot/cblock.js +16 -17
- package/dist/lib/taproot/encode.d.ts +1 -1
- package/dist/lib/taproot/encode.js +9 -8
- 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 +15 -15
- package/dist/lib/taproot/tree.d.ts +2 -2
- package/dist/lib/taproot/tree.js +12 -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 +52 -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 -11
- package/dist/lib/tx/util.d.ts +2 -2
- package/dist/lib/tx/util.js +22 -20
- package/dist/lib/tx/validate.d.ts +1 -1
- package/dist/lib/tx/validate.js +37 -9
- 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 +3305 -2035
- package/dist/main.cjs.map +1 -1
- package/dist/module.mjs +3303 -2036
- package/dist/module.mjs.map +1 -1
- package/dist/package.json +24 -19
- 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 +10 -12
- 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/docs/API.md +1145 -0
- package/docs/CONVENTIONS.md +316 -0
- package/docs/FAQ.md +396 -0
- package/docs/GUIDE.md +1102 -0
- package/package.json +24 -19
- 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 -111
- 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
|
@@ -1,34 +1,36 @@
|
|
|
1
|
-
import { Buff } from
|
|
2
|
-
import { Assert
|
|
1
|
+
import { Buff } from "@vbyte/buff";
|
|
2
|
+
import { Assert } from "@vbyte/util";
|
|
3
|
+
import { B58chk, Bech32, Bech32m } from "@vbyte/crypto";
|
|
4
|
+
import { ValidationError } from "../../error.js";
|
|
3
5
|
const ENCODING_REGEX = {
|
|
4
6
|
base58: /^[13mn2][a-km-zA-HJ-NP-Z1-9]{25,34}$/,
|
|
5
7
|
bech32: /^(bc|tb|bcrt)1q[ac-hj-np-z02-9]{6,87}$/,
|
|
6
|
-
bech32m: /^(bc|tb|bcrt)1p[ac-hj-np-z02-9]{6,87}
|
|
8
|
+
bech32m: /^(bc|tb|bcrt)1p[ac-hj-np-z02-9]{6,87}$/,
|
|
7
9
|
};
|
|
8
10
|
const VERSION = {
|
|
9
11
|
bech32: 0,
|
|
10
|
-
bech32m: 1
|
|
12
|
+
bech32m: 1,
|
|
11
13
|
};
|
|
12
14
|
export function decode_address(address) {
|
|
13
15
|
const format = get_address_format(address);
|
|
14
16
|
if (format === null)
|
|
15
|
-
throw new
|
|
16
|
-
if (format ===
|
|
17
|
+
throw new ValidationError(`unrecognized address format: ${address}`, "address");
|
|
18
|
+
if (format === "base58")
|
|
17
19
|
return base58_decode(address);
|
|
18
|
-
if (format ===
|
|
20
|
+
if (format === "bech32")
|
|
19
21
|
return bech32_decode(address);
|
|
20
|
-
if (format ===
|
|
22
|
+
if (format === "bech32m")
|
|
21
23
|
return bech32m_decode(address);
|
|
22
|
-
throw new
|
|
24
|
+
throw new ValidationError("unable to find a matching address configuration", "address");
|
|
23
25
|
}
|
|
24
26
|
export function encode_address(config) {
|
|
25
|
-
if (config.format ===
|
|
27
|
+
if (config.format === "base58")
|
|
26
28
|
return base58_encode(config);
|
|
27
|
-
if (config.format ===
|
|
29
|
+
if (config.format === "bech32")
|
|
28
30
|
return bech32_encode(config);
|
|
29
|
-
if (config.format ===
|
|
31
|
+
if (config.format === "bech32m")
|
|
30
32
|
return bech32m_encode(config);
|
|
31
|
-
throw new
|
|
33
|
+
throw new ValidationError(`unrecognized encoding format: ${config.format}`, "format");
|
|
32
34
|
}
|
|
33
35
|
function get_address_format(address) {
|
|
34
36
|
for (const [format, regex] of Object.entries(ENCODING_REGEX)) {
|
|
@@ -38,8 +40,8 @@ function get_address_format(address) {
|
|
|
38
40
|
return null;
|
|
39
41
|
}
|
|
40
42
|
function base58_encode(config) {
|
|
41
|
-
Assert.ok(config.format ===
|
|
42
|
-
Assert.exists(config.version,
|
|
43
|
+
Assert.ok(config.format === "base58", "encoding mismatch");
|
|
44
|
+
Assert.exists(config.version, "must specify a version");
|
|
43
45
|
const bytes = Buff.join([config.version, config.data]);
|
|
44
46
|
return B58chk.encode(bytes);
|
|
45
47
|
}
|
|
@@ -47,11 +49,11 @@ function base58_decode(encoded) {
|
|
|
47
49
|
const bytes = B58chk.decode(encoded);
|
|
48
50
|
const data = bytes.slice(1);
|
|
49
51
|
const version = bytes[0];
|
|
50
|
-
return { data, format:
|
|
52
|
+
return { data, format: "base58", version };
|
|
51
53
|
}
|
|
52
54
|
function bech32_encode(config) {
|
|
53
|
-
Assert.ok(config.format ===
|
|
54
|
-
Assert.exists(config.prefix,
|
|
55
|
+
Assert.ok(config.format === "bech32", "encoding mismatch");
|
|
56
|
+
Assert.exists(config.prefix, "prefix is required");
|
|
55
57
|
const bytes = Buff.bytes(config.data);
|
|
56
58
|
const words = Bech32.to_words(bytes);
|
|
57
59
|
return Bech32.encode(config.prefix, [VERSION.bech32, ...words]);
|
|
@@ -59,13 +61,13 @@ function bech32_encode(config) {
|
|
|
59
61
|
function bech32_decode(encoded) {
|
|
60
62
|
const { prefix, words } = Bech32.decode(encoded);
|
|
61
63
|
const [version, ...rest] = words;
|
|
62
|
-
Assert.ok(version === VERSION.bech32,
|
|
64
|
+
Assert.ok(version === VERSION.bech32, "bech32 version mismatch");
|
|
63
65
|
const data = Bech32.to_bytes(rest);
|
|
64
|
-
return { data, format:
|
|
66
|
+
return { data, format: "bech32", prefix, version };
|
|
65
67
|
}
|
|
66
68
|
function bech32m_encode(config) {
|
|
67
|
-
Assert.ok(config.format ===
|
|
68
|
-
Assert.exists(config.prefix,
|
|
69
|
+
Assert.ok(config.format === "bech32m", "encoding mismatch");
|
|
70
|
+
Assert.exists(config.prefix, "prefix is required");
|
|
69
71
|
const bytes = Buff.bytes(config.data);
|
|
70
72
|
const words = Bech32m.to_words(bytes);
|
|
71
73
|
return Bech32m.encode(config.prefix, [VERSION.bech32m, ...words]);
|
|
@@ -73,7 +75,7 @@ function bech32m_encode(config) {
|
|
|
73
75
|
function bech32m_decode(encoded) {
|
|
74
76
|
const { prefix, words } = Bech32m.decode(encoded);
|
|
75
77
|
const [version, ...rest] = words;
|
|
76
|
-
Assert.ok(version === VERSION.bech32m,
|
|
78
|
+
Assert.ok(version === VERSION.bech32m, "bech32m version mismatch");
|
|
77
79
|
const data = Bech32m.to_bytes(rest);
|
|
78
|
-
return { data, format:
|
|
80
|
+
return { data, format: "bech32m", prefix, version };
|
|
79
81
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
export
|
|
2
|
-
export {
|
|
3
|
-
export {
|
|
4
|
-
export {
|
|
5
|
-
export {
|
|
6
|
-
export
|
|
1
|
+
export * from "./api.js";
|
|
2
|
+
export { P2PKH } from "./p2pkh.js";
|
|
3
|
+
export { P2SH } from "./p2sh.js";
|
|
4
|
+
export { P2TR } from "./p2tr.js";
|
|
5
|
+
export { P2WPKH } from "./p2wpkh.js";
|
|
6
|
+
export { P2WSH } from "./p2wsh.js";
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
export
|
|
2
|
-
export {
|
|
3
|
-
export {
|
|
4
|
-
export {
|
|
5
|
-
export {
|
|
6
|
-
export
|
|
1
|
+
export * from "./api.js";
|
|
2
|
+
export { P2PKH } from "./p2pkh.js";
|
|
3
|
+
export { P2SH } from "./p2sh.js";
|
|
4
|
+
export { P2TR } from "./p2tr.js";
|
|
5
|
+
export { P2WPKH } from "./p2wpkh.js";
|
|
6
|
+
export { P2WSH } from "./p2wsh.js";
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { Buff, Bytes } from
|
|
2
|
-
import type { AddressInfo, ChainNetwork } from
|
|
1
|
+
import { Buff, type Bytes } from "@vbyte/buff";
|
|
2
|
+
import type { AddressInfo, ChainNetwork } from "../../types/index.js";
|
|
3
3
|
export declare namespace P2PKH {
|
|
4
4
|
const create_address: typeof create_p2pkh_address;
|
|
5
5
|
const create_script: typeof create_p2pkh_script;
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { Buff } from
|
|
2
|
-
import { Assert } from
|
|
3
|
-
import { hash160 } from
|
|
4
|
-
import {
|
|
5
|
-
import { is_p2pkh_script } from
|
|
6
|
-
import {
|
|
7
|
-
import { get_address_config, get_address_info } from
|
|
1
|
+
import { Buff } from "@vbyte/buff";
|
|
2
|
+
import { Assert } from "@vbyte/util";
|
|
3
|
+
import { hash160 } from "@vbyte/crypto/hash";
|
|
4
|
+
import { LOCK_SCRIPT_TYPE } from "../../const.js";
|
|
5
|
+
import { is_p2pkh_script } from "../../lib/script/lock.js";
|
|
6
|
+
import { encode_address } from "./encode.js";
|
|
7
|
+
import { get_address_config, get_address_info } from "./util.js";
|
|
8
8
|
const ADDRESS_TYPE = LOCK_SCRIPT_TYPE.P2PKH;
|
|
9
9
|
export var P2PKH;
|
|
10
10
|
(function (P2PKH) {
|
|
@@ -15,33 +15,33 @@ export var P2PKH;
|
|
|
15
15
|
P2PKH.decode_address = decode_p2pkh_address;
|
|
16
16
|
P2PKH.decode_script = decode_p2pkh_script;
|
|
17
17
|
})(P2PKH || (P2PKH = {}));
|
|
18
|
-
function create_p2pkh_address(pubkey, network =
|
|
18
|
+
function create_p2pkh_address(pubkey, network = "main") {
|
|
19
19
|
const script = create_p2pkh_script(pubkey);
|
|
20
20
|
return encode_p2pkh_address(script, network);
|
|
21
21
|
}
|
|
22
22
|
function create_p2pkh_script(pubkey) {
|
|
23
23
|
const bytes = Buff.bytes(pubkey);
|
|
24
|
-
Assert.
|
|
24
|
+
Assert.ok(bytes.length === 33, "invalid pubkey size");
|
|
25
25
|
const hash = hash160(bytes);
|
|
26
26
|
return encode_p2pkh_script(hash);
|
|
27
27
|
}
|
|
28
28
|
function encode_p2pkh_script(pk_hash) {
|
|
29
|
-
return Buff.join([
|
|
29
|
+
return Buff.join(["76a914", pk_hash, "88ac"]);
|
|
30
30
|
}
|
|
31
|
-
function encode_p2pkh_address(script, network =
|
|
31
|
+
function encode_p2pkh_address(script, network = "main") {
|
|
32
32
|
const pk_hash = decode_p2pkh_script(script);
|
|
33
33
|
const config = get_address_config(network, ADDRESS_TYPE);
|
|
34
34
|
Assert.exists(config, `unrecognized address config: ${ADDRESS_TYPE} on ${network}`);
|
|
35
|
-
Assert.
|
|
35
|
+
Assert.ok(pk_hash.length === config.size, `invalid payload size: ${pk_hash.length} !== ${config.size}`);
|
|
36
36
|
return encode_address({
|
|
37
37
|
data: pk_hash,
|
|
38
|
-
format:
|
|
39
|
-
version: config.version
|
|
38
|
+
format: "base58",
|
|
39
|
+
version: config.version,
|
|
40
40
|
});
|
|
41
41
|
}
|
|
42
42
|
function decode_p2pkh_address(address) {
|
|
43
43
|
const parsed = get_address_info(address);
|
|
44
|
-
Assert.ok(parsed.type ===
|
|
44
|
+
Assert.ok(parsed.type === "p2pkh", `address type mismatch: ${parsed.type} !== ${ADDRESS_TYPE}`);
|
|
45
45
|
return parsed;
|
|
46
46
|
}
|
|
47
47
|
function decode_p2pkh_script(script) {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { Buff, Bytes } from
|
|
2
|
-
import type { AddressInfo, ChainNetwork } from
|
|
1
|
+
import { Buff, type Bytes } from "@vbyte/buff";
|
|
2
|
+
import type { AddressInfo, ChainNetwork } from "../../types/index.js";
|
|
3
3
|
export declare namespace P2SH {
|
|
4
4
|
const create_address: typeof create_p2sh_address;
|
|
5
5
|
const create_script: typeof create_p2sh_script;
|
package/dist/lib/address/p2sh.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { Buff } from
|
|
2
|
-
import { Assert } from
|
|
3
|
-
import { hash160 } from
|
|
4
|
-
import {
|
|
5
|
-
import { is_p2sh_script } from
|
|
6
|
-
import {
|
|
7
|
-
import { get_address_config, get_address_info } from
|
|
1
|
+
import { Buff } from "@vbyte/buff";
|
|
2
|
+
import { Assert } from "@vbyte/util";
|
|
3
|
+
import { hash160 } from "@vbyte/crypto/hash";
|
|
4
|
+
import { LOCK_SCRIPT_TYPE } from "../../const.js";
|
|
5
|
+
import { is_p2sh_script } from "../../lib/script/lock.js";
|
|
6
|
+
import { encode_address } from "./encode.js";
|
|
7
|
+
import { get_address_config, get_address_info } from "./util.js";
|
|
8
8
|
const ADDRESS_TYPE = LOCK_SCRIPT_TYPE.P2SH;
|
|
9
9
|
export var P2SH;
|
|
10
10
|
(function (P2SH) {
|
|
@@ -15,7 +15,7 @@ export var P2SH;
|
|
|
15
15
|
P2SH.decode_address = decode_p2sh_address;
|
|
16
16
|
P2SH.decode_script = decode_p2sh_script;
|
|
17
17
|
})(P2SH || (P2SH = {}));
|
|
18
|
-
function create_p2sh_address(script, network =
|
|
18
|
+
function create_p2sh_address(script, network = "main") {
|
|
19
19
|
const bytes = Buff.bytes(script);
|
|
20
20
|
const hash = hash160(bytes);
|
|
21
21
|
const p2sh_script = encode_p2sh_script(hash);
|
|
@@ -27,22 +27,22 @@ function create_p2sh_script(script) {
|
|
|
27
27
|
return encode_p2sh_script(hash);
|
|
28
28
|
}
|
|
29
29
|
function encode_p2sh_script(script_hash) {
|
|
30
|
-
return Buff.join([
|
|
30
|
+
return Buff.join(["a914", script_hash, "87"]);
|
|
31
31
|
}
|
|
32
|
-
function encode_p2sh_address(script_pk, network =
|
|
32
|
+
function encode_p2sh_address(script_pk, network = "main") {
|
|
33
33
|
const script_hash = decode_p2sh_script(script_pk);
|
|
34
34
|
const config = get_address_config(network, ADDRESS_TYPE);
|
|
35
35
|
Assert.exists(config, `unrecognized address config: ${ADDRESS_TYPE} on ${network}`);
|
|
36
|
-
Assert.
|
|
36
|
+
Assert.ok(script_hash.length === config.size, `invalid payload size: ${script_hash.length} !== ${config.size}`);
|
|
37
37
|
return encode_address({
|
|
38
38
|
data: script_hash,
|
|
39
|
-
format:
|
|
40
|
-
version: config.version
|
|
39
|
+
format: "base58",
|
|
40
|
+
version: config.version,
|
|
41
41
|
});
|
|
42
42
|
}
|
|
43
43
|
function decode_p2sh_address(address) {
|
|
44
44
|
const parsed = get_address_info(address);
|
|
45
|
-
Assert.ok(parsed.type ===
|
|
45
|
+
Assert.ok(parsed.type === "p2sh", `address type mismatch: ${parsed.type} !== ${ADDRESS_TYPE}`);
|
|
46
46
|
return parsed;
|
|
47
47
|
}
|
|
48
48
|
function decode_p2sh_script(script) {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { Buff, Bytes } from
|
|
2
|
-
import type { AddressInfo, ChainNetwork } from
|
|
1
|
+
import { Buff, type Bytes } from "@vbyte/buff";
|
|
2
|
+
import type { AddressInfo, ChainNetwork } from "../../types/index.js";
|
|
3
3
|
export declare namespace P2TR {
|
|
4
4
|
const create_address: typeof create_p2tr_address;
|
|
5
5
|
const create_script: typeof create_p2tr_script;
|
package/dist/lib/address/p2tr.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { Buff } from
|
|
2
|
-
import { Assert } from
|
|
3
|
-
import {
|
|
4
|
-
import { is_p2tr_script } from
|
|
5
|
-
import {
|
|
6
|
-
import { get_address_config, get_address_info } from
|
|
1
|
+
import { Buff } from "@vbyte/buff";
|
|
2
|
+
import { Assert } from "@vbyte/util";
|
|
3
|
+
import { LOCK_SCRIPT_TYPE } from "../../const.js";
|
|
4
|
+
import { is_p2tr_script } from "../../lib/script/lock.js";
|
|
5
|
+
import { encode_address } from "./encode.js";
|
|
6
|
+
import { get_address_config, get_address_info } from "./util.js";
|
|
7
7
|
const ADDRESS_TYPE = LOCK_SCRIPT_TYPE.P2TR;
|
|
8
8
|
export var P2TR;
|
|
9
9
|
(function (P2TR) {
|
|
@@ -14,32 +14,32 @@ export var P2TR;
|
|
|
14
14
|
P2TR.decode_address = decode_p2tr_address;
|
|
15
15
|
P2TR.decode_script = decode_p2tr_script;
|
|
16
16
|
})(P2TR || (P2TR = {}));
|
|
17
|
-
function create_p2tr_address(pubkey, network =
|
|
17
|
+
function create_p2tr_address(pubkey, network = "main") {
|
|
18
18
|
const script = create_p2tr_script(pubkey);
|
|
19
19
|
return encode_p2tr_address(script, network);
|
|
20
20
|
}
|
|
21
21
|
function create_p2tr_script(pubkey) {
|
|
22
22
|
const bytes = Buff.bytes(pubkey);
|
|
23
|
-
Assert.
|
|
23
|
+
Assert.ok(bytes.length === 32, "invalid pubkey size");
|
|
24
24
|
return encode_p2tr_script(bytes);
|
|
25
25
|
}
|
|
26
26
|
function encode_p2tr_script(pubkey) {
|
|
27
|
-
return Buff.join([
|
|
27
|
+
return Buff.join(["5120", pubkey]);
|
|
28
28
|
}
|
|
29
|
-
function encode_p2tr_address(script_pk, network =
|
|
29
|
+
function encode_p2tr_address(script_pk, network = "main") {
|
|
30
30
|
const pubkey = decode_p2tr_script(script_pk);
|
|
31
31
|
const config = get_address_config(network, ADDRESS_TYPE);
|
|
32
32
|
Assert.exists(config, `unrecognized address config: ${ADDRESS_TYPE} on ${network}`);
|
|
33
|
-
Assert.
|
|
33
|
+
Assert.ok(pubkey.length === config.size, `invalid payload size: ${pubkey.length} !== ${config.size}`);
|
|
34
34
|
return encode_address({
|
|
35
35
|
data: pubkey,
|
|
36
|
-
format:
|
|
37
|
-
prefix: config.prefix
|
|
36
|
+
format: "bech32m",
|
|
37
|
+
prefix: config.prefix,
|
|
38
38
|
});
|
|
39
39
|
}
|
|
40
40
|
function decode_p2tr_address(address) {
|
|
41
41
|
const parsed = get_address_info(address);
|
|
42
|
-
Assert.ok(parsed.type ===
|
|
42
|
+
Assert.ok(parsed.type === "p2tr", `address type mismatch: ${parsed.type} !== ${ADDRESS_TYPE}`);
|
|
43
43
|
return parsed;
|
|
44
44
|
}
|
|
45
45
|
function decode_p2tr_script(script) {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { Buff, Bytes } from
|
|
2
|
-
import type { AddressInfo, ChainNetwork } from
|
|
1
|
+
import { Buff, type Bytes } from "@vbyte/buff";
|
|
2
|
+
import type { AddressInfo, ChainNetwork } from "../../types/index.js";
|
|
3
3
|
export declare namespace P2WPKH {
|
|
4
4
|
const create_address: typeof create_p2wpkh_address;
|
|
5
5
|
const create_script: typeof create_p2wpkh_script;
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { Buff } from
|
|
2
|
-
import { Assert } from
|
|
3
|
-
import { hash160 } from
|
|
4
|
-
import {
|
|
5
|
-
import { is_p2wpkh_script } from
|
|
6
|
-
import {
|
|
7
|
-
import { get_address_config, get_address_info } from
|
|
1
|
+
import { Buff } from "@vbyte/buff";
|
|
2
|
+
import { Assert } from "@vbyte/util";
|
|
3
|
+
import { hash160 } from "@vbyte/crypto/hash";
|
|
4
|
+
import { LOCK_SCRIPT_TYPE } from "../../const.js";
|
|
5
|
+
import { is_p2wpkh_script } from "../../lib/script/lock.js";
|
|
6
|
+
import { encode_address } from "./encode.js";
|
|
7
|
+
import { get_address_config, get_address_info } from "./util.js";
|
|
8
8
|
const ADDRESS_TYPE = LOCK_SCRIPT_TYPE.P2WPKH;
|
|
9
9
|
export var P2WPKH;
|
|
10
10
|
(function (P2WPKH) {
|
|
@@ -15,33 +15,33 @@ export var P2WPKH;
|
|
|
15
15
|
P2WPKH.decode_address = decode_p2wpkh_address;
|
|
16
16
|
P2WPKH.decode_script = decode_p2wpkh_script;
|
|
17
17
|
})(P2WPKH || (P2WPKH = {}));
|
|
18
|
-
function create_p2wpkh_address(pubkey, network =
|
|
18
|
+
function create_p2wpkh_address(pubkey, network = "main") {
|
|
19
19
|
const script = create_p2wpkh_script(pubkey);
|
|
20
20
|
return encode_p2wpkh_address(script, network);
|
|
21
21
|
}
|
|
22
22
|
function create_p2wpkh_script(pubkey) {
|
|
23
23
|
const bytes = Buff.bytes(pubkey);
|
|
24
|
-
Assert.
|
|
24
|
+
Assert.ok(bytes.length === 33, "invalid pubkey size");
|
|
25
25
|
const hash = hash160(bytes);
|
|
26
26
|
return encode_p2wpkh_script(hash);
|
|
27
27
|
}
|
|
28
28
|
function encode_p2wpkh_script(pk_hash) {
|
|
29
|
-
return Buff.join([
|
|
29
|
+
return Buff.join(["0014", pk_hash]);
|
|
30
30
|
}
|
|
31
|
-
function encode_p2wpkh_address(script_pk, network =
|
|
31
|
+
function encode_p2wpkh_address(script_pk, network = "main") {
|
|
32
32
|
const pk_hash = decode_p2wpkh_script(script_pk);
|
|
33
33
|
const config = get_address_config(network, ADDRESS_TYPE);
|
|
34
34
|
Assert.exists(config, `unrecognized address config: ${ADDRESS_TYPE} on ${network}`);
|
|
35
|
-
Assert.
|
|
35
|
+
Assert.ok(pk_hash.length === config.size, `invalid payload size: ${pk_hash.length} !== ${config.size}`);
|
|
36
36
|
return encode_address({
|
|
37
37
|
data: pk_hash,
|
|
38
|
-
format:
|
|
39
|
-
prefix: config.prefix
|
|
38
|
+
format: "bech32",
|
|
39
|
+
prefix: config.prefix,
|
|
40
40
|
});
|
|
41
41
|
}
|
|
42
42
|
function decode_p2wpkh_address(address) {
|
|
43
43
|
const parsed = get_address_info(address);
|
|
44
|
-
Assert.ok(parsed.type ===
|
|
44
|
+
Assert.ok(parsed.type === "p2wpkh", `address type mismatch: ${parsed.type} !== ${ADDRESS_TYPE}`);
|
|
45
45
|
return parsed;
|
|
46
46
|
}
|
|
47
47
|
function decode_p2wpkh_script(script) {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { Buff, Bytes } from
|
|
2
|
-
import type { AddressInfo, ChainNetwork } from
|
|
1
|
+
import { Buff, type Bytes } from "@vbyte/buff";
|
|
2
|
+
import type { AddressInfo, ChainNetwork } from "../../types/index.js";
|
|
3
3
|
export declare namespace P2WSH {
|
|
4
4
|
const create_address: typeof create_p2wsh_address;
|
|
5
5
|
const create_script: typeof create_p2wsh_script;
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { Buff } from
|
|
2
|
-
import { Assert } from
|
|
3
|
-
import { sha256 } from
|
|
4
|
-
import {
|
|
5
|
-
import { is_p2wsh_script } from
|
|
6
|
-
import {
|
|
7
|
-
import { get_address_config, get_address_info } from
|
|
1
|
+
import { Buff } from "@vbyte/buff";
|
|
2
|
+
import { Assert } from "@vbyte/util";
|
|
3
|
+
import { sha256 } from "@vbyte/crypto/hash";
|
|
4
|
+
import { LOCK_SCRIPT_TYPE } from "../../const.js";
|
|
5
|
+
import { is_p2wsh_script } from "../../lib/script/lock.js";
|
|
6
|
+
import { encode_address } from "./encode.js";
|
|
7
|
+
import { get_address_config, get_address_info } from "./util.js";
|
|
8
8
|
const ADDRESS_TYPE = LOCK_SCRIPT_TYPE.P2WSH;
|
|
9
9
|
export var P2WSH;
|
|
10
10
|
(function (P2WSH) {
|
|
@@ -15,7 +15,7 @@ export var P2WSH;
|
|
|
15
15
|
P2WSH.decode_address = decode_p2wsh_address;
|
|
16
16
|
P2WSH.decode_script = decode_p2wsh_script;
|
|
17
17
|
})(P2WSH || (P2WSH = {}));
|
|
18
|
-
function create_p2wsh_address(script, network =
|
|
18
|
+
function create_p2wsh_address(script, network = "main") {
|
|
19
19
|
const wsh_script = create_p2wsh_script(script);
|
|
20
20
|
return encode_p2wsh_address(wsh_script, network);
|
|
21
21
|
}
|
|
@@ -25,22 +25,22 @@ function create_p2wsh_script(script) {
|
|
|
25
25
|
return encode_p2wsh_script(hash);
|
|
26
26
|
}
|
|
27
27
|
function encode_p2wsh_script(script_hash) {
|
|
28
|
-
return Buff.join([
|
|
28
|
+
return Buff.join(["0020", script_hash]);
|
|
29
29
|
}
|
|
30
|
-
function encode_p2wsh_address(script_pk, network =
|
|
30
|
+
function encode_p2wsh_address(script_pk, network = "main") {
|
|
31
31
|
const script_hash = decode_p2wsh_script(script_pk);
|
|
32
32
|
const config = get_address_config(network, ADDRESS_TYPE);
|
|
33
33
|
Assert.exists(config, `unrecognized address config: ${ADDRESS_TYPE} on ${network}`);
|
|
34
|
-
Assert.
|
|
34
|
+
Assert.ok(script_hash.length === config.size, `invalid payload size: ${script_hash.length} !== ${config.size}`);
|
|
35
35
|
return encode_address({
|
|
36
36
|
data: script_hash,
|
|
37
|
-
format:
|
|
38
|
-
prefix: config.prefix
|
|
37
|
+
format: "bech32",
|
|
38
|
+
prefix: config.prefix,
|
|
39
39
|
});
|
|
40
40
|
}
|
|
41
41
|
function decode_p2wsh_address(address) {
|
|
42
42
|
const parsed = get_address_info(address);
|
|
43
|
-
Assert.ok(parsed.type ===
|
|
43
|
+
Assert.ok(parsed.type === "p2wsh", `address type mismatch: ${parsed.type} !== ${ADDRESS_TYPE}`);
|
|
44
44
|
return parsed;
|
|
45
45
|
}
|
|
46
46
|
function decode_p2wsh_script(script) {
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import type { AddressType, ScriptInfo } from
|
|
1
|
+
import type { AddressType, ScriptInfo } from "../../types/index.js";
|
|
2
2
|
export declare function get_address_script(script_key: string, script_type: AddressType): ScriptInfo;
|
|
@@ -1,46 +1,46 @@
|
|
|
1
1
|
export function get_address_script(script_key, script_type) {
|
|
2
2
|
switch (script_type) {
|
|
3
|
-
case
|
|
3
|
+
case "p2pkh":
|
|
4
4
|
return get_p2pkh_script(script_key);
|
|
5
|
-
case
|
|
5
|
+
case "p2sh":
|
|
6
6
|
return get_p2sh_script(script_key);
|
|
7
|
-
case
|
|
7
|
+
case "p2wpkh":
|
|
8
8
|
return get_p2w_pkh_script(script_key);
|
|
9
|
-
case
|
|
9
|
+
case "p2wsh":
|
|
10
10
|
return get_p2w_sh_script(script_key);
|
|
11
|
-
case
|
|
11
|
+
case "p2tr":
|
|
12
12
|
return get_p2tr_script(script_key);
|
|
13
13
|
default:
|
|
14
|
-
throw new Error(
|
|
14
|
+
throw new Error(`unrecognized script type: ${script_type}`);
|
|
15
15
|
}
|
|
16
16
|
}
|
|
17
17
|
function get_p2pkh_script(script_key) {
|
|
18
18
|
return {
|
|
19
|
-
hex:
|
|
20
|
-
asm: [
|
|
19
|
+
hex: `76a914${script_key}88ac`,
|
|
20
|
+
asm: ["OP_DUP", "OP_HASH160", script_key, "OP_EQUALVERIFY", "OP_CHECKSIG"],
|
|
21
21
|
};
|
|
22
22
|
}
|
|
23
23
|
function get_p2sh_script(script_key) {
|
|
24
24
|
return {
|
|
25
|
-
hex:
|
|
26
|
-
asm: [
|
|
25
|
+
hex: `a914${script_key}87`,
|
|
26
|
+
asm: ["OP_HASH160", script_key, "OP_EQUAL"],
|
|
27
27
|
};
|
|
28
28
|
}
|
|
29
29
|
function get_p2w_pkh_script(script_key) {
|
|
30
30
|
return {
|
|
31
|
-
hex:
|
|
32
|
-
asm: [
|
|
31
|
+
hex: `0014${script_key}`,
|
|
32
|
+
asm: ["OP_0", script_key],
|
|
33
33
|
};
|
|
34
34
|
}
|
|
35
35
|
function get_p2w_sh_script(script_key) {
|
|
36
36
|
return {
|
|
37
|
-
hex:
|
|
38
|
-
asm: [
|
|
37
|
+
hex: `0020${script_key}`,
|
|
38
|
+
asm: ["OP_0", script_key],
|
|
39
39
|
};
|
|
40
40
|
}
|
|
41
41
|
function get_p2tr_script(script_key) {
|
|
42
42
|
return {
|
|
43
|
-
hex:
|
|
44
|
-
asm: [
|
|
43
|
+
hex: `5120${script_key}`,
|
|
44
|
+
asm: ["OP_1", script_key],
|
|
45
45
|
};
|
|
46
46
|
}
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import type { AddressConfig, AddressInfo, ChainNetwork, LockScriptType } from
|
|
1
|
+
import type { AddressConfig, AddressInfo, ChainNetwork, LockScriptType } from "../../types/index.js";
|
|
2
2
|
export declare function get_address_config(address_network: ChainNetwork, address_type: LockScriptType): AddressConfig | null;
|
|
3
3
|
export declare function get_address_info(address: string): AddressInfo;
|
package/dist/lib/address/util.js
CHANGED
|
@@ -1,24 +1,25 @@
|
|
|
1
|
-
import { Buff } from
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
1
|
+
import { Buff } from "@vbyte/buff";
|
|
2
|
+
import { ConfigError } from "../../error.js";
|
|
3
|
+
import { decode_address } from "./encode.js";
|
|
4
|
+
import { get_address_script } from "./script.js";
|
|
4
5
|
const CONFIG_TABLE = [
|
|
5
|
-
[
|
|
6
|
-
[
|
|
7
|
-
[
|
|
8
|
-
[
|
|
9
|
-
[
|
|
10
|
-
[
|
|
11
|
-
[
|
|
12
|
-
[
|
|
13
|
-
[
|
|
14
|
-
[
|
|
15
|
-
[
|
|
16
|
-
[
|
|
17
|
-
[
|
|
18
|
-
[
|
|
19
|
-
[
|
|
20
|
-
[
|
|
21
|
-
[
|
|
6
|
+
["1", "p2pkh", "main", 20, "base58", 0x00],
|
|
7
|
+
["3", "p2sh", "main", 20, "base58", 0x05],
|
|
8
|
+
["m", "p2pkh", "testnet", 20, "base58", 0x6f],
|
|
9
|
+
["n", "p2pkh", "testnet", 20, "base58", 0x6f],
|
|
10
|
+
["2", "p2sh", "testnet", 20, "base58", 0xc4],
|
|
11
|
+
["m", "p2pkh", "regtest", 20, "base58", 0x6f],
|
|
12
|
+
["n", "p2pkh", "regtest", 20, "base58", 0x6f],
|
|
13
|
+
["2", "p2sh", "regtest", 20, "base58", 0xc4],
|
|
14
|
+
["bc", "p2wpkh", "main", 20, "bech32", 0],
|
|
15
|
+
["tb", "p2wpkh", "testnet", 20, "bech32", 0],
|
|
16
|
+
["bcrt", "p2wpkh", "regtest", 20, "bech32", 0],
|
|
17
|
+
["bc", "p2wsh", "main", 32, "bech32", 0],
|
|
18
|
+
["tb", "p2wsh", "testnet", 32, "bech32", 0],
|
|
19
|
+
["bcrt", "p2wsh", "regtest", 32, "bech32", 0],
|
|
20
|
+
["bc", "p2tr", "main", 32, "bech32m", 1],
|
|
21
|
+
["tb", "p2tr", "testnet", 32, "bech32m", 1],
|
|
22
|
+
["bcrt", "p2tr", "regtest", 32, "bech32m", 1],
|
|
22
23
|
];
|
|
23
24
|
export function get_address_config(address_network, address_type) {
|
|
24
25
|
for (const [prefix, type, network, size, format, version] of CONFIG_TABLE) {
|
|
@@ -45,9 +46,10 @@ export function get_address_info(address) {
|
|
|
45
46
|
if (!address.startsWith(prefix))
|
|
46
47
|
continue;
|
|
47
48
|
}
|
|
48
|
-
const data = Buff.
|
|
49
|
+
const data = Buff.bytes(dec.data).hex;
|
|
49
50
|
const script = get_address_script(data, type);
|
|
50
51
|
return { data, script, type, prefix, network, size, format, version };
|
|
51
52
|
}
|
|
52
|
-
throw new
|
|
53
|
+
throw new ConfigError(`unrecognized address configuration: format=${dec.format}, size=${dec.data.length}, version=${dec.version}. ` +
|
|
54
|
+
`Supported formats: base58 (p2pkh, p2sh), bech32 (p2wpkh, p2wsh), bech32m (p2tr)`);
|
|
53
55
|
}
|
package/dist/lib/meta/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export * from
|
|
2
|
-
export * from
|
|
3
|
-
export * from
|
|
4
|
-
export * from
|
|
1
|
+
export * from "./locktime.js";
|
|
2
|
+
export * from "./ref.js";
|
|
3
|
+
export * from "./scribe.js";
|
|
4
|
+
export * from "./sequence.js";
|