@bsv/sdk 2.1.0 → 2.1.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 (392) hide show
  1. package/README.md +7 -7
  2. package/dist/cjs/package.json +1 -1
  3. package/dist/cjs/src/auth/Peer.js +8 -13
  4. package/dist/cjs/src/auth/Peer.js.map +1 -1
  5. package/dist/cjs/src/auth/SessionManager.js +4 -7
  6. package/dist/cjs/src/auth/SessionManager.js.map +1 -1
  7. package/dist/cjs/src/auth/certificates/MasterCertificate.js +1 -1
  8. package/dist/cjs/src/auth/certificates/MasterCertificate.js.map +1 -1
  9. package/dist/cjs/src/auth/certificates/__tests/CompletedProtoWallet.js +1 -1
  10. package/dist/cjs/src/auth/certificates/__tests/CompletedProtoWallet.js.map +1 -1
  11. package/dist/cjs/src/auth/clients/AuthFetch.js +32 -32
  12. package/dist/cjs/src/auth/clients/AuthFetch.js.map +1 -1
  13. package/dist/cjs/src/auth/transports/SimplifiedFetchTransport.js +4 -4
  14. package/dist/cjs/src/auth/transports/SimplifiedFetchTransport.js.map +1 -1
  15. package/dist/cjs/src/compat/ECIES.js +29 -34
  16. package/dist/cjs/src/compat/ECIES.js.map +1 -1
  17. package/dist/cjs/src/compat/HD.js +9 -4
  18. package/dist/cjs/src/compat/HD.js.map +1 -1
  19. package/dist/cjs/src/compat/Mnemonic.js +12 -12
  20. package/dist/cjs/src/compat/Mnemonic.js.map +1 -1
  21. package/dist/cjs/src/identity/ContactsManager.js +212 -234
  22. package/dist/cjs/src/identity/ContactsManager.js.map +1 -1
  23. package/dist/cjs/src/identity/IdentityClient.js +199 -63
  24. package/dist/cjs/src/identity/IdentityClient.js.map +1 -1
  25. package/dist/cjs/src/kvstore/GlobalKVStore.js +30 -31
  26. package/dist/cjs/src/kvstore/GlobalKVStore.js.map +1 -1
  27. package/dist/cjs/src/kvstore/LocalKVStore.js +9 -9
  28. package/dist/cjs/src/kvstore/LocalKVStore.js.map +1 -1
  29. package/dist/cjs/src/kvstore/kvStoreInterpreter.js +2 -2
  30. package/dist/cjs/src/kvstore/kvStoreInterpreter.js.map +1 -1
  31. package/dist/cjs/src/messages/SignedMessage.js +1 -1
  32. package/dist/cjs/src/messages/SignedMessage.js.map +1 -1
  33. package/dist/cjs/src/overlay-tools/Historian.js +1 -1
  34. package/dist/cjs/src/overlay-tools/Historian.js.map +1 -1
  35. package/dist/cjs/src/overlay-tools/LookupResolver.js +213 -93
  36. package/dist/cjs/src/overlay-tools/LookupResolver.js.map +1 -1
  37. package/dist/cjs/src/overlay-tools/SHIPBroadcaster.js +75 -146
  38. package/dist/cjs/src/overlay-tools/SHIPBroadcaster.js.map +1 -1
  39. package/dist/cjs/src/primitives/AESGCM.js +2 -2
  40. package/dist/cjs/src/primitives/AESGCM.js.map +1 -1
  41. package/dist/cjs/src/primitives/BigNumber.js +164 -148
  42. package/dist/cjs/src/primitives/BigNumber.js.map +1 -1
  43. package/dist/cjs/src/primitives/Curve.js +17 -15
  44. package/dist/cjs/src/primitives/Curve.js.map +1 -1
  45. package/dist/cjs/src/primitives/ECDSA.js +12 -7
  46. package/dist/cjs/src/primitives/ECDSA.js.map +1 -1
  47. package/dist/cjs/src/primitives/Hash.js +312 -105
  48. package/dist/cjs/src/primitives/Hash.js.map +1 -1
  49. package/dist/cjs/src/primitives/JacobianPoint.js +8 -8
  50. package/dist/cjs/src/primitives/JacobianPoint.js.map +1 -1
  51. package/dist/cjs/src/primitives/K256.js +3 -3
  52. package/dist/cjs/src/primitives/K256.js.map +1 -1
  53. package/dist/cjs/src/primitives/Point.js +36 -40
  54. package/dist/cjs/src/primitives/Point.js.map +1 -1
  55. package/dist/cjs/src/primitives/PrivateKey.js +4 -4
  56. package/dist/cjs/src/primitives/PrivateKey.js.map +1 -1
  57. package/dist/cjs/src/primitives/PublicKey.js +4 -4
  58. package/dist/cjs/src/primitives/PublicKey.js.map +1 -1
  59. package/dist/cjs/src/primitives/Random.js +10 -14
  60. package/dist/cjs/src/primitives/Random.js.map +1 -1
  61. package/dist/cjs/src/primitives/ReaderUint8Array.js +6 -6
  62. package/dist/cjs/src/primitives/ReaderUint8Array.js.map +1 -1
  63. package/dist/cjs/src/primitives/Schnorr.js +2 -2
  64. package/dist/cjs/src/primitives/Schnorr.js.map +1 -1
  65. package/dist/cjs/src/primitives/Secp256r1.js +2 -1
  66. package/dist/cjs/src/primitives/Secp256r1.js.map +1 -1
  67. package/dist/cjs/src/primitives/Signature.js +8 -8
  68. package/dist/cjs/src/primitives/Signature.js.map +1 -1
  69. package/dist/cjs/src/primitives/SymmetricKey.js +123 -1
  70. package/dist/cjs/src/primitives/SymmetricKey.js.map +1 -1
  71. package/dist/cjs/src/primitives/TransactionSignature.js +20 -21
  72. package/dist/cjs/src/primitives/TransactionSignature.js.map +1 -1
  73. package/dist/cjs/src/primitives/utils.js +39 -46
  74. package/dist/cjs/src/primitives/utils.js.map +1 -1
  75. package/dist/cjs/src/registry/RegistryClient.js +31 -23
  76. package/dist/cjs/src/registry/RegistryClient.js.map +1 -1
  77. package/dist/cjs/src/remittance/RemittanceManager.js +19 -18
  78. package/dist/cjs/src/remittance/RemittanceManager.js.map +1 -1
  79. package/dist/cjs/src/remittance/modules/BasicBRC29.js.map +1 -1
  80. package/dist/cjs/src/script/Script.js +93 -170
  81. package/dist/cjs/src/script/Script.js.map +1 -1
  82. package/dist/cjs/src/script/ScriptEvaluationError.js +2 -2
  83. package/dist/cjs/src/script/ScriptEvaluationError.js.map +1 -1
  84. package/dist/cjs/src/script/Spend.js +14 -12
  85. package/dist/cjs/src/script/Spend.js.map +1 -1
  86. package/dist/cjs/src/script/templates/PushDrop.js +22 -18
  87. package/dist/cjs/src/script/templates/PushDrop.js.map +1 -1
  88. package/dist/cjs/src/script/templates/RPuzzle.js +2 -4
  89. package/dist/cjs/src/script/templates/RPuzzle.js.map +1 -1
  90. package/dist/cjs/src/storage/StorageDownloader.js +42 -9
  91. package/dist/cjs/src/storage/StorageDownloader.js.map +1 -1
  92. package/dist/cjs/src/totp/totp.js +1 -1
  93. package/dist/cjs/src/totp/totp.js.map +1 -1
  94. package/dist/cjs/src/transaction/Beef.js +239 -192
  95. package/dist/cjs/src/transaction/Beef.js.map +1 -1
  96. package/dist/cjs/src/transaction/BeefConstants.js +19 -0
  97. package/dist/cjs/src/transaction/BeefConstants.js.map +1 -0
  98. package/dist/cjs/src/transaction/BeefTx.js +12 -12
  99. package/dist/cjs/src/transaction/BeefTx.js.map +1 -1
  100. package/dist/cjs/src/transaction/MerklePath.js +4 -4
  101. package/dist/cjs/src/transaction/MerklePath.js.map +1 -1
  102. package/dist/cjs/src/transaction/Transaction.js +49 -52
  103. package/dist/cjs/src/transaction/Transaction.js.map +1 -1
  104. package/dist/cjs/src/transaction/fee-models/SatoshisPerKilobyte.js +1 -1
  105. package/dist/cjs/src/transaction/fee-models/SatoshisPerKilobyte.js.map +1 -1
  106. package/dist/cjs/src/transaction/http/BinaryFetchClient.js +9 -9
  107. package/dist/cjs/src/transaction/http/BinaryFetchClient.js.map +1 -1
  108. package/dist/cjs/src/transaction/http/DefaultHttpClient.js +9 -9
  109. package/dist/cjs/src/transaction/http/DefaultHttpClient.js.map +1 -1
  110. package/dist/cjs/src/wallet/CachedKeyDeriver.js +1 -1
  111. package/dist/cjs/src/wallet/CachedKeyDeriver.js.map +1 -1
  112. package/dist/cjs/src/wallet/WalletClient.js.map +1 -1
  113. package/dist/cjs/src/wallet/WalletError.js.map +1 -1
  114. package/dist/cjs/src/wallet/substrates/HTTPWalletJSON.js +5 -4
  115. package/dist/cjs/src/wallet/substrates/HTTPWalletJSON.js.map +1 -1
  116. package/dist/cjs/src/wallet/substrates/ReactNativeWebView.js +9 -9
  117. package/dist/cjs/src/wallet/substrates/ReactNativeWebView.js.map +1 -1
  118. package/dist/cjs/src/wallet/substrates/WalletWireProcessor.js +92 -92
  119. package/dist/cjs/src/wallet/substrates/WalletWireProcessor.js.map +1 -1
  120. package/dist/cjs/src/wallet/substrates/WalletWireTransceiver.js +387 -711
  121. package/dist/cjs/src/wallet/substrates/WalletWireTransceiver.js.map +1 -1
  122. package/dist/cjs/src/wallet/substrates/XDM.js +4 -4
  123. package/dist/cjs/src/wallet/substrates/XDM.js.map +1 -1
  124. package/dist/cjs/src/wallet/substrates/window.CWI.js +2 -2
  125. package/dist/cjs/src/wallet/substrates/window.CWI.js.map +1 -1
  126. package/dist/cjs/src/wallet/validationHelpers.js +9 -9
  127. package/dist/cjs/src/wallet/validationHelpers.js.map +1 -1
  128. package/dist/cjs/tsconfig.cjs.tsbuildinfo +1 -1
  129. package/dist/esm/src/auth/Peer.js +25 -13
  130. package/dist/esm/src/auth/Peer.js.map +1 -1
  131. package/dist/esm/src/auth/SessionManager.js +4 -7
  132. package/dist/esm/src/auth/SessionManager.js.map +1 -1
  133. package/dist/esm/src/auth/certificates/MasterCertificate.js +1 -1
  134. package/dist/esm/src/auth/certificates/MasterCertificate.js.map +1 -1
  135. package/dist/esm/src/auth/certificates/__tests/CompletedProtoWallet.js +1 -1
  136. package/dist/esm/src/auth/certificates/__tests/CompletedProtoWallet.js.map +1 -1
  137. package/dist/esm/src/auth/clients/AuthFetch.js +32 -32
  138. package/dist/esm/src/auth/clients/AuthFetch.js.map +1 -1
  139. package/dist/esm/src/auth/transports/SimplifiedFetchTransport.js +4 -4
  140. package/dist/esm/src/auth/transports/SimplifiedFetchTransport.js.map +1 -1
  141. package/dist/esm/src/compat/ECIES.js +29 -34
  142. package/dist/esm/src/compat/ECIES.js.map +1 -1
  143. package/dist/esm/src/compat/HD.js +9 -4
  144. package/dist/esm/src/compat/HD.js.map +1 -1
  145. package/dist/esm/src/compat/Mnemonic.js +12 -12
  146. package/dist/esm/src/compat/Mnemonic.js.map +1 -1
  147. package/dist/esm/src/identity/ContactsManager.js +212 -234
  148. package/dist/esm/src/identity/ContactsManager.js.map +1 -1
  149. package/dist/esm/src/identity/IdentityClient.js +199 -63
  150. package/dist/esm/src/identity/IdentityClient.js.map +1 -1
  151. package/dist/esm/src/kvstore/GlobalKVStore.js +30 -31
  152. package/dist/esm/src/kvstore/GlobalKVStore.js.map +1 -1
  153. package/dist/esm/src/kvstore/LocalKVStore.js +9 -9
  154. package/dist/esm/src/kvstore/LocalKVStore.js.map +1 -1
  155. package/dist/esm/src/kvstore/kvStoreInterpreter.js +2 -2
  156. package/dist/esm/src/kvstore/kvStoreInterpreter.js.map +1 -1
  157. package/dist/esm/src/messages/SignedMessage.js +1 -1
  158. package/dist/esm/src/messages/SignedMessage.js.map +1 -1
  159. package/dist/esm/src/overlay-tools/Historian.js +1 -1
  160. package/dist/esm/src/overlay-tools/Historian.js.map +1 -1
  161. package/dist/esm/src/overlay-tools/LookupResolver.js +213 -93
  162. package/dist/esm/src/overlay-tools/LookupResolver.js.map +1 -1
  163. package/dist/esm/src/overlay-tools/SHIPBroadcaster.js +74 -146
  164. package/dist/esm/src/overlay-tools/SHIPBroadcaster.js.map +1 -1
  165. package/dist/esm/src/primitives/AESGCM.js +2 -2
  166. package/dist/esm/src/primitives/AESGCM.js.map +1 -1
  167. package/dist/esm/src/primitives/BigNumber.js +167 -154
  168. package/dist/esm/src/primitives/BigNumber.js.map +1 -1
  169. package/dist/esm/src/primitives/Curve.js +17 -15
  170. package/dist/esm/src/primitives/Curve.js.map +1 -1
  171. package/dist/esm/src/primitives/ECDSA.js +12 -7
  172. package/dist/esm/src/primitives/ECDSA.js.map +1 -1
  173. package/dist/esm/src/primitives/Hash.js +316 -105
  174. package/dist/esm/src/primitives/Hash.js.map +1 -1
  175. package/dist/esm/src/primitives/JacobianPoint.js +8 -8
  176. package/dist/esm/src/primitives/JacobianPoint.js.map +1 -1
  177. package/dist/esm/src/primitives/K256.js +3 -3
  178. package/dist/esm/src/primitives/K256.js.map +1 -1
  179. package/dist/esm/src/primitives/Point.js +36 -40
  180. package/dist/esm/src/primitives/Point.js.map +1 -1
  181. package/dist/esm/src/primitives/PrivateKey.js +4 -4
  182. package/dist/esm/src/primitives/PrivateKey.js.map +1 -1
  183. package/dist/esm/src/primitives/PublicKey.js +4 -4
  184. package/dist/esm/src/primitives/PublicKey.js.map +1 -1
  185. package/dist/esm/src/primitives/Random.js +10 -14
  186. package/dist/esm/src/primitives/Random.js.map +1 -1
  187. package/dist/esm/src/primitives/ReaderUint8Array.js +6 -6
  188. package/dist/esm/src/primitives/ReaderUint8Array.js.map +1 -1
  189. package/dist/esm/src/primitives/Schnorr.js +1 -1
  190. package/dist/esm/src/primitives/Schnorr.js.map +1 -1
  191. package/dist/esm/src/primitives/Secp256r1.js +2 -1
  192. package/dist/esm/src/primitives/Secp256r1.js.map +1 -1
  193. package/dist/esm/src/primitives/Signature.js +8 -8
  194. package/dist/esm/src/primitives/Signature.js.map +1 -1
  195. package/dist/esm/src/primitives/SymmetricKey.js +123 -1
  196. package/dist/esm/src/primitives/SymmetricKey.js.map +1 -1
  197. package/dist/esm/src/primitives/TransactionSignature.js +20 -21
  198. package/dist/esm/src/primitives/TransactionSignature.js.map +1 -1
  199. package/dist/esm/src/primitives/utils.js +39 -48
  200. package/dist/esm/src/primitives/utils.js.map +1 -1
  201. package/dist/esm/src/registry/RegistryClient.js +31 -23
  202. package/dist/esm/src/registry/RegistryClient.js.map +1 -1
  203. package/dist/esm/src/remittance/RemittanceManager.js +19 -18
  204. package/dist/esm/src/remittance/RemittanceManager.js.map +1 -1
  205. package/dist/esm/src/remittance/modules/BasicBRC29.js.map +1 -1
  206. package/dist/esm/src/script/Script.js +93 -170
  207. package/dist/esm/src/script/Script.js.map +1 -1
  208. package/dist/esm/src/script/ScriptEvaluationError.js +2 -2
  209. package/dist/esm/src/script/ScriptEvaluationError.js.map +1 -1
  210. package/dist/esm/src/script/Spend.js +14 -12
  211. package/dist/esm/src/script/Spend.js.map +1 -1
  212. package/dist/esm/src/script/templates/PushDrop.js +4 -3
  213. package/dist/esm/src/script/templates/PushDrop.js.map +1 -1
  214. package/dist/esm/src/script/templates/RPuzzle.js +2 -4
  215. package/dist/esm/src/script/templates/RPuzzle.js.map +1 -1
  216. package/dist/esm/src/storage/StorageDownloader.js +1 -1
  217. package/dist/esm/src/storage/StorageDownloader.js.map +1 -1
  218. package/dist/esm/src/totp/totp.js +1 -1
  219. package/dist/esm/src/totp/totp.js.map +1 -1
  220. package/dist/esm/src/transaction/Beef.js +229 -186
  221. package/dist/esm/src/transaction/Beef.js.map +1 -1
  222. package/dist/esm/src/transaction/BeefConstants.js +16 -0
  223. package/dist/esm/src/transaction/BeefConstants.js.map +1 -0
  224. package/dist/esm/src/transaction/BeefTx.js +3 -3
  225. package/dist/esm/src/transaction/BeefTx.js.map +1 -1
  226. package/dist/esm/src/transaction/MerklePath.js +4 -4
  227. package/dist/esm/src/transaction/MerklePath.js.map +1 -1
  228. package/dist/esm/src/transaction/Transaction.js +49 -52
  229. package/dist/esm/src/transaction/Transaction.js.map +1 -1
  230. package/dist/esm/src/transaction/fee-models/SatoshisPerKilobyte.js +1 -1
  231. package/dist/esm/src/transaction/fee-models/SatoshisPerKilobyte.js.map +1 -1
  232. package/dist/esm/src/transaction/http/BinaryFetchClient.js +9 -9
  233. package/dist/esm/src/transaction/http/BinaryFetchClient.js.map +1 -1
  234. package/dist/esm/src/transaction/http/DefaultHttpClient.js +9 -9
  235. package/dist/esm/src/transaction/http/DefaultHttpClient.js.map +1 -1
  236. package/dist/esm/src/wallet/CachedKeyDeriver.js +1 -1
  237. package/dist/esm/src/wallet/CachedKeyDeriver.js.map +1 -1
  238. package/dist/esm/src/wallet/WalletClient.js.map +1 -1
  239. package/dist/esm/src/wallet/WalletError.js.map +1 -1
  240. package/dist/esm/src/wallet/substrates/HTTPWalletJSON.js +5 -4
  241. package/dist/esm/src/wallet/substrates/HTTPWalletJSON.js.map +1 -1
  242. package/dist/esm/src/wallet/substrates/ReactNativeWebView.js +9 -9
  243. package/dist/esm/src/wallet/substrates/ReactNativeWebView.js.map +1 -1
  244. package/dist/esm/src/wallet/substrates/WalletWireProcessor.js +92 -92
  245. package/dist/esm/src/wallet/substrates/WalletWireProcessor.js.map +1 -1
  246. package/dist/esm/src/wallet/substrates/WalletWireTransceiver.js +387 -711
  247. package/dist/esm/src/wallet/substrates/WalletWireTransceiver.js.map +1 -1
  248. package/dist/esm/src/wallet/substrates/XDM.js +4 -4
  249. package/dist/esm/src/wallet/substrates/XDM.js.map +1 -1
  250. package/dist/esm/src/wallet/substrates/window.CWI.js +2 -2
  251. package/dist/esm/src/wallet/substrates/window.CWI.js.map +1 -1
  252. package/dist/esm/src/wallet/validationHelpers.js +9 -9
  253. package/dist/esm/src/wallet/validationHelpers.js.map +1 -1
  254. package/dist/esm/tsconfig.esm.tsbuildinfo +1 -1
  255. package/dist/types/src/auth/Peer.d.ts +13 -0
  256. package/dist/types/src/auth/Peer.d.ts.map +1 -1
  257. package/dist/types/src/auth/SessionManager.d.ts.map +1 -1
  258. package/dist/types/src/auth/clients/AuthFetch.d.ts.map +1 -1
  259. package/dist/types/src/compat/ECIES.d.ts.map +1 -1
  260. package/dist/types/src/compat/HD.d.ts.map +1 -1
  261. package/dist/types/src/identity/ContactsManager.d.ts +31 -2
  262. package/dist/types/src/identity/ContactsManager.d.ts.map +1 -1
  263. package/dist/types/src/identity/IdentityClient.d.ts +75 -10
  264. package/dist/types/src/identity/IdentityClient.d.ts.map +1 -1
  265. package/dist/types/src/kvstore/GlobalKVStore.d.ts.map +1 -1
  266. package/dist/types/src/overlay-tools/LookupResolver.d.ts +73 -2
  267. package/dist/types/src/overlay-tools/LookupResolver.d.ts.map +1 -1
  268. package/dist/types/src/overlay-tools/SHIPBroadcaster.d.ts +18 -3
  269. package/dist/types/src/overlay-tools/SHIPBroadcaster.d.ts.map +1 -1
  270. package/dist/types/src/primitives/BigNumber.d.ts +13 -3
  271. package/dist/types/src/primitives/BigNumber.d.ts.map +1 -1
  272. package/dist/types/src/primitives/Curve.d.ts.map +1 -1
  273. package/dist/types/src/primitives/ECDSA.d.ts.map +1 -1
  274. package/dist/types/src/primitives/Hash.d.ts +22 -17
  275. package/dist/types/src/primitives/Hash.d.ts.map +1 -1
  276. package/dist/types/src/primitives/JacobianPoint.d.ts +3 -1
  277. package/dist/types/src/primitives/JacobianPoint.d.ts.map +1 -1
  278. package/dist/types/src/primitives/Point.d.ts.map +1 -1
  279. package/dist/types/src/primitives/Random.d.ts +2 -2
  280. package/dist/types/src/primitives/Random.d.ts.map +1 -1
  281. package/dist/types/src/primitives/ReaderUint8Array.d.ts.map +1 -1
  282. package/dist/types/src/primitives/Schnorr.d.ts +2 -1
  283. package/dist/types/src/primitives/Schnorr.d.ts.map +1 -1
  284. package/dist/types/src/primitives/Secp256r1.d.ts.map +1 -1
  285. package/dist/types/src/primitives/SymmetricKey.d.ts.map +1 -1
  286. package/dist/types/src/primitives/utils.d.ts +2 -4
  287. package/dist/types/src/primitives/utils.d.ts.map +1 -1
  288. package/dist/types/src/registry/RegistryClient.d.ts.map +1 -1
  289. package/dist/types/src/remittance/RemittanceManager.d.ts.map +1 -1
  290. package/dist/types/src/remittance/modules/BasicBRC29.d.ts.map +1 -1
  291. package/dist/types/src/script/Script.d.ts +15 -8
  292. package/dist/types/src/script/Script.d.ts.map +1 -1
  293. package/dist/types/src/script/Spend.d.ts.map +1 -1
  294. package/dist/types/src/script/templates/PushDrop.d.ts +3 -1
  295. package/dist/types/src/script/templates/PushDrop.d.ts.map +1 -1
  296. package/dist/types/src/script/templates/RPuzzle.d.ts.map +1 -1
  297. package/dist/types/src/transaction/Beef.d.ts +46 -8
  298. package/dist/types/src/transaction/Beef.d.ts.map +1 -1
  299. package/dist/types/src/transaction/BeefConstants.d.ts +15 -0
  300. package/dist/types/src/transaction/BeefConstants.d.ts.map +1 -0
  301. package/dist/types/src/transaction/Transaction.d.ts.map +1 -1
  302. package/dist/types/src/wallet/CachedKeyDeriver.d.ts.map +1 -1
  303. package/dist/types/src/wallet/KeyDeriver.d.ts +1 -1
  304. package/dist/types/src/wallet/KeyDeriver.d.ts.map +1 -1
  305. package/dist/types/src/wallet/Wallet.interfaces.d.ts +18 -3
  306. package/dist/types/src/wallet/Wallet.interfaces.d.ts.map +1 -1
  307. package/dist/types/src/wallet/WalletClient.d.ts +8 -8
  308. package/dist/types/src/wallet/WalletClient.d.ts.map +1 -1
  309. package/dist/types/src/wallet/substrates/HTTPWalletJSON.d.ts +7 -7
  310. package/dist/types/src/wallet/substrates/HTTPWalletJSON.d.ts.map +1 -1
  311. package/dist/types/src/wallet/substrates/WalletWireTransceiver.d.ts +36 -7
  312. package/dist/types/src/wallet/substrates/WalletWireTransceiver.d.ts.map +1 -1
  313. package/dist/types/src/wallet/substrates/window.CWI.d.ts +9 -9
  314. package/dist/types/src/wallet/substrates/window.CWI.d.ts.map +1 -1
  315. package/dist/types/tsconfig.types.tsbuildinfo +1 -1
  316. package/dist/umd/bundle.js +3 -3
  317. package/package.json +1 -1
  318. package/src/auth/Peer.ts +26 -13
  319. package/src/auth/SessionManager.ts +4 -7
  320. package/src/auth/certificates/MasterCertificate.ts +1 -1
  321. package/src/auth/certificates/__tests/CompletedProtoWallet.ts +1 -1
  322. package/src/auth/clients/AuthFetch.ts +41 -41
  323. package/src/auth/transports/SimplifiedFetchTransport.ts +4 -4
  324. package/src/compat/ECIES.ts +29 -34
  325. package/src/compat/HD.ts +10 -5
  326. package/src/compat/Mnemonic.ts +11 -11
  327. package/src/compat/__tests/HD.test.ts +19 -0
  328. package/src/identity/ContactsManager.ts +236 -258
  329. package/src/identity/IdentityClient.ts +244 -71
  330. package/src/identity/__tests/IdentityClient.additional.test.ts +150 -1
  331. package/src/identity/__tests/IdentityClient.test.ts +27 -3
  332. package/src/kvstore/GlobalKVStore.ts +31 -32
  333. package/src/kvstore/LocalKVStore.ts +8 -8
  334. package/src/kvstore/kvStoreInterpreter.ts +2 -2
  335. package/src/messages/SignedMessage.ts +1 -1
  336. package/src/overlay-tools/Historian.ts +1 -1
  337. package/src/overlay-tools/LookupResolver.ts +264 -90
  338. package/src/overlay-tools/SHIPBroadcaster.ts +92 -168
  339. package/src/primitives/AESGCM.ts +2 -2
  340. package/src/primitives/BigNumber.ts +122 -113
  341. package/src/primitives/Curve.ts +16 -15
  342. package/src/primitives/ECDSA.ts +10 -8
  343. package/src/primitives/Hash.ts +381 -146
  344. package/src/primitives/JacobianPoint.ts +13 -11
  345. package/src/primitives/K256.ts +3 -3
  346. package/src/primitives/Point.ts +35 -38
  347. package/src/primitives/PrivateKey.ts +3 -3
  348. package/src/primitives/PublicKey.ts +3 -3
  349. package/src/primitives/Random.ts +11 -14
  350. package/src/primitives/ReaderUint8Array.ts +7 -7
  351. package/src/primitives/Schnorr.ts +2 -1
  352. package/src/primitives/Secp256r1.ts +2 -1
  353. package/src/primitives/Signature.ts +8 -8
  354. package/src/primitives/SymmetricKey.ts +145 -1
  355. package/src/primitives/TransactionSignature.ts +16 -16
  356. package/src/primitives/__tests/Hash.additional.test.ts +65 -0
  357. package/src/primitives/__tests/Hash.test.ts +6 -1
  358. package/src/primitives/utils.ts +37 -47
  359. package/src/registry/RegistryClient.ts +25 -25
  360. package/src/remittance/RemittanceManager.ts +17 -18
  361. package/src/remittance/modules/BasicBRC29.ts +2 -5
  362. package/src/script/Script.ts +114 -170
  363. package/src/script/ScriptEvaluationError.ts +2 -2
  364. package/src/script/Spend.ts +14 -15
  365. package/src/script/templates/PushDrop.ts +5 -3
  366. package/src/script/templates/RPuzzle.ts +2 -4
  367. package/src/storage/StorageDownloader.ts +1 -1
  368. package/src/totp/totp.ts +1 -1
  369. package/src/transaction/Beef.ts +241 -203
  370. package/src/transaction/BeefConstants.ts +16 -0
  371. package/src/transaction/BeefTx.ts +3 -3
  372. package/src/transaction/MerklePath.ts +4 -4
  373. package/src/transaction/Transaction.ts +48 -51
  374. package/src/transaction/fee-models/SatoshisPerKilobyte.ts +1 -1
  375. package/src/transaction/http/BinaryFetchClient.ts +8 -8
  376. package/src/transaction/http/DefaultHttpClient.ts +8 -8
  377. package/src/wallet/CachedKeyDeriver.ts +8 -6
  378. package/src/wallet/KeyDeriver.ts +1 -1
  379. package/src/wallet/Wallet.interfaces.ts +18 -5
  380. package/src/wallet/WalletClient.ts +9 -9
  381. package/src/wallet/WalletError.ts +1 -1
  382. package/src/wallet/__tests/WalletClient.substrate.test.ts +10 -6
  383. package/src/wallet/substrates/HTTPWalletJSON.ts +22 -21
  384. package/src/wallet/substrates/ReactNativeWebView.ts +9 -9
  385. package/src/wallet/substrates/WalletWireProcessor.ts +83 -83
  386. package/src/wallet/substrates/WalletWireTransceiver.ts +528 -938
  387. package/src/wallet/substrates/XDM.ts +4 -4
  388. package/src/wallet/substrates/__tests/HTTPWalletJSON.test.ts +38 -25
  389. package/src/wallet/substrates/__tests/ReactNativeWebView.test.ts +174 -0
  390. package/src/wallet/substrates/__tests/window.CWI.test.ts +256 -0
  391. package/src/wallet/substrates/window.CWI.ts +11 -11
  392. package/src/wallet/validationHelpers.ts +9 -9
@@ -8,15 +8,12 @@ const MerklePath_js_1 = __importDefault(require("./MerklePath.js"));
8
8
  const BeefTx_js_1 = __importDefault(require("./BeefTx.js"));
9
9
  const utils_js_1 = require("../primitives/utils.js");
10
10
  const Hash_js_1 = require("../primitives/Hash.js");
11
- exports.BEEF_V1 = 4022206465; // 0100BEEF in LE order
12
- exports.BEEF_V2 = 4022206466; // 0200BEEF in LE order
13
- exports.ATOMIC_BEEF = 0x01010101; // 01010101
14
- var TX_DATA_FORMAT;
15
- (function (TX_DATA_FORMAT) {
16
- TX_DATA_FORMAT[TX_DATA_FORMAT["RAWTX"] = 0] = "RAWTX";
17
- TX_DATA_FORMAT[TX_DATA_FORMAT["RAWTX_AND_BUMP_INDEX"] = 1] = "RAWTX_AND_BUMP_INDEX";
18
- TX_DATA_FORMAT[TX_DATA_FORMAT["TXID_ONLY"] = 2] = "TXID_ONLY";
19
- })(TX_DATA_FORMAT || (exports.TX_DATA_FORMAT = TX_DATA_FORMAT = {}));
11
+ const BeefConstants_js_1 = require("./BeefConstants.js");
12
+ var BeefConstants_js_2 = require("./BeefConstants.js");
13
+ Object.defineProperty(exports, "BEEF_V1", { enumerable: true, get: function () { return BeefConstants_js_2.BEEF_V1; } });
14
+ Object.defineProperty(exports, "BEEF_V2", { enumerable: true, get: function () { return BeefConstants_js_2.BEEF_V2; } });
15
+ Object.defineProperty(exports, "ATOMIC_BEEF", { enumerable: true, get: function () { return BeefConstants_js_2.ATOMIC_BEEF; } });
16
+ Object.defineProperty(exports, "TX_DATA_FORMAT", { enumerable: true, get: function () { return BeefConstants_js_2.TX_DATA_FORMAT; } });
20
17
  /*
21
18
  * BEEF standard: BRC-62: Background Evaluation Extended Format (BEEF) Transactions
22
19
  * https://github.com/bsv-blockchain/BRCs/blob/master/transactions/0062.md
@@ -71,10 +68,10 @@ var TX_DATA_FORMAT;
71
68
  * must be discarded.
72
69
  */
73
70
  class Beef {
74
- constructor(version = exports.BEEF_V2) {
71
+ constructor(version = BeefConstants_js_1.BEEF_V2) {
75
72
  this.bumps = [];
76
73
  this.txs = [];
77
- this.version = exports.BEEF_V2;
74
+ this.version = BeefConstants_js_1.BEEF_V2;
78
75
  this.atomicTxid = undefined;
79
76
  this.txidIndex = undefined;
80
77
  this.needsSort = true;
@@ -92,7 +89,9 @@ class Beef {
92
89
  }
93
90
  ensureSerializableState() {
94
91
  for (const tx of this.txs) {
95
- void tx.txid;
92
+ // Access txid to ensure it is computed before serialization
93
+ // eslint-disable-next-line @typescript-eslint/no-unused-expressions
94
+ tx.txid;
96
95
  }
97
96
  }
98
97
  ensureSortedForSerialization() {
@@ -119,13 +118,13 @@ class Beef {
119
118
  throw new Error(`${txid} does not exist in this Beef`);
120
119
  }
121
120
  // If the transaction is not the last one, clone and modify
122
- const beef = (this.txs[this.txs.length - 1] === tx) ? this : this.clone();
121
+ const beef = (this.txs.at(-1) === tx) ? this : this.clone();
123
122
  if (beef !== this) {
124
123
  const i = this.txs.findIndex((t) => t.txid === txid);
125
124
  beef.txs.splice(i + 1);
126
125
  }
127
126
  const writer = new utils_js_1.WriterUint8Array();
128
- writer.writeUInt32LE(exports.ATOMIC_BEEF);
127
+ writer.writeUInt32LE(BeefConstants_js_1.ATOMIC_BEEF);
129
128
  writer.writeReverse((0, utils_js_1.toArray)(txid, 'hex'));
130
129
  return { beef, writer };
131
130
  }
@@ -179,8 +178,7 @@ class Beef {
179
178
  * @returns `MerklePath` with level zero hash equal to txid or undefined.
180
179
  */
181
180
  findBump(txid) {
182
- return this.bumps.find((b) => b.path[0].some((leaf) => leaf.hash === txid) // ✅ Ensure boolean return with `.some()`
183
- );
181
+ return this.bumps.find((b) => b.path[0].some((leaf) => leaf.hash === txid));
184
182
  }
185
183
  /**
186
184
  * Finds a Transaction in this `Beef`
@@ -217,33 +215,39 @@ class Beef {
217
215
  const beefTx = this.findTxid(txid);
218
216
  if ((beefTx == null) || (beefTx.tx == null))
219
217
  return undefined; // Ensure beefTx.tx exists before using it
220
- const addInputProof = (beef, tx) => {
221
- const mp = beef.findBump(tx.id('hex'));
222
- if (mp != null) {
223
- tx.merklePath = mp;
218
+ this.addInputProof(beefTx.tx);
219
+ return beefTx.tx;
220
+ }
221
+ /** Recursively attach merkle paths and source transactions to all inputs. */
222
+ addInputProof(tx) {
223
+ const mp = this.findBump(tx.id('hex'));
224
+ if (mp != null) {
225
+ tx.merklePath = mp;
226
+ return;
227
+ }
228
+ for (const i of tx.inputs) {
229
+ this.resolveInputSource(i);
230
+ if (i.sourceTransaction != null) {
231
+ this.attachMerklePathOrRecurse(i.sourceTransaction);
224
232
  }
225
- else {
226
- for (const i of tx.inputs) {
227
- if (i.sourceTransaction == null) {
228
- const itx = beef.findTxid((0, utils_js_1.verifyNotNull)(i.sourceTXID, 'sourceTXID must be valid'));
229
- if (itx != null) {
230
- i.sourceTransaction = itx.tx;
231
- }
232
- }
233
- if (i.sourceTransaction != null) {
234
- const mp = beef.findBump(i.sourceTransaction.id('hex'));
235
- if (mp != null) {
236
- i.sourceTransaction.merklePath = mp;
237
- }
238
- else {
239
- addInputProof(beef, i.sourceTransaction);
240
- }
241
- }
242
- }
233
+ }
234
+ }
235
+ resolveInputSource(i) {
236
+ if (i.sourceTransaction == null) {
237
+ const itx = this.findTxid((0, utils_js_1.verifyNotNull)(i.sourceTXID, 'sourceTXID must be valid'));
238
+ if (itx != null) {
239
+ i.sourceTransaction = itx.tx;
243
240
  }
244
- };
245
- addInputProof(this, beefTx.tx); // Safe because we checked that beefTx.tx exists
246
- return beefTx.tx;
241
+ }
242
+ }
243
+ attachMerklePathOrRecurse(sourceTx) {
244
+ const mp = this.findBump(sourceTx.id('hex'));
245
+ if (mp != null) {
246
+ sourceTx.merklePath = mp;
247
+ }
248
+ else {
249
+ this.addInputProof(sourceTx);
250
+ }
247
251
  }
248
252
  /**
249
253
  * Merge a MerklePath that is assumed to be fully valid.
@@ -252,46 +256,44 @@ class Beef {
252
256
  */
253
257
  mergeBump(bump) {
254
258
  this.markMutated(false);
255
- let bumpIndex;
256
- // If this proof is identical to another one previously added, we use that first. Otherwise, we try to merge it with proofs from the same block.
259
+ const bumpIndex = this.findOrInsertBump(bump);
260
+ // Review if any transactions are proven by this bump
261
+ const b = this.bumps[bumpIndex];
262
+ for (const tx of this.txs) {
263
+ if (tx.bumpIndex == null) {
264
+ this.tryMarkTxProvenByBump(tx, b, bumpIndex);
265
+ }
266
+ }
267
+ return bumpIndex;
268
+ }
269
+ /**
270
+ * Find an existing compatible bump or insert a new one; return its index.
271
+ */
272
+ findOrInsertBump(bump) {
257
273
  for (let i = 0; i < this.bumps.length; i++) {
258
274
  const b = this.bumps[i];
259
- if (b === bump) {
260
- // Literally the same
275
+ if (b === bump)
276
+ return i;
277
+ if (b.blockHeight !== bump.blockHeight)
278
+ continue;
279
+ if (b.computeRoot() === bump.computeRoot()) {
280
+ b.combine(bump);
261
281
  return i;
262
282
  }
263
- if (b.blockHeight === bump.blockHeight) {
264
- // Probably the same...
265
- const rootA = b.computeRoot();
266
- const rootB = bump.computeRoot();
267
- if (rootA === rootB) {
268
- // Definitely the same... combine them to save space
269
- b.combine(bump);
270
- bumpIndex = i;
271
- break;
272
- }
273
- }
274
- }
275
- // if the proof is not yet added, add a new path.
276
- if (bumpIndex === undefined) {
277
- bumpIndex = this.bumps.length;
278
- this.bumps.push(bump);
279
283
  }
280
- // Review if any transactions are proven by this bump
281
- const b = this.bumps[bumpIndex];
282
- for (const tx of this.txs) {
283
- const txid = tx.txid;
284
- if (tx.bumpIndex == null) { // ✅ Explicitly check for null or undefined
285
- for (const n of b.path[0]) {
286
- if (n.hash === txid) {
287
- tx.bumpIndex = bumpIndex;
288
- n.txid = true;
289
- break;
290
- }
291
- }
284
+ this.bumps.push(bump);
285
+ return this.bumps.length - 1;
286
+ }
287
+ /** If bump's level-0 path contains tx's txid, record the bumpIndex on tx. */
288
+ tryMarkTxProvenByBump(tx, b, bumpIndex) {
289
+ const txid = tx.txid;
290
+ for (const n of b.path[0]) {
291
+ if (n.hash === txid) {
292
+ tx.bumpIndex = bumpIndex;
293
+ n.txid = true;
294
+ break;
292
295
  }
293
296
  }
294
- return bumpIndex;
295
297
  }
296
298
  /**
297
299
  * Merge a serialized transaction.
@@ -466,54 +468,74 @@ class Beef {
466
468
  }
467
469
  // valid txids: only txids if allowed, bump txids, then txids with input's in txids
468
470
  const txids = {};
471
+ if (!this.collectTxidOnlyTxids(txids, allowTxidOnly))
472
+ return r;
473
+ if (!this.collectBumpTxids(txids, r))
474
+ return r;
475
+ if (!this.verifyBumpIndexLeaves())
476
+ return r;
477
+ if (!this.verifyInputDependencies(txids))
478
+ return r;
479
+ r.valid = true;
480
+ return r;
481
+ }
482
+ /** Add txidOnly transaction txids; return false if not allowed. */
483
+ collectTxidOnlyTxids(txids, allowTxidOnly) {
469
484
  for (const tx of this.txs) {
470
- if (tx.isTxidOnly) {
471
- if (allowTxidOnly !== true)
472
- return r; // ✅ Explicit check for `true`
473
- txids[tx.txid] = true;
474
- }
475
- }
476
- const confirmComputedRoot = (b, txid) => {
477
- const root = b.computeRoot(txid);
478
- if (r.roots[b.blockHeight] === undefined || r.roots[b.blockHeight] === '') {
479
- // accept the root as valid for this block and reuse for subsequent txids
480
- r.roots[b.blockHeight] = root;
481
- }
482
- if (r.roots[b.blockHeight] !== root) {
485
+ if (!tx.isTxidOnly)
486
+ continue;
487
+ if (allowTxidOnly !== true)
483
488
  return false;
484
- }
485
- return true;
486
- };
489
+ txids[tx.txid] = true;
490
+ }
491
+ return true;
492
+ }
493
+ /**
494
+ * Record txids proven by bumps; validate all bump roots agree per block height.
495
+ * Returns false if any root conflict is detected.
496
+ */
497
+ collectBumpTxids(txids, r) {
487
498
  for (const b of this.bumps) {
488
499
  for (const n of b.path[0]) {
489
- if (n.txid === true && typeof n.hash === 'string' && n.hash.length > 0) {
490
- txids[n.hash] = true;
491
- // All txid hashes in all bumps must agree on computed merkle path roots
492
- if (!confirmComputedRoot(b, n.hash)) {
493
- return r;
494
- }
495
- }
500
+ if (n.txid !== true || typeof n.hash !== 'string' || n.hash.length === 0)
501
+ continue;
502
+ txids[n.hash] = true;
503
+ if (!this.confirmComputedRoot(b, n.hash, r))
504
+ return false;
496
505
  }
497
506
  }
498
- // All txs with a bumpIndex have matching txid leaf at level zero of BUMP.
507
+ return true;
508
+ }
509
+ /** Verify that every tx with a bumpIndex has a matching txid leaf in its bump. */
510
+ verifyBumpIndexLeaves() {
499
511
  for (const t of this.txs) {
500
- if (t.bumpIndex !== undefined) {
501
- const leaf = this.bumps[t.bumpIndex].path[0].find(l => l.hash === t.txid);
502
- if (leaf == null) {
503
- return r;
504
- }
505
- }
512
+ if (t.bumpIndex === undefined)
513
+ continue;
514
+ const leaf = this.bumps[t.bumpIndex].path[0].find(l => l.hash === t.txid);
515
+ if (leaf == null)
516
+ return false;
506
517
  }
518
+ return true;
519
+ }
520
+ /** Verify all input txids appear before the spending tx in sorted order. */
521
+ verifyInputDependencies(txids) {
507
522
  for (const t of this.txs) {
508
- // all input txids must be included before they are referenced
509
523
  for (const i of t.inputTxids) {
510
524
  if (!txids[i])
511
- return r;
525
+ return false;
512
526
  }
513
527
  txids[t.txid] = true;
514
528
  }
515
- r.valid = true;
516
- return r;
529
+ return true;
530
+ }
531
+ /** Confirm the computed merkle root for txid in bump matches previously accepted root for that height. */
532
+ confirmComputedRoot(b, txid, r) {
533
+ const root = b.computeRoot(txid);
534
+ if (r.roots[b.blockHeight] === undefined || r.roots[b.blockHeight] === '') {
535
+ // accept the root as valid for this block and reuse for subsequent txids
536
+ r.roots[b.blockHeight] = root;
537
+ }
538
+ return r.roots[b.blockHeight] === root;
517
539
  }
518
540
  /**
519
541
  * Serializes this data to `writer`
@@ -595,13 +617,13 @@ class Beef {
595
617
  static fromReader(br) {
596
618
  let version = br.readUInt32LE();
597
619
  let atomicTxid;
598
- if (version === exports.ATOMIC_BEEF) {
620
+ if (version === BeefConstants_js_1.ATOMIC_BEEF) {
599
621
  // Skip the txid and re-read the BEEF version
600
622
  atomicTxid = (0, utils_js_1.toHex)(br.readReverse(32));
601
623
  version = br.readUInt32LE();
602
624
  }
603
- if (version !== exports.BEEF_V1 && version !== exports.BEEF_V2) {
604
- throw new Error(`Serialized BEEF must start with ${exports.BEEF_V1} or ${exports.BEEF_V2} but starts with ${version}`);
625
+ if (version !== BeefConstants_js_1.BEEF_V1 && version !== BeefConstants_js_1.BEEF_V2) {
626
+ throw new Error(`Serialized BEEF must start with ${BeefConstants_js_1.BEEF_V1} or ${BeefConstants_js_1.BEEF_V2} but starts with ${version}`);
605
627
  }
606
628
  const beef = new Beef(version);
607
629
  const bumpsLength = br.readVarIntNum();
@@ -674,11 +696,37 @@ class Beef {
674
696
  const validTxids = {};
675
697
  // Hashtable of all transaction txids to transaction
676
698
  const txidToTx = {};
677
- // queue of unsorted transactions ...
678
- let queue = [];
679
699
  // sorted transactions: hasProof to with longest dependency chain
680
700
  const result = [];
681
701
  const txidOnly = [];
702
+ // Partition into proven, txidOnly, and remaining queue
703
+ let queue = this.partitionTxs(txidToTx, validTxids, result, txidOnly);
704
+ // Separate queue entries that have missing inputs
705
+ const { txsMissingInputs, missingInputs, remaining } = this.separateMissingInputs(queue, txidToTx);
706
+ queue = remaining;
707
+ // Topological sort of remaining queue
708
+ const txsNotValid = this.topoSort(queue, validTxids, result);
709
+ // New order of txs is unsortable (missing inputs or depends on missing inputs), txidOnly, sorted (so newest sorted is last)
710
+ this.txs = txsMissingInputs
711
+ .concat(txsNotValid)
712
+ .concat(txidOnly)
713
+ .concat(result);
714
+ this.needsSort = false;
715
+ this.invalidateSerializationCaches();
716
+ return {
717
+ missingInputs: Object.keys(missingInputs),
718
+ notValid: txsNotValid.map((tx) => tx.txid),
719
+ valid: Object.keys(validTxids),
720
+ withMissingInputs: txsMissingInputs.map((tx) => tx.txid),
721
+ txidOnly: txidOnly.map((tx) => tx.txid)
722
+ };
723
+ }
724
+ /**
725
+ * Partition txs into proven (result), txidOnly, and a queue of the rest.
726
+ * Populates txidToTx and validTxids as side-effects.
727
+ */
728
+ partitionTxs(txidToTx, validTxids, result, txidOnly) {
729
+ const queue = [];
682
730
  for (const tx of this.txs) {
683
731
  txidToTx[tx.txid] = tx;
684
732
  tx.isValid = tx.hasProof;
@@ -694,21 +742,19 @@ class Beef {
694
742
  queue.push(tx);
695
743
  }
696
744
  }
697
- // Hashtable of unknown input txids used to fund transactions without their own proof.
745
+ return queue;
746
+ }
747
+ /**
748
+ * Separate queue entries that have at least one input txid not present in txidToTx.
749
+ */
750
+ separateMissingInputs(candidates, txidToTx) {
698
751
  const missingInputs = {};
699
- // transactions with one or more missing inputs
700
752
  const txsMissingInputs = [];
701
- const possiblyMissingInputs = queue;
702
- queue = [];
703
- // all tx are isValid false, hasProof false.
704
- // if isTxidOnly then has inputTxids
705
- for (const tx of possiblyMissingInputs) {
753
+ const remaining = [];
754
+ for (const tx of candidates) {
706
755
  let hasMissingInput = false;
707
- // For all the unproven transactions,
708
- // link their inputs that exist in this beef,
709
- // make a note of missing inputs.
710
756
  for (const inputTxid of tx.inputTxids) {
711
- if (txidToTx[inputTxid] === undefined) { // Explicitly check for undefined
757
+ if (txidToTx[inputTxid] === undefined) {
712
758
  missingInputs[inputTxid] = true;
713
759
  hasMissingInput = true;
714
760
  }
@@ -717,15 +763,18 @@ class Beef {
717
763
  txsMissingInputs.push(tx);
718
764
  }
719
765
  else {
720
- queue.push(tx);
766
+ remaining.push(tx);
721
767
  }
722
768
  }
723
- // As long as we have unsorted transactions...
769
+ return { txsMissingInputs, missingInputs, remaining };
770
+ }
771
+ /**
772
+ * Topologically sort queue into result; return anything that cannot be sorted.
773
+ */
774
+ topoSort(queue, validTxids, result) {
724
775
  while (queue.length > 0) {
725
776
  const oldQueue = queue;
726
777
  queue = [];
727
- // all tx are isValid false, hasProof false.
728
- // if isTxidOnly then has inputTxids
729
778
  for (const tx of oldQueue) {
730
779
  if (tx.inputTxids.every((txid) => validTxids[txid])) {
731
780
  validTxids[tx.txid] = true;
@@ -735,26 +784,10 @@ class Beef {
735
784
  queue.push(tx);
736
785
  }
737
786
  }
738
- if (oldQueue.length === queue.length) {
787
+ if (oldQueue.length === queue.length)
739
788
  break;
740
- }
741
789
  }
742
- // transactions that don't have proofs and don't chain to proofs
743
- const txsNotValid = queue;
744
- // New order of txs is unsortable (missing inputs or depends on missing inputs), txidOnly, sorted (so newest sorted is last)
745
- this.txs = txsMissingInputs
746
- .concat(txsNotValid)
747
- .concat(txidOnly)
748
- .concat(result);
749
- this.needsSort = false;
750
- this.invalidateSerializationCaches();
751
- return {
752
- missingInputs: Object.keys(missingInputs),
753
- notValid: txsNotValid.map((tx) => tx.txid),
754
- valid: Object.keys(validTxids),
755
- withMissingInputs: txsMissingInputs.map((tx) => tx.txid),
756
- txidOnly: txidOnly.map((tx) => tx.txid)
757
- };
790
+ return queue;
758
791
  }
759
792
  /**
760
793
  * @returns a shallow copy of this beef
@@ -775,6 +808,14 @@ class Beef {
775
808
  * @param knownTxids
776
809
  */
777
810
  trimKnownTxids(knownTxids) {
811
+ let mutated = this.removeKnownTxidOnlyTxs(knownTxids);
812
+ mutated = this.reindexBumps() || mutated;
813
+ if (mutated) {
814
+ this.markMutated(true);
815
+ }
816
+ }
817
+ /** Remove txidOnly entries that appear in knownTxids; return true if any were removed. */
818
+ removeKnownTxidOnlyTxs(knownTxids) {
778
819
  let mutated = false;
779
820
  for (let i = 0; i < this.txs.length;) {
780
821
  const tx = this.txs[i];
@@ -787,41 +828,43 @@ class Beef {
787
828
  i++;
788
829
  }
789
830
  }
790
- // Trim unreferenced bumps after removing known txids
831
+ return mutated;
832
+ }
833
+ /**
834
+ * Remove bumps that are no longer referenced by any tx and update bumpIndex references.
835
+ * Returns true if any bumps were removed.
836
+ */
837
+ reindexBumps() {
791
838
  const referencedBumpIndices = new Set();
792
839
  for (const tx of this.txs) {
793
840
  if (tx.bumpIndex !== undefined) {
794
841
  referencedBumpIndices.add(tx.bumpIndex);
795
842
  }
796
843
  }
797
- // Check if there are any unreferenced bumps to remove
798
- if (referencedBumpIndices.size < this.bumps.length) {
799
- // Build mapping of old indices to new indices after removal
800
- const indexMap = new Map();
801
- let newIndex = 0;
802
- for (let i = 0; i < this.bumps.length; i++) {
803
- if (referencedBumpIndices.has(i)) {
804
- indexMap.set(i, newIndex);
805
- newIndex++;
806
- }
807
- }
808
- // Remove unreferenced bumps
809
- this.bumps = this.bumps.filter((_, i) => referencedBumpIndices.has(i));
810
- // Update all transaction bumpIndex references
811
- for (const tx of this.txs) {
812
- if (tx.bumpIndex !== undefined) {
813
- const newIndex = indexMap.get(tx.bumpIndex);
814
- if (newIndex === undefined) {
815
- throw new Error(`Internal error: bumpIndex ${tx.bumpIndex} not found in indexMap`);
816
- }
817
- tx.bumpIndex = newIndex;
818
- }
844
+ if (referencedBumpIndices.size >= this.bumps.length)
845
+ return false;
846
+ // Build mapping of old indices to new indices after removal
847
+ const indexMap = new Map();
848
+ let newIndex = 0;
849
+ for (let i = 0; i < this.bumps.length; i++) {
850
+ if (referencedBumpIndices.has(i)) {
851
+ indexMap.set(i, newIndex);
852
+ newIndex++;
819
853
  }
820
- mutated = true;
821
854
  }
822
- if (mutated) {
823
- this.markMutated(true);
855
+ // Remove unreferenced bumps
856
+ this.bumps = this.bumps.filter((_, i) => referencedBumpIndices.has(i));
857
+ // Update all transaction bumpIndex references
858
+ for (const tx of this.txs) {
859
+ if (tx.bumpIndex === undefined)
860
+ continue;
861
+ const mapped = indexMap.get(tx.bumpIndex);
862
+ if (mapped === undefined) {
863
+ throw new Error(`Internal error: bumpIndex ${tx.bumpIndex} not found in indexMap`);
864
+ }
865
+ tx.bumpIndex = mapped;
824
866
  }
867
+ return true;
825
868
  }
826
869
  /**
827
870
  * @returns array of transaction txids that either have a proof or whose inputs chain back to a proven transaction.
@@ -840,7 +883,7 @@ class Beef {
840
883
  for (const b of this.bumps) {
841
884
  i++;
842
885
  log += ` BUMP ${i}\n block: ${b.blockHeight}\n txids: [\n${b.path[0]
843
- .filter((n) => n.txid === true) // ✅ Explicitly check if txid is `true`
886
+ .filter((n) => n.txid === true)
844
887
  .map((n) => ` '${n.hash ?? ''}'`)
845
888
  .join(',\n')}\n ]\n`;
846
889
  }
@@ -855,7 +898,7 @@ class Beef {
855
898
  log += ' txidOnly\n';
856
899
  }
857
900
  else {
858
- log += ` rawTx length=${t.rawTx?.length ?? 0}\n`; // ✅ Fix applied here
901
+ log += ` rawTx length=${t.rawTx?.length ?? 0}\n`;
859
902
  }
860
903
  if (t.inputTxids.length > 0) {
861
904
  log += ` inputs: [\n${t.inputTxids
@@ -870,25 +913,29 @@ class Beef {
870
913
  * leaves that can be computed from row zero.
871
914
  */
872
915
  addComputedLeaves() {
873
- const hash = (m) => (0, utils_js_1.toHex)((0, Hash_js_1.hash256)((0, utils_js_1.toArray)(m, 'hex').reverse()).reverse());
874
- for (const bump of this.bumps) { // ✅ Use `this` instead of `beef`
916
+ for (const bump of this.bumps) {
875
917
  for (let row = 1; row < bump.path.length; row++) {
876
- for (const leafL of bump.path[row - 1]) {
877
- if (typeof leafL.hash === 'string' && (leafL.offset & 1) === 0) {
878
- const leafR = bump.path[row - 1].find((l) => l.offset === leafL.offset + 1);
879
- const offsetOnRow = leafL.offset >> 1;
880
- if (leafR !== undefined &&
881
- typeof leafR.hash === 'string' &&
882
- bump.path[row].every((l) => l.offset !== offsetOnRow)) {
883
- // Computable leaf is missing... add it.
884
- bump.path[row].push({
885
- offset: offsetOnRow,
886
- // String concatenation puts the right leaf on the left of the left leaf hash
887
- hash: hash(leafR.hash + leafL.hash)
888
- });
889
- }
890
- }
891
- }
918
+ this.addComputedLeavesForRow(bump, row);
919
+ }
920
+ }
921
+ }
922
+ /** Add any missing computable leaf at `row` derived from two known leaves at `row - 1`. */
923
+ addComputedLeavesForRow(bump, row) {
924
+ const hashPair = (m) => (0, utils_js_1.toHex)((0, Hash_js_1.hash256)((0, utils_js_1.toArray)(m, 'hex').reverse()).reverse());
925
+ for (const leafL of bump.path[row - 1]) {
926
+ if (typeof leafL.hash !== 'string' || (leafL.offset & 1) !== 0)
927
+ continue;
928
+ const leafR = bump.path[row - 1].find((l) => l.offset === leafL.offset + 1);
929
+ if (leafR === undefined || typeof leafR.hash !== 'string')
930
+ continue;
931
+ const offsetOnRow = leafL.offset >> 1;
932
+ if (bump.path[row].every((l) => l.offset !== offsetOnRow)) {
933
+ // Computable leaf is missing... add it.
934
+ bump.path[row].push({
935
+ offset: offsetOnRow,
936
+ // String concatenation puts the right leaf on the left of the left leaf hash
937
+ hash: hashPair(leafR.hash + leafL.hash)
938
+ });
892
939
  }
893
940
  }
894
941
  }