2020-10-02 23:47:05 +03:00
|
|
|
from typing import Callable, List, Optional
|
2021-03-10 05:27:27 +03:00
|
|
|
|
2020-10-02 23:47:05 +03:00
|
|
|
from blspy import AugSchemeMPL, PrivateKey
|
2021-03-10 05:27:27 +03:00
|
|
|
|
2021-04-04 06:55:26 +03:00
|
|
|
from chia.types.coin_solution import CoinSolution
|
|
|
|
from chia.types.spend_bundle import SpendBundle
|
|
|
|
from chia.util.condition_tools import conditions_dict_for_solution, pkm_pairs_for_conditions_dict
|
2020-10-02 23:47:05 +03:00
|
|
|
|
|
|
|
|
|
|
|
async def sign_coin_solutions(
|
|
|
|
coin_solutions: List[CoinSolution],
|
|
|
|
secret_key_for_public_key_f: Callable[[bytes], Optional[PrivateKey]],
|
|
|
|
) -> SpendBundle:
|
|
|
|
signatures = []
|
2020-11-03 17:35:54 +03:00
|
|
|
pk_list = []
|
|
|
|
msg_list = []
|
2020-10-02 23:47:05 +03:00
|
|
|
for coin_solution in coin_solutions:
|
2021-02-18 05:28:21 +03:00
|
|
|
# Get AGG_SIG conditions
|
2021-02-21 22:24:57 +03:00
|
|
|
err, conditions_dict, cost = conditions_dict_for_solution(coin_solution.puzzle_reveal, coin_solution.solution)
|
2020-10-02 23:47:05 +03:00
|
|
|
if err or conditions_dict is None:
|
|
|
|
error_msg = f"Sign transaction failed, con:{conditions_dict}, error: {err}"
|
|
|
|
raise ValueError(error_msg)
|
|
|
|
|
|
|
|
# Create signature
|
2020-12-11 10:27:03 +03:00
|
|
|
for _, msg in pkm_pairs_for_conditions_dict(conditions_dict, bytes(coin_solution.coin.name())):
|
2020-11-03 17:35:54 +03:00
|
|
|
pk_list.append(_)
|
|
|
|
msg_list.append(msg)
|
2020-10-02 23:47:05 +03:00
|
|
|
secret_key = secret_key_for_public_key_f(_)
|
|
|
|
if secret_key is None:
|
|
|
|
e_msg = f"no secret key for {_}"
|
|
|
|
raise ValueError(e_msg)
|
|
|
|
assert bytes(secret_key.get_g1()) == bytes(_)
|
|
|
|
signature = AugSchemeMPL.sign(secret_key, msg)
|
2020-11-03 17:35:54 +03:00
|
|
|
assert AugSchemeMPL.verify(_, msg, signature)
|
2020-10-02 23:47:05 +03:00
|
|
|
signatures.append(signature)
|
|
|
|
|
|
|
|
# Aggregate signatures
|
|
|
|
aggsig = AugSchemeMPL.aggregate(signatures)
|
2020-11-03 17:35:54 +03:00
|
|
|
assert AugSchemeMPL.aggregate_verify(pk_list, msg_list, aggsig)
|
2020-10-02 23:47:05 +03:00
|
|
|
return SpendBundle(coin_solutions, aggsig)
|