예제 #1
0
    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)
예제 #2
0
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"
예제 #3
0
 def deserialize(cls, s):
     return BitPoint.deserialize(s)[0]