@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
@@ -3,6 +3,16 @@ import Certificate from '../../auth/certificates/Certificate.js';
3
3
  import * as Utils from '../../primitives/utils.js';
4
4
  import calls from './WalletWireCalls.js';
5
5
  import { WalletError } from '../WalletError.js';
6
+ const ACTION_STATUS_MAP = {
7
+ 1: 'completed',
8
+ 2: 'unprocessed',
9
+ 3: 'sending',
10
+ 4: 'unproven',
11
+ 5: 'unsigned',
12
+ 6: 'nosend',
13
+ 7: 'nonfinal',
14
+ 8: 'failed'
15
+ };
6
16
  /**
7
17
  * A way to make remote calls to a wallet over a wallet wire.
8
18
  */
@@ -45,259 +55,69 @@ export default class WalletWireTransceiver {
45
55
  async createAction(args, originator) {
46
56
  const paramWriter = new Utils.Writer();
47
57
  // Serialize description
48
- const descriptionBytes = Utils.toArray(args.description, 'utf8');
49
- paramWriter.writeVarIntNum(descriptionBytes.length);
50
- paramWriter.write(descriptionBytes);
58
+ this.writeUTF8(paramWriter, args.description);
51
59
  // input BEEF
52
- if (args.inputBEEF != null) {
60
+ if (args.inputBEEF == null) {
61
+ paramWriter.writeVarIntNum(-1);
62
+ }
63
+ else {
53
64
  paramWriter.writeVarIntNum(args.inputBEEF.length);
54
65
  paramWriter.write(args.inputBEEF);
55
66
  }
56
- else {
67
+ // Serialize inputs
68
+ if (args.inputs == null) {
57
69
  paramWriter.writeVarIntNum(-1);
58
70
  }
59
- // Serialize inputs
60
- if (args.inputs != null) {
71
+ else {
61
72
  paramWriter.writeVarIntNum(args.inputs.length);
62
73
  for (const input of args.inputs) {
63
- // outpoint
64
- paramWriter.write(this.encodeOutpoint(input.outpoint));
65
- // unlockingScript / unlockingScriptLength
66
- if (input.unlockingScript != null && input.unlockingScript !== '') {
67
- const unlockingScriptBytes = Utils.toArray(input.unlockingScript, 'hex');
68
- paramWriter.writeVarIntNum(unlockingScriptBytes.length);
69
- paramWriter.write(unlockingScriptBytes);
70
- }
71
- else {
72
- paramWriter.writeVarIntNum(-1);
73
- paramWriter.writeVarIntNum(input.unlockingScriptLength ?? 0);
74
- }
75
- // inputDescription
76
- const inputDescriptionBytes = Utils.toArray(input.inputDescription, 'utf8');
77
- paramWriter.writeVarIntNum(inputDescriptionBytes.length);
78
- paramWriter.write(inputDescriptionBytes);
79
- // sequenceNumber
80
- if (typeof input.sequenceNumber === 'number') {
81
- paramWriter.writeVarIntNum(input.sequenceNumber);
82
- }
83
- else {
84
- paramWriter.writeVarIntNum(-1);
85
- }
74
+ this.serializeCreateActionInput(paramWriter, input);
86
75
  }
87
76
  }
88
- else {
77
+ // Serialize outputs
78
+ if (args.outputs == null) {
89
79
  paramWriter.writeVarIntNum(-1);
90
80
  }
91
- // Serialize outputs
92
- if (args.outputs != null) {
81
+ else {
93
82
  paramWriter.writeVarIntNum(args.outputs.length);
94
83
  for (const output of args.outputs) {
95
- // lockingScript
96
- const lockingScriptBytes = Utils.toArray(output.lockingScript, 'hex');
97
- paramWriter.writeVarIntNum(lockingScriptBytes.length);
98
- paramWriter.write(lockingScriptBytes);
99
- // satoshis
100
- paramWriter.writeVarIntNum(output.satoshis);
101
- // outputDescription
102
- const outputDescriptionBytes = Utils.toArray(output.outputDescription, 'utf8');
103
- paramWriter.writeVarIntNum(outputDescriptionBytes.length);
104
- paramWriter.write(outputDescriptionBytes);
105
- // basket
106
- if (output.basket != null && output.basket !== '') {
107
- const basketBytes = Utils.toArray(output.basket, 'utf8');
108
- paramWriter.writeVarIntNum(basketBytes.length);
109
- paramWriter.write(basketBytes);
110
- }
111
- else {
112
- paramWriter.writeVarIntNum(-1);
113
- }
114
- // customInstructions
115
- if (output.customInstructions != null && output.customInstructions !== '') {
116
- const customInstructionsBytes = Utils.toArray(output.customInstructions, 'utf8');
117
- paramWriter.writeVarIntNum(customInstructionsBytes.length);
118
- paramWriter.write(customInstructionsBytes);
119
- }
120
- else {
121
- paramWriter.writeVarIntNum(-1);
122
- }
123
- // tags
124
- if (output.tags != null) {
125
- paramWriter.writeVarIntNum(output.tags.length);
126
- for (const tag of output.tags) {
127
- const tagBytes = Utils.toArray(tag, 'utf8');
128
- paramWriter.writeVarIntNum(tagBytes.length);
129
- paramWriter.write(tagBytes);
130
- }
131
- }
132
- else {
133
- paramWriter.writeVarIntNum(-1);
134
- }
84
+ this.serializeCreateActionOutput(paramWriter, output);
135
85
  }
136
86
  }
137
- else {
138
- paramWriter.writeVarIntNum(-1);
139
- }
140
- // Serialize lockTime
141
- if (typeof args.lockTime === 'number') {
142
- paramWriter.writeVarIntNum(args.lockTime);
143
- }
144
- else {
145
- paramWriter.writeVarIntNum(-1);
146
- }
147
- // Serialize version
148
- if (typeof args.version === 'number') {
149
- paramWriter.writeVarIntNum(args.version);
150
- }
151
- else {
152
- paramWriter.writeVarIntNum(-1);
153
- }
87
+ // Serialize lockTime, version
88
+ this.writeOptionalVarInt(paramWriter, args.lockTime);
89
+ this.writeOptionalVarInt(paramWriter, args.version);
154
90
  // Serialize labels
155
- if (args.labels != null) {
156
- paramWriter.writeVarIntNum(args.labels.length);
157
- for (const label of args.labels) {
158
- const labelBytes = Utils.toArray(label, 'utf8');
159
- paramWriter.writeVarIntNum(labelBytes.length);
160
- paramWriter.write(labelBytes);
161
- }
162
- }
163
- else {
164
- paramWriter.writeVarIntNum(-1);
165
- }
91
+ this.writeUTF8Array(paramWriter, args.labels);
166
92
  // Serialize options
167
- if (args.options != null) {
168
- paramWriter.writeInt8(1); // options present
169
- // signAndProcess
170
- if (typeof args.options.signAndProcess === 'boolean') {
171
- paramWriter.writeInt8(args.options.signAndProcess ? 1 : 0);
172
- }
173
- else {
174
- paramWriter.writeInt8(-1);
175
- }
176
- // acceptDelayedBroadcast
177
- if (typeof args.options.acceptDelayedBroadcast === 'boolean') {
178
- paramWriter.writeInt8(args.options.acceptDelayedBroadcast ? 1 : 0);
179
- }
180
- else {
181
- paramWriter.writeInt8(-1);
182
- }
183
- // trustSelf
184
- if (args.options.trustSelf === 'known') {
185
- paramWriter.writeInt8(1);
186
- }
187
- else {
188
- paramWriter.writeInt8(-1);
189
- }
190
- // knownTxids
191
- if (args.options.knownTxids != null) {
192
- paramWriter.writeVarIntNum(args.options.knownTxids.length);
193
- for (const txid of args.options.knownTxids) {
194
- const txidBytes = Utils.toArray(txid, 'hex');
195
- paramWriter.write(txidBytes);
196
- }
197
- }
198
- else {
199
- paramWriter.writeVarIntNum(-1);
200
- }
201
- // returnTXIDOnly
202
- if (typeof args.options.returnTXIDOnly === 'boolean') {
203
- paramWriter.writeInt8(args.options.returnTXIDOnly ? 1 : 0);
204
- }
205
- else {
206
- paramWriter.writeInt8(-1);
207
- }
208
- // noSend
209
- if (typeof args.options.noSend === 'boolean') {
210
- paramWriter.writeInt8(args.options.noSend ? 1 : 0);
211
- }
212
- else {
213
- paramWriter.writeInt8(-1);
214
- }
215
- // noSendChange
216
- if (args.options.noSendChange != null) {
217
- paramWriter.writeVarIntNum(args.options.noSendChange.length);
218
- for (const outpoint of args.options.noSendChange) {
219
- paramWriter.write(this.encodeOutpoint(outpoint));
220
- }
221
- }
222
- else {
223
- paramWriter.writeVarIntNum(-1);
224
- }
225
- // sendWith
226
- if (args.options.sendWith != null) {
227
- paramWriter.writeVarIntNum(args.options.sendWith.length);
228
- for (const txid of args.options.sendWith) {
229
- const txidBytes = Utils.toArray(txid, 'hex');
230
- paramWriter.write(txidBytes);
231
- }
232
- }
233
- else {
234
- paramWriter.writeVarIntNum(-1);
235
- }
236
- // randomizeOutputs
237
- if (typeof args.options.randomizeOutputs === 'boolean') {
238
- paramWriter.writeInt8(args.options.randomizeOutputs ? 1 : 0);
239
- }
240
- else {
241
- paramWriter.writeInt8(-1);
242
- }
243
- }
244
- else {
245
- paramWriter.writeInt8(0); // options not present
246
- }
93
+ this.serializeCreateActionOptions(paramWriter, args.options);
247
94
  // Transmit and parse response
248
95
  const result = await this.transmit('createAction', originator, paramWriter.toArray());
96
+ return this.parseCreateActionResult(result);
97
+ }
98
+ parseCreateActionResult(result) {
249
99
  const resultReader = new Utils.Reader(result);
250
100
  const response = {};
251
- // Parse txid
252
- const txidFlag = resultReader.readInt8();
253
- if (txidFlag === 1) {
254
- const txidBytes = resultReader.read(32);
255
- response.txid = Utils.toHex(txidBytes);
256
- }
257
- // Parse tx
258
- const txFlag = resultReader.readInt8();
259
- if (txFlag === 1) {
260
- const txLength = resultReader.readVarIntNum();
261
- response.tx = resultReader.read(txLength);
262
- }
263
- // Parse noSendChange
101
+ if (resultReader.readInt8() === 1) {
102
+ response.txid = Utils.toHex(resultReader.read(32));
103
+ }
104
+ if (resultReader.readInt8() === 1) {
105
+ response.tx = resultReader.read(resultReader.readVarIntNum());
106
+ }
264
107
  const noSendChangeLength = resultReader.readVarIntNum();
265
108
  if (noSendChangeLength >= 0) {
266
109
  response.noSendChange = [];
267
110
  for (let i = 0; i < noSendChangeLength; i++) {
268
- const outpoint = this.readOutpoint(resultReader);
269
- response.noSendChange.push(outpoint);
270
- }
271
- }
272
- // Parse sendWithResults
273
- const sendWithResultsLength = resultReader.readVarIntNum();
274
- if (sendWithResultsLength >= 0) {
275
- response.sendWithResults = [];
276
- for (let i = 0; i < sendWithResultsLength; i++) {
277
- const txidBytes = resultReader.read(32);
278
- const txid = Utils.toHex(txidBytes);
279
- const statusCode = resultReader.readInt8();
280
- let status = 'unproven';
281
- if (statusCode === 1)
282
- status = 'unproven';
283
- else if (statusCode === 2)
284
- status = 'sending';
285
- else if (statusCode === 3)
286
- status = 'failed';
287
- response.sendWithResults.push({ txid, status });
111
+ response.noSendChange.push(this.readOutpoint(resultReader));
288
112
  }
289
113
  }
290
- // Parse signableTransaction
291
- const signableTransactionFlag = resultReader.readInt8();
292
- if (signableTransactionFlag === 1) {
293
- const txLength = resultReader.readVarIntNum();
294
- const tx = resultReader.read(txLength);
295
- const referenceLength = resultReader.readVarIntNum();
296
- const referenceBytes = resultReader.read(referenceLength);
297
- response.signableTransaction = {
298
- tx,
299
- reference: Utils.toBase64(referenceBytes)
300
- };
114
+ const sendWithResults = this.readSendWithResults(resultReader);
115
+ if (sendWithResults != null)
116
+ response.sendWithResults = sendWithResults;
117
+ if (resultReader.readInt8() === 1) {
118
+ const tx = resultReader.read(resultReader.readVarIntNum());
119
+ const referenceBytes = resultReader.read(resultReader.readVarIntNum());
120
+ response.signableTransaction = { tx, reference: Utils.toBase64(referenceBytes) };
301
121
  }
302
122
  return response;
303
123
  }
@@ -309,95 +129,30 @@ export default class WalletWireTransceiver {
309
129
  for (const index of spendIndexes) {
310
130
  paramWriter.writeVarIntNum(Number(index));
311
131
  const spend = args.spends[Number(index)];
312
- // unlockingScript
313
132
  const unlockingScriptBytes = Utils.toArray(spend.unlockingScript, 'hex');
314
133
  paramWriter.writeVarIntNum(unlockingScriptBytes.length);
315
134
  paramWriter.write(unlockingScriptBytes);
316
- // sequenceNumber
317
- if (typeof spend.sequenceNumber === 'number') {
318
- paramWriter.writeVarIntNum(spend.sequenceNumber);
319
- }
320
- else {
321
- paramWriter.writeVarIntNum(-1);
322
- }
135
+ this.writeOptionalVarInt(paramWriter, spend.sequenceNumber);
323
136
  }
324
137
  // Serialize reference
325
138
  const referenceBytes = Utils.toArray(args.reference, 'base64');
326
139
  paramWriter.writeVarIntNum(referenceBytes.length);
327
140
  paramWriter.write(referenceBytes);
328
141
  // Serialize options
329
- if (args.options != null) {
330
- paramWriter.writeInt8(1); // options present
331
- // acceptDelayedBroadcast
332
- if (typeof args.options.acceptDelayedBroadcast === 'boolean') {
333
- paramWriter.writeInt8(args.options.acceptDelayedBroadcast ? 1 : 0);
334
- }
335
- else {
336
- paramWriter.writeInt8(-1);
337
- }
338
- // returnTXIDOnly
339
- if (typeof args.options.returnTXIDOnly === 'boolean') {
340
- paramWriter.writeInt8(args.options.returnTXIDOnly ? 1 : 0);
341
- }
342
- else {
343
- paramWriter.writeInt8(-1);
344
- }
345
- // noSend
346
- if (typeof args.options.noSend === 'boolean') {
347
- paramWriter.writeInt8(args.options.noSend ? 1 : 0);
348
- }
349
- else {
350
- paramWriter.writeInt8(-1);
351
- }
352
- // sendWith
353
- if (args.options.sendWith != null) {
354
- paramWriter.writeVarIntNum(args.options.sendWith.length);
355
- for (const txid of args.options.sendWith) {
356
- const txidBytes = Utils.toArray(txid, 'hex');
357
- paramWriter.write(txidBytes);
358
- }
359
- }
360
- else {
361
- paramWriter.writeVarIntNum(-1);
362
- }
363
- }
364
- else {
365
- paramWriter.writeInt8(0); // options not present
366
- }
142
+ this.serializeSignActionOptions(paramWriter, args.options);
367
143
  // Transmit and parse response
368
144
  const result = await this.transmit('signAction', originator, paramWriter.toArray());
369
145
  const resultReader = new Utils.Reader(result);
370
146
  const response = {};
371
- // Parse txid
372
- const txidFlag = resultReader.readInt8();
373
- if (txidFlag === 1) {
374
- const txidBytes = resultReader.read(32);
375
- response.txid = Utils.toHex(txidBytes);
376
- }
377
- // Parse tx
378
- const txFlag = resultReader.readInt8();
379
- if (txFlag === 1) {
380
- const txLength = resultReader.readVarIntNum();
381
- response.tx = resultReader.read(txLength);
382
- }
383
- // Parse sendWithResults
384
- const sendWithResultsLength = resultReader.readVarIntNum();
385
- if (sendWithResultsLength >= 0) {
386
- response.sendWithResults = [];
387
- for (let i = 0; i < sendWithResultsLength; i++) {
388
- const txidBytes = resultReader.read(32);
389
- const txid = Utils.toHex(txidBytes);
390
- const statusCode = resultReader.readInt8();
391
- let status = 'unproven';
392
- if (statusCode === 1)
393
- status = 'unproven';
394
- else if (statusCode === 2)
395
- status = 'sending';
396
- else if (statusCode === 3)
397
- status = 'failed';
398
- response.sendWithResults.push({ txid, status });
399
- }
147
+ if (resultReader.readInt8() === 1) {
148
+ response.txid = Utils.toHex(resultReader.read(32));
400
149
  }
150
+ if (resultReader.readInt8() === 1) {
151
+ response.tx = resultReader.read(resultReader.readVarIntNum());
152
+ }
153
+ const sendWithResults = this.readSendWithResults(resultReader);
154
+ if (sendWithResults != null)
155
+ response.sendWithResults = sendWithResults;
401
156
  return response;
402
157
  }
403
158
  async abortAction(args, originator) {
@@ -406,223 +161,109 @@ export default class WalletWireTransceiver {
406
161
  }
407
162
  async listActions(args, originator) {
408
163
  const paramWriter = new Utils.Writer();
409
- // Serialize labels
164
+ // Serialize labels (always-present array, no -1 sentinel)
410
165
  paramWriter.writeVarIntNum(args.labels.length);
411
166
  for (const label of args.labels) {
412
- const labelBytes = Utils.toArray(label, 'utf8');
413
- paramWriter.writeVarIntNum(labelBytes.length);
414
- paramWriter.write(labelBytes);
167
+ this.writeUTF8(paramWriter, label);
415
168
  }
416
169
  // Serialize labelQueryMode
417
- if (args.labelQueryMode === 'any') {
170
+ if (args.labelQueryMode === 'any')
418
171
  paramWriter.writeInt8(1);
419
- }
420
- else if (args.labelQueryMode === 'all') {
172
+ else if (args.labelQueryMode === 'all')
421
173
  paramWriter.writeInt8(2);
422
- }
423
- else {
174
+ else
424
175
  paramWriter.writeInt8(-1);
425
- }
426
176
  // Serialize include options
427
- const includeOptions = [
177
+ for (const option of [
428
178
  args.includeLabels,
429
179
  args.includeInputs,
430
180
  args.includeInputSourceLockingScripts,
431
181
  args.includeInputUnlockingScripts,
432
182
  args.includeOutputs,
433
183
  args.includeOutputLockingScripts
434
- ];
435
- for (const option of includeOptions) {
436
- if (typeof option === 'boolean') {
437
- paramWriter.writeInt8(option ? 1 : 0);
438
- }
439
- else {
440
- paramWriter.writeInt8(-1);
441
- }
442
- }
443
- // Serialize limit and offset
444
- if (typeof args.limit === 'number') {
445
- paramWriter.writeVarIntNum(args.limit);
446
- }
447
- else {
448
- paramWriter.writeVarIntNum(-1);
184
+ ]) {
185
+ this.writeOptionalBool(paramWriter, option);
449
186
  }
450
- if (typeof args.offset === 'number') {
451
- paramWriter.writeVarIntNum(args.offset);
452
- }
453
- else {
454
- paramWriter.writeVarIntNum(-1);
455
- }
456
- // Serialize seekPermission
457
- paramWriter.writeInt8(typeof args.seekPermission === 'boolean'
458
- ? args.seekPermission
459
- ? 1
460
- : 0
461
- : -1);
462
- // Transmit and parse response
187
+ this.writeOptionalVarInt(paramWriter, args.limit);
188
+ this.writeOptionalVarInt(paramWriter, args.offset);
189
+ this.writeOptionalBool(paramWriter, args.seekPermission);
463
190
  const result = await this.transmit('listActions', originator, paramWriter.toArray());
464
191
  const resultReader = new Utils.Reader(result);
465
192
  const totalActions = resultReader.readVarIntNum();
466
193
  const actions = [];
467
194
  for (let i = 0; i < totalActions; i++) {
468
- // Parse action fields
469
- const txidBytes = resultReader.read(32);
470
- const txid = Utils.toHex(txidBytes);
471
- const satoshis = resultReader.readVarIntNum();
472
- const statusCode = resultReader.readInt8();
473
- let status;
474
- switch (statusCode) {
475
- case 1:
476
- status = 'completed';
477
- break;
478
- case 2:
479
- status = 'unprocessed';
480
- break;
481
- case 3:
482
- status = 'sending';
483
- break;
484
- case 4:
485
- status = 'unproven';
486
- break;
487
- case 5:
488
- status = 'unsigned';
489
- break;
490
- case 6:
491
- status = 'nosend';
492
- break;
493
- case 7:
494
- status = 'nonfinal';
495
- break;
496
- case 8:
497
- status = 'failed';
498
- break;
499
- default:
500
- throw new Error(`Unknown status code: ${statusCode}`);
195
+ actions.push(this.parseAction(resultReader));
196
+ }
197
+ return { totalActions, actions };
198
+ }
199
+ parseActionStatus(code) {
200
+ const status = ACTION_STATUS_MAP[code];
201
+ if (status == null)
202
+ throw new Error(`Unknown status code: ${code}`);
203
+ return status;
204
+ }
205
+ parseAction(reader) {
206
+ const txid = Utils.toHex(reader.read(32));
207
+ const satoshis = reader.readVarIntNum();
208
+ const status = this.parseActionStatus(reader.readInt8());
209
+ const isOutgoing = reader.readInt8() === 1;
210
+ const description = Utils.toUTF8(reader.read(reader.readVarIntNum()));
211
+ const action = { txid, satoshis, status, isOutgoing, description, version: 0, lockTime: 0 };
212
+ const labelsLen = reader.readVarIntNum();
213
+ if (labelsLen >= 0) {
214
+ action.labels = [];
215
+ for (let j = 0; j < labelsLen; j++) {
216
+ action.labels.push(Utils.toUTF8(reader.read(reader.readVarIntNum())));
501
217
  }
502
- const isOutgoing = resultReader.readInt8() === 1;
503
- const descriptionLength = resultReader.readVarIntNum();
504
- const descriptionBytes = resultReader.read(descriptionLength);
505
- const description = Utils.toUTF8(descriptionBytes);
506
- const action = {
507
- txid,
508
- satoshis,
509
- status,
510
- isOutgoing,
511
- description,
512
- version: 0,
513
- lockTime: 0
514
- };
515
- // Parse labels
516
- const labelsLength = resultReader.readVarIntNum();
517
- if (labelsLength >= 0) {
518
- action.labels = [];
519
- for (let j = 0; j < labelsLength; j++) {
520
- const labelLength = resultReader.readVarIntNum();
521
- const labelBytes = resultReader.read(labelLength);
522
- action.labels.push(Utils.toUTF8(labelBytes));
523
- }
218
+ }
219
+ action.version = reader.readVarIntNum();
220
+ action.lockTime = reader.readVarIntNum();
221
+ const inputsLen = reader.readVarIntNum();
222
+ if (inputsLen >= 0) {
223
+ action.inputs = [];
224
+ for (let k = 0; k < inputsLen; k++) {
225
+ action.inputs.push(this.parseActionInput(reader));
524
226
  }
525
- // Parse version and lockTime
526
- action.version = resultReader.readVarIntNum();
527
- action.lockTime = resultReader.readVarIntNum();
528
- // Parse inputs
529
- const inputsLength = resultReader.readVarIntNum();
530
- if (inputsLength >= 0) {
531
- action.inputs = [];
532
- for (let k = 0; k < inputsLength; k++) {
533
- const sourceOutpoint = this.readOutpoint(resultReader);
534
- const sourceSatoshis = resultReader.readVarIntNum();
535
- // sourceLockingScript
536
- const sourceLockingScriptLength = resultReader.readVarIntNum();
537
- let sourceLockingScript;
538
- if (sourceLockingScriptLength >= 0) {
539
- const sourceLockingScriptBytes = resultReader.read(sourceLockingScriptLength);
540
- sourceLockingScript = Utils.toHex(sourceLockingScriptBytes);
541
- }
542
- // unlockingScript
543
- const unlockingScriptLength = resultReader.readVarIntNum();
544
- let unlockingScript;
545
- if (unlockingScriptLength >= 0) {
546
- const unlockingScriptBytes = resultReader.read(unlockingScriptLength);
547
- unlockingScript = Utils.toHex(unlockingScriptBytes);
548
- }
549
- // inputDescription
550
- const inputDescriptionLength = resultReader.readVarIntNum();
551
- const inputDescriptionBytes = resultReader.read(inputDescriptionLength);
552
- const inputDescription = Utils.toUTF8(inputDescriptionBytes);
553
- // sequenceNumber
554
- const sequenceNumber = resultReader.readVarIntNum();
555
- action.inputs.push({
556
- sourceOutpoint,
557
- sourceSatoshis,
558
- sourceLockingScript,
559
- unlockingScript,
560
- inputDescription,
561
- sequenceNumber
562
- });
563
- }
227
+ }
228
+ const outputsLen = reader.readVarIntNum();
229
+ if (outputsLen >= 0) {
230
+ action.outputs = [];
231
+ for (let l = 0; l < outputsLen; l++) {
232
+ action.outputs.push(this.parseActionOutput(reader));
564
233
  }
565
- // Parse outputs
566
- const outputsLength = resultReader.readVarIntNum();
567
- if (outputsLength >= 0) {
568
- action.outputs = [];
569
- for (let l = 0; l < outputsLength; l++) {
570
- const outputIndex = resultReader.readVarIntNum();
571
- const satoshis = resultReader.readVarIntNum();
572
- // lockingScript
573
- const lockingScriptLength = resultReader.readVarIntNum();
574
- let lockingScript;
575
- if (lockingScriptLength >= 0) {
576
- const lockingScriptBytes = resultReader.read(lockingScriptLength);
577
- lockingScript = Utils.toHex(lockingScriptBytes);
578
- }
579
- const spendable = resultReader.readInt8() === 1;
580
- // outputDescription
581
- const outputDescriptionLength = resultReader.readVarIntNum();
582
- const outputDescriptionBytes = resultReader.read(outputDescriptionLength);
583
- const outputDescription = Utils.toUTF8(outputDescriptionBytes);
584
- // basket
585
- const basketLength = resultReader.readVarIntNum();
586
- let basket;
587
- if (basketLength >= 0) {
588
- const basketBytes = resultReader.read(basketLength);
589
- basket = Utils.toUTF8(basketBytes);
590
- }
591
- // tags
592
- const tagsLength = resultReader.readVarIntNum();
593
- const tags = [];
594
- if (tagsLength >= 0) {
595
- for (let m = 0; m < tagsLength; m++) {
596
- const tagLength = resultReader.readVarIntNum();
597
- const tagBytes = resultReader.read(tagLength);
598
- tags.push(Utils.toUTF8(tagBytes));
599
- }
600
- }
601
- // customInstructions
602
- const customInstructionsLength = resultReader.readVarIntNum();
603
- let customInstructions;
604
- if (customInstructionsLength >= 0) {
605
- const customInstructionsBytes = resultReader.read(customInstructionsLength);
606
- customInstructions = Utils.toUTF8(customInstructionsBytes);
607
- }
608
- action.outputs.push({
609
- outputIndex,
610
- satoshis,
611
- lockingScript,
612
- spendable,
613
- outputDescription,
614
- basket,
615
- tags,
616
- customInstructions
617
- });
618
- }
234
+ }
235
+ return action;
236
+ }
237
+ parseActionInput(reader) {
238
+ const sourceOutpoint = this.readOutpoint(reader);
239
+ const sourceSatoshis = reader.readVarIntNum();
240
+ const srcLockLen = reader.readVarIntNum();
241
+ const sourceLockingScript = srcLockLen >= 0 ? Utils.toHex(reader.read(srcLockLen)) : undefined;
242
+ const unlockLen = reader.readVarIntNum();
243
+ const unlockingScript = unlockLen >= 0 ? Utils.toHex(reader.read(unlockLen)) : undefined;
244
+ const inputDescription = Utils.toUTF8(reader.read(reader.readVarIntNum()));
245
+ const sequenceNumber = reader.readVarIntNum();
246
+ return { sourceOutpoint, sourceSatoshis, sourceLockingScript, unlockingScript, inputDescription, sequenceNumber };
247
+ }
248
+ parseActionOutput(reader) {
249
+ const outputIndex = reader.readVarIntNum();
250
+ const satoshis = reader.readVarIntNum();
251
+ const lockLen = reader.readVarIntNum();
252
+ const lockingScript = lockLen >= 0 ? Utils.toHex(reader.read(lockLen)) : undefined;
253
+ const spendable = reader.readInt8() === 1;
254
+ const outputDescription = Utils.toUTF8(reader.read(reader.readVarIntNum()));
255
+ const basketLen = reader.readVarIntNum();
256
+ const basket = basketLen >= 0 ? Utils.toUTF8(reader.read(basketLen)) : undefined;
257
+ const tagsLen = reader.readVarIntNum();
258
+ const tags = [];
259
+ if (tagsLen >= 0) {
260
+ for (let m = 0; m < tagsLen; m++) {
261
+ tags.push(Utils.toUTF8(reader.read(reader.readVarIntNum())));
619
262
  }
620
- actions.push(action);
621
263
  }
622
- return {
623
- totalActions,
624
- actions
625
- };
264
+ const custLen = reader.readVarIntNum();
265
+ const customInstructions = custLen >= 0 ? Utils.toUTF8(reader.read(custLen)) : undefined;
266
+ return { outputIndex, satoshis, lockingScript, spendable, outputDescription, basket: basket, tags, customInstructions };
626
267
  }
627
268
  async internalizeAction(args, originator) {
628
269
  const paramWriter = new Utils.Writer();
@@ -630,189 +271,119 @@ export default class WalletWireTransceiver {
630
271
  paramWriter.write(args.tx);
631
272
  paramWriter.writeVarIntNum(args.outputs.length);
632
273
  for (const out of args.outputs) {
633
- paramWriter.writeVarIntNum(out.outputIndex);
634
- if (out.protocol === 'wallet payment') {
635
- if (out.paymentRemittance == null) {
636
- throw new Error('Payment remittance is required for wallet payment');
637
- }
638
- paramWriter.writeUInt8(1);
639
- paramWriter.write(Utils.toArray(out.paymentRemittance.senderIdentityKey, 'hex'));
640
- const derivationPrefixAsArray = Utils.toArray(out.paymentRemittance.derivationPrefix, 'base64');
641
- paramWriter.writeVarIntNum(derivationPrefixAsArray.length);
642
- paramWriter.write(derivationPrefixAsArray);
643
- const derivationSuffixAsArray = Utils.toArray(out.paymentRemittance.derivationSuffix, 'base64');
644
- paramWriter.writeVarIntNum(derivationSuffixAsArray.length);
645
- paramWriter.write(derivationSuffixAsArray);
646
- }
647
- else {
648
- paramWriter.writeUInt8(2);
649
- const basketAsArray = Utils.toArray(out.insertionRemittance?.basket, 'utf8');
650
- paramWriter.writeVarIntNum(basketAsArray.length);
651
- paramWriter.write(basketAsArray);
652
- if (typeof out.insertionRemittance?.customInstructions === 'string' && out.insertionRemittance.customInstructions !== '') {
653
- const customInstructionsAsArray = Utils.toArray(out.insertionRemittance.customInstructions, 'utf8');
654
- paramWriter.writeVarIntNum(customInstructionsAsArray.length);
655
- paramWriter.write(customInstructionsAsArray);
656
- }
657
- else {
658
- paramWriter.writeVarIntNum(-1);
659
- }
660
- if (typeof out.insertionRemittance?.tags === 'object') {
661
- paramWriter.writeVarIntNum(out.insertionRemittance.tags.length);
662
- for (const tag of out.insertionRemittance.tags) {
663
- const tagAsArray = Utils.toArray(tag, 'utf8');
664
- paramWriter.writeVarIntNum(tagAsArray.length);
665
- paramWriter.write(tagAsArray);
666
- }
667
- }
668
- else {
669
- paramWriter.writeVarIntNum(0);
670
- }
671
- }
672
- }
673
- if (typeof args.labels === 'object') {
674
- paramWriter.writeVarIntNum(args.labels.length);
675
- for (const l of args.labels) {
676
- const labelAsArray = Utils.toArray(l, 'utf8');
677
- paramWriter.writeVarIntNum(labelAsArray.length);
678
- paramWriter.write(labelAsArray);
679
- }
680
- }
681
- else {
682
- paramWriter.writeVarIntNum(-1);
274
+ this.serializeInternalizeOutput(paramWriter, out);
683
275
  }
276
+ this.writeUTF8Array(paramWriter, typeof args.labels === 'object' ? args.labels : undefined);
684
277
  const descriptionAsArray = Utils.toArray(args.description);
685
278
  paramWriter.writeVarIntNum(descriptionAsArray.length);
686
279
  paramWriter.write(descriptionAsArray);
687
- // Serialize seekPermission
688
- paramWriter.writeInt8(typeof args.seekPermission === 'boolean'
689
- ? args.seekPermission
690
- ? 1
691
- : 0
692
- : -1);
280
+ this.writeOptionalBool(paramWriter, args.seekPermission);
693
281
  await this.transmit('internalizeAction', originator, paramWriter.toArray());
694
282
  return { accepted: true };
695
283
  }
284
+ serializeInternalizeOutput(writer, out) {
285
+ writer.writeVarIntNum(out.outputIndex);
286
+ if (out.protocol === 'wallet payment') {
287
+ if (out.paymentRemittance == null) {
288
+ throw new Error('Payment remittance is required for wallet payment');
289
+ }
290
+ writer.writeUInt8(1);
291
+ writer.write(Utils.toArray(out.paymentRemittance.senderIdentityKey, 'hex'));
292
+ const prefix = Utils.toArray(out.paymentRemittance.derivationPrefix, 'base64');
293
+ writer.writeVarIntNum(prefix.length);
294
+ writer.write(prefix);
295
+ const suffix = Utils.toArray(out.paymentRemittance.derivationSuffix, 'base64');
296
+ writer.writeVarIntNum(suffix.length);
297
+ writer.write(suffix);
298
+ }
299
+ else {
300
+ writer.writeUInt8(2);
301
+ const basket = Utils.toArray(out.insertionRemittance?.basket, 'utf8');
302
+ writer.writeVarIntNum(basket.length);
303
+ writer.write(basket);
304
+ this.writeOptionalUTF8(writer, out.insertionRemittance?.customInstructions);
305
+ const tags = out.insertionRemittance?.tags;
306
+ if (typeof tags === 'object') {
307
+ writer.writeVarIntNum(tags.length);
308
+ for (const tag of tags) {
309
+ const t = Utils.toArray(tag, 'utf8');
310
+ writer.writeVarIntNum(t.length);
311
+ writer.write(t);
312
+ }
313
+ }
314
+ else {
315
+ writer.writeVarIntNum(0);
316
+ }
317
+ }
318
+ }
696
319
  async listOutputs(args, originator) {
697
320
  const paramWriter = new Utils.Writer();
698
- const basketAsArray = Utils.toArray(args.basket, 'utf8');
699
- paramWriter.writeVarIntNum(basketAsArray.length);
700
- paramWriter.write(basketAsArray);
321
+ this.writeUTF8(paramWriter, args.basket);
701
322
  if (typeof args.tags === 'object') {
702
323
  paramWriter.writeVarIntNum(args.tags.length);
703
324
  for (const tag of args.tags) {
704
- const tagAsArray = Utils.toArray(tag, 'utf8');
705
- paramWriter.writeVarIntNum(tagAsArray.length);
706
- paramWriter.write(tagAsArray);
325
+ this.writeUTF8(paramWriter, tag);
707
326
  }
708
327
  }
709
328
  else {
710
329
  paramWriter.writeVarIntNum(0);
711
330
  }
712
- if (args.tagQueryMode === 'all') {
331
+ if (args.tagQueryMode === 'all')
713
332
  paramWriter.writeInt8(1);
714
- }
715
- else if (args.tagQueryMode === 'any') {
333
+ else if (args.tagQueryMode === 'any')
716
334
  paramWriter.writeInt8(2);
717
- }
718
- else {
335
+ else
719
336
  paramWriter.writeInt8(-1);
720
- }
721
- if (args.include === 'locking scripts') {
337
+ if (args.include === 'locking scripts')
722
338
  paramWriter.writeInt8(1);
723
- }
724
- else if (args.include === 'entire transactions') {
339
+ else if (args.include === 'entire transactions')
725
340
  paramWriter.writeInt8(2);
726
- }
727
- else {
728
- paramWriter.writeInt8(-1);
729
- }
730
- if (typeof args.includeCustomInstructions === 'boolean') {
731
- paramWriter.writeInt8(args.includeCustomInstructions ? 1 : 0);
732
- }
733
- else {
341
+ else
734
342
  paramWriter.writeInt8(-1);
735
- }
736
- if (typeof args.includeTags === 'boolean') {
737
- paramWriter.writeInt8(args.includeTags ? 1 : 0);
738
- }
739
- else {
740
- paramWriter.writeInt8(-1);
741
- }
742
- if (typeof args.includeLabels === 'boolean') {
743
- paramWriter.writeInt8(args.includeLabels ? 1 : 0);
744
- }
745
- else {
746
- paramWriter.writeInt8(-1);
747
- }
748
- if (typeof args.limit === 'number') {
749
- paramWriter.writeVarIntNum(args.limit);
750
- }
751
- else {
752
- paramWriter.writeVarIntNum(-1);
753
- }
754
- if (typeof args.offset === 'number') {
755
- paramWriter.writeVarIntNum(args.offset);
756
- }
757
- else {
758
- paramWriter.writeVarIntNum(-1);
759
- }
760
- // Serialize seekPermission
761
- paramWriter.writeInt8(typeof args.seekPermission === 'boolean'
762
- ? args.seekPermission
763
- ? 1
764
- : 0
765
- : -1);
343
+ this.writeOptionalBool(paramWriter, args.includeCustomInstructions);
344
+ this.writeOptionalBool(paramWriter, args.includeTags);
345
+ this.writeOptionalBool(paramWriter, args.includeLabels);
346
+ this.writeOptionalVarInt(paramWriter, args.limit);
347
+ this.writeOptionalVarInt(paramWriter, args.offset);
348
+ this.writeOptionalBool(paramWriter, args.seekPermission);
766
349
  const result = await this.transmit('listOutputs', originator, paramWriter.toArray());
767
350
  const resultReader = new Utils.Reader(result);
768
351
  const totalOutputs = resultReader.readVarIntNum();
769
352
  const beefLength = resultReader.readVarIntNum();
770
- let BEEF;
771
- if (beefLength >= 0) {
772
- BEEF = resultReader.read(beefLength);
773
- }
353
+ const BEEF = beefLength >= 0 ? resultReader.read(beefLength) : undefined;
774
354
  const outputs = [];
775
355
  for (let i = 0; i < totalOutputs; i++) {
776
- const outpoint = this.readOutpoint(resultReader);
777
- const satoshis = resultReader.readVarIntNum();
778
- const output = {
779
- spendable: true,
780
- outpoint,
781
- satoshis
782
- };
783
- const scriptLength = resultReader.readVarIntNum();
784
- if (scriptLength >= 0) {
785
- output.lockingScript = Utils.toHex(resultReader.read(scriptLength));
786
- }
787
- const customInstructionsLength = resultReader.readVarIntNum();
788
- if (customInstructionsLength >= 0) {
789
- output.customInstructions = Utils.toUTF8(resultReader.read(customInstructionsLength));
790
- }
791
- const tagsLength = resultReader.readVarIntNum();
792
- if (tagsLength !== -1) {
793
- const tags = [];
794
- for (let i = 0; i < tagsLength; i++) {
795
- const tagLength = resultReader.readVarIntNum();
796
- tags.push(Utils.toUTF8(resultReader.read(tagLength)));
797
- }
798
- output.tags = tags;
356
+ outputs.push(this.parseListOutputEntry(resultReader));
357
+ }
358
+ return { totalOutputs, BEEF, outputs };
359
+ }
360
+ parseListOutputEntry(reader) {
361
+ const outpoint = this.readOutpoint(reader);
362
+ const satoshis = reader.readVarIntNum();
363
+ const output = { spendable: true, outpoint, satoshis };
364
+ const scriptLen = reader.readVarIntNum();
365
+ if (scriptLen >= 0)
366
+ output.lockingScript = Utils.toHex(reader.read(scriptLen));
367
+ const custLen = reader.readVarIntNum();
368
+ if (custLen >= 0)
369
+ output.customInstructions = Utils.toUTF8(reader.read(custLen));
370
+ const tagsLen = reader.readVarIntNum();
371
+ if (tagsLen !== -1) {
372
+ const tags = [];
373
+ for (let i = 0; i < tagsLen; i++) {
374
+ tags.push(Utils.toUTF8(reader.read(reader.readVarIntNum())));
799
375
  }
800
- const labelsLength = resultReader.readVarIntNum();
801
- if (labelsLength !== -1) {
802
- const labels = [];
803
- for (let i = 0; i < labelsLength; i++) {
804
- const labelLength = resultReader.readVarIntNum();
805
- labels.push(Utils.toUTF8(resultReader.read(labelLength)));
806
- }
807
- output.labels = labels;
376
+ output.tags = tags;
377
+ }
378
+ const labelsLen = reader.readVarIntNum();
379
+ if (labelsLen !== -1) {
380
+ const labels = [];
381
+ for (let i = 0; i < labelsLen; i++) {
382
+ labels.push(Utils.toUTF8(reader.read(reader.readVarIntNum())));
808
383
  }
809
- outputs.push(output);
384
+ output.labels = labels;
810
385
  }
811
- return {
812
- totalOutputs,
813
- BEEF,
814
- outputs
815
- };
386
+ return output;
816
387
  }
817
388
  async relinquishOutput(args, originator) {
818
389
  const paramWriter = new Utils.Writer();
@@ -838,8 +409,13 @@ export default class WalletWireTransceiver {
838
409
  async getPublicKey(args, originator) {
839
410
  const paramWriter = new Utils.Writer();
840
411
  paramWriter.writeUInt8(args.identityKey ? 1 : 0);
841
- if (!args.identityKey) {
842
- paramWriter.write(this.encodeKeyRelatedParams(args.protocolID ??= [SecurityLevels.Silent, 'default'], args.keyID ??= '', args.counterparty, args.privileged, args.privilegedReason));
412
+ if (args.identityKey) {
413
+ paramWriter.write(this.encodePrivilegedParams(args.privileged, args.privilegedReason));
414
+ }
415
+ else {
416
+ args.protocolID ??= [SecurityLevels.Silent, 'default'];
417
+ args.keyID ??= '';
418
+ paramWriter.write(this.encodeKeyRelatedParams(args.protocolID, args.keyID, args.counterparty, args.privileged, args.privilegedReason));
843
419
  if (typeof args.forSelf === 'boolean') {
844
420
  paramWriter.writeInt8(args.forSelf ? 1 : 0);
845
421
  }
@@ -847,15 +423,8 @@ export default class WalletWireTransceiver {
847
423
  paramWriter.writeInt8(-1);
848
424
  }
849
425
  }
850
- else {
851
- paramWriter.write(this.encodePrivilegedParams(args.privileged, args.privilegedReason));
852
- }
853
426
  // Serialize seekPermission
854
- paramWriter.writeInt8(typeof args.seekPermission === 'boolean'
855
- ? args.seekPermission
856
- ? 1
857
- : 0
858
- : -1);
427
+ this.writeOptionalBool(paramWriter, args.seekPermission);
859
428
  const result = await this.transmit('getPublicKey', originator, paramWriter.toArray());
860
429
  return {
861
430
  publicKey: Utils.toHex(result)
@@ -922,11 +491,7 @@ export default class WalletWireTransceiver {
922
491
  paramWriter.writeVarIntNum(args.plaintext.length);
923
492
  paramWriter.write(args.plaintext);
924
493
  // Serialize seekPermission
925
- paramWriter.writeInt8(typeof args.seekPermission === 'boolean'
926
- ? args.seekPermission
927
- ? 1
928
- : 0
929
- : -1);
494
+ this.writeOptionalBool(paramWriter, args.seekPermission);
930
495
  return {
931
496
  ciphertext: await this.transmit('encrypt', originator, paramWriter.toArray())
932
497
  };
@@ -937,11 +502,7 @@ export default class WalletWireTransceiver {
937
502
  paramWriter.writeVarIntNum(args.ciphertext.length);
938
503
  paramWriter.write(args.ciphertext);
939
504
  // Serialize seekPermission
940
- paramWriter.writeInt8(typeof args.seekPermission === 'boolean'
941
- ? args.seekPermission
942
- ? 1
943
- : 0
944
- : -1);
505
+ this.writeOptionalBool(paramWriter, args.seekPermission);
945
506
  return {
946
507
  plaintext: await this.transmit('decrypt', originator, paramWriter.toArray())
947
508
  };
@@ -952,11 +513,7 @@ export default class WalletWireTransceiver {
952
513
  paramWriter.writeVarIntNum(args.data.length);
953
514
  paramWriter.write(args.data);
954
515
  // Serialize seekPermission
955
- paramWriter.writeInt8(typeof args.seekPermission === 'boolean'
956
- ? args.seekPermission
957
- ? 1
958
- : 0
959
- : -1);
516
+ this.writeOptionalBool(paramWriter, args.seekPermission);
960
517
  return {
961
518
  hmac: await this.transmit('createHmac', originator, paramWriter.toArray())
962
519
  };
@@ -968,11 +525,7 @@ export default class WalletWireTransceiver {
968
525
  paramWriter.writeVarIntNum(args.data.length);
969
526
  paramWriter.write(args.data);
970
527
  // Serialize seekPermission
971
- paramWriter.writeInt8(typeof args.seekPermission === 'boolean'
972
- ? args.seekPermission
973
- ? 1
974
- : 0
975
- : -1);
528
+ this.writeOptionalBool(paramWriter, args.seekPermission);
976
529
  await this.transmit('verifyHmac', originator, paramWriter.toArray());
977
530
  return { valid: true };
978
531
  }
@@ -985,15 +538,12 @@ export default class WalletWireTransceiver {
985
538
  paramWriter.write(args.data);
986
539
  }
987
540
  else {
541
+ args.hashToDirectlySign ??= [];
988
542
  paramWriter.writeUInt8(2);
989
- paramWriter.write(args.hashToDirectlySign ??= []);
543
+ paramWriter.write(args.hashToDirectlySign);
990
544
  }
991
545
  // Serialize seekPermission
992
- paramWriter.writeInt8(typeof args.seekPermission === 'boolean'
993
- ? args.seekPermission
994
- ? 1
995
- : 0
996
- : -1);
546
+ this.writeOptionalBool(paramWriter, args.seekPermission);
997
547
  return {
998
548
  signature: await this.transmit('createSignature', originator, paramWriter.toArray())
999
549
  };
@@ -1019,14 +569,148 @@ export default class WalletWireTransceiver {
1019
569
  paramWriter.write(args.hashToDirectlyVerify ?? []);
1020
570
  }
1021
571
  // Serialize seekPermission
1022
- paramWriter.writeInt8(typeof args.seekPermission === 'boolean'
1023
- ? args.seekPermission
1024
- ? 1
1025
- : 0
1026
- : -1);
572
+ this.writeOptionalBool(paramWriter, args.seekPermission);
1027
573
  await this.transmit('verifySignature', originator, paramWriter.toArray());
1028
574
  return { valid: true };
1029
575
  }
576
+ /** Writes an optional boolean as Int8: 1/0 if present, -1 if absent. */
577
+ writeOptionalBool(writer, val) {
578
+ if (typeof val === 'boolean') {
579
+ writer.writeInt8(val ? 1 : 0);
580
+ }
581
+ else {
582
+ writer.writeInt8(-1);
583
+ }
584
+ }
585
+ /** Writes an optional number as VarInt: the value if present, -1 if absent. */
586
+ writeOptionalVarInt(writer, val) {
587
+ if (typeof val === 'number') {
588
+ writer.writeVarIntNum(val);
589
+ }
590
+ else {
591
+ writer.writeVarIntNum(-1);
592
+ }
593
+ }
594
+ /** Writes a UTF-8 string as (VarInt length, bytes). */
595
+ writeUTF8(writer, val) {
596
+ const bytes = Utils.toArray(val ?? '', 'utf8');
597
+ writer.writeVarIntNum(bytes.length);
598
+ writer.write(bytes);
599
+ }
600
+ /** Writes an optional UTF-8 string: (VarInt length, bytes) if non-empty, -1 if absent/empty. */
601
+ writeOptionalUTF8(writer, val) {
602
+ if (val != null && val !== '') {
603
+ const bytes = Utils.toArray(val, 'utf8');
604
+ writer.writeVarIntNum(bytes.length);
605
+ writer.write(bytes);
606
+ }
607
+ else {
608
+ writer.writeVarIntNum(-1);
609
+ }
610
+ }
611
+ /** Writes an array of UTF-8 strings as (VarInt count, ...items). -1 if null. */
612
+ writeUTF8Array(writer, arr) {
613
+ if (arr == null) {
614
+ writer.writeVarIntNum(-1);
615
+ }
616
+ else {
617
+ writer.writeVarIntNum(arr.length);
618
+ for (const item of arr) {
619
+ const bytes = Utils.toArray(item, 'utf8');
620
+ writer.writeVarIntNum(bytes.length);
621
+ writer.write(bytes);
622
+ }
623
+ }
624
+ }
625
+ /** Writes an array of hex-encoded txids (each 32 bytes) as (VarInt count, ...items). -1 if null. */
626
+ writeTxidArray(writer, arr) {
627
+ if (arr == null) {
628
+ writer.writeVarIntNum(-1);
629
+ }
630
+ else {
631
+ writer.writeVarIntNum(arr.length);
632
+ for (const txid of arr) {
633
+ writer.write(Utils.toArray(txid, 'hex'));
634
+ }
635
+ }
636
+ }
637
+ /** Reads a list of SendWithResults entries from a binary reader. */
638
+ readSendWithResults(reader) {
639
+ const len = reader.readVarIntNum();
640
+ if (len < 0)
641
+ return undefined;
642
+ const results = [];
643
+ for (let i = 0; i < len; i++) {
644
+ const txid = Utils.toHex(reader.read(32));
645
+ const code = reader.readInt8();
646
+ const status = code === 2 ? 'sending' : code === 3 ? 'failed' : 'unproven';
647
+ results.push({ txid, status });
648
+ }
649
+ return results;
650
+ }
651
+ /** Serializes a single createAction input to the writer. */
652
+ serializeCreateActionInput(writer, input) {
653
+ writer.write(this.encodeOutpoint(input.outpoint));
654
+ if (input.unlockingScript != null && input.unlockingScript !== '') {
655
+ const bytes = Utils.toArray(input.unlockingScript, 'hex');
656
+ writer.writeVarIntNum(bytes.length);
657
+ writer.write(bytes);
658
+ }
659
+ else {
660
+ writer.writeVarIntNum(-1);
661
+ writer.writeVarIntNum(input.unlockingScriptLength ?? 0);
662
+ }
663
+ this.writeUTF8(writer, input.inputDescription);
664
+ this.writeOptionalVarInt(writer, input.sequenceNumber);
665
+ }
666
+ /** Serializes a single createAction output to the writer. */
667
+ serializeCreateActionOutput(writer, output) {
668
+ const lockingBytes = Utils.toArray(output.lockingScript, 'hex');
669
+ writer.writeVarIntNum(lockingBytes.length);
670
+ writer.write(lockingBytes);
671
+ writer.writeVarIntNum(output.satoshis);
672
+ this.writeUTF8(writer, output.outputDescription);
673
+ this.writeOptionalUTF8(writer, output.basket);
674
+ this.writeOptionalUTF8(writer, output.customInstructions);
675
+ this.writeUTF8Array(writer, output.tags);
676
+ }
677
+ /** Serializes createAction options to the writer (Int8 presence byte + fields). */
678
+ serializeCreateActionOptions(writer, options) {
679
+ if (options == null) {
680
+ writer.writeInt8(0);
681
+ return;
682
+ }
683
+ writer.writeInt8(1);
684
+ this.writeOptionalBool(writer, options.signAndProcess);
685
+ this.writeOptionalBool(writer, options.acceptDelayedBroadcast);
686
+ writer.writeInt8(options.trustSelf === 'known' ? 1 : -1);
687
+ this.writeTxidArray(writer, options.knownTxids);
688
+ this.writeOptionalBool(writer, options.returnTXIDOnly);
689
+ this.writeOptionalBool(writer, options.noSend);
690
+ if (options.noSendChange == null) {
691
+ writer.writeVarIntNum(-1);
692
+ }
693
+ else {
694
+ writer.writeVarIntNum(options.noSendChange.length);
695
+ for (const outpoint of options.noSendChange) {
696
+ writer.write(this.encodeOutpoint(outpoint));
697
+ }
698
+ }
699
+ this.writeTxidArray(writer, options.sendWith);
700
+ this.writeOptionalBool(writer, options.randomizeOutputs);
701
+ }
702
+ /** Serializes signAction options to the writer (Int8 presence byte + fields). */
703
+ serializeSignActionOptions(writer, options) {
704
+ if (options == null) {
705
+ writer.writeInt8(0);
706
+ return;
707
+ }
708
+ writer.writeInt8(1);
709
+ this.writeOptionalBool(writer, options.acceptDelayedBroadcast);
710
+ this.writeOptionalBool(writer, options.returnTXIDOnly);
711
+ this.writeOptionalBool(writer, options.noSend);
712
+ this.writeTxidArray(writer, options.sendWith);
713
+ }
1030
714
  encodeKeyRelatedParams(protocolID, keyID, counterparty, privileged, privilegedReason) {
1031
715
  const paramWriter = new Utils.Writer();
1032
716
  paramWriter.writeUInt8(protocolID[0]);
@@ -1073,17 +757,17 @@ export default class WalletWireTransceiver {
1073
757
  const signatureAsArray = Utils.toArray(args.signature, 'hex');
1074
758
  paramWriter.writeVarIntNum(signatureAsArray.length);
1075
759
  paramWriter.write(signatureAsArray);
1076
- const keyringRevealerAsArray = args.keyringRevealer !== 'certifier'
1077
- ? Utils.toArray(args.keyringRevealer, 'hex')
1078
- : [11];
760
+ const keyringRevealerAsArray = args.keyringRevealer === 'certifier'
761
+ ? [11]
762
+ : Utils.toArray(args.keyringRevealer, 'hex');
1079
763
  paramWriter.write(keyringRevealerAsArray);
1080
764
  const keyringKeys = Object.keys(args.keyringForSubject ?? {});
1081
765
  paramWriter.writeVarIntNum(keyringKeys.length);
1082
- for (let i = 0; i < keyringKeys.length; i++) {
1083
- const keyringKeysAsArray = Utils.toArray(keyringKeys[i], 'utf8');
766
+ for (const key of keyringKeys) {
767
+ const keyringKeysAsArray = Utils.toArray(key, 'utf8');
1084
768
  paramWriter.writeVarIntNum(keyringKeysAsArray.length);
1085
769
  paramWriter.write(keyringKeysAsArray);
1086
- const keyringForSubjectAsArray = Utils.toArray(args.keyringForSubject?.[keyringKeys[i]], 'base64');
770
+ const keyringForSubjectAsArray = Utils.toArray(args.keyringForSubject?.[key], 'base64');
1087
771
  paramWriter.writeVarIntNum(keyringForSubjectAsArray.length);
1088
772
  paramWriter.write(keyringForSubjectAsArray);
1089
773
  }
@@ -1121,12 +805,12 @@ export default class WalletWireTransceiver {
1121
805
  async listCertificates(args, originator) {
1122
806
  const paramWriter = new Utils.Writer();
1123
807
  paramWriter.writeVarIntNum(args.certifiers.length);
1124
- for (let i = 0; i < args.certifiers.length; i++) {
1125
- paramWriter.write(Utils.toArray(args.certifiers[i], 'hex'));
808
+ for (const certifier of args.certifiers) {
809
+ paramWriter.write(Utils.toArray(certifier, 'hex'));
1126
810
  }
1127
811
  paramWriter.writeVarIntNum(args.types.length);
1128
- for (let i = 0; i < args.types.length; i++) {
1129
- paramWriter.write(Utils.toArray(args.types[i], 'base64'));
812
+ for (const type of args.types) {
813
+ paramWriter.write(Utils.toArray(type, 'base64'));
1130
814
  }
1131
815
  if (typeof args.limit === 'number') {
1132
816
  paramWriter.writeVarIntNum(args.limit);
@@ -1160,7 +844,7 @@ export default class WalletWireTransceiver {
1160
844
  }
1161
845
  }
1162
846
  const verifierLength = resultReader.readVarIntNum();
1163
- let verifier = undefined;
847
+ let verifier;
1164
848
  if (verifierLength > 0) {
1165
849
  verifier = Utils.toUTF8(resultReader.read(verifierLength));
1166
850
  }
@@ -1168,7 +852,7 @@ export default class WalletWireTransceiver {
1168
852
  ...cert,
1169
853
  signature: cert.signature,
1170
854
  keyring: keyringForVerifier,
1171
- verifier,
855
+ verifier
1172
856
  });
1173
857
  }
1174
858
  return {
@@ -1294,11 +978,7 @@ export default class WalletWireTransceiver {
1294
978
  paramWriter.writeVarIntNum(-1);
1295
979
  }
1296
980
  // Serialize seekPermission
1297
- paramWriter.writeInt8(typeof args.seekPermission === 'boolean'
1298
- ? args.seekPermission
1299
- ? 1
1300
- : 0
1301
- : -1);
981
+ this.writeOptionalBool(paramWriter, args.seekPermission);
1302
982
  const result = await this.transmit('discoverByIdentityKey', originator, paramWriter.toArray());
1303
983
  return this.parseDiscoveryResult(result);
1304
984
  }
@@ -1306,11 +986,11 @@ export default class WalletWireTransceiver {
1306
986
  const paramWriter = new Utils.Writer();
1307
987
  const attributeKeys = Object.keys(args.attributes);
1308
988
  paramWriter.writeVarIntNum(attributeKeys.length);
1309
- for (let i = 0; i < attributeKeys.length; i++) {
1310
- paramWriter.writeVarIntNum(attributeKeys[i].length);
1311
- paramWriter.write(Utils.toArray(attributeKeys[i], 'utf8'));
1312
- paramWriter.writeVarIntNum(args.attributes[attributeKeys[i]].length);
1313
- paramWriter.write(Utils.toArray(args.attributes[attributeKeys[i]], 'utf8'));
989
+ for (const attrKey of attributeKeys) {
990
+ paramWriter.writeVarIntNum(attrKey.length);
991
+ paramWriter.write(Utils.toArray(attrKey, 'utf8'));
992
+ paramWriter.writeVarIntNum(args.attributes[attrKey].length);
993
+ paramWriter.write(Utils.toArray(args.attributes[attrKey], 'utf8'));
1314
994
  }
1315
995
  if (typeof args.limit === 'number') {
1316
996
  paramWriter.writeVarIntNum(args.limit);
@@ -1325,11 +1005,7 @@ export default class WalletWireTransceiver {
1325
1005
  paramWriter.writeVarIntNum(-1);
1326
1006
  }
1327
1007
  // Serialize seekPermission
1328
- paramWriter.writeInt8(typeof args.seekPermission === 'boolean'
1329
- ? args.seekPermission
1330
- ? 1
1331
- : 0
1332
- : -1);
1008
+ this.writeOptionalBool(paramWriter, args.seekPermission);
1333
1009
  const result = await this.transmit('discoverByAttributes', originator, paramWriter.toArray());
1334
1010
  return this.parseDiscoveryResult(result);
1335
1011
  }