From 07e6584cf40156d20c8a9f9b43097d589195105a Mon Sep 17 00:00:00 2001 From: Scott Sadler Date: Tue, 20 Jun 2017 10:45:12 +0200 Subject: [PATCH 1/6] cc2 ed25519 condition creation --- package.json | 2 +- src/transaction/makeEd25519Condition.js | 2 +- test/test.js | 15 +++++++++++++++ 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 43ca2c6..061cc31 100644 --- a/package.json +++ b/package.json @@ -59,7 +59,7 @@ "decamelize": "^1.2.0", "es6-promise": "^4.0.5", "fetch-ponyfill": "^4.0.0", - "five-bells-condition": "3.3.1", + "five-bells-condition": "^5.0.1", "isomorphic-fetch": "^2.2.1", "js-sha3": "^0.5.7", "js-utility-belt": "^1.5.0", diff --git a/src/transaction/makeEd25519Condition.js b/src/transaction/makeEd25519Condition.js index 1bdd0e3..3f0ff57 100644 --- a/src/transaction/makeEd25519Condition.js +++ b/src/transaction/makeEd25519Condition.js @@ -16,7 +16,7 @@ import ccJsonify from './utils/ccJsonify' export default function makeEd25519Condition(publicKey, json = true) { const publicKeyBuffer = new Buffer(base58.decode(publicKey)) - const ed25519Fulfillment = new cc.Ed25519() + const ed25519Fulfillment = new cc.Ed25519Sha256() ed25519Fulfillment.setPublicKey(publicKeyBuffer) if (json) { diff --git a/test/test.js b/test/test.js index 5a1e216..d6d6ccc 100644 --- a/test/test.js +++ b/test/test.js @@ -28,3 +28,18 @@ test('Valid CREATE transaction is evaluated by BigchainDB', t => { .then(resTx => t.truthy(resTx)) }) +test('Ed25519 condition correctly formed', t => { + const publicKey = '4zvwRjXUKGfvwnParsHAS3HuSVzV5cA4McphgmoCtajS' + const output = Transaction.makeOutput(Transaction.makeEd25519Condition(publicKey)) + const target = { + details: { + type_id: 4, + bitmask: 32, + signature: null, + public_key: publicKey, + type: 'fulfillment' + }, + uri: 'ni:///sha-256;uLdVX7FEjLWVDkAkfMAkEqPPwFqZj7qfiGE152t_x5c?fpt=ed25519-sha-256&cost=131072' + } + t.deepEqual(target, output.condition) +}) From 35944d6d750e2199070bf66384bb4063ca37004f Mon Sep 17 00:00:00 2001 From: Scott Sadler Date: Tue, 20 Jun 2017 11:35:25 +0200 Subject: [PATCH 2/6] cc2 threshold condition creation --- src/transaction/utils/ccJsonify.js | 9 ++------- test/test.js | 30 +++++++++++++++++++++++------- 2 files changed, 25 insertions(+), 14 deletions(-) diff --git a/src/transaction/utils/ccJsonify.js b/src/transaction/utils/ccJsonify.js index c80ec24..a33d0b6 100644 --- a/src/transaction/utils/ccJsonify.js +++ b/src/transaction/utils/ccJsonify.js @@ -33,13 +33,10 @@ export default function ccJsonify(fulfillment) { if (fulfillment.getTypeId() === 2) { return { 'details': { - 'type_id': 2, - 'type': 'fulfillment', - 'bitmask': fulfillment.getBitmask(), + 'type': 'threshold-sha-256', 'threshold': fulfillment.threshold, 'subfulfillments': fulfillment.subconditions.map((subcondition) => { const subconditionJson = ccJsonify(subcondition.body) - subconditionJson.details.weight = 1 return subconditionJson.details }) }, @@ -48,13 +45,11 @@ export default function ccJsonify(fulfillment) { } if (fulfillment.getTypeId() === 4) { - jsonBody.details.type_id = 4 - jsonBody.details.bitmask = 32 + jsonBody.details.type = 'ed25519-sha-256' if ('publicKey' in fulfillment) { jsonBody.details.signature = null jsonBody.details.public_key = base58.encode(fulfillment.publicKey) - jsonBody.details.type = 'fulfillment' } } diff --git a/test/test.js b/test/test.js index d6d6ccc..48b2fa6 100644 --- a/test/test.js +++ b/test/test.js @@ -28,18 +28,34 @@ test('Valid CREATE transaction is evaluated by BigchainDB', t => { .then(resTx => t.truthy(resTx)) }) -test('Ed25519 condition correctly formed', t => { +test('Ed25519 condition encoding', t => { const publicKey = '4zvwRjXUKGfvwnParsHAS3HuSVzV5cA4McphgmoCtajS' - const output = Transaction.makeOutput(Transaction.makeEd25519Condition(publicKey)) const target = { details: { - type_id: 4, - bitmask: 32, - signature: null, + type: 'ed25519-sha-256', public_key: publicKey, - type: 'fulfillment' + signature: null, }, uri: 'ni:///sha-256;uLdVX7FEjLWVDkAkfMAkEqPPwFqZj7qfiGE152t_x5c?fpt=ed25519-sha-256&cost=131072' } - t.deepEqual(target, output.condition) + t.deepEqual(target, Transaction.makeEd25519Condition(publicKey)) +}) + +test('Threshold condition encoding', t => { + const publicKey = '4zvwRjXUKGfvwnParsHAS3HuSVzV5cA4McphgmoCtajS' + const condition = Transaction.makeThresholdCondition( + 1, [Transaction.makeEd25519Condition(publicKey, false)]) + const target = { + details: { + type: 'threshold-sha-256', + threshold: 1, + subfulfillments: [{ + type: 'ed25519-sha-256', + public_key: publicKey, + signature: null, + }] + }, + uri: 'ni:///sha-256;VBIfZSoBprUQy-LVNAzaZ2q-eyWbrcPKtBg1PuNXIpQ?fpt=threshold-sha-256&cost=132096&subtypes=ed25519-sha-256' + } + t.deepEqual(target, condition) }) From ce523e703fa5eba3bad42696541d305863d78404 Mon Sep 17 00:00:00 2001 From: Scott Sadler Date: Wed, 21 Jun 2017 09:24:39 +0200 Subject: [PATCH 3/6] load condition details and test transfer and sign --- src/transaction/signTransaction.js | 2 +- src/transaction/utils/ccJsonLoad.js | 11 +++-------- test/test.js | 27 +++++++++++++++++++++++++++ 3 files changed, 31 insertions(+), 9 deletions(-) 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))) +}) From 9e423dd2591aa2b40a8a61d4edb6ae0178959def Mon Sep 17 00:00:00 2001 From: Scott Sadler Date: Thu, 22 Jun 2017 15:09:36 +0200 Subject: [PATCH 4/6] work in progress --- test/{test.js => test2.js} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename test/{test.js => test2.js} (100%) diff --git a/test/test.js b/test/test2.js similarity index 100% rename from test/test.js rename to test/test2.js From 70681a3085de6f31758ba5083b84f74d20aa93ed Mon Sep 17 00:00:00 2001 From: Scott Sadler Date: Thu, 22 Jun 2017 16:13:59 +0200 Subject: [PATCH 5/6] tests for cryptoconditions --- .../test_cryptoconditions.js} | 35 ++----------------- 1 file changed, 3 insertions(+), 32 deletions(-) rename test/{test2.js => transaction/test_cryptoconditions.js} (69%) diff --git a/test/test2.js b/test/transaction/test_cryptoconditions.js similarity index 69% rename from test/test2.js rename to test/transaction/test_cryptoconditions.js index 230bdcd..a60b87c 100644 --- a/test/test2.js +++ b/test/transaction/test_cryptoconditions.js @@ -1,38 +1,7 @@ import test from 'ava' import cc from 'five-bells-condition' -import { Ed25519Keypair, Transaction, Connection } from '../src' +import { Ed25519Keypair, Transaction } from '../../src' -const API_PATH = 'http://localhost:9984/api/v1/' - -test('Keypair is created', t => { - const keyPair = new Ed25519Keypair() - - t.truthy(keyPair.publicKey) - t.truthy(keyPair.privateKey) -}) - -test('Valid CREATE transaction is evaluated by BigchainDB', t => { - const alice = new Ed25519Keypair() - const asset = { name: 'Shmui', type: 'cat' } - const metadata = { dayOfTheWeek: 'Caturday' } - - const tx = Transaction.makeCreateTransaction( - asset, - metadata, - [Transaction.makeOutput(Transaction.makeEd25519Condition(alice.publicKey))], - alice.publicKey - ) - - const txSigned = Transaction.signTransaction(tx, alice.privateKey) - const conn = new Connection(API_PATH) - return conn.postTransaction(txSigned) - .then(resTx => t.truthy(resTx)) -}) - - -/* - * CryptoConditions support tests - */ test('Ed25519 condition encoding', t => { const publicKey = '4zvwRjXUKGfvwnParsHAS3HuSVzV5cA4McphgmoCtajS' @@ -47,6 +16,7 @@ test('Ed25519 condition encoding', t => { t.deepEqual(target, Transaction.makeEd25519Condition(publicKey)) }) + test('Threshold condition encoding', t => { const publicKey = '4zvwRjXUKGfvwnParsHAS3HuSVzV5cA4McphgmoCtajS' const condition = Transaction.makeThresholdCondition( @@ -66,6 +36,7 @@ test('Threshold condition encoding', t => { t.deepEqual(target, condition) }) + test('Fulfillment correctly formed', t => { const alice = new Ed25519Keypair() const txCreate = Transaction.makeCreateTransaction( From 46a05b14d394fce77aa908f3c549d79d9e0ce325 Mon Sep 17 00:00:00 2001 From: Scott Sadler Date: Thu, 22 Jun 2017 16:17:43 +0200 Subject: [PATCH 6/6] bump transaction version to 1.0 --- src/transaction/makeTransaction.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/transaction/makeTransaction.js b/src/transaction/makeTransaction.js index bcd118d..00c8ce2 100644 --- a/src/transaction/makeTransaction.js +++ b/src/transaction/makeTransaction.js @@ -9,7 +9,7 @@ function makeTransactionTemplate() { 'inputs': [], 'metadata': null, 'asset': null, - 'version': '0.9', + 'version': '1.0', } }