@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.
Files changed (181) hide show
  1. package/CHANGELOG.md +127 -0
  2. package/LICENSE +21 -121
  3. package/README.md +69 -3
  4. package/dist/const.d.ts +3 -0
  5. package/dist/const.js +23 -22
  6. package/dist/error.d.ts +11 -0
  7. package/dist/error.js +20 -0
  8. package/dist/index.d.ts +12 -11
  9. package/dist/index.js +11 -10
  10. package/dist/lib/address/api.d.ts +2 -2
  11. package/dist/lib/address/api.js +13 -12
  12. package/dist/lib/address/encode.d.ts +1 -1
  13. package/dist/lib/address/encode.js +26 -24
  14. package/dist/lib/address/index.d.ts +6 -6
  15. package/dist/lib/address/index.js +6 -6
  16. package/dist/lib/address/p2pkh.d.ts +2 -2
  17. package/dist/lib/address/p2pkh.js +15 -15
  18. package/dist/lib/address/p2sh.d.ts +2 -2
  19. package/dist/lib/address/p2sh.js +14 -14
  20. package/dist/lib/address/p2tr.d.ts +2 -2
  21. package/dist/lib/address/p2tr.js +14 -14
  22. package/dist/lib/address/p2wpkh.d.ts +2 -2
  23. package/dist/lib/address/p2wpkh.js +15 -15
  24. package/dist/lib/address/p2wsh.d.ts +2 -2
  25. package/dist/lib/address/p2wsh.js +14 -14
  26. package/dist/lib/address/script.d.ts +1 -1
  27. package/dist/lib/address/script.js +16 -16
  28. package/dist/lib/address/util.d.ts +1 -1
  29. package/dist/lib/address/util.js +24 -22
  30. package/dist/lib/meta/index.d.ts +4 -4
  31. package/dist/lib/meta/index.js +4 -4
  32. package/dist/lib/meta/locktime.d.ts +1 -1
  33. package/dist/lib/meta/locktime.js +13 -12
  34. package/dist/lib/meta/ref.js +13 -9
  35. package/dist/lib/meta/scribe.d.ts +2 -2
  36. package/dist/lib/meta/scribe.js +71 -56
  37. package/dist/lib/meta/sequence.d.ts +1 -1
  38. package/dist/lib/meta/sequence.js +21 -19
  39. package/dist/lib/script/decode.d.ts +2 -2
  40. package/dist/lib/script/decode.js +53 -17
  41. package/dist/lib/script/encode.d.ts +1 -1
  42. package/dist/lib/script/encode.js +21 -16
  43. package/dist/lib/script/index.d.ts +5 -13
  44. package/dist/lib/script/index.js +5 -14
  45. package/dist/lib/script/lock.d.ts +2 -2
  46. package/dist/lib/script/lock.js +15 -12
  47. package/dist/lib/script/util.js +4 -4
  48. package/dist/lib/script/words.js +131 -130
  49. package/dist/lib/sighash/index.d.ts +3 -3
  50. package/dist/lib/sighash/index.js +3 -3
  51. package/dist/lib/sighash/segwit.d.ts +2 -2
  52. package/dist/lib/sighash/segwit.js +18 -14
  53. package/dist/lib/sighash/taproot.d.ts +2 -2
  54. package/dist/lib/sighash/taproot.js +27 -23
  55. package/dist/lib/sighash/util.d.ts +2 -2
  56. package/dist/lib/sighash/util.js +8 -7
  57. package/dist/lib/signer/index.d.ts +2 -2
  58. package/dist/lib/signer/index.js +2 -2
  59. package/dist/lib/signer/sign.d.ts +1 -1
  60. package/dist/lib/signer/sign.js +43 -7
  61. package/dist/lib/signer/verify.d.ts +17 -3
  62. package/dist/lib/signer/verify.js +232 -3
  63. package/dist/lib/taproot/cblock.d.ts +1 -1
  64. package/dist/lib/taproot/cblock.js +16 -17
  65. package/dist/lib/taproot/encode.d.ts +1 -1
  66. package/dist/lib/taproot/encode.js +9 -8
  67. package/dist/lib/taproot/index.d.ts +4 -4
  68. package/dist/lib/taproot/index.js +4 -4
  69. package/dist/lib/taproot/parse.d.ts +1 -1
  70. package/dist/lib/taproot/parse.js +15 -15
  71. package/dist/lib/taproot/tree.d.ts +2 -2
  72. package/dist/lib/taproot/tree.js +12 -7
  73. package/dist/lib/tx/create.d.ts +1 -1
  74. package/dist/lib/tx/create.js +28 -12
  75. package/dist/lib/tx/decode.d.ts +2 -2
  76. package/dist/lib/tx/decode.js +52 -17
  77. package/dist/lib/tx/encode.d.ts +2 -2
  78. package/dist/lib/tx/encode.js +13 -16
  79. package/dist/lib/tx/index.d.ts +7 -7
  80. package/dist/lib/tx/index.js +7 -7
  81. package/dist/lib/tx/parse.d.ts +1 -1
  82. package/dist/lib/tx/parse.js +9 -9
  83. package/dist/lib/tx/size.d.ts +2 -2
  84. package/dist/lib/tx/size.js +9 -11
  85. package/dist/lib/tx/util.d.ts +2 -2
  86. package/dist/lib/tx/util.js +22 -20
  87. package/dist/lib/tx/validate.d.ts +1 -1
  88. package/dist/lib/tx/validate.js +37 -9
  89. package/dist/lib/witness/index.d.ts +2 -2
  90. package/dist/lib/witness/index.js +2 -2
  91. package/dist/lib/witness/parse.d.ts +2 -2
  92. package/dist/lib/witness/parse.js +24 -23
  93. package/dist/lib/witness/util.d.ts +2 -2
  94. package/dist/lib/witness/util.js +5 -5
  95. package/dist/main.cjs +3305 -2035
  96. package/dist/main.cjs.map +1 -1
  97. package/dist/module.mjs +3303 -2036
  98. package/dist/module.mjs.map +1 -1
  99. package/dist/package.json +24 -19
  100. package/dist/schema/base.d.ts +1 -1
  101. package/dist/schema/base.js +17 -13
  102. package/dist/schema/index.d.ts +2 -2
  103. package/dist/schema/index.js +2 -2
  104. package/dist/schema/taproot.d.ts +1 -1
  105. package/dist/schema/taproot.js +2 -2
  106. package/dist/schema/tx.d.ts +1 -1
  107. package/dist/schema/tx.js +4 -4
  108. package/dist/script.js +10 -12
  109. package/dist/script.js.map +1 -1
  110. package/dist/types/address.d.ts +4 -4
  111. package/dist/types/index.d.ts +8 -8
  112. package/dist/types/index.js +8 -8
  113. package/dist/types/meta.d.ts +4 -4
  114. package/dist/types/psbt.d.ts +2 -2
  115. package/dist/types/script.d.ts +2 -2
  116. package/dist/types/sighash.d.ts +2 -2
  117. package/dist/types/witness.d.ts +5 -5
  118. package/docs/API.md +1145 -0
  119. package/docs/CONVENTIONS.md +316 -0
  120. package/docs/FAQ.md +396 -0
  121. package/docs/GUIDE.md +1102 -0
  122. package/package.json +24 -19
  123. package/src/const.ts +0 -61
  124. package/src/index.ts +0 -13
  125. package/src/lib/address/api.ts +0 -50
  126. package/src/lib/address/encode.ts +0 -183
  127. package/src/lib/address/index.ts +0 -7
  128. package/src/lib/address/p2pkh.ts +0 -94
  129. package/src/lib/address/p2sh.ts +0 -96
  130. package/src/lib/address/p2tr.ts +0 -91
  131. package/src/lib/address/p2wpkh.ts +0 -94
  132. package/src/lib/address/p2wsh.ts +0 -92
  133. package/src/lib/address/script.ts +0 -63
  134. package/src/lib/address/util.ts +0 -87
  135. package/src/lib/meta/index.ts +0 -4
  136. package/src/lib/meta/locktime.ts +0 -57
  137. package/src/lib/meta/ref.ts +0 -107
  138. package/src/lib/meta/scribe.ts +0 -256
  139. package/src/lib/meta/sequence.ts +0 -146
  140. package/src/lib/script/decode.ts +0 -85
  141. package/src/lib/script/encode.ts +0 -129
  142. package/src/lib/script/index.ts +0 -20
  143. package/src/lib/script/lock.ts +0 -73
  144. package/src/lib/script/util.ts +0 -78
  145. package/src/lib/script/words.ts +0 -182
  146. package/src/lib/sighash/index.ts +0 -3
  147. package/src/lib/sighash/segwit.ts +0 -152
  148. package/src/lib/sighash/taproot.ts +0 -206
  149. package/src/lib/sighash/util.ts +0 -51
  150. package/src/lib/signer/index.ts +0 -2
  151. package/src/lib/signer/sign.ts +0 -39
  152. package/src/lib/signer/verify.ts +0 -88
  153. package/src/lib/taproot/cblock.ts +0 -96
  154. package/src/lib/taproot/encode.ts +0 -49
  155. package/src/lib/taproot/index.ts +0 -4
  156. package/src/lib/taproot/parse.ts +0 -65
  157. package/src/lib/taproot/tree.ts +0 -94
  158. package/src/lib/tx/create.ts +0 -90
  159. package/src/lib/tx/decode.ts +0 -123
  160. package/src/lib/tx/encode.ts +0 -155
  161. package/src/lib/tx/index.ts +0 -7
  162. package/src/lib/tx/parse.ts +0 -69
  163. package/src/lib/tx/size.ts +0 -68
  164. package/src/lib/tx/util.ts +0 -111
  165. package/src/lib/tx/validate.ts +0 -49
  166. package/src/lib/witness/index.ts +0 -2
  167. package/src/lib/witness/parse.ts +0 -127
  168. package/src/lib/witness/util.ts +0 -18
  169. package/src/schema/base.ts +0 -57
  170. package/src/schema/index.ts +0 -2
  171. package/src/schema/taproot.ts +0 -12
  172. package/src/schema/tx.ts +0 -48
  173. package/src/types/address.ts +0 -35
  174. package/src/types/index.ts +0 -8
  175. package/src/types/meta.ts +0 -48
  176. package/src/types/psbt.ts +0 -15
  177. package/src/types/script.ts +0 -18
  178. package/src/types/sighash.ts +0 -16
  179. package/src/types/taproot.ts +0 -41
  180. package/src/types/txdata.ts +0 -85
  181. package/src/types/witness.ts +0 -42
@@ -1,4 +1,4 @@
1
- import { TxSpendInput, TxData, TxInput, TxInputTemplate, TxOutput, TxTemplate, TxOutputTemplate, TxSpendData } from '../../types/index.js';
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;
@@ -1,28 +1,56 @@
1
- import * as Schema from '../../schema/index.js';
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.parse(txdata);
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
- if (vin.some(txin => txin.prevout === null)) {
7
- throw new Error('transaction missing prevouts');
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.parse(txdata);
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.parse(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.parse(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.parse(vin);
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.parse(vout);
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 './parse.js';
2
- export * from './util.js';
1
+ export * from "./parse.js";
2
+ export * from "./util.js";
@@ -1,2 +1,2 @@
1
- export * from './parse.js';
2
- export * from './util.js';
1
+ export * from "./parse.js";
2
+ export * from "./util.js";
@@ -1,3 +1,3 @@
1
- import { Bytes } from '@vbyte/buff';
2
- import type { WitnessData } from '../../types/index.js';
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 '@vbyte/buff';
2
- import { is_valid_script } from '../../lib/script/decode.js';
3
- import { TAPLEAF_VERSIONS } from '../../const.js';
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
- let elem = data.at(-1);
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
- let elem = data.at(-1);
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 'p2ts':
45
+ case "p2ts":
48
46
  script = elems.at(-1);
49
- case 'p2wsh':
47
+ break;
48
+ case "p2wsh":
50
49
  script = elems.at(-1);
50
+ break;
51
51
  }
52
- return (script !== undefined) ? new Buff(script).hex : null;
52
+ return script !== undefined ? new Buff(script).hex : null;
53
53
  }
54
54
  function parse_witness_type(elems, cblock) {
55
- let param_0 = elems.at(0), param_1 = elems.at(1), param_x = elems.at(-1);
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 'p2ts';
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 >= 64 &&
62
+ param_0.length >= 71 &&
63
+ param_0.length <= 73 &&
63
64
  param_1.length === 33) {
64
- return 'p2wpkh';
65
+ return "p2wpkh";
65
66
  }
66
67
  else if (elems.length === 1 &&
67
68
  param_0 !== undefined &&
68
- param_0.length === 64) {
69
- return 'p2tr';
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 'p2wsh';
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('p2w'))
84
+ if (type.startsWith("p2w"))
84
85
  return 0;
85
- if (type.startsWith('p2t'))
86
+ if (type.startsWith("p2t"))
86
87
  return 1;
87
88
  return null;
88
89
  }
@@ -1,4 +1,4 @@
1
- import { Bytes } from '@vbyte/buff';
2
- import type { WitnessSize } from '../../types/index.js';
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[];
@@ -1,13 +1,13 @@
1
- import { Buff } from '@vbyte/buff';
2
- import { Assert } from '@vbyte/micro-lib';
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), 'witness must be an array');
12
- Assert.ok(witness.every(e => Buff.is_bytes(e)), 'witness must be an array of strings or bytes');
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
  }