def sign_transaction_input(tx, input_index, txout_script, intput_script_type, secret, compressed_pubkey=True): # Set all txin to empty txin_scripts_save = [txin.script for txin in tx.in_list] for txin in tx.in_list: txin.script = Script([]) # Set the current input script to the outpoint's script value tx.in_list[input_index].script = txout_script # Serialize and append hash type enctx = TxSerializer().serialize(tx) + b"\x01\x00\x00\x00" # Get hash and Sign txhash = doublesha256(enctx) key = KEY() key.set_secret(secret, compressed_pubkey) signature = key.sign(txhash) + "\x01" # append hash_type SIGHASH_ALL # Restore Txin scripts for txin, script_save in zip(tx.in_list, txin_scripts_save): txin.script = script_save # Set the signed script if intput_script_type == TX_PUBKEYHASH: tx.in_list[input_index].script = make_script_pubkeyhash_sig( key.get_pubkey(), signature) if intput_script_type == TX_PUBKEY: tx.in_list[input_index].script = make_script_pubkey_sig(signature)
def test_ssl_sign_verify(self): key = KEY() key.set_secret(decodehexstr("30d1d8d1d243ab41a80a3cc1481a626a137f771a636b2daca06c1f86cdfecffb")) sig = key.sign("cool") # verify on another key key2 = KEY() key2.set_pubkey(decodehexstr("030a43196c8bf389c0ce5987a3f4dac57f4ca0d9733c232659717d9404074b4504")) self.assertEquals(key2.verify("cool", sig), 1) self.assertEquals(key2.verify("coolx", sig), 0) self.assertEquals(key2.verify("cool", decodehexstr("3045022100ea3cbfca49123ecdcc419cf3277597307dca70b548ca1d4312f39186b043e86802201057af5c3889b65a59333d4f23bea915e76c2c26606dd35c57e00adf416ca31600")), 0)
def get_private_key_secret(self, public_key): if public_key in self.wallet_database.keys: # private key is not crypted k = KEY() k.set_privkey(self.wallet_database.keys[public_key].private_key) return k.get_secret() crypted_secret = self.wallet_database.get_crypted_keys()[public_key] for key in self.plain_masterkeys: self.crypter.set_key(key, doublesha256(public_key)) secret = self.crypter.decrypt(crypted_secret) k = KEY() is_compressed = len(public_key) == 33 k.set_secret(secret, is_compressed) if k.get_pubkey() == public_key: return secret raise KeyDecryptException("Can't decrypt private key, wallet not unlocked or incorrect masterkey")
def get_private_key_secret(self, public_key): if public_key in self.wallet_database.keys: # private key is not crypted k = KEY() k.set_privkey(self.wallet_database.keys[public_key].private_key) return k.get_secret() crypted_secret = self.wallet_database.get_crypted_keys()[public_key] for key in self.plain_masterkeys: self.crypter.set_key(key, doublesha256(public_key)) secret = self.crypter.decrypt(crypted_secret) k = KEY() is_compressed = len(public_key) == 33 k.set_secret(secret, is_compressed) if k.get_pubkey() == public_key: return secret raise KeyDecryptException( "Can't decrypt private key, wallet not unlocked or incorrect masterkey" )
def sign_transaction_input(tx, input_index, txout_script, intput_script_type, secret, compressed_pubkey=True): # Set all txin to empty txin_scripts_save = [txin.script for txin in tx.in_list] for txin in tx.in_list: txin.script = Script([]) # Set the current input script to the outpoint's script value tx.in_list[input_index].script = txout_script # Serialize and append hash type enctx = TxSerializer().serialize(tx) + b"\x01\x00\x00\x00" # Get hash and Sign txhash = doublesha256(enctx) key = KEY() key.set_secret(secret, compressed_pubkey) signature = key.sign(txhash) + "\x01" # append hash_type SIGHASH_ALL # Restore Txin scripts for txin, script_save in zip(tx.in_list, txin_scripts_save): txin.script = script_save # Set the signed script if intput_script_type == TX_PUBKEYHASH: tx.in_list[input_index].script = make_script_pubkeyhash_sig(key.get_pubkey(), signature) if intput_script_type == TX_PUBKEY: tx.in_list[input_index].script = make_script_pubkey_sig(signature)
def test_ssl_get_pubkey(self): key = KEY() key.set_secret(decodehexstr("30d1d8d1d243ab41a80a3cc1481a626a137f771a636b2daca06c1f86cdfecffb"), True) self.assertEquals(hexstr(key.get_pubkey()), "030a43196c8bf389c0ce5987a3f4dac57f4ca0d9733c232659717d9404074b4504")
def test_ssl_set_secret(self): key = KEY() key.set_secret(decodehexstr("30d1d8d1d243ab41a80a3cc1481a626a137f771a636b2daca06c1f86cdfecffb"), True) self.assertEquals(hexstr(key.get_privkey()), "3081d3020101042030d1d8d1d243ab41a80a3cc1481a626a137f771a636b2daca06c1f86cdfecffba08185308182020101302c06072a8648ce3d0101022100fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f300604010004010704210279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798022100fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141020101a124032200030a43196c8bf389c0ce5987a3f4dac57f4ca0d9733c232659717d9404074b4504")