technologies2

Blogs

Told You, We Love Sharing!

  • Creating An Application Using BigchainDB in Python

    BigchainDB is a development platform that has blockchain properties such as decentralization, permanence, proprietor controlled resources, etc., and database properties such as high exchange rate, low inertness, ordering and questioning of organized information. It was first discharged—open source—in February 2016 and has been enhanced continuously from that point forward.

    BigchainDB can serve a myriad of use cases. Wherever there is a requirement for immutable data highlighting digital assets, BigchainDB can come in use. There are numerous industries which can use BigchainDB features for their benefits. In this blog, we will be creating an application using BigchainDB in Python.

    To use BigchainDB we need to install BigchainDB driver for this command
    pip3 install -U bigchaindb-driver
    To connect with BigchainDB we use url that is
    bdb_root_url = 'http://localhost:9984'  # this is your local set up BigchainDb url
    
    If you have not set up Bigchaindb locally then you can use BigchainDB testnet url that is
    bdb_root_url = https://test.bigchaindb.com     # this is testnet url of BigchainDB
    If we use testnet url then we will pass 
    App_id & app_key that we will find after signup on url 
    https://testnet.bigchaindb.com/
    2. To connect with BigchainDB 
    We will import this class
    from bigchaindb_driver import BigchainDB 
    Add appi_id & app_key in header of url like this
    bdb = BigchainDB(
        'https://test.bigchaindb.com',
        headers={'app_id': 'da9c76b6'
                 'app_key': '34f1acaf209503732a3521afb184560a'})
    3. Create private key & public key.
    Import this class to create private key & public key
    from bigchaindb_driver.crypto import generate_keypair
    & create by this method
    user1, user2 = generate_keypair(), generate_keypair()
    
    3. Code of creating asset .
    
    from bigchaindb_driver import BigchainDB
    from bigchaindb_driver.crypto import generate_keypair
    
    user1, user2 = generate_keypair(), generate_keypair()
    
    bdb = BigchainDB(
        'https://test.bigchaindb.com',
        headers={'app_id': 'da9c76b6',
                 'app_key': '34f1acaf209503732a3521afb184560a'})
    Test_asset = {
        'data': {
            'test22': {
                'serial_number': 'abcd123412',
                'manufacturer': 'bkfabahr'
            },
        },
    }
    
    Test_asset_metadata = {
        'country': 'india'
    }
    
    prepared_creation_tx = bdb.transactions.prepare(
        operation='CREATE',
        signers=user1.public_key,
        asset=Test_asset,
        metadata=Test_asset_metadata
    )
    
    fulfilled_creation_tx = bdb.transactions.fulfill(
        prepared_creation_tx,
        private_keys=user1.private_key
    )
    
    sent_creation_tx = bdb.transactions.send_commit(fulfilled_creation_tx)
    print('create')
    print(sent_creation_tx)
    txid = fulfilled_creation_tx['id']
    
    b = bdb.transactions.get(asset_id=txid)
    print('b')
    print(b)
    Response-> after successfull run code
    create
    {'asset': {'data': {'bicycle': {'manufacturer': 'bkfab', 'serial_number': 'abcd1234'}}}, 'id': 'd7fb62d90f1c67861e520cffeb6e7c6a62ed3b7ba335ebfc784c8baba46592a0', 'inputs': [{'fulfillment': 'pGSAIIg31Ki3DZbYOAZMnBmVgV0-hfy_xeDiz9d83I6jBmxagUBKbgqZksjvhMAeyGPSHNDnIjBqmgM8qVdy641Z0PQNB27dbZNYsnGac2fgzkZxXyDx6YqBx1WNCG-wT8BCTokL', 'fulfills': None, 'owners_before': ['AAjpnRqzJRhS8xpQsXNLV1hdaB8gUwgE41qJhinfGtqw']}], 'metadata': {'planet': 'earth'}, 'operation': 'CREATE', 'outputs': [{'amount': '1', 'condition': {'details': {'public_key': 'AAjpnRqzJRhS8xpQsXNLV1hdaB8gUwgE41qJhinfGtqw', 'type': 'ed25519-sha-256'}, 'uri': 'ni:///sha-256;oFneJQzJm7Nq5k786xXdDRUCd-48WFQ1HLlkyojar3M?fpt=ed25519-sha-256&cost=131072'}, 'public_keys': ['AAjpnRqzJRhS8xpQsXNLV1hdaB8gUwgE41qJhinfGtqw']}], 'version': '2.0'}
    4. Transfer Asset ownership.
    We are transfer previously created transaction 
    transfer_asset = {
        'id': asset_id
    }
    
    output_index = 0
    output = fulfilled_creation_tx['outputs'][output_index]
    
    transfer_input = {
        'fulfillment': output['condition']['details'],
        'fulfills': {
            'output_index': output_index,
            'transaction_id': fulfilled_creation_tx['id']
        },
        'owners_before': output['public_keys']
    }
    
    prepared_transfer_tx = bdb.transactions.prepare(
        operation='TRANSFER',
        asset=transfer_asset,
        inputs=transfer_input,
        recipients=user2.public_key,
    )
    
    fulfilled_transfer_tx = bdb.transactions.fulfill(
        prepared_transfer_tx,
        private_keys=alice.private_key,
    )
    
    sent_transfer_tx = bdb.transactions.send_commit(fulfilled_transfer_tx)
    
    print('sent_transfer_tx')
    print(sent_transfer_tx)
     Response are like this of transfer asset ->
    {'asset': {'id': 'd7fb62d90f1c67861e520cffeb6e7c6a62ed3b7ba335ebfc784c8baba46592a0'}, 'id': '0ee2929a01e949fe051d1cfaaa643556e6f5caef5d9fb932dae241a9efe98f65', 'inputs': [{'fulfillment': 'pGSAIIg31Ki3DZbYOAZMnBmVgV0-hfy_xeDiz9d83I6jBmxagUB3Lj4-f5VhdYkiMLuWDrUhctY-_wQVvOcCdtLy0o7zaMGl-rPmx8k0wK0WXdz-24gkOqqDkJh9fp8uB7RQkNIB', 'fulfills': {'output_index': 0, 'transaction_id': 'd7fb62d90f1c67861e520cffeb6e7c6a62ed3b7ba335ebfc784c8baba46592a0'}, 'owners_before': ['AAjpnRqzJRhS8xpQsXNLV1hdaB8gUwgE41qJhinfGtqw']}], 'metadata': None, 'operation': 'TRANSFER', 'outputs': [{'amount': '1', 'condition': {'details': {'public_key': '7STQGzddjH2RiiQEedxatVuoi7CqV7NbPPWRxaubaTke', 'type': 'ed25519-sha-256'}, 'uri': 'ni:///sha-256;JpyXA6_WsPe-MEyBTchq0XVGsqBR_BnmF1fZI0PD8Qc?fpt=ed25519-sha-256&cost=131072'}, 'public_keys': ['7STQGzddjH2RiiQEedxatVuoi7CqV7NbPPWRxaubaTke']}], 'version': '2.0'}
    5. Get transaction details
    Code of get transaction details
    from bigchaindb_driver import BigchainDB
    from bigchaindb_driver.crypto import generate_keypair
    
    bdb = BigchainDB(
        'https://test.bigchaindb.com',
        headers={'app_id': 'da9c76b6',
                 'app_key': '34f1acaf209503732a3521afb184560a'})
    
    
    
    
    print('txid')
    
    b = bdb.transactions.get(asset_id='d7fb62d90f1c67861e520cffeb6e7c6a62ed3b7ba335ebfc784c8baba46592a0')
    print('b')
    print(b)
    Response are like this
    [{'inputs': [{'owners_before': ['AAjpnRqzJRhS8xpQsXNLV1hdaB8gUwgE41qJhinfGtqw'], 'fulfills': None, 'fulfillment': 'pGSAIIg31Ki3DZbYOAZMnBmVgV0-hfy_xeDiz9d83I6jBmxagUBKbgqZksjvhMAeyGPSHNDnIjBqmgM8qVdy641Z0PQNB27dbZNYsnGac2fgzkZxXyDx6YqBx1WNCG-wT8BCTokL'}], 'outputs': [{'public_keys': ['AAjpnRqzJRhS8xpQsXNLV1hdaB8gUwgE41qJhinfGtqw'], 'condition': {'details': {'type': 'ed25519-sha-256', 'public_key': 'AAjpnRqzJRhS8xpQsXNLV1hdaB8gUwgE41qJhinfGtqw'}, 'uri': 'ni:///sha-256;oFneJQzJm7Nq5k786xXdDRUCd-48WFQ1HLlkyojar3M?fpt=ed25519-sha-256&cost=131072'}, 'amount': '1'}], 'operation': 'CREATE', 'metadata': {'planet': 'earth'}, 'asset': {'data': {'bicycle': {'serial_number': 'abcd1234', 'manufacturer': 'bkfab'}}}, 'version': '2.0', 'id': 'd7fb62d90f1c67861e520cffeb6e7c6a62ed3b7ba335ebfc784c8baba46592a0'}, {'inputs': [{'owners_before': ['AAjpnRqzJRhS8xpQsXNLV1hdaB8gUwgE41qJhinfGtqw'], 'fulfills': {'transaction_id': 'd7fb62d90f1c67861e520cffeb6e7c6a62ed3b7ba335ebfc784c8baba46592a0', 'output_index': 0}, 'fulfillment': 'pGSAIIg31Ki3DZbYOAZMnBmVgV0-hfy_xeDiz9d83I6jBmxagUB3Lj4-f5VhdYkiMLuWDrUhctY-_wQVvOcCdtLy0o7zaMGl-rPmx8k0wK0WXdz-24gkOqqDkJh9fp8uB7RQkNIB'}], 'outputs': [{'public_keys': ['7STQGzddjH2RiiQEedxatVuoi7CqV7NbPPWRxaubaTke'], 'condition': {'details': {'type': 'ed25519-sha-256', 'public_key': '7STQGzddjH2RiiQEedxatVuoi7CqV7NbPPWRxaubaTke'}, 'uri': 'ni:///sha-256;JpyXA6_WsPe-MEyBTchq0XVGsqBR_BnmF1fZI0PD8Qc?fpt=ed25519-sha-256&cost=131072'}, 'amount': '1'}], 'operation': 'TRANSFER', 'metadata': None, 'asset': {'id': 'd7fb62d90f1c67861e520cffeb6e7c6a62ed3b7ba335ebfc784c8baba46592a0'}, 'version': '2.0', 'id': '0ee2929a01e949fe051d1cfaaa643556e6f5caef5d9fb932dae241a9efe98f65'}]
    
    6. To Check tranaction have successfully send or not !
    from bigchaindb_driver import BigchainDB
    from bigchaindb_driver.crypto import generate_keypair
    
    bdb = BigchainDB(
        'https://test.bigchaindb.com',
        headers={'app_id': 'da9c76b6',b
    2523
                 'app_key': '34f1acaf209503732a3521afb184560a'})
    b = block_height = bdb.blocks.get(txid='d7fb62d90f1c67861e520cffeb6e7c6a62ed3b7ba335ebfc784c8baba46592a0')
    print('Block Number')
    print(b)
    Response -> this will show block number thats hold the transaction details
    Block Number
    2523
    
    
    Source: https://docs.bigchaindb.com/projects/py-driver/en/latest/usage.html


No Comments Yet.


Leave a Comment

Name is required

Comment is required




Latest Trends

Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.