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))