def split_rootkey(self, rootkey): # split the root key into a master secret key and a master chain code length = len(rootkey) // 2 master_key, chain_code = rootkey[:length], rootkey[length:] if (int(master_key, 16) <= 0) or (int(master_key, 16) >= secp256k1().n): raise ValueError("Master key is not valid!") return unhexlify(master_key), unhexlify(chain_code)
def derive_child_prvkey(self, xprv, xpub, depth, index): # extract private, public, and chain code prv, chain = self.extract_prv(xprv) pub = self.extract_pub(xpub) # generate the private child key child = self.ckd_prv(prv, chain, index) # split the private key and chain code child_prv, child_chain = self.split_childkey(child) # generate the fingerprint for the key fingerprint = self.generate_fingerprint(pub) # generate child private key child_prv = (int(child_prv, 16) + int(prv.hex(), 16)) % secp256k1().n #FIXME: check if derived key is valid child_yprv = self.prv_version + depth + fingerprint + index + unhexlify( child_chain) + b'\x00' + child_prv.to_bytes(32, self.endianness) return b58encode_check(child_yprv).decode()
import unittest from sys import path path.append('../') from biptools.secp256k1 import secp256k1, CurvePoint s = secp256k1() g1 = CurvePoint( 0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798, 0x483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8) g2 = CurvePoint( 0xC6047F9441ED7D6D3045406E95C07CD85C778E4B8CEF3CA7ABAC09B95C709EE5, 0x1AE168FEA63DC339A3C58419466CEAEEF7F632653266D0E1236431A950CFE52A) g3 = CurvePoint( 0xF9308A019258C31049344F85F89D5229B531C845836F99B08601F113BCE036F9, 0x388F7B0F632DE8140FE337E62A37F3566500A99934C2231B6CB9FD7584B8E672) g4 = CurvePoint( 0xE493DBF1C10D80F3581E4904930B1404CC6C13900EE0758474FA94ABE8C4CD13, 0x51ED993EA0D455B75642E2098EA51448D967AE33BFBDFE40CFE97BDC47739922) g5 = CurvePoint( 0x2F8BDE4D1A07209355B4A7250A5C5128E88B84BDDC619AB7CBA8D569B240EFE4, 0xD8AC222636E5E3D6D4DBA9DDA6C9C426F788271BAB0D6840DCA87D3AA6AC62D6) g6 = CurvePoint( 0xFFF97BD5755EEEA420453A14355235D382F6472F8568A18B2F057A1460297556, 0xAE12777AACFBB620F3BE96017F45C560DE80F0F6518FE4A03C870C36B075F297)
def point(self, prv_key): # compute the public key: K = k*G private = int.from_bytes(prv_key, self.endianness) return secp256k1().generate_pubkey(private)