From 2deba0cad8b6880ba9a7a94454f86feb7c2b3c5c Mon Sep 17 00:00:00 2001 From: Michiel Mulders Date: Sat, 20 Jan 2018 14:25:15 +0100 Subject: [PATCH 1/8] First example --- examples/basic-usage-boilerplate/.babelrc | 4 + examples/basic-usage-boilerplate/.gitignore | 1 + examples/basic-usage-boilerplate/README.md | 2 + examples/basic-usage-boilerplate/package.json | 31 +++++++ examples/basic-usage-boilerplate/src/app.js | 81 +++++++++++++++++++ examples/basic-usage/index.js | 71 ++++++++++++++++ 6 files changed, 190 insertions(+) create mode 100644 examples/basic-usage-boilerplate/.babelrc create mode 100644 examples/basic-usage-boilerplate/.gitignore create mode 100644 examples/basic-usage-boilerplate/README.md create mode 100644 examples/basic-usage-boilerplate/package.json create mode 100644 examples/basic-usage-boilerplate/src/app.js create mode 100644 examples/basic-usage/index.js diff --git a/examples/basic-usage-boilerplate/.babelrc b/examples/basic-usage-boilerplate/.babelrc new file mode 100644 index 0000000..d2903f1 --- /dev/null +++ b/examples/basic-usage-boilerplate/.babelrc @@ -0,0 +1,4 @@ +{ + "presets": ["es2015", "stage-3"], + "plugins": ["syntax-async-functions", "transform-runtime", "transform-regenerator", "transform-async-to-generator"] +} \ No newline at end of file diff --git a/examples/basic-usage-boilerplate/.gitignore b/examples/basic-usage-boilerplate/.gitignore new file mode 100644 index 0000000..b512c09 --- /dev/null +++ b/examples/basic-usage-boilerplate/.gitignore @@ -0,0 +1 @@ +node_modules \ No newline at end of file diff --git a/examples/basic-usage-boilerplate/README.md b/examples/basic-usage-boilerplate/README.md new file mode 100644 index 0000000..55f98e5 --- /dev/null +++ b/examples/basic-usage-boilerplate/README.md @@ -0,0 +1,2 @@ +# Usage +`npm install` \ No newline at end of file diff --git a/examples/basic-usage-boilerplate/package.json b/examples/basic-usage-boilerplate/package.json new file mode 100644 index 0000000..ed39c5f --- /dev/null +++ b/examples/basic-usage-boilerplate/package.json @@ -0,0 +1,31 @@ +{ + "name": "js-driver-bigchaindb-examples", + "version": "1.0.0", + "main": "src/app.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" + }, + "author": "BigchainDB", + "license": "MIT", + "devDependencies": { + "babel-cli": "^6.26.0", + "babel-core": "^6.13.2", + "babel-loader": "^6.2.4", + "babel-plugin-syntax-async-functions": "^6.13.0", + "babel-plugin-transform-async-to-generator": "^6.8.0", + "babel-plugin-transform-regenerator": "^6.11.4", + "babel-plugin-transform-runtime": "^6.12.0", + "babel-preset-es2015": "^6.13.2", + "babel-preset-stage-3": "^6.11.0", + "nodemon": "^1.14.8", + "rimraf": "^2.6.2" + }, + "repository": "/", + "private": true, + "dependencies": { + "bigchaindb-driver": "^3.2.0" + } +} diff --git a/examples/basic-usage-boilerplate/src/app.js b/examples/basic-usage-boilerplate/src/app.js new file mode 100644 index 0000000..f5c3543 --- /dev/null +++ b/examples/basic-usage-boilerplate/src/app.js @@ -0,0 +1,81 @@ +// const driver = require('../../../src/index') +import * as driver from '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' } + +// ======== 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) +console.log(txCreateAliceSimpleSigned) + +// ======== Post Transaction and Fetch Result ======== // +conn.postTransaction(txCreateAliceSimpleSigned) + // Check status of transaction every 0.5 seconds until fulfilled + .then(() => conn.pollStatusAndFetchTransaction(txCreateAliceSimpleSigned.id)) + .then(retrievedTx => { + console.log('Transaction', retrievedTx.id, 'successfully posted.') + return retrievedTx + }) + + // Check status after transaction has completed (result: { 'status': 'valid' }) + // If you check the status of a transaction before it's added to BigchainDB, + // BigchainDb will return that the transaction is still waiting in the 'backlog' + .then(() => conn.getStatus(txCreateAliceSimpleSigned.id)) + .then(status => console.log('Retrieved status method 2: ', status)) + +// ======== Transfer Bicycle to Bob ======== // + .then(() => { + try { + console.log(txCreateAliceSimpleSigned) + const txTransferBob = driver.Transaction.makeTransferTransaction( + [{ tx: txCreateAliceSimpleSigned, output_index: 0 }], + [driver.Transaction.makeOutput(driver.Transaction.makeEd25519Condition(bob.publicKey))], + { price: '100 euro' } + ) + + // Sign transfer transaction with Alice's private key + const txTransferBobSigned = driver.Transaction.signTransaction(txTransferBob, alice.privateKey) + console.log('Posting signed transaction: ', txTransferBobSigned) + } catch (err) { + console.log(err) + } + + return conn.postTransaction(txTransferBobSigned) + }) + .then(res => { + console.log('Response from BDB server:', res) + return conn.pollStatusAndFetchTransaction(res.id) + }) + .then(tx => { + console.log('Is Bob the owner?', tx.outputs[0].public_keys[0] === bob.publicKey) + console.log('Was Alice the previous owner?', tx.inputs[0].owners_before[0] === alice.publicKey) + }) + +// ======== Search Asset by Serial Number ======== // + .then(() => conn.searchAssets('Bicycle Inc.')) + .then(assets => console.log('Found assets with serial number Bicycle Inc.:', assets)) diff --git a/examples/basic-usage/index.js b/examples/basic-usage/index.js new file mode 100644 index 0000000..c6d4013 --- /dev/null +++ b/examples/basic-usage/index.js @@ -0,0 +1,71 @@ +const driver = require('../../src/index') + +// ======== 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, + [driver.Transaction.makeOutput( + driver.Transaction.makeEd25519Condition(alice.publicKey)) + ], + 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)) + .then(retrievedTx => console.log('Transaction', retrievedTx.id, 'successfully posted.')) + + // Check status after transaction has completed (result: { 'status': 'valid' }) + // If you check the status of a transaction before it's added to BigchainDB, + // BigchainDb will return that the transaction is still waiting in the 'backlog' + .then(() => conn.getStatus(txCreateAliceSimpleSigned.id)) + .then(status => console.log('Retrieved status method 2: ', status)) + +// ======== Transfer Bicycle to Bob ======== // + .then(() => { + const txTransferBob = driver.Transaction.makeTransferTransaction( + [{ tx: txCreateAliceSimpleSigned, output_index: 0 }], + [driver.Transaction.makeOutput(driver.Transaction.makeEd25519Condition(bob.publicKey))], + { price: '100 euro' } + ) + + // Sign transfer transaction with Alice's private key + const txTransferBobSigned = driver.Transaction.signTransaction(txTransferBob, alice.privateKey) + console.log('Posting signed transaction: ', txTransferBobSigned) + + return conn.postTransaction(txTransferBobSigned) + }) + .then(res => { + console.log('Response from BDB server:', res) + return conn.pollStatusAndFetchTransaction(res.id) + }) + .then(tx => { + console.log('Is Bob the owner?', tx.outputs[0].public_keys[0] === bob.publicKey) + console.log('Was Alice the previous owner?', tx.inputs[0].owners_before[0] === alice.publicKey) + }) + +// ======== Search Asset by Serial Number ======== // + .then(() => conn.searchAssets('Bicycle Inc.')) + .then(assets => console.log('Found assets with serial number Bicycle Inc.:', assets)) From 1aa295830de54654b48f25c355d47a80623ba8c6 Mon Sep 17 00:00:00 2001 From: Michiel Mulders Date: Sat, 20 Jan 2018 14:45:57 +0100 Subject: [PATCH 2/8] First example v2 --- examples/basic-usage-boilerplate/src/app.js | 48 ++++++--------------- 1 file changed, 14 insertions(+), 34 deletions(-) diff --git a/examples/basic-usage-boilerplate/src/app.js b/examples/basic-usage-boilerplate/src/app.js index f5c3543..35630c8 100644 --- a/examples/basic-usage-boilerplate/src/app.js +++ b/examples/basic-usage-boilerplate/src/app.js @@ -1,5 +1,5 @@ // const driver = require('../../../src/index') -import * as driver from 'bigchaindb-driver' +const driver = require('bigchaindb-driver') // ======== Preparation ======== // const conn = new driver.Connection('https://test.bigchaindb.com/api/v1/', { @@ -23,59 +23,39 @@ const metadata = { 'planet': 'earth' } const txCreateAliceSimple = driver.Transaction.makeCreateTransaction( assetdata, metadata, - [driver.Transaction.makeOutput( - driver.Transaction.makeEd25519Condition(alice.publicKey)) + [ + driver.Transaction.makeOutput(driver.Transaction.makeEd25519Condition(alice.publicKey)) ], alice.publicKey ) const txCreateAliceSimpleSigned = driver.Transaction.signTransaction(txCreateAliceSimple, alice.privateKey) -console.log(txCreateAliceSimpleSigned) // ======== Post Transaction and Fetch Result ======== // conn.postTransaction(txCreateAliceSimpleSigned) // Check status of transaction every 0.5 seconds until fulfilled .then(() => conn.pollStatusAndFetchTransaction(txCreateAliceSimpleSigned.id)) - .then(retrievedTx => { - console.log('Transaction', retrievedTx.id, 'successfully posted.') - return retrievedTx - }) - - // Check status after transaction has completed (result: { 'status': 'valid' }) - // If you check the status of a transaction before it's added to BigchainDB, - // BigchainDb will return that the transaction is still waiting in the 'backlog' - .then(() => conn.getStatus(txCreateAliceSimpleSigned.id)) - .then(status => console.log('Retrieved status method 2: ', status)) // ======== Transfer Bicycle to Bob ======== // .then(() => { - try { - console.log(txCreateAliceSimpleSigned) - const txTransferBob = driver.Transaction.makeTransferTransaction( - [{ tx: txCreateAliceSimpleSigned, output_index: 0 }], - [driver.Transaction.makeOutput(driver.Transaction.makeEd25519Condition(bob.publicKey))], - { price: '100 euro' } - ) + const txTransferBob = driver.Transaction.makeTransferTransaction( + [{ tx: txCreateAliceSimpleSigned, output_index: 0 }], + [driver.Transaction.makeOutput(driver.Transaction.makeEd25519Condition(bob.publicKey))], + { price: '100 euro' } + ) - // Sign transfer transaction with Alice's private key - const txTransferBobSigned = driver.Transaction.signTransaction(txTransferBob, alice.privateKey) - console.log('Posting signed transaction: ', txTransferBobSigned) - } catch (err) { - console.log(err) - } + // Sign transfer transaction with Alice's private key + const txTransferBobSigned = driver.Transaction.signTransaction(txTransferBob, alice.privateKey) return conn.postTransaction(txTransferBobSigned) }) - .then(res => { - console.log('Response from BDB server:', res) - return conn.pollStatusAndFetchTransaction(res.id) - }) + .then(res => conn.pollStatusAndFetchTransaction(res.id)) .then(tx => { - console.log('Is Bob the owner?', tx.outputs[0].public_keys[0] === bob.publicKey) - console.log('Was Alice the previous owner?', tx.inputs[0].owners_before[0] === alice.publicKey) + 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 }) // ======== Search Asset by Serial Number ======== // .then(() => conn.searchAssets('Bicycle Inc.')) - .then(assets => console.log('Found assets with serial number Bicycle Inc.:', assets)) + .then(assets => console.log('Found assets with serial number Bicycle Inc.:', assets)) // eslint-disable-line no-console From 10fab5323a9ab2d3516718ea38e17e6bd5508077 Mon Sep 17 00:00:00 2001 From: Michiel Mulders Date: Sat, 20 Jan 2018 15:22:07 +0100 Subject: [PATCH 3/8] Small fix query-all-example --- examples/basic-usage-boilerplate/package.json | 9 ++-- .../src/{app.js => basic-usage.js} | 6 ++- .../src/query-assets.js | 50 +++++++++++++++++++ 3 files changed, 60 insertions(+), 5 deletions(-) rename examples/basic-usage-boilerplate/src/{app.js => basic-usage.js} (97%) create mode 100644 examples/basic-usage-boilerplate/src/query-assets.js diff --git a/examples/basic-usage-boilerplate/package.json b/examples/basic-usage-boilerplate/package.json index ed39c5f..a309435 100644 --- a/examples/basic-usage-boilerplate/package.json +++ b/examples/basic-usage-boilerplate/package.json @@ -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", diff --git a/examples/basic-usage-boilerplate/src/app.js b/examples/basic-usage-boilerplate/src/basic-usage.js similarity index 97% rename from examples/basic-usage-boilerplate/src/app.js rename to examples/basic-usage-boilerplate/src/basic-usage.js index 35630c8..ea5528e 100644 --- a/examples/basic-usage-boilerplate/src/app.js +++ b/examples/basic-usage-boilerplate/src/basic-usage.js @@ -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 diff --git a/examples/basic-usage-boilerplate/src/query-assets.js b/examples/basic-usage-boilerplate/src/query-assets.js new file mode 100644 index 0000000..2195ad8 --- /dev/null +++ b/examples/basic-usage-boilerplate/src/query-assets.js @@ -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 From 01deae78cdaa4d211988c6358f6925561eada772 Mon Sep 17 00:00:00 2001 From: Michiel Mulders Date: Sat, 20 Jan 2018 16:41:05 +0100 Subject: [PATCH 4/8] example seed-func ready --- examples/basic-usage-boilerplate/README.md | 10 +++++- examples/basic-usage-boilerplate/package.json | 7 ++-- .../basic-usage-boilerplate/src/seed-func.js | 34 +++++++++++++++++++ 3 files changed, 48 insertions(+), 3 deletions(-) create mode 100644 examples/basic-usage-boilerplate/src/seed-func.js diff --git a/examples/basic-usage-boilerplate/README.md b/examples/basic-usage-boilerplate/README.md index 55f98e5..21be56d 100644 --- a/examples/basic-usage-boilerplate/README.md +++ b/examples/basic-usage-boilerplate/README.md @@ -1,2 +1,10 @@ # Usage -`npm install` \ No newline at end of file +`npm install` -> Installs all required dependencies to run these examples. + +## Different Examples +**Basic Usage**: Create asset and transfer it to new owner. +-> `npm start` + +**Querying for Assets**: Query for assetdata or metadata. +-> `npm run query-assets` + diff --git a/examples/basic-usage-boilerplate/package.json b/examples/basic-usage-boilerplate/package.json index a309435..a3576c8 100644 --- a/examples/basic-usage-boilerplate/package.json +++ b/examples/basic-usage-boilerplate/package.json @@ -7,7 +7,9 @@ "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" + "query-assets": "nodemon src/query-assets.js --exec babel-node", + "seed-func": "nodemon src/seed-func.js --exec babel-node", + "websocket": "nodemon src/websocket.js --exec babel-node" }, "author": "BigchainDB", "license": "MIT", @@ -27,6 +29,7 @@ "repository": "/", "private": true, "dependencies": { - "bigchaindb-driver": "^3.2.0" + "bigchaindb-driver": "^3.2.0", + "bip39": "^2.5.0" } } diff --git a/examples/basic-usage-boilerplate/src/seed-func.js b/examples/basic-usage-boilerplate/src/seed-func.js new file mode 100644 index 0000000..a8b89fb --- /dev/null +++ b/examples/basic-usage-boilerplate/src/seed-func.js @@ -0,0 +1,34 @@ +import bip39 from 'bip39' + +const driver = require('bigchaindb-driver') + +// ======== Create Keypair ======== // +/** + * Use a passphrase to derive a keypair + * If you use the same seed -> you will derive the same keypair + * + * mnemnoicToSeed() transforms the passphrase you gave as an input + * to a byteArray + * + * BigchainDB however only accepts an input length of 32 characters + * so we have to slice this to give it as input for driver.Ed25519Keypair() + * + * Is it safe to slice? Yes, a seed of length 32 is very safe according + * to related papers discussing this. + */ +const passphrase = 'This is a random passphrase' +const seed = bip39.mnemonicToSeed(passphrase).slice(0, 32) + +const keypair = new driver.Ed25519Keypair(seed) + +console.log(`Public Key: ${keypair.publicKey} - Private Key: ${keypair.privateKey}`) // eslint-disable-line no-console + +// ======== Other Bip39 Functionality not related to BigchainDB ======== // + +/* Create Random passphrase */ +const mnemonic = bip39.generateMnemonic() +console.log('Random passphrase: ', mnemonic) // eslint-disable-line no-console + +/* Validate mnemnoic */ +console.log(bip39.validateMnemonic(mnemonic)) // eslint-disable-line no-console +console.log(bip39.validateMnemonic('some random strings together but to short')) // eslint-disable-line no-console From 87f36946f07b69c3b16da6745cc46824277a83d8 Mon Sep 17 00:00:00 2001 From: Michiel Mulders Date: Sat, 20 Jan 2018 21:38:59 +0100 Subject: [PATCH 5/8] Basic usage async example --- examples/basic-usage-boilerplate/package.json | 1 + .../src/basic-usage-async-await.js | 61 +++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 examples/basic-usage-boilerplate/src/basic-usage-async-await.js diff --git a/examples/basic-usage-boilerplate/package.json b/examples/basic-usage-boilerplate/package.json index a3576c8..7a52d95 100644 --- a/examples/basic-usage-boilerplate/package.json +++ b/examples/basic-usage-boilerplate/package.json @@ -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", diff --git a/examples/basic-usage-boilerplate/src/basic-usage-async-await.js b/examples/basic-usage-boilerplate/src/basic-usage-async-await.js new file mode 100644 index 0000000..b07c5d4 --- /dev/null +++ b/examples/basic-usage-boilerplate/src/basic-usage-async-await.js @@ -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 +} From 0a7e207fef02013f93f543b9f1a84d3cbb488c2d Mon Sep 17 00:00:00 2001 From: Michiel Mulders Date: Sat, 20 Jan 2018 22:19:23 +0100 Subject: [PATCH 6/8] Finished examples - experimenting with websocket --- examples/basic-usage-boilerplate/README.md | 16 ++++++++++++++++ examples/basic-usage-boilerplate/package.json | 6 +++--- .../src/basic-usage-async-await.js | 5 +++-- .../basic-usage-boilerplate/src/basic-usage.js | 5 +++-- .../basic-usage-boilerplate/src/query-assets.js | 5 +++-- 5 files changed, 28 insertions(+), 9 deletions(-) diff --git a/examples/basic-usage-boilerplate/README.md b/examples/basic-usage-boilerplate/README.md index 21be56d..5c9f194 100644 --- a/examples/basic-usage-boilerplate/README.md +++ b/examples/basic-usage-boilerplate/README.md @@ -5,6 +5,22 @@ **Basic Usage**: Create asset and transfer it to new owner. -> `npm start` +**Async/Await Basic Usage**: Basic usage example rewritten with async/await. +-> `npm run basic-async` + **Querying for Assets**: Query for assetdata or metadata. -> `npm run query-assets` +**Seed/Keypair Functionality**: Create keypair with bip39 library. +-> `npm run seed-func` + +## Notes +`dotenv` is listed as a dependencies in `package.json`. +If you want to use this, add a `.env` file to the root of this project (same level as this `README.md` file) +and replace the variables to fit your system. + +``` +BIGCHAINDB_API_PATH=http://localhost:9984/api/v1/ +BIGCHAINDB_APP_ID= +BIGCHAINDB_APP_KEY= +``` \ No newline at end of file diff --git a/examples/basic-usage-boilerplate/package.json b/examples/basic-usage-boilerplate/package.json index 7a52d95..5a31f94 100644 --- a/examples/basic-usage-boilerplate/package.json +++ b/examples/basic-usage-boilerplate/package.json @@ -9,8 +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" + "basic-async": "nodemon src/basic-usage-async-await.js --exec babel-node" }, "author": "BigchainDB", "license": "MIT", @@ -31,6 +30,7 @@ "private": true, "dependencies": { "bigchaindb-driver": "^3.2.0", - "bip39": "^2.5.0" + "bip39": "^2.5.0", + "dotenv": "^4.0.0" } } diff --git a/examples/basic-usage-boilerplate/src/basic-usage-async-await.js b/examples/basic-usage-boilerplate/src/basic-usage-async-await.js index b07c5d4..b1e18bc 100644 --- a/examples/basic-usage-boilerplate/src/basic-usage-async-await.js +++ b/examples/basic-usage-boilerplate/src/basic-usage-async-await.js @@ -1,10 +1,11 @@ const driver = require('bigchaindb-driver') +require('dotenv').config() // ======== Preparation ======== // const conn = new driver.Connection('https://test.bigchaindb.com/api/v1/', { - app_id: 'c17a9968', - app_key: '0b277b94893e7b0a5b4e6afd6bccb01d' + app_id: process.env.BIGCHAINDB_APP_ID, + app_key: process.env.BIGCHAINDB_APP_KEY }) const alice = new driver.Ed25519Keypair() diff --git a/examples/basic-usage-boilerplate/src/basic-usage.js b/examples/basic-usage-boilerplate/src/basic-usage.js index ea5528e..811c4b6 100644 --- a/examples/basic-usage-boilerplate/src/basic-usage.js +++ b/examples/basic-usage-boilerplate/src/basic-usage.js @@ -1,10 +1,11 @@ const driver = require('bigchaindb-driver') +require('dotenv').config() // ======== Preparation ======== // const conn = new driver.Connection('https://test.bigchaindb.com/api/v1/', { - app_id: 'c17a9968', - app_key: '0b277b94893e7b0a5b4e6afd6bccb01d' + app_id: process.env.BIGCHAINDB_APP_ID, + app_key: process.env.BIGCHAINDB_APP_KEY }) const alice = new driver.Ed25519Keypair() diff --git a/examples/basic-usage-boilerplate/src/query-assets.js b/examples/basic-usage-boilerplate/src/query-assets.js index 2195ad8..b60b5ce 100644 --- a/examples/basic-usage-boilerplate/src/query-assets.js +++ b/examples/basic-usage-boilerplate/src/query-assets.js @@ -1,10 +1,11 @@ const driver = require('bigchaindb-driver') +require('dotenv').config() // ======== Preparation ======== // const conn = new driver.Connection('https://test.bigchaindb.com/api/v1/', { - app_id: 'c17a9968', - app_key: '0b277b94893e7b0a5b4e6afd6bccb01d' + app_id: process.env.BIGCHAINDB_APP_ID, + app_key: process.env.BIGCHAINDB_APP_KEY }) const alice = new driver.Ed25519Keypair() From 745ddf9527dda5605a7ef580f7a12a527b770ba8 Mon Sep 17 00:00:00 2001 From: Michiel Mulders Date: Sat, 20 Jan 2018 22:50:17 +0100 Subject: [PATCH 7/8] Update structure eslint test --- .../{basic-usage-boilerplate => }/.babelrc | 0 .../{basic-usage-boilerplate => }/.gitignore | 0 .../{basic-usage-boilerplate => }/README.md | 0 examples/basic-usage/index.js | 71 ------------------- .../package.json | 0 .../src/basic-usage-async-await.js | 2 +- .../src/basic-usage.js | 2 +- .../src/query-assets.js | 2 +- .../src/seed-func.js | 4 +- 9 files changed, 5 insertions(+), 76 deletions(-) rename examples/{basic-usage-boilerplate => }/.babelrc (100%) rename examples/{basic-usage-boilerplate => }/.gitignore (100%) rename examples/{basic-usage-boilerplate => }/README.md (100%) delete mode 100644 examples/basic-usage/index.js rename examples/{basic-usage-boilerplate => }/package.json (100%) rename examples/{basic-usage-boilerplate => }/src/basic-usage-async-await.js (95%) rename examples/{basic-usage-boilerplate => }/src/basic-usage.js (96%) rename examples/{basic-usage-boilerplate => }/src/query-assets.js (95%) rename examples/{basic-usage-boilerplate => }/src/seed-func.js (88%) diff --git a/examples/basic-usage-boilerplate/.babelrc b/examples/.babelrc similarity index 100% rename from examples/basic-usage-boilerplate/.babelrc rename to examples/.babelrc diff --git a/examples/basic-usage-boilerplate/.gitignore b/examples/.gitignore similarity index 100% rename from examples/basic-usage-boilerplate/.gitignore rename to examples/.gitignore diff --git a/examples/basic-usage-boilerplate/README.md b/examples/README.md similarity index 100% rename from examples/basic-usage-boilerplate/README.md rename to examples/README.md diff --git a/examples/basic-usage/index.js b/examples/basic-usage/index.js deleted file mode 100644 index c6d4013..0000000 --- a/examples/basic-usage/index.js +++ /dev/null @@ -1,71 +0,0 @@ -const driver = require('../../src/index') - -// ======== 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, - [driver.Transaction.makeOutput( - driver.Transaction.makeEd25519Condition(alice.publicKey)) - ], - 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)) - .then(retrievedTx => console.log('Transaction', retrievedTx.id, 'successfully posted.')) - - // Check status after transaction has completed (result: { 'status': 'valid' }) - // If you check the status of a transaction before it's added to BigchainDB, - // BigchainDb will return that the transaction is still waiting in the 'backlog' - .then(() => conn.getStatus(txCreateAliceSimpleSigned.id)) - .then(status => console.log('Retrieved status method 2: ', status)) - -// ======== Transfer Bicycle to Bob ======== // - .then(() => { - const txTransferBob = driver.Transaction.makeTransferTransaction( - [{ tx: txCreateAliceSimpleSigned, output_index: 0 }], - [driver.Transaction.makeOutput(driver.Transaction.makeEd25519Condition(bob.publicKey))], - { price: '100 euro' } - ) - - // Sign transfer transaction with Alice's private key - const txTransferBobSigned = driver.Transaction.signTransaction(txTransferBob, alice.privateKey) - console.log('Posting signed transaction: ', txTransferBobSigned) - - return conn.postTransaction(txTransferBobSigned) - }) - .then(res => { - console.log('Response from BDB server:', res) - return conn.pollStatusAndFetchTransaction(res.id) - }) - .then(tx => { - console.log('Is Bob the owner?', tx.outputs[0].public_keys[0] === bob.publicKey) - console.log('Was Alice the previous owner?', tx.inputs[0].owners_before[0] === alice.publicKey) - }) - -// ======== Search Asset by Serial Number ======== // - .then(() => conn.searchAssets('Bicycle Inc.')) - .then(assets => console.log('Found assets with serial number Bicycle Inc.:', assets)) diff --git a/examples/basic-usage-boilerplate/package.json b/examples/package.json similarity index 100% rename from examples/basic-usage-boilerplate/package.json rename to examples/package.json diff --git a/examples/basic-usage-boilerplate/src/basic-usage-async-await.js b/examples/src/basic-usage-async-await.js similarity index 95% rename from examples/basic-usage-boilerplate/src/basic-usage-async-await.js rename to examples/src/basic-usage-async-await.js index b1e18bc..3d8e640 100644 --- a/examples/basic-usage-boilerplate/src/basic-usage-async-await.js +++ b/examples/src/basic-usage-async-await.js @@ -1,4 +1,4 @@ -const driver = require('bigchaindb-driver') +const driver = require('bigchaindb-driver') // eslint-disable-line import/no-unresolved require('dotenv').config() diff --git a/examples/basic-usage-boilerplate/src/basic-usage.js b/examples/src/basic-usage.js similarity index 96% rename from examples/basic-usage-boilerplate/src/basic-usage.js rename to examples/src/basic-usage.js index 811c4b6..77cd25e 100644 --- a/examples/basic-usage-boilerplate/src/basic-usage.js +++ b/examples/src/basic-usage.js @@ -1,4 +1,4 @@ -const driver = require('bigchaindb-driver') +const driver = require('bigchaindb-driver') // eslint-disable-line import/no-unresolved require('dotenv').config() diff --git a/examples/basic-usage-boilerplate/src/query-assets.js b/examples/src/query-assets.js similarity index 95% rename from examples/basic-usage-boilerplate/src/query-assets.js rename to examples/src/query-assets.js index b60b5ce..cad31dd 100644 --- a/examples/basic-usage-boilerplate/src/query-assets.js +++ b/examples/src/query-assets.js @@ -1,4 +1,4 @@ -const driver = require('bigchaindb-driver') +const driver = require('bigchaindb-driver') // eslint-disable-line import/no-unresolved require('dotenv').config() diff --git a/examples/basic-usage-boilerplate/src/seed-func.js b/examples/src/seed-func.js similarity index 88% rename from examples/basic-usage-boilerplate/src/seed-func.js rename to examples/src/seed-func.js index a8b89fb..7d85cd5 100644 --- a/examples/basic-usage-boilerplate/src/seed-func.js +++ b/examples/src/seed-func.js @@ -1,6 +1,6 @@ -import bip39 from 'bip39' +import bip39 from 'bip39' // eslint-disable-line import/no-unresolved -const driver = require('bigchaindb-driver') +const driver = require('bigchaindb-driver') // eslint-disable-line import/no-unresolved // ======== Create Keypair ======== // /** From c7b55e5ad330ab6e7ed46523a8057136585c2ff8 Mon Sep 17 00:00:00 2001 From: Michiel Mulders Date: Tue, 23 Jan 2018 17:12:19 +0100 Subject: [PATCH 8/8] Fix note to top of readme --- .vscode/settings.json | 3 +++ examples/README.md | 22 +++++++++++----------- 2 files changed, 14 insertions(+), 11 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..e8c013f --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "restructuredtext.confPath": "/home/michiel/projects/bigchaindb-driver/js-bigchaindb-driver" +} \ No newline at end of file diff --git a/examples/README.md b/examples/README.md index 5c9f194..efa3767 100644 --- a/examples/README.md +++ b/examples/README.md @@ -1,3 +1,14 @@ +# Quick Notes +`dotenv` is listed as a dependencies in `package.json`. +If you want to use this, add a `.env` file to the root of this project (same level as this `README.md` file) +and replace the variables to fit your specific config. + +``` +BIGCHAINDB_API_PATH=http://localhost:9984/api/v1/ +BIGCHAINDB_APP_ID= +BIGCHAINDB_APP_KEY= +``` + # Usage `npm install` -> Installs all required dependencies to run these examples. @@ -13,14 +24,3 @@ **Seed/Keypair Functionality**: Create keypair with bip39 library. -> `npm run seed-func` - -## Notes -`dotenv` is listed as a dependencies in `package.json`. -If you want to use this, add a `.env` file to the root of this project (same level as this `README.md` file) -and replace the variables to fit your system. - -``` -BIGCHAINDB_API_PATH=http://localhost:9984/api/v1/ -BIGCHAINDB_APP_ID= -BIGCHAINDB_APP_KEY= -``` \ No newline at end of file