diff --git a/package.json b/package.json index 63d53f2..b241b57 100644 --- a/package.json +++ b/package.json @@ -66,7 +66,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/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', } } 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/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/transaction/test_cryptoconditions.js b/test/transaction/test_cryptoconditions.js new file mode 100644 index 0000000..a60b87c --- /dev/null +++ b/test/transaction/test_cryptoconditions.js @@ -0,0 +1,59 @@ +import test from 'ava' +import cc from 'five-bells-condition' +import { Ed25519Keypair, Transaction } from '../../src' + + +test('Ed25519 condition encoding', t => { + const publicKey = '4zvwRjXUKGfvwnParsHAS3HuSVzV5cA4McphgmoCtajS' + const target = { + details: { + type: 'ed25519-sha-256', + public_key: publicKey, + signature: null, + }, + uri: 'ni:///sha-256;uLdVX7FEjLWVDkAkfMAkEqPPwFqZj7qfiGE152t_x5c?fpt=ed25519-sha-256&cost=131072' + } + 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) +}) + + +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))) +})