Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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' }
)
createTransaction(publicKey, privateKey, payload, metadata) {
try {
// Create a transation
const tx = driver.Transaction.makeCreateTransaction(
payload,
metadata,
[
driver.Transaction.makeOutput(driver.Transaction.makeEd25519Condition(publicKey))
],
publicKey
)
// sign/fulfill the transaction
const txSigned = driver.Transaction.signTransaction(tx, privateKey)
return this.conn.postTransactionCommit(txSigned).then(() => txSigned)
} catch (error) {
return Promise.reject(error)
}
}
function sendToBigchainDB(asset, keypair, count, nTx) {
const txSimpleAsset = BigchainDB.Transaction.makeCreateTransaction(
asset,
{
'metadata': 'sdfs'
},
// counterparty is the owner
[BigchainDB.Transaction.makeOutput(
BigchainDB.Transaction.makeEd25519Condition(keypair.publicKey))],
keypair.publicKey
)
// Sign the transaction with private keys
const txSigned = BigchainDB.Transaction.signTransaction(txSimpleAsset, keypair.privateKey)
// console.log(txSigned.id)
// console.log(sizeof(txSigned))
//console.log(JSON.stringify(txSigned))
function sendToBigchainDB(asset, keypair, count, nTx) {
const txSimpleAsset = BigchainDB.Transaction.makeCreateTransaction(
asset,
{
'metadata': 'sdfs'
},
// counterparty is the owner
[BigchainDB.Transaction.makeOutput(
BigchainDB.Transaction.makeEd25519Condition(keypair.publicKey))],
keypair.publicKey
)
// Sign the transaction with private keys
const txSigned = BigchainDB.Transaction.signTransaction(txSimpleAsset, keypair.privateKey)
// console.log(txSigned.id)
// console.log(sizeof(txSigned))
//console.log(JSON.stringify(txSigned))
conn.postTransaction(txSigned)
}
transferTransaction(tx, fromPublicKey, fromPrivateKey, toPublicKey, metadata) {
try {
const txTransfer = driver.Transaction.makeTransferTransaction(
[{ 'tx': tx, 'output_index': 0 }],
[driver.Transaction.makeOutput(driver.Transaction.makeEd25519Condition(toPublicKey))],
metadata,
)
const txTransferSigned = driver.Transaction.signTransaction(txTransfer, fromPrivateKey)
// send it off to BigchainDB
return this.conn.postTransactionCommit(txTransferSigned).then(() => txTransferSigned)
} catch (error) {
return Promise.reject(error)
}
}
async function sendToBigchainDB(asset, keypair) {
const txSimpleAsset = BigchainDB.Transaction.makeCreateTransaction(
asset,
{
'metadata': 'sdfs'
},
// counterparty is the owner
[BigchainDB.Transaction.makeOutput(
BigchainDB.Transaction.makeEd25519Condition(keypair.publicKey))],
keypair.publicKey
)
// Sign the transaction with private keys
const txSigned = BigchainDB.Transaction.signTransaction(txSimpleAsset, keypair.privateKey)
console.log('txSigned ', txSigned.id)
console.log('sizeof ', sizeof(txSigned))
//console.log(JSON.stringify(txSigned))
conn.postTransaction(txSigned)
}
function sendToBigchainDB(asset, keypair, count, nTx) {
const txSimpleAsset = BigchainDB.Transaction.makeCreateTransaction(
asset,
{
'metadata': 'sdfs'
},
// counterparty is the owner
[BigchainDB.Transaction.makeOutput(
BigchainDB.Transaction.makeEd25519Condition(keypair.publicKey))],
keypair.publicKey
)
// Sign the transaction with private keys
const txSigned = BigchainDB.Transaction.signTransaction(txSimpleAsset, keypair.privateKey)
// console.log(txSigned.id)
// console.log(sizeof(txSigned))
//console.log(JSON.stringify(txSigned))
conn.postTransaction(txSigned)
}
async function transfer(txSigned) {
let createTranfer = BigchainDB.Transaction.makeTransferTransaction(
[{ tx: txSigned,
output_index: 0
}],
[BigchainDB.Transaction.makeOutput(
BigchainDB.Transaction.makeEd25519Condition(receiver.publicKey))],
{
what: "Transfer transaction"
}
);
const serializedTransaction = BigchainDB.Transaction.serializeTransactionIntoCanonicalString(createTranfer)
// at the input of the spending transaction
let fulfillment1 = BigchainDB.Transaction.makeEd25519Condition(user1.publicKey, false)
const transactionUniqueFulfillment1 = createTranfer.inputs[0].fulfills ? serializedTransaction
.concat(createTranfer.inputs[0].fulfills.transaction_id)
.concat(createTranfer.inputs[0].fulfills.output_index) : serializedTransaction
const transactionHash1 = sha256Hash(transactionUniqueFulfillment1)
fulfillment1.sign(Buffer.from(transactionHash1, 'hex'), new Buffer.from(base58.decode(user1.privateKey)))
let fulfillment2 = BigchainDB.Transaction.makeEd25519Condition(user2.publicKey, false)
const transactionUniqueFulfillment2 = createTranfer.inputs[0].fulfills ? serializedTransaction
.concat(createTranfer.inputs[0].fulfills.transaction_id)
.concat(createTranfer.inputs[0].fulfills.output_index) : serializedTransaction
const transactionHash2 = sha256Hash(transactionUniqueFulfillment2)
fulfillment2.sign(Buffer.from(transactionHash2, 'hex'), new Buffer.from(base58.decode(user2.privateKey)))
// 2 out of 3 need to sign the fulfillment. Still condition3 is needed as the "circuit definition" is needed.
// See https://github.com/bigchaindb/cryptoconditions/issues/94
if (outputs.length > 0) {
for (const output of outputs) {
let condition = driver.Transaction.makeEd25519Condition(output.publicKey)
let transferOutput
if (output.amount > 0) {
transferOutput = driver.Transaction.makeOutput(condition, output.amount.toString())
} else {
transferOutput = driver.Transaction.makeOutput(condition)
}
transferOutput.public_keys = [output.publicKey]
transferOutputs.push(transferOutput)
}
}
const txTransfer = driver.Transaction.makeTransferTransaction(
unspentTxs,
transferOutputs,
metadata
)
const txSigned = driver.Transaction.signTransaction(txTransfer, keypair.privateKey)
return await conn.postTransactionCommit(txSigned)
}
async function createNewAsset(keypair, asset, metadata) {
let condition = driver.Transaction.makeEd25519Condition(keypair.publicKey, true)
let output = driver.Transaction.makeOutput(condition)
output.public_keys = [keypair.publicKey]
const transaction = driver.Transaction.makeCreateTransaction(
asset,
metadata,
[output],
keypair.publicKey
)
const txSigned = driver.Transaction.signTransaction(transaction, keypair.privateKey)
let tx
await conn.postTransaction(txSigned)
.then(() => conn.pollStatusAndFetchTransaction(txSigned.id))
.then(retrievedTx => {
tx = retrievedTx
})