def create(): # Cryptographic Identities Generation alice, bob = generate_keypair(), generate_keypair() # Digital Asset Definition (e.g. bicycle) asset = Asset(data={ "bicycle": { "manufacturer": "bkfab", "serial_number": "abcd1234" } }) # Metadata Definition metadata = {'planet': 'earth'} # create trnsaction TODO : owners_before might be node_pubkey in v0.8.0 tx = Transaction.create([alice.public_key], [([alice.public_key], 1)], metadata=metadata, asset=asset) # sign with private key tx = tx.sign([alice.private_key]) tx_id = tx.to_dict()['id'] # write to backlog b = Bigchain() b.write_transaction(tx) # wait 2 sec sleep(2) # get tx by id tx = b.get_transaction(tx_id) return tx.to_dict()
def create_duplicate_tx(): ##################################################### 1.CREATE # Cryptographic Identities Generation alice, bob = generate_keypair(), generate_keypair() # Digital Asset Definition (e.g. bicycle) asset = Asset(data={"bicycle": {"manufacturer": "bkfab", "serial_number": "abcd1234"}}) # Metadata Definition metadata = {'planet': 'earth1'} # create trnsaction TODO : owners_before might be node_pubkey in v0.8.0 tx = Transaction.create([alice.public_key], [([alice.public_key], 1)], metadata=metadata, asset=asset) print(" ") print("1.tx_create asset id : alice-----bicycle(", tx.to_dict()['transaction']['asset']['id'], ")----->alice") print("1.tx_create tx id : ", tx.to_dict()['id']) # sign with alice's private key tx = tx.sign([alice.private_key]) tx_id = tx.to_dict()['id'] # write to backlog b = Bigchain() print("1.tx_create db response : ", b.write_transaction(tx)) # wait 2 sec sleep(delay) # get tx by id tx = b.get_transaction(tx_id) print("1.tx_create query : ", tx) print(" ") ##################################################### print("2.write dulpicate tx: ", b.write_transaction(tx))
def get(): # Cryptographic Identities Generation alice, bob = generate_keypair(), generate_keypair() print(" ") # Digital Asset Definition (e.g. bicycle) asset = Asset(data={ "bicycle": { "manufacturer": "bkfab", "serial_number": "abcd1234" } }) # Metadata Definition metadata = {'planet': 'earth'} # create trnsaction TODO : owners_before might be node_pubkey in v0.8.0 tx = Transaction.create([alice.public_key], [alice.public_key], metadata=metadata, asset=asset) # sign with private key tx = tx.sign([alice.private_key]) # get tx by id b = Bigchain() block = b.create_block([tx]) block_voters = block.to_dict()['block']['voters'] print(block_voters) tx_id = '2cb004cad29c0b79872646558f8c867a4c0aecbc4997f0917' tx = b.get_transaction(tx_id) print( "block status : ", b.block_election_status( '2257384a0cee8cf98bd82c3142dd38eee5c268c124b5b357b773b8c6c1fa1221', block_voters))
def _generate_public_and_private_key_if_not_exist(): prifile = settings.PRIKEY_FILENAME pubfile = settings.PUBKEY_FILENAME if not (os.path.exists(prifile) and os.path.exists(pubfile)): print('begin generate public and private key pair.') generate_keypair() print("generated public key file '%s' and private key file '%s'." % (pubfile, prifile))
def bft(): # Cryptographic Identities Generation alice, bob = generate_keypair(), generate_keypair() print(" ") # Digital Asset Definition (e.g. bicycle) asset = Asset(data={ "bicycle": { "manufacturer": "bkfab", "serial_number": "abcd1234" } }) # Metadata Definition metadata = {'planet': 'earth'} # create trnsaction TODO : owners_before might be node_pubkey in v0.8.0 tx = Transaction.create([alice.public_key], [([alice.public_key], 1)], metadata=metadata, asset=asset) # sign with private key tx = tx.sign([alice.private_key]) tx_id = tx.to_dict()['id'] print("tx_id : ", tx_id) # create block b = Bigchain() block = b.create_block([tx]) print("valid block timestamp : ", block.to_dict()['block']['timestamp']) # tamper block block.timestamp = '1' print("tamper block.timestamp to 1 : ") block_id = block.to_dict()['id'] block_voters = block.to_dict()['block']['voters'] print("invalid block timestamp : ", block.to_dict()['block']['timestamp']) print("tamper_block_id : ", block_id) print("db response of block : ", b.write_block(block)) sleep(0) last_voted_id = b.get_last_voted_block().id vote = b.vote(block_id, last_voted_id, True) print("crate vote 'True' : ", vote) print("db response of vote : ", b.write_vote(vote)) print("tamper_block status : ", b.block_election_status(block_id, block_voters)) print("blocks_status_containing_tx : ", b.get_blocks_status_containing_tx(tx_id)) print("wait for 20 sec : ") sleep(20) print("blocks_status_containing_tx : ", b.get_blocks_status_containing_tx(tx_id)) print(" ")
def listen(): own_priv, own_pub = crypto.generate_keypair() remote_sockets = [] remote_keys = [] def forever(s): # Handshake. while True: data = s.recv(1024) if data: remote_user = name.User.from_bytes(data) remote_sockets.append(s) remote_keys.append(remote_user.pub) ip = sock.Server(0, '').ip us = name.User('a chat server', own_pub, ip, 'wellcome') s.sendall(us.to_bytes()) break # Accept text messages. while True: data = s.recv(1024) if data: assert(len(data) < 1024) packet = pack.Packet.from_bytes(data) text = crypto.decrypt(packet.encrypted, own_priv) crypto.verify(text, packet.signature, remote_user.pub) print(text) time.sleep(0.1) handle_input(remote_sockets, own_priv, remote_keys) server = sock.Server(port.CHATSERVER, forever) time.sleep(10) server.alive = False
def tamper_block(): # Cryptographic Identities Generation alice, bob = generate_keypair(), generate_keypair() print(" ") # Digital Asset Definition (e.g. bicycle) asset = Asset(data={ "bicycle": { "manufacturer": "bkfab", "serial_number": "abcd1234" } }) # Metadata Definition metadata = {'planet': 'earth'} # create trnsaction TODO : owners_before might be node_pubkey in v0.8.0 tx = Transaction.create([alice.public_key], [([alice.public_key], 1)], metadata=metadata, asset=asset) # sign with private key tx = tx.sign([alice.private_key]) tx_id = tx.to_dict()['id'] print("tx_id : ", tx_id) # create block b = Bigchain() block = b.create_block([tx]) block_id = block.to_dict()['id'] block_voters = block.to_dict()['block']['voters'] print("last_block_id sig : ", block_id, block.signature) print(block.to_dict()) # tamper block sig block.sign(alice.private_key) block_dict = block.to_dict() print("tamper_block_id sig : ", block_dict['id'], block_dict['signature']) print(block_dict) print("db response : ", b.backend.write_block(serialize(block_dict))) sleep(delay) print("tamper_block status : ", b.block_election_status(block_dict['id'], block_voters)) print("blocks_status_containing_tx : ", b.get_blocks_status_containing_tx(tx_id)) print(" ")
def test(): sock.test() crypto.test() pack.test() time.sleep(2) print('UNIT TESTS PASSED') print() Thread(target=listen).start() time.sleep(1) priv, pub = crypto.generate_keypair() Thread(target=connect, args=['localhost', priv, pub]).start() time.sleep(2) print('INTEGRATION TEST PASSED') print() sys.exit()
def create_transfer(): ##################################################### 1.CREATE # Cryptographic Identities Generation alice, bob = generate_keypair(), generate_keypair() # Digital Asset Definition (e.g. bicycle) asset = Asset(data={"bicycle": {"manufacturer": "bkfab", "serial_number": "abcd1234"}}) # Metadata Definition metadata = {'planet': 'earth'} # create trnsaction TODO : owners_before might be node_pubkey in v0.8.0 tx = Transaction.create([alice.public_key], [([alice.public_key], 1)], metadata=metadata, asset=asset) print(" ") print("1.tx_create asset id : alice-----bicycle(", tx.to_dict()['transaction']['asset']['id'], ")----->alice") print("1.tx_create tx id : ", tx.to_dict()['id']) # sign with alice's private key tx = tx.sign([alice.private_key]) tx_id = tx.to_dict()['id'] # write to backlog b = Bigchain() print("1.tx_create db response : ", b.write_transaction(tx)) # wait 2 sec sleep(delay) # get tx by id tx = b.get_transaction(tx_id) print("1.tx_create query : ", tx) # print("tx1_id:"+tx.to_dict()['id']) print(" ") ##################################################### 2.TRANSFER # inputs and asset [fake] cid = 0 condition = tx.to_dict()['transaction']['conditions'][cid] inputs = Fulfillment.from_dict({ 'fulfillment': condition['condition']['details'], 'input': { 'cid': cid, 'txid': tx.to_dict()['id'], }, 'owners_before': [bob.public_key], }) asset = Asset.from_dict(tx.to_dict()['transaction']['asset']) # transfer tx = Transaction.transfer([inputs], [([bob.public_key], 1)], asset) print("2.tx_fake asset id : bob-----bicycle(", tx.to_dict()['transaction']['asset']['id'], ")----->bob") print("2.tx_fake tx id : ", tx.to_dict()['id']) # sign with bob's private key [fake] tx = tx.sign([bob.private_key]) tx_id = tx.to_dict()['id'] # write to backlog b = Bigchain() print("2.tx_fake db response : ", b.write_transaction(tx)) # wait 2 sec sleep(delay) # get tx by id tx = b.get_transaction(tx_id) print("2.tx_fake query : ", tx) # print("tx2_id:"+tx.to_dict()['id']) print(" ")
def create_transfer(): ##################################################### 1.CREATE # Cryptographic Identities Generation alice, bob, tom = generate_keypair(), generate_keypair(), generate_keypair( ) asset = Asset(data={'money': 'RMB'}, data_id='20170628150000', divisible=True) metadata = {'planet': 'earth'} tx = Transaction.create([alice.public_key], [([alice.public_key], 1000)], metadata=metadata, asset=asset) print(" ") print("1.tx_create asset id : alice-----money 1000(", tx.to_dict()['transaction']['asset']['id'], ")----->alice") print("1.tx_create tx id : ", tx.to_dict()['id']) # sign with alice's private key tx = tx.sign([alice.private_key]) tx_id = tx.to_dict()['id'] # write to backlog b = Bigchain() print("1.tx_create db response : ", b.write_transaction(tx)) # wait 4 sec sleep(delay) # get tx by id tx = b.get_transaction(tx_id) print("1.tx_create query : ", tx) # print("tx1_id:"+tx.to_dict()['id']) print(" ") ##################################################### 2.TRANSFER # inputs and asset cid = 0 condition = tx.to_dict()['transaction']['conditions'][cid] inputs = Fulfillment.from_dict({ 'fulfillment': condition['condition']['details'], 'input': { 'cid': cid, 'txid': tx.to_dict()['id'], }, 'owners_before': condition['owners_after'], }) asset = Asset.from_dict(tx.to_dict()['transaction']['asset']) # transfer tx = Transaction.transfer([inputs], [([bob.public_key], 300), ([alice.public_key], 700)], asset) print("2.tx_transfer asset id : alice-----money 300(", tx.to_dict()['transaction']['asset']['id'], ")----->bob") print("2.tx_transfer tx id : ", tx.to_dict()['id']) print("2.tx_transfer tx : ", tx.to_dict()) print(" ") # sign with alice's private key tx = tx.sign([alice.private_key]) # man in the middle attack, then write to backlog tx.conditions[0].amount = 600 tx.conditions[1].amount = 400 print("3.tx_attack asset id : alice-----money 600(", tx.to_dict()['transaction']['asset']['id'], ")----->tom") print("3.tx_attack tx id : ", tx.to_dict()['id']) print("3.tx_attack tx : ", tx.to_dict()) tx_id = tx.to_dict()['id'] b = Bigchain() print("3.tx_attack db response : ", b.write_transaction(tx)) # wait 4 sec sleep(delay) # get tx by id tx = b.get_transaction(tx_id) print("3.tx_attack query : ", tx) print(" ")
def create_double(): ##################################################### 1.CREATE # Cryptographic Identities Generation alice, bob = generate_keypair(), generate_keypair() # Digital Asset Definition (e.g. money) asset = Asset(data={'money': 'RMB'}, data_id='20170628150000', divisible=True) # Metadata Definition metadata = {'planet': 'earth'} # create trnsaction TODO : owners_before might be node_pubkey in v0.8.0 tx = Transaction.create([alice.public_key], [([alice.public_key], 100), ([alice.public_key], 100)], metadata=metadata, asset=asset) print(" ") print("1.tx_create asset id : alice-----money(", tx.to_dict()['transaction']['asset']['id'], ")----->alice") print("1.tx_create tx id : ", tx.to_dict()['id']) # sign with alice's private key tx = tx.sign([alice.private_key]) tx_id = tx.to_dict()['id'] # write to backlog b = Bigchain() print("1.tx_create db response : ", b.write_transaction(tx)) # wait 2 sec sleep(delay) # get tx by id tx = b.get_transaction(tx_id) unspent = b.get_outputs_filtered_not_include_freeze( alice.public_key, False) print("1.tx_create query : ", tx) print("1.tx_create unspent : ", unspent) print(" ") ##################################################### 2.TRANSFER # inputs and asset cid = 0 condition = tx.to_dict()['transaction']['conditions'][cid] inputs = Fulfillment.from_dict({ 'fulfillment': condition['condition']['details'], 'input': { 'cid': cid, 'txid': tx.to_dict()['id'], }, 'owners_before': [alice.public_key], }) asset = Asset.from_dict(tx.to_dict()['transaction']['asset']) # transfer tx1 = Transaction.transfer([inputs], [([bob.public_key], 100)], asset) print("2.tx_transfer asset id : alice-----money(", tx1.to_dict()['transaction']['asset']['id'], ")----->bob") print("2.tx_transfer tx id : ", tx1.to_dict()['id']) # sign with alice's private key tx1 = tx1.sign([alice.private_key]) tx1_id = tx1.to_dict()['id'] # write to backlog b = Bigchain() print("2.tx_transfer db response : ", b.write_transaction(tx1)) # wait 2 sec sleep(delay) # get tx by id tx1 = b.get_transaction(tx1_id) block = list(b.get_blocks_status_containing_tx(tx1_id).keys())[0] votes = list(b.backend.get_votes_by_block_id(block)) votes_cast = [vote['vote']['is_block_valid'] for vote in votes] election = b.get_blocks_status_containing_tx(tx1_id) print("2.tx_transfer query : ", tx1) print("2.tx_transfer block : ", block) print("2. votes : ", votes) print("2. votes_cast : ", votes_cast) print("2. election : ", election) print(" ") ##################################################### 3.INTERVAL # Cryptographic Identities Generation alice, bob = generate_keypair(), generate_keypair() # Digital Asset Definition (e.g. money) asset = Asset(data={'money': 'RMB'}, data_id='20170628150000', divisible=True) # Metadata Definition metadata = {'planet': 'earth'} # create trnsaction TODO : owners_before might be node_pubkey in v0.8.0 txi = Transaction.create([alice.public_key], [([alice.public_key], 100), ([alice.public_key], 100)], metadata=metadata, asset=asset) print(" ") print("1.tx_create asset id : alice-----money(", tx.to_dict()['transaction']['asset']['id'], ")----->alice") print("1.tx_create tx id : ", txi.to_dict()['id']) # sign with alice's private key txi = txi.sign([alice.private_key]) tx_id = txi.to_dict()['id'] # write to backlog b = Bigchain() print("1.tx_create db response : ", b.write_transaction(tx)) # wait 2 sec sleep(delay) ##################################################### 3.TRANSFER # inputs and asset [double spend] cid = 1 condition = tx.to_dict()['transaction']['conditions'][cid] inputs = Fulfillment.from_dict({ 'fulfillment': condition['condition']['details'], 'input': { 'cid': cid, 'txid': tx.to_dict()['id'], }, 'owners_before': [alice.public_key], }) asset = Asset.from_dict(tx.to_dict()['transaction']['asset']) # transfer tx1 = Transaction.transfer([inputs], [([bob.public_key], 100)], asset) print("3.tx_double asset id : alice-----money(", tx1.to_dict()['transaction']['asset']['id'], ")----->bob") print("3.tx_double tx id : ", tx1.to_dict()['id']) # sign with alice's private key tx1 = tx1.sign([alice.private_key]) tx1_id = tx1.to_dict()['id'] # write to backlog b = Bigchain() print("3.tx_double db response : ", b.write_transaction(tx1)) # wait 2 sec sleep(delay) # get tx by id tx1 = b.get_transaction(tx1_id) block = list(b.get_blocks_status_containing_tx(tx1_id).keys())[0] votes = list(b.backend.get_votes_by_block_id(block)) votes_cast = [vote['vote']['is_block_valid'] for vote in votes] election = b.get_blocks_status_containing_tx(tx1_id) print("3.tx_transfer query : ", tx1) print("3.tx_transfer block : ", block) print("3. votes : ", votes) print("3. votes_cast : ", votes_cast) print("3. election : ", election) print(" ")
# get tx by id tx = b.get_transaction(tx_id) print("freeze 90 asset tx2_id:" + tx.to_dict()['id']) ##################################################### 3.UTXO # inputs and asset utxo = b.get_outputs_filtered_not_include_freeze(alicepub, include_spent) for u in utxo: # print(u) u.pop('details') print('userA unspent asset:') print(json.dumps(utxo, indent=4)) # print(json.load(utxo)) if __name__ == '__main__': alice, bob = generate_keypair(), generate_keypair() alicepublic_key = alice.public_key aliceprivate_key = alice.private_key bobpublic_key = bob.public_key bobprivate_key = bob.private_key include_spent = False print("userA public key : ", alicepublic_key) print("userA private key : ", aliceprivate_key) create_transfer(alicepublic_key, aliceprivate_key, include_spent)
def create_transfer(): ##################################################### 1.CREATE # Cryptographic Identities Generation alice, bob, tom = generate_keypair(), generate_keypair(), generate_keypair() asset = Asset(data={"bicycle": {"manufacturer": "bkfab", "serial_number": "abcd1234"}}) metadata = {'planet': 'earth'} tx = Transaction.create([alice.public_key], [([alice.public_key], 1)], metadata=metadata, asset=asset) print(" ") print("1.tx_create asset id : alice-----bicycle(", tx.to_dict()['transaction']['asset']['id'], ")----->alice") print("1.tx_create tx id : ", tx.to_dict()['id']) # sign with alice's private key tx = tx.sign([alice.private_key]) tx_id = tx.to_dict()['id'] # write to backlog b = Bigchain() print("1.tx_create db response : ", b.write_transaction(tx)) # wait 4 sec sleep(delay) # get tx by id tx = b.get_transaction(tx_id) print("1.tx_create query : ", tx) # print("tx1_id:"+tx.to_dict()['id']) print(" ") ##################################################### 2.TRANSFER # inputs and asset cid = 0 condition = tx.to_dict()['transaction']['conditions'][cid] inputs = Fulfillment.from_dict({ 'fulfillment': condition['condition']['details'], 'input': { 'cid': cid, 'txid': tx.to_dict()['id'], }, 'owners_before': condition['owners_after'], }) asset = Asset.from_dict(tx.to_dict()['transaction']['asset']) # transfer tx = Transaction.transfer([inputs], [([bob.public_key], 1)], asset) print("2.tx_transfer asset id : alice-----bicycle(", tx.to_dict()['transaction']['asset']['id'], ")----->bob") print("2.tx_transfer tx id : ", tx.to_dict()['id']) print("2.tx_transfer tx : ", tx.to_dict()) print(" ") # sign with alice's private key tx = tx.sign([alice.private_key]) # man in the middle attack, then write to backlog tx.conditions[0].owners_after = [tom.public_key] print("3.tx_attack asset id : alice-----bicycle(", tx.to_dict()['transaction']['asset']['id'], ")----->tom") print("3.tx_attack tx id : ", tx.to_dict()['id']) print("3.tx_attack tx : ", tx.to_dict()) tx_id = tx.to_dict()['id'] b = Bigchain() print("3.tx_attack db response : ", b.write_transaction(tx)) # wait 4 sec sleep(delay) # get tx by id tx = b.get_transaction(tx_id) print("3.tx_attack query : ", tx) print(" ")
rat test - run all unnit and integration tests rat help - print this message ''' print(h) if __name__ == '__main__': import sys if len(sys.argv) < 2 or sys.argv[1] == 'help': print_help() elif sys.argv[1] == 'generate': assert(len(sys.argv) == 3) keypath = sys.argv[2] priv, pub = crypto.generate_keypair() crypto.write_keypair(priv, pub, keypath) elif sys.argv[1] == 'serve': serve() elif sys.argv[1] == 'register': _, own_pub = crypto.read_keypair(get_conf()['user']['keypath']) register(sys.argv[2], own_pub) elif sys.argv[1] == 'ask': if len(sys.argv) >= 4: ask(sys.argv[2], sys.argv[3:len(sys.argv)]) else: print('To query namserververs please provide your regex and their IPs.')