@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
@@ -12,11 +12,11 @@ export default class XDMSubstrate extends InvokableWalletBase {
12
12
 
13
13
  constructor(domain: string = '*') {
14
14
  super()
15
- if (typeof window !== 'object') {
16
- throw new Error('The XDM substrate requires a global window object.')
15
+ if (typeof globalThis.window !== 'object') {
16
+ throw new TypeError('The XDM substrate requires a global window object.')
17
17
  }
18
- if (typeof window.postMessage !== 'function') {
19
- throw new Error(
18
+ if (typeof globalThis.window.postMessage !== 'function') {
19
+ throw new TypeError(
20
20
  'The window object does not seem to support postMessage calls.'
21
21
  )
22
22
  }
@@ -7,6 +7,10 @@ import { WERR_REVIEW_ACTIONS } from '../../WERR_REVIEW_ACTIONS'
7
7
  // Helpers
8
8
  // ---------------------------------------------------------------------------
9
9
 
10
+ const TEST_ORIGINATOR = 'example.com'
11
+ const TEST_ORIGIN_HEADER = 'http://example.com'
12
+ const BASE_URL = 'http://localhost:3321'
13
+
10
14
  /** Build a minimal fetch mock that resolves with a JSON-shaped Response. */
11
15
  function makeFetch(
12
16
  body: unknown,
@@ -24,19 +28,23 @@ function makeNetworkErrorFetch(message = 'Network failure'): jest.Mock {
24
28
  return jest.fn().mockRejectedValue(new Error(message))
25
29
  }
26
30
 
31
+ function makeClient(mockFetch: jest.Mock): HTTPWalletJSON {
32
+ return new HTTPWalletJSON(TEST_ORIGINATOR, BASE_URL, mockFetch as unknown as typeof fetch)
33
+ }
34
+
27
35
  // ---------------------------------------------------------------------------
28
36
  // Constructor
29
37
  // ---------------------------------------------------------------------------
30
38
 
31
39
  describe('HTTPWalletJSON – constructor', () => {
32
40
  it('stores the provided baseUrl', () => {
33
- const client = new HTTPWalletJSON('example.com', 'http://my-server:9000')
41
+ const client = new HTTPWalletJSON(TEST_ORIGINATOR, 'http://my-server:9000')
34
42
  expect(client.baseUrl).toBe('http://my-server:9000')
35
43
  })
36
44
 
37
45
  it('uses http://localhost:3321 as the default baseUrl', () => {
38
- const client = new HTTPWalletJSON('example.com')
39
- expect(client.baseUrl).toBe('http://localhost:3321')
46
+ const client = new HTTPWalletJSON(TEST_ORIGINATOR)
47
+ expect(client.baseUrl).toBe(BASE_URL)
40
48
  })
41
49
 
42
50
  it('stores the originator', () => {
@@ -44,14 +52,9 @@ describe('HTTPWalletJSON – constructor', () => {
44
52
  expect(client.originator).toBe('wallet.example.com')
45
53
  })
46
54
 
47
- it('accepts undefined originator', () => {
48
- const client = new HTTPWalletJSON(undefined)
49
- expect(client.originator).toBeUndefined()
50
- })
51
-
52
55
  it('stores the custom httpClient', () => {
53
56
  const mockFetch = jest.fn()
54
- const client = new HTTPWalletJSON('example.com', 'http://localhost:3321', mockFetch as unknown as typeof fetch)
57
+ const client = new HTTPWalletJSON(TEST_ORIGINATOR, BASE_URL, mockFetch as unknown as typeof fetch)
55
58
  expect(client.httpClient).toBe(mockFetch)
56
59
  })
57
60
  })
@@ -63,13 +66,13 @@ describe('HTTPWalletJSON – constructor', () => {
63
66
  describe('HTTPWalletJSON – api() successful responses', () => {
64
67
  it('POSTs to the correct URL and returns the parsed body', async () => {
65
68
  const mockFetch = makeFetch({ version: '1.0.0.0.0.0.0' })
66
- const client = new HTTPWalletJSON(undefined, 'http://localhost:3321', mockFetch as unknown as typeof fetch)
69
+ const client = makeClient(mockFetch)
67
70
 
68
71
  const result = await client.getVersion({})
69
72
 
70
73
  expect(mockFetch).toHaveBeenCalledTimes(1)
71
74
  const [url, init] = mockFetch.mock.calls[0] as [string, RequestInit]
72
- expect(url).toBe('http://localhost:3321/getVersion')
75
+ expect(url).toBe(`${BASE_URL}/getVersion`)
73
76
  expect(init.method).toBe('POST')
74
77
  expect(JSON.parse(init.body as string)).toEqual({})
75
78
  expect(result).toEqual({ version: '1.0.0.0.0.0.0' })
@@ -77,7 +80,7 @@ describe('HTTPWalletJSON – api() successful responses', () => {
77
80
 
78
81
  it('sets Accept and Content-Type headers', async () => {
79
82
  const mockFetch = makeFetch({ height: 800000 })
80
- const client = new HTTPWalletJSON(undefined, 'http://localhost:3321', mockFetch as unknown as typeof fetch)
83
+ const client = makeClient(mockFetch)
81
84
 
82
85
  await client.getHeight({})
83
86
 
@@ -85,17 +88,27 @@ describe('HTTPWalletJSON – api() successful responses', () => {
85
88
  const headers = init.headers as Record<string, string>
86
89
  expect(headers['Accept']).toBe('application/json')
87
90
  expect(headers['Content-Type']).toBe('application/json')
91
+ expect(headers['Origin']).toBe(TEST_ORIGIN_HEADER)
92
+ expect(headers['Originator']).toBe(TEST_ORIGIN_HEADER)
88
93
  })
89
94
 
90
95
  it('serialises args as JSON in the request body', async () => {
91
96
  const mockFetch = makeFetch({ actions: [], totalActions: 0 })
92
- const client = new HTTPWalletJSON(undefined, 'http://localhost:3321', mockFetch as unknown as typeof fetch)
97
+ const client = makeClient(mockFetch)
93
98
 
94
99
  await client.listActions({ labels: ['test-label'] })
95
100
 
96
101
  const [, init] = mockFetch.mock.calls[0] as [string, RequestInit]
97
102
  expect(JSON.parse(init.body as string)).toEqual({ labels: ['test-label'] })
98
103
  })
104
+
105
+ it('throws before making a request in Node when originator is missing', async () => {
106
+ const mockFetch = makeFetch({ version: '1.0.0.0.0.0.0' })
107
+ const client = new HTTPWalletJSON(undefined, BASE_URL, mockFetch as unknown as typeof fetch)
108
+
109
+ await expect(client.getVersion({})).rejects.toThrow('HTTPWalletJSON: originator is required')
110
+ expect(mockFetch).not.toHaveBeenCalled()
111
+ })
99
112
  })
100
113
 
101
114
  // ---------------------------------------------------------------------------
@@ -111,7 +124,7 @@ describe('HTTPWalletJSON – api() error responses', () => {
111
124
  message: 'The description parameter must be at least 5 length.',
112
125
  }
113
126
  const mockFetch = makeFetch(errorBody, { ok: false, status: 400 })
114
- const client = new HTTPWalletJSON(undefined, 'http://localhost:3321', mockFetch as unknown as typeof fetch)
127
+ const client = makeClient(mockFetch)
115
128
 
116
129
  await expect(client.createAction({ description: 'x' })).rejects.toThrow(WERR_INVALID_PARAMETER)
117
130
 
@@ -133,7 +146,7 @@ describe('HTTPWalletJSON – api() error responses', () => {
133
146
  message: 'Custom server message for lockingScript.',
134
147
  }
135
148
  const mockFetch = makeFetch(errorBody, { ok: false, status: 400 })
136
- const client = new HTTPWalletJSON(undefined, 'http://localhost:3321', mockFetch as unknown as typeof fetch)
149
+ const client = makeClient(mockFetch)
137
150
 
138
151
  try {
139
152
  await client.createAction({ description: 'hello world' })
@@ -151,7 +164,7 @@ describe('HTTPWalletJSON – api() error responses', () => {
151
164
  moreSatoshisNeeded: 2000,
152
165
  }
153
166
  const mockFetch = makeFetch(errorBody, { ok: false, status: 400 })
154
- const client = new HTTPWalletJSON(undefined, 'http://localhost:3321', mockFetch as unknown as typeof fetch)
167
+ const client = makeClient(mockFetch)
155
168
 
156
169
  await expect(client.createAction({ description: 'hello world' })).rejects.toThrow(WERR_INSUFFICIENT_FUNDS)
157
170
 
@@ -174,7 +187,7 @@ describe('HTTPWalletJSON – api() error responses', () => {
174
187
  txid: 'abc123',
175
188
  }
176
189
  const mockFetch = makeFetch(errorBody, { ok: false, status: 400 })
177
- const client = new HTTPWalletJSON(undefined, 'http://localhost:3321', mockFetch as unknown as typeof fetch)
190
+ const client = makeClient(mockFetch)
178
191
 
179
192
  await expect(client.createAction({ description: 'hello world' })).rejects.toThrow(WERR_REVIEW_ACTIONS)
180
193
 
@@ -190,7 +203,7 @@ describe('HTTPWalletJSON – api() error responses', () => {
190
203
 
191
204
  it('throws a generic Error when the server returns a non-400 error status', async () => {
192
205
  const mockFetch = makeFetch({ message: 'Internal Server Error' }, { ok: false, status: 500 })
193
- const client = new HTTPWalletJSON(undefined, 'http://localhost:3321', mockFetch as unknown as typeof fetch)
206
+ const client = makeClient(mockFetch)
194
207
 
195
208
  await expect(client.getVersion({})).rejects.toThrow(Error)
196
209
 
@@ -205,7 +218,7 @@ describe('HTTPWalletJSON – api() error responses', () => {
205
218
 
206
219
  it('falls back to "HTTP Client error <status>" when the 500 body has no message', async () => {
207
220
  const mockFetch = makeFetch({}, { ok: false, status: 503 })
208
- const client = new HTTPWalletJSON(undefined, 'http://localhost:3321', mockFetch as unknown as typeof fetch)
221
+ const client = makeClient(mockFetch)
209
222
 
210
223
  try {
211
224
  await client.getVersion({})
@@ -219,7 +232,7 @@ describe('HTTPWalletJSON – api() error responses', () => {
219
232
  // code 99 is unrecognised – falls through to the generic error path
220
233
  const errorBody = { isError: true, code: 99, message: 'Unknown problem' }
221
234
  const mockFetch = makeFetch(errorBody, { ok: false, status: 400 })
222
- const client = new HTTPWalletJSON(undefined, 'http://localhost:3321', mockFetch as unknown as typeof fetch)
235
+ const client = makeClient(mockFetch)
223
236
 
224
237
  await expect(client.getVersion({})).rejects.toThrow(Error)
225
238
  })
@@ -232,7 +245,7 @@ describe('HTTPWalletJSON – api() error responses', () => {
232
245
  describe('HTTPWalletJSON – network errors', () => {
233
246
  it('propagates a fetch rejection as-is', async () => {
234
247
  const mockFetch = makeNetworkErrorFetch('Failed to fetch')
235
- const client = new HTTPWalletJSON(undefined, 'http://localhost:3321', mockFetch as unknown as typeof fetch)
248
+ const client = makeClient(mockFetch)
236
249
 
237
250
  await expect(client.getVersion({})).rejects.toThrow('Failed to fetch')
238
251
  })
@@ -248,7 +261,7 @@ describe('HTTPWalletJSON – method routing', () => {
248
261
 
249
262
  beforeEach(() => {
250
263
  mockFetch = makeFetch({})
251
- client = new HTTPWalletJSON(undefined, 'http://localhost:3321', mockFetch as unknown as typeof fetch)
264
+ client = makeClient(mockFetch)
252
265
  })
253
266
 
254
267
  const expectCallName = async (method: () => Promise<unknown>, expectedPath: string) => {
@@ -470,7 +483,7 @@ describe('HTTPWalletJSON – response body passthrough', () => {
470
483
  it('returns the exact JSON body from a successful getVersion call', async () => {
471
484
  const expected = { version: '1.0.0.0.0.0.0' }
472
485
  const mockFetch = makeFetch(expected)
473
- const client = new HTTPWalletJSON(undefined, 'http://localhost:3321', mockFetch as unknown as typeof fetch)
486
+ const client = makeClient(mockFetch)
474
487
 
475
488
  const result = await client.getVersion({})
476
489
  expect(result).toEqual(expected)
@@ -479,7 +492,7 @@ describe('HTTPWalletJSON – response body passthrough', () => {
479
492
  it('returns the exact JSON body from a successful listActions call', async () => {
480
493
  const expected = { totalActions: 2, actions: [{ txid: 'aa', status: 'completed' }] }
481
494
  const mockFetch = makeFetch(expected)
482
- const client = new HTTPWalletJSON(undefined, 'http://localhost:3321', mockFetch as unknown as typeof fetch)
495
+ const client = makeClient(mockFetch)
483
496
 
484
497
  const result = await client.listActions({ labels: [] })
485
498
  expect(result).toEqual(expected)
@@ -488,7 +501,7 @@ describe('HTTPWalletJSON – response body passthrough', () => {
488
501
  it('returns the exact JSON body from a successful getNetwork call', async () => {
489
502
  const expected = { network: 'mainnet' as const }
490
503
  const mockFetch = makeFetch(expected)
491
- const client = new HTTPWalletJSON(undefined, 'http://localhost:3321', mockFetch as unknown as typeof fetch)
504
+ const client = makeClient(mockFetch)
492
505
 
493
506
  const result = await client.getNetwork({})
494
507
  expect(result).toEqual(expected)
@@ -0,0 +1,174 @@
1
+ import ReactNativeWebView from '../ReactNativeWebView'
2
+ import { WalletError } from '../../WalletError'
3
+ import * as Utils from '../../../primitives/utils'
4
+
5
+ describe('ReactNativeWebView', () => {
6
+ let originalWindow: typeof global.window
7
+ let addEventListenerMock: jest.Mock
8
+ let removeEventListenerMock: jest.Mock
9
+ let postMessageMock: jest.Mock
10
+
11
+ beforeEach(() => {
12
+ originalWindow = global.window
13
+ addEventListenerMock = jest.fn()
14
+ removeEventListenerMock = jest.fn()
15
+ postMessageMock = jest.fn()
16
+
17
+ global.window = {
18
+ ReactNativeWebView: {
19
+ postMessage: postMessageMock
20
+ },
21
+ addEventListener: addEventListenerMock,
22
+ removeEventListener: removeEventListenerMock
23
+ } as unknown as Window & typeof globalThis
24
+ })
25
+
26
+ afterEach(() => {
27
+ global.window = originalWindow
28
+ jest.restoreAllMocks()
29
+ })
30
+
31
+ const getMessageListener = (): ((event: { data: string }) => void) => {
32
+ const call = addEventListenerMock.mock.calls.at(-1)
33
+ if (call == null) {
34
+ throw new Error('No message listener registered.')
35
+ }
36
+ return call[1] as (event: { data: string }) => void
37
+ }
38
+
39
+ const dispatchMessage = (data: unknown): void => {
40
+ getMessageListener()({ data: JSON.stringify(data) })
41
+ }
42
+
43
+ describe('constructor', () => {
44
+ it('throws if window is not available', () => {
45
+ ;(global as any).window = undefined
46
+
47
+ expect(() => new ReactNativeWebView()).toThrow(
48
+ 'The XDM substrate requires a global window object.'
49
+ )
50
+ })
51
+
52
+ it('throws if ReactNativeWebView is not bound to window', () => {
53
+ delete (global.window as any).ReactNativeWebView
54
+
55
+ expect(() => new ReactNativeWebView()).toThrow(
56
+ 'The window object does not have a ReactNativeWebView property.'
57
+ )
58
+ })
59
+
60
+ it('throws if ReactNativeWebView does not support postMessage', () => {
61
+ ;(global.window as any).ReactNativeWebView.postMessage = undefined
62
+
63
+ expect(() => new ReactNativeWebView()).toThrow(
64
+ 'The window.ReactNativeWebView property does not seem to support postMessage calls.'
65
+ )
66
+ })
67
+ })
68
+
69
+ describe('invoke', () => {
70
+ it('posts an invocation message to the React Native bridge', () => {
71
+ jest.spyOn(Utils, 'toBase64').mockReturnValue('request-id')
72
+ const substrate = new ReactNativeWebView()
73
+
74
+ void substrate.invoke('getVersion', {})
75
+
76
+ expect(addEventListenerMock).toHaveBeenCalledWith('message', expect.any(Function))
77
+ expect(postMessageMock).toHaveBeenCalledWith(
78
+ JSON.stringify({
79
+ type: 'CWI',
80
+ isInvocation: true,
81
+ id: 'request-id',
82
+ call: 'getVersion',
83
+ args: {}
84
+ })
85
+ )
86
+ })
87
+
88
+ it('resolves the result from a matching response', async () => {
89
+ jest.spyOn(Utils, 'toBase64').mockReturnValue('request-id')
90
+ const substrate = new ReactNativeWebView()
91
+
92
+ const promise = substrate.invoke('getVersion', {})
93
+ dispatchMessage({
94
+ type: 'CWI',
95
+ isInvocation: false,
96
+ id: 'request-id',
97
+ status: 'success',
98
+ result: { version: '1.0.0' }
99
+ })
100
+
101
+ await expect(promise).resolves.toEqual({ version: '1.0.0' })
102
+ expect(removeEventListenerMock).toHaveBeenCalledWith('message', expect.any(Function))
103
+ })
104
+
105
+ it('rejects matching error responses as WalletError', async () => {
106
+ jest.spyOn(Utils, 'toBase64').mockReturnValue('request-id')
107
+ const substrate = new ReactNativeWebView()
108
+
109
+ const promise = substrate.invoke('createAction', { description: 'Test action' })
110
+ dispatchMessage({
111
+ type: 'CWI',
112
+ isInvocation: false,
113
+ id: 'request-id',
114
+ status: 'error',
115
+ description: 'Action was rejected',
116
+ code: 123
117
+ })
118
+
119
+ await expect(promise).rejects.toThrow(WalletError)
120
+ await expect(promise).rejects.toThrow('Action was rejected')
121
+ await promise.catch((err) => {
122
+ expect(err.code).toBe(123)
123
+ })
124
+ expect(removeEventListenerMock).toHaveBeenCalledWith('message', expect.any(Function))
125
+ })
126
+
127
+ it('ignores unrelated response messages', async () => {
128
+ jest.spyOn(Utils, 'toBase64').mockReturnValue('request-id')
129
+ const substrate = new ReactNativeWebView()
130
+ const promise = substrate.invoke('getVersion', {})
131
+ let settled = false
132
+ promise.then(
133
+ () => { settled = true },
134
+ () => { settled = true }
135
+ )
136
+
137
+ dispatchMessage({
138
+ type: 'other',
139
+ isInvocation: false,
140
+ id: 'request-id',
141
+ status: 'success',
142
+ result: {}
143
+ })
144
+ dispatchMessage({
145
+ type: 'CWI',
146
+ isInvocation: false,
147
+ id: 'other-id',
148
+ status: 'success',
149
+ result: {}
150
+ })
151
+ dispatchMessage({
152
+ type: 'CWI',
153
+ isInvocation: true,
154
+ id: 'request-id',
155
+ status: 'success',
156
+ result: {}
157
+ })
158
+
159
+ await new Promise((resolve) => setTimeout(resolve, 1))
160
+ expect(settled).toBe(false)
161
+ expect(removeEventListenerMock).not.toHaveBeenCalled()
162
+
163
+ dispatchMessage({
164
+ type: 'CWI',
165
+ isInvocation: false,
166
+ id: 'request-id',
167
+ status: 'success',
168
+ result: { version: '1.0.0' }
169
+ })
170
+
171
+ await expect(promise).resolves.toEqual({ version: '1.0.0' })
172
+ })
173
+ })
174
+ })
@@ -0,0 +1,256 @@
1
+ import WindowCWISubstrate from '../window.CWI'
2
+
3
+ const originator = 'example.com'
4
+
5
+ const methodCases = [
6
+ {
7
+ methodName: 'createAction',
8
+ args: { description: 'Test description', inputs: [], outputs: [] },
9
+ result: { txid: 'abc123' }
10
+ },
11
+ {
12
+ methodName: 'signAction',
13
+ args: { spends: {}, reference: 'someReference' },
14
+ result: { txid: 'abc123' }
15
+ },
16
+ {
17
+ methodName: 'abortAction',
18
+ args: { reference: 'someReference' },
19
+ result: { aborted: true }
20
+ },
21
+ {
22
+ methodName: 'listActions',
23
+ args: { labels: [] },
24
+ result: { totalActions: 0, actions: [] }
25
+ },
26
+ {
27
+ methodName: 'internalizeAction',
28
+ args: { tx: 'someTx', outputs: [], description: 'Test description' },
29
+ result: { accepted: true }
30
+ },
31
+ {
32
+ methodName: 'listOutputs',
33
+ args: { basket: 'someBasket' },
34
+ result: { totalOutputs: 0, outputs: [] }
35
+ },
36
+ {
37
+ methodName: 'relinquishOutput',
38
+ args: { basket: 'someBasket', output: 'someOutput' },
39
+ result: { relinquished: true }
40
+ },
41
+ {
42
+ methodName: 'getPublicKey',
43
+ args: { identityKey: true },
44
+ result: { publicKey: 'somePubKey' }
45
+ },
46
+ {
47
+ methodName: 'revealCounterpartyKeyLinkage',
48
+ args: { counterparty: 'someCounterparty', verifier: 'someVerifier' },
49
+ result: {
50
+ prover: 'someProver',
51
+ verifier: 'someVerifier',
52
+ counterparty: 'someCounterparty',
53
+ revelationTime: 'someTime',
54
+ encryptedLinkage: [],
55
+ encryptedLinkageProof: []
56
+ }
57
+ },
58
+ {
59
+ methodName: 'revealSpecificKeyLinkage',
60
+ args: {
61
+ counterparty: 'someCounterparty',
62
+ verifier: 'someVerifier',
63
+ protocolID: [0, 'someProtocol'],
64
+ keyID: 'someKeyID'
65
+ },
66
+ result: {
67
+ prover: 'someProver',
68
+ verifier: 'someVerifier',
69
+ counterparty: 'someCounterparty',
70
+ protocolID: [0, 'someProtocol'],
71
+ keyID: 'someKeyID',
72
+ encryptedLinkage: [],
73
+ encryptedLinkageProof: [],
74
+ proofType: 1
75
+ }
76
+ },
77
+ {
78
+ methodName: 'encrypt',
79
+ args: { plaintext: [], protocolID: [0, 'someProtocol'], keyID: 'someKeyID' },
80
+ result: { ciphertext: [] }
81
+ },
82
+ {
83
+ methodName: 'decrypt',
84
+ args: { ciphertext: [], protocolID: [0, 'someProtocol'], keyID: 'someKeyID' },
85
+ result: { plaintext: [] }
86
+ },
87
+ {
88
+ methodName: 'createHmac',
89
+ args: { data: [], protocolID: [0, 'someProtocol'], keyID: 'someKeyID' },
90
+ result: { hmac: [] }
91
+ },
92
+ {
93
+ methodName: 'verifyHmac',
94
+ args: { data: [], hmac: [], protocolID: [0, 'someProtocol'], keyID: 'someKeyID' },
95
+ result: { valid: true }
96
+ },
97
+ {
98
+ methodName: 'createSignature',
99
+ args: { data: [], protocolID: [0, 'someProtocol'], keyID: 'someKeyID' },
100
+ result: { signature: [] }
101
+ },
102
+ {
103
+ methodName: 'verifySignature',
104
+ args: { data: [], signature: [], protocolID: [0, 'someProtocol'], keyID: 'someKeyID' },
105
+ result: { valid: true }
106
+ },
107
+ {
108
+ methodName: 'acquireCertificate',
109
+ args: {
110
+ type: 'someType',
111
+ subject: 'someSubject',
112
+ serialNumber: 'someSerialNumber',
113
+ revocationOutpoint: 'someOutpoint',
114
+ signature: 'someSignature',
115
+ fields: {},
116
+ certifier: 'someCertifier',
117
+ keyringRevealer: 'certifier',
118
+ keyringForSubject: {},
119
+ acquisitionProtocol: 'direct'
120
+ },
121
+ result: {
122
+ type: 'someType',
123
+ subject: 'someSubject',
124
+ serialNumber: 'someSerialNumber',
125
+ certifier: 'someCertifier',
126
+ revocationOutpoint: 'someOutpoint',
127
+ signature: 'someSignature',
128
+ fields: {}
129
+ }
130
+ },
131
+ {
132
+ methodName: 'listCertificates',
133
+ args: { certifiers: [], types: [] },
134
+ result: { totalCertificates: 0, certificates: [] }
135
+ },
136
+ {
137
+ methodName: 'proveCertificate',
138
+ args: {
139
+ certificate: {
140
+ type: 'someType',
141
+ subject: 'someSubject',
142
+ serialNumber: 'someSerialNumber',
143
+ certifier: 'someCertifier',
144
+ revocationOutpoint: 'someOutpoint',
145
+ signature: 'someSignature',
146
+ fields: {}
147
+ },
148
+ fieldsToReveal: [],
149
+ verifier: 'someVerifier'
150
+ },
151
+ result: { keyringForVerifier: {} }
152
+ },
153
+ {
154
+ methodName: 'relinquishCertificate',
155
+ args: { type: 'someType', serialNumber: 'someSerialNumber', certifier: 'someCertifier' },
156
+ result: { relinquished: true }
157
+ },
158
+ {
159
+ methodName: 'discoverByIdentityKey',
160
+ args: { identityKey: 'someIdentityKey' },
161
+ result: { totalCertificates: 0, certificates: [] }
162
+ },
163
+ {
164
+ methodName: 'discoverByAttributes',
165
+ args: { attributes: {} },
166
+ result: { totalCertificates: 0, certificates: [] }
167
+ },
168
+ {
169
+ methodName: 'isAuthenticated',
170
+ args: {},
171
+ result: { authenticated: true }
172
+ },
173
+ {
174
+ methodName: 'waitForAuthentication',
175
+ args: {},
176
+ result: { authenticated: true }
177
+ },
178
+ {
179
+ methodName: 'getHeight',
180
+ args: {},
181
+ result: { height: 1000 }
182
+ },
183
+ {
184
+ methodName: 'getHeaderForHeight',
185
+ args: { height: 1000 },
186
+ result: { header: 'someHeader' }
187
+ },
188
+ {
189
+ methodName: 'getNetwork',
190
+ args: {},
191
+ result: { network: 'mainnet' }
192
+ },
193
+ {
194
+ methodName: 'getVersion',
195
+ args: {},
196
+ result: { version: '1.0.0' }
197
+ }
198
+ ]
199
+
200
+ describe('WindowCWISubstrate', () => {
201
+ let originalWindow: typeof global.window
202
+ let mockCWI: Record<string, jest.Mock>
203
+
204
+ beforeEach(() => {
205
+ originalWindow = global.window
206
+ mockCWI = Object.fromEntries(
207
+ methodCases.map(({ methodName, result }) => [
208
+ methodName,
209
+ jest.fn().mockResolvedValue(result)
210
+ ])
211
+ )
212
+ global.window = {
213
+ CWI: mockCWI
214
+ } as unknown as Window & typeof globalThis
215
+ })
216
+
217
+ afterEach(() => {
218
+ global.window = originalWindow
219
+ jest.restoreAllMocks()
220
+ })
221
+
222
+ it('throws if window is not available', () => {
223
+ ;(global as any).window = undefined
224
+
225
+ expect(() => new WindowCWISubstrate()).toThrow(
226
+ 'The window.CWI substrate requires a global window object.'
227
+ )
228
+ })
229
+
230
+ it('throws if window.CWI is not bound', () => {
231
+ delete (global.window as any).CWI
232
+
233
+ expect(() => new WindowCWISubstrate()).toThrow(
234
+ 'The window.CWI interface does not appear to be bound to the window object.'
235
+ )
236
+ })
237
+
238
+ it('binds the CWI object that exists at construction time', async () => {
239
+ const substrate = new WindowCWISubstrate()
240
+ const replacement = {
241
+ getVersion: jest.fn().mockResolvedValue({ version: '2.0.0' })
242
+ }
243
+ ;(global.window as any).CWI = replacement
244
+
245
+ await expect(substrate.getVersion({})).resolves.toEqual({ version: '1.0.0' })
246
+ expect(mockCWI.getVersion).toHaveBeenCalledWith({}, undefined)
247
+ expect(replacement.getVersion).not.toHaveBeenCalled()
248
+ })
249
+
250
+ test.each(methodCases)('delegates $methodName to window.CWI', async ({ methodName, args, result }) => {
251
+ const substrate = new WindowCWISubstrate()
252
+
253
+ await expect((substrate as any)[methodName](args, originator)).resolves.toEqual(result)
254
+ expect(mockCWI[methodName]).toHaveBeenCalledWith(args, originator)
255
+ })
256
+ })