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
@@ -1,20 +1,21 @@
1
- import { Preconditions } from '../util/preconditions.js';
2
- import { JSUtil } from '../util/js.js';
3
- import { BufferReader } from '../encoding/bufferreader.js';
4
- import { BufferWriter } from '../encoding/bufferwriter.js';
5
- import { Hash } from '../crypto/hash.js';
6
- import { Signature } from '../crypto/signature.js';
7
- import { verify } from './sighash.js';
8
- import { BitcoreError } from '../errors.js';
9
- import { Address } from '../address.js';
10
- import { UnspentOutput } from './unspentoutput.js';
11
- import { Input, MultisigInput, MultisigScriptHashInput, PublicKeyInput, PublicKeyHashInput, TaprootInput, MuSigTaprootInput, } from './input.js';
12
- import { Output } from './output.js';
13
- import { Script } from '../script.js';
14
- import { PrivateKey } from '../privatekey.js';
15
- import { BN } from '../crypto/bn.js';
16
- import { sighash as computeSighash } from './sighash.js';
17
- import { Interpreter } from '../script/interpreter.js';
1
+ import { Preconditions } from '../util/preconditions';
2
+ import { JSUtil } from '../util/js';
3
+ import { BufferReader } from '../encoding/bufferreader';
4
+ import { BufferWriter } from '../encoding/bufferwriter';
5
+ import { Hash } from '../crypto/hash';
6
+ import { Signature } from '../crypto/signature';
7
+ import { verify } from './sighash';
8
+ import { BitcoreError } from '../errors';
9
+ import { Address } from '../address';
10
+ import { UnspentOutput } from './unspentoutput';
11
+ import { Input, MultisigInput, MultisigScriptHashInput, PublicKeyInput, PublicKeyHashInput, TaprootInput, MuSig2TaprootInput, } from './input';
12
+ import { Output } from './output';
13
+ import { Script } from '../script';
14
+ import { PrivateKey } from '../privatekey';
15
+ import { BN } from '../crypto/bn';
16
+ import { sighash as computeSighash } from './sighash';
17
+ import { Interpreter } from '../script/interpreter';
18
+ import { BufferUtil } from '../util';
18
19
  const CURRENT_VERSION = 2;
19
20
  const DEFAULT_NLOCKTIME = 0;
20
21
  const MAX_BLOCK_SIZE = 32_000_000;
@@ -26,7 +27,7 @@ const NLOCKTIME_MAX_VALUE = 4294967295;
26
27
  const FEE_PER_KB = 1_000;
27
28
  const CHANGE_OUTPUT_MAX_SIZE = 20 + 4 + 34 + 4;
28
29
  const MAXIMUM_EXTRA_SIZE = 4 + 9 + 9 + 4;
29
- const NULL_HASH = Buffer.from('0000000000000000000000000000000000000000000000000000000000000000', 'hex');
30
+ const NULL_HASH = BufferUtil.from('0000000000000000000000000000000000000000000000000000000000000000', 'hex');
30
31
  export class Transaction {
31
32
  static DUST_AMOUNT = DUST_AMOUNT;
32
33
  static FEE_SECURITY_MARGIN = FEE_SECURITY_MARGIN;
@@ -62,7 +63,7 @@ export class Transaction {
62
63
  else if (typeof serialized === 'string' && JSUtil.isHexa(serialized)) {
63
64
  this.fromString(serialized);
64
65
  }
65
- else if (Buffer.isBuffer(serialized)) {
66
+ else if (BufferUtil.isBuffer(serialized)) {
66
67
  this.fromBuffer(serialized);
67
68
  }
68
69
  else if (serialized && typeof serialized === 'object') {
@@ -99,6 +100,9 @@ export class Transaction {
99
100
  }
100
101
  return this._hash;
101
102
  }
103
+ get hashBuffer() {
104
+ return this._getHash();
105
+ }
102
106
  get id() {
103
107
  return this.txid;
104
108
  }
@@ -110,6 +114,9 @@ export class Transaction {
110
114
  }
111
115
  return this._txid;
112
116
  }
117
+ get txidBuffer() {
118
+ return this._getTxid();
119
+ }
113
120
  get inputAmount() {
114
121
  return this._getInputAmount();
115
122
  }
@@ -198,7 +205,7 @@ export class Transaction {
198
205
  size += 1;
199
206
  }
200
207
  for (let i = 0; i < size; i += 2) {
201
- const buf = Buffer.concat([hashes[j + i], hashes[j + i + 1]]);
208
+ const buf = BufferUtil.concat([hashes[j + i], hashes[j + i + 1]]);
202
209
  hashes.push(Hash.sha256sha256(buf));
203
210
  }
204
211
  j += size;
@@ -347,7 +354,7 @@ export class Transaction {
347
354
  return this;
348
355
  }
349
356
  fromString(str) {
350
- return this.fromBuffer(Buffer.from(str, 'hex'));
357
+ return this.fromBuffer(BufferUtil.from(str, 'hex'));
351
358
  }
352
359
  toObject() {
353
360
  const inputs = this.inputs.map(input => input.toObject());
@@ -504,12 +511,12 @@ export class Transaction {
504
511
  return this.sign(privateKey, Signature.SIGHASH_ALL | Signature.SIGHASH_LOTUS, 'schnorr');
505
512
  }
506
513
  getMuSig2Inputs() {
507
- return this.inputs.filter(input => input instanceof MuSigTaprootInput);
514
+ return this.inputs.filter(input => input instanceof MuSig2TaprootInput);
508
515
  }
509
516
  getMuSig2Sighash(inputIndex) {
510
517
  const input = this.inputs[inputIndex];
511
- if (!(input instanceof MuSigTaprootInput)) {
512
- throw new Error(`Input ${inputIndex} is not a MuSigTaprootInput`);
518
+ if (!(input instanceof MuSig2TaprootInput)) {
519
+ throw new Error(`Input ${inputIndex} is not a MuSig2TaprootInput`);
513
520
  }
514
521
  if (!input.output) {
515
522
  throw new Error(`Input ${inputIndex} is missing output information`);
@@ -519,16 +526,16 @@ export class Transaction {
519
526
  }
520
527
  addMuSig2Nonce(inputIndex, signerIndex, nonce) {
521
528
  const input = this.inputs[inputIndex];
522
- if (!(input instanceof MuSigTaprootInput)) {
523
- throw new Error(`Input ${inputIndex} is not a MuSigTaprootInput`);
529
+ if (!(input instanceof MuSig2TaprootInput)) {
530
+ throw new Error(`Input ${inputIndex} is not a MuSig2TaprootInput`);
524
531
  }
525
532
  input.addPublicNonce(signerIndex, nonce);
526
533
  return this;
527
534
  }
528
535
  addMuSig2PartialSignature(inputIndex, signerIndex, partialSig) {
529
536
  const input = this.inputs[inputIndex];
530
- if (!(input instanceof MuSigTaprootInput)) {
531
- throw new Error(`Input ${inputIndex} is not a MuSigTaprootInput`);
537
+ if (!(input instanceof MuSig2TaprootInput)) {
538
+ throw new Error(`Input ${inputIndex} is not a MuSig2TaprootInput`);
532
539
  }
533
540
  input.addPartialSignature(signerIndex, partialSig);
534
541
  return this;
@@ -537,13 +544,13 @@ export class Transaction {
537
544
  const musigInputs = this.getMuSig2Inputs();
538
545
  for (let i = 0; i < this.inputs.length; i++) {
539
546
  const input = this.inputs[i];
540
- if (input instanceof MuSigTaprootInput) {
547
+ if (input instanceof MuSig2TaprootInput) {
541
548
  if (!input.hasAllPartialSignatures()) {
542
549
  throw new Error(`MuSig2 input ${i} is missing partial signatures. ` +
543
550
  `Has ${input.partialSignatures?.size || 0} of ${input.keyAggContext?.pubkeys.length || 0}`);
544
551
  }
545
552
  const sighash = this.getMuSig2Sighash(i);
546
- input.finalizeMuSigSignature(this, sighash);
553
+ input.finalizeMuSig2Signature(this, sighash);
547
554
  }
548
555
  }
549
556
  return this;
@@ -556,7 +563,7 @@ export class Transaction {
556
563
  const input = this.inputs[index];
557
564
  let inputSigtype = sigtype;
558
565
  if (!inputSigtype) {
559
- if (input.output?.script.isPayToTaproot()) {
566
+ if (input.output?.script.isTaprootOut()) {
560
567
  const taprootInput = input;
561
568
  if (taprootInput.hasScriptTree()) {
562
569
  inputSigtype = Signature.SIGHASH_ALL | Signature.SIGHASH_FORKID;
@@ -660,7 +667,7 @@ export class Transaction {
660
667
  x.i = i++;
661
668
  });
662
669
  copy.sort((first, second) => {
663
- const prevTxIdCompare = Buffer.compare(first.prevTxId, second.prevTxId);
670
+ const prevTxIdCompare = BufferUtil.compare(first.prevTxId, second.prevTxId);
664
671
  if (prevTxIdCompare !== 0)
665
672
  return prevTxIdCompare;
666
673
  const outputIndexCompare = first.outputIndex - second.outputIndex;
@@ -682,7 +689,7 @@ export class Transaction {
682
689
  const satoshisCompare = first.satoshis - second.satoshis;
683
690
  if (satoshisCompare !== 0)
684
691
  return satoshisCompare;
685
- const scriptCompare = Buffer.compare(first.scriptBuffer, second.scriptBuffer);
692
+ const scriptCompare = BufferUtil.compare(first.scriptBuffer, second.scriptBuffer);
686
693
  if (scriptCompare !== 0)
687
694
  return scriptCompare;
688
695
  return (first.i -
@@ -817,13 +824,10 @@ export class Transaction {
817
824
  try {
818
825
  const verifyFlags = flags !== undefined
819
826
  ? flags
820
- : Interpreter.SCRIPT_VERIFY_P2SH |
821
- Interpreter.SCRIPT_VERIFY_STRICTENC |
822
- Interpreter.SCRIPT_VERIFY_DERSIG |
823
- Interpreter.SCRIPT_VERIFY_LOW_S |
824
- Interpreter.SCRIPT_VERIFY_NULLFAIL |
825
- Interpreter.SCRIPT_ENABLE_SIGHASH_FORKID |
826
- Interpreter.SCRIPT_ENABLE_SCHNORR_MULTISIG;
827
+ : Interpreter.SCRIPT_ENABLE_SIGHASH_FORKID |
828
+ Interpreter.SCRIPT_VERIFY_CLEANSTACK |
829
+ Interpreter.SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_NOPS |
830
+ Interpreter.SCRIPT_VERIFY_INPUT_SIGCHECKS;
827
831
  const interpreter = new Interpreter();
828
832
  const isValid = interpreter.verify(input.script, input.output.script, this, i, verifyFlags, BigInt(input.output.satoshis));
829
833
  if (!isValid) {
@@ -870,11 +874,14 @@ export class Transaction {
870
874
  _fromNonP2SH(utxo) {
871
875
  let clazz;
872
876
  const unspentOutput = new UnspentOutput(utxo);
873
- if (unspentOutput.script.isPayToTaproot()) {
877
+ if (unspentOutput.script.isTaprootOut()) {
874
878
  if (unspentOutput.keyAggContext &&
875
879
  unspentOutput.mySignerIndex !== undefined) {
876
- clazz = MuSigTaprootInput;
877
- const input = new MuSigTaprootInput({
880
+ if (!unspentOutput.internalPubKey || !unspentOutput.merkleRoot) {
881
+ throw new BitcoreError('MuSig2 Taproot input requires internalPubKey and merkleRoot');
882
+ }
883
+ clazz = MuSig2TaprootInput;
884
+ const input = new MuSig2TaprootInput({
878
885
  output: new Output({
879
886
  script: unspentOutput.script,
880
887
  satoshis: unspentOutput.satoshis,
@@ -884,10 +891,15 @@ export class Transaction {
884
891
  script: new Script(),
885
892
  keyAggContext: unspentOutput.keyAggContext,
886
893
  mySignerIndex: unspentOutput.mySignerIndex,
894
+ internalPubKey: unspentOutput.internalPubKey,
895
+ merkleRoot: unspentOutput.merkleRoot,
887
896
  });
888
897
  this.addInput(input);
889
898
  return;
890
899
  }
900
+ if (!unspentOutput.internalPubKey) {
901
+ throw new BitcoreError('Taproot input requires internalPubKey');
902
+ }
891
903
  clazz = TaprootInput;
892
904
  const taprootInput = new TaprootInput({
893
905
  output: new Output({
@@ -898,12 +910,12 @@ export class Transaction {
898
910
  outputIndex: unspentOutput.outputIndex,
899
911
  script: new Script(),
900
912
  internalPubKey: unspentOutput.internalPubKey,
901
- merkleRoot: unspentOutput.merkleRoot || Buffer.alloc(32),
913
+ merkleRoot: unspentOutput.merkleRoot || BufferUtil.alloc(32),
902
914
  });
903
915
  this.addInput(taprootInput);
904
916
  return;
905
917
  }
906
- else if (unspentOutput.script.isPayToPublicKeyHash()) {
918
+ else if (unspentOutput.script.isPublicKeyHashOut()) {
907
919
  clazz = PublicKeyHashInput;
908
920
  }
909
921
  else if (unspentOutput.script.isPublicKeyOut()) {
@@ -936,7 +948,7 @@ export class Transaction {
936
948
  script: new Script(),
937
949
  }), pubkeys, threshold, undefined, opts));
938
950
  }
939
- else if (unspentOutput.script.isPayToScriptHash()) {
951
+ else if (unspentOutput.script.isScriptHashOut()) {
940
952
  this.addInput(new MultisigScriptHashInput(new Input({
941
953
  output: new Output({
942
954
  script: unspentOutput.script,
@@ -1,9 +1,10 @@
1
- import { Preconditions } from '../util/preconditions.js';
2
- import { JSUtil } from '../util/js.js';
3
- import { Script } from '../script.js';
4
- import { Address } from '../address.js';
5
- import { PublicKey } from '../publickey.js';
6
- import { Unit } from '../unit.js';
1
+ import { Preconditions } from '../util/preconditions';
2
+ import { BufferUtil } from '../util/buffer';
3
+ import { JSUtil } from '../util/js';
4
+ import { Script } from '../script';
5
+ import { Address } from '../address';
6
+ import { PublicKey } from '../publickey';
7
+ import { Unit } from '../unit';
7
8
  export class UnspentOutput {
8
9
  address;
9
10
  txId;
@@ -18,7 +19,7 @@ export class UnspentOutput {
18
19
  Preconditions.checkArgument(typeof data === 'object' && data !== null, 'Must provide an object from where to extract data');
19
20
  const address = data.address ? new Address(data.address) : undefined;
20
21
  const txId = data.txid || data.txId;
21
- if (!txId || !JSUtil.isHexaString(txId) || txId.length > 64) {
22
+ if (!txId || !JSUtil.isHexa(txId) || txId.length > 64) {
22
23
  throw new Error('Invalid TXID in object: ' + JSON.stringify(data));
23
24
  }
24
25
  const outputIndex = data.vout !== undefined ? data.vout : data.outputIndex;
@@ -51,7 +52,7 @@ export class UnspentOutput {
51
52
  if (data.internalPubKey instanceof PublicKey) {
52
53
  this.internalPubKey = data.internalPubKey;
53
54
  }
54
- else if (Buffer.isBuffer(data.internalPubKey)) {
55
+ else if (BufferUtil.isBuffer(data.internalPubKey)) {
55
56
  this.internalPubKey = new PublicKey(data.internalPubKey);
56
57
  }
57
58
  else if (typeof data.internalPubKey === 'string') {
@@ -69,7 +70,7 @@ export class UnspentOutput {
69
70
  return Unit.fromSatoshis(this.satoshis);
70
71
  }
71
72
  isValid() {
72
- return (JSUtil.isHexaString(this.txId) &&
73
+ return (JSUtil.isHexa(this.txId) &&
73
74
  this.txId.length === 64 &&
74
75
  this.outputIndex >= 0 &&
75
76
  this.satoshis > 0 &&
@@ -116,10 +117,13 @@ export class UnspentOutput {
116
117
  return this.txId + ':' + this.outputIndex;
117
118
  }
118
119
  isPayToPublicKeyHash() {
119
- return this.script.isPayToPublicKeyHash();
120
+ return this.script.isPublicKeyHashOut();
120
121
  }
121
122
  isPayToScriptHash() {
122
- return this.script.isPayToScriptHash();
123
+ return this.script.isScriptHashOut();
124
+ }
125
+ isPayToTaproot() {
126
+ return this.script.isTaprootOut();
123
127
  }
124
128
  getAddress() {
125
129
  try {
@@ -0,0 +1,27 @@
1
+ export function bitsToBytes(bits) {
2
+ const bytes = new Array(Math.ceil(bits.length / 8)).fill(0);
3
+ for (let p = 0; p < bits.length; p++) {
4
+ bytes[p >> 3] |= (bits[p] ? 1 : 0) << (p & 7);
5
+ }
6
+ return bytes;
7
+ }
8
+ export function bytesToBits(bytes) {
9
+ const bits = new Array(bytes.length * 8);
10
+ for (let p = 0; p < bits.length; p++) {
11
+ bits[p] = (bytes[p >> 3] & (1 << (p & 7))) !== 0;
12
+ }
13
+ return bits;
14
+ }
15
+ export function extractBit(bytes, position) {
16
+ return (bytes[position >> 3] & (1 << (position & 7))) !== 0;
17
+ }
18
+ export function setBit(bytes, position, value) {
19
+ const byteIndex = position >> 3;
20
+ const bitIndex = position & 7;
21
+ if (value) {
22
+ bytes[byteIndex] |= 1 << bitIndex;
23
+ }
24
+ else {
25
+ bytes[byteIndex] &= ~(1 << bitIndex);
26
+ }
27
+ }
@@ -1,8 +1,7 @@
1
- import { Preconditions } from './preconditions.js';
1
+ import { Buffer } from 'buffer/';
2
2
  export class BufferUtil {
3
+ static Buffer = Buffer;
3
4
  static fill(buffer, value) {
4
- Preconditions.checkArgumentType(buffer, 'Buffer', 'buffer');
5
- Preconditions.checkArgumentType(value, 'number', 'value');
6
5
  const length = buffer.length;
7
6
  for (let i = 0; i < length; i++) {
8
7
  buffer[i] = value;
@@ -18,13 +17,15 @@ export class BufferUtil {
18
17
  return Buffer.isBuffer(arg) || arg instanceof Uint8Array;
19
18
  }
20
19
  static emptyBuffer(bytes) {
21
- Preconditions.checkArgumentType(bytes, 'number', 'bytes');
22
20
  const result = Buffer.alloc(bytes);
23
21
  for (let i = 0; i < bytes; i++) {
24
22
  result.write('\0', i);
25
23
  }
26
24
  return result;
27
25
  }
26
+ static alloc(size, fill) {
27
+ return Buffer.alloc(size, fill);
28
+ }
28
29
  static concat(list, totalLength) {
29
30
  return Buffer.concat(list, totalLength);
30
31
  }
@@ -44,11 +45,9 @@ export class BufferUtil {
44
45
  return BufferUtil.equals(a, b);
45
46
  }
46
47
  static integerAsSingleByteBuffer(integer) {
47
- Preconditions.checkArgumentType(integer, 'number', 'integer');
48
48
  return Buffer.from([integer & 0xff]);
49
49
  }
50
50
  static integerAsBuffer(integer) {
51
- Preconditions.checkArgumentType(integer, 'number', 'integer');
52
51
  const bytes = [];
53
52
  bytes.push((integer >> 24) & 0xff);
54
53
  bytes.push((integer >> 16) & 0xff);
@@ -57,20 +56,32 @@ export class BufferUtil {
57
56
  return Buffer.from(bytes);
58
57
  }
59
58
  static integerFromBuffer(buffer) {
60
- Preconditions.checkArgumentType(buffer, 'Buffer', 'buffer');
61
59
  return (buffer[0] << 24) | (buffer[1] << 16) | (buffer[2] << 8) | buffer[3];
62
60
  }
63
61
  static integerFromSingleByteBuffer(buffer) {
64
- Preconditions.checkArgumentType(buffer, 'Buffer', 'buffer');
65
62
  return buffer[0];
66
63
  }
67
64
  static bufferToHex(buffer) {
68
- Preconditions.checkArgumentType(buffer, 'Buffer', 'buffer');
69
65
  return buffer.toString('hex');
70
66
  }
71
67
  static reverse(param) {
72
68
  return Buffer.from(param).reverse();
73
69
  }
70
+ static from(data, byteOffsetOrEncoding, length) {
71
+ if (Array.isArray(data)) {
72
+ return Buffer.from(data);
73
+ }
74
+ if (typeof data === 'string') {
75
+ return Buffer.from(data, (byteOffsetOrEncoding || 'utf8'));
76
+ }
77
+ if (data instanceof ArrayBuffer) {
78
+ return Buffer.from(data, byteOffsetOrEncoding, length);
79
+ }
80
+ return Buffer.from(data);
81
+ }
82
+ static compare(buf1, buf2) {
83
+ return Buffer.compare(buf1, buf2);
84
+ }
74
85
  }
75
86
  export const NULL_HASH = BufferUtil.fill(Buffer.alloc(32), 0);
76
87
  export const EMPTY_BUFFER = Buffer.alloc(0);
@@ -5,9 +5,6 @@ export class JSUtil {
5
5
  }
6
6
  return /^[0-9a-fA-F]+$/.test(value);
7
7
  }
8
- static isHexaString(str) {
9
- return typeof str === 'string' && /^[0-9a-fA-F]+$/.test(str);
10
- }
11
8
  static isValidJSON(arg) {
12
9
  if (typeof arg !== 'string') {
13
10
  return false;
@@ -0,0 +1,47 @@
1
+ import { Hash } from '../crypto/hash';
2
+ import { BufferUtil } from './buffer';
3
+ export function calcTreeWidth(nLeaves, height) {
4
+ return (nLeaves + (1 << height) - 1) >> height;
5
+ }
6
+ export function calcTreeHeight(nLeaves) {
7
+ let height = 0;
8
+ while (calcTreeWidth(nLeaves, height) > 1) {
9
+ height++;
10
+ }
11
+ return height;
12
+ }
13
+ export function calcMerkleHash(height, pos, hashes, nullHash = BufferUtil.alloc(32)) {
14
+ if (height === 0) {
15
+ return hashes[pos];
16
+ }
17
+ const left = calcMerkleHash(height - 1, pos * 2, hashes, nullHash);
18
+ let right = left;
19
+ if (pos * 2 + 1 < calcTreeWidth(hashes.length, height - 1)) {
20
+ right = calcMerkleHash(height - 1, pos * 2 + 1, hashes, nullHash);
21
+ }
22
+ else {
23
+ right = nullHash.toString('hex');
24
+ }
25
+ return Hash.sha256sha256(BufferUtil.concat([
26
+ BufferUtil.from(left, 'hex'),
27
+ BufferUtil.from(right, 'hex'),
28
+ ])).toString('hex');
29
+ }
30
+ export function traverseAndBuild(height, pos, nLeaves, hashes, vfMatch, vBits, vHash, nullHash = BufferUtil.alloc(32)) {
31
+ let fParentOfMatch = false;
32
+ const startIdx = pos << height;
33
+ const endIdx = Math.min((pos + 1) << height, nLeaves);
34
+ for (let p = startIdx; p < endIdx; p++) {
35
+ fParentOfMatch ||= vfMatch[p];
36
+ }
37
+ vBits.push(fParentOfMatch);
38
+ if (height === 0 || !fParentOfMatch) {
39
+ vHash.push(calcMerkleHash(height, pos, hashes, nullHash));
40
+ }
41
+ else {
42
+ traverseAndBuild(height - 1, pos * 2, nLeaves, hashes, vfMatch, vBits, vHash, nullHash);
43
+ if (pos * 2 + 1 < calcTreeWidth(nLeaves, height - 1)) {
44
+ traverseAndBuild(height - 1, pos * 2 + 1, nLeaves, hashes, vfMatch, vBits, vHash, nullHash);
45
+ }
46
+ }
47
+ }
@@ -1,4 +1,5 @@
1
- import { BitcoreError } from '../errors.js';
1
+ import { BitcoreError } from '../errors';
2
+ import { BufferUtil } from './buffer';
2
3
  export class Preconditions {
3
4
  static checkState(condition, message) {
4
5
  if (!condition) {
@@ -14,7 +15,7 @@ export class Preconditions {
14
15
  argumentName = argumentName || '(unknown name)';
15
16
  if (typeof type === 'string') {
16
17
  if (type === 'Buffer') {
17
- if (!Buffer.isBuffer(argument)) {
18
+ if (!BufferUtil.isBuffer(argument)) {
18
19
  throw new BitcoreError.Precondition.InvalidArgumentType(argument, type, argumentName);
19
20
  }
20
21
  }
@@ -1,3 +1,4 @@
1
+ import { Buffer } from 'buffer/';
1
2
  import { Preconditions } from './util/preconditions.js';
2
3
  import { Base58 } from './encoding/base58.js';
3
4
  import { BufferWriter } from './encoding/bufferwriter.js';
@@ -5,8 +6,6 @@ import { Networks } from './networks.js';
5
6
  import { Hash } from './crypto/hash.js';
6
7
  import { JSUtil } from './util/js.js';
7
8
  import { BufferUtil } from './util/buffer.js';
8
- const TOKEN_NAME = 'lotus';
9
- const ALPHABET = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz';
10
9
  export class XAddress {
11
10
  static PayToPublicKeyHash = 'pubkeyhash';
12
11
  static PayToScriptHash = 'scripthash';
@@ -15,7 +14,7 @@ export class XAddress {
15
14
  hashBuffer;
16
15
  network;
17
16
  type;
18
- constructor(data, network, type, prefix = TOKEN_NAME) {
17
+ constructor(data, network, type, prefix = 'lotus') {
19
18
  if (data instanceof XAddress) {
20
19
  return data;
21
20
  }
@@ -96,7 +95,7 @@ export class XAddress {
96
95
  }
97
96
  return info;
98
97
  }
99
- static _transformBuffer(buffer, network, type, prefix = TOKEN_NAME) {
98
+ static _transformBuffer(buffer, network, type, prefix = 'lotus') {
100
99
  const info = {};
101
100
  if (!Buffer.isBuffer(buffer) && !(buffer instanceof Uint8Array)) {
102
101
  throw new TypeError('XAddress supplied is not a buffer.');
@@ -188,7 +187,7 @@ function createChecksum(prefix, networkChar, typeByte, payload) {
188
187
  typeByte,
189
188
  payload,
190
189
  ]);
191
- return Hash.sha256(data).subarray(0, 4);
190
+ return Hash.sha256(data).slice(0, 4);
192
191
  }
193
192
  function createChecksumLegacy(prefix, networkChar, typeByte, payload) {
194
193
  const bw = new BufferWriter();
@@ -199,7 +198,7 @@ function createChecksumLegacy(prefix, networkChar, typeByte, payload) {
199
198
  bw.writeVarintNum(payload.length);
200
199
  bw.write(payload);
201
200
  const buf = bw.concat();
202
- return Hash.sha256(buf).subarray(0, 4);
201
+ return Hash.sha256(buf).slice(0, 4);
203
202
  }
204
203
  function getType(typeByte) {
205
204
  switch (typeByte) {
@@ -210,7 +209,7 @@ function getType(typeByte) {
210
209
  case 2:
211
210
  return 'taproot';
212
211
  }
213
- return 'pubkeyhash';
212
+ return null;
214
213
  }
215
214
  function getTypeByte(type) {
216
215
  switch (type) {
@@ -263,15 +262,20 @@ function decode(address) {
263
262
  const networkChar = address.substring(splitLocation, splitLocation + 1);
264
263
  const encodedPayload = address.substring(splitLocation + 1);
265
264
  const decodedBytes = Base58.decode(encodedPayload);
266
- const typeByte = decodedBytes.subarray(0, 1);
267
- const payload = decodedBytes.subarray(1, decodedBytes.length - 4);
268
- const decodedChecksum = decodedBytes.subarray(decodedBytes.length - 4);
265
+ const typeByte = decodedBytes.slice(0, 1);
266
+ const payload = decodedBytes.slice(1, decodedBytes.length - 4);
267
+ const decodedChecksum = decodedBytes.slice(decodedBytes.length - 4);
269
268
  const checksum = createChecksum(prefix, networkChar, typeByte, payload);
270
- Preconditions.checkArgument(checksum.toString('hex') === decodedChecksum.toString('hex'), 'checksum', 'Invalid checksum: ' + address);
269
+ const legacyChecksum = createChecksumLegacy(prefix, networkChar, typeByte, payload);
270
+ const validChecksum = checksum.toString('hex') === decodedChecksum.toString('hex') ||
271
+ legacyChecksum.toString('hex') === decodedChecksum.toString('hex');
272
+ Preconditions.checkArgument(validChecksum, 'checksum', 'Invalid checksum: ' + address);
273
+ const type = getType(typeByte[0]);
274
+ Preconditions.checkArgument(type !== null, 'typeByte', 'Invalid type byte: ' + typeByte[0]);
271
275
  const info = {};
272
276
  info.hashBuffer = payload;
273
277
  info.network = getNetworkFromChar(networkChar);
274
- info.type = getType(typeByte[0]);
278
+ info.type = type;
275
279
  info.prefix = prefix;
276
280
  return info;
277
281
  }