Created
October 26, 2017 13:19
-
-
Save theblockstalk/9fa3cce017ff8122b53e85c07c60258b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const bigchain = require('bigchaindb-driver') | |
const API_PATH = 'http://localhost:9984/api/v1/' | |
const bigchainConnection = new bigchain.Connection(API_PATH) | |
var aliceKey = new bigchain.Ed25519Keypair() | |
var bobKey = new bigchain.Ed25519Keypair() | |
// Condition that BOTH Alice AND Bob are needed to fulfill this output | |
var subConditionUser = bigchain.Transaction.makeEd25519Condition(aliceKey.publicKey, false) | |
var subConditionIndorse = bigchain.Transaction.makeEd25519Condition(bobKey.publicKey, false) | |
var condition = bigchain.Transaction.makeThresholdCondition(2, [subConditionUser, subConditionIndorse]) | |
var outputs = [ bigchain.Transaction.makeOutput(condition), ] | |
var issuers = aliceKey.publicKey | |
var txCreateAssetThreshold = bigchain.Transaction.makeCreateTransaction( | |
{ myAsset: 'some asset' }, | |
{ message: 'This create transaction can only be transfered if both Alice and Bob allow it' }, | |
outputs, | |
issuers | |
) | |
var txCreateAssetThresholdSigned = bigchain.Transaction.signTransaction( | |
txCreateAssetThreshold, | |
aliceKey.privateKey | |
) | |
console.log('========================txCreateAssetThresholdSigned') | |
console.log(txCreateAssetThresholdSigned) | |
bigchainConnection.postTransaction(txCreateAssetThresholdSigned) | |
.then(() => bigchainConnection.pollStatusAndFetchTransaction(txCreateAssetThresholdSigned.id)) | |
.then(() => bigchainConnection.getStatus(txCreateAssetThresholdSigned.id)) | |
.then((status) => { | |
if (status.status == 'valid') { | |
return | |
} | |
}).then(() => { | |
// We can successfully get to this point in the program | |
// Create a transfer transaction referrencing the Threshold condition asset | |
var txTransferAssetSimple = bigchain.Transaction.makeTransferTransaction( | |
txCreateAssetThresholdSigned, | |
{ message: 'This should create a transaction that can only be transfered by Bob'}, | |
[bigchain.Transaction.makeEd25519Condition(bobKey.publicKey), ], | |
0 | |
) | |
// Fullfil the output conditions by signing it as Bob and Alice (both are required) | |
console.log('========================txTransferAssetSimple') | |
console.log(txTransferAssetSimple) | |
var txTransferAssetSimpleSignedByAlice = bigchain.Transaction.signTransaction( | |
txTransferAssetSimple, | |
aliceKey.privateKey | |
) | |
console.log('========================txTransferAssetSimpleSignedByAlice') | |
console.log(txTransferAssetSimpleSignedByAlice) | |
var txTransferAssetSimpleSignedByAliceAndBob = bigchain.Transaction.signTransaction( | |
txTransferAssetSimpleSignedByAlice, | |
bobKey.privateKey | |
) | |
console.log('========================txTransferAssetSimpleSignedByAliceAndBob') | |
console.log(txTransferAssetSimpleSignedByAliceAndBob) | |
// NOTE: the following will not work... | |
// var txTransferAssetSimpleSignedDouble = bigchain.Transaction.signTransaction( | |
// txTransferAssetSimple, | |
// [aliceKey.privateKey, bobKey.privateKey] | |
// ) | |
// console.log('========================txTransferAssetSimpleSignedDouble') | |
// console.log(txTransferAssetSimpleSignedDouble) | |
// NOTE: This call will fail. | |
// Server responds with code 400 'BAD REQUEST' | |
bigchainConnection.postTransaction(txTransferAssetSimpleSigned) | |
.then(() => bigchainConnection.pollStatusAndFetchTransaction(txTransferAssetSimpleSigned.id)) | |
.then(() => bigchainConnection.getStatus(txTransferAssetSimpleSigned.id)) | |
.then((status) => { | |
if (status.status == 'valid') { | |
console.log('Transaction successfully posted to bigchain') | |
} | |
}) | |
}).catch((err) => console.log(err)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment