Make p2_delegated_puzzle_or_hidden_puzzle more like p2_delegated_puzzle.

This commit is contained in:
Richard Kiss 2020-09-09 12:28:48 -07:00 committed by Gene Hoffman
parent 1fd67f9605
commit 7f77ae95b9

View File

@ -15,50 +15,71 @@ This roughly corresponds to bitcoin's taproot.
"""
import hashlib
from typing import List, Union
from blspy import G1Element
from clvm.casts import int_from_bytes
from src.types.program import Program
from src.types.sized_bytes import bytes32
from .load_clvm import load_clvm
from .p2_conditions import puzzle_for_conditions
DEFAULT_HIDDEN_PUZZLE = Program.from_bytes(bytes.fromhex("ff0980")) # (x)
DEFAULT_HIDDEN_PUZZLE = Program.from_bytes(
bytes.fromhex("ff0980")
) # this puzzle `(x)` always fails
MOD = load_clvm("p2_delegated_puzzle_or_hidden_puzzle.clvm")
SYNTHETIC_MOD = load_clvm("calculate_synthetic_public_key.clvm")
PublicKeyProgram = Union[bytes, Program]
def calculate_synthetic_offset(public_key, hidden_puzzle_hash) -> int:
def calculate_synthetic_offset(
public_key: G1Element, hidden_puzzle_hash: bytes32
) -> int:
blob = hashlib.sha256(bytes(public_key) + hidden_puzzle_hash).digest()
return int_from_bytes(blob)
def calculate_synthetic_public_key(public_key, hidden_puzzle) -> Program:
def calculate_synthetic_public_key(
public_key: PublicKeyProgram, hidden_puzzle: Program
) -> Program:
r = SYNTHETIC_MOD.run([public_key, hidden_puzzle.tree_hash()])
return r
def puzzle_for_synthetic_public_key(synthetic_public_key) -> Program:
def puzzle_for_synthetic_public_key(synthetic_public_key: PublicKeyProgram) -> Program:
return MOD.curry(synthetic_public_key)
def puzzle_for_public_key_and_hidden_puzzle(
public_key, hidden_puzzle=DEFAULT_HIDDEN_PUZZLE
public_key: PublicKeyProgram, hidden_puzzle: Program = DEFAULT_HIDDEN_PUZZLE
) -> Program:
synthetic_public_key = calculate_synthetic_public_key(public_key, hidden_puzzle)
return puzzle_for_synthetic_public_key(synthetic_public_key)
def puzzle_for_pk(public_key: PublicKeyProgram) -> Program:
return MOD.curry(public_key)
def solution_with_delegated_puzzle(
synthetic_public_key, delegated_puzzle, solution
synthetic_public_key: PublicKeyProgram, delegated_puzzle: Program, solution: Program
) -> Program:
puzzle = puzzle_for_synthetic_public_key(synthetic_public_key)
return Program.to([puzzle, [[], delegated_puzzle, solution]])
def solution_with_hidden_puzzle(
hidden_public_key, hidden_puzzle, solution_to_hidden_puzzle
hidden_public_key: PublicKeyProgram,
hidden_puzzle: Program,
solution_to_hidden_puzzle: Program,
) -> Program:
synthetic_public_key = calculate_synthetic_public_key(
hidden_public_key, hidden_puzzle
@ -67,3 +88,9 @@ def solution_with_hidden_puzzle(
return Program.to(
[puzzle, [hidden_public_key, hidden_puzzle, solution_to_hidden_puzzle]]
)
def solution_for_conditions(puzzle_reveal: Program, conditions: Program) -> Program:
delegated_puzzle = puzzle_for_conditions(conditions)
solution: List = []
return Program.to([puzzle_reveal, [delegated_puzzle, solution]])