Skip to content

Instantly share code, notes, and snippets.

@michielmulders
Created March 28, 2018 07:49
Show Gist options
  • Save michielmulders/f99abb800eb966235d1499d8042bdd87 to your computer and use it in GitHub Desktop.
Save michielmulders/f99abb800eb966235d1499d8042bdd87 to your computer and use it in GitHub Desktop.
BigchainDB crypto conditions
import { Router } from "express";
import logger from "logops";
import * as driver from "bigchaindb-driver";
import conn from "../helpers/bigchaindb";
const base58 = require("bs58");
const cryptoconditions = require("crypto-conditions");
const router = Router();
export default router;
/* eslint:disable */
/**
* Transfer asset from creator to new owner where two out of three random users have to sign
*/
/*eslint-disable */
const testCondition = async (req, res) => {
logger.info("Request from %s:", "Michiel");
const user1 = new driver.Ed25519Keypair();
const user2 = new driver.Ed25519Keypair();
const user3 = new driver.Ed25519Keypair();
const creator = new driver.Ed25519Keypair();
const receiver = new driver.Ed25519Keypair();
// console.log(driver.Transaction.makeEd25519Condition(user1.publicKey));
// at the output of the transaction to-be-spent
// Generate threshold condition 2 out of 3
const threshold = 2;
const condition1 = driver.Transaction.makeEd25519Condition(
user1.publicKey,
false
);
const condition2 = driver.Transaction.makeEd25519Condition(
user2.publicKey,
false
);
const condition3 = driver.Transaction.makeEd25519Condition(
user3.publicKey,
false
);
const passwordCondition = new cryptoconditions.PreimageSha256();
passwordCondition.setPreimage(new Buffer('mysecret'));
logger.info(JSON.stringify(passwordCondition.getConditionUri()));
const thresholdCondition = driver.Transaction.makeThresholdCondition(
threshold,
[condition1, condition2, condition3, passwordCondition.getConditionUri()]
);
console.log(JSON.stringify(thresholdCondition));
let output = driver.Transaction.makeOutput(thresholdCondition);
output.public_keys = [user1.publicKey, user2.publicKey, user3.publicKey];
const assetdata = {
bicycle: {
serial_number: "abcd1234",
manufacturer: "Bicycle Inc."
}
};
const metadata = { planet: "earth" };
const tx = driver.Transaction.makeCreateTransaction(
assetdata,
metadata,
[output],
creator.publicKey
);
// Sign the transaction with private keys
const txSigned = driver.Transaction.signTransaction(tx, creator.privateKey);
// ======== Post Transaction and Fetch Result ======== //
await conn.postTransaction(txSigned);
const postedCreateTx = await conn.pollStatusAndFetchTransaction(txSigned.id);
console.log(postedCreateTx);
// ======== Transfer Transaction ======== //
let createTranfer = driver.Transaction.makeTransferTransaction(
[
{
tx: txSigned,
output_index: 0
}
],
[
driver.Transaction.makeOutput(
driver.Transaction.makeEd25519Condition(receiver.publicKey)
)
],
{
what: "Transfer transaction"
}
);
// at the input of the spending transaction
let fulfillment1 = driver.Transaction.makeEd25519Condition(
user1.publicKey,
false
);
let fulfillment2 = driver.Transaction.makeEd25519Condition(
user2.publicKey,
false
);
fulfillment1.sign(
new Buffer(
driver.Transaction.serializeTransactionIntoCanonicalString(createTranfer)
),
new Buffer(base58.decode(user1.privateKey))
);
fulfillment2.sign(
new Buffer(
driver.Transaction.serializeTransactionIntoCanonicalString(createTranfer)
),
new Buffer(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
let fulfillment = new cryptoconditions.ThresholdSha256();
fulfillment.threshold = 2;
fulfillment.addSubfulfillment(fulfillment1.serializeUri());
fulfillment.addSubfulfillment(fulfillment2.serializeUri());
fulfillment.addSubconditionUri(condition3.getConditionUri());
//Sign the transaction
const fulfillmentUri = fulfillment.serializeUri();
createTranfer.inputs[0].fulfillment = fulfillmentUri;
await conn.postTransaction(createTranfer);
const transferTxResult = await conn.pollStatusAndFetchTransaction(
createTranfer.id
);
console.log(JSON.stringify(transferTxResult));
res.send(JSON.stringify(transferTxResult));
};
router.route("/test").get(testCondition);
@pdmendki
Copy link

pdmendki commented May 8, 2018

Is this working with the latest version of bigchaindb v2.0.0a3 ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment