def __handleDeposit(self, protocol, msgObj): account = self.__getCurrentAccount() bps = [] bpData = msgObj.bpData # debugPrint(bpData[:15], "...", bpData[-15:], len(bpData), type(bpData)) while bpData: newBitPoint, offset = BitPoint.deserialize(bpData) bpData = bpData[offset:] bps.append(newBitPoint) result = self.__bank.depositCash(account, bps) if not result.succeeded(): self.__logSecure("Deposit failed, %s" % result.msg()) response = self.__createResponse(msgObj, RequestFailure) response.RequestId = msgObj.RequestId response.ErrorMessage = result.msg() else: result = self.__bank.generateReceipt(account) if not result.succeeded(): self.__logSecure("Could not generate receipt? %s" % result.msg()) response = self.__createResponse(msgObj, RequestFailure) response.RequestId = msgObj.RequestId response.ErrorMessage = result.msg() else: self.__logSecure("Deposit complete. Sending Signed Receipt") receipt, signature = result.value() response = self.__createResponse(msgObj, Receipt) response.RequestId = msgObj.RequestId response.Receipt = receipt response.ReceiptSignature = signature self.sendPacket(response)
def mintBitPoints(self, count, depositor): newSerialNumbers = self.__getNewSerialNumbers(count) bitPoints = [] for i in range(count): bitPoint = BitPoint.mintNew(issuer=self.ISSUER, serialNumber="%020d" % newSerialNumbers[i], timestamp=time.ctime()) bitPointBin = bitPoint.mainDataBlob() bitPoint.setSignature(self.__signaturePad.sign(bitPointBin)) bitPoints.append(bitPoint) depositor(bitPoints)
def main(BankCoreModule, args): #sys.path.append("../..") from playground.crypto import X509Certificate from getpass import getpass from Crypto.PublicKey import RSA from Exchange import BitPoint if args[0] == "create": cert, key, path = args[1:4] with open(cert) as f: cert = X509Certificate.loadPEM(f.read()) with open(key) as f: key = RSA.importKey(f.read()) passwd = getpass() BankCoreModule.Ledger.InitializeDb(path, cert, key, passwd) elif args[0] == "vault_deposit": cert, path, bpFile = args[1:4] with open(cert) as f: cert = X509Certificate.loadPEM(f.read()) passwd = getpass() bank = BankCoreModule.Ledger(path, cert, passwd) with open(bpFile) as f: bpData = f.read() bps = [] while bpData: newBitPoint, offset = BitPoint.deserialize(bpData) bpData = bpData[offset:] bps.append(newBitPoint) print "depositing", len(bps), "bit points" result = bank.depositCash("VAULT", bps) if not result.succeeded(): print "Deposit failed", result.msg() else: print "Vault balance", bank.getBalance("VAULT") elif args[0] == "balances": cert, path = args[1:3] with open(cert) as f: cert = X509Certificate.loadPEM(f.read()) passwd = getpass() bank = BankCoreModule.Ledger(path, cert, passwd) for account in bank.getAccounts(): print "%s balance" % account, bank.getBalance(account) elif args[0] == "create_account": accountName, cert, path = args[1:4] with open(cert) as f: cert = X509Certificate.loadPEM(f.read()) passwd = getpass() bank = BankCoreModule.Ledger(path, cert, passwd) bank.createAccount(accountName) elif args[0] == "transfer": fromAccount, toAccount, amount, cert, path = args[1:6] with open(cert) as f: cert = X509Certificate.loadPEM(f.read()) passwd = getpass() bank = BankCoreModule.Ledger(path, cert, passwd) amount = int(amount) result = bank.transfer(fromAccount, toAccount, amount) if not result.succeeded(): print "Failed: ", result.msg() for account in [fromAccount, toAccount]: print "%s balance" % account, bank.getBalance(account) elif args[0] == "correct": cert, path = args[1:3] with open(cert) as f: cert = X509Certificate.loadPEM(f.read()) passwd = getpass() bank = BankCoreModule.Ledger(path, cert, passwd) for account in bank.getAccounts(): if account == "VAULT": continue bank.transfer("VAULT", account, 100) elif args[0] == "display_receipt": receiptFile, sigFile, cert = args[1:4] with open(receiptFile) as f: receiptData = f.read() with open(sigFile) as f: sigData = f.read() with open(cert) as f: certData = f.read() cert = X509Certificate.loadPEM(certData) pubKey = RSA.importKey(cert.getPublicKeyBlob()) verifier = PKCS1_v1_5.new(pubKey) receipt = pickle.loads(receiptData) print "Receipt:", receipt.toHumanReadableString() if verifier.verify(SHA.new(receiptData), sigData): print "Receipt is signed by the bank." else: print "Receipt is forged"
def deserialize(cls, s): return BitPoint.deserialize(s)[0]
def main(args): if args[0] == "create": cert, key, filename = args[1:4] with open(cert) as f: cert = X509Certificate.loadPEM(f.read()) with open(key) as f: key = RSA.importKey(f.read()) passwd = getpass.getpass("Create mint password: "******"Re-enter mint password: "******"Passwords do not match") PrintingPress.CreateBankVault(filename, cert, key, passwd) elif args[0] == "mint": if len(args) == 1 or args[1].lower() in ["--help", "-h", "help"]: sys.exit("mint <amount> <cert> <filename> [<output_dir>]\n" + " amount can be of the form <amount>:<denomination>") amount, cert, filename = args[1:4] if len(args) > 4: outputDir = args[4] else: outputDir = None with open(cert) as f: cert = X509Certificate.loadPEM(f.read()) if ":" in amount: amount, denominations = amount.split(":") else: denominations = amount amount = int(amount) denominations = int(denominations) passwd = getpass.getpass("Mint password: "******"Minting %d of %d bitpoints" % ( (total + denominations), amount) mint.mintBitPoints(denominations, serializer) total += denominations elif args[0] == "info": filename = args[1] if len(args) > 2: sampleSize = args[2] else: sampleSize = None bitpoints = [] with open(filename, "rb") as f: bitpoints = BitPoint.deserializeAll(f) print "Deserialized", len(bitpoints), "bitpoints" if sampleSize == None: sample = [] elif sampleSize.lower() == "all": sample = bitpoints else: start, stop = sampleSize.split(":") start = int(start.strip()) stop = int(stop.strip()) sample = bitpoints[start:stop] for bp in sample: print bp elif args[0] == "validate": filename, issuingCert = args[1:3] bitpoints = [] with open(filename, "rb") as f: bitpoints = BitPoint.deserializeAll(f) with open(issuingCert) as f: cert = X509Certificate.loadPEM(f.read()) verifier = BitPointVerifier(cert) for bp in bitpoints: isValid, reason = verifier.verify(bp) if isValid: print bp.serialNumber(), "is valid" else: print bp.serialNumber(), "is NOT valid:", reason
def full_test(args): # args = ["full_test", # "./certs/signed_certs/5001cert", # "./certs/signed_certs/5001pk", # "bitpoints.10.Thu_Oct_19_19_17_39_2017", # "./certs/signed_certs/6001cert" # ] from getpass import getpass certPath, keyFile, bpsFile, fakeCertPath = args[1:5] cert = loadCertFromFile(certPath) key = loadPrivateKeyFromPemFile(keyFile) fakeCert = loadCertFromFile(fakeCertPath) with open(bpsFile, "rb") as f: bps = BitPoint.deserializeAll(f) passwd = getpass() fakePass = "******" accountPEM = "Fady'sPEM" path = os.path.dirname("./test_bankcore_db/") # Overwrite directory if it already exists if os.path.exists(path): print("Previous test db found. Deleting and recreating folder") from shutil import rmtree rmtree(path) os.makedirs(path) # create a db with a wrong key/cert combination try: Ledger.InitializeDb(path, fakeCert, key, passwd) except Exception as e: print("Wrong combination of cert and key caught correctly! (%s)" % e) # Create a db Ledger.InitializeDb(path, cert, key, passwd) # Attempt to load bank with wrong password try: Ledger(path, cert, fakePass) except ValueError as e: print("Wrong password for this bank caught correctly! (%s)" % e) # Attempt to load bank with wrong cert try: Ledger(path, fakeCert, passwd) except Exception as e: print("Wrong certificate for this bank caught correctly! (%s)" % e) # Correctly load bank bank = Ledger(path, cert, passwd) # Check initial accounts are there assert (set(bank.getAccounts()) == set(Ledger.INITIAL_ACCOUNTS)) # Add an account (SHOULD BE Public Key PEM BYTES) result = bank.createAccount(accountPEM) assert (isinstance(result, LedgerOperationSuccess)) # Check the new account is there assert (accountPEM in bank.getAccounts()) # Try to create a new account with the same PEM # (returns a LedgerOperationFailure object) result = bank.createAccount(accountPEM) assert (isinstance(result, LedgerOperationFailure)) # Try to transfer money INTO non-existent account result = bank.transfer("VAULT", "Non-existentPEM", 1) assert (isinstance(result, LedgerOperationFailure)) # Try to transfer money FROM non-existent account result = bank.transfer("Non-existentPEM", "VAULT", 2) assert (isinstance(result, LedgerOperationFailure)) # All accounts should still be 0 for account in bank.getAccounts(): assert (bank.getBalance(account) == 0) # Transfer to and from valid accounts result = bank.transfer("VAULT", accountPEM, 3) assert (isinstance(result, LedgerOperationSuccess)) assert (bank.getBalance(accountPEM) == 3) assert (bank.getBalance("VAULT") == -3) # Reload bank from file and check it correctly saved all changes bank = Ledger(path, cert, passwd) assert (set(bank.getAccounts()) == set(Ledger.INITIAL_ACCOUNTS + [accountPEM])) assert (bank.getBalance(accountPEM) == 3) assert (bank.getBalance("VAULT") == -3) # Transfer to and from valid accounts result = bank.transfer("VAULT", accountPEM, 5) assert (isinstance(result, LedgerOperationSuccess)) # Register a mint cert (in this case we'll just use the same one...) result = bank.registerMintCert(certPath) assert (isinstance(result, LedgerOperationSuccess)) # With that cert, we can deposit some BitPoints minted by that cert result = bank.depositCash("VAULT", bps) assert (isinstance(result, LedgerOperationSuccess)) # Withdraw BitPoints result = bank.withdrawCash(accountPEM, 2) assert (isinstance(result, LedgerOperationSuccess)) # Create a receipt result = bank.generateReceipt(accountPEM, 0) assert (isinstance(result, LedgerOperationSuccess)) receiptData, signatureData = result.value()[0], result.value()[1] receipt = pickle.loads(receiptData) print("Receipt:", receipt.toHumanReadableString()) # Verify the receipt verifier = RSA_SIGNATURE_MAC(cert.public_key()) try: verifier.verify(signatureData, SHA(receiptData).digest()) print("Receipt is signed by the bank.") except AssertionError: print("Receipt is forged") # Try to withdraw more BitPoints than the bank has result = bank.withdrawCash("VAULT", 9999999999) try: assert (isinstance(result, LedgerOperationSuccess)) raise (Exception("Over-withdrawal not caught!!!")) except AssertionError as e: print("Over-withdrawal caught correctly! (%s)" % result.msg()) # Test LedgerLineSearch filter = lambda ledgerLine: ledgerLine.partOfTransaction("CIRCULATION") matches = bank.searchLedger(filter) assert (set(matches) == {2, 3}) # Test getLedgerLine ledgerLine = bank.getLedgerLine(1) assert (ledgerLine.getTransactionAmount("VAULT") == -5) assert (ledgerLine.getTransactionAmount(accountPEM) == 5) # Debug output print("\nDebug:\n---------\n") filter = lambda ledgerLine: True allLines = bank.searchLedger(filter) for line in allLines: print("Ledger line ", line, ":\n", bank.getLedgerLine(line).toHumanReadableString()) print() print("Full test completed successfully!") # In case we're in the REPL and want to keep playing return bank
def main(BankCoreModule, args): #sys.path.append("../..") from getpass import getpass from Exchange import BitPoint if args[0] == "create": cert, key, path = args[1:4] cert = loadCertFromFile(cert) key = loadPrivateKeyFromPemFile(key) passwd = getpass() BankCoreModule.Ledger.InitializeDb(path, cert, key, passwd) elif args[0] == "vault_deposit": cert, path, bpFile = args[1:4] cert = loadCertFromFile(cert) passwd = getpass() bank = BankCoreModule.Ledger(path, cert, passwd) with open(bpFile, "rb") as f: bps = BitPoint.deserializeAll(f) print("depositing", len(bps), "bit points") result = bank.depositCash("VAULT", bps) if not result.succeeded(): print("Deposit failed", result.msg()) else: print("Vault balance", bank.getBalance("VAULT")) elif args[0] == "balances": cert, path = args[1:3] cert = loadCertFromFile(cert) passwd = getpass() bank = BankCoreModule.Ledger(path, cert, passwd) for account in bank.getAccounts(): print("%s balance" % account, bank.getBalance(account)) elif args[0] == "register_mint": bankcertpath, path, mintcertpath = args[1:4] passwd = getpass() bankcert = loadCertFromFile(bankcertpath) bank = Ledger(path, bankcert, passwd) result = bank.registerMintCert(mintcertpath) if not isinstance(result, LedgerOperationSuccess): print("Could not load certificate", result.msg()) else: print("Mint cert registration successful?") elif args[0] == "create_account": accountName, cert, path = args[1:4] cert = loadCertFromFile(cert) passwd = getpass() bank = BankCoreModule.Ledger(path, cert, passwd) bank.createAccount(accountName) elif args[0] == "transfer": fromAccount, toAccount, amount, cert, path = args[1:6] cert = loadCertFromFile(cert) passwd = getpass() bank = BankCoreModule.Ledger(path, cert, passwd) amount = int(amount) result = bank.transfer(fromAccount, toAccount, amount) if not result.succeeded(): print("Failed: ", result.msg()) for account in [fromAccount, toAccount]: print("%s balance" % account, bank.getBalance(account)) elif args[0] == "correct": cert, path = args[1:3] cert = loadCertFromFile(cert) passwd = getpass() bank = BankCoreModule.Ledger(path, cert, passwd) for account in bank.getAccounts(): if account == "VAULT": continue bank.transfer("VAULT", account, 100) elif args[0] == "display_receipt": receiptFile, sigFile, cert = args[1:4] with open(receiptFile) as f: receiptData = f.read() with open(sigFile) as f: sigData = f.read() cert = loadCertFromFile(cert) verifier = RSA_SIGNATURE_MAC(cert.public_key()) receipt = pickle.loads(receiptData) print("Receipt:", receipt.toHumanReadableString()) if verifier.verify(sigData, SHA(receiptData).digest()): print("Receipt is signed by the bank.") else: print("Receipt is forged") elif args[0] == "test": full_test(args)