@btc-vision/bitcoin 6.3.1 → 6.3.2

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 (259) hide show
  1. package/.babelrc +4 -0
  2. package/.gitattributes +2 -0
  3. package/.nyc_output/6368a5b2-daa5-4821-8ed0-b742d6fc7eab.json +1 -0
  4. package/.nyc_output/processinfo/6368a5b2-daa5-4821-8ed0-b742d6fc7eab.json +1 -0
  5. package/.nyc_output/processinfo/index.json +1 -0
  6. package/.prettierrc.json +12 -0
  7. package/CHANGELOG.md +403 -0
  8. package/CONTRIBUTING.md +83 -0
  9. package/browser/address.d.ts +16 -0
  10. package/{src → browser}/bip66.d.ts +6 -7
  11. package/{src → browser}/block.d.ts +29 -30
  12. package/{src → browser}/bufferutils.d.ts +34 -54
  13. package/browser/crypto/crypto.d.ts +1 -0
  14. package/{src → browser}/crypto.d.ts +13 -18
  15. package/browser/ecc_lib.d.ts +3 -0
  16. package/browser/hooks/AdvancedSignatureManager.d.ts +16 -0
  17. package/{src → browser}/hooks/HookedSigner.d.ts +4 -4
  18. package/browser/hooks/SignatureManager.d.ts +13 -0
  19. package/browser/index.d.ts +58 -0
  20. package/browser/index.js +2 -0
  21. package/browser/index.js.LICENSE.txt +14 -0
  22. package/browser/merkle.d.ts +1 -0
  23. package/browser/networks.d.ts +23 -0
  24. package/{src → browser}/ops.d.ts +126 -126
  25. package/browser/payments/bip341.d.ts +23 -0
  26. package/browser/payments/embed.d.ts +2 -0
  27. package/browser/payments/index.d.ts +41 -0
  28. package/{src → browser}/payments/lazy.d.ts +2 -2
  29. package/browser/payments/p2ms.d.ts +2 -0
  30. package/browser/payments/p2pk.d.ts +2 -0
  31. package/browser/payments/p2pkh.d.ts +2 -0
  32. package/browser/payments/p2sh.d.ts +2 -0
  33. package/browser/payments/p2tr.d.ts +2 -0
  34. package/browser/payments/p2wpkh.d.ts +2 -0
  35. package/browser/payments/p2wsh.d.ts +2 -0
  36. package/browser/psbt/bip371.d.ts +16 -0
  37. package/browser/psbt/psbtutils.d.ts +26 -0
  38. package/{src → browser}/psbt.d.ts +167 -238
  39. package/browser/push_data.d.ts +7 -0
  40. package/browser/script.d.ts +17 -0
  41. package/browser/script_number.d.ts +2 -0
  42. package/browser/script_signature.d.ts +7 -0
  43. package/{src → browser}/transaction.d.ts +48 -60
  44. package/{src → browser}/types.d.ts +37 -54
  45. package/build/address.d.ts +16 -0
  46. package/build/address.js +148 -0
  47. package/build/bip66.d.ts +6 -0
  48. package/build/bip66.js +99 -0
  49. package/build/block.d.ts +29 -0
  50. package/build/block.js +181 -0
  51. package/build/bufferutils.d.ts +34 -0
  52. package/build/bufferutils.js +141 -0
  53. package/build/crypto/crypto.d.ts +1 -0
  54. package/build/crypto/crypto.js +1 -0
  55. package/build/crypto.d.ts +13 -0
  56. package/build/crypto.js +87 -0
  57. package/build/ecc_lib.d.ts +3 -0
  58. package/build/ecc_lib.js +61 -0
  59. package/build/hooks/AdvancedSignatureManager.d.ts +16 -0
  60. package/build/hooks/AdvancedSignatureManager.js +52 -0
  61. package/build/hooks/HookedSigner.d.ts +4 -0
  62. package/build/hooks/HookedSigner.js +64 -0
  63. package/build/hooks/SignatureManager.d.ts +13 -0
  64. package/build/hooks/SignatureManager.js +45 -0
  65. package/build/index.d.ts +58 -0
  66. package/build/index.js +32 -0
  67. package/build/merkle.d.ts +1 -0
  68. package/build/merkle.js +19 -0
  69. package/build/networks.d.ts +23 -0
  70. package/build/networks.js +121 -0
  71. package/build/ops.d.ts +126 -0
  72. package/{src → build}/ops.js +127 -131
  73. package/build/payments/bip341.d.ts +23 -0
  74. package/build/payments/bip341.js +82 -0
  75. package/build/payments/embed.d.ts +2 -0
  76. package/build/payments/embed.js +39 -0
  77. package/build/payments/index.d.ts +41 -0
  78. package/build/payments/index.js +10 -0
  79. package/build/payments/lazy.d.ts +2 -0
  80. package/{src → build}/payments/lazy.js +28 -32
  81. package/build/payments/p2ms.d.ts +2 -0
  82. package/{src → build}/payments/p2ms.js +128 -158
  83. package/build/payments/p2pk.d.ts +2 -0
  84. package/build/payments/p2pk.js +68 -0
  85. package/build/payments/p2pkh.d.ts +2 -0
  86. package/build/payments/p2pkh.js +135 -0
  87. package/build/payments/p2sh.d.ts +2 -0
  88. package/build/payments/p2sh.js +175 -0
  89. package/build/payments/p2tr.d.ts +2 -0
  90. package/build/payments/p2tr.js +254 -0
  91. package/build/payments/p2wpkh.d.ts +2 -0
  92. package/build/payments/p2wpkh.js +130 -0
  93. package/build/payments/p2wsh.d.ts +2 -0
  94. package/build/payments/p2wsh.js +180 -0
  95. package/build/psbt/bip371.d.ts +16 -0
  96. package/build/psbt/bip371.js +246 -0
  97. package/build/psbt/psbtutils.d.ts +26 -0
  98. package/build/psbt/psbtutils.js +170 -0
  99. package/build/psbt.d.ts +167 -0
  100. package/build/psbt.js +1305 -0
  101. package/build/push_data.d.ts +7 -0
  102. package/build/push_data.js +57 -0
  103. package/build/script.d.ts +17 -0
  104. package/build/script.js +167 -0
  105. package/build/script_number.d.ts +2 -0
  106. package/build/script_number.js +49 -0
  107. package/build/script_signature.d.ts +7 -0
  108. package/build/script_signature.js +49 -0
  109. package/build/transaction.d.ts +48 -0
  110. package/build/transaction.js +445 -0
  111. package/build/types.d.ts +37 -0
  112. package/build/types.js +73 -0
  113. package/cjs/package.json +3 -0
  114. package/eslint.config.js +56 -0
  115. package/gulpfile.js +42 -0
  116. package/package.json +105 -50
  117. package/src/{address.js → address.ts} +93 -73
  118. package/src/{bip66.js → bip66.ts} +23 -19
  119. package/src/{block.js → block.ts} +114 -105
  120. package/src/{bufferutils.js → bufferutils.ts} +65 -67
  121. package/src/crypto/crypto-browser.js +75 -0
  122. package/src/crypto/crypto.ts +1 -0
  123. package/src/crypto.ts +108 -0
  124. package/src/{ecc_lib.js → ecc_lib.ts} +25 -53
  125. package/src/hooks/{AdvancedSignatureManager.js → AdvancedSignatureManager.ts} +34 -18
  126. package/src/hooks/HookedSigner.ts +108 -0
  127. package/src/hooks/{SignatureManager.js → SignatureManager.ts} +26 -14
  128. package/src/index.ts +86 -0
  129. package/src/{merkle.js → merkle.ts} +8 -7
  130. package/src/{networks.js → networks.ts} +44 -29
  131. package/src/ops.ts +282 -0
  132. package/src/payments/bip341.ts +140 -0
  133. package/src/payments/embed.ts +55 -0
  134. package/src/payments/{index.d.ts → index.ts} +20 -10
  135. package/src/payments/lazy.ts +28 -0
  136. package/src/payments/p2ms.ts +150 -0
  137. package/src/payments/{p2pk.js → p2pk.ts} +32 -29
  138. package/src/payments/{p2pkh.js → p2pkh.ts} +53 -47
  139. package/src/payments/{p2sh.js → p2sh.ts} +72 -71
  140. package/src/payments/{p2tr.js → p2tr.ts} +114 -125
  141. package/src/payments/{p2wpkh.js → p2wpkh.ts} +51 -56
  142. package/src/payments/{p2wsh.js → p2wsh.ts} +69 -81
  143. package/src/psbt/{bip371.js → bip371.ts} +191 -174
  144. package/src/psbt/psbtutils.ts +299 -0
  145. package/src/{psbt.js → psbt.ts} +1025 -679
  146. package/src/{push_data.js → push_data.ts} +35 -21
  147. package/src/{script.js → script.ts} +93 -77
  148. package/src/{script_number.js → script_number.ts} +15 -21
  149. package/src/{script_signature.js → script_signature.ts} +26 -14
  150. package/src/{transaction.js → transaction.ts} +247 -167
  151. package/src/types.ts +122 -0
  152. package/test/address.spec.js +124 -0
  153. package/test/address.spec.ts +177 -0
  154. package/test/bitcoin.core.spec.js +170 -0
  155. package/test/bitcoin.core.spec.ts +234 -0
  156. package/test/block.spec.js +141 -0
  157. package/test/block.spec.ts +194 -0
  158. package/test/bufferutils.spec.js +427 -0
  159. package/test/bufferutils.spec.ts +513 -0
  160. package/test/crypto.spec.js +41 -0
  161. package/test/crypto.spec.ts +55 -0
  162. package/test/fixtures/address.json +329 -0
  163. package/test/fixtures/block.json +148 -0
  164. package/test/fixtures/bufferutils.json +102 -0
  165. package/test/fixtures/core/README.md +26 -0
  166. package/test/fixtures/core/base58_encode_decode.json +50 -0
  167. package/test/fixtures/core/base58_keys_invalid.json +152 -0
  168. package/test/fixtures/core/base58_keys_valid.json +452 -0
  169. package/test/fixtures/core/blocks.json +27 -0
  170. package/test/fixtures/core/sig_canonical.json +7 -0
  171. package/test/fixtures/core/sig_noncanonical.json +33 -0
  172. package/test/fixtures/core/sighash.json +3505 -0
  173. package/test/fixtures/core/tx_valid.json +2023 -0
  174. package/test/fixtures/crypto.json +43 -0
  175. package/test/fixtures/ecdsa.json +217 -0
  176. package/test/fixtures/ecpair.json +141 -0
  177. package/test/fixtures/embed.json +108 -0
  178. package/test/fixtures/p2ms.json +434 -0
  179. package/test/fixtures/p2pk.json +179 -0
  180. package/test/fixtures/p2pkh.json +276 -0
  181. package/test/fixtures/p2sh.json +508 -0
  182. package/test/fixtures/p2tr.json +1198 -0
  183. package/test/fixtures/p2wpkh.json +290 -0
  184. package/test/fixtures/p2wsh.json +489 -0
  185. package/test/fixtures/psbt.json +924 -0
  186. package/test/fixtures/script.json +465 -0
  187. package/test/fixtures/script_number.json +225 -0
  188. package/test/fixtures/signature.json +140 -0
  189. package/test/fixtures/transaction.json +916 -0
  190. package/test/integration/_regtest.js +7 -0
  191. package/test/integration/_regtest.ts +6 -0
  192. package/test/integration/addresses.spec.js +116 -0
  193. package/test/integration/addresses.spec.ts +154 -0
  194. package/test/integration/bip32.spec.js +85 -0
  195. package/test/integration/bip32.spec.ts +151 -0
  196. package/test/integration/blocks.spec.js +26 -0
  197. package/test/integration/blocks.spec.ts +28 -0
  198. package/test/integration/cltv.spec.js +199 -0
  199. package/test/integration/cltv.spec.ts +283 -0
  200. package/test/integration/csv.spec.js +362 -0
  201. package/test/integration/csv.spec.ts +527 -0
  202. package/test/integration/payments.spec.js +98 -0
  203. package/test/integration/payments.spec.ts +135 -0
  204. package/test/integration/taproot.spec.js +532 -0
  205. package/test/integration/taproot.spec.ts +707 -0
  206. package/test/integration/transactions.spec.js +561 -0
  207. package/test/integration/transactions.spec.ts +769 -0
  208. package/test/payments.spec.js +97 -0
  209. package/test/payments.spec.ts +125 -0
  210. package/test/payments.utils.js +190 -0
  211. package/test/payments.utils.ts +208 -0
  212. package/test/psbt.spec.js +1044 -0
  213. package/test/psbt.spec.ts +1414 -0
  214. package/test/script.spec.js +151 -0
  215. package/test/script.spec.ts +210 -0
  216. package/test/script_number.spec.js +24 -0
  217. package/test/script_number.spec.ts +29 -0
  218. package/test/script_signature.spec.js +52 -0
  219. package/test/script_signature.spec.ts +66 -0
  220. package/test/transaction.spec.js +269 -0
  221. package/test/transaction.spec.ts +387 -0
  222. package/test/ts-node-register.js +5 -0
  223. package/test/tsconfig.json +45 -0
  224. package/test/types.spec.js +46 -0
  225. package/test/types.spec.ts +58 -0
  226. package/tsconfig.base.json +27 -0
  227. package/tsconfig.json +19 -0
  228. package/tsconfig.webpack.json +18 -0
  229. package/webpack.config.js +79 -0
  230. package/src/address.d.ts +0 -42
  231. package/src/crypto.js +0 -128
  232. package/src/ecc_lib.d.ts +0 -17
  233. package/src/hooks/AdvancedSignatureManager.d.ts +0 -44
  234. package/src/hooks/HookedSigner.js +0 -90
  235. package/src/hooks/SignatureManager.d.ts +0 -35
  236. package/src/index.d.ts +0 -42
  237. package/src/index.js +0 -87
  238. package/src/merkle.d.ts +0 -10
  239. package/src/networks.d.ts +0 -83
  240. package/src/payments/bip341.d.ts +0 -49
  241. package/src/payments/bip341.js +0 -124
  242. package/src/payments/embed.d.ts +0 -9
  243. package/src/payments/embed.js +0 -54
  244. package/src/payments/index.js +0 -69
  245. package/src/payments/p2ms.d.ts +0 -9
  246. package/src/payments/p2pk.d.ts +0 -10
  247. package/src/payments/p2pkh.d.ts +0 -10
  248. package/src/payments/p2sh.d.ts +0 -10
  249. package/src/payments/p2tr.d.ts +0 -10
  250. package/src/payments/p2wpkh.d.ts +0 -10
  251. package/src/payments/p2wsh.d.ts +0 -10
  252. package/src/psbt/bip371.d.ts +0 -42
  253. package/src/psbt/psbtutils.d.ts +0 -64
  254. package/src/psbt/psbtutils.js +0 -191
  255. package/src/push_data.d.ts +0 -29
  256. package/src/script.d.ts +0 -42
  257. package/src/script_number.d.ts +0 -19
  258. package/src/script_signature.d.ts +0 -21
  259. package/src/types.js +0 -106
package/package.json CHANGED
@@ -1,11 +1,31 @@
1
1
  {
2
2
  "name": "@btc-vision/bitcoin",
3
- "version": "6.3.1",
3
+ "type": "module",
4
+ "version": "6.3.2",
4
5
  "description": "Client-side Bitcoin JavaScript library",
5
- "main": "./src/index.js",
6
- "types": "./src/index.d.ts",
7
6
  "engines": {
8
- "node": ">=8.0.0"
7
+ "node": ">=16.0.0"
8
+ },
9
+ "exports": {
10
+ ".": {
11
+ "browser": "./browser/index.js",
12
+ "import": "./build/index.js",
13
+ "require": "./build/index.js",
14
+ "types": "./build/index.d.ts"
15
+ },
16
+ "./browser": {
17
+ "import": "./browser/index.js",
18
+ "require": "./browser/index.js",
19
+ "types": "./browser/index.d.ts"
20
+ }
21
+ },
22
+ "browser": {
23
+ "./build/index.js": "./browser/index.js",
24
+ "./build/index.d.ts": "./browser/index.d.ts",
25
+ "Buffer": "buffer",
26
+ "crypto": "./src/crypto/crypto-browser.js",
27
+ "stream": "stream-browserify",
28
+ "zlib": "browserify-zlib"
9
29
  },
10
30
  "keywords": [
11
31
  "bitcoinjs",
@@ -14,11 +34,27 @@
14
34
  "javascript",
15
35
  "bitcoinjs"
16
36
  ],
37
+ "license": "MIT",
38
+ "main": "build/index.js",
39
+ "types": "build/index.d.ts",
40
+ "typings": "build/index.d.ts",
41
+ "module": "build/index.js",
42
+ "publishConfig": {
43
+ "access": "public",
44
+ "tag": "latest"
45
+ },
46
+ "repository": {
47
+ "type": "git",
48
+ "url": "https://github.com/btc-vision/bitcoin.git"
49
+ },
17
50
  "scripts": {
51
+ "watch": "gulp watch",
52
+ "build": "gulp build",
53
+ "setup": "npm i && npm run build",
54
+ "browserBuild": "webpack --mode production",
18
55
  "audit": "better-npm-audit audit -l high",
19
- "build": "npm run clean && tsc -p ./tsconfig.json && npm run formatjs",
20
56
  "build:tests": "npm run clean:jstests && tsc -p ./test/tsconfig.json",
21
- "clean": "rimraf src",
57
+ "clean": "rimraf build",
22
58
  "clean:jstests": "rimraf 'test/**/!(ts-node-register)*.js'",
23
59
  "coverage-report": "npm run build && npm run nobuild:coverage-report",
24
60
  "coverage-html": "npm run build && npm run nobuild:coverage-html",
@@ -29,7 +65,7 @@
29
65
  "format:ci": "npm run prettier -- --check && npm run prettierjs -- --check",
30
66
  "gitdiff:ci": "npm run build && git diff --exit-code",
31
67
  "integration": "npm run build && npm run nobuild:integration",
32
- "lint": "eslint ts_src/** src/**/*.js",
68
+ "lint": "eslint ts_src/**",
33
69
  "lint:tests": "eslint test/**/*.spec.ts",
34
70
  "mocha:ts": "mocha --recursive --require test/ts-node-register",
35
71
  "nobuild:coverage-report": "nyc report --reporter=lcov",
@@ -38,58 +74,77 @@
38
74
  "nobuild:integration": "npm run mocha:ts -- --timeout 50000 'test/integration/*.ts'",
39
75
  "nobuild:unit": "npm run mocha:ts -- 'test/*.ts'",
40
76
  "prettier": "prettier \"ts_src/**/*.ts\" \"test/**/*.ts\" --ignore-path ./.prettierignore",
41
- "prettierjs": "prettier \"src/**/*.js\" --ignore-path ./.prettierignore",
42
77
  "test": "npm run build && npm run format:ci && npm run lint && npm run nobuild:coverage",
43
78
  "unit": "npm run build && npm run nobuild:unit"
44
79
  },
45
- "repository": {
46
- "type": "git",
47
- "url": "https://github.com/btc-vision/bitcoin.git"
48
- },
49
- "files": [
50
- "src"
51
- ],
52
- "dependencies": {
53
- "@noble/hashes": "^1.2.0",
54
- "bech32": "^2.0.0",
55
- "bip174": "^2.1.1",
56
- "bs58check": "^3.0.1",
57
- "typeforce": "^1.11.3",
58
- "varuint-bitcoin": "^1.1.2"
59
- },
60
80
  "devDependencies": {
61
- "@types/bs58": "^4.0.0",
62
- "@types/bs58check": "^2.1.0",
63
- "@types/mocha": "^5.2.7",
64
- "@types/node": "^16.11.7",
65
- "@types/proxyquire": "^1.3.28",
66
- "@types/randombytes": "^2.0.0",
67
- "@typescript-eslint/eslint-plugin": "^8.7.0",
68
- "@typescript-eslint/parser": "^8.7.0",
69
- "better-npm-audit": "^3.7.3",
81
+ "@babel/core": "^7.26.0",
82
+ "@babel/plugin-proposal-class-properties": "^7.18.6",
83
+ "@babel/plugin-transform-runtime": "^7.25.9",
84
+ "@babel/preset-env": "^7.26.0",
85
+ "@babel/preset-flow": "^7.25.9",
86
+ "@babel/preset-react": "^7.26.3",
87
+ "@babel/preset-typescript": "^7.26.0",
88
+ "@types/bs58check": "^2.1.2",
89
+ "@types/mocha": "^10.0.10",
90
+ "@types/node": "^20.17.10",
91
+ "@types/proxyquire": "^1.3.31",
92
+ "@types/randombytes": "^2.0.3",
93
+ "better-npm-audit": "^3.11.0",
70
94
  "bip32": "^4.0.0",
71
95
  "bip39": "^3.1.0",
72
- "bip65": "^1.0.1",
73
- "bip68": "^1.0.3",
74
- "bs58": "^4.0.0",
75
- "dhttp": "^3.0.0",
96
+ "bip65": "^1.0.3",
97
+ "bip68": "^1.0.4",
98
+ "bs58": "^6.0.0",
99
+ "dhttp": "^3.0.3",
76
100
  "ecpair": "^2.0.1",
77
- "eslint": "^8.29.0",
78
- "eslint-config-prettier": "^8.5.0",
79
- "eslint-plugin-prettier": "^4.2.1",
101
+ "eslint": "^9.17.0",
102
+ "gulp": "^5.0.0",
103
+ "gulp-cached": "^1.1.1",
104
+ "gulp-typescript": "^6.0.0-alpha.1",
80
105
  "hoodwink": "^2.0.0",
106
+ "https-browserify": "^1.0.0",
81
107
  "minimaldata": "^1.0.2",
82
- "mocha": "^10.0.0",
83
- "nyc": "^15.1.0",
84
- "prettier": "^2.8.0",
85
- "proxyquire": "^2.0.1",
108
+ "mocha": "^11.0.1",
109
+ "nyc": "^17.1.0",
110
+ "os-browserify": "^0.3.0",
111
+ "prettier": "^3.4.2",
112
+ "proxyquire": "^2.1.3",
86
113
  "randombytes": "^2.1.0",
87
- "regtest-client": "0.2.0",
88
- "rimraf": "^2.6.3",
89
- "tiny-secp256k1": "^2.2.0",
90
- "ts-node": "^8.3.0",
91
- "typedoc": "^0.25.1",
92
- "typescript": "^4.4.4"
114
+ "regtest-client": "0.2.1",
115
+ "rimraf": "^6.0.1",
116
+ "stream-browserify": "^3.0.0",
117
+ "stream-http": "^3.2.0",
118
+ "tiny-secp256k1": "^2.2.3",
119
+ "ts-loader": "^9.5.1",
120
+ "ts-node": "^10.9.2",
121
+ "typedoc": "^0.27.6",
122
+ "typescript": "^5.7.2",
123
+ "typescript-eslint": "^8.19.0",
124
+ "webpack": "^5.97.1",
125
+ "webpack-cli": "^6.0.1"
93
126
  },
94
- "license": "MIT"
127
+ "dependencies": {
128
+ "@babel/plugin-proposal-object-rest-spread": "^7.20.7",
129
+ "@btc-vision/logger": "^1.0.6",
130
+ "@eslint/js": "^9.17.0",
131
+ "@noble/hashes": "^1.6.1",
132
+ "@noble/secp256k1": "^2.1.0",
133
+ "assert": "^2.1.0",
134
+ "babel-loader": "^9.2.1",
135
+ "babel-plugin-transform-import-meta": "^2.2.1",
136
+ "babel-preset-react": "^6.24.1",
137
+ "babelify": "^10.0.0",
138
+ "bech32": "^2.0.0",
139
+ "bip174": "^2.1.1",
140
+ "browserify-zlib": "^0.2.0",
141
+ "bs58check": "^4.0.0",
142
+ "buffer": "^6.0.3",
143
+ "gulp-clean": "^0.4.0",
144
+ "gulp-eslint-new": "^2.4.0",
145
+ "gulp-logger-new": "^1.0.1",
146
+ "process": "^0.11.10",
147
+ "typeforce": "^1.18.0",
148
+ "varuint-bitcoin": "^1.1.2"
149
+ }
95
150
  }
@@ -1,12 +1,3 @@
1
- 'use strict';
2
- Object.defineProperty(exports, '__esModule', { value: true });
3
- exports.toOutputScript =
4
- exports.fromOutputScript =
5
- exports.toBech32 =
6
- exports.toBase58Check =
7
- exports.fromBech32 =
8
- exports.fromBase58Check =
9
- void 0;
10
1
  /**
11
2
  * bitcoin address decode and encode tools, include base58、bech32 and output script
12
3
  *
@@ -16,162 +7,190 @@ exports.toOutputScript =
16
7
  *
17
8
  * @packageDocumentation
18
9
  */
19
- const networks = require('./networks');
20
- const payments = require('./payments');
21
- const bscript = require('./script');
22
- const types_1 = require('./types');
23
- const bech32_1 = require('bech32');
24
- const bs58check = require('bs58check');
25
- const FUTURE_SEGWIT_MAX_SIZE = 40;
26
- const FUTURE_SEGWIT_MIN_SIZE = 2;
27
- const FUTURE_SEGWIT_MAX_VERSION = 16;
28
- const FUTURE_SEGWIT_MIN_VERSION = 2;
29
- const FUTURE_SEGWIT_VERSION_DIFF = 0x50;
30
- const FUTURE_SEGWIT_VERSION_WARNING =
10
+ import { bech32, bech32m } from 'bech32';
11
+ import * as bs58check from 'bs58check';
12
+ import { payments } from './index.js';
13
+ import * as networks from './networks.js';
14
+ import { Network } from './networks.js';
15
+ import * as bscript from './script.js';
16
+ import { Hash160bit, tuple, typeforce, UInt8 } from './types.js';
17
+
18
+ /** base58check decode result */
19
+ export interface Base58CheckResult {
20
+ /** address hash */
21
+ hash: Buffer;
22
+ /** address version: 0x00 for P2PKH, 0x05 for P2SH */
23
+ version: number;
24
+ }
25
+
26
+ /** bech32 decode result */
27
+ export interface Bech32Result {
28
+ /** address version: 0x00 for P2WPKH、P2WSH, 0x01 for P2TR*/
29
+ version: number;
30
+ /** address prefix: bc for P2WPKH、P2WSH、P2TR */
31
+ prefix: string;
32
+ /** address data:20 bytes for P2WPKH, 32 bytes for P2WSH、P2TR */
33
+ data: Buffer;
34
+ }
35
+
36
+ const FUTURE_SEGWIT_MAX_SIZE: number = 40;
37
+ const FUTURE_SEGWIT_MIN_SIZE: number = 2;
38
+ const FUTURE_SEGWIT_MAX_VERSION: number = 16;
39
+ const FUTURE_SEGWIT_MIN_VERSION: number = 2;
40
+ const FUTURE_SEGWIT_VERSION_DIFF: number = 0x50;
41
+ const FUTURE_SEGWIT_VERSION_WARNING: string =
31
42
  'WARNING: Sending to a future segwit version address can lead to loss of funds. ' +
32
43
  'End users MUST be warned carefully in the GUI and asked if they wish to proceed ' +
33
44
  'with caution. Wallets should verify the segwit version from the output of fromBech32, ' +
34
45
  'then decide when it is safe to use which version of segwit.';
35
- function _toFutureSegwitAddress(output, network) {
46
+
47
+ function _toFutureSegwitAddress(output: Buffer, network: Network): string {
36
48
  const data = output.slice(2);
37
- if (
38
- data.length < FUTURE_SEGWIT_MIN_SIZE ||
39
- data.length > FUTURE_SEGWIT_MAX_SIZE
40
- )
49
+
50
+ if (data.length < FUTURE_SEGWIT_MIN_SIZE || data.length > FUTURE_SEGWIT_MAX_SIZE)
41
51
  throw new TypeError('Invalid program length for segwit address');
52
+
42
53
  const version = output[0] - FUTURE_SEGWIT_VERSION_DIFF;
43
- if (
44
- version < FUTURE_SEGWIT_MIN_VERSION ||
45
- version > FUTURE_SEGWIT_MAX_VERSION
46
- )
54
+
55
+ if (version < FUTURE_SEGWIT_MIN_VERSION || version > FUTURE_SEGWIT_MAX_VERSION)
47
56
  throw new TypeError('Invalid version for segwit address');
48
- if (output[1] !== data.length)
49
- throw new TypeError('Invalid script for segwit address');
57
+
58
+ if (output[1] !== data.length) throw new TypeError('Invalid script for segwit address');
59
+
50
60
  console.warn(FUTURE_SEGWIT_VERSION_WARNING);
61
+
51
62
  return toBech32(data, version, network.bech32);
52
63
  }
64
+
53
65
  /**
54
66
  * decode address with base58 specification, return address version and address hash if valid
55
67
  */
56
- function fromBase58Check(address) {
57
- const payload = Buffer.from(bs58check.decode(address));
68
+ export function fromBase58Check(address: string): Base58CheckResult {
69
+ const payload = Buffer.from(bs58check.default.decode(address));
70
+
58
71
  // TODO: 4.0.0, move to "toOutputScript"
59
72
  if (payload.length < 21) throw new TypeError(address + ' is too short');
60
73
  if (payload.length > 21) throw new TypeError(address + ' is too long');
74
+
61
75
  const version = payload.readUInt8(0);
62
76
  const hash = payload.slice(1);
77
+
63
78
  return { version, hash };
64
79
  }
65
- exports.fromBase58Check = fromBase58Check;
80
+
66
81
  /**
67
82
  * decode address with bech32 specification, return address version、address prefix and address data if valid
68
83
  */
69
- function fromBech32(address) {
84
+ export function fromBech32(address: string): Bech32Result {
70
85
  let result;
71
86
  let version;
72
87
  try {
73
- result = bech32_1.bech32.decode(address);
88
+ result = bech32.decode(address);
74
89
  } catch (e) {}
90
+
75
91
  if (result) {
76
92
  version = result.words[0];
77
- if (version !== 0)
78
- throw new TypeError(address + ' uses wrong encoding');
93
+ if (version !== 0) throw new TypeError(address + ' uses wrong encoding');
79
94
  } else {
80
- result = bech32_1.bech32m.decode(address);
95
+ result = bech32m.decode(address);
81
96
  version = result.words[0];
82
- if (version === 0)
83
- throw new TypeError(address + ' uses wrong encoding');
97
+ if (version === 0) throw new TypeError(address + ' uses wrong encoding');
84
98
  }
85
- const data = bech32_1.bech32.fromWords(result.words.slice(1));
99
+
100
+ const data = bech32.fromWords(result.words.slice(1));
101
+
86
102
  return {
87
103
  version,
88
104
  prefix: result.prefix,
89
105
  data: Buffer.from(data),
90
106
  };
91
107
  }
92
- exports.fromBech32 = fromBech32;
108
+
93
109
  /**
94
110
  * encode address hash to base58 address with version
95
111
  */
96
- function toBase58Check(hash, version) {
97
- (0, types_1.typeforce)(
98
- (0, types_1.tuple)(types_1.Hash160bit, types_1.UInt8),
99
- arguments,
100
- );
112
+ export function toBase58Check(hash: Buffer, version: number): string {
113
+ typeforce(tuple(Hash160bit, UInt8), arguments);
114
+
101
115
  const payload = Buffer.allocUnsafe(21);
102
116
  payload.writeUInt8(version, 0);
103
117
  hash.copy(payload, 1);
104
- return bs58check.encode(payload);
118
+
119
+ return bs58check.default.encode(payload);
105
120
  }
106
- exports.toBase58Check = toBase58Check;
121
+
107
122
  /**
108
123
  * encode address hash to bech32 address with version and prefix
109
124
  */
110
- function toBech32(data, version, prefix) {
111
- const words = bech32_1.bech32.toWords(data);
125
+ export function toBech32(data: Buffer, version: number, prefix: string): string {
126
+ const words = bech32.toWords(data);
112
127
  words.unshift(version);
113
- return version === 0
114
- ? bech32_1.bech32.encode(prefix, words)
115
- : bech32_1.bech32m.encode(prefix, words);
128
+
129
+ return version === 0 ? bech32.encode(prefix, words) : bech32m.encode(prefix, words);
116
130
  }
117
- exports.toBech32 = toBech32;
131
+
118
132
  /**
119
133
  * decode address from output script with network, return address if matched
120
134
  */
121
- function fromOutputScript(output, network) {
135
+ export function fromOutputScript(output: Buffer, network?: Network): string {
122
136
  // TODO: Network
123
137
  network = network || networks.bitcoin;
138
+
124
139
  try {
125
- return payments.p2pkh({ output, network }).address;
140
+ return payments.p2pkh({ output, network }).address as string;
126
141
  } catch (e) {}
127
142
  try {
128
- return payments.p2sh({ output, network }).address;
143
+ return payments.p2sh({ output, network }).address as string;
129
144
  } catch (e) {}
130
145
  try {
131
- return payments.p2wpkh({ output, network }).address;
146
+ return payments.p2wpkh({ output, network }).address as string;
132
147
  } catch (e) {}
133
148
  try {
134
- return payments.p2wsh({ output, network }).address;
149
+ return payments.p2wsh({ output, network }).address as string;
135
150
  } catch (e) {}
136
151
  try {
137
- return payments.p2tr({ output, network }).address;
152
+ return payments.p2tr({ output, network }).address as string;
138
153
  } catch (e) {}
139
154
  try {
140
155
  return _toFutureSegwitAddress(output, network);
141
156
  } catch (e) {}
157
+
142
158
  throw new Error(bscript.toASM(output) + ' has no matching Address');
143
159
  }
144
- exports.fromOutputScript = fromOutputScript;
160
+
145
161
  /**
146
162
  * encodes address to output script with network, return output script if address matched
147
163
  */
148
- function toOutputScript(address, network) {
164
+ export function toOutputScript(address: string, network?: Network): Buffer {
149
165
  network = network || networks.bitcoin;
150
- let decodeBase58;
151
- let decodeBech32;
166
+
167
+ let decodeBase58: Base58CheckResult | undefined;
168
+ let decodeBech32: Bech32Result | undefined;
152
169
  try {
153
170
  decodeBase58 = fromBase58Check(address);
154
171
  } catch (e) {}
172
+
155
173
  if (decodeBase58) {
156
174
  if (decodeBase58.version === network.pubKeyHash)
157
- return payments.p2pkh({ hash: decodeBase58.hash }).output;
175
+ return payments.p2pkh({ hash: decodeBase58.hash }).output as Buffer;
158
176
  if (decodeBase58.version === network.scriptHash)
159
- return payments.p2sh({ hash: decodeBase58.hash }).output;
177
+ return payments.p2sh({ hash: decodeBase58.hash }).output as Buffer;
160
178
  } else {
161
179
  try {
162
180
  decodeBech32 = fromBech32(address);
163
181
  } catch (e) {}
182
+
164
183
  if (decodeBech32) {
165
184
  if (decodeBech32.prefix !== network.bech32)
166
185
  throw new Error(address + ' has an invalid prefix');
167
186
  if (decodeBech32.version === 0) {
168
187
  if (decodeBech32.data.length === 20)
169
- return payments.p2wpkh({ hash: decodeBech32.data }).output;
188
+ return payments.p2wpkh({ hash: decodeBech32.data }).output as Buffer;
170
189
  if (decodeBech32.data.length === 32)
171
- return payments.p2wsh({ hash: decodeBech32.data }).output;
190
+ return payments.p2wsh({ hash: decodeBech32.data }).output as Buffer;
172
191
  } else if (decodeBech32.version === 1) {
173
192
  if (decodeBech32.data.length === 32)
174
- return payments.p2tr({ pubkey: decodeBech32.data }).output;
193
+ return payments.p2tr({ pubkey: decodeBech32.data }).output as Buffer;
175
194
  } else if (
176
195
  decodeBech32.version >= FUTURE_SEGWIT_MIN_VERSION &&
177
196
  decodeBech32.version <= FUTURE_SEGWIT_MAX_VERSION &&
@@ -179,6 +198,7 @@ function toOutputScript(address, network) {
179
198
  decodeBech32.data.length <= FUTURE_SEGWIT_MAX_SIZE
180
199
  ) {
181
200
  console.warn(FUTURE_SEGWIT_VERSION_WARNING);
201
+
182
202
  return bscript.compile([
183
203
  decodeBech32.version + FUTURE_SEGWIT_VERSION_DIFF,
184
204
  decodeBech32.data,
@@ -186,6 +206,6 @@ function toOutputScript(address, network) {
186
206
  }
187
207
  }
188
208
  }
209
+
189
210
  return Buffer.from(address, 'hex');
190
211
  }
191
- exports.toOutputScript = toOutputScript;
@@ -1,58 +1,62 @@
1
- 'use strict';
2
1
  // Reference https://github.com/bitcoin/bips/blob/master/bip-0066.mediawiki
3
2
  // Format: 0x30 [total-length] 0x02 [R-length] [R] 0x02 [S-length] [S]
4
3
  // NOTE: SIGHASH byte ignored AND restricted, truncate before use
5
- Object.defineProperty(exports, '__esModule', { value: true });
6
- exports.encode = exports.decode = exports.check = void 0;
7
- function check(buffer) {
4
+
5
+ export function check(buffer: Buffer): boolean {
8
6
  if (buffer.length < 8) return false;
9
7
  if (buffer.length > 72) return false;
10
8
  if (buffer[0] !== 0x30) return false;
11
9
  if (buffer[1] !== buffer.length - 2) return false;
12
10
  if (buffer[2] !== 0x02) return false;
11
+
13
12
  const lenR = buffer[3];
14
13
  if (lenR === 0) return false;
15
14
  if (5 + lenR >= buffer.length) return false;
16
15
  if (buffer[4 + lenR] !== 0x02) return false;
16
+
17
17
  const lenS = buffer[5 + lenR];
18
18
  if (lenS === 0) return false;
19
19
  if (6 + lenR + lenS !== buffer.length) return false;
20
+
20
21
  if (buffer[4] & 0x80) return false;
21
22
  if (lenR > 1 && buffer[4] === 0x00 && !(buffer[5] & 0x80)) return false;
23
+
22
24
  if (buffer[lenR + 6] & 0x80) return false;
23
- if (lenS > 1 && buffer[lenR + 6] === 0x00 && !(buffer[lenR + 7] & 0x80))
24
- return false;
25
+ if (lenS > 1 && buffer[lenR + 6] === 0x00 && !(buffer[lenR + 7] & 0x80)) return false;
25
26
  return true;
26
27
  }
27
- exports.check = check;
28
- function decode(buffer) {
28
+
29
+ export function decode(buffer: Buffer): { r: Buffer; s: Buffer } {
29
30
  if (buffer.length < 8) throw new Error('DER sequence length is too short');
30
31
  if (buffer.length > 72) throw new Error('DER sequence length is too long');
31
32
  if (buffer[0] !== 0x30) throw new Error('Expected DER sequence');
32
- if (buffer[1] !== buffer.length - 2)
33
- throw new Error('DER sequence length is invalid');
33
+ if (buffer[1] !== buffer.length - 2) throw new Error('DER sequence length is invalid');
34
34
  if (buffer[2] !== 0x02) throw new Error('Expected DER integer');
35
+
35
36
  const lenR = buffer[3];
36
37
  if (lenR === 0) throw new Error('R length is zero');
37
38
  if (5 + lenR >= buffer.length) throw new Error('R length is too long');
38
39
  if (buffer[4 + lenR] !== 0x02) throw new Error('Expected DER integer (2)');
40
+
39
41
  const lenS = buffer[5 + lenR];
40
42
  if (lenS === 0) throw new Error('S length is zero');
41
- if (6 + lenR + lenS !== buffer.length)
42
- throw new Error('S length is invalid');
43
+ if (6 + lenR + lenS !== buffer.length) throw new Error('S length is invalid');
44
+
43
45
  if (buffer[4] & 0x80) throw new Error('R value is negative');
44
46
  if (lenR > 1 && buffer[4] === 0x00 && !(buffer[5] & 0x80))
45
47
  throw new Error('R value excessively padded');
48
+
46
49
  if (buffer[lenR + 6] & 0x80) throw new Error('S value is negative');
47
50
  if (lenS > 1 && buffer[lenR + 6] === 0x00 && !(buffer[lenR + 7] & 0x80))
48
51
  throw new Error('S value excessively padded');
52
+
49
53
  // non-BIP66 - extract R, S values
50
54
  return {
51
55
  r: buffer.slice(4, 4 + lenR),
52
56
  s: buffer.slice(6 + lenR),
53
57
  };
54
58
  }
55
- exports.decode = decode;
59
+
56
60
  /*
57
61
  * Expects r and s to be positive DER integers.
58
62
  *
@@ -75,7 +79,7 @@ exports.decode = decode;
75
79
  * 62300 => 0x00f35c
76
80
  * -62300 => 0xff0ca4
77
81
  */
78
- function encode(r, s) {
82
+ export function encode(r: Buffer, s: Buffer): Buffer {
79
83
  const lenR = r.length;
80
84
  const lenS = s.length;
81
85
  if (lenR === 0) throw new Error('R length is zero');
@@ -84,11 +88,11 @@ function encode(r, s) {
84
88
  if (lenS > 33) throw new Error('S length is too long');
85
89
  if (r[0] & 0x80) throw new Error('R value is negative');
86
90
  if (s[0] & 0x80) throw new Error('S value is negative');
87
- if (lenR > 1 && r[0] === 0x00 && !(r[1] & 0x80))
88
- throw new Error('R value excessively padded');
89
- if (lenS > 1 && s[0] === 0x00 && !(s[1] & 0x80))
90
- throw new Error('S value excessively padded');
91
+ if (lenR > 1 && r[0] === 0x00 && !(r[1] & 0x80)) throw new Error('R value excessively padded');
92
+ if (lenS > 1 && s[0] === 0x00 && !(s[1] & 0x80)) throw new Error('S value excessively padded');
93
+
91
94
  const signature = Buffer.allocUnsafe(6 + lenR + lenS);
95
+
92
96
  // 0x30 [total-length] 0x02 [R-length] [R] 0x02 [S-length] [S]
93
97
  signature[0] = 0x30;
94
98
  signature[1] = signature.length - 2;
@@ -98,6 +102,6 @@ function encode(r, s) {
98
102
  signature[4 + lenR] = 0x02;
99
103
  signature[5 + lenR] = s.length;
100
104
  s.copy(signature, 6 + lenR);
105
+
101
106
  return signature;
102
107
  }
103
- exports.encode = encode;