@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,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { TxData, TxInput, TxInputTemplate, TxOutput, TxOutputTemplate, TxSpendData, TxSpendInput, TxTemplate } from "../../types/index.js";
|
|
2
2
|
export declare function assert_tx_template(txdata: unknown): asserts txdata is TxTemplate;
|
|
3
3
|
export declare function assert_has_prevouts(vin: TxInput[]): asserts vin is TxSpendInput[];
|
|
4
4
|
export declare function assert_tx_data(txdata: unknown): asserts txdata is TxData;
|
package/dist/lib/tx/validate.js
CHANGED
|
@@ -1,28 +1,56 @@
|
|
|
1
|
-
import * as Schema from
|
|
1
|
+
import * as Schema from "../../schema/index.js";
|
|
2
|
+
import { ValidationError } from "../../error.js";
|
|
3
|
+
function format_zod_error(error) {
|
|
4
|
+
const issues = error.issues.map((issue) => {
|
|
5
|
+
const path = issue.path.length > 0 ? `${issue.path.join(".")}: ` : "";
|
|
6
|
+
return `${path}${issue.message}`;
|
|
7
|
+
});
|
|
8
|
+
return issues.join("; ");
|
|
9
|
+
}
|
|
2
10
|
export function assert_tx_template(txdata) {
|
|
3
|
-
Schema.tx.tx_template.
|
|
11
|
+
const result = Schema.tx.tx_template.safeParse(txdata);
|
|
12
|
+
if (!result.success) {
|
|
13
|
+
throw new ValidationError(`invalid transaction template: ${format_zod_error(result.error)}`);
|
|
14
|
+
}
|
|
4
15
|
}
|
|
5
16
|
export function assert_has_prevouts(vin) {
|
|
6
|
-
|
|
7
|
-
|
|
17
|
+
const missingIdx = vin.findIndex((txin) => txin.prevout === null || txin.prevout === undefined);
|
|
18
|
+
if (missingIdx !== -1) {
|
|
19
|
+
throw new ValidationError(`transaction input at index ${missingIdx} is missing prevout data. ` +
|
|
20
|
+
`Prevout (previous output) is required for signing`);
|
|
8
21
|
}
|
|
9
22
|
}
|
|
10
23
|
export function assert_tx_data(txdata) {
|
|
11
|
-
Schema.tx.tx_data.
|
|
24
|
+
const result = Schema.tx.tx_data.safeParse(txdata);
|
|
25
|
+
if (!result.success) {
|
|
26
|
+
throw new ValidationError(`invalid transaction data: ${format_zod_error(result.error)}`);
|
|
27
|
+
}
|
|
12
28
|
}
|
|
13
29
|
export function assert_tx_spend_data(txdata) {
|
|
14
30
|
assert_tx_data(txdata);
|
|
15
31
|
assert_has_prevouts(txdata.vin);
|
|
16
32
|
}
|
|
17
33
|
export function assert_tx_input(tx_input) {
|
|
18
|
-
Schema.tx.tx_input.
|
|
34
|
+
const result = Schema.tx.tx_input.safeParse(tx_input);
|
|
35
|
+
if (!result.success) {
|
|
36
|
+
throw new ValidationError(`invalid transaction input: ${format_zod_error(result.error)}`);
|
|
37
|
+
}
|
|
19
38
|
}
|
|
20
39
|
export function assert_tx_output(tx_output) {
|
|
21
|
-
Schema.tx.tx_output.
|
|
40
|
+
const result = Schema.tx.tx_output.safeParse(tx_output);
|
|
41
|
+
if (!result.success) {
|
|
42
|
+
throw new ValidationError(`invalid transaction output: ${format_zod_error(result.error)}`);
|
|
43
|
+
}
|
|
22
44
|
}
|
|
23
45
|
export function assert_vin_template(vin) {
|
|
24
|
-
Schema.tx.vin_template.
|
|
46
|
+
const result = Schema.tx.vin_template.safeParse(vin);
|
|
47
|
+
if (!result.success) {
|
|
48
|
+
throw new ValidationError(`invalid input template: ${format_zod_error(result.error)}`);
|
|
49
|
+
}
|
|
25
50
|
}
|
|
26
51
|
export function assert_vout_template(vout) {
|
|
27
|
-
Schema.tx.vout_template.
|
|
52
|
+
const result = Schema.tx.vout_template.safeParse(vout);
|
|
53
|
+
if (!result.success) {
|
|
54
|
+
throw new ValidationError(`invalid output template: ${format_zod_error(result.error)}`);
|
|
55
|
+
}
|
|
28
56
|
}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export * from
|
|
2
|
-
export * from
|
|
1
|
+
export * from "./parse.js";
|
|
2
|
+
export * from "./util.js";
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export * from
|
|
2
|
-
export * from
|
|
1
|
+
export * from "./parse.js";
|
|
2
|
+
export * from "./util.js";
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import { Bytes } from
|
|
2
|
-
import type { WitnessData } from
|
|
1
|
+
import { type Bytes } from "@vbyte/buff";
|
|
2
|
+
import type { WitnessData } from "../../types/index.js";
|
|
3
3
|
export declare function parse_witness(witness: Bytes[]): WitnessData;
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { Buff } from
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
1
|
+
import { Buff } from "@vbyte/buff";
|
|
2
|
+
import { TAPLEAF_VERSIONS } from "../../const.js";
|
|
3
|
+
import { is_valid_script } from "../../lib/script/decode.js";
|
|
4
4
|
export function parse_witness(witness) {
|
|
5
|
-
const elems = witness.map(e => Buff.bytes(e));
|
|
6
|
-
const stack = witness.map(e => Buff.bytes(e).hex);
|
|
5
|
+
const elems = witness.map((e) => Buff.bytes(e));
|
|
6
|
+
const stack = witness.map((e) => Buff.bytes(e).hex);
|
|
7
7
|
const annex = parse_annex_data(elems);
|
|
8
8
|
if (annex !== null)
|
|
9
9
|
elems.pop();
|
|
@@ -15,14 +15,12 @@ export function parse_witness(witness) {
|
|
|
15
15
|
const script = parse_witness_script(elems, type);
|
|
16
16
|
if (script !== null)
|
|
17
17
|
elems.pop();
|
|
18
|
-
const params = elems.map(e => e.hex);
|
|
18
|
+
const params = elems.map((e) => e.hex);
|
|
19
19
|
return { annex, cblock, params, script, stack, type, version };
|
|
20
20
|
}
|
|
21
21
|
function parse_annex_data(data) {
|
|
22
|
-
|
|
23
|
-
if (data.length > 1 &&
|
|
24
|
-
elem instanceof Uint8Array &&
|
|
25
|
-
elem[0] === 0x50) {
|
|
22
|
+
const elem = data.at(-1);
|
|
23
|
+
if (data.length > 1 && elem instanceof Uint8Array && elem[0] === 0x50) {
|
|
26
24
|
return new Buff(elem).hex;
|
|
27
25
|
}
|
|
28
26
|
else {
|
|
@@ -30,7 +28,7 @@ function parse_annex_data(data) {
|
|
|
30
28
|
}
|
|
31
29
|
}
|
|
32
30
|
function parse_cblock_data(data) {
|
|
33
|
-
|
|
31
|
+
const elem = data.at(-1);
|
|
34
32
|
if (data.length > 1 &&
|
|
35
33
|
elem instanceof Uint8Array &&
|
|
36
34
|
elem.length > 32 &&
|
|
@@ -44,34 +42,37 @@ function parse_cblock_data(data) {
|
|
|
44
42
|
function parse_witness_script(elems, type) {
|
|
45
43
|
let script;
|
|
46
44
|
switch (type) {
|
|
47
|
-
case
|
|
45
|
+
case "p2ts":
|
|
48
46
|
script = elems.at(-1);
|
|
49
|
-
|
|
47
|
+
break;
|
|
48
|
+
case "p2wsh":
|
|
50
49
|
script = elems.at(-1);
|
|
50
|
+
break;
|
|
51
51
|
}
|
|
52
|
-
return
|
|
52
|
+
return script !== undefined ? new Buff(script).hex : null;
|
|
53
53
|
}
|
|
54
54
|
function parse_witness_type(elems, cblock) {
|
|
55
|
-
|
|
55
|
+
const param_0 = elems.at(0), param_1 = elems.at(1), param_x = elems.at(-1);
|
|
56
56
|
if (cblock !== null && param_x !== undefined) {
|
|
57
|
-
return
|
|
57
|
+
return "p2ts";
|
|
58
58
|
}
|
|
59
59
|
else if (elems.length === 2 &&
|
|
60
60
|
param_0 !== undefined &&
|
|
61
61
|
param_1 !== undefined &&
|
|
62
|
-
param_0.length >=
|
|
62
|
+
param_0.length >= 71 &&
|
|
63
|
+
param_0.length <= 73 &&
|
|
63
64
|
param_1.length === 33) {
|
|
64
|
-
return
|
|
65
|
+
return "p2wpkh";
|
|
65
66
|
}
|
|
66
67
|
else if (elems.length === 1 &&
|
|
67
68
|
param_0 !== undefined &&
|
|
68
|
-
param_0.length === 64) {
|
|
69
|
-
return
|
|
69
|
+
(param_0.length === 64 || param_0.length === 65)) {
|
|
70
|
+
return "p2tr";
|
|
70
71
|
}
|
|
71
72
|
else if (elems.length > 1 &&
|
|
72
73
|
param_x !== undefined &&
|
|
73
74
|
is_valid_script(param_x)) {
|
|
74
|
-
return
|
|
75
|
+
return "p2wsh";
|
|
75
76
|
}
|
|
76
77
|
else {
|
|
77
78
|
return null;
|
|
@@ -80,9 +81,9 @@ function parse_witness_type(elems, cblock) {
|
|
|
80
81
|
function parse_witness_version(type) {
|
|
81
82
|
if (type === null)
|
|
82
83
|
return null;
|
|
83
|
-
if (type.startsWith(
|
|
84
|
+
if (type.startsWith("p2w"))
|
|
84
85
|
return 0;
|
|
85
|
-
if (type.startsWith(
|
|
86
|
+
if (type.startsWith("p2t"))
|
|
86
87
|
return 1;
|
|
87
88
|
return null;
|
|
88
89
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Bytes } from
|
|
2
|
-
import type { WitnessSize } from
|
|
1
|
+
import { type Bytes } from "@vbyte/buff";
|
|
2
|
+
import type { WitnessSize } from "../../types/index.js";
|
|
3
3
|
export declare function get_witness_size(witness: Bytes[]): WitnessSize;
|
|
4
4
|
export declare function assert_witness(witness: unknown): asserts witness is Bytes[];
|
package/dist/lib/witness/util.js
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import { Buff } from
|
|
2
|
-
import { Assert } from
|
|
1
|
+
import { Buff } from "@vbyte/buff";
|
|
2
|
+
import { Assert } from "@vbyte/util";
|
|
3
3
|
const WIT_LENGTH_BYTE = 1;
|
|
4
4
|
export function get_witness_size(witness) {
|
|
5
|
-
const stack = witness.map(e => Buff.bytes(e));
|
|
5
|
+
const stack = witness.map((e) => Buff.bytes(e));
|
|
6
6
|
const size = stack.reduce((prev, next) => prev + next.length, 0);
|
|
7
7
|
const vsize = Math.ceil(WIT_LENGTH_BYTE + size / 4);
|
|
8
8
|
return { total: size, vsize };
|
|
9
9
|
}
|
|
10
10
|
export function assert_witness(witness) {
|
|
11
|
-
Assert.ok(Array.isArray(witness),
|
|
12
|
-
Assert.ok(witness.every(e => Buff.is_bytes(e)),
|
|
11
|
+
Assert.ok(Array.isArray(witness), "witness must be an array");
|
|
12
|
+
Assert.ok(witness.every((e) => Buff.is_bytes(e)), "witness must be an array of strings or bytes");
|
|
13
13
|
}
|