Example #1
0
def create_transaction_handler():
    parser = ArgumentParser(description="Creates transaction object")
    parser.add_argument("-nonce", type=int, required=True)
    parser.add_argument("-out",
                        type=str,
                        help="{\\\"address\\\":amount,...}",
                        required=True)
    parser.add_argument("-private_key", type=str, required=True)
    args = parser.parse_args(sys.argv[2:])
    transaction = Transaction(args.nonce, json.loads(args.out))
    transaction.sign(args.private_key)
    print(transaction.serialize().hex())
Example #2
0
 def setUp(self):
     self.blockchain = Blockchain()
     address = "c70f4891d2ce22b1f62492605c1d5c2fc1a8ef47"
     timestamp = 1579861388
     self.genesis = Block(0, "0" * 64, address, 0, timestamp, 0)
     self.transaction = Transaction(
         0, {"0000000000000000000000000000000000000000": 5})
     self.transaction.sign(
         "685cf62751cef607271ed7190b6a707405c5b07ec0830156e748c0c2ea4a2cfe")
     self.block = Block(1, self.genesis.hash(), address, 0, timestamp + 60,
                        0, [self.transaction])
     self.blockchain.add_block(self.genesis)
     self.blockchain.add_block(self.block)
Example #3
0
class TestBlockchain(TestCase):
    def setUp(self):
        self.blockchain = Blockchain()
        address = "c70f4891d2ce22b1f62492605c1d5c2fc1a8ef47"
        timestamp = 1579861388
        self.genesis = Block(0, "0" * 64, address, 0, timestamp, 0)
        self.transaction = Transaction(
            0, {"0000000000000000000000000000000000000000": 5})
        self.transaction.sign(
            "685cf62751cef607271ed7190b6a707405c5b07ec0830156e748c0c2ea4a2cfe")
        self.block = Block(1, self.genesis.hash(), address, 0, timestamp + 60,
                           0, [self.transaction])
        self.blockchain.add_block(self.genesis)
        self.blockchain.add_block(self.block)

    def test_get_latest_block(self):
        self.assertEqual(self.block.hash(),
                         self.blockchain.get_latest_block().hash())

    def test_get_genesis_block(self):
        self.assertEqual(self.genesis.hash(),
                         self.blockchain.get_genesis_block().hash())

    def test_get_block(self):
        self.assertEqual(self.block.hash(),
                         self.blockchain.get_block(self.block.hash()).hash())

    def test_get_block_hash(self):
        self.assertEqual(self.block.hash(), self.blockchain.get_block_hash(1))

    def test_get_transaction(self):
        self.assertEqual(
            self.transaction.id(),
            self.blockchain.get_block(self.block.hash()).transactions[0].id())

    def test_get_balance(self):
        self.assertEqual(
            5,
            self.blockchain.get_balance(
                "0000000000000000000000000000000000000000"))

    def test_get_nonce(self):
        self.assertEqual(
            1,
            self.blockchain.get_nonce(
                "c70f4891d2ce22b1f62492605c1d5c2fc1a8ef47"))
Example #4
0
class TestTransaction(TestCase):
    def setUp(self):
        private_key = "685cf62751cef607271ed7190b6a707405c5b07ec0830156e748c0c2ea4a2cfe"
        out = {"38fb65b08416b9870067b6cba63fa50a81bc78c8": 100}
        self.transaction = Transaction(1, out)
        self.transaction.sign(private_key)

    def test_id(self):
        self.assertEqual(
            self.transaction.id(),
            "d1ed0b9ab80eb6dcacb8d54cc164700e34a1950fbe0589a181b158568f7c4041")

    def test_address(self):
        self.assertEqual(self.transaction.address(),
                         "c70f4891d2ce22b1f62492605c1d5c2fc1a8ef47")

    def test_value(self):
        self.assertEqual(self.transaction.value(), 100)

    def test_serialize(self):
        self.assertEqual(
            self.transaction.serialize().hex(),
            "01000138fb65b08416b9870067b6cba63fa50a81bc78c8640000000000000034c4ac66523f355dba984e99baff0d991096bcf52b64909201a604b78fb48433106b598de5a8a69a79655414338dc43f8f197ed0d607e29f12d6f67b6fb852a301"
        )

    def test_deserialize(self):
        serialized = self.transaction.serialize()
        temp_transaction = Transaction.deserialize(serialized)
        self.assertEqual(serialized, temp_transaction.serialize())
Example #5
0
def create_block_handler():
    parser = ArgumentParser(description="Creates block object")
    parser.add_argument("-number", type=int, required=True)
    parser.add_argument("-parent", type=str, required=True)
    parser.add_argument("-beneficiary", type=str, required=True)
    parser.add_argument("-target", type=int, required=True)
    parser.add_argument("-timestamp", type=int, required=False)
    parser.add_argument("-nonce", type=int, required=True)
    parser.add_argument('transactions', type=str, nargs='*')
    args = parser.parse_args(sys.argv[2:])
    block_timestamp = args.timestamp
    if block_timestamp is None:
        block_timestamp = timestamp()
    block = Block(args.number, args.parent, args.beneficiary, args.target,
                  block_timestamp, args.nonce)
    for serialized in args.transactions:
        block.add_transaction(
            Transaction.deserialize(bytes.fromhex(serialized)))
    print(block.serialize().hex())
Example #6
0
 def setUp(self):
     private_key = "685cf62751cef607271ed7190b6a707405c5b07ec0830156e748c0c2ea4a2cfe"
     out = {"38fb65b08416b9870067b6cba63fa50a81bc78c8": 100}
     self.transaction = Transaction(1, out)
     self.transaction.sign(private_key)
Example #7
0
 def test_deserialize(self):
     serialized = self.transaction.serialize()
     temp_transaction = Transaction.deserialize(serialized)
     self.assertEqual(serialized, temp_transaction.serialize())
Example #8
0
def decode_transaction_handler():
    parser = ArgumentParser(description="Decodes transaction object")
    parser.add_argument("data", type=str)
    args = parser.parse_args(sys.argv[2:])
    transaction = Transaction.deserialize(bytes.fromhex(args.data))
    print(json.dumps(transaction.to_dict(), indent=4))