def sign_tx(self, pw): if not self.is_wallet_loaded: raise Exception('Tried to spend when wallet not loaded.') if not self.is_dest_addr_set: raise Exception('Tried to spend when destination address not set.') if not self.is_send_amount_set: raise Exception('Tried to spend when amount not set.') if self.send_amount + self.txfee > self.balance: raise LowBalanceError("Insufficient funds to send {0} + {1} BTC.".format(core.satoshi_to_btc(self.send_amount), core.satoshi_to_btc(self.txfee))) try: prv = wallet.decrypt_privkey(self.encr_privkey, pw) addr = bc.privtoaddr(prv, self.magic_byte) except: raise PasswordError("Wrong password!") if addr != self.addr: raise Exception('Address from wallet does not match address from private key!') tx_ins, tx_outs = core.simple_tx_inputs_outputs(self.addr, self.unspent, self.dest_addr, self.send_amount, self.txfee) # Make transaction tx = bc.mktx(tx_ins, tx_outs) # Sign transaction for i in range(len(tx_ins)): tx = bc.sign(tx,i,prv) return tx_ins, tx_outs, tx, bc.deserialize(tx)
def create_wallet(self, method, input, aes_pw): privkey = '' if method == 'wif': fmt = bc.get_privkey_format(input) if fmt == 'wif': privkey = bc.encode_privkey(input, 'hex') elif fmt == 'wif_compressed': privkey = bc.encode_privkey(input, 'hex_compressed') else: raise Exception('Unrecoginized format for private key.') elif method == 'random': privkey = bc.random_key() else: raise Exception('Unsupported method: {0}.'.format(method)) wal_addr = bc.privtoaddr(privkey, self.magic_byte) encr_privkey = wallet.encrypt_privkey(privkey, aes_pw) wallet.create_wallet_file(self.wallet_filename, encr_privkey, wal_addr) # Read back from wallet to ensure consistency encr_privkey2, wal_addr2 = wallet.read_from_wallet_file(self.wallet_filename) privkey2 = wallet.decrypt_privkey(encr_privkey2, aes_pw) if encr_privkey2 != encr_privkey or wal_addr2 != wal_addr: raise Exception('Inconsistency in reading from/writing to wallet!') if privkey2 != privkey: raise Exception('Inconsistency in encrypting/decrypting private key!') return
def get_wif_privkey(self, pw): if not self.is_wallet_loaded: raise Exception('Tried getting private key when wallet not loaded.') prv = '' try: prv = wallet.decrypt_privkey(self.encr_privkey, pw) except: raise PasswordError("Wrong password!") frm = bc.get_privkey_format(prv) if frm == 'hex': wif_privkey = bc.encode_privkey(prv, 'wif', self.magic_byte) elif frm == 'hex_compressed': wif_privkey = bc.encode_privkey(prv, 'wif_compressed', self.magic_byte) else: raise Exception('Unrecognized private key format: ' + frm) return wif_privkey