1
0
mirror of https://github.com/bigchaindb/js-bigchaindb-driver.git synced 2024-12-27 23:27:50 +01:00

Small fix query-all-example

This commit is contained in:
Michiel Mulders 2018-01-20 15:22:07 +01:00
parent 1aa295830d
commit 10fab5323a
3 changed files with 60 additions and 5 deletions

View File

@ -1,12 +1,13 @@
{
"name": "js-driver-bigchaindb-examples",
"version": "1.0.0",
"main": "src/app.js",
"main": "src/basic-usage.js",
"scripts": {
"start": "nodemon src/app.js --exec babel-node",
"build": "npm run clean && babel src -d dist",
"serve": "node dist/app.js",
"clean": "rimraf ./dist"
"serve": "node dist/basic-usage.js",
"clean": "rimraf ./dist",
"start": "nodemon src/basic-usage.js --exec babel-node",
"query-assets": "nodemon src/query-assets.js --exec babel-node"
},
"author": "BigchainDB",
"license": "MIT",

View File

@ -1,6 +1,6 @@
// const driver = require('../../../src/index')
const driver = require('bigchaindb-driver')
// ======== Preparation ======== //
const conn = new driver.Connection('https://test.bigchaindb.com/api/v1/', {
app_id: 'c17a9968',
@ -19,6 +19,7 @@ const assetdata = {
const metadata = { 'planet': 'earth' }
// ======== Create Transaction Bicycle ======== //
const txCreateAliceSimple = driver.Transaction.makeCreateTransaction(
assetdata,
@ -32,11 +33,13 @@ const txCreateAliceSimple = driver.Transaction.makeCreateTransaction(
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(() => {
const txTransferBob = driver.Transaction.makeTransferTransaction(
@ -56,6 +59,7 @@ conn.postTransaction(txCreateAliceSimpleSigned)
console.log('Was Alice the previous owner?', tx.inputs[0].owners_before[0] === alice.publicKey) // eslint-disable-line no-console
})
// ======== Search Asset by Serial Number ======== //
.then(() => conn.searchAssets('Bicycle Inc.'))
.then(assets => console.log('Found assets with serial number Bicycle Inc.:', assets)) // eslint-disable-line no-console

View File

@ -0,0 +1,50 @@
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()
// ======== Asset Array ======== //
const assetArray = []
assetArray.push({ 'bicycle': { 'serial_number': 'abc', 'manufacturer': 'BicyclesInc' } })
assetArray.push({ 'bicycle': { 'serial_number': 'cde', 'manufacturer': 'BicyclesInc' } })
assetArray.push({ 'bicycle': { 'serial_number': 'fgh', 'manufacturer': 'BicyclesInc' } })
const metadata = { 'planet': 'Pluto' }
// ======== Create Transactions for bicycles ======== //
function createTx(assetdata) {
const txCreate = driver.Transaction.makeCreateTransaction(
assetdata,
metadata,
[
driver.Transaction.makeOutput(driver.Transaction.makeEd25519Condition(alice.publicKey))
],
alice.publicKey
)
const txCreateSigned = driver.Transaction.signTransaction(txCreate, alice.privateKey)
return conn.postTransaction(txCreateSigned)
.then(() => conn.pollStatusAndFetchTransaction(txCreateSigned.id))
}
// ======== Execute all promises in order to post transactions and fetch them ======== //
Promise.all(assetArray.map(createTx))
// ======== Querying Assets for Assetdata ======== //
.then(() => conn.searchAssets('BicyclesInc'))
.then(assets => console.log('Found assets with serial number "BicyclesInc":', assets)) // eslint-disable-line no-console
// ======== Querying Assets for Metadata ======== //
.then(() => conn.searchMetadata('Pluto'))
.then(assets => console.log('Found assets with metadata "Pluto":', assets)) // eslint-disable-line no-console