Exemple #1
0
    print("NEW TEST:\n")
    blockchain = Blockchain("blockchain")
    print("empty blockchain")
    print(blockchain)
    db = blockchain.db
    previousHash = blockchain.get_last_hash()
    print("previousHash")
    print(previousHash)

    print("TESTING Blocks DB")

    sender_address = Address()
    receiver1_address = Address()
    receiver2_address = Address()
    miner_address = Address()
    transaction = Transaction(sender_address.public(),
                              [(str(receiver1_address), 123),
                               (str(receiver2_address), 321)])
    print("transaction")
    print(str(transaction.toJson()))
    block = Block(previousHash, str(miner_address), [transaction])
    block.set_proof("43334")
    print("block")
    print(str(block.toJson()))
    json_block = block.toJson()
    print("json_block")
    print(str(json_block))

    db.add_json_block(previousHash, json_block)
    print("updated DB")
    print_blocks(db)
Exemple #2
0
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
Exemple #3
0
 def test_public_address(self):
     a = Address()
     b = a.public()
     self.assertEqual(str(a), str(b))
     self.assertNotEqual(a.toJson(), b.toJson())