async def save_info(self, did_info: DIDInfo, in_transaction: bool): self.did_info = did_info current_info = self.wallet_info data_str = json.dumps(did_info.to_json_dict()) wallet_info = WalletInfo(current_info.id, current_info.name, current_info.type, data_str) self.wallet_info = wallet_info await self.wallet_state_manager.user_store.update_wallet(wallet_info, in_transaction)
async def save_info(self, cc_info: CCInfo, in_transaction): self.cc_info = cc_info current_info = self.wallet_info data_str = bytes(cc_info).hex() wallet_info = WalletInfo(current_info.id, current_info.name, current_info.type, data_str) self.wallet_info = wallet_info await self.wallet_state_manager.user_store.update_wallet(wallet_info, in_transaction)
async def get_wallet_by_id(self, id: int) -> Optional[WalletInfo]: """ Return a wallet by id """ cursor = await self.db_connection.execute( "SELECT * from users_wallets WHERE id=?", (id, )) row = await cursor.fetchone() await cursor.close() if row is None: return None return WalletInfo(row[0], row[1], row[2], row[3])
async def get_all_wallet_info_entries(self) -> List[WalletInfo]: """ Return a set containing all wallets """ cursor = await self.db_connection.execute("SELECT * from users_wallets" ) rows = await cursor.fetchall() await cursor.close() result = [] for row in rows: result.append(WalletInfo(row[0], row[1], row[2], row[3])) return result
async def set_user_info( self, interval: uint64, limit: uint64, origin_parent_id: str, origin_puzzle_hash: str, origin_amount: uint64, admin_pubkey: str, ) -> None: admin_pubkey_bytes = hexstr_to_bytes(admin_pubkey) assert self.rl_info.user_pubkey is not None origin = Coin( hexstr_to_bytes(origin_parent_id), hexstr_to_bytes(origin_puzzle_hash), origin_amount, ) rl_puzzle = rl_puzzle_for_pk( pubkey=self.rl_info.user_pubkey, rate_amount=limit, interval_time=interval, origin_id=origin.name(), clawback_pk=admin_pubkey_bytes, ) rl_puzzle_hash = rl_puzzle.get_tree_hash() new_rl_info = RLInfo( "user", admin_pubkey_bytes, self.rl_info.user_pubkey, limit, interval, origin, origin.name(), rl_puzzle_hash, True, ) rl_puzzle_hash = rl_puzzle.get_tree_hash() if await self.wallet_state_manager.puzzle_store.puzzle_hash_exists( rl_puzzle_hash): raise ValueError( "Cannot create multiple Rate Limited wallets under the same keys. This will change in a future release." ) user_pubkey: G1Element = G1Element.from_bytes(self.rl_info.user_pubkey) index = await self.wallet_state_manager.puzzle_store.index_for_pubkey( user_pubkey) assert index is not None record = DerivationRecord( index, rl_puzzle_hash, user_pubkey, WalletType.RATE_LIMITED, self.id(), ) aggregation_puzzlehash = self.rl_get_aggregation_puzzlehash( new_rl_info.rl_puzzle_hash) record2 = DerivationRecord( index + 1, aggregation_puzzlehash, user_pubkey, WalletType.RATE_LIMITED, self.id(), ) await self.wallet_state_manager.puzzle_store.add_derivation_paths( [record, record2]) self.wallet_state_manager.set_coin_with_puzzlehash_created_callback( aggregation_puzzlehash, self.aggregate_this_coin) data_str = json.dumps(new_rl_info.to_json_dict()) new_wallet_info = WalletInfo(self.id(), self.wallet_info.name, self.type(), data_str) await self.wallet_state_manager.user_store.update_wallet( new_wallet_info, False) await self.wallet_state_manager.add_new_wallet(self, self.id()) self.wallet_info = new_wallet_info self.rl_info = new_rl_info
async def admin_create_coin( self, interval: uint64, limit: uint64, user_pubkey: str, amount: uint64, fee: uint64, ) -> bool: coins = await self.wallet_state_manager.main_wallet.select_coins(amount ) if coins is None: return False origin = coins.copy().pop() origin_id = origin.name() user_pubkey_bytes = hexstr_to_bytes(user_pubkey) assert self.rl_info.admin_pubkey is not None rl_puzzle = rl_puzzle_for_pk( pubkey=user_pubkey_bytes, rate_amount=limit, interval_time=interval, origin_id=origin_id, clawback_pk=self.rl_info.admin_pubkey, ) rl_puzzle_hash = rl_puzzle.get_tree_hash() index = await self.wallet_state_manager.puzzle_store.index_for_pubkey( G1Element.from_bytes(self.rl_info.admin_pubkey)) assert index is not None record = DerivationRecord( index, rl_puzzle_hash, G1Element.from_bytes(self.rl_info.admin_pubkey), WalletType.RATE_LIMITED, self.id(), ) await self.wallet_state_manager.puzzle_store.add_derivation_paths( [record]) spend_bundle = await self.main_wallet.generate_signed_transaction( amount, rl_puzzle_hash, fee, origin_id, coins) if spend_bundle is None: return False await self.main_wallet.push_transaction(spend_bundle) new_rl_info = RLInfo( "admin", self.rl_info.admin_pubkey, user_pubkey_bytes, limit, interval, origin, origin.name(), rl_puzzle_hash, True, ) data_str = json.dumps(new_rl_info.to_json_dict()) new_wallet_info = WalletInfo(self.id(), self.wallet_info.name, self.type(), data_str) await self.wallet_state_manager.user_store.update_wallet( new_wallet_info, False) await self.wallet_state_manager.add_new_wallet(self, self.id()) self.wallet_info = new_wallet_info self.rl_info = new_rl_info return True