xpi-ts 0.2.13 → 0.2.15

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 (277) hide show
  1. package/dist/cjs/lib/bitcore/address.js +75 -74
  2. package/dist/cjs/lib/bitcore/block/block.js +124 -126
  3. package/dist/cjs/lib/bitcore/block/blockheader.js +142 -208
  4. package/dist/cjs/lib/bitcore/block/index.js +3 -1
  5. package/dist/cjs/lib/bitcore/block/merkleblock.js +301 -0
  6. package/dist/cjs/lib/bitcore/crypto/bn.js +18 -22
  7. package/dist/cjs/lib/bitcore/crypto/ecdsa.js +50 -49
  8. package/dist/cjs/lib/bitcore/crypto/hash.js +25 -22
  9. package/dist/cjs/lib/bitcore/crypto/musig2.js +86 -92
  10. package/dist/cjs/lib/bitcore/crypto/point.js +42 -35
  11. package/dist/cjs/lib/bitcore/crypto/random.js +3 -2
  12. package/dist/cjs/lib/bitcore/crypto/schnorr.js +46 -55
  13. package/dist/cjs/lib/bitcore/crypto/signature.js +67 -55
  14. package/dist/cjs/lib/bitcore/crypto/sigtype.js +1 -1
  15. package/dist/cjs/lib/bitcore/encoding/base58.js +5 -4
  16. package/dist/cjs/lib/bitcore/encoding/base58check.js +14 -57
  17. package/dist/cjs/lib/bitcore/encoding/bufferreader.js +17 -17
  18. package/dist/cjs/lib/bitcore/encoding/bufferwriter.js +22 -26
  19. package/dist/cjs/lib/bitcore/encoding/varint.js +12 -11
  20. package/dist/cjs/lib/bitcore/errors.js +1 -0
  21. package/dist/cjs/lib/bitcore/hdprivatekey.js +74 -66
  22. package/dist/cjs/lib/bitcore/hdpublickey.js +96 -116
  23. package/dist/cjs/lib/bitcore/index.js +165 -172
  24. package/dist/cjs/lib/bitcore/message.js +36 -35
  25. package/dist/cjs/lib/bitcore/mnemonic/mnemonic.js +4 -3
  26. package/dist/cjs/lib/bitcore/mnemonic/pbkdf2.js +6 -5
  27. package/dist/cjs/lib/bitcore/mnemonic/words/french.js +2053 -0
  28. package/dist/cjs/lib/bitcore/mnemonic/words/index.js +2 -0
  29. package/dist/cjs/lib/bitcore/musig2/session.js +26 -25
  30. package/dist/cjs/lib/bitcore/musig2/signer.js +17 -16
  31. package/dist/cjs/lib/bitcore/networks.js +4 -3
  32. package/dist/cjs/lib/bitcore/opcode.js +11 -7
  33. package/dist/cjs/lib/bitcore/privatekey.js +42 -42
  34. package/dist/cjs/lib/bitcore/publickey.js +61 -57
  35. package/dist/cjs/lib/bitcore/script/interpreter/condition-stack.js +41 -0
  36. package/dist/cjs/lib/bitcore/script/interpreter/index.js +25 -0
  37. package/dist/cjs/lib/bitcore/script/interpreter/interpreter.js +1362 -0
  38. package/dist/cjs/lib/bitcore/script/interpreter/script-num.js +200 -0
  39. package/dist/cjs/lib/bitcore/script/interpreter/types.js +96 -0
  40. package/dist/cjs/lib/bitcore/{taproot.js → script/taproot.js} +107 -95
  41. package/dist/cjs/lib/bitcore/script.js +163 -104
  42. package/dist/cjs/lib/bitcore/taproot/musig2.js +51 -42
  43. package/dist/cjs/lib/bitcore/transaction/index.js +21 -21
  44. package/dist/cjs/lib/bitcore/transaction/input/multisig.js +10 -0
  45. package/dist/cjs/lib/bitcore/transaction/input/multisigscripthash.js +10 -0
  46. package/dist/cjs/lib/bitcore/transaction/input/publickey.js +10 -0
  47. package/dist/cjs/lib/bitcore/transaction/input/publickeyhash.js +10 -0
  48. package/dist/cjs/lib/bitcore/transaction/input/taproot.js +20 -0
  49. package/dist/cjs/lib/bitcore/transaction/input.js +145 -166
  50. package/dist/cjs/lib/bitcore/transaction/outpoint.js +89 -0
  51. package/dist/cjs/lib/bitcore/transaction/output.js +13 -8
  52. package/dist/cjs/lib/bitcore/transaction/sighash.js +96 -96
  53. package/dist/cjs/lib/bitcore/transaction/signature.js +39 -40
  54. package/dist/cjs/lib/bitcore/transaction/transaction.js +130 -118
  55. package/dist/cjs/lib/bitcore/transaction/unspentoutput.js +27 -23
  56. package/dist/cjs/lib/bitcore/util/bits.js +33 -0
  57. package/dist/cjs/lib/bitcore/util/buffer.js +29 -18
  58. package/dist/cjs/lib/bitcore/util/js.js +0 -3
  59. package/dist/cjs/lib/bitcore/util/merkle.js +53 -0
  60. package/dist/cjs/lib/bitcore/util/preconditions.js +8 -7
  61. package/dist/cjs/lib/bitcore/xaddress.js +27 -23
  62. package/dist/cjs/lib/{bitcore/taproot/nft.js → nft.js} +98 -29
  63. package/dist/cjs/lib/rank/index.js +28 -27
  64. package/dist/cjs/lib/rank/script.js +2 -1
  65. package/dist/cjs/lib/rank/transaction.js +2 -1
  66. package/dist/cjs/utils/string.js +6 -5
  67. package/dist/esm/lib/bitcore/address.js +42 -41
  68. package/dist/esm/lib/bitcore/block/block.js +126 -128
  69. package/dist/esm/lib/bitcore/block/blockheader.js +141 -207
  70. package/dist/esm/lib/bitcore/block/index.js +1 -0
  71. package/dist/esm/lib/bitcore/block/merkleblock.js +295 -0
  72. package/dist/esm/lib/bitcore/crypto/bn.js +18 -22
  73. package/dist/esm/lib/bitcore/crypto/ecdsa.js +25 -24
  74. package/dist/esm/lib/bitcore/crypto/hash.js +25 -22
  75. package/dist/esm/lib/bitcore/crypto/musig2.js +60 -66
  76. package/dist/esm/lib/bitcore/crypto/point.js +22 -15
  77. package/dist/esm/lib/bitcore/crypto/random.js +3 -2
  78. package/dist/esm/lib/bitcore/crypto/schnorr.js +35 -44
  79. package/dist/esm/lib/bitcore/crypto/signature.js +61 -49
  80. package/dist/esm/lib/bitcore/crypto/sigtype.js +1 -1
  81. package/dist/esm/lib/bitcore/encoding/base58.js +5 -4
  82. package/dist/esm/lib/bitcore/encoding/base58check.js +11 -57
  83. package/dist/esm/lib/bitcore/encoding/bufferreader.js +7 -7
  84. package/dist/esm/lib/bitcore/encoding/bufferwriter.js +18 -21
  85. package/dist/esm/lib/bitcore/encoding/varint.js +7 -6
  86. package/dist/esm/lib/bitcore/errors.js +1 -0
  87. package/dist/esm/lib/bitcore/hdprivatekey.js +47 -39
  88. package/dist/esm/lib/bitcore/hdpublickey.js +77 -97
  89. package/dist/esm/lib/bitcore/index.js +42 -45
  90. package/dist/esm/lib/bitcore/message.js +16 -15
  91. package/dist/esm/lib/bitcore/mnemonic/mnemonic.js +4 -3
  92. package/dist/esm/lib/bitcore/mnemonic/pbkdf2.js +5 -4
  93. package/dist/esm/lib/bitcore/mnemonic/words/french.js +2050 -0
  94. package/dist/esm/lib/bitcore/mnemonic/words/index.js +2 -0
  95. package/dist/esm/lib/bitcore/musig2/session.js +17 -16
  96. package/dist/esm/lib/bitcore/musig2/signer.js +19 -18
  97. package/dist/esm/lib/bitcore/networks.js +4 -3
  98. package/dist/esm/lib/bitcore/opcode.js +9 -5
  99. package/dist/esm/lib/bitcore/privatekey.js +25 -25
  100. package/dist/esm/lib/bitcore/publickey.js +46 -42
  101. package/dist/esm/lib/bitcore/script/interpreter/condition-stack.js +37 -0
  102. package/dist/esm/lib/bitcore/script/interpreter/index.js +4 -0
  103. package/dist/esm/lib/bitcore/script/interpreter/interpreter.js +1356 -0
  104. package/dist/esm/lib/bitcore/script/interpreter/script-num.js +195 -0
  105. package/dist/esm/lib/bitcore/script/interpreter/types.js +93 -0
  106. package/dist/esm/lib/bitcore/{taproot.js → script/taproot.js} +92 -81
  107. package/dist/esm/lib/bitcore/script.js +163 -102
  108. package/dist/esm/lib/bitcore/taproot/musig2.js +36 -27
  109. package/dist/esm/lib/bitcore/transaction/index.js +6 -6
  110. package/dist/esm/lib/bitcore/transaction/input/multisig.js +6 -0
  111. package/dist/esm/lib/bitcore/transaction/input/multisigscripthash.js +6 -0
  112. package/dist/esm/lib/bitcore/transaction/input/publickey.js +6 -0
  113. package/dist/esm/lib/bitcore/transaction/input/publickeyhash.js +6 -0
  114. package/dist/esm/lib/bitcore/transaction/input/taproot.js +16 -0
  115. package/dist/esm/lib/bitcore/transaction/input.js +55 -76
  116. package/dist/esm/lib/bitcore/transaction/outpoint.js +85 -0
  117. package/dist/esm/lib/bitcore/transaction/output.js +13 -8
  118. package/dist/esm/lib/bitcore/transaction/sighash.js +22 -22
  119. package/dist/esm/lib/bitcore/transaction/signature.js +16 -17
  120. package/dist/esm/lib/bitcore/transaction/transaction.js +58 -46
  121. package/dist/esm/lib/bitcore/transaction/unspentoutput.js +15 -11
  122. package/dist/esm/lib/bitcore/util/bits.js +27 -0
  123. package/dist/esm/lib/bitcore/util/buffer.js +20 -9
  124. package/dist/esm/lib/bitcore/util/js.js +0 -3
  125. package/dist/esm/lib/bitcore/util/merkle.js +47 -0
  126. package/dist/esm/lib/bitcore/util/preconditions.js +3 -2
  127. package/dist/esm/lib/bitcore/xaddress.js +16 -12
  128. package/dist/esm/lib/{bitcore/taproot/nft.js → nft.js} +97 -28
  129. package/dist/esm/lib/rank/index.js +15 -14
  130. package/dist/esm/lib/rank/script.js +1 -0
  131. package/dist/esm/lib/rank/transaction.js +1 -0
  132. package/dist/esm/utils/string.js +1 -0
  133. package/dist/types/lib/bitcore/address.d.ts +4 -3
  134. package/dist/types/lib/bitcore/address.d.ts.map +1 -1
  135. package/dist/types/lib/bitcore/block/block.d.ts +11 -19
  136. package/dist/types/lib/bitcore/block/block.d.ts.map +1 -1
  137. package/dist/types/lib/bitcore/block/blockheader.d.ts +30 -32
  138. package/dist/types/lib/bitcore/block/blockheader.d.ts.map +1 -1
  139. package/dist/types/lib/bitcore/block/index.d.ts +2 -0
  140. package/dist/types/lib/bitcore/block/index.d.ts.map +1 -1
  141. package/dist/types/lib/bitcore/block/merkleblock.d.ts +53 -0
  142. package/dist/types/lib/bitcore/block/merkleblock.d.ts.map +1 -0
  143. package/dist/types/lib/bitcore/crypto/bn.d.ts +2 -2
  144. package/dist/types/lib/bitcore/crypto/bn.d.ts.map +1 -1
  145. package/dist/types/lib/bitcore/crypto/ecdsa.d.ts +5 -4
  146. package/dist/types/lib/bitcore/crypto/ecdsa.d.ts.map +1 -1
  147. package/dist/types/lib/bitcore/crypto/hash.d.ts +3 -1
  148. package/dist/types/lib/bitcore/crypto/hash.d.ts.map +1 -1
  149. package/dist/types/lib/bitcore/crypto/musig2.d.ts +22 -30
  150. package/dist/types/lib/bitcore/crypto/musig2.d.ts.map +1 -1
  151. package/dist/types/lib/bitcore/crypto/point.d.ts +8 -3
  152. package/dist/types/lib/bitcore/crypto/point.d.ts.map +1 -1
  153. package/dist/types/lib/bitcore/crypto/random.d.ts +1 -0
  154. package/dist/types/lib/bitcore/crypto/random.d.ts.map +1 -1
  155. package/dist/types/lib/bitcore/crypto/schnorr.d.ts +5 -4
  156. package/dist/types/lib/bitcore/crypto/schnorr.d.ts.map +1 -1
  157. package/dist/types/lib/bitcore/crypto/signature.d.ts +4 -1
  158. package/dist/types/lib/bitcore/crypto/signature.d.ts.map +1 -1
  159. package/dist/types/lib/bitcore/crypto/sigtype.d.ts +1 -0
  160. package/dist/types/lib/bitcore/crypto/sigtype.d.ts.map +1 -1
  161. package/dist/types/lib/bitcore/encoding/base58.d.ts +1 -0
  162. package/dist/types/lib/bitcore/encoding/base58.d.ts.map +1 -1
  163. package/dist/types/lib/bitcore/encoding/base58check.d.ts +1 -2
  164. package/dist/types/lib/bitcore/encoding/base58check.d.ts.map +1 -1
  165. package/dist/types/lib/bitcore/encoding/bufferreader.d.ts +2 -1
  166. package/dist/types/lib/bitcore/encoding/bufferreader.d.ts.map +1 -1
  167. package/dist/types/lib/bitcore/encoding/bufferwriter.d.ts +2 -2
  168. package/dist/types/lib/bitcore/encoding/bufferwriter.d.ts.map +1 -1
  169. package/dist/types/lib/bitcore/encoding/varint.d.ts +3 -2
  170. package/dist/types/lib/bitcore/encoding/varint.d.ts.map +1 -1
  171. package/dist/types/lib/bitcore/errors.d.ts +3 -0
  172. package/dist/types/lib/bitcore/errors.d.ts.map +1 -1
  173. package/dist/types/lib/bitcore/hdprivatekey.d.ts +5 -4
  174. package/dist/types/lib/bitcore/hdprivatekey.d.ts.map +1 -1
  175. package/dist/types/lib/bitcore/hdpublickey.d.ts +4 -3
  176. package/dist/types/lib/bitcore/hdpublickey.d.ts.map +1 -1
  177. package/dist/types/lib/bitcore/index.d.ts +55 -60
  178. package/dist/types/lib/bitcore/index.d.ts.map +1 -1
  179. package/dist/types/lib/bitcore/message.d.ts +7 -4
  180. package/dist/types/lib/bitcore/message.d.ts.map +1 -1
  181. package/dist/types/lib/bitcore/mnemonic/mnemonic.d.ts +2 -0
  182. package/dist/types/lib/bitcore/mnemonic/mnemonic.d.ts.map +1 -1
  183. package/dist/types/lib/bitcore/mnemonic/pbkdf2.d.ts +1 -0
  184. package/dist/types/lib/bitcore/mnemonic/pbkdf2.d.ts.map +1 -1
  185. package/dist/types/lib/bitcore/mnemonic/words/french.d.ts +2 -0
  186. package/dist/types/lib/bitcore/mnemonic/words/french.d.ts.map +1 -0
  187. package/dist/types/lib/bitcore/mnemonic/words/index.d.ts +1 -0
  188. package/dist/types/lib/bitcore/mnemonic/words/index.d.ts.map +1 -1
  189. package/dist/types/lib/bitcore/musig2/session.d.ts +8 -7
  190. package/dist/types/lib/bitcore/musig2/session.d.ts.map +1 -1
  191. package/dist/types/lib/bitcore/musig2/signer.d.ts +10 -9
  192. package/dist/types/lib/bitcore/musig2/signer.d.ts.map +1 -1
  193. package/dist/types/lib/bitcore/networks.d.ts +1 -0
  194. package/dist/types/lib/bitcore/networks.d.ts.map +1 -1
  195. package/dist/types/lib/bitcore/opcode.d.ts +5 -0
  196. package/dist/types/lib/bitcore/opcode.d.ts.map +1 -1
  197. package/dist/types/lib/bitcore/privatekey.d.ts +5 -4
  198. package/dist/types/lib/bitcore/privatekey.d.ts.map +1 -1
  199. package/dist/types/lib/bitcore/publickey.d.ts +13 -6
  200. package/dist/types/lib/bitcore/publickey.d.ts.map +1 -1
  201. package/dist/types/lib/bitcore/{chunk.d.ts → script/chunk.d.ts} +1 -0
  202. package/dist/types/lib/bitcore/script/chunk.d.ts.map +1 -0
  203. package/dist/types/lib/bitcore/script/interpreter/condition-stack.d.ts +11 -0
  204. package/dist/types/lib/bitcore/script/interpreter/condition-stack.d.ts.map +1 -0
  205. package/dist/types/lib/bitcore/script/interpreter/index.d.ts +5 -0
  206. package/dist/types/lib/bitcore/script/interpreter/index.d.ts.map +1 -0
  207. package/dist/types/lib/bitcore/script/interpreter/interpreter.d.ts +21 -0
  208. package/dist/types/lib/bitcore/script/interpreter/interpreter.d.ts.map +1 -0
  209. package/dist/types/lib/bitcore/script/interpreter/script-num.d.ts +35 -0
  210. package/dist/types/lib/bitcore/script/interpreter/script-num.d.ts.map +1 -0
  211. package/dist/types/lib/bitcore/script/interpreter/types.d.ts +101 -0
  212. package/dist/types/lib/bitcore/script/interpreter/types.d.ts.map +1 -0
  213. package/dist/types/lib/bitcore/{taproot.d.ts → script/taproot.d.ts} +20 -7
  214. package/dist/types/lib/bitcore/script/taproot.d.ts.map +1 -0
  215. package/dist/types/lib/bitcore/script.d.ts +12 -11
  216. package/dist/types/lib/bitcore/script.d.ts.map +1 -1
  217. package/dist/types/lib/bitcore/taproot/musig2.d.ts +17 -16
  218. package/dist/types/lib/bitcore/taproot/musig2.d.ts.map +1 -1
  219. package/dist/types/lib/bitcore/transaction/index.d.ts +11 -11
  220. package/dist/types/lib/bitcore/transaction/index.d.ts.map +1 -1
  221. package/dist/types/lib/bitcore/transaction/input/multisig.d.ts +5 -0
  222. package/dist/types/lib/bitcore/transaction/input/multisig.d.ts.map +1 -0
  223. package/dist/types/lib/bitcore/transaction/input/multisigscripthash.d.ts +5 -0
  224. package/dist/types/lib/bitcore/transaction/input/multisigscripthash.d.ts.map +1 -0
  225. package/dist/types/lib/bitcore/transaction/input/publickey.d.ts +5 -0
  226. package/dist/types/lib/bitcore/transaction/input/publickey.d.ts.map +1 -0
  227. package/dist/types/lib/bitcore/transaction/input/publickeyhash.d.ts +6 -0
  228. package/dist/types/lib/bitcore/transaction/input/publickeyhash.d.ts.map +1 -0
  229. package/dist/types/lib/bitcore/transaction/input/taproot.d.ts +18 -0
  230. package/dist/types/lib/bitcore/transaction/input/taproot.d.ts.map +1 -0
  231. package/dist/types/lib/bitcore/transaction/input.d.ts +27 -35
  232. package/dist/types/lib/bitcore/transaction/input.d.ts.map +1 -1
  233. package/dist/types/lib/bitcore/transaction/outpoint.d.ts +32 -0
  234. package/dist/types/lib/bitcore/transaction/outpoint.d.ts.map +1 -0
  235. package/dist/types/lib/bitcore/transaction/output.d.ts +3 -1
  236. package/dist/types/lib/bitcore/transaction/output.d.ts.map +1 -1
  237. package/dist/types/lib/bitcore/transaction/sighash.d.ts +8 -7
  238. package/dist/types/lib/bitcore/transaction/sighash.d.ts.map +1 -1
  239. package/dist/types/lib/bitcore/transaction/signature.d.ts +3 -2
  240. package/dist/types/lib/bitcore/transaction/signature.d.ts.map +1 -1
  241. package/dist/types/lib/bitcore/transaction/transaction.d.ts +18 -15
  242. package/dist/types/lib/bitcore/transaction/transaction.d.ts.map +1 -1
  243. package/dist/types/lib/bitcore/transaction/unspentoutput.d.ts +9 -7
  244. package/dist/types/lib/bitcore/transaction/unspentoutput.d.ts.map +1 -1
  245. package/dist/types/lib/bitcore/unit.d.ts.map +1 -1
  246. package/dist/types/lib/bitcore/util/base32.d.ts.map +1 -1
  247. package/dist/types/lib/bitcore/util/bits.d.ts +5 -0
  248. package/dist/types/lib/bitcore/util/bits.d.ts.map +1 -0
  249. package/dist/types/lib/bitcore/util/buffer.d.ts +10 -2
  250. package/dist/types/lib/bitcore/util/buffer.d.ts.map +1 -1
  251. package/dist/types/lib/bitcore/util/js.d.ts +0 -1
  252. package/dist/types/lib/bitcore/util/js.d.ts.map +1 -1
  253. package/dist/types/lib/bitcore/util/merkle.d.ts +6 -0
  254. package/dist/types/lib/bitcore/util/merkle.d.ts.map +1 -0
  255. package/dist/types/lib/bitcore/util/preconditions.d.ts.map +1 -1
  256. package/dist/types/lib/bitcore/xaddress.d.ts +1 -0
  257. package/dist/types/lib/bitcore/xaddress.d.ts.map +1 -1
  258. package/dist/types/lib/{bitcore/taproot/nft.d.ts → nft.d.ts} +26 -12
  259. package/dist/types/lib/nft.d.ts.map +1 -0
  260. package/dist/types/lib/rank/index.d.ts +1 -0
  261. package/dist/types/lib/rank/index.d.ts.map +1 -1
  262. package/dist/types/lib/rank/script.d.ts +1 -0
  263. package/dist/types/lib/rank/script.d.ts.map +1 -1
  264. package/dist/types/lib/rank/transaction.d.ts +1 -0
  265. package/dist/types/lib/rank/transaction.d.ts.map +1 -1
  266. package/dist/types/utils/string.d.ts +1 -0
  267. package/dist/types/utils/string.d.ts.map +1 -1
  268. package/package.json +3 -2
  269. package/dist/cjs/lib/bitcore/script/interpreter.js +0 -1757
  270. package/dist/esm/lib/bitcore/script/interpreter.js +0 -1753
  271. package/dist/types/lib/bitcore/chunk.d.ts.map +0 -1
  272. package/dist/types/lib/bitcore/script/interpreter.d.ts +0 -101
  273. package/dist/types/lib/bitcore/script/interpreter.d.ts.map +0 -1
  274. package/dist/types/lib/bitcore/taproot/nft.d.ts.map +0 -1
  275. package/dist/types/lib/bitcore/taproot.d.ts.map +0 -1
  276. /package/dist/cjs/lib/bitcore/{chunk.js → script/chunk.js} +0 -0
  277. /package/dist/esm/lib/bitcore/{chunk.js → script/chunk.js} +0 -0
@@ -0,0 +1,195 @@
1
+ import { BufferUtil } from '../../util/buffer';
2
+ import { SCRIPTNUM_MAX_ELEMENT_SIZE } from './types';
3
+ export class ScriptNumError extends Error {
4
+ constructor(message) {
5
+ super(message);
6
+ this.name = 'ScriptNumError';
7
+ }
8
+ }
9
+ export class ScriptNum {
10
+ static INT64_MAX = (1n << 63n) - 1n;
11
+ static INT64_MIN_EXCLUSIVE = -(1n << 63n) + 1n;
12
+ value;
13
+ constructor(value) {
14
+ this.value = value;
15
+ }
16
+ static fromBuffer(buf, requireMinimal = true, maxSize = SCRIPTNUM_MAX_ELEMENT_SIZE) {
17
+ if (buf.length > maxSize) {
18
+ throw new ScriptNumError('script number overflow');
19
+ }
20
+ if (requireMinimal && !ScriptNum.isMinimallyEncoded(buf, maxSize)) {
21
+ throw new ScriptNumError('non-minimally encoded script number');
22
+ }
23
+ return new ScriptNum(ScriptNum.decode(buf));
24
+ }
25
+ static isMinimallyEncoded(buf, maxSize = SCRIPTNUM_MAX_ELEMENT_SIZE) {
26
+ if (buf.length > maxSize) {
27
+ return false;
28
+ }
29
+ if (buf.length === 0) {
30
+ return true;
31
+ }
32
+ if ((buf[buf.length - 1] & 0x7f) === 0) {
33
+ if (buf.length <= 1 || (buf[buf.length - 2] & 0x80) === 0) {
34
+ return false;
35
+ }
36
+ }
37
+ return true;
38
+ }
39
+ static minimallyEncode(data) {
40
+ if (data.length === 0) {
41
+ return data;
42
+ }
43
+ let last = data[data.length - 1];
44
+ if ((last & 0x7f) !== 0) {
45
+ return data;
46
+ }
47
+ let i = data.length - 1;
48
+ while (i > 0 && data[i] === 0) {
49
+ i--;
50
+ }
51
+ if (i === 0 && data[0] === 0) {
52
+ return BufferUtil.alloc(0);
53
+ }
54
+ const result = BufferUtil.alloc(i + 1 + ((data[i] & 0x80) !== 0 ? 1 : 0));
55
+ data.copy(result, 0, 0, i + 1);
56
+ if ((data[i] & 0x80) !== 0) {
57
+ result[result.length - 1] = last & 0x80;
58
+ }
59
+ else {
60
+ result[i] |= last & 0x80;
61
+ }
62
+ return result;
63
+ }
64
+ static decode(buf) {
65
+ if (buf.length === 0) {
66
+ return 0n;
67
+ }
68
+ let result = 0n;
69
+ for (let i = 0; i < buf.length; i++) {
70
+ result |= BigInt(buf[i]) << BigInt(8 * i);
71
+ }
72
+ if (buf[buf.length - 1] & 0x80) {
73
+ const mask = 0x80n << BigInt(8 * (buf.length - 1));
74
+ return -(result & ~mask);
75
+ }
76
+ return result;
77
+ }
78
+ toBuffer() {
79
+ return ScriptNum.serialize(this.value);
80
+ }
81
+ static serialize(value) {
82
+ if (value === 0n) {
83
+ return BufferUtil.alloc(0);
84
+ }
85
+ const neg = value < 0n;
86
+ let absvalue = neg ? -value : value;
87
+ const result = [];
88
+ while (absvalue > 0n) {
89
+ result.push(Number(absvalue & 0xffn));
90
+ absvalue >>= 8n;
91
+ }
92
+ if (result[result.length - 1] & 0x80) {
93
+ result.push(neg ? 0x80 : 0x00);
94
+ }
95
+ else if (neg) {
96
+ result[result.length - 1] |= 0x80;
97
+ }
98
+ return BufferUtil.from(result);
99
+ }
100
+ toNumber() {
101
+ const MAX_INT = 2_147_483_647;
102
+ const MIN_INT = -2_147_483_648;
103
+ if (this.value > BigInt(MAX_INT)) {
104
+ return MAX_INT;
105
+ }
106
+ if (this.value < BigInt(MIN_INT)) {
107
+ return MIN_INT;
108
+ }
109
+ return Number(this.value);
110
+ }
111
+ add(other) {
112
+ const result = this.value + other.value;
113
+ ScriptNum.checkOverflow(result);
114
+ return new ScriptNum(result);
115
+ }
116
+ sub(other) {
117
+ const result = this.value - other.value;
118
+ ScriptNum.checkOverflow(result);
119
+ return new ScriptNum(result);
120
+ }
121
+ negate() {
122
+ return new ScriptNum(-this.value);
123
+ }
124
+ abs() {
125
+ return this.value < 0n ? this.negate() : this;
126
+ }
127
+ div(other) {
128
+ return new ScriptNum(this.value / other.value);
129
+ }
130
+ mod(other) {
131
+ return new ScriptNum(this.value % other.value);
132
+ }
133
+ mulpow2(shift) {
134
+ if (this.value === 0n) {
135
+ return new ScriptNum(0n);
136
+ }
137
+ const sign = this.value > 0n ? 1n : -1n;
138
+ const absval = this.value > 0n ? this.value : -this.value;
139
+ if (shift.value > 0n) {
140
+ if (shift.value >= 63n) {
141
+ throw new ScriptNumError('script number mulpow2 non-zero shift >= 63');
142
+ }
143
+ const overflowMask = ~((1n << (63n - shift.value)) - 1n) & ((1n << 64n) - 1n);
144
+ if ((absval & overflowMask) !== 0n) {
145
+ throw new ScriptNumError('script number mulpow2 overflow');
146
+ }
147
+ return new ScriptNum(sign * (absval << shift.value));
148
+ }
149
+ else {
150
+ const rshift = -shift.value;
151
+ if (rshift >= 63n) {
152
+ return new ScriptNum(0n);
153
+ }
154
+ return new ScriptNum(sign * (absval >> rshift));
155
+ }
156
+ }
157
+ bitwiseAnd(mask) {
158
+ return new ScriptNum(this.value & mask);
159
+ }
160
+ isZero() {
161
+ return this.value === 0n;
162
+ }
163
+ isNegative() {
164
+ return this.value < 0n;
165
+ }
166
+ lt(other) {
167
+ const val = other instanceof ScriptNum ? other.value : other;
168
+ return this.value < val;
169
+ }
170
+ gt(other) {
171
+ const val = other instanceof ScriptNum ? other.value : other;
172
+ return this.value > val;
173
+ }
174
+ lte(other) {
175
+ const val = other instanceof ScriptNum ? other.value : other;
176
+ return this.value <= val;
177
+ }
178
+ gte(other) {
179
+ const val = other instanceof ScriptNum ? other.value : other;
180
+ return this.value >= val;
181
+ }
182
+ eq(other) {
183
+ const val = other instanceof ScriptNum ? other.value : other;
184
+ return this.value === val;
185
+ }
186
+ neq(other) {
187
+ const val = other instanceof ScriptNum ? other.value : other;
188
+ return this.value !== val;
189
+ }
190
+ static checkOverflow(value) {
191
+ if (value > ScriptNum.INT64_MAX || value < ScriptNum.INT64_MIN_EXCLUSIVE) {
192
+ throw new ScriptNumError('script number overflow');
193
+ }
194
+ }
195
+ }
@@ -0,0 +1,93 @@
1
+ export const MAX_SCRIPT_ELEMENT_SIZE = 520;
2
+ export const MAX_OPS_PER_SCRIPT = 400;
3
+ export const MAX_PUBKEYS_PER_MULTISIG = 20;
4
+ export const MAX_SCRIPT_SIZE = 10_000;
5
+ export const MAX_STACK_SIZE = 1_000;
6
+ export const MAX_NUM2BIN_SIZE = 68;
7
+ export const LOCKTIME_THRESHOLD = 500_000_000;
8
+ export const SCRIPTNUM_MAX_ELEMENT_SIZE = 8;
9
+ export var ScriptFlags;
10
+ (function (ScriptFlags) {
11
+ ScriptFlags[ScriptFlags["VERIFY_NONE"] = 0] = "VERIFY_NONE";
12
+ ScriptFlags[ScriptFlags["TAPROOT_KEY_SPEND_PATH"] = 1] = "TAPROOT_KEY_SPEND_PATH";
13
+ ScriptFlags[ScriptFlags["DISABLE_TAPROOT_SIGHASH_LOTUS"] = 2] = "DISABLE_TAPROOT_SIGHASH_LOTUS";
14
+ ScriptFlags[ScriptFlags["VERIFY_DISCOURAGE_UPGRADABLE_NOPS"] = 128] = "VERIFY_DISCOURAGE_UPGRADABLE_NOPS";
15
+ ScriptFlags[ScriptFlags["VERIFY_CLEANSTACK"] = 256] = "VERIFY_CLEANSTACK";
16
+ ScriptFlags[ScriptFlags["VERIFY_MINIMALIF"] = 8192] = "VERIFY_MINIMALIF";
17
+ ScriptFlags[ScriptFlags["ENABLE_SIGHASH_FORKID"] = 65536] = "ENABLE_SIGHASH_FORKID";
18
+ ScriptFlags[ScriptFlags["ENABLE_REPLAY_PROTECTION"] = 131072] = "ENABLE_REPLAY_PROTECTION";
19
+ ScriptFlags[ScriptFlags["VERIFY_INPUT_SIGCHECKS"] = 4194304] = "VERIFY_INPUT_SIGCHECKS";
20
+ })(ScriptFlags || (ScriptFlags = {}));
21
+ export const STANDARD_SCRIPT_VERIFY_FLAGS = ScriptFlags.VERIFY_CLEANSTACK |
22
+ ScriptFlags.VERIFY_DISCOURAGE_UPGRADABLE_NOPS |
23
+ ScriptFlags.VERIFY_MINIMALIF |
24
+ ScriptFlags.ENABLE_SIGHASH_FORKID |
25
+ ScriptFlags.VERIFY_INPUT_SIGCHECKS;
26
+ export var ScriptError;
27
+ (function (ScriptError) {
28
+ ScriptError["OK"] = "OK";
29
+ ScriptError["UNKNOWN"] = "UNKNOWN";
30
+ ScriptError["EVAL_FALSE"] = "EVAL_FALSE";
31
+ ScriptError["OP_RETURN"] = "OP_RETURN";
32
+ ScriptError["SCRIPT_SIZE"] = "SCRIPT_SIZE";
33
+ ScriptError["PUSH_SIZE"] = "PUSH_SIZE";
34
+ ScriptError["OP_COUNT"] = "OP_COUNT";
35
+ ScriptError["STACK_SIZE"] = "STACK_SIZE";
36
+ ScriptError["SIG_COUNT"] = "SIG_COUNT";
37
+ ScriptError["PUBKEY_COUNT"] = "PUBKEY_COUNT";
38
+ ScriptError["INPUT_SIGCHECKS"] = "INPUT_SIGCHECKS";
39
+ ScriptError["INVALID_OPERAND_SIZE"] = "INVALID_OPERAND_SIZE";
40
+ ScriptError["INVALID_NUMBER_RANGE"] = "INVALID_NUMBER_RANGE";
41
+ ScriptError["IMPOSSIBLE_ENCODING"] = "IMPOSSIBLE_ENCODING";
42
+ ScriptError["INVALID_SPLIT_RANGE"] = "INVALID_SPLIT_RANGE";
43
+ ScriptError["INVALID_BIT_COUNT"] = "INVALID_BIT_COUNT";
44
+ ScriptError["VERIFY"] = "VERIFY";
45
+ ScriptError["EQUALVERIFY"] = "EQUALVERIFY";
46
+ ScriptError["CHECKMULTISIGVERIFY"] = "CHECKMULTISIGVERIFY";
47
+ ScriptError["CHECKSIGVERIFY"] = "CHECKSIGVERIFY";
48
+ ScriptError["CHECKDATASIGVERIFY"] = "CHECKDATASIGVERIFY";
49
+ ScriptError["NUMEQUALVERIFY"] = "NUMEQUALVERIFY";
50
+ ScriptError["BAD_OPCODE"] = "BAD_OPCODE";
51
+ ScriptError["DISABLED_OPCODE"] = "DISABLED_OPCODE";
52
+ ScriptError["INVALID_STACK_OPERATION"] = "INVALID_STACK_OPERATION";
53
+ ScriptError["INVALID_ALTSTACK_OPERATION"] = "INVALID_ALTSTACK_OPERATION";
54
+ ScriptError["UNBALANCED_CONDITIONAL"] = "UNBALANCED_CONDITIONAL";
55
+ ScriptError["DIV_BY_ZERO"] = "DIV_BY_ZERO";
56
+ ScriptError["MOD_BY_ZERO"] = "MOD_BY_ZERO";
57
+ ScriptError["INVALID_BITFIELD_SIZE"] = "INVALID_BITFIELD_SIZE";
58
+ ScriptError["INVALID_BIT_RANGE"] = "INVALID_BIT_RANGE";
59
+ ScriptError["NEGATIVE_LOCKTIME"] = "NEGATIVE_LOCKTIME";
60
+ ScriptError["UNSATISFIED_LOCKTIME"] = "UNSATISFIED_LOCKTIME";
61
+ ScriptError["SIG_HASHTYPE"] = "SIG_HASHTYPE";
62
+ ScriptError["SIG_DER"] = "SIG_DER";
63
+ ScriptError["MINIMALDATA"] = "MINIMALDATA";
64
+ ScriptError["SIG_PUSHONLY"] = "SIG_PUSHONLY";
65
+ ScriptError["SIG_HIGH_S"] = "SIG_HIGH_S";
66
+ ScriptError["PUBKEYTYPE"] = "PUBKEYTYPE";
67
+ ScriptError["CLEANSTACK"] = "CLEANSTACK";
68
+ ScriptError["MINIMALIF"] = "MINIMALIF";
69
+ ScriptError["SIG_NULLFAIL"] = "SIG_NULLFAIL";
70
+ ScriptError["SIG_BADLENGTH"] = "SIG_BADLENGTH";
71
+ ScriptError["SIG_NONSCHNORR"] = "SIG_NONSCHNORR";
72
+ ScriptError["DISCOURAGE_UPGRADABLE_NOPS"] = "DISCOURAGE_UPGRADABLE_NOPS";
73
+ ScriptError["ILLEGAL_FORKID"] = "ILLEGAL_FORKID";
74
+ ScriptError["MUST_USE_FORKID"] = "MUST_USE_FORKID";
75
+ ScriptError["INVALID_NUM2BIN_SIZE"] = "INVALID_NUM2BIN_SIZE";
76
+ ScriptError["INVALID_OP_SCRIPTTYPE"] = "INVALID_OP_SCRIPTTYPE";
77
+ ScriptError["SCRIPTTYPE_INVALID_TYPE"] = "SCRIPTTYPE_INVALID_TYPE";
78
+ ScriptError["SCRIPTTYPE_MALFORMED_SCRIPT"] = "SCRIPTTYPE_MALFORMED_SCRIPT";
79
+ ScriptError["TAPROOT_KEY_SPEND_MUST_USE_LOTUS_SIGHASH"] = "TAPROOT_KEY_SPEND_MUST_USE_LOTUS_SIGHASH";
80
+ ScriptError["TAPROOT_KEY_SPEND_MUST_USE_SCHNORR_SIG"] = "TAPROOT_KEY_SPEND_MUST_USE_SCHNORR_SIG";
81
+ ScriptError["TAPROOT_VERIFY_SIGNATURE_FAILED"] = "TAPROOT_VERIFY_SIGNATURE_FAILED";
82
+ ScriptError["TAPROOT_ANNEX_NOT_SUPPORTED"] = "TAPROOT_ANNEX_NOT_SUPPORTED";
83
+ ScriptError["TAPROOT_WRONG_CONTROL_SIZE"] = "TAPROOT_WRONG_CONTROL_SIZE";
84
+ ScriptError["TAPROOT_VERIFY_COMMITMENT_FAILED"] = "TAPROOT_VERIFY_COMMITMENT_FAILED";
85
+ ScriptError["TAPROOT_LEAF_VERSION_NOT_SUPPORTED"] = "TAPROOT_LEAF_VERSION_NOT_SUPPORTED";
86
+ ScriptError["TAPROOT_PHASEOUT"] = "TAPROOT_PHASEOUT";
87
+ })(ScriptError || (ScriptError = {}));
88
+ export const NULL_SIGNATURE_CHECKER = {
89
+ verifySignature: () => false,
90
+ checkSignature: () => false,
91
+ checkLockTime: () => false,
92
+ checkSequence: () => false,
93
+ };
@@ -1,11 +1,12 @@
1
- import { Hash } from './crypto/hash.js';
2
- import { PublicKey } from './publickey.js';
3
- import { PrivateKey } from './privatekey.js';
4
- import { Script } from './script.js';
5
- import { Opcode } from './opcode.js';
6
- import { BN } from './crypto/bn.js';
7
- import { BufferWriter } from './encoding/bufferwriter.js';
8
- import { Signature } from './crypto/signature.js';
1
+ import { Hash } from '../crypto/hash';
2
+ import { PUBKEY_PREFIX_EVEN, PUBKEY_PREFIX_ODD, PublicKey } from '../publickey';
3
+ import { PrivateKey } from '../privatekey';
4
+ import { Script } from '../script';
5
+ import { Opcode } from '../opcode';
6
+ import { BN } from '../crypto/bn';
7
+ import { BufferWriter } from '../encoding/bufferwriter';
8
+ import { BufferUtil } from '../util/buffer';
9
+ import { Signature } from '../crypto/signature';
9
10
  export const TAPROOT_LEAF_MASK = 0xfe;
10
11
  export const TAPROOT_LEAF_TAPSCRIPT = 0xc0;
11
12
  export const TAPROOT_CONTROL_BASE_SIZE = 33;
@@ -15,39 +16,45 @@ export const TAPROOT_CONTROL_MAX_SIZE = TAPROOT_CONTROL_BASE_SIZE +
15
16
  TAPROOT_CONTROL_NODE_SIZE * TAPROOT_CONTROL_MAX_NODE_COUNT;
16
17
  export const TAPROOT_SCRIPTTYPE = Opcode.OP_1;
17
18
  export const TAPROOT_INTRO_SIZE = 3;
18
- export const TAPROOT_SIZE_WITHOUT_STATE = TAPROOT_INTRO_SIZE + 33;
19
- export const TAPROOT_SIZE_WITH_STATE = TAPROOT_INTRO_SIZE + 33 + 33;
19
+ export const PUBKEY_COMPRESSED_SIZE = 33;
20
+ export const PUBKEY_UNCOMPRESSED_SIZE = 65;
21
+ export const PUBKEY_XCOORD_SIZE = 32;
22
+ export const SHA256_HASH_SIZE = 32;
23
+ export const TAPROOT_STATE_PUSH_SIZE = 32;
24
+ export const TAPROOT_SIZE_WITHOUT_STATE = TAPROOT_INTRO_SIZE + PUBKEY_COMPRESSED_SIZE;
25
+ export const TAPROOT_SIZE_WITH_STATE = TAPROOT_INTRO_SIZE + PUBKEY_COMPRESSED_SIZE + 1 + SHA256_HASH_SIZE;
20
26
  export const TAPROOT_SIGHASH_TYPE = Signature.SIGHASH_ALL | Signature.SIGHASH_LOTUS;
21
27
  export const TAPROOT_ANNEX_TAG = 0x50;
28
+ export const TAPROOT_TAG_TAPLEAF = 'TapLeaf';
29
+ export const TAPROOT_TAG_TAPBRANCH = 'TapBranch';
30
+ export const TAPROOT_TAG_TAPTWEAK = 'TapTweak';
22
31
  export function taggedHash(tag, data) {
23
- const tagHash = Hash.sha256(Buffer.from(tag, 'utf8'));
24
- const combined = Buffer.concat([tagHash, tagHash, data]);
32
+ const tagHash = Hash.sha256(BufferUtil.from(tag, 'utf8'));
33
+ const combined = BufferUtil.concat([tagHash, tagHash, data]);
25
34
  return Hash.sha256(combined);
26
35
  }
27
- export function calculateTapTweak(internalPubKey, merkleRoot = Buffer.alloc(32)) {
28
- const pubKeyBytes = internalPubKey.toBuffer();
29
- const data = Buffer.concat([pubKeyBytes, merkleRoot]);
30
- return taggedHash('TapTweak', data);
36
+ export function calculateTapTweak(internalPubKey, merkleRoot = BufferUtil.alloc(SHA256_HASH_SIZE)) {
37
+ return taggedHash(TAPROOT_TAG_TAPTWEAK, BufferUtil.concat([internalPubKey.toBuffer(), merkleRoot]));
31
38
  }
32
39
  export function calculateTapLeaf(script, leafVersion = TAPROOT_LEAF_TAPSCRIPT) {
33
- const scriptBuf = Buffer.isBuffer(script) ? script : script.toBuffer();
40
+ const scriptBuf = BufferUtil.isBuffer(script) ? script : script.toBuffer();
34
41
  const writer = new BufferWriter();
35
42
  writer.writeUInt8(leafVersion);
36
43
  writer.writeVarintNum(scriptBuf.length);
37
44
  writer.write(scriptBuf);
38
- return taggedHash('TapLeaf', writer.toBuffer());
45
+ return taggedHash(TAPROOT_TAG_TAPLEAF, writer.toBuffer());
39
46
  }
40
47
  export function calculateTapBranch(left, right) {
41
- const ordered = Buffer.compare(left, right) < 0
42
- ? Buffer.concat([left, right])
43
- : Buffer.concat([right, left]);
44
- return taggedHash('TapBranch', ordered);
48
+ const ordered = BufferUtil.compare(left, right) < 0
49
+ ? BufferUtil.concat([left, right])
50
+ : BufferUtil.concat([right, left]);
51
+ return taggedHash(TAPROOT_TAG_TAPBRANCH, ordered);
45
52
  }
46
- export function tweakPublicKey(internalPubKey, merkleRoot = Buffer.alloc(32)) {
53
+ export function tweakPublicKey(internalPubKey, merkleRoot = BufferUtil.alloc(SHA256_HASH_SIZE)) {
47
54
  const tweak = calculateTapTweak(internalPubKey, merkleRoot);
48
55
  return internalPubKey.addScalar(tweak);
49
56
  }
50
- export function tweakPrivateKey(internalPrivKey, merkleRoot = Buffer.alloc(32)) {
57
+ export function tweakPrivateKey(internalPrivKey, merkleRoot = BufferUtil.alloc(SHA256_HASH_SIZE)) {
51
58
  const internalPubKey = internalPrivKey.publicKey;
52
59
  const tweak = calculateTapTweak(internalPubKey, merkleRoot);
53
60
  const tweakBN = new BN(tweak);
@@ -65,7 +72,7 @@ export function buildTapTree(tree) {
65
72
  if (isTapLeafNode(tree)) {
66
73
  const leafNode = tree;
67
74
  const leafVersion = leafNode.leafVersion || TAPROOT_LEAF_TAPSCRIPT;
68
- const scriptBuf = Buffer.isBuffer(leafNode.script)
75
+ const scriptBuf = BufferUtil.isBuffer(leafNode.script)
69
76
  ? leafNode.script
70
77
  : leafNode.script.toBuffer();
71
78
  const leafHash = calculateTapLeaf(scriptBuf, leafVersion);
@@ -104,83 +111,83 @@ export function createControlBlock(internalPubKey, leafIndex, tree) {
104
111
  }
105
112
  const leaf = treeResult.leaves[leafIndex];
106
113
  const pubKeyBytes = internalPubKey.toBuffer();
107
- const parity = pubKeyBytes[0] === 0x03 ? 1 : 0;
114
+ const parity = pubKeyBytes[0] === PUBKEY_PREFIX_ODD ? 1 : 0;
108
115
  const controlByte = (leaf.leafVersion & TAPROOT_LEAF_MASK) | parity;
109
116
  const writer = new BufferWriter();
110
117
  writer.writeUInt8(controlByte);
111
- writer.write(pubKeyBytes.slice(1, 33));
118
+ writer.write(pubKeyBytes.slice(1, PUBKEY_COMPRESSED_SIZE));
112
119
  for (const node of leaf.merklePath) {
113
120
  writer.write(node);
114
121
  }
115
122
  return writer.toBuffer();
116
123
  }
117
- export function verifyTaprootCommitment(commitmentPubKey, internalPubKey, merkleRoot) {
118
- const expectedCommitment = tweakPublicKey(internalPubKey, merkleRoot);
119
- return commitmentPubKey.toString() === expectedCommitment.toString();
124
+ export function applyTweakToPublicKey(internalPubKey, tweak) {
125
+ return internalPubKey.addScalar(tweak);
120
126
  }
121
- export function isPayToTaproot(script) {
122
- const buf = script.toBuffer();
123
- if (buf.length < TAPROOT_SIZE_WITHOUT_STATE) {
124
- return false;
125
- }
126
- if (buf[0] !== Opcode.OP_SCRIPTTYPE || buf[1] !== TAPROOT_SCRIPTTYPE) {
127
- return false;
128
- }
129
- if (buf[2] !== 33) {
130
- return false;
127
+ export function verifyTaprootCommitment(controlBlock, commitment, script) {
128
+ try {
129
+ if (controlBlock.length < TAPROOT_CONTROL_BASE_SIZE) {
130
+ return { tapleafHash: BufferUtil.alloc(SHA256_HASH_SIZE), success: false };
131
+ }
132
+ const pathLen = Math.floor((controlBlock.length - TAPROOT_CONTROL_BASE_SIZE) /
133
+ TAPROOT_CONTROL_NODE_SIZE);
134
+ const leafVersion = controlBlock[0] & TAPROOT_LEAF_MASK;
135
+ let leafHash = calculateTapLeaf(script, leafVersion);
136
+ let merkleHash = leafHash;
137
+ for (let i = 0; i < pathLen; i++) {
138
+ const nodeOffset = TAPROOT_CONTROL_BASE_SIZE + i * TAPROOT_CONTROL_NODE_SIZE;
139
+ const node = controlBlock.slice(nodeOffset, nodeOffset + TAPROOT_CONTROL_NODE_SIZE);
140
+ if (BufferUtil.compare(merkleHash, node) < 0) {
141
+ merkleHash = calculateTapBranch(merkleHash, node);
142
+ }
143
+ else {
144
+ merkleHash = calculateTapBranch(node, merkleHash);
145
+ }
146
+ }
147
+ const pubkeyBytes = controlBlock.slice(0, TAPROOT_CONTROL_BASE_SIZE);
148
+ const pubkeyBuffer = BufferUtil.from(pubkeyBytes);
149
+ pubkeyBuffer[0] =
150
+ pubkeyBuffer[0] & 1 ? PUBKEY_PREFIX_ODD : PUBKEY_PREFIX_EVEN;
151
+ const internalPubKey = new PublicKey(pubkeyBuffer);
152
+ const tweakHash = calculateTapTweak(internalPubKey, merkleHash);
153
+ const commitmentKey = new PublicKey(commitment);
154
+ const expectedCommitment = applyTweakToPublicKey(internalPubKey, tweakHash);
155
+ return {
156
+ tapleafHash: leafHash,
157
+ success: commitmentKey.toString() === expectedCommitment.toString(),
158
+ };
131
159
  }
132
- if (buf.length === TAPROOT_SIZE_WITHOUT_STATE) {
133
- return true;
160
+ catch (e) {
161
+ return { tapleafHash: BufferUtil.alloc(SHA256_HASH_SIZE), success: false };
134
162
  }
135
- return (buf.length === TAPROOT_SIZE_WITH_STATE &&
136
- buf[TAPROOT_SIZE_WITHOUT_STATE] === 32);
137
163
  }
138
164
  export function extractTaprootCommitment(script) {
139
- if (!isPayToTaproot(script)) {
165
+ if (!script.isTaprootOut()) {
140
166
  throw new Error('Not a valid Pay-To-Taproot script');
141
167
  }
142
- const buf = script.toBuffer();
143
- const commitmentBytes = buf.subarray(3, 3 + 33);
144
- return PublicKey.fromBuffer(commitmentBytes);
168
+ return PublicKey.fromBuffer(script.chunks[2].buf);
145
169
  }
146
170
  export function extractTaprootState(script) {
147
- const buf = script.toBuffer();
148
- if (buf.length !== TAPROOT_SIZE_WITH_STATE) {
171
+ if (!script.isTaprootOut()) {
149
172
  return null;
150
173
  }
151
- return buf.subarray(TAPROOT_SIZE_WITHOUT_STATE + 1, TAPROOT_SIZE_WITH_STATE);
152
- }
153
- export function buildPayToTaproot(commitment, state) {
154
- if (state && state.length !== 32) {
155
- throw new Error('Taproot state must be exactly 32 bytes');
156
- }
157
- const commitmentBytes = commitment.toBuffer();
158
- if (commitmentBytes.length !== 33) {
159
- throw new Error('Commitment must be 33-byte compressed public key');
160
- }
161
- if (state) {
162
- return new Script()
163
- .add(Opcode.OP_SCRIPTTYPE)
164
- .add(TAPROOT_SCRIPTTYPE)
165
- .add(commitmentBytes)
166
- .add(state);
167
- }
168
- else {
169
- return new Script()
170
- .add(Opcode.OP_SCRIPTTYPE)
171
- .add(TAPROOT_SCRIPTTYPE)
172
- .add(commitmentBytes);
174
+ if (script.chunks.length !== 4) {
175
+ return null;
173
176
  }
177
+ return script.chunks[3].buf;
178
+ }
179
+ export function buildPayToTaproot(_commitment, _state) {
180
+ throw new Error('This function has been deprecated. Used Script.buildTaprootOut instead');
174
181
  }
175
- export function buildKeyPathTaproot(internalPubKey, state) {
176
- const merkleRoot = Buffer.alloc(32);
182
+ export function buildKeyPathTaproot(internalPubKey) {
183
+ const merkleRoot = BufferUtil.alloc(SHA256_HASH_SIZE);
177
184
  const commitment = tweakPublicKey(internalPubKey, merkleRoot);
178
- return buildPayToTaproot(commitment, state);
185
+ return Script.buildTaprootOut(commitment);
179
186
  }
180
187
  export function buildScriptPathTaproot(internalPubKey, tree, state) {
181
188
  const treeInfo = buildTapTree(tree);
182
189
  const commitment = tweakPublicKey(internalPubKey, treeInfo.merkleRoot);
183
- const script = buildPayToTaproot(commitment, state);
190
+ const script = Script.buildTaprootOut(commitment, state);
184
191
  return {
185
192
  script,
186
193
  commitment,
@@ -190,14 +197,14 @@ export function buildScriptPathTaproot(internalPubKey, tree, state) {
190
197
  }
191
198
  export function verifyTaprootScriptPath(internalPubKey, script, commitmentPubKey, leafVersion, merklePath, parity) {
192
199
  try {
193
- const pubkeyPrefix = parity === 0 ? 0x02 : 0x03;
194
- const fullPubkey = Buffer.concat([
195
- Buffer.from([pubkeyPrefix]),
200
+ const pubkeyPrefix = parity === 0 ? PUBKEY_PREFIX_EVEN : PUBKEY_PREFIX_ODD;
201
+ const fullPubkey = BufferUtil.concat([
202
+ BufferUtil.from([pubkeyPrefix]),
196
203
  internalPubKey,
197
204
  ]);
198
205
  let leafHash = calculateTapLeaf(script, leafVersion);
199
206
  for (const pathNode of merklePath) {
200
- if (Buffer.compare(leafHash, pathNode) < 0) {
207
+ if (BufferUtil.compare(leafHash, pathNode) < 0) {
201
208
  leafHash = calculateTapBranch(leafHash, pathNode);
202
209
  }
203
210
  else {
@@ -216,6 +223,10 @@ export function verifyTaprootScriptPath(internalPubKey, script, commitmentPubKey
216
223
  return false;
217
224
  }
218
225
  }
226
+ export function verifyTaprootScriptPathFromControlBlock(controlBlock, commitment, script) {
227
+ const result = verifyTaprootCommitment(controlBlock, commitment, script);
228
+ return result.success;
229
+ }
219
230
  export function verifyTaprootSpend(scriptPubkey, stack, flags) {
220
231
  const SCRIPT_DISABLE_TAPROOT_SIGHASH_LOTUS = 1 << 22;
221
232
  const SCRIPT_TAPROOT_KEY_SPEND_PATH = 1 << 23;
@@ -223,7 +234,7 @@ export function verifyTaprootSpend(scriptPubkey, stack, flags) {
223
234
  if (flags & SCRIPT_DISABLE_TAPROOT_SIGHASH_LOTUS) {
224
235
  return { success: false, error: 'SCRIPT_ERR_TAPROOT_PHASEOUT' };
225
236
  }
226
- if (!isPayToTaproot(scriptPubkey)) {
237
+ if (!scriptPubkey.isTaprootOut()) {
227
238
  return { success: false, error: 'SCRIPT_ERR_SCRIPTTYPE_MALFORMED_SCRIPT' };
228
239
  }
229
240
  const scriptBuf = scriptPubkey.toBuffer();