-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsk_derivation.py
70 lines (59 loc) · 2.23 KB
/
sk_derivation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
from blspy import AugSchemeMPL, PrivateKey, G1Element
from chia.util.bech32m import encode_puzzle_hash, decode_puzzle_hash
from chia.types.blockchain_format.program import Program
from chia.consensus.coinbase import create_puzzlehash_for_pk
from chia.wallet.puzzles.p2_delegated_puzzle_or_hidden_puzzle import puzzle_hash_for_pk
from chia.cmds.keys_funcs import resolve_derivation_master_key
from typing import Optional, Union
from pathlib import Path
import os
base_path = [12381, 8444, 2]
def get_sk(fp: Optional[Union[int,str]] = None) -> PrivateKey:
"""
Returns a private key from a file or a fingerprint.
"""
master_sk = resolve_derivation_master_key(fp)
return master_sk
def get_wallet_pk_from_sk(sk: PrivateKey, path: list[int]) -> PrivateKey:
"""
Returns a public key from a private key.
"""
for i in base_path:
int_sk = AugSchemeMPL.derive_child_sk_unhardened(sk, i)
return int_sk
def derive_public_key(sk_int: PrivateKey, path: list[int]) -> G1Element:
"""
Derives a public key from a list of paths.
"""
pubkey = sk_int
for index in path:
pubkey: G1Element = AugSchemeMPL.derive_child_sk_unhardened(pubkey, index)
return pubkey.get_g1()
def get_observer_address(sk: PrivateKey, path: list[int]) -> str:
"""
Returns a Chia address from a private key.
"""
full_path = base_path + path
for index in full_path:
sk = AugSchemeMPL.derive_child_sk_unhardened(sk, index)
puzzle_hash = puzzle_hash_for_pk(sk.get_g1())
address = encode_puzzle_hash(puzzle_hash, "xch")
return address
def get_non_observer_address(sk: PrivateKey, path: list[int]) -> str:
"""
Returns a Chia address from a private key.
"""
full_path = base_path + path
for index in full_path:
sk = AugSchemeMPL.derive_child_sk(sk, index)
puzzle_hash = puzzle_hash_for_pk(sk.get_g1())
address = encode_puzzle_hash(puzzle_hash, "xch")
return address
def get_address(sk: PrivateKey, index: int) -> str:
"""
Returns a Chia address from a public key.
"""
puzzle_hash = puzzle_hash_for_pk(derive_public_key(sk, [index]))
#ph = std_tx.curry([Program])
address = encode_puzzle_hash(puzzle_hash, "xch")
return address