def sign_message(self, sequence, message, password): sig = None try: message = message.encode('utf8') inputPath = self.get_derivation_prefix() + "/%d/%d" % sequence msg_hash = sha256d(msg_magic(message)) inputHash = to_hexstr(msg_hash) hasharray = [] hasharray.append({'hash': inputHash, 'keypath': inputPath}) hasharray = json.dumps(hasharray) msg = ('{"sign":{"meta":"sign message", "data":%s}}' % hasharray).encode('utf8') dbb_client = self.plugin.get_client(self) if not dbb_client.is_paired(): raise Exception(_("Could not sign message.")) reply = dbb_client.hid_send_encrypt(msg) self.handler.show_message(_("Signing message ...") + "\n\n" + _("To continue, touch the Digital Bitbox's blinking light for 3 seconds.") + "\n\n" + _("To cancel, briefly touch the blinking light or wait for the timeout.")) reply = dbb_client.hid_send_encrypt(msg) # Send twice, first returns an echo for smart verification (not implemented) self.handler.finished() if 'error' in reply: raise Exception(reply['error']['message']) if 'sign' not in reply: raise Exception(_("Could not sign message.")) if 'recid' in reply['sign'][0]: # firmware > v2.1.1 sig_string = binascii.unhexlify(reply['sign'][0]['sig']) recid = int(reply['sign'][0]['recid'], 16) sig = ecc.construct_sig65(sig_string, recid, True) pubkey, compressed = ecc.ECPubkey.from_signature65(sig, msg_hash) addr = public_key_to_p2pkh(pubkey.get_public_key_bytes(compressed=compressed)) if ecc.verify_message_with_address(addr, sig, message) is False: raise Exception(_("Could not sign message")) elif 'pubkey' in reply['sign'][0]: # firmware <= v2.1.1 for recid in range(4): sig_string = binascii.unhexlify(reply['sign'][0]['sig']) sig = ecc.construct_sig65(sig_string, recid, True) try: addr = public_key_to_p2pkh(binascii.unhexlify(reply['sign'][0]['pubkey'])) if ecc.verify_message_with_address(addr, sig, message): break except Exception: continue else: raise Exception(_("Could not sign message")) except BaseException as e: self.give_error(e) return sig
def _do_test_crypto(self, message): G = ecc.generator() _r = G.order() pvk = ecdsa.util.randrange(_r) Pub = pvk * G pubkey_c = Pub.get_public_key_bytes(True) #pubkey_u = point_to_ser(Pub,False) addr_c = public_key_to_p2pkh(pubkey_c) #print "Private key ", '%064x'%pvk eck = ecc.ECPrivkey(number_to_string(pvk, _r)) #print "Compressed public key ", pubkey_c.encode('hex') enc = ecc.ECPubkey(pubkey_c).encrypt_message(message) dec = eck.decrypt_message(enc) self.assertEqual(message, dec) #print "Uncompressed public key", pubkey_u.encode('hex') #enc2 = EC_KEY.encrypt_message(message, pubkey_u) dec2 = eck.decrypt_message(enc) self.assertEqual(message, dec2) signature = eck.sign_message(message, True) #print signature eck.verify_message_for_address(signature, message)
def data(self, index, role = Qt.DisplayRole): data = None if not index.isValid(): return QVariant(data) if role not in [Qt.DisplayRole, Qt.EditRole, Qt.ToolTipRole, Qt.FontRole, Qt.BackgroundRole]: return None mn = self.masternodes[index.row()] i = index.column() if i == self.ALIAS: data = mn.alias elif i == self.STATUS: status = self.manager.masternode_statuses.get(mn.get_collateral_hash_str()) data = masternode_status(status) if role == Qt.BackgroundRole: data = QBrush(QColor(ENABLED_MASTERNODE_BG)) if data[0] else None # Return the long description for data widget mappers. elif role == Qt.EditRole: data = data[2] else: data = data[1] elif i == self.VIN_PREVOUT_HASH: data = mn.vin.get('prevout_hash', '') elif i == self.VIN_PREVOUT_N: data = str(mn.vin.get('prevout_n', '')) elif i == self.VIN_ADDRESS: data = mn.vin.get('address', '') elif i == self.VIN_VALUE: data = str(mn.vin.get('value', '')) elif i == self.VIN_SCRIPTSIG: data = mn.vin.get('scriptSig', '') elif i == self.COLLATERAL: data = mn.collateral_key if role in [Qt.EditRole, Qt.DisplayRole, Qt.ToolTipRole] and data: data = bitcoin.public_key_to_p2pkh(bfh(data)) elif role == Qt.FontRole: data = util.MONOSPACE_FONT elif i == self.DELEGATE: data = mn.masternode_pubkey if role in [Qt.EditRole, Qt.DisplayRole, Qt.ToolTipRole] and data: data = self.manager.get_delegate_privkey(data) elif role == Qt.FontRole: data = util.MONOSPACE_FONT elif i == self.ADDR: data = '' if mn.addr.ip: data = str(mn.addr) elif i == self.PROTOCOL_VERSION: data = mn.protocol_version return QVariant(data)
def sign_message(self, sequence, message, password): sig = None try: message = message.encode('utf8') inputPath = self.get_derivation() + "/%d/%d" % sequence msg_hash = Hash(msg_magic(message)) inputHash = to_hexstr(msg_hash) hasharray = [] hasharray.append({'hash': inputHash, 'keypath': inputPath}) hasharray = json.dumps(hasharray) msg = b'{"sign":{"meta":"sign message", "data":%s}}' % hasharray.encode( 'utf8') dbb_client = self.plugin.get_client(self) if not dbb_client.is_paired(): raise Exception("Could not sign message.") reply = dbb_client.hid_send_encrypt(msg) self.handler.show_message(_("Signing message ...\r\n\r\n" \ "To continue, touch the Digital Bitbox's blinking light for 3 seconds.\r\n\r\n" \ "To cancel, briefly touch the blinking light or wait for the timeout.")) reply = dbb_client.hid_send_encrypt( msg ) # Send twice, first returns an echo for smart verification (not implemented) self.handler.finished() if 'error' in reply: raise Exception(reply['error']['message']) if 'sign' not in reply: raise Exception("Could not sign message.") if 'recid' in reply['sign'][0]: # firmware > v2.1.1 sig = bytes([27 + int(reply['sign'][0]['recid'], 16) + 4 ]) + binascii.unhexlify(reply['sign'][0]['sig']) pk, compressed = pubkey_from_signature(sig, msg_hash) pk = point_to_ser(pk.pubkey.point, compressed) addr = public_key_to_p2pkh(pk) if verify_message(addr, sig, message) is False: raise Exception("Could not sign message") elif 'pubkey' in reply['sign'][0]: # firmware <= v2.1.1 for i in range(4): sig = bytes([27 + i + 4]) + binascii.unhexlify( reply['sign'][0]['sig']) try: addr = public_key_to_p2pkh( binascii.unhexlify(reply['sign'][0]['pubkey'])) if verify_message(addr, sig, message): break except Exception: continue else: raise Exception("Could not sign message") except BaseException as e: self.give_error(e) return sig