mirror of
https://github.com/bigchaindb/js-bigchaindb-driver.git
synced 2025-01-03 18:35:13 +01:00
67 lines
2.3 KiB
JavaScript
67 lines
2.3 KiB
JavaScript
// Copyright BigchainDB GmbH and BigchainDB contributors
|
|
// SPDX-License-Identifier: (Apache-2.0 AND CC-BY-4.0)
|
|
// Code is Apache-2.0 and docs are CC-BY-4.0
|
|
|
|
/* eslint-disable import/no-unresolved */
|
|
|
|
const driver = require('bigchaindb-driver')
|
|
require('dotenv').config()
|
|
|
|
|
|
// ======== Preparation ======== //
|
|
const conn = new driver.Connection('https://test.bigchaindb.com/api/v1/', {
|
|
app_id: process.env.BIGCHAINDB_APP_ID,
|
|
app_key: process.env.BIGCHAINDB_APP_KEY
|
|
})
|
|
|
|
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.postTransactionCommit(txCreateAliceSimpleSigned)
|
|
// ======== Transfer Bicycle to Bob ======== //
|
|
.then((fetchedTx) => {
|
|
const txTransferBob = driver.Transaction.makeTransferTransaction(
|
|
[{ tx: fetchedTx, 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)
|
|
|
|
return conn.postTransactionCommit(txTransferBobSigned)
|
|
})
|
|
.then(tx => {
|
|
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)) // eslint-disable-line no-console
|