mirror of
https://github.com/bigchaindb/js-bigchaindb-driver.git
synced 2024-12-27 23:27:50 +01:00
Basic usage async example
This commit is contained in:
parent
01deae78cd
commit
87f36946f0
@ -9,6 +9,7 @@
|
||||
"start": "nodemon src/basic-usage.js --exec babel-node",
|
||||
"query-assets": "nodemon src/query-assets.js --exec babel-node",
|
||||
"seed-func": "nodemon src/seed-func.js --exec babel-node",
|
||||
"basic-async": "nodemon src/basic-usage-async-await.js --exec babel-node",
|
||||
"websocket": "nodemon src/websocket.js --exec babel-node"
|
||||
},
|
||||
"author": "BigchainDB",
|
||||
|
@ -0,0 +1,61 @@
|
||||
const driver = require('bigchaindb-driver')
|
||||
|
||||
|
||||
// ======== Preparation ======== //
|
||||
const conn = new driver.Connection('https://test.bigchaindb.com/api/v1/', {
|
||||
app_id: 'c17a9968',
|
||||
app_key: '0b277b94893e7b0a5b4e6afd6bccb01d'
|
||||
})
|
||||
|
||||
const alice = new driver.Ed25519Keypair()
|
||||
const bob = new driver.Ed25519Keypair()
|
||||
|
||||
const assetdata = {
|
||||
'bicycle': {
|
||||
'serial_number': 'abcd1234',
|
||||
'manufacturer': 'Bicycle Inc.',
|
||||
}
|
||||
}
|
||||
|
||||
const metadata = { 'planet': 'earth' }
|
||||
|
||||
|
||||
// Call async basic usage function
|
||||
basicUsage()
|
||||
|
||||
|
||||
async function basicUsage() {
|
||||
// ======== Create Transaction Bicycle ======== //
|
||||
const txCreateAliceSimple = driver.Transaction.makeCreateTransaction(
|
||||
assetdata,
|
||||
metadata,
|
||||
[
|
||||
driver.Transaction.makeOutput(driver.Transaction.makeEd25519Condition(alice.publicKey))
|
||||
],
|
||||
alice.publicKey
|
||||
)
|
||||
|
||||
const txCreateAliceSimpleSigned =
|
||||
driver.Transaction.signTransaction(txCreateAliceSimple, alice.privateKey)
|
||||
|
||||
|
||||
// ======== Post Transaction and Fetch Result ======== //
|
||||
await conn.postTransaction(txCreateAliceSimpleSigned)
|
||||
await conn.pollStatusAndFetchTransaction(txCreateAliceSimpleSigned.id)
|
||||
|
||||
const txTransferBob = driver.Transaction.makeTransferTransaction(
|
||||
[{ tx: txCreateAliceSimpleSigned, output_index: 0 }],
|
||||
[driver.Transaction.makeOutput(driver.Transaction.makeEd25519Condition(bob.publicKey))],
|
||||
{ price: '100 euro' }
|
||||
)
|
||||
|
||||
const txTransferBobSigned = driver.Transaction.signTransaction(txTransferBob, alice.privateKey)
|
||||
|
||||
await conn.postTransaction(txTransferBobSigned)
|
||||
await conn.pollStatusAndFetchTransaction(txTransferBobSigned.id)
|
||||
|
||||
|
||||
// ======== Querying Assets ======== //
|
||||
const assets = await conn.searchAssets('Bicycle Inc.')
|
||||
console.log(assets) // eslint-disable-line no-console
|
||||
}
|
Loading…
Reference in New Issue
Block a user