diff --git a/src/transaction/signTransaction.js b/src/transaction/signTransaction.js index 4faa888..32422a7 100644 --- a/src/transaction/signTransaction.js +++ b/src/transaction/signTransaction.js @@ -24,7 +24,7 @@ export default function signTransaction(transaction, ...privateKeys) { const privateKey = privateKeys[index] const privateKeyBuffer = new Buffer(base58.decode(privateKey)) const serializedTransaction = serializeTransactionIntoCanonicalString(transaction) - const ed25519Fulfillment = new cc.Ed25519() + const ed25519Fulfillment = new cc.Ed25519Sha256() ed25519Fulfillment.sign(new Buffer(serializedTransaction), privateKeyBuffer) const fulfillmentUri = ed25519Fulfillment.serializeUri() diff --git a/src/transaction/utils/ccJsonLoad.js b/src/transaction/utils/ccJsonLoad.js index 1a3ac7c..5e9a02f 100644 --- a/src/transaction/utils/ccJsonLoad.js +++ b/src/transaction/utils/ccJsonLoad.js @@ -19,7 +19,7 @@ export default function ccJsonLoad(conditionJson) { } else { let fulfillment - if (conditionJson.type_id === 2) { + if (conditionJson.type === 'threshold-sha-256') { fulfillment = new cc.ThresholdSha256() fulfillment.threshold = conditionJson.threshold conditionJson.subfulfillments.forEach((subfulfillmentJson) => { @@ -32,13 +32,8 @@ export default function ccJsonLoad(conditionJson) { }) } - if (conditionJson.type_id === 0) { - fulfillment = new cc.PreimageSha256() - fulfillment.preimage = new Buffer(conditionJson.preimage) - } - - if (conditionJson.type_id === 4) { - fulfillment = new cc.Ed25519() + if (conditionJson.type === 'ed25519-sha-256') { + fulfillment = new cc.Ed25519Sha256() fulfillment.publicKey = new Buffer(base58.decode(conditionJson.public_key)) if (conditionJson.signature) { fulfillment.signature = new Buffer(base58.decode(conditionJson.signature)) diff --git a/test/test.js b/test/test.js index 48b2fa6..230bdcd 100644 --- a/test/test.js +++ b/test/test.js @@ -1,4 +1,5 @@ import test from 'ava' +import cc from 'five-bells-condition' import { Ed25519Keypair, Transaction, Connection } from '../src' const API_PATH = 'http://localhost:9984/api/v1/' @@ -28,6 +29,11 @@ test('Valid CREATE transaction is evaluated by BigchainDB', t => { .then(resTx => t.truthy(resTx)) }) + +/* + * CryptoConditions support tests + */ + test('Ed25519 condition encoding', t => { const publicKey = '4zvwRjXUKGfvwnParsHAS3HuSVzV5cA4McphgmoCtajS' const target = { @@ -59,3 +65,24 @@ test('Threshold condition encoding', t => { } t.deepEqual(target, condition) }) + +test('Fulfillment correctly formed', t => { + const alice = new Ed25519Keypair() + const txCreate = Transaction.makeCreateTransaction( + {}, + {}, + [Transaction.makeOutput(Transaction.makeEd25519Condition(alice.publicKey))], + alice.publicKey + ) + const txTransfer = Transaction.makeTransferTransaction( + txCreate, + {}, + [Transaction.makeOutput(Transaction.makeEd25519Condition(alice.publicKey))], + [0] + ) + const msg = Transaction.serializeTransactionIntoCanonicalString(txTransfer) + const txSigned = Transaction.signTransaction(txTransfer, alice.privateKey) + t.truthy(cc.validateFulfillment(txSigned.inputs[0].fulfillment, + txCreate.outputs[0].condition.uri, + new Buffer(msg))) +})