def transfer(bc, relay, from_addr, to_addr, amount): f = get_address(from_addr) if f is None: exit('no address '+str(from_addr)+' in database') total = bc.get_amount_of_address(f) if total < amount: exit('not enough money for this transfer') pwd = getpass.getpass() while not f.decryptPrivateKey(pwd): print('Invalid password') pwd = getpass.getpass() receivers = [(to_addr, amount)] if total > amount: new = Address() new.encryptPrivateKey(pwd) db.add_address('client', new, 0) print('created new address '+str(new)+' to receive remaining funds') receivers.append((str(new), total - amount)) t = Transaction(f, receivers) if relay.submit_transaction(t): print('transaction sent to the network') else: print('error sending transaction')
def createTransaction(self, password, destList): """Create a new transaction and send it to the RelayNode destList is a list of tuples Each tuples is like : (str_address, value) The last transaction is the rest of the wallet send to the new user address """ self.checkUpdate() newAddr = Address() newAddr.encryptPrivateKey(password) total = sum([i[1] for i in destList]) if total <= self.count: destList.append((str(newAddr), (self.count - total))) transac = Transaction(self.addr.public(), destList) self.addr.decryptPrivateKey(password) transac.sign(self.addr) debug('valid: ' + ('True' if transac.is_signed() else 'False')) self.addr.encryptPrivateKey(password) if not self.relay.submit_transaction(transac): return False self.addrList.append(newAddr) self.addr = newAddr add_address(self.user_ID, self.addr, len(self.addrList) - 1) return True else: return False
def create_address(): pwd = getpass.getpass() addr = Address() addr.encryptPrivateKey(pwd) db.add_address('client', addr, 0) print('created new address '+str(addr))
class Wallet(object): """Wallet is the principal user Wallet have money and can create some Transaction to send money to an another Wallet """ def __init__(self, user_ID, password): """Create a new wallet user_ID : The ID of the user to select it's own address on the DB password : The password is used to generate a AES_Key to ecrypt / decrypt the private key on DB Here, we used it to load all the address or write the new address """ self.blockChain = Blockchain('client') self.relay = RelayClient() self.updater = Updater(self.blockChain, self.relay) self.updater.update() self.user_ID = user_ID self.addrList = loadAddressList(self.user_ID) # list of address self.last = len(self.addrList) - 1 #index of the actual address if self.addrList == []: #New Wallet : Create the first Address self.addr = Address() self.addr.encryptPrivateKey(password) add_address(self.user_ID, self.addr, 0) self.addrList.append(self.addr) else: self.addr = self.addrList[len(self.addrList) - 1] self.count = self.blockChain.get_amount_of_address(self.addr) def backAddress(self): """Return to the previous address """ if len(self.addrList) >= 2: self.addr = self.addrList[len(addrList) - 2] self.addrList.pop(self.addrList[len(self.addrList) - 1]) def checkUpdate(self): """Update the amount and the blockChain """ self.updater.update() self.count = self.blockChain.get_amount_of_address(self.addr) def createTransaction(self, password, destList): """Create a new transaction and send it to the RelayNode destList is a list of tuples Each tuples is like : (str_address, value) The last transaction is the rest of the wallet send to the new user address """ self.checkUpdate() newAddr = Address() newAddr.encryptPrivateKey(password) total = sum([i[1] for i in destList]) if total <= self.count: destList.append((str(newAddr), (self.count - total))) transac = Transaction(self.addr.public(), destList) self.addr.decryptPrivateKey(password) transac.sign(self.addr) debug('valid: ' + ('True' if transac.is_signed() else 'False')) self.addr.encryptPrivateKey(password) if not self.relay.submit_transaction(transac): return False self.addrList.append(newAddr) self.addr = newAddr add_address(self.user_ID, self.addr, len(self.addrList) - 1) return True else: return False
def test_encrypted_json(self): a = Address() a.encryptPrivateKey('pwd') b = Address.fromJson(a.toJson()) self.assertTrue(self.equalAddresses(a, b, False, True))
def test_encrypted(self): a = Address() b = Address(a.dsa) b.encryptPrivateKey('pwd') b.decryptPrivateKey('pwd') self.assertTrue(self.equalAddresses(a, b))