1
0
mirror of https://github.com/bigchaindb/js-bigchaindb-driver.git synced 2025-01-07 20:26:17 +01:00
js-bigchaindb-driver/examples/basic-usage-boilerplate/src/app.js

62 lines
2.3 KiB
JavaScript
Raw Normal View History

2018-01-20 14:25:15 +01:00
// const driver = require('../../../src/index')
2018-01-20 14:45:57 +01:00
const driver = require('bigchaindb-driver')
2018-01-20 14:25:15 +01:00
// ======== 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' }
// ======== Create Transaction Bicycle ======== //
const txCreateAliceSimple = driver.Transaction.makeCreateTransaction(
assetdata,
metadata,
2018-01-20 14:45:57 +01:00
[
driver.Transaction.makeOutput(driver.Transaction.makeEd25519Condition(alice.publicKey))
2018-01-20 14:25:15 +01:00
],
alice.publicKey
)
const txCreateAliceSimpleSigned =
driver.Transaction.signTransaction(txCreateAliceSimple, alice.privateKey)
// ======== Post Transaction and Fetch Result ======== //
conn.postTransaction(txCreateAliceSimpleSigned)
// Check status of transaction every 0.5 seconds until fulfilled
.then(() => conn.pollStatusAndFetchTransaction(txCreateAliceSimpleSigned.id))
// ======== Transfer Bicycle to Bob ======== //
.then(() => {
2018-01-20 14:45:57 +01:00
const txTransferBob = driver.Transaction.makeTransferTransaction(
[{ tx: txCreateAliceSimpleSigned, output_index: 0 }],
[driver.Transaction.makeOutput(driver.Transaction.makeEd25519Condition(bob.publicKey))],
{ price: '100 euro' }
)
2018-01-20 14:25:15 +01:00
2018-01-20 14:45:57 +01:00
// Sign transfer transaction with Alice's private key
const txTransferBobSigned = driver.Transaction.signTransaction(txTransferBob, alice.privateKey)
2018-01-20 14:25:15 +01:00
return conn.postTransaction(txTransferBobSigned)
})
2018-01-20 14:45:57 +01:00
.then(res => conn.pollStatusAndFetchTransaction(res.id))
2018-01-20 14:25:15 +01:00
.then(tx => {
2018-01-20 14:45:57 +01:00
console.log('Is Bob the owner?', tx.outputs[0].public_keys[0] === bob.publicKey) // eslint-disable-line no-console
console.log('Was Alice the previous owner?', tx.inputs[0].owners_before[0] === alice.publicKey) // eslint-disable-line no-console
2018-01-20 14:25:15 +01:00
})
// ======== Search Asset by Serial Number ======== //
.then(() => conn.searchAssets('Bicycle Inc.'))
2018-01-20 14:45:57 +01:00
.then(assets => console.log('Found assets with serial number Bicycle Inc.:', assets)) // eslint-disable-line no-console