def getOldBroadcastMessage(self): self.sig_time = int(time.time()) serializedData = ipport(self.ip, self.port) serializedData += str(self.sig_time) serializedData += binascii.unhexlify(bitcoin.hash160(bytes.fromhex(self.collateral['pubKey'])))[::-1].hex() serializedData += binascii.unhexlify(bitcoin.hash160(bytes.fromhex(self.mnPubKey)))[::-1].hex() serializedData += str(self.protocol_version) return serializedData
def sign_message(self, collateral_bip32_path: str, hw_sign_message_fun: typing.Callable, hw_session, mn_privkey_wif: str, dash_network: str): self.mn_ping.sign_message(mn_privkey_wif, dash_network) str_for_serialize = self.mn_ip + ':' + str(self.mn_port) + str(self.sig_time) + \ binascii.unhexlify(bitcoin.hash160(self.pubkey_collateral))[::-1].hex() + \ binascii.unhexlify(bitcoin.hash160(self.pubkey_masternode))[::-1].hex() + \ str(self.protocol_version) self.sig = hw_sign_message_fun(hw_session, collateral_bip32_path, str_for_serialize) return self.sig
def p2sh_scriptaddr(self, script, magicbyte=5, magicbyte_length=1): """ 根据reedem-script生成地址 """ if re.match('^[0-9a-fA-F]*$', script): script = bitcoin.binascii.unhexlify(script) return self.hex_to_b58check(bitcoin.hash160(script), magicbyte=magicbyte, magicbyte_length=magicbyte_length)
def pubkey_to_bech32_address(pubkey, prefix=BECH32_BITCOIN_PREFIX): if isinstance(pubkey, (list, tuple)): pubkey = encode_pubkey(pubkey, 'bin') if len(pubkey) in [66, 130]: pubkey = binascii.unhexlify(pubkey) pubkey_hash = hash160(pubkey) return bech32encode('0014' + pubkey_hash, prefix=prefix) raise ValueError()
def signature1(self, device): self.sig_time = int(time.time()) serializedData = ipport(self.ip, self.port) serializedData += str(self.sig_time) serializedData += binascii.unhexlify(bitcoin.hash160(bytes.fromhex(self.collateral['pubKey'])))[::-1].hex() serializedData += binascii.unhexlify(bitcoin.hash160(bytes.fromhex(self.mnPubKey)))[::-1].hex() serializedData += str(self.protocol_version) printDbg("Masternode PubKey: %s" % self.mnPubKey) printDbg("SerializedData: MY_IP:%s" % serializedData.split(':')[1]) try: device.signMess(self.caller, self.nodePath, serializedData) #wait for signal when device.sig1 is ready then --> finalizeStartMessage except Exception as e: err_msg = "error in signature1" printException(getCallerName(), getFunctionName(), err_msg, e.args) except KeyboardInterrupt: err_msg = "Keyboard Interrupt" printException(getCallerName(), getFunctionName(), err_msg, e.args) return None
def bech32_sign(tx, i, priv, amount, script=None, hashcode=SIGHASH_ALL): from bitcoin import deserialize, segwit_signature_form, ecdsa_raw_sign, der_encode_sig, serialize, compress i = int(i) txobj = tx if isinstance(tx, dict) else deserialize(tx) if len(priv) <= 33: priv = binascii.hexlify(priv) pub = compress(privkey_to_pubkey(priv)) script = script or ('76a914' + hash160(binascii.unhexlify(pub)) + '88ac') signing_tx = segwit_signature_form(tx, i, script, amount, hashcode=hashcode) rawsig = ecdsa_raw_sign( hashlib.sha256( hashlib.sha256( binascii.unhexlify(signing_tx)).digest()).hexdigest(), priv) sig = der_encode_sig(*rawsig) + encode(hashcode, 16, 2) txobj['ins'][i]['txinwitness'] = [sig, pub] return serialize(txobj)
def get_message_to_sign(self): str_for_serialize = self.mn_ip + ':' + str(self.mn_port) + str(self.sig_time) + \ binascii.unhexlify(bitcoin.hash160(self.pubkey_collateral))[::-1].hex() + \ binascii.unhexlify(bitcoin.hash160(self.pubkey_masternode))[::-1].hex() + \ str(self.protocol_version) return str_for_serialize
def test_hash160(): assert '0e3397b4abc7a382b3ea2365883c3c7ca5f07600' == \ btc.hash160('The quick brown fox jumps over the lazy dog')
def setup(): myprivate = ecdsa.util.randrange(order) privkeyhex = myprivate.to_bytes(32, 'big').hex() mypubpoint = myprivate * G mypubkey = point_to_ser(mypubpoint) mypubkeyhash = hash160(mypubkey) print("==Setup phase 1==") print("Share your pubkey hash:::\n ", mypubkeyhash.hex()) response = input( "Enter pubkey hashes of other players, separated by spaces:\n") try: otherhashes = [ bytes.fromhex(hhex.strip()) for hhex in response.split() ] except: raise ProtoProblem("Could not convert from hex.") for h in otherhashes: if len(h) != 20: raise ProtoProblem("Incorrect length", h.hex()) if h == mypubkeyhash: raise ProtoProblem("One of the others' hashes is same as yours.") print("Total players: ", len(otherhashes) + 1) print() print("==Setup phase 2==") print("Share your pubkey:::\n ", mypubkey.hex()) while True: response = input( "Enter pubkeys of other players, separated by spaces:\n") try: otherpubkeys = [ bytes.fromhex(phex.strip()) for phex in response.split() ] except: raise ProtoProblem("Could not convert from hex.") if len(otherpubkeys) == len(otherhashes): break print("Incorrect number of pubkeys") # will have all keys added in to aggpoint aggpoint = mypubpoint for pub in otherpubkeys: h = hash160(pub) try: otherhashes.remove(h) except ValueError: raise ProtoProblem("Unannounced pubkey: ", pub.hex()) try: P = ser_to_point(pub) except Exception as e: raise raise ProtoProblem("Bad pubkey: ", pub.hex()) aggpoint = aggpoint + P assert len(otherhashes) == 0 assert aggpoint != INFINITY, "agg pubkey at infinity, should never happen!" aggpub = point_to_ser(aggpoint) aggpubhash = hash160(aggpub) aggaddress = cashaddr.encode_full(prefix, cashaddr.PUBKEY_TYPE, aggpubhash) print() print("==Setup results==") print("Aggregate pubkey: ", aggpub.hex()) print("Aggregate address:", aggaddress) print( "Please confirm the above address with each other, before continuing.") response = input("Add a note to self (optional): ") return dict( privatekey=privkeyhex, otherpubkeys=[pub.hex() for pub in otherpubkeys], # this is stored just to double check. aggaddress=aggaddress, note=response, )