@vbyte/btc-dev 1.1.7 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (174) hide show
  1. package/CHANGELOG.md +94 -0
  2. package/README.md +260 -3
  3. package/dist/const.d.ts +3 -0
  4. package/dist/const.js +23 -22
  5. package/dist/index.d.ts +11 -11
  6. package/dist/index.js +10 -10
  7. package/dist/lib/address/api.d.ts +2 -2
  8. package/dist/lib/address/api.js +12 -12
  9. package/dist/lib/address/encode.d.ts +1 -1
  10. package/dist/lib/address/encode.js +24 -24
  11. package/dist/lib/address/index.d.ts +6 -6
  12. package/dist/lib/address/index.js +6 -6
  13. package/dist/lib/address/p2pkh.d.ts +2 -2
  14. package/dist/lib/address/p2pkh.js +14 -14
  15. package/dist/lib/address/p2sh.d.ts +2 -2
  16. package/dist/lib/address/p2sh.js +13 -13
  17. package/dist/lib/address/p2tr.d.ts +2 -2
  18. package/dist/lib/address/p2tr.js +13 -13
  19. package/dist/lib/address/p2wpkh.d.ts +2 -2
  20. package/dist/lib/address/p2wpkh.js +14 -14
  21. package/dist/lib/address/p2wsh.d.ts +2 -2
  22. package/dist/lib/address/p2wsh.js +13 -13
  23. package/dist/lib/address/script.d.ts +1 -1
  24. package/dist/lib/address/script.js +16 -16
  25. package/dist/lib/address/util.d.ts +1 -1
  26. package/dist/lib/address/util.js +22 -22
  27. package/dist/lib/meta/index.d.ts +4 -4
  28. package/dist/lib/meta/index.js +4 -4
  29. package/dist/lib/meta/locktime.d.ts +1 -1
  30. package/dist/lib/meta/locktime.js +12 -12
  31. package/dist/lib/meta/ref.js +9 -6
  32. package/dist/lib/meta/scribe.d.ts +2 -2
  33. package/dist/lib/meta/scribe.js +48 -53
  34. package/dist/lib/meta/sequence.d.ts +1 -1
  35. package/dist/lib/meta/sequence.js +16 -15
  36. package/dist/lib/script/decode.d.ts +2 -2
  37. package/dist/lib/script/decode.js +50 -15
  38. package/dist/lib/script/encode.d.ts +1 -1
  39. package/dist/lib/script/encode.js +20 -16
  40. package/dist/lib/script/index.d.ts +5 -13
  41. package/dist/lib/script/index.js +5 -14
  42. package/dist/lib/script/lock.d.ts +2 -2
  43. package/dist/lib/script/lock.js +15 -12
  44. package/dist/lib/script/util.js +4 -4
  45. package/dist/lib/script/words.js +129 -129
  46. package/dist/lib/sighash/index.d.ts +3 -3
  47. package/dist/lib/sighash/index.js +3 -3
  48. package/dist/lib/sighash/segwit.d.ts +2 -2
  49. package/dist/lib/sighash/segwit.js +15 -14
  50. package/dist/lib/sighash/taproot.d.ts +2 -2
  51. package/dist/lib/sighash/taproot.js +24 -23
  52. package/dist/lib/sighash/util.d.ts +2 -2
  53. package/dist/lib/sighash/util.js +7 -7
  54. package/dist/lib/signer/index.d.ts +2 -2
  55. package/dist/lib/signer/index.js +2 -2
  56. package/dist/lib/signer/sign.d.ts +1 -1
  57. package/dist/lib/signer/sign.js +42 -7
  58. package/dist/lib/signer/verify.d.ts +17 -3
  59. package/dist/lib/signer/verify.js +233 -3
  60. package/dist/lib/taproot/cblock.d.ts +1 -1
  61. package/dist/lib/taproot/cblock.js +14 -16
  62. package/dist/lib/taproot/encode.d.ts +1 -1
  63. package/dist/lib/taproot/encode.js +7 -7
  64. package/dist/lib/taproot/index.d.ts +4 -4
  65. package/dist/lib/taproot/index.js +4 -4
  66. package/dist/lib/taproot/parse.d.ts +1 -1
  67. package/dist/lib/taproot/parse.js +12 -14
  68. package/dist/lib/taproot/tree.d.ts +2 -2
  69. package/dist/lib/taproot/tree.js +11 -7
  70. package/dist/lib/tx/create.d.ts +1 -1
  71. package/dist/lib/tx/create.js +28 -12
  72. package/dist/lib/tx/decode.d.ts +2 -2
  73. package/dist/lib/tx/decode.js +50 -17
  74. package/dist/lib/tx/encode.d.ts +2 -2
  75. package/dist/lib/tx/encode.js +13 -16
  76. package/dist/lib/tx/index.d.ts +7 -7
  77. package/dist/lib/tx/index.js +7 -7
  78. package/dist/lib/tx/parse.d.ts +1 -1
  79. package/dist/lib/tx/parse.js +9 -9
  80. package/dist/lib/tx/size.d.ts +2 -2
  81. package/dist/lib/tx/size.js +9 -9
  82. package/dist/lib/tx/util.d.ts +2 -2
  83. package/dist/lib/tx/util.js +23 -22
  84. package/dist/lib/tx/validate.d.ts +1 -1
  85. package/dist/lib/tx/validate.js +3 -3
  86. package/dist/lib/witness/index.d.ts +2 -2
  87. package/dist/lib/witness/index.js +2 -2
  88. package/dist/lib/witness/parse.d.ts +2 -2
  89. package/dist/lib/witness/parse.js +24 -23
  90. package/dist/lib/witness/util.d.ts +2 -2
  91. package/dist/lib/witness/util.js +5 -5
  92. package/dist/main.cjs +2308 -1007
  93. package/dist/main.cjs.map +1 -1
  94. package/dist/module.mjs +2308 -1007
  95. package/dist/module.mjs.map +1 -1
  96. package/dist/package.json +20 -17
  97. package/dist/schema/base.d.ts +1 -1
  98. package/dist/schema/base.js +17 -13
  99. package/dist/schema/index.d.ts +2 -2
  100. package/dist/schema/index.js +2 -2
  101. package/dist/schema/taproot.d.ts +1 -1
  102. package/dist/schema/taproot.js +2 -2
  103. package/dist/schema/tx.d.ts +1 -1
  104. package/dist/schema/tx.js +4 -4
  105. package/dist/script.js +8 -8
  106. package/dist/script.js.map +1 -1
  107. package/dist/types/address.d.ts +4 -4
  108. package/dist/types/index.d.ts +8 -8
  109. package/dist/types/index.js +8 -8
  110. package/dist/types/meta.d.ts +4 -4
  111. package/dist/types/psbt.d.ts +2 -2
  112. package/dist/types/script.d.ts +2 -2
  113. package/dist/types/sighash.d.ts +2 -2
  114. package/dist/types/witness.d.ts +5 -5
  115. package/package.json +20 -17
  116. package/src/const.ts +0 -61
  117. package/src/index.ts +0 -13
  118. package/src/lib/address/api.ts +0 -50
  119. package/src/lib/address/encode.ts +0 -183
  120. package/src/lib/address/index.ts +0 -7
  121. package/src/lib/address/p2pkh.ts +0 -94
  122. package/src/lib/address/p2sh.ts +0 -96
  123. package/src/lib/address/p2tr.ts +0 -91
  124. package/src/lib/address/p2wpkh.ts +0 -94
  125. package/src/lib/address/p2wsh.ts +0 -92
  126. package/src/lib/address/script.ts +0 -63
  127. package/src/lib/address/util.ts +0 -87
  128. package/src/lib/meta/index.ts +0 -4
  129. package/src/lib/meta/locktime.ts +0 -57
  130. package/src/lib/meta/ref.ts +0 -107
  131. package/src/lib/meta/scribe.ts +0 -256
  132. package/src/lib/meta/sequence.ts +0 -146
  133. package/src/lib/script/decode.ts +0 -85
  134. package/src/lib/script/encode.ts +0 -129
  135. package/src/lib/script/index.ts +0 -20
  136. package/src/lib/script/lock.ts +0 -73
  137. package/src/lib/script/util.ts +0 -78
  138. package/src/lib/script/words.ts +0 -182
  139. package/src/lib/sighash/index.ts +0 -3
  140. package/src/lib/sighash/segwit.ts +0 -152
  141. package/src/lib/sighash/taproot.ts +0 -206
  142. package/src/lib/sighash/util.ts +0 -51
  143. package/src/lib/signer/index.ts +0 -2
  144. package/src/lib/signer/sign.ts +0 -39
  145. package/src/lib/signer/verify.ts +0 -88
  146. package/src/lib/taproot/cblock.ts +0 -96
  147. package/src/lib/taproot/encode.ts +0 -49
  148. package/src/lib/taproot/index.ts +0 -4
  149. package/src/lib/taproot/parse.ts +0 -65
  150. package/src/lib/taproot/tree.ts +0 -94
  151. package/src/lib/tx/create.ts +0 -90
  152. package/src/lib/tx/decode.ts +0 -123
  153. package/src/lib/tx/encode.ts +0 -155
  154. package/src/lib/tx/index.ts +0 -7
  155. package/src/lib/tx/parse.ts +0 -69
  156. package/src/lib/tx/size.ts +0 -68
  157. package/src/lib/tx/util.ts +0 -113
  158. package/src/lib/tx/validate.ts +0 -49
  159. package/src/lib/witness/index.ts +0 -2
  160. package/src/lib/witness/parse.ts +0 -127
  161. package/src/lib/witness/util.ts +0 -18
  162. package/src/schema/base.ts +0 -57
  163. package/src/schema/index.ts +0 -2
  164. package/src/schema/taproot.ts +0 -12
  165. package/src/schema/tx.ts +0 -48
  166. package/src/types/address.ts +0 -35
  167. package/src/types/index.ts +0 -8
  168. package/src/types/meta.ts +0 -48
  169. package/src/types/psbt.ts +0 -15
  170. package/src/types/script.ts +0 -18
  171. package/src/types/sighash.ts +0 -16
  172. package/src/types/taproot.ts +0 -41
  173. package/src/types/txdata.ts +0 -85
  174. package/src/types/witness.ts +0 -42
@@ -1,7 +1,7 @@
1
- import type { ScriptInfo } from './script.js';
2
- export type AddressFormat = 'base58' | 'bech32' | 'bech32m';
3
- export type AddressType = 'p2pkh' | 'p2sh' | 'p2wpkh' | 'p2wsh' | 'p2tr';
4
- export type ChainNetwork = 'main' | 'testnet' | 'regtest' | string;
1
+ import type { ScriptInfo } from "./script.js";
2
+ export type AddressFormat = "base58" | "bech32" | "bech32m";
3
+ export type AddressType = "p2pkh" | "p2sh" | "p2wpkh" | "p2wsh" | "p2tr";
4
+ export type ChainNetwork = "main" | "testnet" | "regtest" | string;
5
5
  export type AddressConfigEntry = [
6
6
  prefix: string,
7
7
  type: AddressType,
@@ -1,8 +1,8 @@
1
- export * from './address.js';
2
- export * from './meta.js';
3
- export * from './psbt.js';
4
- export * from './script.js';
5
- export * from './sighash.js';
6
- export * from './taproot.js';
7
- export * from './txdata.js';
8
- export * from './witness.js';
1
+ export * from "./address.js";
2
+ export * from "./meta.js";
3
+ export * from "./psbt.js";
4
+ export * from "./script.js";
5
+ export * from "./sighash.js";
6
+ export * from "./taproot.js";
7
+ export * from "./txdata.js";
8
+ export * from "./witness.js";
@@ -1,8 +1,8 @@
1
- export * from './address.js';
2
- export * from './meta.js';
3
- export * from './psbt.js';
4
- export * from './script.js';
5
- export * from './sighash.js';
6
- export * from './taproot.js';
7
- export * from './txdata.js';
8
- export * from './witness.js';
1
+ export * from "./address.js";
2
+ export * from "./meta.js";
3
+ export * from "./psbt.js";
4
+ export * from "./script.js";
5
+ export * from "./sighash.js";
6
+ export * from "./taproot.js";
7
+ export * from "./txdata.js";
8
+ export * from "./witness.js";
@@ -2,20 +2,20 @@ export type LocktimeData = LocktimeStamp | LocktimeHeight;
2
2
  export type SequenceConfig = Partial<SequenceData>;
3
3
  export type SequenceData = SequenceHeightLock | SequenceStampLock;
4
4
  export interface LocktimeStamp {
5
- type: 'timelock';
5
+ type: "timelock";
6
6
  stamp: number;
7
7
  }
8
8
  export interface LocktimeHeight {
9
- type: 'heightlock';
9
+ type: "heightlock";
10
10
  height: number;
11
11
  }
12
12
  export interface SequenceStampLock {
13
13
  stamp: number;
14
- mode: 'stamp';
14
+ mode: "stamp";
15
15
  }
16
16
  export interface SequenceHeightLock {
17
17
  height: number;
18
- mode: 'height';
18
+ mode: "height";
19
19
  }
20
20
  export interface LocktimeField {
21
21
  hex: string;
@@ -1,5 +1,5 @@
1
- import type { Transaction } from '@scure/btc-signer';
2
- import type { TransactionInput, TransactionOutput } from '@scure/btc-signer/psbt';
1
+ import type { Transaction } from "@scure/btc-signer";
2
+ import type { TransactionInput, TransactionOutput } from "@scure/btc-signer/psbt.js";
3
3
  export type PSBTData = Transaction;
4
4
  export type PSBTInput = TransactionInput;
5
5
  export type PSBTOutput = TransactionOutput;
@@ -1,5 +1,5 @@
1
- export type LockScriptType = 'p2pkh' | 'p2sh' | 'p2wpkh' | 'p2wsh' | 'p2tr' | 'opreturn';
2
- export type SpendScriptType = 'p2pkh' | 'p2sh' | 'p2wpkh' | 'p2wsh' | 'p2tr' | 'p2ts';
1
+ export type LockScriptType = "p2pkh" | "p2sh" | "p2wpkh" | "p2wsh" | "p2tr" | "opreturn";
2
+ export type SpendScriptType = "p2pkh" | "p2sh" | "p2wpkh" | "p2wsh" | "p2tr" | "p2ts";
3
3
  export type WitnessVersion = 0 | 1 | null;
4
4
  export interface ScriptInfo {
5
5
  asm: string[];
@@ -1,5 +1,5 @@
1
- import { TxInput } from './txdata.js';
2
- export type SigHashType = 'segwit' | 'taproot';
1
+ import type { TxInput } from "./txdata.js";
2
+ export type SigHashType = "segwit" | "taproot";
3
3
  export interface SigHashOptions {
4
4
  extension?: string;
5
5
  extflag?: number;
@@ -1,4 +1,4 @@
1
- import type { SpendScriptType, WitnessVersion } from './script.js';
1
+ import type { SpendScriptType, WitnessVersion } from "./script.js";
2
2
  export type WitnessContext = WitnessData | TaprootScript | SegwitScript | TaprootSpend | SegwitSpend;
3
3
  export interface WitnessSize {
4
4
  total: number;
@@ -16,19 +16,19 @@ export interface WitnessData {
16
16
  export interface TaprootScript extends WitnessData {
17
17
  cblock: string;
18
18
  script: string;
19
- type: 'p2ts';
19
+ type: "p2ts";
20
20
  version: 1;
21
21
  }
22
22
  export interface SegwitScript extends WitnessData {
23
23
  script: string;
24
- type: 'p2wsh';
24
+ type: "p2wsh";
25
25
  version: 0;
26
26
  }
27
27
  export interface TaprootSpend extends WitnessData {
28
- type: 'p2tr';
28
+ type: "p2tr";
29
29
  version: 1;
30
30
  }
31
31
  export interface SegwitSpend extends WitnessData {
32
- type: 'p2wpkh';
32
+ type: "p2wpkh";
33
33
  version: 0;
34
34
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vbyte/btc-dev",
3
- "version": "1.1.7",
3
+ "version": "2.0.0",
4
4
  "description": "Batteries-included toolset for plebian bitcoin development",
5
5
  "type": "module",
6
6
  "keywords": [
@@ -24,8 +24,8 @@
24
24
  "files": [
25
25
  "README.md",
26
26
  "LICENSE",
27
- "dist",
28
- "src"
27
+ "CHANGELOG.md",
28
+ "dist"
29
29
  ],
30
30
  "main": "./dist/main.cjs",
31
31
  "module": "./dist/index.js",
@@ -33,9 +33,9 @@
33
33
  "types": "./dist/types/index.d.ts",
34
34
  "exports": {
35
35
  ".": {
36
+ "types": "./dist/types/index.d.ts",
36
37
  "import": "./dist/index.js",
37
- "default": "./dist/index.js",
38
- "types": "./dist/index.d.ts"
38
+ "default": "./dist/index.js"
39
39
  },
40
40
  "./address": {
41
41
  "types": "./dist/lib/address/index.d.ts",
@@ -72,6 +72,8 @@
72
72
  },
73
73
  "scripts": {
74
74
  "build": "./scripts/build.sh",
75
+ "check": "npx tsc --project tsconfig.json --noEmit",
76
+ "lint": "biome check src/",
75
77
  "package": "npm test | faucet && npm run build",
76
78
  "release": "./scripts/release.sh",
77
79
  "scratch": "npm run script test/scratch.ts",
@@ -79,27 +81,28 @@
79
81
  "test": "npm run script test/src/tape.ts"
80
82
  },
81
83
  "dependencies": {
82
- "@noble/curves": "1.9.7",
83
- "@noble/hashes": "1.8.0",
84
- "@scure/btc-signer": "1.8.1",
85
- "@vbyte/buff": "^1.0.2",
84
+ "@noble/curves": "2.0.1",
85
+ "@noble/hashes": "2.0.1",
86
+ "@scure/btc-signer": "2.0.1",
87
+ "@vbyte/buff": "^1.0.4",
86
88
  "@vbyte/micro-lib": "^1.1.2",
87
- "zod": "^4.1.5"
89
+ "zod": "^4.3.5"
88
90
  },
89
91
  "devDependencies": {
92
+ "@biomejs/biome": "2.3.12",
90
93
  "@cmdcode/core-cmd": "^1.6.5",
91
- "@rollup/plugin-commonjs": "^28.0.6",
94
+ "@rollup/plugin-commonjs": "^29.0.0",
92
95
  "@rollup/plugin-json": "^6.1.0",
93
- "@rollup/plugin-node-resolve": "^16.0.1",
96
+ "@rollup/plugin-node-resolve": "^16.0.3",
94
97
  "@rollup/plugin-terser": "^0.4.4",
95
- "@rollup/plugin-typescript": "^12.1.4",
96
- "@types/node": "^24.3.1",
98
+ "@rollup/plugin-typescript": "^12.3.0",
99
+ "@types/node": "^25.0.10",
97
100
  "@types/tape": "^5.8.1",
98
101
  "faucet": "^0.0.4",
99
- "rollup": "^4.50.1",
102
+ "rollup": "^4.55.3",
100
103
  "tape": "^5.9.0",
101
104
  "tslib": "^2.8.1",
102
- "tsx": "^4.20.5",
103
- "typescript": "^5.9.2"
105
+ "tsx": "^4.21.0",
106
+ "typescript": "^5.9.3"
104
107
  }
105
108
  }
package/src/const.ts DELETED
@@ -1,61 +0,0 @@
1
- export const COINBASE = {
2
- TXID : '00'.repeat(32),
3
- VOUT : 0xFFFFFFFF,
4
- }
5
-
6
- export const DEFAULT = {
7
- LOCKTIME : 0,
8
- SEQUENCE : 0xFFFFFFFF,
9
- VERSION : 2,
10
- }
11
-
12
- export const TAPLEAF_VERSIONS = [
13
- 0xc0, 0xc2, 0xc4, 0xc6, 0xc8, 0xca, 0xcc, 0xce,
14
- 0xd0, 0xd2, 0xd4, 0xd6, 0xd8, 0xda, 0xdc, 0xde,
15
- 0xe0, 0xe2, 0xe4, 0xe6, 0xe8, 0xea, 0xec, 0xee,
16
- 0xf0, 0xf2, 0xf4, 0xf6, 0xf8, 0xfa, 0xfc, 0xfe,
17
- 0x66, 0x7e, 0x80, 0x84, 0x96, 0x98, 0xba, 0xbc,
18
- 0xbe
19
- ]
20
-
21
- export const TAPLEAF_DEFAULT_VERSION = 0xc0
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
-
41
- export const LOCK_SCRIPT_REGEX : Record<string, RegExp> = {
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
49
-
50
- export const SCRIPT_INT_KEY = ''
51
-
52
- export const TX_SIZE = {
53
- GLOBAL_BASE : 8,
54
- GLOBAL_WIT : 10,
55
- TXIN_BASE : 32 + 4 + 4,
56
- TXOUT_BASE : 8,
57
- }
58
-
59
- export const SIGHASH_DEFAULT = 0x01
60
- export const SIGHASH_SEGWIT = [ 0x01, 0x02, 0x03, 0x81, 0x82, 0x83 ]
61
- export const SIGHASH_TAPROOT = [ 0x00, ...SIGHASH_SEGWIT ]
package/src/index.ts DELETED
@@ -1,13 +0,0 @@
1
- export * as ADDRESS from './lib/address/index.js'
2
- export * as META from './lib/meta/index.js'
3
- export * as SCRIPT from './lib/script/index.js'
4
- export * as SIGHASH from './lib/sighash/index.js'
5
- export * as SIGNER from './lib/signer/index.js'
6
- export * as TAPROOT from './lib/taproot/index.js'
7
- export * as TX from './lib/tx/index.js'
8
- export * as WITNESS from './lib/witness/index.js'
9
-
10
- export * as CONST from './const.js'
11
- export * as SCHEMA from './schema/index.js'
12
-
13
- export type * from './types/index.js'
@@ -1,50 +0,0 @@
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
- * Get the address for a given locking script.
16
- *
17
- * @param script - The locking script.
18
- * @param network - The network to use.
19
- * @returns The address.
20
- */
21
- export function get_address (
22
- script : Bytes,
23
- network : ChainNetwork = 'main'
24
- ) : string {
25
- // Convert the script into bytes.
26
- const bytes = Buff.bytes(script)
27
- // Get the address configuration.
28
- const type = get_lock_script_type(bytes)
29
- // If the script type is not recognized, throw an error.
30
- if (type === null) throw new Error('unknown locking script: ' + bytes.hex)
31
- // Create the address based on the script type.
32
- switch (type) {
33
- case LOCK_SCRIPT_TYPE.P2PKH:
34
- return P2PKH.encode_address(script, network)
35
- case LOCK_SCRIPT_TYPE.P2SH:
36
- return P2SH.encode_address(script, network)
37
- case LOCK_SCRIPT_TYPE.P2WPKH:
38
- return P2WPKH.encode_address(script, network)
39
- case LOCK_SCRIPT_TYPE.P2WSH:
40
- return P2WSH.encode_address(script, network)
41
- case LOCK_SCRIPT_TYPE.P2TR:
42
- return P2TR.encode_address(script, network)
43
- default:
44
- throw new Error('unknown script type: ' + type)
45
- }
46
- }
47
-
48
- export function parse_address (address : string) : AddressInfo {
49
- return get_address_info(address)
50
- }
@@ -1,183 +0,0 @@
1
- import { Buff } from '@vbyte/buff'
2
-
3
- import { Assert, B58chk, Bech32, Bech32m } from '@vbyte/micro-lib'
4
-
5
- import type {
6
- AddressFormat,
7
- EncoderConfig,
8
- } from '@/types/address.js'
9
-
10
- const ENCODING_REGEX = {
11
- base58 : /^[13mn2][a-km-zA-HJ-NP-Z1-9]{25,34}$/,
12
- bech32 : /^(bc|tb|bcrt)1q[ac-hj-np-z02-9]{6,87}$/,
13
- bech32m : /^(bc|tb|bcrt)1p[ac-hj-np-z02-9]{6,87}$/
14
- }
15
-
16
- const VERSION = {
17
- bech32 : 0,
18
- bech32m : 1
19
- }
20
-
21
- /**
22
- * Decode an address.
23
- *
24
- * @param address - The address to decode.
25
- * @returns The decoded address.
26
- */
27
- export function decode_address (address : string) : EncoderConfig {
28
- // Get the address format.
29
- const format = get_address_format(address)
30
- // If the format is not found, throw an error.
31
- if (format === null) throw new Error('unrecognized address format: ' + format)
32
- // Decode the address based on the format.
33
- if (format === 'base58') return base58_decode(address)
34
- if (format === 'bech32') return bech32_decode(address)
35
- if (format === 'bech32m') return bech32m_decode(address)
36
- // If we didn't find a matching decoder, throw.
37
- throw new Error('unable to find a matching address configuration')
38
- }
39
-
40
- /**
41
- * Encode an address.
42
- *
43
- * @param address - The address to encode.
44
- * @returns The encoded address as a string, or null if the address is not
45
- * recognized.
46
- */
47
- export function encode_address (
48
- config : EncoderConfig
49
- ) : string {
50
- // Encode the address based on the format.
51
- if (config.format === 'base58') return base58_encode(config)
52
- if (config.format === 'bech32') return bech32_encode(config)
53
- if (config.format === 'bech32m') return bech32m_encode(config)
54
- // If the format is not recognized, throw an error.
55
- throw new Error('unrecognized encoding format: ' + config.format)
56
- }
57
-
58
- /**
59
- * Get the encoding type for a given address.
60
- *
61
- * @param address - The address to get the encoding type for.
62
- * @returns The encoding type, or null if the address is not recognized.
63
- */
64
- function get_address_format (address : string) : AddressFormat | null {
65
- // For each encoding type, check if the address matches the regex.
66
- for (const [ format, regex ] of Object.entries(ENCODING_REGEX)) {
67
- // If the address matches the regex, return the format.
68
- if (regex.test(address)) return format as AddressFormat
69
- // If the address does not match the regex, continue to the next encoding type.
70
- }
71
- // If no encoding type matches the address, return null.
72
- return null
73
- }
74
-
75
- /**
76
- * Encode data as a base58 string.
77
- *
78
- * @param config - The encoder configuration.
79
- * @returns The encoded base58 string.
80
- */
81
- function base58_encode (config : EncoderConfig) : string {
82
- // Assert the format is correct.
83
- Assert.ok(config.format === 'base58', 'encoding mismatch')
84
- // Assert the version is specified.
85
- Assert.exists(config.version, 'must specify a version')
86
- // Convert the data into bytes with a version prefix.
87
- const bytes = Buff.join([ config.version, config.data ])
88
- // Encode the data as a base58 string.
89
- return B58chk.encode(bytes)
90
- }
91
-
92
- /**
93
- * Decode data as a base58 string.
94
- *
95
- * @param encoded - The base58 string to decode.
96
- * @returns The decoded data.
97
- */
98
- function base58_decode (encoded : string) : EncoderConfig {
99
- // Decode the encoded data.
100
- const bytes = B58chk.decode(encoded)
101
- // Get the data from the decoded bytes.
102
- const data = bytes.slice(1)
103
- // Get the version from the decoded bytes.
104
- const version = bytes[0]
105
- // Return the decoded address.
106
- return { data, format: 'base58', version }
107
- }
108
-
109
- /**
110
- * Encode data as a bech32 string.
111
- *
112
- * @param config - The encoder configuration.
113
- * @returns The encoded bech32 string.
114
- */
115
- function bech32_encode (config : EncoderConfig) : string {
116
- // Assert the format is correct.
117
- Assert.ok(config.format === 'bech32', 'encoding mismatch')
118
- // Assert the prefix is specified.
119
- Assert.exists(config.prefix, 'prefix is required')
120
- // Convert the data into bytes.
121
- const bytes = Buff.bytes(config.data)
122
- // Convert the bytes into words.
123
- const words = Bech32.to_words(bytes)
124
- // Encode the data as a bech32 string.
125
- return Bech32.encode(config.prefix, [ VERSION.bech32, ...words ])
126
- }
127
-
128
- /**
129
- * Decode data as a bech32 string.
130
- *
131
- * @param encoded - The bech32 string to decode.
132
- * @returns The decoded data.
133
- */
134
- function bech32_decode (encoded : string) : EncoderConfig {
135
- // Decode the encoded data.
136
- const { prefix, words } = Bech32.decode(encoded)
137
- // Get the version and rest of the words.
138
- const [ version, ...rest ] = words
139
- // Assert the version is correct.
140
- Assert.ok(version === VERSION.bech32, 'bech32 version mismatch')
141
- // Convert the rest of the words into bytes.
142
- const data = Bech32.to_bytes(rest)
143
- // Return the decoded address.
144
- return { data, format: 'bech32', prefix, version }
145
- }
146
-
147
- /**
148
- * Encode data as a bech32 string.
149
- *
150
- * @param config - The encoder configuration.
151
- * @returns The encoded bech32 string.
152
- */
153
- function bech32m_encode (config : EncoderConfig) : string {
154
- // Assert the format is correct.
155
- Assert.ok(config.format === 'bech32m', 'encoding mismatch')
156
- // Assert the prefix is specified.
157
- Assert.exists(config.prefix, 'prefix is required')
158
- // Convert the data into bytes.
159
- const bytes = Buff.bytes(config.data)
160
- // Convert the bytes into words.
161
- const words = Bech32m.to_words(bytes)
162
- // Encode the data as a bech32m string.
163
- return Bech32m.encode(config.prefix, [ VERSION.bech32m, ...words ])
164
- }
165
-
166
- /**
167
- * Decode data as a bech32 string.
168
- *
169
- * @param encoded - The bech32 string to decode.
170
- * @returns The decoded data.
171
- */
172
- function bech32m_decode (encoded : string) : EncoderConfig {
173
- // Decode the encoded data.
174
- const { prefix, words } = Bech32m.decode(encoded)
175
- // Get the version and rest of the words.
176
- const [ version, ...rest ] = words
177
- // Assert the version is correct.
178
- Assert.ok(version === VERSION.bech32m, 'bech32m version mismatch')
179
- // Convert the rest of the words into bytes.
180
- const data = Bech32m.to_bytes(rest)
181
- // Return the decoded address.
182
- return { data, format: 'bech32m', prefix, version }
183
- }
@@ -1,7 +0,0 @@
1
- export { P2PKH } from './p2pkh.js'
2
- export { P2SH } from './p2sh.js'
3
- export { P2WPKH } from './p2wpkh.js'
4
- export { P2WSH } from './p2wsh.js'
5
- export { P2TR } from './p2tr.js'
6
-
7
- export * from './api.js'
@@ -1,94 +0,0 @@
1
- import { Buff, Bytes } from '@vbyte/buff'
2
- import { Assert } from '@vbyte/micro-lib'
3
- import { hash160 } from '@vbyte/micro-lib/hash'
4
- import { encode_address } from './encode.js'
5
- import { is_p2pkh_script } from '@/lib/script/lock.js'
6
- import { LOCK_SCRIPT_TYPE } from '@/const.js'
7
-
8
- import {
9
- get_address_config,
10
- get_address_info
11
- } from './util.js'
12
-
13
- import type {
14
- AddressInfo,
15
- ChainNetwork
16
- } from '@/types/index.js'
17
-
18
- const ADDRESS_TYPE = LOCK_SCRIPT_TYPE.P2PKH
19
-
20
- export namespace P2PKH {
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
27
- }
28
-
29
- function create_p2pkh_address (
30
- pubkey : Bytes,
31
- network : ChainNetwork = 'main',
32
- ) : string {
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')
44
- // Convert the bytes into a hash.
45
- const hash = hash160(bytes)
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' ])
52
- }
53
-
54
- function encode_p2pkh_address (
55
- script : Bytes,
56
- network : ChainNetwork = 'main',
57
- ) : string {
58
- // Get the public key hash from the script.
59
- const pk_hash = decode_p2pkh_script(script)
60
- // Get the address configuration.
61
- const config = get_address_config(network, ADDRESS_TYPE)
62
- // Assert the configuration exists.
63
- Assert.exists(config, `unrecognized address config: ${ADDRESS_TYPE} on ${network}` )
64
- // Assert the payload size is correct.
65
- Assert.size(pk_hash, config.size, `invalid payload size: ${pk_hash.length} !== ${config.size}` )
66
- // Encode the address.
67
- return encode_address({
68
- data : pk_hash,
69
- format : 'base58',
70
- version : config.version
71
- })
72
- }
73
-
74
- function decode_p2pkh_address (
75
- address : string
76
- ) : AddressInfo {
77
- // Parse the address.
78
- const parsed = get_address_info(address)
79
- // Assert the address type is correct.
80
- Assert.ok(parsed.type === 'p2pkh', `address type mismatch: ${parsed.type} !== ${ADDRESS_TYPE}`)
81
- // Return the parsed address.
82
- return parsed
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
- }