def get_new_address(cls, secexp, n): # Electrum has two branches of keys - standard and change addresses # n[0] represent index in branch # n[1] == 0 is for standard addresses, n[1] == 1 for change addresses if len(n) != 2: raise Exception("n must have exactly two values") """Publickey(type,n) = Master_public_key + H(n|S|type)*point """ master_public_key = cls.init_master_public_key(secexp) z = cls._get_sequence(master_public_key, n) master_public_key = ecdsa.VerifyingKey.from_string(master_public_key, curve=SECP256k1 ) pubkey_point = master_public_key.pubkey.point + z*SECP256k1.generator public_key2 = ecdsa.VerifyingKey.from_public_point( pubkey_point, curve=SECP256k1 ) address = public_key_to_bc_address('04'.decode('hex') + public_key2.to_string() ) return address
def get_new_address(cls, secexp, n): # Electrum has two branches of keys - standard and change addresses # n[0] represent index in branch # n[1] == 0 is for standard addresses, n[1] == 1 for change addresses if len(n) != 2: raise Exception("n must have exactly two values") """Publickey(type,n) = Master_public_key + H(n|S|type)*point """ master_public_key = cls.init_master_public_key(secexp) z = cls._get_sequence(master_public_key, n) master_public_key = ecdsa.VerifyingKey.from_string(master_public_key, curve=SECP256k1) pubkey_point = master_public_key.pubkey.point + z * SECP256k1.generator public_key2 = ecdsa.VerifyingKey.from_public_point(pubkey_point, curve=SECP256k1) address = public_key_to_bc_address('04'.decode('hex') + public_key2.to_string()) return address
def verify_message(address, signature, message): """ See http://www.secg.org/download/aid-780/sec1-v2.pdf for the math """ curve = ecdsa.curves.SECP256k1.curve # curve_secp256k1 G = ecdsa.curves.SECP256k1.generator order = G.order() # extract r,s from signature if len(signature) != 65: raise BaseException("Wrong signature") r, s = util.sigdecode_string(signature[1:], order) nV = ord(signature[0]) if nV < 27 or nV >= 35: raise BaseException("Bad encoding") if nV >= 31: compressed = True nV -= 4 else: compressed = False recid = nV - 27 # 1.1 x = r + (recid / 2) * order # 1.3 alpha = (x * x * x + curve.a() * x + curve.b()) % curve.p() beta = ecdsa.numbertheory.square_root_mod_prime(alpha, curve.p()) y = beta if (beta - recid) % 2 == 0 else curve.p() - beta # 1.4 the constructor checks that nR is at infinity R = ellipticcurve.Point(curve, x, y, order) # 1.5 compute e from message: h = sha256(sha256(message_magic(message)).digest()).digest() e = util.string_to_number(h) minus_e = -e % order # 1.6 compute Q = r^-1 (sR - eG) inv_r = numbertheory.inverse_mod(r, order) Q = inv_r * (s * R + minus_e * G) public_key = ecdsa.VerifyingKey.from_public_point(Q, curve=ecdsa.curves.SECP256k1) # check that Q is the public key public_key.verify_digest(signature[1:], h, sigdecode=ecdsa.util.sigdecode_string) if address: address_type = int(binascii.hexlify(tools.b58decode(address, None)[0]), 16) addr = tools.public_key_to_bc_address('\x04' + public_key.to_string(), address_type, compress=compressed) if address != addr: raise Exception("Invalid signature")
def get_address(public_node, address_type): return tools.public_key_to_bc_address(public_node.public_key, address_type)
def get_address(self, n, address_type): secexp = string_to_number(self.get_private_key(n)) pubkey = SigningKey.from_secret_exponent(secexp, SECP256k1).get_verifying_key() address = public_key_to_bc_address('\x04' + pubkey.to_string(), address_type) return address
def get_address(self, coin, n): pubkey = self.get_public_node(n).public_key address = public_key_to_bc_address(pubkey, coin.address_type) return address