示例#1
0
from bigchaindb import Bigchain
import writeout

b = Bigchain()

# create 7 test users
testuser1_priv, testuser1_pub = b.generate_keys()
writeout.exportData((testuser1_priv, testuser1_pub), "user1")

testuser2_priv, testuser2_pub = b.generate_keys()
writeout.exportData((testuser2_priv, testuser2_pub), "user2")

testuser3_priv, testuser3_pub = b.generate_keys()
writeout.exportData((testuser3_priv, testuser3_pub), "user3")

testuser4_priv, testuser4_pub = b.generate_keys()
writeout.exportData((testuser4_priv, testuser4_pub), "user4")

testuser5_priv, testuser5_pub = b.generate_keys()
writeout.exportData((testuser5_priv, testuser5_pub), "user5")

testuser6_priv, testuser6_pub = b.generate_keys()
writeout.exportData((testuser6_priv, testuser6_pub), "user6")

testuser7_priv, testuser7_pub = b.generate_keys()
writeout.exportData((testuser7_priv, testuser7_pub), "govt")
示例#2
0
# "NET PRICE":  "25" ,
# "PRICE":  "50" ,
# "QUANTITY":  "50000" ,
# "SIDE":  "S" ,
# "TYPE":  "EQ"
# },
# ...]
data = []
with open('data.csv', 'r') as fid:
    reader = csv.DictReader(fid)
    for row in reader:
        data.append(row)

# generate distinct users from the CPTY1 column and assign keys
users = []
for user_name in set([d['CPTY1'] for d in data]):
    sk, vk = b.generate_keys()
    user = users.append(
        {
            'name': user_name,
            'signing_key': sk,
            'verifying_key': vk
        }
    )

# create assets on the blockchain with the payload and assign to the user
for asset in data:
    user = [u for u in users if u['name'] == asset['CPTY1']][0]
    tx = b.create_transaction(b.me, user['verifying_key'], None, 'CREATE', payload=asset)
    tx_signed = b.sign_transaction(tx, b.me_private)
    b.write_transaction(tx_signed)
# env
#
b = Bigchain()


# lib
#
def sign_and_write(tx, private_key):
    tx_signed = b.sign_transaction(tx, private_key)
    b.write_transaction(tx_signed)
    return tx_signed


# setup
#
pvt_key, pub_key = b.generate_keys()
pvt_key2, pub_key2 = b.generate_keys()

print("Public Key:", pub_key, "\n")

# init
#
#
# db initialization
# first transaction - asset creation - required, asset logic not used for our use-case: storing arbitrary data
print("DB init - first TX:")
asset_payload = {'msg': 'Arbitrary data asset'}
tx = b.create_transaction(b.me, pub_key, None, 'CREATE', payload=asset_payload)
tx_signed = sign_and_write(tx, b.me_private)
print("TX:", tx_signed, "\n")