chia-blockchain 2.4.4__py3-none-any.whl

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 (1028) hide show
  1. chia/__init__.py +10 -0
  2. chia/__main__.py +5 -0
  3. chia/_tests/README.md +53 -0
  4. chia/_tests/__init__.py +0 -0
  5. chia/_tests/blockchain/__init__.py +0 -0
  6. chia/_tests/blockchain/blockchain_test_utils.py +197 -0
  7. chia/_tests/blockchain/config.py +4 -0
  8. chia/_tests/blockchain/test_augmented_chain.py +147 -0
  9. chia/_tests/blockchain/test_blockchain.py +4100 -0
  10. chia/_tests/blockchain/test_blockchain_transactions.py +1050 -0
  11. chia/_tests/blockchain/test_build_chains.py +61 -0
  12. chia/_tests/blockchain/test_get_block_generator.py +72 -0
  13. chia/_tests/blockchain/test_lookup_fork_chain.py +195 -0
  14. chia/_tests/build-init-files.py +93 -0
  15. chia/_tests/build-job-matrix.py +204 -0
  16. chia/_tests/check_pytest_monitor_output.py +34 -0
  17. chia/_tests/check_sql_statements.py +73 -0
  18. chia/_tests/chia-start-sim +42 -0
  19. chia/_tests/clvm/__init__.py +0 -0
  20. chia/_tests/clvm/benchmark_costs.py +23 -0
  21. chia/_tests/clvm/coin_store.py +147 -0
  22. chia/_tests/clvm/test_chialisp_deserialization.py +101 -0
  23. chia/_tests/clvm/test_clvm_step.py +37 -0
  24. chia/_tests/clvm/test_condition_codes.py +13 -0
  25. chia/_tests/clvm/test_curry_and_treehash.py +57 -0
  26. chia/_tests/clvm/test_program.py +150 -0
  27. chia/_tests/clvm/test_puzzle_compression.py +144 -0
  28. chia/_tests/clvm/test_puzzle_drivers.py +45 -0
  29. chia/_tests/clvm/test_puzzles.py +247 -0
  30. chia/_tests/clvm/test_singletons.py +540 -0
  31. chia/_tests/clvm/test_spend_sim.py +181 -0
  32. chia/_tests/cmds/__init__.py +0 -0
  33. chia/_tests/cmds/cmd_test_utils.py +472 -0
  34. chia/_tests/cmds/config.py +3 -0
  35. chia/_tests/cmds/conftest.py +23 -0
  36. chia/_tests/cmds/test_click_types.py +195 -0
  37. chia/_tests/cmds/test_cmd_framework.py +400 -0
  38. chia/_tests/cmds/test_cmds_util.py +97 -0
  39. chia/_tests/cmds/test_daemon.py +92 -0
  40. chia/_tests/cmds/test_farm_cmd.py +67 -0
  41. chia/_tests/cmds/test_show.py +116 -0
  42. chia/_tests/cmds/test_sim.py +207 -0
  43. chia/_tests/cmds/test_timelock_args.py +75 -0
  44. chia/_tests/cmds/test_tx_config_args.py +153 -0
  45. chia/_tests/cmds/testing_classes.py +59 -0
  46. chia/_tests/cmds/wallet/__init__.py +0 -0
  47. chia/_tests/cmds/wallet/test_coins.py +195 -0
  48. chia/_tests/cmds/wallet/test_consts.py +47 -0
  49. chia/_tests/cmds/wallet/test_dao.py +565 -0
  50. chia/_tests/cmds/wallet/test_did.py +403 -0
  51. chia/_tests/cmds/wallet/test_nft.py +470 -0
  52. chia/_tests/cmds/wallet/test_notifications.py +124 -0
  53. chia/_tests/cmds/wallet/test_offer.toffer +1 -0
  54. chia/_tests/cmds/wallet/test_tx_decorators.py +27 -0
  55. chia/_tests/cmds/wallet/test_vcs.py +376 -0
  56. chia/_tests/cmds/wallet/test_wallet.py +1126 -0
  57. chia/_tests/cmds/wallet/test_wallet_check.py +111 -0
  58. chia/_tests/conftest.py +1304 -0
  59. chia/_tests/connection_utils.py +124 -0
  60. chia/_tests/core/__init__.py +0 -0
  61. chia/_tests/core/cmds/__init__.py +0 -0
  62. chia/_tests/core/cmds/test_beta.py +382 -0
  63. chia/_tests/core/cmds/test_keys.py +1734 -0
  64. chia/_tests/core/cmds/test_wallet.py +126 -0
  65. chia/_tests/core/config.py +3 -0
  66. chia/_tests/core/consensus/__init__.py +0 -0
  67. chia/_tests/core/consensus/test_block_creation.py +56 -0
  68. chia/_tests/core/consensus/test_pot_iterations.py +117 -0
  69. chia/_tests/core/custom_types/__init__.py +0 -0
  70. chia/_tests/core/custom_types/test_coin.py +109 -0
  71. chia/_tests/core/custom_types/test_proof_of_space.py +144 -0
  72. chia/_tests/core/custom_types/test_spend_bundle.py +71 -0
  73. chia/_tests/core/daemon/__init__.py +0 -0
  74. chia/_tests/core/daemon/config.py +4 -0
  75. chia/_tests/core/daemon/test_daemon.py +2128 -0
  76. chia/_tests/core/daemon/test_daemon_register.py +109 -0
  77. chia/_tests/core/daemon/test_keychain_proxy.py +100 -0
  78. chia/_tests/core/data_layer/__init__.py +0 -0
  79. chia/_tests/core/data_layer/config.py +5 -0
  80. chia/_tests/core/data_layer/conftest.py +105 -0
  81. chia/_tests/core/data_layer/test_data_cli.py +57 -0
  82. chia/_tests/core/data_layer/test_data_layer.py +83 -0
  83. chia/_tests/core/data_layer/test_data_layer_util.py +219 -0
  84. chia/_tests/core/data_layer/test_data_rpc.py +3865 -0
  85. chia/_tests/core/data_layer/test_data_store.py +2423 -0
  86. chia/_tests/core/data_layer/test_data_store_schema.py +381 -0
  87. chia/_tests/core/data_layer/test_plugin.py +91 -0
  88. chia/_tests/core/data_layer/util.py +232 -0
  89. chia/_tests/core/farmer/__init__.py +0 -0
  90. chia/_tests/core/farmer/config.py +3 -0
  91. chia/_tests/core/farmer/test_farmer_api.py +101 -0
  92. chia/_tests/core/full_node/__init__.py +0 -0
  93. chia/_tests/core/full_node/config.py +4 -0
  94. chia/_tests/core/full_node/dos/__init__.py +0 -0
  95. chia/_tests/core/full_node/dos/config.py +3 -0
  96. chia/_tests/core/full_node/full_sync/__init__.py +0 -0
  97. chia/_tests/core/full_node/full_sync/config.py +4 -0
  98. chia/_tests/core/full_node/full_sync/test_full_sync.py +448 -0
  99. chia/_tests/core/full_node/ram_db.py +27 -0
  100. chia/_tests/core/full_node/stores/__init__.py +0 -0
  101. chia/_tests/core/full_node/stores/config.py +4 -0
  102. chia/_tests/core/full_node/stores/test_block_store.py +488 -0
  103. chia/_tests/core/full_node/stores/test_coin_store.py +888 -0
  104. chia/_tests/core/full_node/stores/test_full_node_store.py +1215 -0
  105. chia/_tests/core/full_node/stores/test_hint_store.py +230 -0
  106. chia/_tests/core/full_node/stores/test_sync_store.py +135 -0
  107. chia/_tests/core/full_node/test_address_manager.py +588 -0
  108. chia/_tests/core/full_node/test_block_height_map.py +556 -0
  109. chia/_tests/core/full_node/test_conditions.py +558 -0
  110. chia/_tests/core/full_node/test_full_node.py +2445 -0
  111. chia/_tests/core/full_node/test_generator_tools.py +82 -0
  112. chia/_tests/core/full_node/test_hint_management.py +104 -0
  113. chia/_tests/core/full_node/test_node_load.py +34 -0
  114. chia/_tests/core/full_node/test_performance.py +182 -0
  115. chia/_tests/core/full_node/test_subscriptions.py +492 -0
  116. chia/_tests/core/full_node/test_transactions.py +203 -0
  117. chia/_tests/core/full_node/test_tx_processing_queue.py +154 -0
  118. chia/_tests/core/large_block.py +2388 -0
  119. chia/_tests/core/make_block_generator.py +72 -0
  120. chia/_tests/core/mempool/__init__.py +0 -0
  121. chia/_tests/core/mempool/config.py +4 -0
  122. chia/_tests/core/mempool/test_mempool.py +3180 -0
  123. chia/_tests/core/mempool/test_mempool_fee_estimator.py +104 -0
  124. chia/_tests/core/mempool/test_mempool_fee_protocol.py +55 -0
  125. chia/_tests/core/mempool/test_mempool_item_queries.py +192 -0
  126. chia/_tests/core/mempool/test_mempool_manager.py +2054 -0
  127. chia/_tests/core/mempool/test_mempool_performance.py +65 -0
  128. chia/_tests/core/mempool/test_singleton_fast_forward.py +567 -0
  129. chia/_tests/core/node_height.py +28 -0
  130. chia/_tests/core/server/__init__.py +0 -0
  131. chia/_tests/core/server/config.py +3 -0
  132. chia/_tests/core/server/flood.py +82 -0
  133. chia/_tests/core/server/serve.py +132 -0
  134. chia/_tests/core/server/test_capabilities.py +68 -0
  135. chia/_tests/core/server/test_dos.py +320 -0
  136. chia/_tests/core/server/test_event_loop.py +109 -0
  137. chia/_tests/core/server/test_loop.py +290 -0
  138. chia/_tests/core/server/test_node_discovery.py +74 -0
  139. chia/_tests/core/server/test_rate_limits.py +370 -0
  140. chia/_tests/core/server/test_server.py +225 -0
  141. chia/_tests/core/server/test_upnp.py +8 -0
  142. chia/_tests/core/services/__init__.py +0 -0
  143. chia/_tests/core/services/config.py +3 -0
  144. chia/_tests/core/services/test_services.py +166 -0
  145. chia/_tests/core/ssl/__init__.py +0 -0
  146. chia/_tests/core/ssl/config.py +3 -0
  147. chia/_tests/core/ssl/test_ssl.py +198 -0
  148. chia/_tests/core/test_coins.py +33 -0
  149. chia/_tests/core/test_cost_calculation.py +314 -0
  150. chia/_tests/core/test_crawler.py +175 -0
  151. chia/_tests/core/test_crawler_rpc.py +53 -0
  152. chia/_tests/core/test_daemon_rpc.py +24 -0
  153. chia/_tests/core/test_db_conversion.py +129 -0
  154. chia/_tests/core/test_db_validation.py +161 -0
  155. chia/_tests/core/test_farmer_harvester_rpc.py +504 -0
  156. chia/_tests/core/test_filter.py +37 -0
  157. chia/_tests/core/test_full_node_rpc.py +794 -0
  158. chia/_tests/core/test_merkle_set.py +343 -0
  159. chia/_tests/core/test_program.py +49 -0
  160. chia/_tests/core/test_rpc_util.py +87 -0
  161. chia/_tests/core/test_seeder.py +308 -0
  162. chia/_tests/core/test_setproctitle.py +13 -0
  163. chia/_tests/core/util/__init__.py +0 -0
  164. chia/_tests/core/util/config.py +4 -0
  165. chia/_tests/core/util/test_block_cache.py +44 -0
  166. chia/_tests/core/util/test_cached_bls.py +57 -0
  167. chia/_tests/core/util/test_config.py +337 -0
  168. chia/_tests/core/util/test_file_keyring_synchronization.py +105 -0
  169. chia/_tests/core/util/test_files.py +391 -0
  170. chia/_tests/core/util/test_jsonify.py +146 -0
  171. chia/_tests/core/util/test_keychain.py +514 -0
  172. chia/_tests/core/util/test_keyring_wrapper.py +490 -0
  173. chia/_tests/core/util/test_lockfile.py +380 -0
  174. chia/_tests/core/util/test_log_exceptions.py +187 -0
  175. chia/_tests/core/util/test_lru_cache.py +56 -0
  176. chia/_tests/core/util/test_significant_bits.py +40 -0
  177. chia/_tests/core/util/test_streamable.py +883 -0
  178. chia/_tests/db/__init__.py +0 -0
  179. chia/_tests/db/test_db_wrapper.py +565 -0
  180. chia/_tests/environments/__init__.py +0 -0
  181. chia/_tests/environments/common.py +35 -0
  182. chia/_tests/environments/full_node.py +47 -0
  183. chia/_tests/environments/wallet.py +368 -0
  184. chia/_tests/ether.py +19 -0
  185. chia/_tests/farmer_harvester/__init__.py +0 -0
  186. chia/_tests/farmer_harvester/config.py +3 -0
  187. chia/_tests/farmer_harvester/test_farmer.py +1264 -0
  188. chia/_tests/farmer_harvester/test_farmer_harvester.py +292 -0
  189. chia/_tests/farmer_harvester/test_filter_prefix_bits.py +130 -0
  190. chia/_tests/farmer_harvester/test_third_party_harvesters.py +501 -0
  191. chia/_tests/farmer_harvester/test_third_party_harvesters_data.json +29 -0
  192. chia/_tests/fee_estimation/__init__.py +0 -0
  193. chia/_tests/fee_estimation/config.py +3 -0
  194. chia/_tests/fee_estimation/test_fee_estimation_integration.py +262 -0
  195. chia/_tests/fee_estimation/test_fee_estimation_rpc.py +287 -0
  196. chia/_tests/fee_estimation/test_fee_estimation_unit_tests.py +145 -0
  197. chia/_tests/fee_estimation/test_mempoolitem_height_added.py +146 -0
  198. chia/_tests/generator/__init__.py +0 -0
  199. chia/_tests/generator/puzzles/__init__.py +0 -0
  200. chia/_tests/generator/puzzles/test_generator_deserialize.clsp +3 -0
  201. chia/_tests/generator/puzzles/test_generator_deserialize.clsp.hex +1 -0
  202. chia/_tests/generator/puzzles/test_multiple_generator_input_arguments.clsp +19 -0
  203. chia/_tests/generator/puzzles/test_multiple_generator_input_arguments.clsp.hex +1 -0
  204. chia/_tests/generator/test_compression.py +218 -0
  205. chia/_tests/generator/test_generator_types.py +44 -0
  206. chia/_tests/generator/test_rom.py +182 -0
  207. chia/_tests/plot_sync/__init__.py +0 -0
  208. chia/_tests/plot_sync/config.py +3 -0
  209. chia/_tests/plot_sync/test_delta.py +102 -0
  210. chia/_tests/plot_sync/test_plot_sync.py +617 -0
  211. chia/_tests/plot_sync/test_receiver.py +451 -0
  212. chia/_tests/plot_sync/test_sender.py +116 -0
  213. chia/_tests/plot_sync/test_sync_simulated.py +450 -0
  214. chia/_tests/plot_sync/util.py +67 -0
  215. chia/_tests/plotting/__init__.py +0 -0
  216. chia/_tests/plotting/config.py +3 -0
  217. chia/_tests/plotting/test_plot_manager.py +738 -0
  218. chia/_tests/plotting/util.py +13 -0
  219. chia/_tests/pools/__init__.py +0 -0
  220. chia/_tests/pools/config.py +5 -0
  221. chia/_tests/pools/test_pool_cmdline.py +23 -0
  222. chia/_tests/pools/test_pool_config.py +44 -0
  223. chia/_tests/pools/test_pool_puzzles_lifecycle.py +398 -0
  224. chia/_tests/pools/test_pool_rpc.py +1010 -0
  225. chia/_tests/pools/test_pool_wallet.py +201 -0
  226. chia/_tests/pools/test_wallet_pool_store.py +161 -0
  227. chia/_tests/process_junit.py +349 -0
  228. chia/_tests/rpc/__init__.py +0 -0
  229. chia/_tests/rpc/test_rpc_client.py +81 -0
  230. chia/_tests/simulation/__init__.py +0 -0
  231. chia/_tests/simulation/config.py +6 -0
  232. chia/_tests/simulation/test_simulation.py +501 -0
  233. chia/_tests/simulation/test_simulator.py +234 -0
  234. chia/_tests/simulation/test_start_simulator.py +106 -0
  235. chia/_tests/testconfig.py +13 -0
  236. chia/_tests/timelord/__init__.py +0 -0
  237. chia/_tests/timelord/config.py +3 -0
  238. chia/_tests/timelord/test_new_peak.py +437 -0
  239. chia/_tests/timelord/test_timelord.py +11 -0
  240. chia/_tests/tools/1315537.json +170 -0
  241. chia/_tests/tools/1315544.json +160 -0
  242. chia/_tests/tools/1315630.json +150 -0
  243. chia/_tests/tools/300000.json +105 -0
  244. chia/_tests/tools/442734.json +140 -0
  245. chia/_tests/tools/466212.json +130 -0
  246. chia/_tests/tools/__init__.py +0 -0
  247. chia/_tests/tools/config.py +5 -0
  248. chia/_tests/tools/test-blockchain-db.sqlite +0 -0
  249. chia/_tests/tools/test_full_sync.py +30 -0
  250. chia/_tests/tools/test_legacy_keyring.py +82 -0
  251. chia/_tests/tools/test_run_block.py +129 -0
  252. chia/_tests/util/__init__.py +0 -0
  253. chia/_tests/util/benchmark_cost.py +170 -0
  254. chia/_tests/util/benchmarks.py +154 -0
  255. chia/_tests/util/bip39_test_vectors.json +148 -0
  256. chia/_tests/util/blockchain.py +133 -0
  257. chia/_tests/util/blockchain_mock.py +132 -0
  258. chia/_tests/util/build_network_protocol_files.py +302 -0
  259. chia/_tests/util/clvm_generator.bin +0 -0
  260. chia/_tests/util/config.py +3 -0
  261. chia/_tests/util/constants.py +20 -0
  262. chia/_tests/util/db_connection.py +36 -0
  263. chia/_tests/util/full_sync.py +245 -0
  264. chia/_tests/util/gen_ssl_certs.py +115 -0
  265. chia/_tests/util/generator_tools_testing.py +47 -0
  266. chia/_tests/util/key_tool.py +37 -0
  267. chia/_tests/util/misc.py +722 -0
  268. chia/_tests/util/network_protocol_data.py +1074 -0
  269. chia/_tests/util/protocol_messages_bytes-v1.0 +0 -0
  270. chia/_tests/util/protocol_messages_json.py +2700 -0
  271. chia/_tests/util/rpc.py +23 -0
  272. chia/_tests/util/run_block.py +163 -0
  273. chia/_tests/util/setup_nodes.py +479 -0
  274. chia/_tests/util/split_managers.py +99 -0
  275. chia/_tests/util/temp_file.py +14 -0
  276. chia/_tests/util/test_action_scope.py +143 -0
  277. chia/_tests/util/test_async_pool.py +366 -0
  278. chia/_tests/util/test_build_job_matrix.py +43 -0
  279. chia/_tests/util/test_build_network_protocol_files.py +7 -0
  280. chia/_tests/util/test_chia_version.py +50 -0
  281. chia/_tests/util/test_collection.py +11 -0
  282. chia/_tests/util/test_condition_tools.py +231 -0
  283. chia/_tests/util/test_config.py +426 -0
  284. chia/_tests/util/test_dump_keyring.py +60 -0
  285. chia/_tests/util/test_errors.py +10 -0
  286. chia/_tests/util/test_full_block_utils.py +271 -0
  287. chia/_tests/util/test_installed.py +20 -0
  288. chia/_tests/util/test_limited_semaphore.py +52 -0
  289. chia/_tests/util/test_logging_filter.py +43 -0
  290. chia/_tests/util/test_misc.py +444 -0
  291. chia/_tests/util/test_network.py +74 -0
  292. chia/_tests/util/test_network_protocol_files.py +579 -0
  293. chia/_tests/util/test_network_protocol_json.py +266 -0
  294. chia/_tests/util/test_network_protocol_test.py +257 -0
  295. chia/_tests/util/test_paginator.py +72 -0
  296. chia/_tests/util/test_pprint.py +17 -0
  297. chia/_tests/util/test_priority_mutex.py +487 -0
  298. chia/_tests/util/test_recursive_replace.py +116 -0
  299. chia/_tests/util/test_replace_str_to_bytes.py +137 -0
  300. chia/_tests/util/test_service_groups.py +15 -0
  301. chia/_tests/util/test_ssl_check.py +31 -0
  302. chia/_tests/util/test_testnet_overrides.py +19 -0
  303. chia/_tests/util/test_tests_misc.py +38 -0
  304. chia/_tests/util/test_timing.py +37 -0
  305. chia/_tests/util/test_trusted_peer.py +51 -0
  306. chia/_tests/util/time_out_assert.py +154 -0
  307. chia/_tests/wallet/__init__.py +0 -0
  308. chia/_tests/wallet/cat_wallet/__init__.py +0 -0
  309. chia/_tests/wallet/cat_wallet/config.py +4 -0
  310. chia/_tests/wallet/cat_wallet/test_cat_lifecycle.py +468 -0
  311. chia/_tests/wallet/cat_wallet/test_cat_outer_puzzle.py +69 -0
  312. chia/_tests/wallet/cat_wallet/test_cat_wallet.py +1738 -0
  313. chia/_tests/wallet/cat_wallet/test_offer_lifecycle.py +291 -0
  314. chia/_tests/wallet/cat_wallet/test_trades.py +2578 -0
  315. chia/_tests/wallet/clawback/__init__.py +0 -0
  316. chia/_tests/wallet/clawback/config.py +3 -0
  317. chia/_tests/wallet/clawback/test_clawback_decorator.py +80 -0
  318. chia/_tests/wallet/clawback/test_clawback_lifecycle.py +292 -0
  319. chia/_tests/wallet/clawback/test_clawback_metadata.py +51 -0
  320. chia/_tests/wallet/config.py +4 -0
  321. chia/_tests/wallet/conftest.py +217 -0
  322. chia/_tests/wallet/dao_wallet/__init__.py +0 -0
  323. chia/_tests/wallet/dao_wallet/config.py +3 -0
  324. chia/_tests/wallet/dao_wallet/test_dao_clvm.py +1322 -0
  325. chia/_tests/wallet/dao_wallet/test_dao_wallets.py +3488 -0
  326. chia/_tests/wallet/db_wallet/__init__.py +0 -0
  327. chia/_tests/wallet/db_wallet/config.py +3 -0
  328. chia/_tests/wallet/db_wallet/test_db_graftroot.py +143 -0
  329. chia/_tests/wallet/db_wallet/test_dl_offers.py +491 -0
  330. chia/_tests/wallet/db_wallet/test_dl_wallet.py +823 -0
  331. chia/_tests/wallet/did_wallet/__init__.py +0 -0
  332. chia/_tests/wallet/did_wallet/config.py +4 -0
  333. chia/_tests/wallet/did_wallet/test_did.py +1481 -0
  334. chia/_tests/wallet/nft_wallet/__init__.py +0 -0
  335. chia/_tests/wallet/nft_wallet/config.py +4 -0
  336. chia/_tests/wallet/nft_wallet/test_nft_1_offers.py +1492 -0
  337. chia/_tests/wallet/nft_wallet/test_nft_bulk_mint.py +1014 -0
  338. chia/_tests/wallet/nft_wallet/test_nft_lifecycle.py +376 -0
  339. chia/_tests/wallet/nft_wallet/test_nft_offers.py +1209 -0
  340. chia/_tests/wallet/nft_wallet/test_nft_puzzles.py +172 -0
  341. chia/_tests/wallet/nft_wallet/test_nft_wallet.py +2558 -0
  342. chia/_tests/wallet/nft_wallet/test_ownership_outer_puzzle.py +70 -0
  343. chia/_tests/wallet/rpc/__init__.py +0 -0
  344. chia/_tests/wallet/rpc/config.py +4 -0
  345. chia/_tests/wallet/rpc/test_dl_wallet_rpc.py +287 -0
  346. chia/_tests/wallet/rpc/test_wallet_rpc.py +3106 -0
  347. chia/_tests/wallet/simple_sync/__init__.py +0 -0
  348. chia/_tests/wallet/simple_sync/config.py +3 -0
  349. chia/_tests/wallet/simple_sync/test_simple_sync_protocol.py +719 -0
  350. chia/_tests/wallet/sync/__init__.py +0 -0
  351. chia/_tests/wallet/sync/config.py +4 -0
  352. chia/_tests/wallet/sync/test_wallet_sync.py +1529 -0
  353. chia/_tests/wallet/test_address_type.py +189 -0
  354. chia/_tests/wallet/test_bech32m.py +45 -0
  355. chia/_tests/wallet/test_clvm_streamable.py +244 -0
  356. chia/_tests/wallet/test_coin_selection.py +589 -0
  357. chia/_tests/wallet/test_conditions.py +388 -0
  358. chia/_tests/wallet/test_debug_spend_bundle.py +76 -0
  359. chia/_tests/wallet/test_new_wallet_protocol.py +1176 -0
  360. chia/_tests/wallet/test_nft_store.py +193 -0
  361. chia/_tests/wallet/test_notifications.py +196 -0
  362. chia/_tests/wallet/test_offer_parsing_performance.py +48 -0
  363. chia/_tests/wallet/test_puzzle_store.py +133 -0
  364. chia/_tests/wallet/test_sign_coin_spends.py +159 -0
  365. chia/_tests/wallet/test_signer_protocol.py +948 -0
  366. chia/_tests/wallet/test_singleton.py +122 -0
  367. chia/_tests/wallet/test_singleton_lifecycle_fast.py +772 -0
  368. chia/_tests/wallet/test_singleton_store.py +152 -0
  369. chia/_tests/wallet/test_taproot.py +19 -0
  370. chia/_tests/wallet/test_transaction_store.py +941 -0
  371. chia/_tests/wallet/test_util.py +181 -0
  372. chia/_tests/wallet/test_wallet.py +2139 -0
  373. chia/_tests/wallet/test_wallet_action_scope.py +85 -0
  374. chia/_tests/wallet/test_wallet_blockchain.py +113 -0
  375. chia/_tests/wallet/test_wallet_coin_store.py +1002 -0
  376. chia/_tests/wallet/test_wallet_interested_store.py +43 -0
  377. chia/_tests/wallet/test_wallet_key_val_store.py +40 -0
  378. chia/_tests/wallet/test_wallet_node.py +783 -0
  379. chia/_tests/wallet/test_wallet_retry.py +95 -0
  380. chia/_tests/wallet/test_wallet_state_manager.py +252 -0
  381. chia/_tests/wallet/test_wallet_test_framework.py +275 -0
  382. chia/_tests/wallet/test_wallet_trade_store.py +218 -0
  383. chia/_tests/wallet/test_wallet_user_store.py +34 -0
  384. chia/_tests/wallet/test_wallet_utils.py +155 -0
  385. chia/_tests/wallet/vc_wallet/__init__.py +0 -0
  386. chia/_tests/wallet/vc_wallet/config.py +3 -0
  387. chia/_tests/wallet/vc_wallet/test_cr_outer_puzzle.py +70 -0
  388. chia/_tests/wallet/vc_wallet/test_vc_lifecycle.py +883 -0
  389. chia/_tests/wallet/vc_wallet/test_vc_wallet.py +801 -0
  390. chia/_tests/wallet/wallet_block_tools.py +327 -0
  391. chia/_tests/weight_proof/__init__.py +0 -0
  392. chia/_tests/weight_proof/config.py +3 -0
  393. chia/_tests/weight_proof/test_weight_proof.py +528 -0
  394. chia/clvm/__init__.py +0 -0
  395. chia/clvm/spend_sim.py +488 -0
  396. chia/cmds/__init__.py +0 -0
  397. chia/cmds/beta.py +183 -0
  398. chia/cmds/beta_funcs.py +133 -0
  399. chia/cmds/check_wallet_db.py +418 -0
  400. chia/cmds/chia.py +143 -0
  401. chia/cmds/cmd_classes.py +315 -0
  402. chia/cmds/cmds_util.py +498 -0
  403. chia/cmds/coin_funcs.py +260 -0
  404. chia/cmds/coins.py +220 -0
  405. chia/cmds/completion.py +49 -0
  406. chia/cmds/configure.py +331 -0
  407. chia/cmds/dao.py +1008 -0
  408. chia/cmds/dao_funcs.py +576 -0
  409. chia/cmds/data.py +707 -0
  410. chia/cmds/data_funcs.py +380 -0
  411. chia/cmds/db.py +86 -0
  412. chia/cmds/db_backup_func.py +77 -0
  413. chia/cmds/db_upgrade_func.py +452 -0
  414. chia/cmds/db_validate_func.py +184 -0
  415. chia/cmds/dev.py +16 -0
  416. chia/cmds/farm.py +87 -0
  417. chia/cmds/farm_funcs.py +207 -0
  418. chia/cmds/init.py +70 -0
  419. chia/cmds/init_funcs.py +367 -0
  420. chia/cmds/installers.py +129 -0
  421. chia/cmds/keys.py +510 -0
  422. chia/cmds/keys_funcs.py +864 -0
  423. chia/cmds/netspace.py +47 -0
  424. chia/cmds/netspace_funcs.py +53 -0
  425. chia/cmds/options.py +32 -0
  426. chia/cmds/param_types.py +228 -0
  427. chia/cmds/passphrase.py +130 -0
  428. chia/cmds/passphrase_funcs.py +346 -0
  429. chia/cmds/peer.py +50 -0
  430. chia/cmds/peer_funcs.py +129 -0
  431. chia/cmds/plotnft.py +206 -0
  432. chia/cmds/plotnft_funcs.py +374 -0
  433. chia/cmds/plots.py +222 -0
  434. chia/cmds/plotters.py +17 -0
  435. chia/cmds/rpc.py +188 -0
  436. chia/cmds/show.py +71 -0
  437. chia/cmds/show_funcs.py +214 -0
  438. chia/cmds/signer.py +304 -0
  439. chia/cmds/sim.py +217 -0
  440. chia/cmds/sim_funcs.py +509 -0
  441. chia/cmds/start.py +24 -0
  442. chia/cmds/start_funcs.py +112 -0
  443. chia/cmds/stop.py +61 -0
  444. chia/cmds/units.py +11 -0
  445. chia/cmds/wallet.py +1745 -0
  446. chia/cmds/wallet_funcs.py +1800 -0
  447. chia/consensus/__init__.py +0 -0
  448. chia/consensus/block_body_validation.py +515 -0
  449. chia/consensus/block_creation.py +525 -0
  450. chia/consensus/block_header_validation.py +1064 -0
  451. chia/consensus/block_record.py +32 -0
  452. chia/consensus/block_rewards.py +53 -0
  453. chia/consensus/block_root_validation.py +46 -0
  454. chia/consensus/blockchain.py +1100 -0
  455. chia/consensus/blockchain_interface.py +56 -0
  456. chia/consensus/coinbase.py +30 -0
  457. chia/consensus/condition_costs.py +9 -0
  458. chia/consensus/constants.py +49 -0
  459. chia/consensus/cost_calculator.py +15 -0
  460. chia/consensus/default_constants.py +90 -0
  461. chia/consensus/deficit.py +55 -0
  462. chia/consensus/difficulty_adjustment.py +412 -0
  463. chia/consensus/find_fork_point.py +111 -0
  464. chia/consensus/full_block_to_block_record.py +167 -0
  465. chia/consensus/get_block_challenge.py +106 -0
  466. chia/consensus/get_block_generator.py +26 -0
  467. chia/consensus/make_sub_epoch_summary.py +210 -0
  468. chia/consensus/multiprocess_validation.py +365 -0
  469. chia/consensus/pos_quality.py +19 -0
  470. chia/consensus/pot_iterations.py +67 -0
  471. chia/consensus/puzzles/__init__.py +0 -0
  472. chia/consensus/puzzles/chialisp_deserialisation.clsp +69 -0
  473. chia/consensus/puzzles/chialisp_deserialisation.clsp.hex +1 -0
  474. chia/consensus/puzzles/rom_bootstrap_generator.clsp +37 -0
  475. chia/consensus/puzzles/rom_bootstrap_generator.clsp.hex +1 -0
  476. chia/consensus/vdf_info_computation.py +156 -0
  477. chia/daemon/__init__.py +0 -0
  478. chia/daemon/client.py +233 -0
  479. chia/daemon/keychain_proxy.py +501 -0
  480. chia/daemon/keychain_server.py +365 -0
  481. chia/daemon/server.py +1616 -0
  482. chia/daemon/windows_signal.py +56 -0
  483. chia/data_layer/__init__.py +0 -0
  484. chia/data_layer/data_layer.py +1303 -0
  485. chia/data_layer/data_layer_api.py +25 -0
  486. chia/data_layer/data_layer_errors.py +50 -0
  487. chia/data_layer/data_layer_server.py +170 -0
  488. chia/data_layer/data_layer_util.py +985 -0
  489. chia/data_layer/data_layer_wallet.py +1315 -0
  490. chia/data_layer/data_store.py +2267 -0
  491. chia/data_layer/dl_wallet_store.py +407 -0
  492. chia/data_layer/download_data.py +389 -0
  493. chia/data_layer/puzzles/__init__.py +0 -0
  494. chia/data_layer/puzzles/graftroot_dl_offers.clsp +100 -0
  495. chia/data_layer/puzzles/graftroot_dl_offers.clsp.hex +1 -0
  496. chia/data_layer/s3_plugin_config.yml +33 -0
  497. chia/data_layer/s3_plugin_service.py +468 -0
  498. chia/data_layer/util/__init__.py +0 -0
  499. chia/data_layer/util/benchmark.py +108 -0
  500. chia/data_layer/util/plugin.py +41 -0
  501. chia/farmer/__init__.py +0 -0
  502. chia/farmer/farmer.py +920 -0
  503. chia/farmer/farmer_api.py +814 -0
  504. chia/full_node/__init__.py +0 -0
  505. chia/full_node/bitcoin_fee_estimator.py +85 -0
  506. chia/full_node/block_height_map.py +271 -0
  507. chia/full_node/block_store.py +570 -0
  508. chia/full_node/bundle_tools.py +19 -0
  509. chia/full_node/coin_store.py +646 -0
  510. chia/full_node/fee_estimate.py +54 -0
  511. chia/full_node/fee_estimate_store.py +24 -0
  512. chia/full_node/fee_estimation.py +93 -0
  513. chia/full_node/fee_estimator.py +90 -0
  514. chia/full_node/fee_estimator_constants.py +38 -0
  515. chia/full_node/fee_estimator_interface.py +42 -0
  516. chia/full_node/fee_history.py +26 -0
  517. chia/full_node/fee_tracker.py +564 -0
  518. chia/full_node/full_node.py +3052 -0
  519. chia/full_node/full_node_api.py +1974 -0
  520. chia/full_node/full_node_store.py +1033 -0
  521. chia/full_node/hint_management.py +56 -0
  522. chia/full_node/hint_store.py +94 -0
  523. chia/full_node/mempool.py +583 -0
  524. chia/full_node/mempool_check_conditions.py +177 -0
  525. chia/full_node/mempool_manager.py +858 -0
  526. chia/full_node/pending_tx_cache.py +112 -0
  527. chia/full_node/puzzles/__init__.py +0 -0
  528. chia/full_node/puzzles/block_program_zero.clsp +14 -0
  529. chia/full_node/puzzles/block_program_zero.clsp.hex +1 -0
  530. chia/full_node/puzzles/decompress_coin_spend_entry.clsp +5 -0
  531. chia/full_node/puzzles/decompress_coin_spend_entry.clsp.hex +1 -0
  532. chia/full_node/puzzles/decompress_coin_spend_entry_with_prefix.clsp +7 -0
  533. chia/full_node/puzzles/decompress_coin_spend_entry_with_prefix.clsp.hex +1 -0
  534. chia/full_node/puzzles/decompress_puzzle.clsp +6 -0
  535. chia/full_node/puzzles/decompress_puzzle.clsp.hex +1 -0
  536. chia/full_node/signage_point.py +16 -0
  537. chia/full_node/subscriptions.py +248 -0
  538. chia/full_node/sync_store.py +145 -0
  539. chia/full_node/tx_processing_queue.py +78 -0
  540. chia/full_node/weight_proof.py +1719 -0
  541. chia/harvester/__init__.py +0 -0
  542. chia/harvester/harvester.py +271 -0
  543. chia/harvester/harvester_api.py +374 -0
  544. chia/introducer/__init__.py +0 -0
  545. chia/introducer/introducer.py +120 -0
  546. chia/introducer/introducer_api.py +64 -0
  547. chia/legacy/__init__.py +0 -0
  548. chia/legacy/keyring.py +154 -0
  549. chia/plot_sync/__init__.py +0 -0
  550. chia/plot_sync/delta.py +61 -0
  551. chia/plot_sync/exceptions.py +56 -0
  552. chia/plot_sync/receiver.py +385 -0
  553. chia/plot_sync/sender.py +337 -0
  554. chia/plot_sync/util.py +43 -0
  555. chia/plotters/__init__.py +0 -0
  556. chia/plotters/bladebit.py +388 -0
  557. chia/plotters/chiapos.py +63 -0
  558. chia/plotters/madmax.py +224 -0
  559. chia/plotters/plotters.py +577 -0
  560. chia/plotters/plotters_util.py +131 -0
  561. chia/plotting/__init__.py +0 -0
  562. chia/plotting/cache.py +212 -0
  563. chia/plotting/check_plots.py +283 -0
  564. chia/plotting/create_plots.py +278 -0
  565. chia/plotting/manager.py +436 -0
  566. chia/plotting/util.py +324 -0
  567. chia/pools/__init__.py +0 -0
  568. chia/pools/pool_config.py +110 -0
  569. chia/pools/pool_puzzles.py +459 -0
  570. chia/pools/pool_wallet.py +926 -0
  571. chia/pools/pool_wallet_info.py +118 -0
  572. chia/pools/puzzles/__init__.py +0 -0
  573. chia/pools/puzzles/pool_member_innerpuz.clsp +70 -0
  574. chia/pools/puzzles/pool_member_innerpuz.clsp.hex +1 -0
  575. chia/pools/puzzles/pool_waitingroom_innerpuz.clsp +69 -0
  576. chia/pools/puzzles/pool_waitingroom_innerpuz.clsp.hex +1 -0
  577. chia/protocols/__init__.py +0 -0
  578. chia/protocols/farmer_protocol.py +102 -0
  579. chia/protocols/full_node_protocol.py +219 -0
  580. chia/protocols/harvester_protocol.py +216 -0
  581. chia/protocols/introducer_protocol.py +26 -0
  582. chia/protocols/pool_protocol.py +177 -0
  583. chia/protocols/protocol_message_types.py +139 -0
  584. chia/protocols/protocol_state_machine.py +87 -0
  585. chia/protocols/protocol_timing.py +7 -0
  586. chia/protocols/shared_protocol.py +86 -0
  587. chia/protocols/timelord_protocol.py +93 -0
  588. chia/protocols/wallet_protocol.py +401 -0
  589. chia/py.typed +0 -0
  590. chia/rpc/__init__.py +0 -0
  591. chia/rpc/crawler_rpc_api.py +75 -0
  592. chia/rpc/data_layer_rpc_api.py +639 -0
  593. chia/rpc/data_layer_rpc_client.py +188 -0
  594. chia/rpc/data_layer_rpc_util.py +62 -0
  595. chia/rpc/farmer_rpc_api.py +360 -0
  596. chia/rpc/farmer_rpc_client.py +86 -0
  597. chia/rpc/full_node_rpc_api.py +954 -0
  598. chia/rpc/full_node_rpc_client.py +292 -0
  599. chia/rpc/harvester_rpc_api.py +136 -0
  600. chia/rpc/harvester_rpc_client.py +54 -0
  601. chia/rpc/rpc_client.py +144 -0
  602. chia/rpc/rpc_server.py +447 -0
  603. chia/rpc/timelord_rpc_api.py +27 -0
  604. chia/rpc/util.py +293 -0
  605. chia/rpc/wallet_request_types.py +688 -0
  606. chia/rpc/wallet_rpc_api.py +4779 -0
  607. chia/rpc/wallet_rpc_client.py +1844 -0
  608. chia/seeder/__init__.py +0 -0
  609. chia/seeder/crawl_store.py +427 -0
  610. chia/seeder/crawler.py +423 -0
  611. chia/seeder/crawler_api.py +129 -0
  612. chia/seeder/dns_server.py +544 -0
  613. chia/seeder/peer_record.py +146 -0
  614. chia/seeder/start_crawler.py +88 -0
  615. chia/server/__init__.py +0 -0
  616. chia/server/address_manager.py +658 -0
  617. chia/server/address_manager_store.py +237 -0
  618. chia/server/api_protocol.py +11 -0
  619. chia/server/capabilities.py +24 -0
  620. chia/server/chia_policy.py +345 -0
  621. chia/server/introducer_peers.py +76 -0
  622. chia/server/node_discovery.py +718 -0
  623. chia/server/outbound_message.py +33 -0
  624. chia/server/rate_limit_numbers.py +204 -0
  625. chia/server/rate_limits.py +113 -0
  626. chia/server/server.py +720 -0
  627. chia/server/signal_handlers.py +117 -0
  628. chia/server/ssl_context.py +32 -0
  629. chia/server/start_data_layer.py +137 -0
  630. chia/server/start_farmer.py +86 -0
  631. chia/server/start_full_node.py +106 -0
  632. chia/server/start_harvester.py +80 -0
  633. chia/server/start_introducer.py +69 -0
  634. chia/server/start_service.py +328 -0
  635. chia/server/start_timelord.py +82 -0
  636. chia/server/start_wallet.py +109 -0
  637. chia/server/upnp.py +117 -0
  638. chia/server/ws_connection.py +752 -0
  639. chia/simulator/__init__.py +0 -0
  640. chia/simulator/block_tools.py +2053 -0
  641. chia/simulator/full_node_simulator.py +802 -0
  642. chia/simulator/keyring.py +128 -0
  643. chia/simulator/setup_services.py +505 -0
  644. chia/simulator/simulator_constants.py +13 -0
  645. chia/simulator/simulator_full_node_rpc_api.py +101 -0
  646. chia/simulator/simulator_full_node_rpc_client.py +62 -0
  647. chia/simulator/simulator_protocol.py +29 -0
  648. chia/simulator/simulator_test_tools.py +163 -0
  649. chia/simulator/socket.py +27 -0
  650. chia/simulator/ssl_certs.py +114 -0
  651. chia/simulator/ssl_certs_1.py +699 -0
  652. chia/simulator/ssl_certs_10.py +699 -0
  653. chia/simulator/ssl_certs_2.py +699 -0
  654. chia/simulator/ssl_certs_3.py +699 -0
  655. chia/simulator/ssl_certs_4.py +699 -0
  656. chia/simulator/ssl_certs_5.py +699 -0
  657. chia/simulator/ssl_certs_6.py +699 -0
  658. chia/simulator/ssl_certs_7.py +699 -0
  659. chia/simulator/ssl_certs_8.py +699 -0
  660. chia/simulator/ssl_certs_9.py +699 -0
  661. chia/simulator/start_simulator.py +135 -0
  662. chia/simulator/wallet_tools.py +245 -0
  663. chia/ssl/__init__.py +0 -0
  664. chia/ssl/chia_ca.crt +19 -0
  665. chia/ssl/chia_ca.key +28 -0
  666. chia/ssl/create_ssl.py +249 -0
  667. chia/ssl/dst_root_ca.pem +20 -0
  668. chia/timelord/__init__.py +0 -0
  669. chia/timelord/iters_from_block.py +50 -0
  670. chia/timelord/timelord.py +1202 -0
  671. chia/timelord/timelord_api.py +132 -0
  672. chia/timelord/timelord_launcher.py +188 -0
  673. chia/timelord/timelord_state.py +244 -0
  674. chia/timelord/types.py +22 -0
  675. chia/types/__init__.py +0 -0
  676. chia/types/aliases.py +35 -0
  677. chia/types/block_protocol.py +20 -0
  678. chia/types/blockchain_format/__init__.py +0 -0
  679. chia/types/blockchain_format/classgroup.py +5 -0
  680. chia/types/blockchain_format/coin.py +28 -0
  681. chia/types/blockchain_format/foliage.py +8 -0
  682. chia/types/blockchain_format/pool_target.py +5 -0
  683. chia/types/blockchain_format/program.py +270 -0
  684. chia/types/blockchain_format/proof_of_space.py +135 -0
  685. chia/types/blockchain_format/reward_chain_block.py +6 -0
  686. chia/types/blockchain_format/serialized_program.py +5 -0
  687. chia/types/blockchain_format/sized_bytes.py +11 -0
  688. chia/types/blockchain_format/slots.py +9 -0
  689. chia/types/blockchain_format/sub_epoch_summary.py +5 -0
  690. chia/types/blockchain_format/tree_hash.py +72 -0
  691. chia/types/blockchain_format/vdf.py +86 -0
  692. chia/types/clvm_cost.py +13 -0
  693. chia/types/coin_record.py +43 -0
  694. chia/types/coin_spend.py +115 -0
  695. chia/types/condition_opcodes.py +73 -0
  696. chia/types/condition_with_args.py +17 -0
  697. chia/types/eligible_coin_spends.py +364 -0
  698. chia/types/end_of_slot_bundle.py +5 -0
  699. chia/types/fee_rate.py +38 -0
  700. chia/types/full_block.py +5 -0
  701. chia/types/generator_types.py +14 -0
  702. chia/types/header_block.py +5 -0
  703. chia/types/internal_mempool_item.py +19 -0
  704. chia/types/mempool_inclusion_status.py +9 -0
  705. chia/types/mempool_item.py +85 -0
  706. chia/types/mempool_submission_status.py +30 -0
  707. chia/types/mojos.py +7 -0
  708. chia/types/peer_info.py +64 -0
  709. chia/types/signing_mode.py +29 -0
  710. chia/types/spend_bundle.py +31 -0
  711. chia/types/spend_bundle_conditions.py +7 -0
  712. chia/types/transaction_queue_entry.py +55 -0
  713. chia/types/unfinished_block.py +5 -0
  714. chia/types/unfinished_header_block.py +37 -0
  715. chia/types/weight_proof.py +50 -0
  716. chia/util/__init__.py +0 -0
  717. chia/util/action_scope.py +168 -0
  718. chia/util/api_decorators.py +89 -0
  719. chia/util/async_pool.py +224 -0
  720. chia/util/augmented_chain.py +130 -0
  721. chia/util/batches.py +39 -0
  722. chia/util/bech32m.py +123 -0
  723. chia/util/beta_metrics.py +118 -0
  724. chia/util/block_cache.py +56 -0
  725. chia/util/byte_types.py +10 -0
  726. chia/util/check_fork_next_block.py +32 -0
  727. chia/util/chia_logging.py +124 -0
  728. chia/util/chia_version.py +33 -0
  729. chia/util/collection.py +17 -0
  730. chia/util/condition_tools.py +201 -0
  731. chia/util/config.py +366 -0
  732. chia/util/cpu.py +20 -0
  733. chia/util/db_synchronous.py +21 -0
  734. chia/util/db_version.py +30 -0
  735. chia/util/db_wrapper.py +427 -0
  736. chia/util/default_root.py +10 -0
  737. chia/util/dump_keyring.py +93 -0
  738. chia/util/english.txt +2048 -0
  739. chia/util/errors.py +351 -0
  740. chia/util/file_keyring.py +480 -0
  741. chia/util/files.py +95 -0
  742. chia/util/full_block_utils.py +321 -0
  743. chia/util/generator_tools.py +62 -0
  744. chia/util/hash.py +29 -0
  745. chia/util/initial-config.yaml +675 -0
  746. chia/util/inline_executor.py +24 -0
  747. chia/util/ints.py +19 -0
  748. chia/util/json_util.py +41 -0
  749. chia/util/keychain.py +673 -0
  750. chia/util/keyring_wrapper.py +266 -0
  751. chia/util/limited_semaphore.py +39 -0
  752. chia/util/lock.py +47 -0
  753. chia/util/log_exceptions.py +29 -0
  754. chia/util/logging.py +34 -0
  755. chia/util/lru_cache.py +29 -0
  756. chia/util/math.py +20 -0
  757. chia/util/network.py +240 -0
  758. chia/util/paginator.py +46 -0
  759. chia/util/path.py +29 -0
  760. chia/util/permissions.py +19 -0
  761. chia/util/pprint.py +40 -0
  762. chia/util/prev_transaction_block.py +23 -0
  763. chia/util/priority_mutex.py +92 -0
  764. chia/util/profiler.py +194 -0
  765. chia/util/recursive_replace.py +22 -0
  766. chia/util/safe_cancel_task.py +14 -0
  767. chia/util/service_groups.py +47 -0
  768. chia/util/setproctitle.py +20 -0
  769. chia/util/significant_bits.py +30 -0
  770. chia/util/ssl_check.py +213 -0
  771. chia/util/streamable.py +654 -0
  772. chia/util/task_timing.py +378 -0
  773. chia/util/timing.py +64 -0
  774. chia/util/vdf_prover.py +31 -0
  775. chia/util/ws_message.py +66 -0
  776. chia/wallet/__init__.py +0 -0
  777. chia/wallet/cat_wallet/__init__.py +0 -0
  778. chia/wallet/cat_wallet/cat_constants.py +75 -0
  779. chia/wallet/cat_wallet/cat_info.py +47 -0
  780. chia/wallet/cat_wallet/cat_outer_puzzle.py +120 -0
  781. chia/wallet/cat_wallet/cat_utils.py +163 -0
  782. chia/wallet/cat_wallet/cat_wallet.py +869 -0
  783. chia/wallet/cat_wallet/dao_cat_info.py +28 -0
  784. chia/wallet/cat_wallet/dao_cat_wallet.py +669 -0
  785. chia/wallet/cat_wallet/lineage_store.py +74 -0
  786. chia/wallet/cat_wallet/puzzles/__init__.py +0 -0
  787. chia/wallet/cat_wallet/puzzles/cat_truths.clib +31 -0
  788. chia/wallet/cat_wallet/puzzles/cat_v2.clsp +397 -0
  789. chia/wallet/cat_wallet/puzzles/cat_v2.clsp.hex +1 -0
  790. chia/wallet/cat_wallet/puzzles/delegated_tail.clsp +25 -0
  791. chia/wallet/cat_wallet/puzzles/delegated_tail.clsp.hex +1 -0
  792. chia/wallet/cat_wallet/puzzles/everything_with_signature.clsp +15 -0
  793. chia/wallet/cat_wallet/puzzles/everything_with_signature.clsp.hex +1 -0
  794. chia/wallet/cat_wallet/puzzles/genesis_by_coin_id.clsp +26 -0
  795. chia/wallet/cat_wallet/puzzles/genesis_by_coin_id.clsp.hex +1 -0
  796. chia/wallet/cat_wallet/puzzles/genesis_by_coin_id_or_singleton.clsp +42 -0
  797. chia/wallet/cat_wallet/puzzles/genesis_by_coin_id_or_singleton.clsp.hex +1 -0
  798. chia/wallet/cat_wallet/puzzles/genesis_by_puzzle_hash.clsp +24 -0
  799. chia/wallet/cat_wallet/puzzles/genesis_by_puzzle_hash.clsp.hex +1 -0
  800. chia/wallet/coin_selection.py +188 -0
  801. chia/wallet/conditions.py +1326 -0
  802. chia/wallet/dao_wallet/__init__.py +0 -0
  803. chia/wallet/dao_wallet/dao_info.py +61 -0
  804. chia/wallet/dao_wallet/dao_utils.py +810 -0
  805. chia/wallet/dao_wallet/dao_wallet.py +2121 -0
  806. chia/wallet/db_wallet/__init__.py +0 -0
  807. chia/wallet/db_wallet/db_wallet_puzzles.py +107 -0
  808. chia/wallet/derivation_record.py +30 -0
  809. chia/wallet/derive_keys.py +146 -0
  810. chia/wallet/did_wallet/__init__.py +0 -0
  811. chia/wallet/did_wallet/did_info.py +39 -0
  812. chia/wallet/did_wallet/did_wallet.py +1485 -0
  813. chia/wallet/did_wallet/did_wallet_puzzles.py +220 -0
  814. chia/wallet/did_wallet/puzzles/__init__.py +0 -0
  815. chia/wallet/did_wallet/puzzles/did_innerpuz.clsp +135 -0
  816. chia/wallet/did_wallet/puzzles/did_innerpuz.clsp.hex +1 -0
  817. chia/wallet/driver_protocol.py +26 -0
  818. chia/wallet/key_val_store.py +55 -0
  819. chia/wallet/lineage_proof.py +58 -0
  820. chia/wallet/nft_wallet/__init__.py +0 -0
  821. chia/wallet/nft_wallet/metadata_outer_puzzle.py +92 -0
  822. chia/wallet/nft_wallet/nft_info.py +120 -0
  823. chia/wallet/nft_wallet/nft_puzzles.py +305 -0
  824. chia/wallet/nft_wallet/nft_wallet.py +1686 -0
  825. chia/wallet/nft_wallet/ownership_outer_puzzle.py +101 -0
  826. chia/wallet/nft_wallet/puzzles/__init__.py +0 -0
  827. chia/wallet/nft_wallet/puzzles/create_nft_launcher_from_did.clsp +6 -0
  828. chia/wallet/nft_wallet/puzzles/create_nft_launcher_from_did.clsp.hex +1 -0
  829. chia/wallet/nft_wallet/puzzles/nft_intermediate_launcher.clsp +6 -0
  830. chia/wallet/nft_wallet/puzzles/nft_intermediate_launcher.clsp.hex +1 -0
  831. chia/wallet/nft_wallet/puzzles/nft_metadata_updater_default.clsp +30 -0
  832. chia/wallet/nft_wallet/puzzles/nft_metadata_updater_default.clsp.hex +1 -0
  833. chia/wallet/nft_wallet/puzzles/nft_metadata_updater_updateable.clsp +28 -0
  834. chia/wallet/nft_wallet/puzzles/nft_metadata_updater_updateable.clsp.hex +1 -0
  835. chia/wallet/nft_wallet/puzzles/nft_ownership_layer.clsp +100 -0
  836. chia/wallet/nft_wallet/puzzles/nft_ownership_layer.clsp.hex +1 -0
  837. chia/wallet/nft_wallet/puzzles/nft_ownership_transfer_program_one_way_claim_with_royalties.clsp +78 -0
  838. chia/wallet/nft_wallet/puzzles/nft_ownership_transfer_program_one_way_claim_with_royalties.clsp.hex +1 -0
  839. chia/wallet/nft_wallet/puzzles/nft_state_layer.clsp +74 -0
  840. chia/wallet/nft_wallet/puzzles/nft_state_layer.clsp.hex +1 -0
  841. chia/wallet/nft_wallet/singleton_outer_puzzle.py +101 -0
  842. chia/wallet/nft_wallet/transfer_program_puzzle.py +82 -0
  843. chia/wallet/nft_wallet/uncurry_nft.py +217 -0
  844. chia/wallet/notification_manager.py +117 -0
  845. chia/wallet/notification_store.py +178 -0
  846. chia/wallet/outer_puzzles.py +84 -0
  847. chia/wallet/payment.py +34 -0
  848. chia/wallet/puzzle_drivers.py +118 -0
  849. chia/wallet/puzzles/__init__.py +0 -0
  850. chia/wallet/puzzles/augmented_condition.clsp +13 -0
  851. chia/wallet/puzzles/augmented_condition.clsp.hex +1 -0
  852. chia/wallet/puzzles/clawback/__init__.py +0 -0
  853. chia/wallet/puzzles/clawback/drivers.py +188 -0
  854. chia/wallet/puzzles/clawback/metadata.py +38 -0
  855. chia/wallet/puzzles/clawback/puzzle_decorator.py +67 -0
  856. chia/wallet/puzzles/condition_codes.clib +77 -0
  857. chia/wallet/puzzles/curry-and-treehash.clib +102 -0
  858. chia/wallet/puzzles/curry.clib +135 -0
  859. chia/wallet/puzzles/curry_by_index.clib +16 -0
  860. chia/wallet/puzzles/dao_cat_eve.clsp +17 -0
  861. chia/wallet/puzzles/dao_cat_eve.clsp.hex +1 -0
  862. chia/wallet/puzzles/dao_cat_launcher.clsp +36 -0
  863. chia/wallet/puzzles/dao_cat_launcher.clsp.hex +1 -0
  864. chia/wallet/puzzles/dao_finished_state.clsp +35 -0
  865. chia/wallet/puzzles/dao_finished_state.clsp.hex +1 -0
  866. chia/wallet/puzzles/dao_finished_state.clsp.hex.sha256tree +1 -0
  867. chia/wallet/puzzles/dao_lockup.clsp +288 -0
  868. chia/wallet/puzzles/dao_lockup.clsp.hex +1 -0
  869. chia/wallet/puzzles/dao_lockup.clsp.hex.sha256tree +1 -0
  870. chia/wallet/puzzles/dao_proposal.clsp +377 -0
  871. chia/wallet/puzzles/dao_proposal.clsp.hex +1 -0
  872. chia/wallet/puzzles/dao_proposal.clsp.hex.sha256tree +1 -0
  873. chia/wallet/puzzles/dao_proposal_timer.clsp +78 -0
  874. chia/wallet/puzzles/dao_proposal_timer.clsp.hex +1 -0
  875. chia/wallet/puzzles/dao_proposal_timer.clsp.hex.sha256tree +1 -0
  876. chia/wallet/puzzles/dao_proposal_validator.clsp +87 -0
  877. chia/wallet/puzzles/dao_proposal_validator.clsp.hex +1 -0
  878. chia/wallet/puzzles/dao_proposal_validator.clsp.hex.sha256tree +1 -0
  879. chia/wallet/puzzles/dao_spend_p2_singleton_v2.clsp +240 -0
  880. chia/wallet/puzzles/dao_spend_p2_singleton_v2.clsp.hex +1 -0
  881. chia/wallet/puzzles/dao_spend_p2_singleton_v2.clsp.hex.sha256tree +1 -0
  882. chia/wallet/puzzles/dao_treasury.clsp +115 -0
  883. chia/wallet/puzzles/dao_treasury.clsp.hex +1 -0
  884. chia/wallet/puzzles/dao_update_proposal.clsp +44 -0
  885. chia/wallet/puzzles/dao_update_proposal.clsp.hex +1 -0
  886. chia/wallet/puzzles/deployed_puzzle_hashes.json +67 -0
  887. chia/wallet/puzzles/json.clib +25 -0
  888. chia/wallet/puzzles/load_clvm.py +162 -0
  889. chia/wallet/puzzles/merkle_utils.clib +18 -0
  890. chia/wallet/puzzles/notification.clsp +7 -0
  891. chia/wallet/puzzles/notification.clsp.hex +1 -0
  892. chia/wallet/puzzles/p2_1_of_n.clsp +22 -0
  893. chia/wallet/puzzles/p2_1_of_n.clsp.hex +1 -0
  894. chia/wallet/puzzles/p2_conditions.clsp +3 -0
  895. chia/wallet/puzzles/p2_conditions.clsp.hex +1 -0
  896. chia/wallet/puzzles/p2_conditions.py +27 -0
  897. chia/wallet/puzzles/p2_delegated_conditions.clsp +18 -0
  898. chia/wallet/puzzles/p2_delegated_conditions.clsp.hex +1 -0
  899. chia/wallet/puzzles/p2_delegated_conditions.py +22 -0
  900. chia/wallet/puzzles/p2_delegated_puzzle.clsp +19 -0
  901. chia/wallet/puzzles/p2_delegated_puzzle.clsp.hex +1 -0
  902. chia/wallet/puzzles/p2_delegated_puzzle.py +35 -0
  903. chia/wallet/puzzles/p2_delegated_puzzle_or_hidden_puzzle.clsp +91 -0
  904. chia/wallet/puzzles/p2_delegated_puzzle_or_hidden_puzzle.clsp.hex +1 -0
  905. chia/wallet/puzzles/p2_delegated_puzzle_or_hidden_puzzle.py +161 -0
  906. chia/wallet/puzzles/p2_m_of_n_delegate_direct.clsp +108 -0
  907. chia/wallet/puzzles/p2_m_of_n_delegate_direct.clsp.hex +1 -0
  908. chia/wallet/puzzles/p2_m_of_n_delegate_direct.py +22 -0
  909. chia/wallet/puzzles/p2_parent.clsp +19 -0
  910. chia/wallet/puzzles/p2_parent.clsp.hex +1 -0
  911. chia/wallet/puzzles/p2_puzzle_hash.clsp +18 -0
  912. chia/wallet/puzzles/p2_puzzle_hash.clsp.hex +1 -0
  913. chia/wallet/puzzles/p2_puzzle_hash.py +28 -0
  914. chia/wallet/puzzles/p2_singleton.clsp +30 -0
  915. chia/wallet/puzzles/p2_singleton.clsp.hex +1 -0
  916. chia/wallet/puzzles/p2_singleton_aggregator.clsp +81 -0
  917. chia/wallet/puzzles/p2_singleton_aggregator.clsp.hex +1 -0
  918. chia/wallet/puzzles/p2_singleton_or_delayed_puzhash.clsp +50 -0
  919. chia/wallet/puzzles/p2_singleton_or_delayed_puzhash.clsp.hex +1 -0
  920. chia/wallet/puzzles/p2_singleton_via_delegated_puzzle.clsp +47 -0
  921. chia/wallet/puzzles/p2_singleton_via_delegated_puzzle.clsp.hex +1 -0
  922. chia/wallet/puzzles/puzzle_utils.py +34 -0
  923. chia/wallet/puzzles/settlement_payments.clsp +49 -0
  924. chia/wallet/puzzles/settlement_payments.clsp.hex +1 -0
  925. chia/wallet/puzzles/sha256tree.clib +11 -0
  926. chia/wallet/puzzles/singleton_launcher.clsp +16 -0
  927. chia/wallet/puzzles/singleton_launcher.clsp.hex +1 -0
  928. chia/wallet/puzzles/singleton_top_layer.clsp +177 -0
  929. chia/wallet/puzzles/singleton_top_layer.clsp.hex +1 -0
  930. chia/wallet/puzzles/singleton_top_layer.py +295 -0
  931. chia/wallet/puzzles/singleton_top_layer_v1_1.clsp +107 -0
  932. chia/wallet/puzzles/singleton_top_layer_v1_1.clsp.hex +1 -0
  933. chia/wallet/puzzles/singleton_top_layer_v1_1.py +344 -0
  934. chia/wallet/puzzles/singleton_truths.clib +21 -0
  935. chia/wallet/puzzles/tails.py +344 -0
  936. chia/wallet/puzzles/utility_macros.clib +48 -0
  937. chia/wallet/signer_protocol.py +126 -0
  938. chia/wallet/singleton.py +106 -0
  939. chia/wallet/singleton_record.py +30 -0
  940. chia/wallet/trade_manager.py +1088 -0
  941. chia/wallet/trade_record.py +67 -0
  942. chia/wallet/trading/__init__.py +0 -0
  943. chia/wallet/trading/offer.py +703 -0
  944. chia/wallet/trading/trade_status.py +13 -0
  945. chia/wallet/trading/trade_store.py +526 -0
  946. chia/wallet/transaction_record.py +143 -0
  947. chia/wallet/transaction_sorting.py +14 -0
  948. chia/wallet/uncurried_puzzle.py +17 -0
  949. chia/wallet/util/__init__.py +0 -0
  950. chia/wallet/util/address_type.py +55 -0
  951. chia/wallet/util/blind_signer_tl.py +168 -0
  952. chia/wallet/util/clvm_streamable.py +203 -0
  953. chia/wallet/util/compute_hints.py +66 -0
  954. chia/wallet/util/compute_memos.py +45 -0
  955. chia/wallet/util/curry_and_treehash.py +90 -0
  956. chia/wallet/util/debug_spend_bundle.py +234 -0
  957. chia/wallet/util/merkle_tree.py +100 -0
  958. chia/wallet/util/merkle_utils.py +102 -0
  959. chia/wallet/util/new_peak_queue.py +82 -0
  960. chia/wallet/util/notifications.py +12 -0
  961. chia/wallet/util/peer_request_cache.py +174 -0
  962. chia/wallet/util/puzzle_compression.py +96 -0
  963. chia/wallet/util/puzzle_decorator.py +100 -0
  964. chia/wallet/util/puzzle_decorator_type.py +7 -0
  965. chia/wallet/util/query_filter.py +60 -0
  966. chia/wallet/util/transaction_type.py +23 -0
  967. chia/wallet/util/tx_config.py +158 -0
  968. chia/wallet/util/wallet_sync_utils.py +348 -0
  969. chia/wallet/util/wallet_types.py +65 -0
  970. chia/wallet/vc_wallet/__init__.py +0 -0
  971. chia/wallet/vc_wallet/cr_cat_drivers.py +663 -0
  972. chia/wallet/vc_wallet/cr_cat_wallet.py +875 -0
  973. chia/wallet/vc_wallet/cr_outer_puzzle.py +102 -0
  974. chia/wallet/vc_wallet/cr_puzzles/__init__.py +0 -0
  975. chia/wallet/vc_wallet/cr_puzzles/conditions_w_fee_announce.clsp +3 -0
  976. chia/wallet/vc_wallet/cr_puzzles/conditions_w_fee_announce.clsp.hex +1 -0
  977. chia/wallet/vc_wallet/cr_puzzles/credential_restriction.clsp +304 -0
  978. chia/wallet/vc_wallet/cr_puzzles/credential_restriction.clsp.hex +1 -0
  979. chia/wallet/vc_wallet/cr_puzzles/flag_proofs_checker.clsp +45 -0
  980. chia/wallet/vc_wallet/cr_puzzles/flag_proofs_checker.clsp.hex +1 -0
  981. chia/wallet/vc_wallet/vc_drivers.py +838 -0
  982. chia/wallet/vc_wallet/vc_puzzles/__init__.py +0 -0
  983. chia/wallet/vc_wallet/vc_puzzles/covenant_layer.clsp +30 -0
  984. chia/wallet/vc_wallet/vc_puzzles/covenant_layer.clsp.hex +1 -0
  985. chia/wallet/vc_wallet/vc_puzzles/eml_covenant_morpher.clsp +75 -0
  986. chia/wallet/vc_wallet/vc_puzzles/eml_covenant_morpher.clsp.hex +1 -0
  987. chia/wallet/vc_wallet/vc_puzzles/eml_transfer_program_covenant_adapter.clsp +32 -0
  988. chia/wallet/vc_wallet/vc_puzzles/eml_transfer_program_covenant_adapter.clsp.hex +1 -0
  989. chia/wallet/vc_wallet/vc_puzzles/eml_update_metadata_with_DID.clsp +80 -0
  990. chia/wallet/vc_wallet/vc_puzzles/eml_update_metadata_with_DID.clsp.hex +1 -0
  991. chia/wallet/vc_wallet/vc_puzzles/exigent_metadata_layer.clsp +163 -0
  992. chia/wallet/vc_wallet/vc_puzzles/exigent_metadata_layer.clsp.hex +1 -0
  993. chia/wallet/vc_wallet/vc_puzzles/p2_announced_delegated_puzzle.clsp +16 -0
  994. chia/wallet/vc_wallet/vc_puzzles/p2_announced_delegated_puzzle.clsp.hex +1 -0
  995. chia/wallet/vc_wallet/vc_puzzles/standard_vc_backdoor_puzzle.clsp +74 -0
  996. chia/wallet/vc_wallet/vc_puzzles/standard_vc_backdoor_puzzle.clsp.hex +1 -0
  997. chia/wallet/vc_wallet/vc_puzzles/std_parent_morpher.clsp +23 -0
  998. chia/wallet/vc_wallet/vc_puzzles/std_parent_morpher.clsp.hex +1 -0
  999. chia/wallet/vc_wallet/vc_puzzles/viral_backdoor.clsp +64 -0
  1000. chia/wallet/vc_wallet/vc_puzzles/viral_backdoor.clsp.hex +1 -0
  1001. chia/wallet/vc_wallet/vc_store.py +263 -0
  1002. chia/wallet/vc_wallet/vc_wallet.py +638 -0
  1003. chia/wallet/wallet.py +698 -0
  1004. chia/wallet/wallet_action_scope.py +95 -0
  1005. chia/wallet/wallet_blockchain.py +244 -0
  1006. chia/wallet/wallet_coin_record.py +72 -0
  1007. chia/wallet/wallet_coin_store.py +351 -0
  1008. chia/wallet/wallet_info.py +36 -0
  1009. chia/wallet/wallet_interested_store.py +188 -0
  1010. chia/wallet/wallet_nft_store.py +279 -0
  1011. chia/wallet/wallet_node.py +1769 -0
  1012. chia/wallet/wallet_node_api.py +201 -0
  1013. chia/wallet/wallet_pool_store.py +120 -0
  1014. chia/wallet/wallet_protocol.py +90 -0
  1015. chia/wallet/wallet_puzzle_store.py +365 -0
  1016. chia/wallet/wallet_retry_store.py +70 -0
  1017. chia/wallet/wallet_singleton_store.py +258 -0
  1018. chia/wallet/wallet_spend_bundle.py +41 -0
  1019. chia/wallet/wallet_state_manager.py +2820 -0
  1020. chia/wallet/wallet_transaction_store.py +470 -0
  1021. chia/wallet/wallet_user_store.py +110 -0
  1022. chia/wallet/wallet_weight_proof_handler.py +126 -0
  1023. chia_blockchain-2.4.4.dist-info/LICENSE +201 -0
  1024. chia_blockchain-2.4.4.dist-info/METADATA +161 -0
  1025. chia_blockchain-2.4.4.dist-info/RECORD +1028 -0
  1026. chia_blockchain-2.4.4.dist-info/WHEEL +4 -0
  1027. chia_blockchain-2.4.4.dist-info/entry_points.txt +17 -0
  1028. mozilla-ca/cacert.pem +3666 -0
@@ -0,0 +1,2820 @@
1
+ from __future__ import annotations
2
+
3
+ import asyncio
4
+ import contextlib
5
+ import dataclasses
6
+ import logging
7
+ import multiprocessing.context
8
+ import time
9
+ import traceback
10
+ from contextlib import asynccontextmanager
11
+ from pathlib import Path
12
+ from typing import (
13
+ TYPE_CHECKING,
14
+ Any,
15
+ AsyncIterator,
16
+ Callable,
17
+ Dict,
18
+ Iterator,
19
+ List,
20
+ Optional,
21
+ Set,
22
+ Tuple,
23
+ Type,
24
+ TypeVar,
25
+ Union,
26
+ )
27
+
28
+ import aiosqlite
29
+ from chia_rs import AugSchemeMPL, G1Element, G2Element, PrivateKey
30
+
31
+ from chia.consensus.block_rewards import calculate_base_farmer_reward, calculate_pool_reward
32
+ from chia.consensus.coinbase import farmer_parent_id, pool_parent_id
33
+ from chia.consensus.constants import ConsensusConstants
34
+ from chia.data_layer.data_layer_wallet import DataLayerWallet
35
+ from chia.data_layer.dl_wallet_store import DataLayerStore
36
+ from chia.pools.pool_puzzles import (
37
+ SINGLETON_LAUNCHER_HASH,
38
+ get_most_recent_singleton_coin_from_coin_spend,
39
+ solution_to_pool_state,
40
+ )
41
+ from chia.pools.pool_wallet import PoolWallet
42
+ from chia.protocols.wallet_protocol import CoinState
43
+ from chia.rpc.rpc_server import StateChangedProtocol
44
+ from chia.server.outbound_message import NodeType
45
+ from chia.server.server import ChiaServer
46
+ from chia.server.ws_connection import WSChiaConnection
47
+ from chia.types.blockchain_format.coin import Coin
48
+ from chia.types.blockchain_format.program import Program
49
+ from chia.types.blockchain_format.sized_bytes import bytes32
50
+ from chia.types.coin_record import CoinRecord
51
+ from chia.types.coin_spend import CoinSpend, compute_additions
52
+ from chia.types.mempool_inclusion_status import MempoolInclusionStatus
53
+ from chia.util.bech32m import encode_puzzle_hash
54
+ from chia.util.condition_tools import conditions_dict_for_solution, pkm_pairs_for_conditions_dict
55
+ from chia.util.db_synchronous import db_synchronous_on
56
+ from chia.util.db_wrapper import DBWrapper2
57
+ from chia.util.errors import Err
58
+ from chia.util.hash import std_hash
59
+ from chia.util.ints import uint16, uint32, uint64, uint128
60
+ from chia.util.lru_cache import LRUCache
61
+ from chia.util.path import path_from_root
62
+ from chia.util.streamable import Streamable, UInt32Range, UInt64Range, VersionedBlob
63
+ from chia.wallet.cat_wallet.cat_constants import DEFAULT_CATS
64
+ from chia.wallet.cat_wallet.cat_info import CATCoinData, CATInfo, CRCATInfo
65
+ from chia.wallet.cat_wallet.cat_utils import CAT_MOD, CAT_MOD_HASH, construct_cat_puzzle, match_cat_puzzle
66
+ from chia.wallet.cat_wallet.cat_wallet import CATWallet
67
+ from chia.wallet.cat_wallet.dao_cat_wallet import DAOCATWallet
68
+ from chia.wallet.conditions import (
69
+ AssertCoinAnnouncement,
70
+ Condition,
71
+ ConditionValidTimes,
72
+ CreateCoinAnnouncement,
73
+ parse_timelock_info,
74
+ )
75
+ from chia.wallet.dao_wallet.dao_utils import (
76
+ get_p2_singleton_puzhash,
77
+ match_dao_cat_puzzle,
78
+ match_finished_puzzle,
79
+ match_funding_puzzle,
80
+ match_proposal_puzzle,
81
+ match_treasury_puzzle,
82
+ )
83
+ from chia.wallet.dao_wallet.dao_wallet import DAOWallet
84
+ from chia.wallet.db_wallet.db_wallet_puzzles import MIRROR_PUZZLE_HASH
85
+ from chia.wallet.derivation_record import DerivationRecord
86
+ from chia.wallet.derive_keys import (
87
+ _derive_path,
88
+ _derive_pk_unhardened,
89
+ master_pk_to_wallet_pk_unhardened,
90
+ master_pk_to_wallet_pk_unhardened_intermediate,
91
+ master_sk_to_wallet_sk,
92
+ master_sk_to_wallet_sk_intermediate,
93
+ master_sk_to_wallet_sk_unhardened,
94
+ )
95
+ from chia.wallet.did_wallet.did_info import DIDCoinData
96
+ from chia.wallet.did_wallet.did_wallet import DIDWallet
97
+ from chia.wallet.did_wallet.did_wallet_puzzles import DID_INNERPUZ_MOD, match_did_puzzle
98
+ from chia.wallet.key_val_store import KeyValStore
99
+ from chia.wallet.nft_wallet.nft_puzzles import get_metadata_and_phs, get_new_owner_did
100
+ from chia.wallet.nft_wallet.nft_wallet import NFTWallet
101
+ from chia.wallet.nft_wallet.uncurry_nft import NFTCoinData, UncurriedNFT
102
+ from chia.wallet.notification_manager import NotificationManager
103
+ from chia.wallet.outer_puzzles import AssetType
104
+ from chia.wallet.payment import Payment
105
+ from chia.wallet.puzzle_drivers import PuzzleInfo
106
+ from chia.wallet.puzzles.clawback.drivers import generate_clawback_spend_bundle, match_clawback_puzzle
107
+ from chia.wallet.puzzles.clawback.metadata import ClawbackMetadata, ClawbackVersion
108
+ from chia.wallet.signer_protocol import (
109
+ KeyHints,
110
+ PathHint,
111
+ SignedTransaction,
112
+ SigningInstructions,
113
+ SigningResponse,
114
+ SigningTarget,
115
+ Spend,
116
+ SumHint,
117
+ TransactionInfo,
118
+ UnsignedTransaction,
119
+ )
120
+ from chia.wallet.singleton import create_singleton_puzzle, get_inner_puzzle_from_singleton, get_singleton_id_from_puzzle
121
+ from chia.wallet.trade_manager import TradeManager
122
+ from chia.wallet.trading.offer import Offer
123
+ from chia.wallet.trading.trade_status import TradeStatus
124
+ from chia.wallet.transaction_record import TransactionRecord
125
+ from chia.wallet.uncurried_puzzle import uncurry_puzzle
126
+ from chia.wallet.util.address_type import AddressType
127
+ from chia.wallet.util.compute_hints import compute_spend_hints_and_additions
128
+ from chia.wallet.util.compute_memos import compute_memos
129
+ from chia.wallet.util.curry_and_treehash import NIL_TREEHASH
130
+ from chia.wallet.util.puzzle_decorator import PuzzleDecoratorManager
131
+ from chia.wallet.util.query_filter import HashFilter
132
+ from chia.wallet.util.transaction_type import CLAWBACK_INCOMING_TRANSACTION_TYPES, TransactionType
133
+ from chia.wallet.util.tx_config import TXConfig, TXConfigLoader
134
+ from chia.wallet.util.wallet_sync_utils import (
135
+ PeerRequestException,
136
+ fetch_coin_spend_for_coin_state,
137
+ last_change_height_cs,
138
+ )
139
+ from chia.wallet.util.wallet_types import CoinType, WalletIdentifier, WalletType
140
+ from chia.wallet.vc_wallet.cr_cat_drivers import CRCAT, ProofsChecker, construct_pending_approval_state
141
+ from chia.wallet.vc_wallet.cr_cat_wallet import CRCATWallet
142
+ from chia.wallet.vc_wallet.vc_drivers import VerifiedCredential
143
+ from chia.wallet.vc_wallet.vc_store import VCStore
144
+ from chia.wallet.vc_wallet.vc_wallet import VCWallet
145
+ from chia.wallet.wallet import Wallet
146
+ from chia.wallet.wallet_action_scope import WalletActionScope, new_wallet_action_scope
147
+ from chia.wallet.wallet_blockchain import WalletBlockchain
148
+ from chia.wallet.wallet_coin_record import MetadataTypes, WalletCoinRecord
149
+ from chia.wallet.wallet_coin_store import WalletCoinStore
150
+ from chia.wallet.wallet_info import WalletInfo
151
+ from chia.wallet.wallet_interested_store import WalletInterestedStore
152
+ from chia.wallet.wallet_nft_store import WalletNftStore
153
+ from chia.wallet.wallet_pool_store import WalletPoolStore
154
+ from chia.wallet.wallet_protocol import WalletProtocol
155
+ from chia.wallet.wallet_puzzle_store import WalletPuzzleStore
156
+ from chia.wallet.wallet_retry_store import WalletRetryStore
157
+ from chia.wallet.wallet_spend_bundle import WalletSpendBundle
158
+ from chia.wallet.wallet_transaction_store import WalletTransactionStore
159
+ from chia.wallet.wallet_user_store import WalletUserStore
160
+
161
+ TWalletType = TypeVar("TWalletType", bound=WalletProtocol[Any])
162
+
163
+ if TYPE_CHECKING:
164
+ from chia.wallet.wallet_node import WalletNode
165
+
166
+
167
+ PendingTxCallback = Callable[[], None]
168
+
169
+
170
+ class WalletStateManager:
171
+ interested_ph_cache: Dict[bytes32, List[int]] = {}
172
+ interested_coin_cache: Dict[bytes32, List[int]] = {}
173
+ constants: ConsensusConstants
174
+ config: Dict[str, Any]
175
+ tx_store: WalletTransactionStore
176
+ puzzle_store: WalletPuzzleStore
177
+ user_store: WalletUserStore
178
+ nft_store: WalletNftStore
179
+ vc_store: VCStore
180
+ basic_store: KeyValStore
181
+
182
+ # Makes sure only one asyncio thread is changing the blockchain state at one time
183
+ lock: asyncio.Lock
184
+
185
+ log: logging.Logger
186
+
187
+ # TODO Don't allow user to send tx until wallet is synced
188
+ _sync_target: Optional[uint32]
189
+
190
+ state_changed_callback: Optional[StateChangedProtocol] = None
191
+ pending_tx_callback: Optional[PendingTxCallback]
192
+ db_path: Path
193
+ db_wrapper: DBWrapper2
194
+
195
+ main_wallet: Wallet
196
+ wallets: Dict[uint32, WalletProtocol[Any]]
197
+ private_key: Optional[PrivateKey]
198
+ root_pubkey: G1Element
199
+
200
+ trade_manager: TradeManager
201
+ notification_manager: NotificationManager
202
+ blockchain: WalletBlockchain
203
+ coin_store: WalletCoinStore
204
+ interested_store: WalletInterestedStore
205
+ retry_store: WalletRetryStore
206
+ multiprocessing_context: multiprocessing.context.BaseContext
207
+ server: ChiaServer
208
+ root_path: Path
209
+ wallet_node: WalletNode
210
+ pool_store: WalletPoolStore
211
+ dl_store: DataLayerStore
212
+ default_cats: Dict[str, Any]
213
+ asset_to_wallet_map: Dict[AssetType, Any]
214
+ initial_num_public_keys: int
215
+ decorator_manager: PuzzleDecoratorManager
216
+
217
+ @staticmethod
218
+ async def create(
219
+ private_key: Optional[PrivateKey],
220
+ config: Dict[str, Any],
221
+ db_path: Path,
222
+ constants: ConsensusConstants,
223
+ server: ChiaServer,
224
+ root_path: Path,
225
+ wallet_node: WalletNode,
226
+ root_pubkey: Optional[G1Element] = None,
227
+ ) -> WalletStateManager:
228
+ self = WalletStateManager()
229
+
230
+ self.config = config
231
+ self.constants = constants
232
+ self.server = server
233
+ self.root_path = root_path
234
+ self.log = logging.getLogger(__name__)
235
+ self.lock = asyncio.Lock()
236
+ self.log.debug(f"Starting in db path: {db_path}")
237
+ sql_log_path: Optional[Path] = None
238
+ if self.config.get("log_sqlite_cmds", False):
239
+ sql_log_path = path_from_root(self.root_path, "log/wallet_sql.log")
240
+ self.log.info(f"logging SQL commands to {sql_log_path}")
241
+
242
+ self.db_wrapper = await DBWrapper2.create(
243
+ database=db_path,
244
+ reader_count=self.config.get("db_readers", 4),
245
+ log_path=sql_log_path,
246
+ synchronous=db_synchronous_on(self.config.get("db_sync", "auto")),
247
+ )
248
+
249
+ self.initial_num_public_keys = config["initial_num_public_keys"]
250
+ min_num_public_keys = 425
251
+ if not config.get("testing", False) and self.initial_num_public_keys < min_num_public_keys:
252
+ self.initial_num_public_keys = min_num_public_keys
253
+
254
+ self.coin_store = await WalletCoinStore.create(self.db_wrapper)
255
+ self.tx_store = await WalletTransactionStore.create(self.db_wrapper)
256
+ self.puzzle_store = await WalletPuzzleStore.create(self.db_wrapper)
257
+ self.user_store = await WalletUserStore.create(self.db_wrapper)
258
+ self.nft_store = await WalletNftStore.create(self.db_wrapper)
259
+ self.vc_store = await VCStore.create(self.db_wrapper)
260
+ self.basic_store = await KeyValStore.create(self.db_wrapper)
261
+ self.trade_manager = await TradeManager.create(self, self.db_wrapper)
262
+ self.notification_manager = await NotificationManager.create(self, self.db_wrapper)
263
+ self.pool_store = await WalletPoolStore.create(self.db_wrapper)
264
+ self.dl_store = await DataLayerStore.create(self.db_wrapper)
265
+ self.interested_store = await WalletInterestedStore.create(self.db_wrapper)
266
+ self.retry_store = await WalletRetryStore.create(self.db_wrapper)
267
+ self.default_cats = DEFAULT_CATS
268
+
269
+ self.wallet_node = wallet_node
270
+ self._sync_target = None
271
+ self.blockchain = await WalletBlockchain.create(self.basic_store, self.constants)
272
+ self.state_changed_callback = None
273
+ self.pending_tx_callback = None
274
+ self.db_path = db_path
275
+
276
+ main_wallet_info = await self.user_store.get_wallet_by_id(1)
277
+ assert main_wallet_info is not None
278
+
279
+ self.private_key = private_key
280
+ if private_key is None: # pragma: no cover
281
+ if root_pubkey is None:
282
+ raise ValueError("WalletStateManager requires either a root private key or root public key")
283
+ else:
284
+ self.root_pubkey = root_pubkey
285
+ else:
286
+ calculated_root_public_key: G1Element = private_key.get_g1()
287
+ if root_pubkey is not None:
288
+ assert root_pubkey == calculated_root_public_key
289
+ self.root_pubkey = calculated_root_public_key
290
+
291
+ fingerprint = self.root_pubkey.get_fingerprint()
292
+ puzzle_decorators = self.config.get("puzzle_decorators", {}).get(fingerprint, [])
293
+ self.decorator_manager = PuzzleDecoratorManager.create(puzzle_decorators)
294
+
295
+ self.main_wallet = await Wallet.create(self, main_wallet_info)
296
+
297
+ self.wallets = {main_wallet_info.id: self.main_wallet}
298
+
299
+ self.asset_to_wallet_map = {
300
+ AssetType.CAT: CATWallet,
301
+ }
302
+
303
+ wallet: Optional[WalletProtocol[Any]] = None
304
+ for wallet_info in await self.get_all_wallet_info_entries():
305
+ wallet_type = WalletType(wallet_info.type)
306
+ if wallet_type == WalletType.STANDARD_WALLET:
307
+ if wallet_info.id == 1:
308
+ continue
309
+ wallet = await Wallet.create(self, wallet_info)
310
+ elif wallet_type == WalletType.CAT:
311
+ wallet = await CATWallet.create(
312
+ self,
313
+ self.main_wallet,
314
+ wallet_info,
315
+ )
316
+ elif wallet_type == WalletType.DECENTRALIZED_ID:
317
+ wallet = await DIDWallet.create(
318
+ self,
319
+ self.main_wallet,
320
+ wallet_info,
321
+ )
322
+ elif wallet_type == WalletType.NFT:
323
+ wallet = await NFTWallet.create(
324
+ self,
325
+ self.main_wallet,
326
+ wallet_info,
327
+ )
328
+ elif wallet_type == WalletType.POOLING_WALLET:
329
+ wallet = await PoolWallet.create_from_db(
330
+ self,
331
+ self.main_wallet,
332
+ wallet_info,
333
+ )
334
+ elif wallet_type == WalletType.DATA_LAYER: # pragma: no cover
335
+ wallet = await DataLayerWallet.create(
336
+ self,
337
+ wallet_info,
338
+ )
339
+ elif wallet_type == WalletType.DAO: # pragma: no cover
340
+ wallet = await DAOWallet.create(
341
+ self,
342
+ self.main_wallet,
343
+ wallet_info,
344
+ )
345
+ elif wallet_type == WalletType.DAO_CAT: # pragma: no cover
346
+ wallet = await DAOCATWallet.create(
347
+ self,
348
+ self.main_wallet,
349
+ wallet_info,
350
+ )
351
+ elif wallet_type == WalletType.VC: # pragma: no cover
352
+ wallet = await VCWallet.create(
353
+ self,
354
+ self.main_wallet,
355
+ wallet_info,
356
+ )
357
+ elif wallet_type == WalletType.CRCAT: # pragma: no cover
358
+ wallet = await CRCATWallet.create(
359
+ self,
360
+ self.main_wallet,
361
+ wallet_info,
362
+ )
363
+ if wallet is not None:
364
+ self.wallets[wallet_info.id] = wallet
365
+
366
+ return self
367
+
368
+ def get_public_key_unhardened(self, index: uint32) -> G1Element:
369
+ return master_pk_to_wallet_pk_unhardened(self.root_pubkey, index)
370
+
371
+ async def get_private_key(self, puzzle_hash: bytes32) -> PrivateKey:
372
+ record = await self.puzzle_store.record_for_puzzle_hash(puzzle_hash)
373
+ if record is None:
374
+ raise ValueError(f"No key for puzzle hash: {puzzle_hash.hex()}")
375
+ if record.hardened:
376
+ return master_sk_to_wallet_sk(self.get_master_private_key(), record.index)
377
+ return master_sk_to_wallet_sk_unhardened(self.get_master_private_key(), record.index)
378
+
379
+ async def get_public_key(self, puzzle_hash: bytes32) -> bytes:
380
+ record = await self.puzzle_store.record_for_puzzle_hash(puzzle_hash)
381
+ if record is None:
382
+ raise ValueError(f"No key for puzzle hash: {puzzle_hash.hex()}")
383
+ if isinstance(record._pubkey, bytes):
384
+ pk_bytes = record._pubkey
385
+ else:
386
+ pk_bytes = bytes(record._pubkey)
387
+ return pk_bytes
388
+
389
+ def get_master_private_key(self) -> PrivateKey:
390
+ if self.private_key is None: # pragma: no cover
391
+ raise ValueError("Wallet is currently in observer mode and access to private key is denied")
392
+
393
+ return self.private_key
394
+
395
+ def get_wallet(self, id: uint32, required_type: Type[TWalletType]) -> TWalletType:
396
+ wallet = self.wallets[id]
397
+ if not isinstance(wallet, required_type):
398
+ raise Exception(
399
+ f"wallet id {id} is of type {type(wallet).__name__} but type {required_type.__name__} is required",
400
+ )
401
+
402
+ return wallet
403
+
404
+ async def create_more_puzzle_hashes(
405
+ self,
406
+ from_zero: bool = False,
407
+ mark_existing_as_used: bool = True,
408
+ up_to_index: Optional[uint32] = None,
409
+ num_additional_phs: Optional[int] = None,
410
+ ) -> None:
411
+ """
412
+ For all wallets in the user store, generates the first few puzzle hashes so
413
+ that we can restore the wallet from only the private keys.
414
+ """
415
+ targets = list(self.wallets.keys())
416
+ self.log.debug("Target wallets to generate puzzle hashes for: %s", repr(targets))
417
+ unused: Optional[uint32] = (
418
+ uint32(up_to_index + 1) if up_to_index is not None else await self.puzzle_store.get_unused_derivation_path()
419
+ )
420
+ if unused is None:
421
+ # This handles the case where the database has entries but they have all been used
422
+ unused = await self.puzzle_store.get_last_derivation_path()
423
+ self.log.debug("Tried finding unused: %s", unused)
424
+ if unused is None:
425
+ # This handles the case where the database is empty
426
+ unused = uint32(0)
427
+
428
+ self.log.debug(f"Requested to generate puzzle hashes to at least index {unused}")
429
+ start_t = time.time()
430
+ to_generate = num_additional_phs if num_additional_phs is not None else self.initial_num_public_keys
431
+
432
+ # iterate all wallets that need derived keys and establish the start
433
+ # index for all of them
434
+ start_index_by_wallet: Dict[uint32, int] = {}
435
+ last_index = unused + to_generate
436
+ for wallet_id in targets:
437
+ target_wallet = self.wallets[wallet_id]
438
+ if not target_wallet.require_derivation_paths():
439
+ self.log.debug("Skipping wallet %s as no derivation paths required", wallet_id)
440
+ continue
441
+ if from_zero:
442
+ start_index_by_wallet[wallet_id] = 0
443
+ continue
444
+ last: Optional[uint32] = await self.puzzle_store.get_last_derivation_path_for_wallet(wallet_id)
445
+ if last is not None:
446
+ if last + 1 >= last_index:
447
+ self.log.debug(f"Nothing to create for for wallet_id: {wallet_id}, index: {last_index}")
448
+ continue
449
+ start_index_by_wallet[wallet_id] = last + 1
450
+ else:
451
+ start_index_by_wallet[wallet_id] = 0
452
+
453
+ if len(start_index_by_wallet) == 0:
454
+ return
455
+
456
+ lowest_start_index = min(start_index_by_wallet.values())
457
+
458
+ # now derive the keysfrom lowest_start_index to last_index
459
+ # these maps derivation index to public key
460
+ hardened_keys: Dict[int, G1Element] = {}
461
+ unhardened_keys: Dict[int, G1Element] = {}
462
+
463
+ if self.private_key is not None:
464
+ # Hardened
465
+ intermediate_sk = master_sk_to_wallet_sk_intermediate(self.private_key)
466
+ for index in range(lowest_start_index, last_index):
467
+ hardened_keys[index] = _derive_path(intermediate_sk, [index]).get_g1()
468
+
469
+ # Unhardened
470
+ intermediate_pk_un = master_pk_to_wallet_pk_unhardened_intermediate(self.root_pubkey)
471
+ for index in range(lowest_start_index, last_index):
472
+ unhardened_keys[index] = _derive_pk_unhardened(intermediate_pk_un, [index])
473
+
474
+ for wallet_id, start_index in start_index_by_wallet.items():
475
+ target_wallet = self.wallets[wallet_id]
476
+ assert target_wallet.type() != WalletType.POOLING_WALLET
477
+ assert start_index < last_index
478
+
479
+ derivation_paths: List[DerivationRecord] = []
480
+ creating_msg = f"Creating puzzle hashes from {start_index} to {last_index - 1} for wallet_id: {wallet_id}"
481
+ self.log.info(f"Start: {creating_msg}")
482
+ for index in range(start_index, last_index):
483
+ pubkey: Optional[G1Element] = hardened_keys.get(index)
484
+ if pubkey is not None:
485
+ # Hardened
486
+ puzzlehash: bytes32 = target_wallet.puzzle_hash_for_pk(pubkey)
487
+ self.log.debug(f"Puzzle at index {index} wallet ID {wallet_id} puzzle hash {puzzlehash.hex()}")
488
+ derivation_paths.append(
489
+ DerivationRecord(
490
+ uint32(index),
491
+ puzzlehash,
492
+ pubkey,
493
+ target_wallet.type(),
494
+ uint32(target_wallet.id()),
495
+ True,
496
+ )
497
+ )
498
+ # Unhardened
499
+ pubkey = unhardened_keys.get(index)
500
+ assert pubkey is not None
501
+ puzzlehash_unhardened: bytes32 = target_wallet.puzzle_hash_for_pk(pubkey)
502
+ self.log.debug(
503
+ f"Puzzle at index {index} wallet ID {wallet_id} puzzle hash {puzzlehash_unhardened.hex()}"
504
+ )
505
+ derivation_paths.append(
506
+ DerivationRecord(
507
+ uint32(index),
508
+ puzzlehash_unhardened,
509
+ pubkey,
510
+ target_wallet.type(),
511
+ uint32(target_wallet.id()),
512
+ False,
513
+ )
514
+ )
515
+ self.log.info(f"Done: {creating_msg} Time: {time.time() - start_t} seconds")
516
+ if len(derivation_paths) > 0:
517
+ await self.puzzle_store.add_derivation_paths(derivation_paths)
518
+ if wallet_id == self.main_wallet.id():
519
+ await self.wallet_node.new_peak_queue.subscribe_to_puzzle_hashes(
520
+ [record.puzzle_hash for record in derivation_paths]
521
+ )
522
+ if len(unhardened_keys) > 0:
523
+ self.state_changed("new_derivation_index", data_object={"index": last_index - 1})
524
+ # By default, we'll mark previously generated unused puzzle hashes as used if we have new paths
525
+ if mark_existing_as_used and unused > 0 and len(unhardened_keys) > 0:
526
+ self.log.info(f"Updating last used derivation index: {unused - 1}")
527
+ await self.puzzle_store.set_used_up_to(uint32(unused - 1))
528
+
529
+ async def update_wallet_puzzle_hashes(self, wallet_id: uint32) -> None:
530
+ derivation_paths: List[DerivationRecord] = []
531
+ target_wallet = self.wallets[wallet_id]
532
+ last: Optional[uint32] = await self.puzzle_store.get_last_derivation_path_for_wallet(wallet_id)
533
+ unused: Optional[uint32] = await self.puzzle_store.get_unused_derivation_path()
534
+ if unused is None:
535
+ # This handles the case where the database has entries but they have all been used
536
+ unused = await self.puzzle_store.get_last_derivation_path()
537
+ if unused is None:
538
+ # This handles the case where the database is empty
539
+ unused = uint32(0)
540
+ if last is not None:
541
+ for index in range(unused, last):
542
+ # Since DID are not released yet we can assume they are only using unhardened keys derivation
543
+ pubkey: G1Element = self.get_public_key_unhardened(uint32(index))
544
+ puzzlehash = target_wallet.puzzle_hash_for_pk(pubkey)
545
+ self.log.info(f"Generating public key at index {index} puzzle hash {puzzlehash.hex()}")
546
+ derivation_paths.append(
547
+ DerivationRecord(
548
+ uint32(index),
549
+ puzzlehash,
550
+ pubkey,
551
+ WalletType(target_wallet.wallet_info.type),
552
+ uint32(target_wallet.wallet_info.id),
553
+ False,
554
+ )
555
+ )
556
+ await self.puzzle_store.add_derivation_paths(derivation_paths)
557
+
558
+ async def get_unused_derivation_record(self, wallet_id: uint32, *, hardened: bool = False) -> DerivationRecord:
559
+ """
560
+ Creates a puzzle hash for the given wallet, and then makes more puzzle hashes
561
+ for every wallet to ensure we always have more in the database. Never reusue the
562
+ same public key more than once (for privacy).
563
+ """
564
+ async with self.puzzle_store.lock:
565
+ # If we have no unused public keys, we will create new ones
566
+ unused: Optional[uint32] = await self.puzzle_store.get_unused_derivation_path()
567
+ if unused is None:
568
+ self.log.debug("No unused paths, generate more ")
569
+ await self.create_more_puzzle_hashes()
570
+ # Now we must have unused public keys
571
+ unused = await self.puzzle_store.get_unused_derivation_path()
572
+ assert unused is not None
573
+
574
+ self.log.debug("Fetching derivation record for: %s %s %s", unused, wallet_id, hardened)
575
+ record: Optional[DerivationRecord] = await self.puzzle_store.get_derivation_record(
576
+ unused, wallet_id, hardened
577
+ )
578
+ if record is None:
579
+ raise ValueError(f"Missing derivation '{unused}' for wallet id '{wallet_id}' (hardened={hardened})")
580
+
581
+ # Set this key to used so we never use it again
582
+ await self.puzzle_store.set_used_up_to(record.index)
583
+
584
+ # Create more puzzle hashes / keys
585
+ await self.create_more_puzzle_hashes()
586
+ return record
587
+
588
+ async def get_current_derivation_record_for_wallet(self, wallet_id: uint32) -> Optional[DerivationRecord]:
589
+ async with self.puzzle_store.lock:
590
+ # If we have no unused public keys, we will create new ones
591
+ current: Optional[DerivationRecord] = await self.puzzle_store.get_current_derivation_record_for_wallet(
592
+ wallet_id
593
+ )
594
+ return current
595
+
596
+ def set_callback(self, callback: StateChangedProtocol) -> None:
597
+ """
598
+ Callback to be called when the state of the wallet changes.
599
+ """
600
+ self.state_changed_callback = callback
601
+
602
+ def set_pending_callback(self, callback: PendingTxCallback) -> None:
603
+ """
604
+ Callback to be called when new pending transaction enters the store
605
+ """
606
+ self.pending_tx_callback = callback
607
+
608
+ def state_changed(
609
+ self, state: str, wallet_id: Optional[int] = None, data_object: Optional[Dict[str, Any]] = None
610
+ ) -> None:
611
+ """
612
+ Calls the callback if it's present.
613
+ """
614
+ if self.state_changed_callback is None:
615
+ return None
616
+ change_data: Dict[str, Any] = {"state": state}
617
+ if wallet_id is not None:
618
+ change_data["wallet_id"] = wallet_id
619
+ if data_object is not None:
620
+ change_data["additional_data"] = data_object
621
+ self.state_changed_callback(state, change_data)
622
+
623
+ def tx_pending_changed(self) -> None:
624
+ """
625
+ Notifies the wallet node that there's new tx pending
626
+ """
627
+ if self.pending_tx_callback is None:
628
+ return None
629
+
630
+ self.pending_tx_callback()
631
+
632
+ async def synced(self, block_is_current_at: Optional[int] = None) -> bool:
633
+ if block_is_current_at is None:
634
+ block_is_current_at = int(time.time() - 60 * 5)
635
+ if len(self.server.get_connections(NodeType.FULL_NODE)) == 0:
636
+ return False
637
+
638
+ latest = await self.blockchain.get_peak_block()
639
+ if latest is None:
640
+ return False
641
+
642
+ if "simulator" in self.config.get("selected_network", ""):
643
+ return True # sim is always synced if we have a genesis block.
644
+
645
+ if latest.height - await self.blockchain.get_finished_sync_up_to() > 1:
646
+ return False
647
+
648
+ latest_timestamp = self.blockchain.get_latest_timestamp()
649
+ has_pending_queue_items = self.wallet_node.new_peak_queue.has_pending_data_process_items()
650
+
651
+ if latest_timestamp > block_is_current_at and not has_pending_queue_items:
652
+ return True
653
+ return False
654
+
655
+ @property
656
+ def sync_mode(self) -> bool:
657
+ return self._sync_target is not None
658
+
659
+ @property
660
+ def sync_target(self) -> Optional[uint32]:
661
+ return self._sync_target
662
+
663
+ @asynccontextmanager
664
+ async def set_sync_mode(self, target_height: uint32) -> AsyncIterator[uint32]:
665
+ if self.log.level == logging.DEBUG:
666
+ self.log.debug(f"set_sync_mode enter {await self.blockchain.get_finished_sync_up_to()}-{target_height}")
667
+ async with self.lock:
668
+ self._sync_target = target_height
669
+ start_time = time.time()
670
+ start_height = await self.blockchain.get_finished_sync_up_to()
671
+ self.log.info(f"set_sync_mode syncing - range: {start_height}-{target_height}")
672
+ self.state_changed("sync_changed")
673
+ try:
674
+ yield start_height
675
+ except Exception:
676
+ self.log.exception(
677
+ f"set_sync_mode failed - range: {start_height}-{target_height}, seconds: {time.time() - start_time}"
678
+ )
679
+ finally:
680
+ self.state_changed("sync_changed")
681
+ if self.log.level == logging.DEBUG:
682
+ self.log.debug(
683
+ f"set_sync_mode exit - range: {start_height}-{target_height}, "
684
+ f"get_finished_sync_up_to: {await self.blockchain.get_finished_sync_up_to()}, "
685
+ f"seconds: {time.time() - start_time}"
686
+ )
687
+ self._sync_target = None
688
+
689
+ async def get_confirmed_spendable_balance_for_wallet(
690
+ self, wallet_id: int, unspent_records: Optional[Set[WalletCoinRecord]] = None
691
+ ) -> uint128:
692
+ """
693
+ Returns the balance amount of all coins that are spendable.
694
+ """
695
+
696
+ spendable: Set[WalletCoinRecord] = await self.get_spendable_coins_for_wallet(wallet_id, unspent_records)
697
+
698
+ spendable_amount: uint128 = uint128(0)
699
+ for record in spendable:
700
+ spendable_amount = uint128(spendable_amount + record.coin.amount)
701
+
702
+ return spendable_amount
703
+
704
+ async def does_coin_belong_to_wallet(
705
+ self, coin: Coin, wallet_id: int, hint_dict: Dict[bytes32, bytes32] = {}
706
+ ) -> bool:
707
+ """
708
+ Returns true if we have the key for this coin.
709
+ """
710
+ wallet_identifier = await self.get_wallet_identifier_for_coin(coin, hint_dict)
711
+ return wallet_identifier is not None and wallet_identifier.id == wallet_id
712
+
713
+ async def get_confirmed_balance_for_wallet(
714
+ self,
715
+ wallet_id: int,
716
+ unspent_coin_records: Optional[Set[WalletCoinRecord]] = None,
717
+ ) -> uint128:
718
+ """
719
+ Returns the confirmed balance, including coinbase rewards that are not spendable.
720
+ """
721
+ # lock only if unspent_coin_records is None
722
+ if unspent_coin_records is None:
723
+ if self.wallets[uint32(wallet_id)].type() == WalletType.CRCAT:
724
+ coin_type = CoinType.CRCAT
725
+ else:
726
+ coin_type = CoinType.NORMAL
727
+ unspent_coin_records = await self.coin_store.get_unspent_coins_for_wallet(wallet_id, coin_type)
728
+ return uint128(sum(cr.coin.amount for cr in unspent_coin_records))
729
+
730
+ async def get_unconfirmed_balance(
731
+ self, wallet_id: int, unspent_coin_records: Optional[Set[WalletCoinRecord]] = None
732
+ ) -> uint128:
733
+ """
734
+ Returns the balance, including coinbase rewards that are not spendable, and unconfirmed
735
+ transactions.
736
+ """
737
+ # This API should change so that get_balance_from_coin_records is called for Set[WalletCoinRecord]
738
+ # and this method is called only for the unspent_coin_records==None case.
739
+ if unspent_coin_records is None:
740
+ wallet_type: WalletType = self.wallets[uint32(wallet_id)].type()
741
+ if wallet_type == WalletType.CRCAT:
742
+ unspent_coin_records = await self.coin_store.get_unspent_coins_for_wallet(wallet_id, CoinType.CRCAT)
743
+ pending_crcat = await self.coin_store.get_unspent_coins_for_wallet(wallet_id, CoinType.CRCAT_PENDING)
744
+ unspent_coin_records = unspent_coin_records.union(pending_crcat)
745
+ else:
746
+ unspent_coin_records = await self.coin_store.get_unspent_coins_for_wallet(wallet_id)
747
+
748
+ unconfirmed_tx: List[TransactionRecord] = await self.tx_store.get_unconfirmed_for_wallet(wallet_id)
749
+ all_unspent_coins: Set[Coin] = {cr.coin for cr in unspent_coin_records}
750
+
751
+ for record in unconfirmed_tx:
752
+ if record.type in CLAWBACK_INCOMING_TRANSACTION_TYPES:
753
+ # We do not wish to consider clawback-able funds as unconfirmed.
754
+ # That is reserved for when the action to actually claw a tx back or forward is initiated.
755
+ continue
756
+ for addition in record.additions:
757
+ # This change or a self transaction
758
+ if await self.does_coin_belong_to_wallet(addition, wallet_id, record.hint_dict()):
759
+ all_unspent_coins.add(addition)
760
+
761
+ for removal in record.removals:
762
+ if (
763
+ await self.does_coin_belong_to_wallet(removal, wallet_id, record.hint_dict())
764
+ and removal in all_unspent_coins
765
+ ):
766
+ all_unspent_coins.remove(removal)
767
+
768
+ return uint128(sum(coin.amount for coin in all_unspent_coins))
769
+
770
+ async def unconfirmed_removals_for_wallet(self, wallet_id: int) -> Dict[bytes32, Coin]:
771
+ """
772
+ Returns new removals transactions that have not been confirmed yet.
773
+ """
774
+ removals: Dict[bytes32, Coin] = {}
775
+ unconfirmed_tx = await self.tx_store.get_unconfirmed_for_wallet(wallet_id)
776
+ for record in unconfirmed_tx:
777
+ if record.type in CLAWBACK_INCOMING_TRANSACTION_TYPES:
778
+ # We do not wish to consider clawback-able funds as pending removal.
779
+ # That is reserved for when the action to actually claw a tx back or forward is initiated.
780
+ continue
781
+ for coin in record.removals:
782
+ removals[coin.name()] = coin
783
+ trade_removals: Dict[bytes32, WalletCoinRecord] = await self.trade_manager.get_locked_coins()
784
+ return {**removals, **{coin_id: cr.coin for coin_id, cr in trade_removals.items() if cr.wallet_id == wallet_id}}
785
+
786
+ async def determine_coin_type(
787
+ self, peer: WSChiaConnection, coin_state: CoinState, fork_height: Optional[uint32]
788
+ ) -> Tuple[Optional[WalletIdentifier], Optional[Streamable]]:
789
+ if coin_state.created_height is not None and (
790
+ self.is_pool_reward(uint32(coin_state.created_height), coin_state.coin)
791
+ or self.is_farmer_reward(uint32(coin_state.created_height), coin_state.coin)
792
+ ):
793
+ return None, None
794
+
795
+ response: List[CoinState] = await self.wallet_node.get_coin_state(
796
+ [coin_state.coin.parent_coin_info], peer=peer, fork_height=fork_height
797
+ )
798
+ if len(response) == 0:
799
+ self.log.warning(f"Could not find a parent coin with ID: {coin_state.coin.parent_coin_info.hex()}")
800
+ return None, None
801
+ parent_coin_state = response[0]
802
+ assert parent_coin_state.spent_height == coin_state.created_height
803
+
804
+ coin_spend = await fetch_coin_spend_for_coin_state(parent_coin_state, peer)
805
+
806
+ uncurried = uncurry_puzzle(coin_spend.puzzle_reveal)
807
+
808
+ dao_ids = []
809
+ wallets = self.wallets.values()
810
+ for wallet in wallets:
811
+ if wallet.type() == WalletType.DAO.value:
812
+ assert isinstance(wallet, DAOWallet)
813
+ dao_ids.append(wallet.dao_info.treasury_id)
814
+ funding_puzzle_check = match_funding_puzzle(
815
+ uncurried, coin_spend.solution.to_program(), coin_state.coin, dao_ids
816
+ )
817
+ if funding_puzzle_check:
818
+ return await self.get_dao_wallet_from_coinspend_hint(coin_spend, coin_state), None
819
+
820
+ # Check if the coin is a DAO Treasury
821
+ dao_curried_args = match_treasury_puzzle(uncurried.mod, uncurried.args)
822
+ if dao_curried_args is not None:
823
+ return await self.handle_dao_treasury(dao_curried_args, parent_coin_state, coin_state, coin_spend), None
824
+ # Check if the coin is a Proposal and that it isn't the timer coin (amount == 0)
825
+ dao_curried_args = match_proposal_puzzle(uncurried.mod, uncurried.args)
826
+ if (dao_curried_args is not None) and (coin_state.coin.amount != 0):
827
+ return await self.handle_dao_proposal(dao_curried_args, parent_coin_state, coin_state, coin_spend), None
828
+
829
+ # Check if the coin is a finished proposal
830
+ dao_curried_args = match_finished_puzzle(uncurried.mod, uncurried.args)
831
+ if dao_curried_args is not None:
832
+ return (
833
+ await self.handle_dao_finished_proposals(dao_curried_args, parent_coin_state, coin_state, coin_spend),
834
+ None,
835
+ )
836
+
837
+ # Check if the coin is a DAO CAT
838
+ dao_cat_args = match_dao_cat_puzzle(uncurried)
839
+ if dao_cat_args:
840
+ return await self.handle_dao_cat(dao_cat_args, parent_coin_state, coin_state, coin_spend, fork_height), None
841
+
842
+ # Check if the coin is a CAT
843
+ cat_curried_args = match_cat_puzzle(uncurried)
844
+ if cat_curried_args is not None:
845
+ cat_mod_hash, tail_program_hash, cat_inner_puzzle = cat_curried_args
846
+ cat_data: CATCoinData = CATCoinData(
847
+ bytes32(cat_mod_hash.as_atom()),
848
+ bytes32(tail_program_hash.as_atom()),
849
+ cat_inner_puzzle,
850
+ parent_coin_state.coin.parent_coin_info,
851
+ uint64(parent_coin_state.coin.amount),
852
+ )
853
+ return (
854
+ await self.handle_cat(
855
+ cat_data,
856
+ parent_coin_state,
857
+ coin_state,
858
+ coin_spend,
859
+ peer,
860
+ fork_height,
861
+ ),
862
+ cat_data,
863
+ )
864
+
865
+ # Check if the coin is a NFT
866
+ # hint
867
+ # First spend where 1 mojo coin -> Singleton launcher -> NFT -> NFT
868
+ uncurried_nft = UncurriedNFT.uncurry(uncurried.mod, uncurried.args)
869
+ if uncurried_nft is not None and coin_state.coin.amount % 2 == 1:
870
+ nft_data = NFTCoinData(uncurried_nft, parent_coin_state, coin_spend)
871
+ return await self.handle_nft(nft_data), nft_data
872
+
873
+ # Check if the coin is a DID
874
+ did_curried_args = match_did_puzzle(uncurried.mod, uncurried.args)
875
+ if did_curried_args is not None and coin_state.coin.amount % 2 == 1:
876
+ p2_puzzle, recovery_list_hash, num_verification, singleton_struct, metadata = did_curried_args
877
+ did_data: DIDCoinData = DIDCoinData(
878
+ p2_puzzle,
879
+ bytes32(recovery_list_hash.as_atom()),
880
+ uint16(num_verification.as_int()),
881
+ singleton_struct,
882
+ metadata,
883
+ get_inner_puzzle_from_singleton(coin_spend.puzzle_reveal),
884
+ parent_coin_state,
885
+ )
886
+ return await self.handle_did(did_data, parent_coin_state, coin_state, coin_spend, peer), did_data
887
+
888
+ # Check if the coin is clawback
889
+ clawback_coin_data = match_clawback_puzzle(uncurried, coin_spend.puzzle_reveal, coin_spend.solution)
890
+ if clawback_coin_data is not None:
891
+ return await self.handle_clawback(clawback_coin_data, coin_state, coin_spend, peer), clawback_coin_data
892
+
893
+ # Check if the coin is a VC
894
+ is_vc, err_msg = VerifiedCredential.is_vc(uncurried)
895
+ if is_vc:
896
+ vc: VerifiedCredential = VerifiedCredential.get_next_from_coin_spend(coin_spend)
897
+ return await self.handle_vc(vc), vc
898
+
899
+ await self.notification_manager.potentially_add_new_notification(coin_state, coin_spend)
900
+
901
+ return None, None
902
+
903
+ async def auto_claim_coins(self) -> None:
904
+ # Get unspent clawback coin
905
+ current_timestamp = self.blockchain.get_latest_timestamp()
906
+ clawback_coins: Dict[Coin, ClawbackMetadata] = {}
907
+ tx_fee = uint64(self.config.get("auto_claim", {}).get("tx_fee", 0))
908
+ assert self.wallet_node.logged_in_fingerprint is not None
909
+ tx_config_loader: TXConfigLoader = TXConfigLoader.from_json_dict(self.config.get("auto_claim", {}))
910
+ if tx_config_loader.min_coin_amount is None:
911
+ tx_config_loader = tx_config_loader.override(
912
+ min_coin_amount=self.config.get("auto_claim", {}).get("min_amount"),
913
+ )
914
+ tx_config: TXConfig = tx_config_loader.autofill(
915
+ constants=self.constants,
916
+ config=self.config,
917
+ logged_in_fingerprint=self.wallet_node.logged_in_fingerprint,
918
+ )
919
+ unspent_coins = await self.coin_store.get_coin_records(
920
+ coin_type=CoinType.CLAWBACK,
921
+ wallet_type=WalletType.STANDARD_WALLET,
922
+ spent_range=UInt32Range(stop=uint32(0)),
923
+ amount_range=UInt64Range(
924
+ start=tx_config.coin_selection_config.min_coin_amount,
925
+ stop=tx_config.coin_selection_config.max_coin_amount,
926
+ ),
927
+ )
928
+ async with self.new_action_scope(tx_config, push=True) as action_scope:
929
+ for coin in unspent_coins.records:
930
+ try:
931
+ metadata: MetadataTypes = coin.parsed_metadata()
932
+ assert isinstance(metadata, ClawbackMetadata)
933
+ if await metadata.is_recipient(self.puzzle_store):
934
+ coin_timestamp = await self.wallet_node.get_timestamp_for_height(coin.confirmed_block_height)
935
+ if current_timestamp - coin_timestamp >= metadata.time_lock:
936
+ clawback_coins[coin.coin] = metadata
937
+ if len(clawback_coins) >= self.config.get("auto_claim", {}).get("batch_size", 50):
938
+ await self.spend_clawback_coins(clawback_coins, tx_fee, action_scope)
939
+ async with action_scope.use() as interface:
940
+ # TODO: editing this is not ideal, action scopes should know what coins are spent
941
+ action_scope._config = dataclasses.replace(
942
+ action_scope._config,
943
+ tx_config=dataclasses.replace(
944
+ action_scope._config.tx_config,
945
+ excluded_coin_ids=[
946
+ *action_scope.config.tx_config.excluded_coin_ids,
947
+ *(
948
+ c.name()
949
+ for tx in interface.side_effects.transactions
950
+ for c in tx.removals
951
+ ),
952
+ ],
953
+ ),
954
+ )
955
+ clawback_coins = {}
956
+ except Exception as e:
957
+ self.log.error(f"Failed to claim clawback coin {coin.coin.name().hex()}: %s", e)
958
+ if len(clawback_coins) > 0:
959
+ await self.spend_clawback_coins(clawback_coins, tx_fee, action_scope)
960
+
961
+ async def spend_clawback_coins(
962
+ self,
963
+ clawback_coins: Dict[Coin, ClawbackMetadata],
964
+ fee: uint64,
965
+ action_scope: WalletActionScope,
966
+ force: bool = False,
967
+ extra_conditions: Tuple[Condition, ...] = tuple(),
968
+ ) -> None:
969
+ assert len(clawback_coins) > 0
970
+ coin_spends: List[CoinSpend] = []
971
+ message: bytes32 = std_hash(b"".join([c.name() for c in clawback_coins.keys()]))
972
+ now: uint64 = uint64(int(time.time()))
973
+ derivation_record: Optional[DerivationRecord] = None
974
+ amount: uint64 = uint64(0)
975
+ for coin, metadata in clawback_coins.items():
976
+ try:
977
+ self.log.info(f"Claiming clawback coin {coin.name().hex()}")
978
+ # Get incoming tx
979
+ incoming_tx = await self.tx_store.get_transaction_record(coin.name())
980
+ assert incoming_tx is not None, f"Cannot find incoming tx for clawback coin {coin.name().hex()}"
981
+ if incoming_tx.sent > 0 and not force:
982
+ self.log.error(
983
+ f"Clawback coin {coin.name().hex()} is already in a pending spend bundle. {incoming_tx}"
984
+ )
985
+ continue
986
+
987
+ recipient_puzhash: bytes32 = metadata.recipient_puzzle_hash
988
+ sender_puzhash: bytes32 = metadata.sender_puzzle_hash
989
+ is_recipient: bool = await metadata.is_recipient(self.puzzle_store)
990
+ if is_recipient:
991
+ derivation_record = await self.puzzle_store.get_derivation_record_for_puzzle_hash(recipient_puzhash)
992
+ else:
993
+ derivation_record = await self.puzzle_store.get_derivation_record_for_puzzle_hash(sender_puzhash)
994
+ assert derivation_record is not None
995
+ amount = uint64(amount + coin.amount)
996
+ # Remove the clawback hint since it is unnecessary for the XCH coin
997
+ memos: List[bytes] = [] if len(incoming_tx.memos) == 0 else incoming_tx.memos[0][1][1:]
998
+ inner_puzzle: Program = self.main_wallet.puzzle_for_pk(derivation_record.pubkey)
999
+ inner_solution: Program = self.main_wallet.make_solution(
1000
+ primaries=[
1001
+ Payment(
1002
+ derivation_record.puzzle_hash,
1003
+ uint64(coin.amount),
1004
+ memos, # Forward memo of the first coin
1005
+ )
1006
+ ],
1007
+ conditions=(
1008
+ extra_conditions
1009
+ if len(coin_spends) > 0 or fee == 0
1010
+ else (*extra_conditions, CreateCoinAnnouncement(message))
1011
+ ),
1012
+ )
1013
+ coin_spend: CoinSpend = generate_clawback_spend_bundle(coin, metadata, inner_puzzle, inner_solution)
1014
+ coin_spends.append(coin_spend)
1015
+ # Update incoming tx to prevent double spend and mark it is pending
1016
+ await self.tx_store.increment_sent(incoming_tx.name, "", MempoolInclusionStatus.PENDING, None)
1017
+ except Exception as e:
1018
+ self.log.error(f"Failed to create clawback spend bundle for {coin.name().hex()}: {e}")
1019
+ if len(coin_spends) == 0:
1020
+ return
1021
+ spend_bundle = WalletSpendBundle(coin_spends, G2Element())
1022
+ if fee > 0:
1023
+ async with self.new_action_scope(action_scope.config.tx_config, push=False) as inner_action_scope:
1024
+ await self.main_wallet.create_tandem_xch_tx(
1025
+ fee,
1026
+ inner_action_scope,
1027
+ extra_conditions=(
1028
+ AssertCoinAnnouncement(asserted_id=coin_spends[0].coin.name(), asserted_msg=message),
1029
+ ),
1030
+ )
1031
+ async with action_scope.use() as interface:
1032
+ # This should not be looked to for best practice. Ideally, the two spend bundles can exist separately on
1033
+ # each tx record until they are pushed. This is not very supported behavior at the moment so to avoid
1034
+ # any potential backwards compatibility issues, we're moving the spend bundle from this TX to the main
1035
+ interface.side_effects.transactions.extend(
1036
+ [dataclasses.replace(tx, spend_bundle=None) for tx in inner_action_scope.side_effects.transactions]
1037
+ )
1038
+ spend_bundle = WalletSpendBundle.aggregate(
1039
+ [
1040
+ spend_bundle,
1041
+ *(
1042
+ tx.spend_bundle
1043
+ for tx in inner_action_scope.side_effects.transactions
1044
+ if tx.spend_bundle is not None
1045
+ ),
1046
+ ]
1047
+ )
1048
+ assert derivation_record is not None
1049
+ tx_record = TransactionRecord(
1050
+ confirmed_at_height=uint32(0),
1051
+ created_at_time=now,
1052
+ to_puzzle_hash=derivation_record.puzzle_hash,
1053
+ amount=amount,
1054
+ fee_amount=uint64(fee),
1055
+ confirmed=False,
1056
+ sent=uint32(0),
1057
+ spend_bundle=spend_bundle,
1058
+ additions=spend_bundle.additions(),
1059
+ removals=spend_bundle.removals(),
1060
+ wallet_id=uint32(1),
1061
+ sent_to=[],
1062
+ trade_id=None,
1063
+ type=uint32(TransactionType.OUTGOING_CLAWBACK),
1064
+ name=spend_bundle.name(),
1065
+ memos=list(compute_memos(spend_bundle).items()),
1066
+ valid_times=parse_timelock_info(extra_conditions),
1067
+ )
1068
+ async with action_scope.use() as interface:
1069
+ interface.side_effects.transactions.append(tx_record)
1070
+
1071
+ async def filter_spam(self, new_coin_state: List[CoinState]) -> List[CoinState]:
1072
+ xch_spam_amount = self.config.get("xch_spam_amount", 1000000)
1073
+
1074
+ # No need to filter anything if the filter is set to 1 or 0 mojos
1075
+ if xch_spam_amount <= 1:
1076
+ return new_coin_state
1077
+
1078
+ spam_filter_after_n_txs = self.config.get("spam_filter_after_n_txs", 200)
1079
+ small_unspent_count = await self.coin_store.count_small_unspent(xch_spam_amount)
1080
+
1081
+ # if small_unspent_count > spam_filter_after_n_txs:
1082
+ filtered_cs: List[CoinState] = []
1083
+ is_standard_wallet_phs: Set[bytes32] = set()
1084
+
1085
+ for cs in new_coin_state:
1086
+ # Only apply filter to new coins being sent to our wallet, that are very small
1087
+ if (
1088
+ cs.created_height is not None
1089
+ and cs.spent_height is None
1090
+ and cs.coin.amount < xch_spam_amount
1091
+ and (cs.coin.puzzle_hash in is_standard_wallet_phs or await self.is_standard_wallet_tx(cs))
1092
+ ):
1093
+ is_standard_wallet_phs.add(cs.coin.puzzle_hash)
1094
+ if small_unspent_count < spam_filter_after_n_txs:
1095
+ filtered_cs.append(cs)
1096
+ small_unspent_count += 1
1097
+ else:
1098
+ filtered_cs.append(cs)
1099
+ return filtered_cs
1100
+
1101
+ async def is_standard_wallet_tx(self, coin_state: CoinState) -> bool:
1102
+ wallet_identifier = await self.get_wallet_identifier_for_puzzle_hash(coin_state.coin.puzzle_hash)
1103
+ return wallet_identifier is not None and wallet_identifier.type == WalletType.STANDARD_WALLET
1104
+
1105
+ async def handle_dao_cat(
1106
+ self,
1107
+ curried_args: Iterator[Program],
1108
+ parent_coin_state: CoinState,
1109
+ coin_state: CoinState,
1110
+ coin_spend: CoinSpend,
1111
+ fork_height: Optional[uint32],
1112
+ ) -> Optional[WalletIdentifier]:
1113
+ """
1114
+ Handle the new coin when it is a DAO CAT
1115
+ """
1116
+ mod_hash, tail_hash, inner_puzzle = curried_args
1117
+ asset_id: bytes32 = bytes32(bytes(tail_hash)[1:])
1118
+ for wallet in self.wallets.values():
1119
+ if wallet.type() == WalletType.DAO_CAT:
1120
+ assert isinstance(wallet, DAOCATWallet)
1121
+ if wallet.dao_cat_info.limitations_program_hash == asset_id:
1122
+ return WalletIdentifier.create(wallet)
1123
+ # Found a DAO_CAT, but we don't have a wallet for it. Add to unacknowledged
1124
+ await self.interested_store.add_unacknowledged_token(
1125
+ asset_id,
1126
+ CATWallet.default_wallet_name_for_unknown_cat(asset_id.hex()),
1127
+ None if parent_coin_state.spent_height is None else uint32(parent_coin_state.spent_height),
1128
+ parent_coin_state.coin.puzzle_hash,
1129
+ )
1130
+ await self.interested_store.add_unacknowledged_coin_state(
1131
+ asset_id,
1132
+ coin_state,
1133
+ fork_height,
1134
+ )
1135
+ self.state_changed("added_stray_cat")
1136
+ return None # pragma: no cover
1137
+
1138
+ async def handle_cat(
1139
+ self,
1140
+ parent_data: CATCoinData,
1141
+ parent_coin_state: CoinState,
1142
+ coin_state: CoinState,
1143
+ coin_spend: CoinSpend,
1144
+ peer: WSChiaConnection,
1145
+ fork_height: Optional[uint32],
1146
+ ) -> Optional[WalletIdentifier]:
1147
+ """
1148
+ Handle the new coin when it is a CAT
1149
+ :param parent_data: Parent CAT coin uncurried metadata
1150
+ :param parent_coin_state: Parent coin state
1151
+ :param coin_state: Current coin state
1152
+ :param coin_spend: New coin spend
1153
+ :param fork_height: Current block height
1154
+ :return: Wallet ID & Wallet Type
1155
+ """
1156
+ hinted_coin = compute_spend_hints_and_additions(coin_spend)[0][coin_state.coin.name()]
1157
+ assert hinted_coin.hint is not None, f"hint missing for coin {hinted_coin.coin}"
1158
+ derivation_record = await self.puzzle_store.get_derivation_record_for_puzzle_hash(hinted_coin.hint)
1159
+
1160
+ if derivation_record is None:
1161
+ self.log.info(f"Received state for the coin that doesn't belong to us {coin_state}")
1162
+ return None
1163
+ else:
1164
+ our_inner_puzzle: Program = self.main_wallet.puzzle_for_pk(derivation_record.pubkey)
1165
+ asset_id: bytes32 = parent_data.tail_program_hash
1166
+ cat_puzzle = construct_cat_puzzle(CAT_MOD, asset_id, our_inner_puzzle, CAT_MOD_HASH)
1167
+ is_crcat: bool = False
1168
+ if cat_puzzle.get_tree_hash() != coin_state.coin.puzzle_hash:
1169
+ # Check if it is a CRCAT
1170
+ if CRCAT.is_cr_cat(uncurry_puzzle(coin_spend.puzzle_reveal)):
1171
+ is_crcat = True
1172
+ else:
1173
+ return None # pragma: no cover
1174
+ if is_crcat:
1175
+ # Since CRCAT wallet doesn't have derivation path, every CRCAT will go through this code path
1176
+ crcat: CRCAT = next(
1177
+ crc for crc in CRCAT.get_next_from_coin_spend(coin_spend) if crc.coin == coin_state.coin
1178
+ )
1179
+
1180
+ # Make sure we control the inner puzzle or we control it if it's wrapped in the pending state
1181
+ if (
1182
+ await self.puzzle_store.get_derivation_record_for_puzzle_hash(crcat.inner_puzzle_hash) is None
1183
+ and crcat.inner_puzzle_hash
1184
+ != construct_pending_approval_state(
1185
+ hinted_coin.hint,
1186
+ uint64(coin_state.coin.amount),
1187
+ ).get_tree_hash()
1188
+ ):
1189
+ self.log.error(f"Unknown CRCAT inner puzzle, coin ID:{crcat.coin.name().hex()}") # pragma: no cover
1190
+ return None # pragma: no cover
1191
+
1192
+ # Check if we already have a wallet
1193
+ for wallet_info in await self.get_all_wallet_info_entries(wallet_type=WalletType.CRCAT):
1194
+ crcat_info: CRCATInfo = CRCATInfo.from_bytes(bytes.fromhex(wallet_info.data))
1195
+ if crcat_info.limitations_program_hash == asset_id:
1196
+ return WalletIdentifier(wallet_info.id, WalletType(wallet_info.type))
1197
+
1198
+ # We didn't find a matching CR-CAT wallet, but maybe we have a matching CAT wallet that we can convert
1199
+ for wallet_info in await self.get_all_wallet_info_entries(wallet_type=WalletType.CAT):
1200
+ cat_info: CATInfo = CATInfo.from_bytes(bytes.fromhex(wallet_info.data))
1201
+ found_cat_wallet = self.wallets[wallet_info.id]
1202
+ assert isinstance(found_cat_wallet, CATWallet)
1203
+ if cat_info.limitations_program_hash == crcat.tail_hash:
1204
+ await CRCATWallet.convert_to_cr(
1205
+ found_cat_wallet,
1206
+ crcat.authorized_providers,
1207
+ ProofsChecker.from_program(uncurry_puzzle(crcat.proofs_checker)),
1208
+ )
1209
+ self.state_changed("converted cat wallet to cr", wallet_info.id)
1210
+ return WalletIdentifier(wallet_info.id, WalletType(WalletType.CRCAT))
1211
+ if parent_data.tail_program_hash.hex() in self.default_cats or self.config.get(
1212
+ "automatically_add_unknown_cats", False
1213
+ ):
1214
+ if is_crcat:
1215
+ cat_wallet: Union[CATWallet, CRCATWallet] = await CRCATWallet.get_or_create_wallet_for_cat(
1216
+ self,
1217
+ self.main_wallet,
1218
+ crcat.tail_hash.hex(),
1219
+ authorized_providers=crcat.authorized_providers,
1220
+ proofs_checker=ProofsChecker.from_program(uncurry_puzzle(crcat.proofs_checker)),
1221
+ )
1222
+ else:
1223
+ cat_wallet = await CATWallet.get_or_create_wallet_for_cat(
1224
+ self, self.main_wallet, parent_data.tail_program_hash.hex()
1225
+ )
1226
+ return WalletIdentifier.create(cat_wallet)
1227
+ else:
1228
+ # Found unacknowledged CAT, save it in the database.
1229
+ await self.interested_store.add_unacknowledged_token(
1230
+ asset_id,
1231
+ CATWallet.default_wallet_name_for_unknown_cat(asset_id.hex()),
1232
+ None if parent_coin_state.spent_height is None else uint32(parent_coin_state.spent_height),
1233
+ parent_coin_state.coin.puzzle_hash,
1234
+ )
1235
+ await self.interested_store.add_unacknowledged_coin_state(
1236
+ asset_id,
1237
+ coin_state,
1238
+ fork_height,
1239
+ )
1240
+ self.state_changed("added_stray_cat")
1241
+ return None
1242
+
1243
+ async def handle_did(
1244
+ self,
1245
+ parent_data: DIDCoinData,
1246
+ parent_coin_state: CoinState,
1247
+ coin_state: CoinState,
1248
+ coin_spend: CoinSpend,
1249
+ peer: WSChiaConnection,
1250
+ ) -> Optional[WalletIdentifier]:
1251
+ """
1252
+ Handle the new coin when it is a DID
1253
+ :param parent_data: Curried data of the DID coin
1254
+ :param parent_coin_state: Parent coin state
1255
+ :param coin_state: Current coin state
1256
+ :param coin_spend: New coin spend
1257
+ :return: Wallet ID & Wallet Type
1258
+ """
1259
+
1260
+ inner_puzzle_hash = parent_data.p2_puzzle.get_tree_hash()
1261
+ self.log.info(f"parent: {parent_coin_state.coin.name()} inner_puzzle_hash for parent is {inner_puzzle_hash}")
1262
+
1263
+ hinted_coin = compute_spend_hints_and_additions(coin_spend)[0][coin_state.coin.name()]
1264
+ assert hinted_coin.hint is not None, f"hint missing for coin {hinted_coin.coin}"
1265
+ derivation_record = await self.puzzle_store.get_derivation_record_for_puzzle_hash(hinted_coin.hint)
1266
+
1267
+ launch_id = bytes32(parent_data.singleton_struct.rest().first().as_atom())
1268
+ if derivation_record is None:
1269
+ self.log.info(f"Received state for the coin that doesn't belong to us {coin_state}")
1270
+ # Check if it was owned by us
1271
+ # If the puzzle inside is no longer recognised then delete the wallet associated
1272
+ removed_wallet_ids = []
1273
+ for wallet in self.wallets.values():
1274
+ if not isinstance(wallet, DIDWallet):
1275
+ continue
1276
+ if (
1277
+ wallet.did_info.origin_coin is not None
1278
+ and launch_id == wallet.did_info.origin_coin.name()
1279
+ and not wallet.did_info.sent_recovery_transaction
1280
+ ):
1281
+ await self.user_store.delete_wallet(wallet.id())
1282
+ removed_wallet_ids.append(wallet.id())
1283
+ for remove_id in removed_wallet_ids:
1284
+ self.wallets.pop(remove_id)
1285
+ self.log.info(f"Removed DID wallet {remove_id}, Launch_ID: {launch_id.hex()}")
1286
+ self.state_changed("wallet_removed", remove_id)
1287
+ return None
1288
+ else:
1289
+ our_inner_puzzle: Program = self.main_wallet.puzzle_for_pk(derivation_record.pubkey)
1290
+
1291
+ self.log.info(f"Found DID, launch_id {launch_id}.")
1292
+ did_puzzle = DID_INNERPUZ_MOD.curry(
1293
+ our_inner_puzzle,
1294
+ parent_data.recovery_list_hash,
1295
+ parent_data.num_verification,
1296
+ parent_data.singleton_struct,
1297
+ parent_data.metadata,
1298
+ )
1299
+ full_puzzle = create_singleton_puzzle(did_puzzle, launch_id)
1300
+ did_puzzle_empty_recovery = DID_INNERPUZ_MOD.curry(
1301
+ our_inner_puzzle,
1302
+ NIL_TREEHASH,
1303
+ uint64(0),
1304
+ parent_data.singleton_struct,
1305
+ parent_data.metadata,
1306
+ )
1307
+ full_puzzle_empty_recovery = create_singleton_puzzle(did_puzzle_empty_recovery, launch_id)
1308
+ if full_puzzle.get_tree_hash() != coin_state.coin.puzzle_hash:
1309
+ if full_puzzle_empty_recovery.get_tree_hash() == coin_state.coin.puzzle_hash:
1310
+ did_puzzle = did_puzzle_empty_recovery
1311
+ self.log.info("DID recovery list was reset by the previous owner.")
1312
+ else:
1313
+ self.log.error("DID puzzle hash doesn't match, please check curried parameters.")
1314
+ return None
1315
+ # Create DID wallet
1316
+ response: List[CoinState] = await self.wallet_node.get_coin_state([launch_id], peer=peer)
1317
+ if len(response) == 0:
1318
+ self.log.warning(f"Could not find the launch coin with ID: {launch_id}")
1319
+ return None
1320
+ launch_coin: CoinState = response[0]
1321
+ origin_coin = launch_coin.coin
1322
+
1323
+ for wallet in self.wallets.values():
1324
+ if wallet.type() == WalletType.DECENTRALIZED_ID:
1325
+ assert isinstance(wallet, DIDWallet)
1326
+ assert wallet.did_info.origin_coin is not None
1327
+ if origin_coin.name() == wallet.did_info.origin_coin.name():
1328
+ return WalletIdentifier.create(wallet)
1329
+ if coin_state.spent_height is not None:
1330
+ # The first coin we received for DID wallet is spent.
1331
+ # This means the wallet is in a resync process, skip the coin
1332
+ return None
1333
+ did_wallet = await DIDWallet.create_new_did_wallet_from_coin_spend(
1334
+ self,
1335
+ self.main_wallet,
1336
+ launch_coin.coin,
1337
+ did_puzzle,
1338
+ coin_spend,
1339
+ f"DID {encode_puzzle_hash(launch_id, AddressType.DID.hrp(self.config))}",
1340
+ )
1341
+ wallet_identifier = WalletIdentifier.create(did_wallet)
1342
+ self.state_changed("wallet_created", wallet_identifier.id, {"did_id": did_wallet.get_my_DID()})
1343
+ return wallet_identifier
1344
+
1345
+ async def get_minter_did(self, launcher_coin: Coin, peer: WSChiaConnection) -> Optional[bytes32]:
1346
+ # Get minter DID
1347
+ eve_coin = (await self.wallet_node.fetch_children(launcher_coin.name(), peer=peer))[0]
1348
+ eve_coin_spend = await fetch_coin_spend_for_coin_state(eve_coin, peer)
1349
+ eve_full_puzzle: Program = Program.from_bytes(bytes(eve_coin_spend.puzzle_reveal))
1350
+ eve_uncurried_nft: Optional[UncurriedNFT] = UncurriedNFT.uncurry(*eve_full_puzzle.uncurry())
1351
+ if eve_uncurried_nft is None:
1352
+ raise ValueError("Couldn't get minter DID for NFT")
1353
+ if not eve_uncurried_nft.supports_did:
1354
+ return None
1355
+ minter_did = get_new_owner_did(eve_uncurried_nft, eve_coin_spend.solution.to_program())
1356
+ if minter_did == b"":
1357
+ minter_did = None
1358
+ if minter_did is None:
1359
+ # Check if the NFT is a bulk minting
1360
+ launcher_parent: List[CoinState] = await self.wallet_node.get_coin_state(
1361
+ [launcher_coin.parent_coin_info], peer=peer
1362
+ )
1363
+ assert (
1364
+ launcher_parent is not None
1365
+ and len(launcher_parent) == 1
1366
+ and launcher_parent[0].spent_height is not None
1367
+ )
1368
+ # NFTs minted out of coinbase coins would not have minter DIDs
1369
+ if self.constants.GENESIS_CHALLENGE[:16] in bytes(
1370
+ launcher_parent[0].coin.parent_coin_info
1371
+ ) or self.constants.GENESIS_CHALLENGE[16:] in bytes(launcher_parent[0].coin.parent_coin_info):
1372
+ return None
1373
+ did_coin: List[CoinState] = await self.wallet_node.get_coin_state(
1374
+ [launcher_parent[0].coin.parent_coin_info], peer=peer
1375
+ )
1376
+ assert did_coin is not None and len(did_coin) == 1 and did_coin[0].spent_height is not None
1377
+ did_spend = await fetch_coin_spend_for_coin_state(did_coin[0], peer)
1378
+ uncurried = uncurry_puzzle(did_spend.puzzle_reveal)
1379
+ did_curried_args = match_did_puzzle(uncurried.mod, uncurried.args)
1380
+ if did_curried_args is not None:
1381
+ p2_puzzle, recovery_list_hash, num_verification, singleton_struct, metadata = did_curried_args
1382
+ minter_did = bytes32(bytes(singleton_struct.rest().first())[1:])
1383
+ return minter_did
1384
+
1385
+ async def handle_dao_treasury(
1386
+ self,
1387
+ uncurried_args: Iterator[Program],
1388
+ parent_coin_state: CoinState,
1389
+ coin_state: CoinState,
1390
+ coin_spend: CoinSpend,
1391
+ ) -> Optional[WalletIdentifier]:
1392
+ self.log.info("Entering dao_treasury handling in WalletStateManager")
1393
+ singleton_id = get_singleton_id_from_puzzle(coin_spend.puzzle_reveal)
1394
+ for wallet in self.wallets.values():
1395
+ if wallet.type() == WalletType.DAO:
1396
+ assert isinstance(wallet, DAOWallet)
1397
+ if wallet.dao_info.treasury_id == singleton_id:
1398
+ return WalletIdentifier.create(wallet)
1399
+
1400
+ # TODO: If we can't find the wallet for this DAO but we've got here because we're subscribed,
1401
+ # then create the wallet. (see early in dao-wallet commits for how to do this)
1402
+ return None # pragma: no cover
1403
+
1404
+ async def handle_dao_proposal(
1405
+ self,
1406
+ uncurried_args: Iterator[Program],
1407
+ parent_coin_state: CoinState,
1408
+ coin_state: CoinState,
1409
+ coin_spend: CoinSpend,
1410
+ ) -> Optional[WalletIdentifier]:
1411
+ (
1412
+ # ; second hash
1413
+ SELF_HASH,
1414
+ PROPOSAL_ID,
1415
+ PROPOSED_PUZ_HASH,
1416
+ YES_VOTES,
1417
+ TOTAL_VOTES,
1418
+ # ; first hash
1419
+ PROPOSAL_TIMER_MOD_HASH,
1420
+ SINGLETON_MOD_HASH,
1421
+ SINGLETON_LAUNCHER_PUZHASH,
1422
+ CAT_MOD_HASH,
1423
+ DAO_FINISHED_STATE_MOD_HASH,
1424
+ TREASURY_MOD_HASH,
1425
+ LOCKUP_SELF_HASH,
1426
+ CAT_TAIL_HASH,
1427
+ TREASURY_ID,
1428
+ ) = uncurried_args
1429
+ for wallet in self.wallets.values():
1430
+ if wallet.type() == WalletType.DAO:
1431
+ assert isinstance(wallet, DAOWallet)
1432
+ if wallet.dao_info.treasury_id == TREASURY_ID.as_atom():
1433
+ assert isinstance(coin_state.created_height, int)
1434
+ await wallet.add_or_update_proposal_info(coin_spend, uint32(coin_state.created_height))
1435
+ return WalletIdentifier.create(wallet)
1436
+ return None # pragma: no cover
1437
+
1438
+ async def handle_dao_finished_proposals(
1439
+ self,
1440
+ uncurried_args: Iterator[Program],
1441
+ parent_coin_state: CoinState,
1442
+ coin_state: CoinState,
1443
+ coin_spend: CoinSpend,
1444
+ ) -> Optional[WalletIdentifier]:
1445
+ if coin_state.created_height is None: # pragma: no cover
1446
+ raise ValueError("coin_state argument to handle_dao_finished_proposals cannot have created_height of None")
1447
+ (
1448
+ SINGLETON_STRUCT, # (SINGLETON_MOD_HASH, (SINGLETON_ID, LAUNCHER_PUZZLE_HASH))
1449
+ FINISHED_STATE_MOD_HASH,
1450
+ ) = uncurried_args
1451
+ proposal_id = SINGLETON_STRUCT.rest().first().as_atom()
1452
+ for wallet in self.wallets.values():
1453
+ if wallet.type() == WalletType.DAO:
1454
+ assert isinstance(wallet, DAOWallet)
1455
+ for proposal_info in wallet.dao_info.proposals_list:
1456
+ if proposal_info.proposal_id == proposal_id:
1457
+ await wallet.add_or_update_proposal_info(coin_spend, uint32(coin_state.created_height))
1458
+ return WalletIdentifier.create(wallet)
1459
+ return None
1460
+
1461
+ async def get_dao_wallet_from_coinspend_hint(
1462
+ self, coin_spend: CoinSpend, coin_state: CoinState
1463
+ ) -> Optional[WalletIdentifier]:
1464
+ hinted_coin = compute_spend_hints_and_additions(coin_spend)[0][coin_state.coin.name()]
1465
+ if hinted_coin:
1466
+ for wallet in self.wallets.values():
1467
+ if wallet.type() == WalletType.DAO.value:
1468
+ assert isinstance(wallet, DAOWallet)
1469
+ if get_p2_singleton_puzhash(wallet.dao_info.treasury_id) == hinted_coin.hint:
1470
+ return WalletIdentifier.create(wallet)
1471
+ return None
1472
+
1473
+ async def handle_nft(
1474
+ self,
1475
+ nft_data: NFTCoinData,
1476
+ ) -> Optional[WalletIdentifier]:
1477
+ """
1478
+ Handle the new coin when it is a NFT
1479
+ :param nft_data: all necessary data to process a NFT coin
1480
+ :return: Wallet ID & Wallet Type
1481
+ """
1482
+ wallet_identifier = None
1483
+ # DID ID determines which NFT wallet should process the NFT
1484
+ new_did_id: Optional[bytes32] = None
1485
+ old_did_id = None
1486
+ # P2 puzzle hash determines if we should ignore the NFT
1487
+ uncurried_nft: UncurriedNFT = nft_data.uncurried_nft
1488
+ old_p2_puzhash = uncurried_nft.p2_puzzle.get_tree_hash()
1489
+ metadata, new_p2_puzhash = get_metadata_and_phs(
1490
+ uncurried_nft,
1491
+ nft_data.parent_coin_spend.solution,
1492
+ )
1493
+ if uncurried_nft.supports_did:
1494
+ _new_did_id = get_new_owner_did(uncurried_nft, nft_data.parent_coin_spend.solution.to_program())
1495
+ old_did_id = uncurried_nft.owner_did
1496
+ if _new_did_id is None:
1497
+ new_did_id = old_did_id
1498
+ elif _new_did_id == b"":
1499
+ new_did_id = None
1500
+ else:
1501
+ new_did_id = _new_did_id
1502
+ self.log.debug(
1503
+ "Handling NFT: %s, old DID:%s, new DID:%s, old P2:%s, new P2:%s",
1504
+ nft_data.parent_coin_spend,
1505
+ old_did_id,
1506
+ new_did_id,
1507
+ old_p2_puzhash,
1508
+ new_p2_puzhash,
1509
+ )
1510
+ new_derivation_record: Optional[DerivationRecord] = (
1511
+ await self.puzzle_store.get_derivation_record_for_puzzle_hash(new_p2_puzhash)
1512
+ )
1513
+ old_derivation_record: Optional[DerivationRecord] = (
1514
+ await self.puzzle_store.get_derivation_record_for_puzzle_hash(old_p2_puzhash)
1515
+ )
1516
+ if new_derivation_record is None and old_derivation_record is None:
1517
+ self.log.debug(
1518
+ "Cannot find a P2 puzzle hash for NFT:%s, this NFT belongs to others.",
1519
+ uncurried_nft.singleton_launcher_id.hex(),
1520
+ )
1521
+ return wallet_identifier
1522
+ for nft_wallet in self.wallets.copy().values():
1523
+ if not isinstance(nft_wallet, NFTWallet):
1524
+ continue
1525
+ if nft_wallet.nft_wallet_info.did_id == old_did_id and old_derivation_record is not None:
1526
+ self.log.info(
1527
+ "Removing old NFT, NFT_ID:%s, DID_ID:%s",
1528
+ uncurried_nft.singleton_launcher_id.hex(),
1529
+ old_did_id,
1530
+ )
1531
+ if nft_data.parent_coin_state.spent_height is not None:
1532
+ await nft_wallet.remove_coin(
1533
+ nft_data.parent_coin_spend.coin, uint32(nft_data.parent_coin_state.spent_height)
1534
+ )
1535
+ is_empty = await nft_wallet.is_empty()
1536
+ has_did = False
1537
+ for did_wallet in self.wallets.values():
1538
+ if not isinstance(did_wallet, DIDWallet):
1539
+ continue
1540
+ assert did_wallet.did_info.origin_coin is not None
1541
+ if did_wallet.did_info.origin_coin.name() == old_did_id:
1542
+ has_did = True
1543
+ break
1544
+ if is_empty and nft_wallet.did_id is not None and not has_did:
1545
+ self.log.info(f"No NFT, deleting wallet {nft_wallet.did_id.hex()} ...")
1546
+ await self.user_store.delete_wallet(nft_wallet.wallet_info.id)
1547
+ self.wallets.pop(nft_wallet.wallet_info.id)
1548
+ if nft_wallet.nft_wallet_info.did_id == new_did_id and new_derivation_record is not None:
1549
+ self.log.info(
1550
+ "Adding new NFT, NFT_ID:%s, DID_ID:%s",
1551
+ uncurried_nft.singleton_launcher_id.hex(),
1552
+ new_did_id,
1553
+ )
1554
+ wallet_identifier = WalletIdentifier.create(nft_wallet)
1555
+
1556
+ if wallet_identifier is None and new_derivation_record is not None:
1557
+ # Cannot find an existed NFT wallet for the new NFT
1558
+ self.log.info(
1559
+ "Cannot find a NFT wallet for NFT_ID: %s DID_ID: %s, creating a new one.",
1560
+ uncurried_nft.singleton_launcher_id,
1561
+ new_did_id,
1562
+ )
1563
+ new_nft_wallet: NFTWallet = await NFTWallet.create_new_nft_wallet(
1564
+ self, self.main_wallet, did_id=new_did_id, name="NFT Wallet"
1565
+ )
1566
+ wallet_identifier = WalletIdentifier.create(new_nft_wallet)
1567
+ return wallet_identifier
1568
+
1569
+ async def handle_clawback(
1570
+ self,
1571
+ metadata: ClawbackMetadata,
1572
+ coin_state: CoinState,
1573
+ coin_spend: CoinSpend,
1574
+ peer: WSChiaConnection,
1575
+ ) -> Optional[WalletIdentifier]:
1576
+ """
1577
+ Handle Clawback coins
1578
+ :param metadata: Clawback metadata for spending the merkle coin
1579
+ :param coin_state: Clawback merkle coin
1580
+ :param coin_spend: Parent coin spend
1581
+ :param peer: Fullnode peer
1582
+ :return:
1583
+ """
1584
+ # Record metadata
1585
+ assert coin_state.created_height is not None
1586
+ is_recipient: Optional[bool] = None
1587
+ # Check if the wallet is the sender
1588
+ sender_derivation_record: Optional[DerivationRecord] = (
1589
+ await self.puzzle_store.get_derivation_record_for_puzzle_hash(metadata.sender_puzzle_hash)
1590
+ )
1591
+ # Check if the wallet is the recipient
1592
+ recipient_derivation_record = await self.puzzle_store.get_derivation_record_for_puzzle_hash(
1593
+ metadata.recipient_puzzle_hash
1594
+ )
1595
+ if sender_derivation_record is not None:
1596
+ self.log.info("Found Clawback merkle coin %s as the sender.", coin_state.coin.name().hex())
1597
+ is_recipient = False
1598
+ elif recipient_derivation_record is not None:
1599
+ self.log.info("Found Clawback merkle coin %s as the recipient.", coin_state.coin.name().hex())
1600
+ is_recipient = True
1601
+ # For the recipient we need to manually subscribe the merkle coin
1602
+ await self.add_interested_coin_ids([coin_state.coin.name()])
1603
+ if is_recipient is not None:
1604
+ spend_bundle = WalletSpendBundle([coin_spend], G2Element())
1605
+ memos = compute_memos(spend_bundle)
1606
+ spent_height: uint32 = uint32(0)
1607
+ if coin_state.spent_height is not None:
1608
+ self.log.debug("Resync clawback coin: %s", coin_state.coin.name().hex())
1609
+ # Resync case
1610
+ spent_height = uint32(coin_state.spent_height)
1611
+ # Create Clawback outgoing transaction
1612
+ created_timestamp = await self.wallet_node.get_timestamp_for_height(uint32(coin_state.spent_height))
1613
+ clawback_coin_spend: CoinSpend = await fetch_coin_spend_for_coin_state(coin_state, peer)
1614
+ clawback_spend_bundle = WalletSpendBundle([clawback_coin_spend], G2Element())
1615
+ if await self.puzzle_store.puzzle_hash_exists(clawback_spend_bundle.additions()[0].puzzle_hash):
1616
+ tx_record = TransactionRecord(
1617
+ confirmed_at_height=uint32(coin_state.spent_height),
1618
+ created_at_time=created_timestamp,
1619
+ to_puzzle_hash=(
1620
+ metadata.sender_puzzle_hash
1621
+ if clawback_spend_bundle.additions()[0].puzzle_hash == metadata.sender_puzzle_hash
1622
+ else metadata.recipient_puzzle_hash
1623
+ ),
1624
+ amount=uint64(coin_state.coin.amount),
1625
+ fee_amount=uint64(0),
1626
+ confirmed=True,
1627
+ sent=uint32(0),
1628
+ spend_bundle=clawback_spend_bundle,
1629
+ additions=clawback_spend_bundle.additions(),
1630
+ removals=clawback_spend_bundle.removals(),
1631
+ wallet_id=uint32(1),
1632
+ sent_to=[],
1633
+ trade_id=None,
1634
+ type=uint32(TransactionType.OUTGOING_CLAWBACK),
1635
+ name=clawback_spend_bundle.name(),
1636
+ memos=list(compute_memos(clawback_spend_bundle).items()),
1637
+ valid_times=ConditionValidTimes(),
1638
+ )
1639
+ await self.tx_store.add_transaction_record(tx_record)
1640
+ coin_record = WalletCoinRecord(
1641
+ coin_state.coin,
1642
+ uint32(coin_state.created_height),
1643
+ spent_height,
1644
+ spent_height != 0,
1645
+ False,
1646
+ WalletType.STANDARD_WALLET,
1647
+ 1,
1648
+ CoinType.CLAWBACK,
1649
+ VersionedBlob(ClawbackVersion.V1.value, bytes(metadata)),
1650
+ )
1651
+ # Add merkle coin
1652
+ await self.coin_store.add_coin_record(coin_record)
1653
+ # Add tx record
1654
+ # We use TransactionRecord.confirmed to indicate if a Clawback transaction is claimable
1655
+ # If the Clawback coin is unspent, confirmed should be false
1656
+ created_timestamp = await self.wallet_node.get_timestamp_for_height(uint32(coin_state.created_height))
1657
+ tx_record = TransactionRecord(
1658
+ confirmed_at_height=uint32(coin_state.created_height),
1659
+ created_at_time=uint64(created_timestamp),
1660
+ to_puzzle_hash=metadata.recipient_puzzle_hash,
1661
+ amount=uint64(coin_state.coin.amount),
1662
+ fee_amount=uint64(0),
1663
+ confirmed=spent_height != 0,
1664
+ sent=uint32(0),
1665
+ spend_bundle=None,
1666
+ additions=[coin_state.coin],
1667
+ removals=[coin_spend.coin],
1668
+ wallet_id=uint32(1),
1669
+ sent_to=[],
1670
+ trade_id=None,
1671
+ type=uint32(
1672
+ TransactionType.INCOMING_CLAWBACK_RECEIVE
1673
+ if is_recipient
1674
+ else TransactionType.INCOMING_CLAWBACK_SEND
1675
+ ),
1676
+ # Use coin ID as the TX ID to mapping with the coin table
1677
+ name=coin_record.coin.name(),
1678
+ memos=list(memos.items()),
1679
+ valid_times=ConditionValidTimes(),
1680
+ )
1681
+ await self.tx_store.add_transaction_record(tx_record)
1682
+ return None
1683
+
1684
+ async def handle_vc(self, vc: VerifiedCredential) -> Optional[WalletIdentifier]:
1685
+ # Check the ownership
1686
+ derivation_record: Optional[DerivationRecord] = await self.puzzle_store.get_derivation_record_for_puzzle_hash(
1687
+ vc.inner_puzzle_hash
1688
+ )
1689
+ if derivation_record is None:
1690
+ self.log.warning(
1691
+ f"Verified credential {vc.launcher_id.hex()} is not belong to the current wallet."
1692
+ ) # pragma: no cover
1693
+ return None # pragma: no cover
1694
+ self.log.info(f"Found verified credential {vc.launcher_id.hex()}.")
1695
+ for wallet_info in await self.get_all_wallet_info_entries(wallet_type=WalletType.VC):
1696
+ return WalletIdentifier(wallet_info.id, WalletType.VC)
1697
+ else:
1698
+ # Create a new VC wallet
1699
+ vc_wallet = await VCWallet.create_new_vc_wallet(self, self.main_wallet) # pragma: no cover
1700
+ return WalletIdentifier(vc_wallet.id(), WalletType.VC) # pragma: no cover
1701
+
1702
+ async def _add_coin_states(
1703
+ self,
1704
+ coin_states: List[CoinState],
1705
+ peer: WSChiaConnection,
1706
+ fork_height: Optional[uint32],
1707
+ ) -> None:
1708
+ # TODO: add comment about what this method does
1709
+ # Input states should already be sorted by cs_height, with reorgs at the beginning
1710
+ curr_h = -1
1711
+ for c_state in coin_states:
1712
+ last_change_height = last_change_height_cs(c_state)
1713
+ if last_change_height < curr_h:
1714
+ raise ValueError("Input coin_states is not sorted properly")
1715
+ curr_h = last_change_height
1716
+
1717
+ trade_removals = await self.trade_manager.get_coins_of_interest()
1718
+ all_unconfirmed: List[TransactionRecord] = await self.tx_store.get_all_unconfirmed()
1719
+ used_up_to = -1
1720
+ ph_to_index_cache: LRUCache[bytes32, uint32] = LRUCache(100)
1721
+
1722
+ coin_names = [bytes32(coin_state.coin.name()) for coin_state in coin_states]
1723
+ local_records = await self.coin_store.get_coin_records(coin_id_filter=HashFilter.include(coin_names))
1724
+
1725
+ for coin_name, coin_state in zip(coin_names, coin_states):
1726
+ if peer.closed:
1727
+ raise ConnectionError("Connection closed")
1728
+ self.log.debug("Add coin state: %s: %s", coin_name, coin_state)
1729
+ local_record = local_records.coin_id_to_record.get(coin_name)
1730
+ rollback_wallets = None
1731
+ try:
1732
+ async with self.db_wrapper.writer():
1733
+ rollback_wallets = self.wallets.copy() # Shallow copy of wallets if writer rolls back the db
1734
+ # This only succeeds if we don't raise out of the transaction
1735
+ await self.retry_store.remove_state(coin_state)
1736
+
1737
+ wallet_identifier = await self.get_wallet_identifier_for_puzzle_hash(coin_state.coin.puzzle_hash)
1738
+ coin_data: Optional[Streamable] = None
1739
+ # If we already have this coin, & it was spent & confirmed at the same heights, then return (done)
1740
+ if local_record is not None:
1741
+ local_spent = None
1742
+ if local_record.spent_block_height != 0:
1743
+ local_spent = local_record.spent_block_height
1744
+ if (
1745
+ local_spent == coin_state.spent_height
1746
+ and local_record.confirmed_block_height == coin_state.created_height
1747
+ ):
1748
+ continue
1749
+
1750
+ if coin_state.spent_height is not None and coin_name in trade_removals:
1751
+ await self.trade_manager.coins_of_interest_farmed(coin_state, fork_height, peer)
1752
+ if wallet_identifier is not None:
1753
+ self.log.debug(f"Found existing wallet_identifier: {wallet_identifier}, coin: {coin_name}")
1754
+ elif local_record is not None:
1755
+ wallet_identifier = WalletIdentifier(uint32(local_record.wallet_id), local_record.wallet_type)
1756
+ elif coin_state.created_height is not None:
1757
+ wallet_identifier, coin_data = await self.determine_coin_type(peer, coin_state, fork_height)
1758
+ try:
1759
+ dl_wallet = self.get_dl_wallet()
1760
+ except ValueError:
1761
+ pass
1762
+ else:
1763
+ if (
1764
+ await dl_wallet.get_singleton_record(coin_name) is not None
1765
+ or coin_state.coin.puzzle_hash == MIRROR_PUZZLE_HASH
1766
+ ):
1767
+ wallet_identifier = WalletIdentifier.create(dl_wallet)
1768
+
1769
+ if wallet_identifier is None:
1770
+ # Confirm tx records for txs which we submitted for coins which aren't in our wallet
1771
+ if coin_state.created_height is not None and coin_state.spent_height is not None:
1772
+ all_unconfirmed = await self.tx_store.get_all_unconfirmed()
1773
+ tx_records_to_confirm: List[TransactionRecord] = []
1774
+ for out_tx_record in all_unconfirmed:
1775
+ if coin_state.coin in out_tx_record.removals:
1776
+ tx_records_to_confirm.append(out_tx_record)
1777
+
1778
+ if len(tx_records_to_confirm) > 0:
1779
+ for tx_record in tx_records_to_confirm:
1780
+ await self.tx_store.set_confirmed(tx_record.name, uint32(coin_state.spent_height))
1781
+ self.log.debug(f"No wallet for coin state: {coin_state}")
1782
+ continue
1783
+
1784
+ # Update the DB to signal that we used puzzle hashes up to this one
1785
+ derivation_index = ph_to_index_cache.get(coin_state.coin.puzzle_hash)
1786
+ if derivation_index is None:
1787
+ derivation_index = await self.puzzle_store.index_for_puzzle_hash(coin_state.coin.puzzle_hash)
1788
+ if derivation_index is not None:
1789
+ ph_to_index_cache.put(coin_state.coin.puzzle_hash, derivation_index)
1790
+ if derivation_index > used_up_to:
1791
+ await self.puzzle_store.set_used_up_to(derivation_index)
1792
+ used_up_to = derivation_index
1793
+
1794
+ if coin_state.created_height is None:
1795
+ # TODO implements this coin got reorged
1796
+ # TODO: we need to potentially roll back the pool wallet here
1797
+ pass
1798
+ # if the new coin has not been spent (i.e not ephemeral)
1799
+ elif coin_state.created_height is not None and coin_state.spent_height is None:
1800
+ if local_record is None:
1801
+ await self.coin_added(
1802
+ coin_state.coin,
1803
+ uint32(coin_state.created_height),
1804
+ all_unconfirmed,
1805
+ wallet_identifier.id,
1806
+ wallet_identifier.type,
1807
+ peer,
1808
+ coin_name,
1809
+ coin_data,
1810
+ )
1811
+ await self.add_interested_coin_ids([coin_name])
1812
+
1813
+ # if the coin has been spent
1814
+ elif coin_state.created_height is not None and coin_state.spent_height is not None:
1815
+ self.log.debug("Coin spent: %s", coin_state)
1816
+ children = await self.wallet_node.fetch_children(coin_name, peer=peer, fork_height=fork_height)
1817
+ record = local_record
1818
+ if record is None:
1819
+ farmer_reward = False
1820
+ pool_reward = False
1821
+ tx_type: int
1822
+ if self.is_farmer_reward(uint32(coin_state.created_height), coin_state.coin):
1823
+ farmer_reward = True
1824
+ tx_type = TransactionType.FEE_REWARD.value
1825
+ elif self.is_pool_reward(uint32(coin_state.created_height), coin_state.coin):
1826
+ pool_reward = True
1827
+ tx_type = TransactionType.COINBASE_REWARD.value
1828
+ else:
1829
+ tx_type = TransactionType.INCOMING_TX.value
1830
+ record = WalletCoinRecord(
1831
+ coin_state.coin,
1832
+ uint32(coin_state.created_height),
1833
+ uint32(coin_state.spent_height),
1834
+ True,
1835
+ farmer_reward or pool_reward,
1836
+ wallet_identifier.type,
1837
+ wallet_identifier.id,
1838
+ )
1839
+ await self.coin_store.add_coin_record(record)
1840
+ # Coin first received
1841
+ parent_coin_record: Optional[WalletCoinRecord] = await self.coin_store.get_coin_record(
1842
+ coin_state.coin.parent_coin_info
1843
+ )
1844
+ if (
1845
+ parent_coin_record is not None
1846
+ and wallet_identifier.type == parent_coin_record.wallet_type
1847
+ ):
1848
+ change = True
1849
+ else:
1850
+ change = False
1851
+
1852
+ if not change:
1853
+ created_timestamp = await self.wallet_node.get_timestamp_for_height(
1854
+ uint32(coin_state.created_height)
1855
+ )
1856
+ tx_record = TransactionRecord(
1857
+ confirmed_at_height=uint32(coin_state.created_height),
1858
+ created_at_time=uint64(created_timestamp),
1859
+ to_puzzle_hash=(
1860
+ await self.convert_puzzle_hash(
1861
+ wallet_identifier.id, coin_state.coin.puzzle_hash
1862
+ )
1863
+ ),
1864
+ amount=uint64(coin_state.coin.amount),
1865
+ fee_amount=uint64(0),
1866
+ confirmed=True,
1867
+ sent=uint32(0),
1868
+ spend_bundle=None,
1869
+ additions=[coin_state.coin],
1870
+ removals=[],
1871
+ wallet_id=wallet_identifier.id,
1872
+ sent_to=[],
1873
+ trade_id=None,
1874
+ type=uint32(tx_type),
1875
+ name=bytes32.secret(),
1876
+ memos=[],
1877
+ valid_times=ConditionValidTimes(),
1878
+ )
1879
+ await self.tx_store.add_transaction_record(tx_record)
1880
+
1881
+ additions = [state.coin for state in children]
1882
+ if len(children) > 0:
1883
+ fee = 0
1884
+
1885
+ to_puzzle_hash = None
1886
+ coin_spend: Optional[CoinSpend] = None
1887
+ clawback_metadata: Optional[ClawbackMetadata] = None
1888
+ # Find coin that doesn't belong to us
1889
+ amount = 0
1890
+ for coin in additions:
1891
+ derivation_record = await self.puzzle_store.get_derivation_record_for_puzzle_hash(
1892
+ coin.puzzle_hash
1893
+ )
1894
+ if derivation_record is None: # not change
1895
+ to_puzzle_hash = coin.puzzle_hash
1896
+ amount += coin.amount
1897
+ if coin_spend is None:
1898
+ # To prevent unnecessary fetch, we only fetch once,
1899
+ # if there is a child coin that is not owned by the wallet.
1900
+ coin_spend = await fetch_coin_spend_for_coin_state(coin_state, peer)
1901
+ # Check if the parent coin is a Clawback coin
1902
+ uncurried = uncurry_puzzle(coin_spend.puzzle_reveal)
1903
+ clawback_metadata = match_clawback_puzzle(
1904
+ uncurried, coin_spend.puzzle_reveal, coin_spend.solution
1905
+ )
1906
+ if clawback_metadata is not None:
1907
+ # Add the Clawback coin as the interested coin for the sender
1908
+ await self.add_interested_coin_ids([coin.name()])
1909
+ elif wallet_identifier.type == WalletType.CAT:
1910
+ # We subscribe to change for CATs since they didn't hint previously
1911
+ await self.add_interested_coin_ids([coin.name()])
1912
+
1913
+ if to_puzzle_hash is None:
1914
+ to_puzzle_hash = additions[0].puzzle_hash
1915
+
1916
+ spent_timestamp = await self.wallet_node.get_timestamp_for_height(
1917
+ uint32(coin_state.spent_height)
1918
+ )
1919
+
1920
+ # Reorg rollback adds reorged transactions so it's possible there is tx_record already
1921
+ # Even though we are just adding coin record to the db (after reorg)
1922
+ tx_records: List[TransactionRecord] = []
1923
+ for out_tx_record in all_unconfirmed:
1924
+ for rem_coin in out_tx_record.removals:
1925
+ if rem_coin == coin_state.coin:
1926
+ tx_records.append(out_tx_record)
1927
+
1928
+ if len(tx_records) > 0:
1929
+ for tx_record in tx_records:
1930
+ await self.tx_store.set_confirmed(
1931
+ tx_record.name, uint32(coin_state.spent_height)
1932
+ )
1933
+ else:
1934
+ tx_name = bytes(coin_state.coin.name())
1935
+ for added_coin in additions:
1936
+ tx_name += bytes(added_coin.name())
1937
+ tx_name = std_hash(tx_name)
1938
+ tx_record = TransactionRecord(
1939
+ confirmed_at_height=uint32(coin_state.spent_height),
1940
+ created_at_time=uint64(spent_timestamp),
1941
+ to_puzzle_hash=(
1942
+ await self.convert_puzzle_hash(wallet_identifier.id, to_puzzle_hash)
1943
+ ),
1944
+ amount=uint64(int(amount)),
1945
+ fee_amount=uint64(fee),
1946
+ confirmed=True,
1947
+ sent=uint32(0),
1948
+ spend_bundle=None,
1949
+ additions=additions,
1950
+ removals=[coin_state.coin],
1951
+ wallet_id=wallet_identifier.id,
1952
+ sent_to=[],
1953
+ trade_id=None,
1954
+ type=uint32(TransactionType.OUTGOING_TX.value),
1955
+ name=tx_name,
1956
+ memos=[],
1957
+ valid_times=ConditionValidTimes(),
1958
+ )
1959
+
1960
+ await self.tx_store.add_transaction_record(tx_record)
1961
+ else:
1962
+ await self.coin_store.set_spent(coin_name, uint32(coin_state.spent_height))
1963
+ if record.coin_type == CoinType.CLAWBACK:
1964
+ await self.interested_store.remove_interested_coin_id(coin_state.coin.name())
1965
+ confirmed_tx_records: List[TransactionRecord] = []
1966
+
1967
+ for tx_record in all_unconfirmed:
1968
+ if tx_record.type in CLAWBACK_INCOMING_TRANSACTION_TYPES:
1969
+ for add_coin in tx_record.additions:
1970
+ if add_coin == coin_state.coin:
1971
+ confirmed_tx_records.append(tx_record)
1972
+ else:
1973
+ for rem_coin in tx_record.removals:
1974
+ if rem_coin == coin_state.coin:
1975
+ confirmed_tx_records.append(tx_record)
1976
+
1977
+ for tx_record in confirmed_tx_records:
1978
+ await self.tx_store.set_confirmed(tx_record.name, uint32(coin_state.spent_height))
1979
+ for unconfirmed_record in all_unconfirmed:
1980
+ for rem_coin in unconfirmed_record.removals:
1981
+ if rem_coin == coin_state.coin:
1982
+ self.log.info(f"Setting tx_id: {unconfirmed_record.name} to confirmed")
1983
+ await self.tx_store.set_confirmed(
1984
+ unconfirmed_record.name, uint32(coin_state.spent_height)
1985
+ )
1986
+
1987
+ if record.wallet_type in [WalletType.POOLING_WALLET, WalletType.DAO]:
1988
+ wallet_type_to_class = {WalletType.POOLING_WALLET: PoolWallet, WalletType.DAO: DAOWallet}
1989
+ if coin_state.spent_height is not None and coin_state.coin.amount == uint64(1):
1990
+ singleton_wallet: Union[PoolWallet, DAOWallet] = self.get_wallet(
1991
+ id=uint32(record.wallet_id), required_type=wallet_type_to_class[record.wallet_type]
1992
+ )
1993
+ curr_coin_state: CoinState = coin_state
1994
+
1995
+ while curr_coin_state.spent_height is not None:
1996
+ cs: CoinSpend = await fetch_coin_spend_for_coin_state(curr_coin_state, peer)
1997
+ success = await singleton_wallet.apply_state_transition(
1998
+ cs, uint32(curr_coin_state.spent_height)
1999
+ )
2000
+ if not success:
2001
+ break
2002
+ new_singleton_coin = get_most_recent_singleton_coin_from_coin_spend(cs)
2003
+ if new_singleton_coin is None:
2004
+ # No more singleton (maybe destroyed?)
2005
+ break
2006
+
2007
+ coin_name = new_singleton_coin.name()
2008
+ existing = await self.coin_store.get_coin_record(coin_name)
2009
+ if existing is None:
2010
+ await self.coin_added(
2011
+ new_singleton_coin,
2012
+ uint32(curr_coin_state.spent_height),
2013
+ [],
2014
+ uint32(record.wallet_id),
2015
+ record.wallet_type,
2016
+ peer,
2017
+ coin_name,
2018
+ coin_data,
2019
+ )
2020
+ await self.coin_store.set_spent(
2021
+ curr_coin_state.coin.name(), uint32(curr_coin_state.spent_height)
2022
+ )
2023
+ await self.add_interested_coin_ids([new_singleton_coin.name()])
2024
+ new_coin_state: List[CoinState] = await self.wallet_node.get_coin_state(
2025
+ [coin_name], peer=peer, fork_height=fork_height
2026
+ )
2027
+ assert len(new_coin_state) == 1
2028
+ curr_coin_state = new_coin_state[0]
2029
+ if record.wallet_type == WalletType.DATA_LAYER:
2030
+ singleton_spend = await fetch_coin_spend_for_coin_state(coin_state, peer)
2031
+ dl_wallet = self.get_wallet(id=uint32(record.wallet_id), required_type=DataLayerWallet)
2032
+ await dl_wallet.singleton_removed(
2033
+ singleton_spend,
2034
+ uint32(coin_state.spent_height),
2035
+ )
2036
+
2037
+ elif record.wallet_type == WalletType.NFT:
2038
+ if coin_state.spent_height is not None:
2039
+ nft_wallet = self.get_wallet(id=uint32(record.wallet_id), required_type=NFTWallet)
2040
+ await nft_wallet.remove_coin(coin_state.coin, uint32(coin_state.spent_height))
2041
+ elif record.wallet_type == WalletType.VC:
2042
+ if coin_state.spent_height is not None:
2043
+ vc_wallet = self.get_wallet(id=uint32(record.wallet_id), required_type=VCWallet)
2044
+ await vc_wallet.remove_coin(coin_state.coin, uint32(coin_state.spent_height))
2045
+
2046
+ # Check if a child is a singleton launcher
2047
+ for child in children:
2048
+ if child.coin.puzzle_hash != SINGLETON_LAUNCHER_HASH:
2049
+ continue
2050
+ if await self.have_a_pool_wallet_with_launched_id(child.coin.name()):
2051
+ continue
2052
+ if child.spent_height is None:
2053
+ # TODO handle spending launcher later block
2054
+ continue
2055
+ launcher_spend = await fetch_coin_spend_for_coin_state(child, peer)
2056
+ if launcher_spend is None:
2057
+ continue
2058
+ try:
2059
+ pool_state = solution_to_pool_state(launcher_spend)
2060
+ assert pool_state is not None
2061
+ except (AssertionError, ValueError) as e:
2062
+ self.log.debug(f"Not a pool wallet launcher {e}, child: {child}")
2063
+ matched, inner_puzhash = await DataLayerWallet.match_dl_launcher(launcher_spend)
2064
+ if (
2065
+ matched
2066
+ and inner_puzhash is not None
2067
+ and (await self.puzzle_store.puzzle_hash_exists(inner_puzhash))
2068
+ ):
2069
+ try:
2070
+ dl_wallet = self.get_dl_wallet()
2071
+ except ValueError:
2072
+ dl_wallet = await DataLayerWallet.create_new_dl_wallet(
2073
+ self,
2074
+ )
2075
+ await dl_wallet.track_new_launcher_id(
2076
+ child.coin.name(),
2077
+ peer,
2078
+ spend=launcher_spend,
2079
+ height=uint32(child.spent_height),
2080
+ )
2081
+ continue
2082
+
2083
+ # solution_to_pool_state may return None but this may not be an error
2084
+ if pool_state is None:
2085
+ self.log.debug("solution_to_pool_state returned None, ignore and continue")
2086
+ continue
2087
+
2088
+ pool_wallet = await PoolWallet.create(
2089
+ self,
2090
+ self.main_wallet,
2091
+ child.coin.name(),
2092
+ [launcher_spend],
2093
+ uint32(child.spent_height),
2094
+ name="pool_wallet",
2095
+ )
2096
+ launcher_spend_additions = compute_additions(launcher_spend)
2097
+ assert len(launcher_spend_additions) == 1
2098
+ coin_added = launcher_spend_additions[0]
2099
+ coin_name = coin_added.name()
2100
+ existing = await self.coin_store.get_coin_record(coin_name)
2101
+ if existing is None:
2102
+ await self.coin_added(
2103
+ coin_added,
2104
+ uint32(coin_state.spent_height),
2105
+ [],
2106
+ pool_wallet.id(),
2107
+ pool_wallet.type(),
2108
+ peer,
2109
+ coin_name,
2110
+ coin_data,
2111
+ )
2112
+ await self.add_interested_coin_ids([coin_name])
2113
+
2114
+ else:
2115
+ raise RuntimeError("All cases already handled") # Logic error, all cases handled
2116
+ except Exception as e:
2117
+ self.log.exception(f"Failed to add coin_state: {coin_state}, error: {e}")
2118
+ if rollback_wallets is not None:
2119
+ self.wallets = rollback_wallets # Restore since DB will be rolled back by writer
2120
+ if isinstance(e, (PeerRequestException, aiosqlite.Error)):
2121
+ await self.retry_store.add_state(coin_state, peer.peer_node_id, fork_height)
2122
+ else:
2123
+ await self.retry_store.remove_state(coin_state)
2124
+ continue
2125
+
2126
+ async def add_coin_states(
2127
+ self,
2128
+ coin_states: List[CoinState],
2129
+ peer: WSChiaConnection,
2130
+ fork_height: Optional[uint32],
2131
+ ) -> bool:
2132
+ try:
2133
+ await self._add_coin_states(coin_states, peer, fork_height)
2134
+ except Exception as e:
2135
+ log_level = logging.DEBUG if peer.closed else logging.ERROR
2136
+ self.log.log(log_level, f"add_coin_states failed - exception {e}, traceback: {traceback.format_exc()}")
2137
+ return False
2138
+
2139
+ await self.blockchain.clean_block_records()
2140
+
2141
+ return True
2142
+
2143
+ async def have_a_pool_wallet_with_launched_id(self, launcher_id: bytes32) -> bool:
2144
+ for wallet_id, wallet in self.wallets.items():
2145
+ if wallet.type() == WalletType.POOLING_WALLET:
2146
+ assert isinstance(wallet, PoolWallet)
2147
+ if (await wallet.get_current_state()).launcher_id == launcher_id:
2148
+ self.log.warning("Already have, not recreating")
2149
+ return True
2150
+ return False
2151
+
2152
+ def is_pool_reward(self, created_height: uint32, coin: Coin) -> bool:
2153
+ if coin.amount != calculate_pool_reward(created_height) and coin.amount != calculate_pool_reward(
2154
+ uint32(max(0, created_height - 128))
2155
+ ):
2156
+ # Optimization to avoid the computation below. Any coin that has a different amount is not a pool reward
2157
+ return False
2158
+ for i in range(0, 30):
2159
+ try_height = created_height - i
2160
+ if try_height < 0:
2161
+ break
2162
+ calculated = pool_parent_id(uint32(try_height), self.constants.GENESIS_CHALLENGE)
2163
+ if calculated == coin.parent_coin_info:
2164
+ return True
2165
+ return False
2166
+
2167
+ def is_farmer_reward(self, created_height: uint32, coin: Coin) -> bool:
2168
+ if coin.amount < calculate_base_farmer_reward(created_height):
2169
+ # Optimization to avoid the computation below. Any coin less than this base amount cannot be farmer reward
2170
+ return False
2171
+ for i in range(0, 30):
2172
+ try_height = created_height - i
2173
+ if try_height < 0:
2174
+ break
2175
+ calculated = farmer_parent_id(uint32(try_height), self.constants.GENESIS_CHALLENGE)
2176
+ if calculated == coin.parent_coin_info:
2177
+ return True
2178
+ return False
2179
+
2180
+ async def get_wallet_identifier_for_puzzle_hash(self, puzzle_hash: bytes32) -> Optional[WalletIdentifier]:
2181
+ wallet_identifier = await self.puzzle_store.get_wallet_identifier_for_puzzle_hash(puzzle_hash)
2182
+ if wallet_identifier is not None:
2183
+ return wallet_identifier
2184
+
2185
+ interested_wallet_id = await self.interested_store.get_interested_puzzle_hash_wallet_id(puzzle_hash=puzzle_hash)
2186
+ if interested_wallet_id is not None:
2187
+ wallet_id = uint32(interested_wallet_id)
2188
+ if wallet_id not in self.wallets.keys():
2189
+ self.log.warning(f"Do not have wallet {wallet_id} for puzzle_hash {puzzle_hash}")
2190
+ return None
2191
+ return WalletIdentifier(uint32(wallet_id), self.wallets[uint32(wallet_id)].type())
2192
+ return None
2193
+
2194
+ async def get_wallet_identifier_for_coin(
2195
+ self, coin: Coin, hint_dict: Dict[bytes32, bytes32] = {}
2196
+ ) -> Optional[WalletIdentifier]:
2197
+ wallet_identifier = await self.puzzle_store.get_wallet_identifier_for_puzzle_hash(coin.puzzle_hash)
2198
+ if (
2199
+ wallet_identifier is None
2200
+ and coin.name() in hint_dict
2201
+ and await self.puzzle_store.puzzle_hash_exists(hint_dict[coin.name()])
2202
+ ):
2203
+ wallet_identifier = await self.get_wallet_identifier_for_hinted_coin(coin, hint_dict[coin.name()])
2204
+ if wallet_identifier is None:
2205
+ coin_record = await self.coin_store.get_coin_record(coin.name())
2206
+ if coin_record is not None:
2207
+ wallet_identifier = WalletIdentifier(uint32(coin_record.wallet_id), coin_record.wallet_type)
2208
+
2209
+ return wallet_identifier
2210
+
2211
+ async def get_wallet_identifier_for_hinted_coin(self, coin: Coin, hint: bytes32) -> Optional[WalletIdentifier]:
2212
+ for wallet in self.wallets.values():
2213
+ if await wallet.match_hinted_coin(coin, hint):
2214
+ return WalletIdentifier(wallet.id(), wallet.type())
2215
+ return None
2216
+
2217
+ async def coin_added(
2218
+ self,
2219
+ coin: Coin,
2220
+ height: uint32,
2221
+ all_unconfirmed_transaction_records: List[TransactionRecord],
2222
+ wallet_id: uint32,
2223
+ wallet_type: WalletType,
2224
+ peer: WSChiaConnection,
2225
+ coin_name: bytes32,
2226
+ coin_data: Optional[Streamable],
2227
+ ) -> None:
2228
+ """
2229
+ Adding coin to DB
2230
+ """
2231
+
2232
+ self.log.debug(
2233
+ "Adding record to state manager coin: %s at %s wallet_id: %s and type: %s",
2234
+ coin,
2235
+ height,
2236
+ wallet_id,
2237
+ wallet_type,
2238
+ )
2239
+
2240
+ if self.is_pool_reward(height, coin):
2241
+ tx_type = TransactionType.COINBASE_REWARD
2242
+ elif self.is_farmer_reward(height, coin):
2243
+ tx_type = TransactionType.FEE_REWARD
2244
+ else:
2245
+ tx_type = TransactionType.INCOMING_TX
2246
+
2247
+ coinbase = tx_type in {TransactionType.FEE_REWARD, TransactionType.COINBASE_REWARD}
2248
+ coin_confirmed_transaction = False
2249
+ if not coinbase:
2250
+ for record in all_unconfirmed_transaction_records:
2251
+ if coin in record.additions and not record.confirmed:
2252
+ await self.tx_store.set_confirmed(record.name, height)
2253
+ coin_confirmed_transaction = True
2254
+ break
2255
+
2256
+ parent_coin_record: Optional[WalletCoinRecord] = await self.coin_store.get_coin_record(coin.parent_coin_info)
2257
+ change = parent_coin_record is not None and wallet_type.value == parent_coin_record.wallet_type
2258
+ # If the coin is from a Clawback spent, we want to add the INCOMING_TX,
2259
+ # no matter if there is another TX updated.
2260
+ clawback = parent_coin_record is not None and parent_coin_record.coin_type == CoinType.CLAWBACK
2261
+
2262
+ if coinbase or clawback or not coin_confirmed_transaction and not change:
2263
+ tx_record = TransactionRecord(
2264
+ confirmed_at_height=uint32(height),
2265
+ created_at_time=await self.wallet_node.get_timestamp_for_height(height),
2266
+ to_puzzle_hash=await self.convert_puzzle_hash(wallet_id, coin.puzzle_hash),
2267
+ amount=uint64(coin.amount),
2268
+ fee_amount=uint64(0),
2269
+ confirmed=True,
2270
+ sent=uint32(0),
2271
+ spend_bundle=None,
2272
+ additions=[coin],
2273
+ removals=[],
2274
+ wallet_id=wallet_id,
2275
+ sent_to=[],
2276
+ trade_id=None,
2277
+ type=uint32(tx_type),
2278
+ name=coin_name,
2279
+ memos=[],
2280
+ valid_times=ConditionValidTimes(),
2281
+ )
2282
+ if tx_record.amount > 0:
2283
+ await self.tx_store.add_transaction_record(tx_record)
2284
+
2285
+ # We only add normal coins here
2286
+ coin_record: WalletCoinRecord = WalletCoinRecord(
2287
+ coin, height, uint32(0), False, coinbase, wallet_type, wallet_id
2288
+ )
2289
+
2290
+ await self.coin_store.add_coin_record(coin_record, coin_name)
2291
+
2292
+ await self.wallets[wallet_id].coin_added(coin, height, peer, coin_data)
2293
+
2294
+ if wallet_type == WalletType.DAO:
2295
+ return
2296
+
2297
+ await self.create_more_puzzle_hashes()
2298
+
2299
+ async def add_pending_transactions(
2300
+ self,
2301
+ tx_records: List[TransactionRecord],
2302
+ push: bool = True,
2303
+ merge_spends: bool = True,
2304
+ sign: Optional[bool] = None,
2305
+ additional_signing_responses: Optional[List[SigningResponse]] = None,
2306
+ extra_spends: Optional[List[WalletSpendBundle]] = None,
2307
+ ) -> List[TransactionRecord]:
2308
+ """
2309
+ Add a list of transactions to be submitted to the full node.
2310
+ Aggregates the `spend_bundle` property for each transaction onto the first transaction in the list.
2311
+ """
2312
+ if sign is None:
2313
+ sign = self.config.get("auto_sign_txs", True)
2314
+ agg_spend = WalletSpendBundle.aggregate([tx.spend_bundle for tx in tx_records if tx.spend_bundle is not None])
2315
+ if extra_spends is not None:
2316
+ agg_spend = WalletSpendBundle.aggregate([agg_spend, *extra_spends])
2317
+ actual_spend_involved: bool = agg_spend != WalletSpendBundle([], G2Element())
2318
+ if merge_spends and actual_spend_involved:
2319
+ tx_records = [
2320
+ dataclasses.replace(
2321
+ tx,
2322
+ spend_bundle=agg_spend if i == 0 else None,
2323
+ name=agg_spend.name() if i == 0 else bytes32.secret(),
2324
+ )
2325
+ for i, tx in enumerate(tx_records)
2326
+ ]
2327
+ elif extra_spends is not None and extra_spends != []:
2328
+ extra_spends.extend([] if tx_records[0].spend_bundle is None else [tx_records[0].spend_bundle])
2329
+ extra_spend_bundle = WalletSpendBundle.aggregate(extra_spends)
2330
+ tx_records = [
2331
+ dataclasses.replace(
2332
+ tx,
2333
+ spend_bundle=extra_spend_bundle if i == 0 else tx.spend_bundle,
2334
+ name=extra_spend_bundle.name() if i == 0 else bytes32.secret(),
2335
+ )
2336
+ for i, tx in enumerate(tx_records)
2337
+ ]
2338
+ if sign:
2339
+ tx_records, _ = await self.sign_transactions(
2340
+ tx_records,
2341
+ [] if additional_signing_responses is None else additional_signing_responses,
2342
+ additional_signing_responses != [] and additional_signing_responses is not None,
2343
+ )
2344
+ if push:
2345
+ all_coins_names = []
2346
+ async with self.db_wrapper.writer_maybe_transaction():
2347
+ for tx_record in tx_records:
2348
+ # Wallet node will use this queue to retry sending this transaction until full nodes receives it
2349
+ await self.tx_store.add_transaction_record(tx_record)
2350
+ all_coins_names.extend([coin.name() for coin in tx_record.additions])
2351
+ all_coins_names.extend([coin.name() for coin in tx_record.removals])
2352
+
2353
+ await self.add_interested_coin_ids(all_coins_names)
2354
+
2355
+ if actual_spend_involved:
2356
+ self.tx_pending_changed()
2357
+ for wallet_id in {tx.wallet_id for tx in tx_records}:
2358
+ self.state_changed("pending_transaction", wallet_id)
2359
+ await self.wallet_node.update_ui()
2360
+
2361
+ return tx_records
2362
+
2363
+ async def add_transaction(self, tx_record: TransactionRecord) -> None:
2364
+ """
2365
+ Called from wallet to add transaction that is not being set to full_node
2366
+ """
2367
+ await self.tx_store.add_transaction_record(tx_record)
2368
+ self.state_changed("pending_transaction", tx_record.wallet_id)
2369
+
2370
+ async def remove_from_queue(
2371
+ self,
2372
+ spendbundle_id: bytes32,
2373
+ name: str,
2374
+ send_status: MempoolInclusionStatus,
2375
+ error: Optional[Err],
2376
+ ) -> None:
2377
+ """
2378
+ Full node received our transaction, no need to keep it in queue anymore, unless there was an error
2379
+ """
2380
+
2381
+ updated = await self.tx_store.increment_sent(spendbundle_id, name, send_status, error)
2382
+ if updated:
2383
+ tx: Optional[TransactionRecord] = await self.get_transaction(spendbundle_id)
2384
+ if tx is not None and tx.spend_bundle is not None:
2385
+ self.log.info("Checking if we need to cancel trade for tx: %s", tx.name)
2386
+ # we're only interested in errors that are not temporary
2387
+ if (
2388
+ send_status != MempoolInclusionStatus.SUCCESS
2389
+ and error
2390
+ and error not in (Err.INVALID_FEE_LOW_FEE, Err.INVALID_FEE_TOO_CLOSE_TO_ZERO)
2391
+ ):
2392
+ coins_removed = tx.spend_bundle.removals()
2393
+ trade_coins_removed = set()
2394
+ trades = []
2395
+ for removed_coin in coins_removed:
2396
+ trade = await self.trade_manager.get_trade_by_coin(removed_coin)
2397
+ if trade is not None and trade.status in (
2398
+ TradeStatus.PENDING_CONFIRM.value,
2399
+ TradeStatus.PENDING_ACCEPT.value,
2400
+ TradeStatus.PENDING_CANCEL.value,
2401
+ ):
2402
+ if trade not in trades:
2403
+ trades.append(trade)
2404
+ # offer was tied to these coins, lets subscribe to them to get a confirmation to
2405
+ # cancel it if it's confirmed
2406
+ # we send transactions to multiple peers, and in cases when mempool gets
2407
+ # fragmented, it's safest to wait for confirmation from blockchain before setting
2408
+ # offer to failed
2409
+ trade_coins_removed.add(removed_coin.name())
2410
+ if trades != [] and trade_coins_removed != set():
2411
+ if not tx.is_valid():
2412
+ # we've tried to send this transaction to a full node multiple times
2413
+ # but failed, it's safe to assume that it's not going to be accepted
2414
+ # we can mark this offer as failed
2415
+ self.log.info("This offer can't be posted, removing it from pending offers")
2416
+ for trade in trades:
2417
+ await self.trade_manager.fail_pending_offer(trade.trade_id)
2418
+ else:
2419
+ self.log.info(
2420
+ "Subscribing to unspendable offer coins: %s",
2421
+ [x.hex() for x in trade_coins_removed],
2422
+ )
2423
+ await self.add_interested_coin_ids(list(trade_coins_removed))
2424
+
2425
+ self.state_changed(
2426
+ "tx_update", tx.wallet_id, {"transaction": tx, "error": error.name, "status": send_status.value}
2427
+ )
2428
+ else:
2429
+ self.state_changed("tx_update", tx.wallet_id, {"transaction": tx})
2430
+
2431
+ async def get_all_transactions(self, wallet_id: int) -> List[TransactionRecord]:
2432
+ """
2433
+ Retrieves all confirmed and pending transactions
2434
+ """
2435
+ records = await self.tx_store.get_all_transactions_for_wallet(wallet_id)
2436
+ return records
2437
+
2438
+ async def get_transaction(self, tx_id: bytes32) -> Optional[TransactionRecord]:
2439
+ return await self.tx_store.get_transaction_record(tx_id)
2440
+
2441
+ async def get_coin_record_by_wallet_record(self, wr: WalletCoinRecord) -> CoinRecord:
2442
+ timestamp: uint64 = await self.wallet_node.get_timestamp_for_height(wr.confirmed_block_height)
2443
+ return wr.to_coin_record(timestamp)
2444
+
2445
+ async def get_coin_records_by_coin_ids(self, **kwargs: Any) -> List[CoinRecord]:
2446
+ result = await self.coin_store.get_coin_records(**kwargs)
2447
+ return [await self.get_coin_record_by_wallet_record(record) for record in result.records]
2448
+
2449
+ async def get_wallet_for_coin(self, coin_id: bytes32) -> Optional[WalletProtocol[Any]]:
2450
+ coin_record = await self.coin_store.get_coin_record(coin_id)
2451
+ if coin_record is None:
2452
+ return None
2453
+ wallet_id = uint32(coin_record.wallet_id)
2454
+ wallet = self.wallets[wallet_id]
2455
+ return wallet
2456
+
2457
+ async def reorg_rollback(self, height: int) -> List[uint32]:
2458
+ """
2459
+ Rolls back and updates the coin_store and transaction store. It's possible this height
2460
+ is the tip, or even beyond the tip.
2461
+ """
2462
+ await self.retry_store.rollback_to_block(height)
2463
+ await self.nft_store.rollback_to_block(height)
2464
+ await self.coin_store.rollback_to_block(height)
2465
+ await self.interested_store.rollback_to_block(height)
2466
+ await self.dl_store.rollback_to_block(height)
2467
+ reorged: List[TransactionRecord] = await self.tx_store.get_transaction_above(height)
2468
+ await self.tx_store.rollback_to_block(height)
2469
+ for record in reorged:
2470
+ if TransactionType(record.type) in [
2471
+ TransactionType.OUTGOING_TX,
2472
+ TransactionType.OUTGOING_TRADE,
2473
+ TransactionType.INCOMING_TRADE,
2474
+ TransactionType.OUTGOING_CLAWBACK,
2475
+ TransactionType.INCOMING_CLAWBACK_SEND,
2476
+ TransactionType.INCOMING_CLAWBACK_RECEIVE,
2477
+ ]:
2478
+ await self.tx_store.tx_reorged(record)
2479
+
2480
+ # Removes wallets that were created from a blockchain transaction which got reorged.
2481
+ remove_ids: List[uint32] = []
2482
+ for wallet_id, wallet in self.wallets.items():
2483
+ if wallet.type() == WalletType.POOLING_WALLET.value:
2484
+ assert isinstance(wallet, PoolWallet)
2485
+ remove: bool = await wallet.rewind(height)
2486
+ if remove:
2487
+ remove_ids.append(wallet_id)
2488
+ for wallet_id in remove_ids:
2489
+ await self.user_store.delete_wallet(wallet_id)
2490
+ self.state_changed("wallet_removed", wallet_id)
2491
+
2492
+ return remove_ids
2493
+
2494
+ async def _await_closed(self) -> None:
2495
+ await self.db_wrapper.close()
2496
+
2497
+ def unlink_db(self) -> None:
2498
+ Path(self.db_path).unlink()
2499
+
2500
+ async def get_all_wallet_info_entries(self, wallet_type: Optional[WalletType] = None) -> List[WalletInfo]:
2501
+ return await self.user_store.get_all_wallet_info_entries(wallet_type)
2502
+
2503
+ async def get_wallet_for_asset_id(self, asset_id: str) -> Optional[WalletProtocol[Any]]:
2504
+ for wallet_id, wallet in self.wallets.items():
2505
+ if wallet.type() in (WalletType.CAT, WalletType.CRCAT):
2506
+ assert isinstance(wallet, CATWallet)
2507
+ if wallet.get_asset_id() == asset_id:
2508
+ return wallet
2509
+ elif wallet.type() == WalletType.DATA_LAYER:
2510
+ assert isinstance(wallet, DataLayerWallet)
2511
+ if await wallet.get_latest_singleton(bytes32.from_hexstr(asset_id)) is not None:
2512
+ return wallet
2513
+ elif wallet.type() == WalletType.NFT:
2514
+ assert isinstance(wallet, NFTWallet)
2515
+ nft_coin = await self.nft_store.get_nft_by_id(bytes32.from_hexstr(asset_id), wallet_id)
2516
+ if nft_coin:
2517
+ return wallet
2518
+ return None
2519
+
2520
+ async def get_wallet_for_puzzle_info(self, puzzle_driver: PuzzleInfo) -> Optional[WalletProtocol[Any]]:
2521
+ for wallet in self.wallets.values():
2522
+ match_function = getattr(wallet, "match_puzzle_info", None)
2523
+ if match_function is not None and callable(match_function):
2524
+ if await match_function(puzzle_driver):
2525
+ return wallet
2526
+ return None
2527
+
2528
+ async def create_wallet_for_puzzle_info(self, puzzle_driver: PuzzleInfo, name: Optional[str] = None) -> None:
2529
+ if AssetType(puzzle_driver.type()) in self.asset_to_wallet_map:
2530
+ await self.asset_to_wallet_map[AssetType(puzzle_driver.type())].create_from_puzzle_info(
2531
+ self,
2532
+ self.main_wallet,
2533
+ puzzle_driver,
2534
+ name,
2535
+ potential_subclasses={
2536
+ AssetType.CR: CRCATWallet,
2537
+ },
2538
+ )
2539
+
2540
+ async def add_new_wallet(self, wallet: WalletProtocol[Any]) -> None:
2541
+ self.wallets[wallet.id()] = wallet
2542
+ await self.create_more_puzzle_hashes()
2543
+ self.state_changed("wallet_created")
2544
+
2545
+ async def get_spendable_coins_for_wallet(
2546
+ self, wallet_id: int, records: Optional[Set[WalletCoinRecord]] = None
2547
+ ) -> Set[WalletCoinRecord]:
2548
+ wallet_type = self.wallets[uint32(wallet_id)].type()
2549
+ if records is None:
2550
+ if wallet_type == WalletType.CRCAT:
2551
+ records = await self.coin_store.get_unspent_coins_for_wallet(wallet_id, CoinType.CRCAT)
2552
+ else:
2553
+ records = await self.coin_store.get_unspent_coins_for_wallet(wallet_id)
2554
+
2555
+ # Coins that are currently part of a transaction
2556
+ unconfirmed_tx: List[TransactionRecord] = await self.tx_store.get_unconfirmed_for_wallet(wallet_id)
2557
+ removal_dict: Dict[bytes32, Coin] = {}
2558
+ for tx in unconfirmed_tx:
2559
+ for coin in tx.removals:
2560
+ # TODO, "if" might not be necessary once unconfirmed tx doesn't contain coins for other wallets
2561
+ if await self.does_coin_belong_to_wallet(coin, wallet_id, tx.hint_dict()):
2562
+ removal_dict[coin.name()] = coin
2563
+
2564
+ # Coins that are part of the trade
2565
+ offer_locked_coins: Dict[bytes32, WalletCoinRecord] = await self.trade_manager.get_locked_coins()
2566
+
2567
+ filtered = set()
2568
+ for record in records:
2569
+ if record.coin.name() in offer_locked_coins:
2570
+ continue
2571
+ if record.coin.name() in removal_dict:
2572
+ continue
2573
+ filtered.add(record)
2574
+
2575
+ return filtered
2576
+
2577
+ async def new_peak(self, height: uint32) -> None:
2578
+ for wallet_id, wallet in self.wallets.items():
2579
+ if wallet.type() == WalletType.POOLING_WALLET:
2580
+ assert isinstance(wallet, PoolWallet)
2581
+ await wallet.new_peak(height)
2582
+ current_time = int(time.time())
2583
+
2584
+ if self.wallet_node.last_wallet_tx_resend_time < current_time - self.wallet_node.wallet_tx_resend_timeout_secs:
2585
+ self.tx_pending_changed()
2586
+
2587
+ async def add_interested_puzzle_hashes(self, puzzle_hashes: List[bytes32], wallet_ids: List[int]) -> None:
2588
+ # TODO: It's unclear if the intended use for this is that each puzzle hash should store all
2589
+ # the elements of wallet_ids. It only stores one wallet_id per puzzle hash in the interested_store
2590
+ # but the coin_cache keeps all wallet_ids for each puzzle hash
2591
+ for puzzle_hash in puzzle_hashes:
2592
+ if puzzle_hash in self.interested_coin_cache:
2593
+ wallet_ids_to_add = list({w for w in wallet_ids if w not in self.interested_coin_cache[puzzle_hash]})
2594
+ self.interested_coin_cache[puzzle_hash].extend(wallet_ids_to_add)
2595
+ else:
2596
+ self.interested_coin_cache[puzzle_hash] = list(set(wallet_ids))
2597
+ for puzzle_hash, wallet_id in zip(puzzle_hashes, wallet_ids):
2598
+ await self.interested_store.add_interested_puzzle_hash(puzzle_hash, wallet_id)
2599
+ if len(puzzle_hashes) > 0:
2600
+ await self.wallet_node.new_peak_queue.subscribe_to_puzzle_hashes(puzzle_hashes)
2601
+
2602
+ async def add_interested_coin_ids(self, coin_ids: List[bytes32], wallet_ids: List[int] = []) -> None:
2603
+ # TODO: FIX: wallet_ids is sometimes populated unexpectedly when called from add_pending_transaction
2604
+ for coin_id in coin_ids:
2605
+ if coin_id in self.interested_coin_cache:
2606
+ # prevent repeated wallet_ids from appearing in the coin cache
2607
+ wallet_ids_to_add = list({w for w in wallet_ids if w not in self.interested_coin_cache[coin_id]})
2608
+ self.interested_coin_cache[coin_id].extend(wallet_ids_to_add)
2609
+ else:
2610
+ self.interested_coin_cache[coin_id] = list(set(wallet_ids))
2611
+ for coin_id in coin_ids:
2612
+ await self.interested_store.add_interested_coin_id(coin_id)
2613
+ if len(coin_ids) > 0:
2614
+ await self.wallet_node.new_peak_queue.subscribe_to_coin_ids(coin_ids)
2615
+
2616
+ async def delete_trade_transactions(self, trade_id: bytes32) -> None:
2617
+ txs: List[TransactionRecord] = await self.tx_store.get_transactions_by_trade_id(trade_id)
2618
+ for tx in txs:
2619
+ await self.tx_store.delete_transaction_record(tx.name)
2620
+
2621
+ async def convert_puzzle_hash(self, wallet_id: uint32, puzzle_hash: bytes32) -> bytes32:
2622
+ wallet = self.wallets[wallet_id]
2623
+ # This should be general to wallets but for right now this is just for CATs so we'll add this if
2624
+ if wallet.type() in (WalletType.CAT.value, WalletType.CRCAT.value):
2625
+ assert isinstance(wallet, CATWallet)
2626
+ return await wallet.convert_puzzle_hash(puzzle_hash)
2627
+
2628
+ return puzzle_hash
2629
+
2630
+ def get_dl_wallet(self) -> DataLayerWallet:
2631
+ for wallet in self.wallets.values():
2632
+ if wallet.type() == WalletType.DATA_LAYER.value:
2633
+ assert isinstance(
2634
+ wallet, DataLayerWallet
2635
+ ), f"WalletType.DATA_LAYER should be a DataLayerWallet instance got: {type(wallet).__name__}"
2636
+ return wallet
2637
+ raise ValueError("DataLayerWallet not available")
2638
+
2639
+ async def get_or_create_vc_wallet(self) -> VCWallet:
2640
+ for _, wallet in self.wallets.items():
2641
+ if WalletType(wallet.type()) == WalletType.VC:
2642
+ assert isinstance(wallet, VCWallet)
2643
+ vc_wallet: VCWallet = wallet
2644
+ break
2645
+ else:
2646
+ # Create a new VC wallet
2647
+ vc_wallet = await VCWallet.create_new_vc_wallet(self, self.main_wallet)
2648
+
2649
+ return vc_wallet
2650
+
2651
+ async def sum_hint_for_pubkey(self, pk: bytes) -> Optional[SumHint]:
2652
+ return await self.main_wallet.sum_hint_for_pubkey(pk)
2653
+
2654
+ async def path_hint_for_pubkey(self, pk: bytes) -> Optional[PathHint]:
2655
+ return await self.main_wallet.path_hint_for_pubkey(pk)
2656
+
2657
+ async def key_hints_for_pubkeys(self, pks: List[bytes]) -> KeyHints:
2658
+ return KeyHints(
2659
+ [sum_hint for pk in pks for sum_hint in (await self.sum_hint_for_pubkey(pk),) if sum_hint is not None],
2660
+ [path_hint for pk in pks for path_hint in (await self.path_hint_for_pubkey(pk),) if path_hint is not None],
2661
+ )
2662
+
2663
+ async def gather_signing_info(self, coin_spends: List[Spend]) -> SigningInstructions:
2664
+ pks: List[bytes] = []
2665
+ signing_targets: List[SigningTarget] = []
2666
+ for coin_spend in coin_spends:
2667
+ _coin_spend = coin_spend.as_coin_spend()
2668
+ # Get AGG_SIG conditions
2669
+ conditions_dict = conditions_dict_for_solution(
2670
+ _coin_spend.puzzle_reveal.to_program(),
2671
+ _coin_spend.solution.to_program(),
2672
+ self.constants.MAX_BLOCK_COST_CLVM,
2673
+ )
2674
+ # Create signature
2675
+ for pk, msg in pkm_pairs_for_conditions_dict(
2676
+ conditions_dict, _coin_spend.coin, self.constants.AGG_SIG_ME_ADDITIONAL_DATA
2677
+ ):
2678
+ pk_bytes = bytes(pk)
2679
+ pks.append(pk_bytes)
2680
+ fingerprint: bytes = pk.get_fingerprint().to_bytes(4, "big")
2681
+ signing_targets.append(SigningTarget(fingerprint, msg, std_hash(pk_bytes + msg)))
2682
+
2683
+ return SigningInstructions(
2684
+ await self.key_hints_for_pubkeys(pks),
2685
+ signing_targets,
2686
+ )
2687
+
2688
+ async def gather_signing_info_for_bundles(self, bundles: List[WalletSpendBundle]) -> List[UnsignedTransaction]:
2689
+ utxs: List[UnsignedTransaction] = []
2690
+ for bundle in bundles:
2691
+ signer_protocol_spends: List[Spend] = [Spend.from_coin_spend(spend) for spend in bundle.coin_spends]
2692
+ utxs.append(
2693
+ UnsignedTransaction(
2694
+ TransactionInfo(signer_protocol_spends), await self.gather_signing_info(signer_protocol_spends)
2695
+ )
2696
+ )
2697
+
2698
+ return utxs
2699
+
2700
+ async def gather_signing_info_for_txs(self, txs: List[TransactionRecord]) -> List[UnsignedTransaction]:
2701
+ return await self.gather_signing_info_for_bundles(
2702
+ [tx.spend_bundle for tx in txs if tx.spend_bundle is not None]
2703
+ )
2704
+
2705
+ async def gather_signing_info_for_trades(self, offers: List[Offer]) -> List[UnsignedTransaction]:
2706
+ return await self.gather_signing_info_for_bundles([offer._bundle for offer in offers])
2707
+
2708
+ async def execute_signing_instructions(
2709
+ self, signing_instructions: SigningInstructions, partial_allowed: bool = False
2710
+ ) -> List[SigningResponse]:
2711
+ return await self.main_wallet.execute_signing_instructions(signing_instructions, partial_allowed)
2712
+
2713
+ async def apply_signatures(
2714
+ self, spends: List[Spend], signing_responses: List[SigningResponse]
2715
+ ) -> SignedTransaction:
2716
+ return await self.main_wallet.apply_signatures(spends, signing_responses)
2717
+
2718
+ def signed_tx_to_spendbundle(self, signed_tx: SignedTransaction) -> WalletSpendBundle:
2719
+ if len([_ for _ in signed_tx.signatures if _.type != "bls_12381_aug_scheme"]) > 0:
2720
+ raise ValueError("Unable to handle signatures that are not bls_12381_aug_scheme") # pragma: no cover
2721
+ return WalletSpendBundle(
2722
+ [spend.as_coin_spend() for spend in signed_tx.transaction_info.spends],
2723
+ AugSchemeMPL.aggregate([G2Element.from_bytes(sig.signature) for sig in signed_tx.signatures]),
2724
+ )
2725
+
2726
+ async def sign_transactions(
2727
+ self,
2728
+ tx_records: List[TransactionRecord],
2729
+ additional_signing_responses: List[SigningResponse] = [],
2730
+ partial_allowed: bool = False,
2731
+ ) -> Tuple[List[TransactionRecord], List[SigningResponse]]:
2732
+ unsigned_txs: List[UnsignedTransaction] = await self.gather_signing_info_for_txs(tx_records)
2733
+ new_txs: List[TransactionRecord] = []
2734
+ all_signing_responses = additional_signing_responses.copy()
2735
+ for unsigned_tx, tx in zip(
2736
+ unsigned_txs, [tx_record for tx_record in tx_records if tx_record.spend_bundle is not None]
2737
+ ):
2738
+ signing_responses: List[SigningResponse] = await self.execute_signing_instructions(
2739
+ unsigned_tx.signing_instructions, partial_allowed=partial_allowed
2740
+ )
2741
+ all_signing_responses.extend(signing_responses)
2742
+ new_bundle = self.signed_tx_to_spendbundle(
2743
+ await self.apply_signatures(
2744
+ unsigned_tx.transaction_info.spends,
2745
+ [*additional_signing_responses, *signing_responses],
2746
+ )
2747
+ )
2748
+ new_txs.append(dataclasses.replace(tx, spend_bundle=new_bundle, name=new_bundle.name()))
2749
+ new_txs.extend([tx_record for tx_record in tx_records if tx_record.spend_bundle is None])
2750
+ return new_txs, all_signing_responses
2751
+
2752
+ async def sign_offers(
2753
+ self,
2754
+ offers: List[Offer],
2755
+ additional_signing_responses: List[SigningResponse] = [],
2756
+ partial_allowed: bool = False,
2757
+ ) -> Tuple[List[Offer], List[SigningResponse]]:
2758
+ unsigned_txs: List[UnsignedTransaction] = await self.gather_signing_info_for_trades(offers)
2759
+ new_offers: List[Offer] = []
2760
+ all_signing_responses = additional_signing_responses.copy()
2761
+ for unsigned_tx, offer in zip(unsigned_txs, [offer for offer in offers]):
2762
+ signing_responses: List[SigningResponse] = await self.execute_signing_instructions(
2763
+ unsigned_tx.signing_instructions, partial_allowed=partial_allowed
2764
+ )
2765
+ all_signing_responses.extend(signing_responses)
2766
+ new_bundle = self.signed_tx_to_spendbundle(
2767
+ await self.apply_signatures(
2768
+ unsigned_tx.transaction_info.spends,
2769
+ [*additional_signing_responses, *signing_responses],
2770
+ )
2771
+ )
2772
+ new_offers.append(Offer(offer.requested_payments, new_bundle, offer.driver_dict))
2773
+ return new_offers, all_signing_responses
2774
+
2775
+ async def sign_bundle(
2776
+ self,
2777
+ coin_spends: List[CoinSpend],
2778
+ additional_signing_responses: List[SigningResponse] = [],
2779
+ partial_allowed: bool = False,
2780
+ ) -> Tuple[WalletSpendBundle, List[SigningResponse]]:
2781
+ [unsigned_tx] = await self.gather_signing_info_for_bundles([WalletSpendBundle(coin_spends, G2Element())])
2782
+ signing_responses: List[SigningResponse] = await self.execute_signing_instructions(
2783
+ unsigned_tx.signing_instructions, partial_allowed=partial_allowed
2784
+ )
2785
+ return (
2786
+ self.signed_tx_to_spendbundle(
2787
+ await self.apply_signatures(
2788
+ unsigned_tx.transaction_info.spends,
2789
+ [*additional_signing_responses, *signing_responses],
2790
+ )
2791
+ ),
2792
+ signing_responses,
2793
+ )
2794
+
2795
+ async def submit_transactions(self, signed_txs: List[SignedTransaction]) -> List[bytes32]:
2796
+ bundles: List[WalletSpendBundle] = [self.signed_tx_to_spendbundle(tx) for tx in signed_txs]
2797
+ for bundle in bundles:
2798
+ await self.wallet_node.push_tx(bundle)
2799
+ return [bundle.name() for bundle in bundles]
2800
+
2801
+ @contextlib.asynccontextmanager
2802
+ async def new_action_scope(
2803
+ self,
2804
+ tx_config: TXConfig,
2805
+ push: bool = False,
2806
+ merge_spends: bool = True,
2807
+ sign: Optional[bool] = None,
2808
+ additional_signing_responses: List[SigningResponse] = [],
2809
+ extra_spends: List[WalletSpendBundle] = [],
2810
+ ) -> AsyncIterator[WalletActionScope]:
2811
+ async with new_wallet_action_scope(
2812
+ self,
2813
+ tx_config,
2814
+ push=push,
2815
+ merge_spends=merge_spends,
2816
+ sign=sign,
2817
+ additional_signing_responses=additional_signing_responses,
2818
+ extra_spends=extra_spends,
2819
+ ) as action_scope:
2820
+ yield action_scope