2020-11-11 20:22:29 +03:00
|
|
|
import logging
|
2020-04-28 04:42:08 +03:00
|
|
|
import time
|
2021-03-10 05:27:27 +03:00
|
|
|
from typing import Any, Dict, List, Optional, Set
|
2020-11-11 20:22:29 +03:00
|
|
|
|
2020-10-02 23:47:05 +03:00
|
|
|
from blspy import G1Element
|
2020-11-11 20:22:29 +03:00
|
|
|
|
2021-04-17 09:13:22 +03:00
|
|
|
from chia.consensus.cost_calculator import calculate_cost_of_program, NPCResult
|
|
|
|
from chia.full_node.bundle_tools import simple_solution_generator
|
|
|
|
from chia.full_node.mempool_check_conditions import get_name_puzzle_conditions
|
2021-04-04 06:55:26 +03:00
|
|
|
from chia.types.blockchain_format.coin import Coin
|
2021-04-29 21:13:00 +03:00
|
|
|
from chia.types.blockchain_format.program import Program, SerializedProgram
|
|
|
|
from chia.types.announcement import Announcement
|
2021-04-04 06:55:26 +03:00
|
|
|
from chia.types.blockchain_format.sized_bytes import bytes32
|
|
|
|
from chia.types.coin_solution import CoinSolution
|
2021-04-17 09:13:22 +03:00
|
|
|
from chia.types.generator_types import BlockGenerator
|
2021-04-04 06:55:26 +03:00
|
|
|
from chia.types.spend_bundle import SpendBundle
|
|
|
|
from chia.util.ints import uint8, uint32, uint64, uint128
|
2021-04-29 21:13:00 +03:00
|
|
|
from chia.util.hash import std_hash
|
2021-04-04 06:55:26 +03:00
|
|
|
from chia.wallet.derivation_record import DerivationRecord
|
|
|
|
from chia.wallet.puzzles.p2_delegated_puzzle_or_hidden_puzzle import (
|
2020-11-11 20:22:29 +03:00
|
|
|
DEFAULT_HIDDEN_PUZZLE_HASH,
|
|
|
|
calculate_synthetic_secret_key,
|
2021-03-10 05:27:27 +03:00
|
|
|
puzzle_for_pk,
|
|
|
|
solution_for_conditions,
|
2020-09-17 02:41:06 +03:00
|
|
|
)
|
2021-04-04 06:55:26 +03:00
|
|
|
from chia.wallet.puzzles.puzzle_utils import (
|
2021-04-14 21:28:18 +03:00
|
|
|
make_assert_coin_announcement,
|
|
|
|
make_assert_puzzle_announcement,
|
2020-02-13 22:57:40 +03:00
|
|
|
make_assert_my_coin_id_condition,
|
2021-04-15 23:00:14 +03:00
|
|
|
make_assert_absolute_seconds_exceeds_condition,
|
2021-04-14 21:28:18 +03:00
|
|
|
make_create_coin_announcement,
|
|
|
|
make_create_puzzle_announcement,
|
2020-02-13 22:57:40 +03:00
|
|
|
make_create_coin_condition,
|
2021-02-23 06:53:00 +03:00
|
|
|
make_reserve_fee_condition,
|
2020-04-20 08:10:19 +03:00
|
|
|
)
|
2021-04-04 06:55:26 +03:00
|
|
|
from chia.wallet.secret_key_store import SecretKeyStore
|
|
|
|
from chia.wallet.sign_coin_solutions import sign_coin_solutions
|
|
|
|
from chia.wallet.transaction_record import TransactionRecord
|
|
|
|
from chia.wallet.util.transaction_type import TransactionType
|
|
|
|
from chia.wallet.util.wallet_types import WalletType
|
|
|
|
from chia.wallet.wallet_coin_record import WalletCoinRecord
|
|
|
|
from chia.wallet.wallet_info import WalletInfo
|
2020-02-24 20:16:47 +03:00
|
|
|
|
2020-02-12 00:00:41 +03:00
|
|
|
|
2020-09-24 03:06:42 +03:00
|
|
|
class Wallet:
|
2020-04-07 12:17:44 +03:00
|
|
|
wallet_state_manager: Any
|
2020-02-13 22:57:40 +03:00
|
|
|
log: logging.Logger
|
2020-09-24 03:56:03 +03:00
|
|
|
wallet_id: uint32
|
2020-10-02 23:47:05 +03:00
|
|
|
secret_key_store: SecretKeyStore
|
2021-02-16 02:17:22 +03:00
|
|
|
cost_of_single_tx: Optional[int]
|
2020-02-13 23:59:03 +03:00
|
|
|
|
2020-02-13 22:57:40 +03:00
|
|
|
@staticmethod
|
2020-02-24 21:41:54 +03:00
|
|
|
async def create(
|
2020-09-15 19:56:04 +03:00
|
|
|
wallet_state_manager: Any,
|
|
|
|
info: WalletInfo,
|
|
|
|
name: str = None,
|
2020-02-24 21:41:54 +03:00
|
|
|
):
|
2020-02-13 22:57:40 +03:00
|
|
|
self = Wallet()
|
2021-06-14 19:13:41 +03:00
|
|
|
self.log = logging.getLogger(name if name else __name__)
|
2020-02-24 20:16:47 +03:00
|
|
|
self.wallet_state_manager = wallet_state_manager
|
2020-09-24 03:56:03 +03:00
|
|
|
self.wallet_id = info.id
|
2020-10-02 23:47:05 +03:00
|
|
|
self.secret_key_store = SecretKeyStore()
|
2021-02-16 02:17:22 +03:00
|
|
|
self.cost_of_single_tx = None
|
2020-02-13 22:57:40 +03:00
|
|
|
return self
|
2020-02-12 04:01:39 +03:00
|
|
|
|
2021-02-16 02:17:22 +03:00
|
|
|
async def get_max_send_amount(self, records=None):
|
|
|
|
spendable: List[WalletCoinRecord] = list(
|
|
|
|
await self.wallet_state_manager.get_spendable_coins_for_wallet(self.id(), records)
|
|
|
|
)
|
|
|
|
if len(spendable) == 0:
|
|
|
|
return 0
|
|
|
|
spendable.sort(reverse=True, key=lambda record: record.coin.amount)
|
|
|
|
if self.cost_of_single_tx is None:
|
|
|
|
coin = spendable[0].coin
|
|
|
|
tx = await self.generate_signed_transaction(
|
2021-02-17 09:47:36 +03:00
|
|
|
coin.amount, coin.puzzle_hash, coins={coin}, ignore_max_send_amount=True
|
2021-02-16 02:17:22 +03:00
|
|
|
)
|
2021-04-17 09:13:22 +03:00
|
|
|
program: BlockGenerator = simple_solution_generator(tx.spend_bundle)
|
2021-02-16 02:17:22 +03:00
|
|
|
# npc contains names of the coins removed, puzzle_hashes and their spend conditions
|
2021-04-16 12:34:54 +03:00
|
|
|
result: NPCResult = get_name_puzzle_conditions(
|
2021-06-23 19:49:23 +03:00
|
|
|
program,
|
|
|
|
self.wallet_state_manager.constants.MAX_BLOCK_COST_CLVM,
|
|
|
|
cost_per_byte=self.wallet_state_manager.constants.COST_PER_BYTE,
|
|
|
|
safe_mode=True,
|
2021-04-16 12:34:54 +03:00
|
|
|
)
|
2021-04-17 09:13:22 +03:00
|
|
|
cost_result: uint64 = calculate_cost_of_program(
|
|
|
|
program.program, result, self.wallet_state_manager.constants.COST_PER_BYTE
|
2021-02-16 02:17:22 +03:00
|
|
|
)
|
2021-04-17 09:13:22 +03:00
|
|
|
self.cost_of_single_tx = cost_result
|
2021-02-16 02:17:22 +03:00
|
|
|
self.log.info(f"Cost of a single tx for standard wallet: {self.cost_of_single_tx}")
|
|
|
|
|
2021-04-18 09:57:07 +03:00
|
|
|
max_cost = self.wallet_state_manager.constants.MAX_BLOCK_COST_CLVM / 5 # avoid full block TXs
|
2021-02-16 02:17:22 +03:00
|
|
|
current_cost = 0
|
|
|
|
total_amount = 0
|
|
|
|
total_coin_count = 0
|
|
|
|
for record in spendable:
|
|
|
|
current_cost += self.cost_of_single_tx
|
|
|
|
total_amount += record.coin.amount
|
|
|
|
total_coin_count += 1
|
|
|
|
if current_cost + self.cost_of_single_tx > max_cost:
|
|
|
|
break
|
|
|
|
|
|
|
|
return total_amount
|
|
|
|
|
2020-09-24 03:47:06 +03:00
|
|
|
@classmethod
|
2020-09-24 03:56:03 +03:00
|
|
|
def type(cls) -> uint8:
|
2020-09-24 07:08:32 +03:00
|
|
|
return uint8(WalletType.STANDARD_WALLET)
|
2020-09-24 03:56:03 +03:00
|
|
|
|
2021-04-02 20:37:01 +03:00
|
|
|
def id(self) -> uint32:
|
2020-09-24 03:56:03 +03:00
|
|
|
return self.wallet_id
|
2020-09-24 03:47:06 +03:00
|
|
|
|
2021-02-11 06:53:38 +03:00
|
|
|
async def get_confirmed_balance(self, unspent_records=None) -> uint128:
|
2021-01-16 07:04:34 +03:00
|
|
|
return await self.wallet_state_manager.get_confirmed_balance_for_wallet(self.id(), unspent_records)
|
2020-02-13 22:57:40 +03:00
|
|
|
|
2021-02-11 06:53:38 +03:00
|
|
|
async def get_unconfirmed_balance(self, unspent_records=None) -> uint128:
|
2021-01-16 07:04:34 +03:00
|
|
|
return await self.wallet_state_manager.get_unconfirmed_balance(self.id(), unspent_records)
|
2020-02-13 23:59:03 +03:00
|
|
|
|
2021-02-11 06:53:38 +03:00
|
|
|
async def get_spendable_balance(self, unspent_records=None) -> uint128:
|
2021-01-16 07:04:34 +03:00
|
|
|
spendable = await self.wallet_state_manager.get_confirmed_spendable_balance_for_wallet(
|
|
|
|
self.id(), unspent_records
|
|
|
|
)
|
|
|
|
return spendable
|
2020-05-20 10:41:10 +03:00
|
|
|
|
|
|
|
async def get_pending_change_balance(self) -> uint64:
|
2020-12-11 10:27:03 +03:00
|
|
|
unconfirmed_tx = await self.wallet_state_manager.tx_store.get_unconfirmed_for_wallet(self.id())
|
2020-05-20 10:41:10 +03:00
|
|
|
addition_amount = 0
|
|
|
|
|
|
|
|
for record in unconfirmed_tx:
|
2021-02-23 07:16:41 +03:00
|
|
|
if not record.is_in_mempool():
|
2021-05-01 22:13:04 +03:00
|
|
|
self.log.warning(f"Record: {record} not in mempool")
|
2021-02-23 07:16:41 +03:00
|
|
|
continue
|
2020-05-20 10:41:10 +03:00
|
|
|
our_spend = False
|
|
|
|
for coin in record.removals:
|
2020-12-11 10:27:03 +03:00
|
|
|
if await self.wallet_state_manager.does_coin_belong_to_wallet(coin, self.id()):
|
2020-05-20 10:41:10 +03:00
|
|
|
our_spend = True
|
|
|
|
break
|
|
|
|
|
|
|
|
if our_spend is not True:
|
|
|
|
continue
|
|
|
|
|
|
|
|
for coin in record.additions:
|
2020-12-11 10:27:03 +03:00
|
|
|
if await self.wallet_state_manager.does_coin_belong_to_wallet(coin, self.id()):
|
2020-05-20 10:41:10 +03:00
|
|
|
addition_amount += coin.amount
|
|
|
|
|
|
|
|
return uint64(addition_amount)
|
|
|
|
|
2020-03-27 17:30:07 +03:00
|
|
|
def puzzle_for_pk(self, pubkey: bytes) -> Program:
|
2020-02-12 04:01:39 +03:00
|
|
|
return puzzle_for_pk(pubkey)
|
|
|
|
|
2020-12-11 10:27:03 +03:00
|
|
|
async def hack_populate_secret_key_for_puzzle_hash(self, puzzle_hash: bytes32) -> G1Element:
|
2020-09-17 01:49:18 +03:00
|
|
|
maybe = await self.wallet_state_manager.get_keys(puzzle_hash)
|
|
|
|
if maybe is None:
|
|
|
|
error_msg = f"Wallet couldn't find keys for puzzle_hash {puzzle_hash}"
|
|
|
|
self.log.error(error_msg)
|
|
|
|
raise ValueError(error_msg)
|
|
|
|
|
|
|
|
# Get puzzle for pubkey
|
|
|
|
public_key, secret_key = maybe
|
|
|
|
|
|
|
|
# HACK
|
2020-12-11 10:27:03 +03:00
|
|
|
synthetic_secret_key = calculate_synthetic_secret_key(secret_key, DEFAULT_HIDDEN_PUZZLE_HASH)
|
2020-11-11 20:22:29 +03:00
|
|
|
self.secret_key_store.save_secret_key(synthetic_secret_key)
|
|
|
|
|
2020-09-17 01:53:22 +03:00
|
|
|
return public_key
|
2020-09-17 01:49:18 +03:00
|
|
|
|
2020-12-11 10:27:03 +03:00
|
|
|
async def hack_populate_secret_keys_for_coin_solutions(self, coin_solutions: List[CoinSolution]) -> None:
|
2020-10-02 23:47:05 +03:00
|
|
|
"""
|
|
|
|
This hack forces secret keys into the `_pk2sk` lookup. This should eventually be replaced
|
|
|
|
by a persistent DB table that can do this look-up directly.
|
|
|
|
"""
|
|
|
|
for coin_solution in coin_solutions:
|
2020-12-11 10:27:03 +03:00
|
|
|
await self.hack_populate_secret_key_for_puzzle_hash(coin_solution.coin.puzzle_hash)
|
2020-10-02 23:47:05 +03:00
|
|
|
|
2020-09-17 01:53:22 +03:00
|
|
|
async def puzzle_for_puzzle_hash(self, puzzle_hash: bytes32) -> Program:
|
|
|
|
public_key = await self.hack_populate_secret_key_for_puzzle_hash(puzzle_hash)
|
2020-09-17 01:49:18 +03:00
|
|
|
return puzzle_for_pk(bytes(public_key))
|
|
|
|
|
2020-04-10 17:53:31 +03:00
|
|
|
async def get_new_puzzle(self) -> Program:
|
2020-09-24 03:56:03 +03:00
|
|
|
dr = await self.wallet_state_manager.get_unused_derivation_record(self.id())
|
2020-08-13 03:08:05 +03:00
|
|
|
return puzzle_for_pk(bytes(dr.pubkey))
|
2020-04-10 17:53:31 +03:00
|
|
|
|
2021-03-12 06:36:28 +03:00
|
|
|
async def get_puzzle_hash(self, new: bool):
|
|
|
|
if new:
|
|
|
|
return await self.get_new_puzzlehash()
|
|
|
|
else:
|
|
|
|
record: Optional[
|
|
|
|
DerivationRecord
|
|
|
|
] = await self.wallet_state_manager.get_current_derivation_record_for_wallet(self.id())
|
|
|
|
if record is None:
|
|
|
|
return await self.get_new_puzzlehash()
|
|
|
|
return record.puzzle_hash
|
|
|
|
|
2020-02-27 23:37:32 +03:00
|
|
|
async def get_new_puzzlehash(self) -> bytes32:
|
2020-12-11 10:27:03 +03:00
|
|
|
return (await self.wallet_state_manager.get_unused_derivation_record(self.id())).puzzle_hash
|
2020-02-12 04:01:39 +03:00
|
|
|
|
2021-02-10 21:28:13 +03:00
|
|
|
def make_solution(
|
|
|
|
self,
|
2021-03-31 11:04:10 +03:00
|
|
|
primaries: Optional[List[Dict[str, Any]]] = None,
|
2021-02-10 21:28:13 +03:00
|
|
|
min_time=0,
|
|
|
|
me=None,
|
2021-04-29 20:30:24 +03:00
|
|
|
coin_announcements: Optional[List[bytes32]] = None,
|
|
|
|
coin_announcements_to_assert: Optional[List[bytes32]] = None,
|
2021-04-14 21:28:18 +03:00
|
|
|
puzzle_announcements=None,
|
|
|
|
puzzle_announcements_to_assert=None,
|
2021-02-10 21:28:13 +03:00
|
|
|
fee=0,
|
2021-02-21 22:24:57 +03:00
|
|
|
) -> Program:
|
2020-09-16 03:09:17 +03:00
|
|
|
assert fee >= 0
|
2020-03-06 02:53:36 +03:00
|
|
|
condition_list = []
|
2020-02-13 22:57:40 +03:00
|
|
|
if primaries:
|
|
|
|
for primary in primaries:
|
2020-12-11 10:27:03 +03:00
|
|
|
condition_list.append(make_create_coin_condition(primary["puzzlehash"], primary["amount"]))
|
2020-02-12 04:01:39 +03:00
|
|
|
if min_time > 0:
|
2021-04-15 23:00:14 +03:00
|
|
|
condition_list.append(make_assert_absolute_seconds_exceeds_condition(min_time))
|
2020-02-12 04:01:39 +03:00
|
|
|
if me:
|
2020-03-06 02:53:36 +03:00
|
|
|
condition_list.append(make_assert_my_coin_id_condition(me["id"]))
|
2020-04-20 08:08:16 +03:00
|
|
|
if fee:
|
2021-02-23 06:53:00 +03:00
|
|
|
condition_list.append(make_reserve_fee_condition(fee))
|
2021-04-14 21:28:18 +03:00
|
|
|
if coin_announcements:
|
|
|
|
for announcement in coin_announcements:
|
|
|
|
condition_list.append(make_create_coin_announcement(announcement))
|
|
|
|
if coin_announcements_to_assert:
|
|
|
|
for announcement_hash in coin_announcements_to_assert:
|
|
|
|
condition_list.append(make_assert_coin_announcement(announcement_hash))
|
|
|
|
if puzzle_announcements:
|
|
|
|
for announcement in puzzle_announcements:
|
|
|
|
condition_list.append(make_create_puzzle_announcement(announcement))
|
|
|
|
if puzzle_announcements_to_assert:
|
|
|
|
for announcement_hash in puzzle_announcements_to_assert:
|
|
|
|
condition_list.append(make_assert_puzzle_announcement(announcement_hash))
|
2020-09-17 02:31:30 +03:00
|
|
|
return solution_for_conditions(condition_list)
|
2020-02-12 04:01:39 +03:00
|
|
|
|
2020-09-14 12:29:03 +03:00
|
|
|
async def select_coins(self, amount, exclude: List[Coin] = None) -> Set[Coin]:
|
2021-05-01 22:13:04 +03:00
|
|
|
"""
|
|
|
|
Returns a set of coins that can be used for generating a new transaction.
|
|
|
|
Note: This must be called under a wallet state manager lock
|
|
|
|
"""
|
|
|
|
if exclude is None:
|
|
|
|
exclude = []
|
2020-04-22 08:59:48 +03:00
|
|
|
|
2021-05-01 22:13:04 +03:00
|
|
|
spendable_amount = await self.get_spendable_balance()
|
2020-03-19 11:26:51 +03:00
|
|
|
|
2021-05-01 22:13:04 +03:00
|
|
|
if amount > spendable_amount:
|
|
|
|
error_msg = (
|
|
|
|
f"Can't select amount higher than our spendable balance. Amount: {amount}, spendable: "
|
|
|
|
f" {spendable_amount}"
|
2020-04-02 20:33:38 +03:00
|
|
|
)
|
2021-05-01 22:13:04 +03:00
|
|
|
self.log.warning(error_msg)
|
|
|
|
raise ValueError(error_msg)
|
2020-04-08 09:29:34 +03:00
|
|
|
|
2021-05-01 22:13:04 +03:00
|
|
|
self.log.info(f"About to select coins for amount {amount}")
|
|
|
|
unspent: List[WalletCoinRecord] = list(
|
|
|
|
await self.wallet_state_manager.get_spendable_coins_for_wallet(self.id())
|
|
|
|
)
|
|
|
|
sum_value = 0
|
|
|
|
used_coins: Set = set()
|
2020-04-08 09:29:34 +03:00
|
|
|
|
2021-05-01 22:13:04 +03:00
|
|
|
# Use older coins first
|
|
|
|
unspent.sort(reverse=True, key=lambda r: r.coin.amount)
|
2020-03-28 00:03:48 +03:00
|
|
|
|
2021-05-01 22:13:04 +03:00
|
|
|
# Try to use coins from the store, if there isn't enough of "unused"
|
|
|
|
# coins use change coins that are not confirmed yet
|
|
|
|
unconfirmed_removals: Dict[bytes32, Coin] = await self.wallet_state_manager.unconfirmed_removals_for_wallet(
|
|
|
|
self.id()
|
|
|
|
)
|
|
|
|
for coinrecord in unspent:
|
|
|
|
if sum_value >= amount and len(used_coins) > 0:
|
|
|
|
break
|
|
|
|
if coinrecord.coin.name() in unconfirmed_removals:
|
|
|
|
continue
|
|
|
|
if coinrecord.coin in exclude:
|
|
|
|
continue
|
|
|
|
sum_value += coinrecord.coin.amount
|
|
|
|
used_coins.add(coinrecord.coin)
|
|
|
|
self.log.debug(f"Selected coin: {coinrecord.coin.name()} at height {coinrecord.confirmed_block_height}!")
|
|
|
|
|
|
|
|
# This happens when we couldn't use one of the coins because it's already used
|
|
|
|
# but unconfirmed, and we are waiting for the change. (unconfirmed_additions)
|
|
|
|
if sum_value < amount:
|
|
|
|
raise ValueError(
|
|
|
|
"Can't make this transaction at the moment. Waiting for the change from the previous transaction."
|
|
|
|
)
|
2020-12-11 21:44:15 +03:00
|
|
|
|
2021-02-17 09:47:36 +03:00
|
|
|
self.log.debug(f"Successfully selected coins: {used_coins}")
|
2020-06-17 02:46:51 +03:00
|
|
|
return used_coins
|
2020-03-19 11:26:51 +03:00
|
|
|
|
2021-05-01 22:13:04 +03:00
|
|
|
async def _generate_unsigned_transaction(
|
2020-03-23 22:59:53 +03:00
|
|
|
self,
|
2020-03-27 17:30:07 +03:00
|
|
|
amount: uint64,
|
2020-03-23 22:59:53 +03:00
|
|
|
newpuzzlehash: bytes32,
|
2020-03-27 17:30:07 +03:00
|
|
|
fee: uint64 = uint64(0),
|
2020-03-23 22:59:53 +03:00
|
|
|
origin_id: bytes32 = None,
|
|
|
|
coins: Set[Coin] = None,
|
2021-03-31 11:04:10 +03:00
|
|
|
primaries_input: Optional[List[Dict[str, Any]]] = None,
|
2021-02-17 09:47:36 +03:00
|
|
|
ignore_max_send_amount: bool = False,
|
2020-09-17 00:25:10 +03:00
|
|
|
) -> List[CoinSolution]:
|
2020-03-06 02:53:36 +03:00
|
|
|
"""
|
|
|
|
Generates a unsigned transaction in form of List(Puzzle, Solutions)
|
2021-05-01 22:13:04 +03:00
|
|
|
Note: this must be called under a wallet state manager lock
|
2020-03-06 02:53:36 +03:00
|
|
|
"""
|
2021-03-31 11:04:10 +03:00
|
|
|
if primaries_input is None:
|
2021-04-29 21:13:00 +03:00
|
|
|
primaries: Optional[List[Dict]] = None
|
2021-02-16 02:17:22 +03:00
|
|
|
total_amount = amount + fee
|
|
|
|
else:
|
2021-03-31 11:04:10 +03:00
|
|
|
primaries = primaries_input.copy()
|
2021-02-16 02:17:22 +03:00
|
|
|
primaries_amount = 0
|
|
|
|
for prim in primaries:
|
|
|
|
primaries_amount += prim["amount"]
|
|
|
|
total_amount = amount + fee + primaries_amount
|
|
|
|
|
2021-02-17 09:47:36 +03:00
|
|
|
if not ignore_max_send_amount:
|
2021-02-16 02:17:22 +03:00
|
|
|
max_send = await self.get_max_send_amount()
|
|
|
|
if total_amount > max_send:
|
|
|
|
raise ValueError(f"Can't send more than {max_send} in a single transaction")
|
|
|
|
|
2020-03-23 22:46:17 +03:00
|
|
|
if coins is None:
|
2021-02-16 02:17:22 +03:00
|
|
|
coins = await self.select_coins(total_amount)
|
2020-09-14 12:29:03 +03:00
|
|
|
assert len(coins) > 0
|
2020-03-06 02:53:36 +03:00
|
|
|
|
2020-04-01 04:59:14 +03:00
|
|
|
self.log.info(f"coins is not None {coins}")
|
2020-03-23 22:46:17 +03:00
|
|
|
spend_value = sum([coin.amount for coin in coins])
|
2021-02-16 02:17:22 +03:00
|
|
|
change = spend_value - total_amount
|
2021-04-29 20:30:24 +03:00
|
|
|
assert change >= 0
|
2020-03-06 02:53:36 +03:00
|
|
|
|
2020-09-17 00:25:10 +03:00
|
|
|
spends: List[CoinSolution] = []
|
2021-04-29 21:13:00 +03:00
|
|
|
primary_announcement_hash: Optional[bytes32] = None
|
2020-03-06 02:53:36 +03:00
|
|
|
|
2021-04-21 21:17:21 +03:00
|
|
|
# Check for duplicates
|
|
|
|
if primaries is not None:
|
|
|
|
all_primaries_list = [(p["puzzlehash"], p["amount"]) for p in primaries] + [(newpuzzlehash, amount)]
|
|
|
|
if len(set(all_primaries_list)) != len(all_primaries_list):
|
|
|
|
raise ValueError("Cannot create two identical coins")
|
|
|
|
|
2020-03-23 22:46:17 +03:00
|
|
|
for coin in coins:
|
2020-04-01 04:59:14 +03:00
|
|
|
self.log.info(f"coin from coins {coin}")
|
2020-09-17 01:49:18 +03:00
|
|
|
puzzle: Program = await self.puzzle_for_puzzle_hash(coin.puzzle_hash)
|
2020-03-06 02:53:36 +03:00
|
|
|
|
|
|
|
# Only one coin creates outputs
|
2021-04-29 21:13:00 +03:00
|
|
|
if primary_announcement_hash is None and origin_id in (None, coin.name()):
|
2021-02-16 02:17:22 +03:00
|
|
|
if primaries is None:
|
|
|
|
primaries = [{"puzzlehash": newpuzzlehash, "amount": amount}]
|
|
|
|
else:
|
|
|
|
primaries.append({"puzzlehash": newpuzzlehash, "amount": amount})
|
2020-02-12 04:01:39 +03:00
|
|
|
if change > 0:
|
2021-04-29 21:13:00 +03:00
|
|
|
change_puzzle_hash: bytes32 = await self.get_new_puzzlehash()
|
|
|
|
primaries.append({"puzzlehash": change_puzzle_hash, "amount": change})
|
|
|
|
message_list: List[bytes32] = [c.name() for c in coins]
|
|
|
|
for primary in primaries:
|
|
|
|
message_list.append(Coin(coin.name(), primary["puzzlehash"], primary["amount"]).name())
|
|
|
|
message: bytes32 = std_hash(b"".join(message_list))
|
|
|
|
solution: Program = self.make_solution(primaries=primaries, fee=fee, coin_announcements=[message])
|
|
|
|
primary_announcement_hash = Announcement(coin.name(), message).name()
|
2020-02-12 04:01:39 +03:00
|
|
|
else:
|
2021-04-29 21:13:00 +03:00
|
|
|
solution = self.make_solution(coin_announcements_to_assert=[primary_announcement_hash])
|
2020-03-06 02:53:36 +03:00
|
|
|
|
2021-04-29 21:13:00 +03:00
|
|
|
spends.append(
|
|
|
|
CoinSolution(
|
|
|
|
coin, SerializedProgram.from_bytes(bytes(puzzle)), SerializedProgram.from_bytes(bytes(solution))
|
|
|
|
)
|
|
|
|
)
|
2020-04-01 04:59:14 +03:00
|
|
|
|
|
|
|
self.log.info(f"Spends is {spends}")
|
2020-02-12 04:01:39 +03:00
|
|
|
return spends
|
|
|
|
|
2020-09-17 02:41:06 +03:00
|
|
|
async def sign_transaction(self, coin_solutions: List[CoinSolution]) -> SpendBundle:
|
2021-04-14 08:03:14 +03:00
|
|
|
return await sign_coin_solutions(
|
|
|
|
coin_solutions,
|
|
|
|
self.secret_key_store.secret_key_for_public_key,
|
|
|
|
self.wallet_state_manager.constants.AGG_SIG_ME_ADDITIONAL_DATA,
|
2021-04-16 12:34:54 +03:00
|
|
|
self.wallet_state_manager.constants.MAX_BLOCK_COST_CLVM,
|
2021-04-14 08:03:14 +03:00
|
|
|
)
|
2020-09-17 00:25:10 +03:00
|
|
|
|
2020-02-13 22:57:40 +03:00
|
|
|
async def generate_signed_transaction(
|
2020-03-23 22:59:53 +03:00
|
|
|
self,
|
2020-03-27 17:30:07 +03:00
|
|
|
amount: uint64,
|
|
|
|
puzzle_hash: bytes32,
|
|
|
|
fee: uint64 = uint64(0),
|
2020-03-23 22:59:53 +03:00
|
|
|
origin_id: bytes32 = None,
|
|
|
|
coins: Set[Coin] = None,
|
2021-02-16 02:17:22 +03:00
|
|
|
primaries: Optional[List[Dict[str, bytes32]]] = None,
|
2021-02-17 09:47:36 +03:00
|
|
|
ignore_max_send_amount: bool = False,
|
2020-09-14 12:29:03 +03:00
|
|
|
) -> TransactionRecord:
|
2021-05-01 22:13:04 +03:00
|
|
|
"""
|
|
|
|
Use this to generate transaction.
|
|
|
|
Note: this must be called under a wallet state manager lock
|
|
|
|
"""
|
2021-03-31 11:04:10 +03:00
|
|
|
if primaries is None:
|
|
|
|
non_change_amount = amount
|
|
|
|
else:
|
|
|
|
non_change_amount = uint64(amount + sum(p["amount"] for p in primaries))
|
2020-03-19 11:26:51 +03:00
|
|
|
|
2021-05-01 22:13:04 +03:00
|
|
|
transaction = await self._generate_unsigned_transaction(
|
2021-02-17 09:47:36 +03:00
|
|
|
amount, puzzle_hash, fee, origin_id, coins, primaries, ignore_max_send_amount
|
2021-02-16 02:17:22 +03:00
|
|
|
)
|
2020-09-14 12:29:03 +03:00
|
|
|
assert len(transaction) > 0
|
2020-04-01 04:59:14 +03:00
|
|
|
|
2020-04-01 07:00:49 +03:00
|
|
|
self.log.info("About to sign a transaction")
|
2020-10-13 23:26:22 +03:00
|
|
|
await self.hack_populate_secret_keys_for_coin_solutions(transaction)
|
2020-10-02 23:47:05 +03:00
|
|
|
spend_bundle: SpendBundle = await sign_coin_solutions(
|
2021-04-14 08:03:14 +03:00
|
|
|
transaction,
|
|
|
|
self.secret_key_store.secret_key_for_public_key,
|
|
|
|
self.wallet_state_manager.constants.AGG_SIG_ME_ADDITIONAL_DATA,
|
2021-04-16 12:34:54 +03:00
|
|
|
self.wallet_state_manager.constants.MAX_BLOCK_COST_CLVM,
|
2020-10-02 23:47:05 +03:00
|
|
|
)
|
2020-02-13 22:57:40 +03:00
|
|
|
|
2020-04-28 04:42:08 +03:00
|
|
|
now = uint64(int(time.time()))
|
2020-09-17 00:25:10 +03:00
|
|
|
add_list: List[Coin] = list(spend_bundle.additions())
|
|
|
|
rem_list: List[Coin] = list(spend_bundle.removals())
|
2021-03-31 11:04:10 +03:00
|
|
|
assert sum(a.amount for a in add_list) + fee == sum(r.amount for r in rem_list)
|
2020-04-28 04:42:08 +03:00
|
|
|
|
2020-09-14 12:29:03 +03:00
|
|
|
return TransactionRecord(
|
2021-01-08 07:49:15 +03:00
|
|
|
confirmed_at_height=uint32(0),
|
2020-04-28 04:42:08 +03:00
|
|
|
created_at_time=now,
|
|
|
|
to_puzzle_hash=puzzle_hash,
|
2021-03-31 11:04:10 +03:00
|
|
|
amount=uint64(non_change_amount),
|
2020-04-28 04:42:08 +03:00
|
|
|
fee_amount=uint64(fee),
|
|
|
|
confirmed=False,
|
|
|
|
sent=uint32(0),
|
|
|
|
spend_bundle=spend_bundle,
|
|
|
|
additions=add_list,
|
|
|
|
removals=rem_list,
|
2020-09-24 03:56:03 +03:00
|
|
|
wallet_id=self.id(),
|
2020-04-28 04:42:08 +03:00
|
|
|
sent_to=[],
|
2020-06-12 02:24:10 +03:00
|
|
|
trade_id=None,
|
2020-11-25 22:48:20 +03:00
|
|
|
type=uint32(TransactionType.OUTGOING_TX.value),
|
2021-01-08 08:39:44 +03:00
|
|
|
name=spend_bundle.name(),
|
2020-03-19 22:11:58 +03:00
|
|
|
)
|
2020-04-21 16:55:59 +03:00
|
|
|
|
2020-04-28 04:42:08 +03:00
|
|
|
async def push_transaction(self, tx: TransactionRecord) -> None:
|
2021-04-29 21:13:00 +03:00
|
|
|
"""Use this API to send transactions."""
|
2020-04-28 04:42:08 +03:00
|
|
|
await self.wallet_state_manager.add_pending_transaction(tx)
|
|
|
|
|
2020-04-21 16:55:59 +03:00
|
|
|
# This is to be aggregated together with a coloured coin offer to ensure that the trade happens
|
2020-12-11 10:27:03 +03:00
|
|
|
async def create_spend_bundle_relative_chia(self, chia_amount: int, exclude: List[Coin]) -> SpendBundle:
|
2020-04-21 16:55:59 +03:00
|
|
|
list_of_solutions = []
|
|
|
|
utxos = None
|
|
|
|
|
|
|
|
# If we're losing value then get coins with at least that much value
|
|
|
|
# If we're gaining value then our amount doesn't matter
|
|
|
|
if chia_amount < 0:
|
2020-04-24 05:54:10 +03:00
|
|
|
utxos = await self.select_coins(abs(chia_amount), exclude)
|
2020-04-21 16:55:59 +03:00
|
|
|
else:
|
2020-04-24 05:54:10 +03:00
|
|
|
utxos = await self.select_coins(0, exclude)
|
2020-04-21 16:55:59 +03:00
|
|
|
|
2020-09-14 12:29:03 +03:00
|
|
|
assert len(utxos) > 0
|
2020-04-21 16:55:59 +03:00
|
|
|
|
|
|
|
# Calculate output amount given sum of utxos
|
|
|
|
spend_value = sum([coin.amount for coin in utxos])
|
|
|
|
chia_amount = spend_value + chia_amount
|
|
|
|
|
|
|
|
# Create coin solutions for each utxo
|
|
|
|
output_created = None
|
|
|
|
for coin in utxos:
|
2020-09-17 01:49:18 +03:00
|
|
|
puzzle = await self.puzzle_for_puzzle_hash(coin.puzzle_hash)
|
2020-04-21 16:55:59 +03:00
|
|
|
if output_created is None:
|
|
|
|
newpuzhash = await self.get_new_puzzlehash()
|
|
|
|
primaries = [{"puzzlehash": newpuzhash, "amount": chia_amount}]
|
|
|
|
solution = self.make_solution(primaries=primaries)
|
|
|
|
output_created = coin
|
2021-02-21 22:24:57 +03:00
|
|
|
list_of_solutions.append(CoinSolution(coin, puzzle, solution))
|
2020-04-21 16:55:59 +03:00
|
|
|
|
2020-10-13 23:26:22 +03:00
|
|
|
await self.hack_populate_secret_keys_for_coin_solutions(list_of_solutions)
|
2021-04-14 08:03:14 +03:00
|
|
|
spend_bundle = await sign_coin_solutions(
|
|
|
|
list_of_solutions,
|
|
|
|
self.secret_key_store.secret_key_for_public_key,
|
|
|
|
self.wallet_state_manager.constants.AGG_SIG_ME_ADDITIONAL_DATA,
|
2021-04-16 12:34:54 +03:00
|
|
|
self.wallet_state_manager.constants.MAX_BLOCK_COST_CLVM,
|
2021-04-14 08:03:14 +03:00
|
|
|
)
|
2020-09-17 01:53:22 +03:00
|
|
|
return spend_bundle
|