资产创建
from bigchaindb_driver import BigchainDB
from bigchaindb_driver.crypto import generate_keypair
bdb_root_url = 'https://example.com:9984' # Use YOUR BigchainDB Root URL here
bdb = BigchainDB(bdb_root_url)
alice = generate_keypair()
digital_asset_payload = {'data': {'msg': 'Hello BigchainDB!'}}
tx = bdb.transactions.prepare(operation='CREATE',
signers=alice.public_key,
asset=digital_asset_payload)
signed_tx = bdb.transactions.fulfill(tx, private_keys=alice.private_key)
sent_tx = bdb.transactions.send(signed_tx)
sent_tx == signed_tx
读取创建的事务
# Retrieve a block height
block_height = bdb.blocks.get(txid=signed_tx['id'])
# Retrieve a block
block = bdb.blocks.retrieve(block_height)
转移数字资产
为了准备一个转账事务,Alice需要提供至少三样东西:
- inputs - 一个或多个实现先前事务的输出条件的实现
- asset['id'] - 被转移的资产的id
- 接收者的public_keys - 一个或多个表示新接收方的公钥。
创建input
output_index = 0
output = signed_tx['outputs'][output_index]
input_ = {
'fulfillment': output['condition']['details'],
'fulfills': {
'output_index': output_index,
'transaction_id': signed_tx['id'],
},
'owners_before': output['public_keys'],
}
转账事务中的资产必须是带有id键的字典:
transfer_asset_id = signed_tx['id']
transfer_asset = {
'id': transfer_asset_id,
}
准备转移事务
tx_transfer = bdb.transactions.prepare(
operation='TRANSFER',
inputs=input_,
asset=transfer_asset,
recipients=bob.public_key,
)
交易现在需要由alice来完成:
signed_tx_transfer = bdb.transactions.fulfill(
tx_transfer,
private_keys=alice.private_key,
)
sent_tx_transfer = bdb.transactions.send(signed_tx_transfer)
Double Spends
相同的数字资产多次交易
from bigchaindb_driver.exceptions import BigchaindbException
try:
bdb.transactions.send(fulfilled_tx_transfer_2)
except BigchaindbException as e:
print(e.info)
# {'message': 'Invalid transaction', 'status': 400}
Multiple Owners
多个所有者
car_creation_tx = bdb.transactions.prepare(
operation='CREATE',
signers=alice.public_key,
recipients=(alice.public_key, bob.public_key),
asset=car_asset,
)
signed_car_creation_tx = bdb.transactions.fulfill(
car_creation_tx,
private_keys=alice.private_key,
)
或
signed_car_creation_tx = bdb.transactions.fulfill(
car_creation_tx,
private_keys=[alice.private_key, bob.private_key],
)
输出条件的目的是锁定交易,这样就需要一个有效的输入实现来解锁它。
在基于签名的方案中,锁基本上是一个公钥,这样为了解锁事务,就需要有私钥。