mirror of
https://github.com/bigchaindb/js-bigchaindb-driver.git
synced 2025-01-03 18:35:13 +01:00
Merge pull request #51 from bigchaindb/cc2
Integrate cryptoconditions 2
This commit is contained in:
commit
cf53b9da46
@ -66,7 +66,7 @@
|
|||||||
"decamelize": "^1.2.0",
|
"decamelize": "^1.2.0",
|
||||||
"es6-promise": "^4.0.5",
|
"es6-promise": "^4.0.5",
|
||||||
"fetch-ponyfill": "^4.0.0",
|
"fetch-ponyfill": "^4.0.0",
|
||||||
"five-bells-condition": "3.3.1",
|
"five-bells-condition": "^5.0.1",
|
||||||
"isomorphic-fetch": "^2.2.1",
|
"isomorphic-fetch": "^2.2.1",
|
||||||
"js-sha3": "^0.5.7",
|
"js-sha3": "^0.5.7",
|
||||||
"js-utility-belt": "^1.5.0",
|
"js-utility-belt": "^1.5.0",
|
||||||
|
@ -16,7 +16,7 @@ import ccJsonify from './utils/ccJsonify'
|
|||||||
export default function makeEd25519Condition(publicKey, json = true) {
|
export default function makeEd25519Condition(publicKey, json = true) {
|
||||||
const publicKeyBuffer = new Buffer(base58.decode(publicKey))
|
const publicKeyBuffer = new Buffer(base58.decode(publicKey))
|
||||||
|
|
||||||
const ed25519Fulfillment = new cc.Ed25519()
|
const ed25519Fulfillment = new cc.Ed25519Sha256()
|
||||||
ed25519Fulfillment.setPublicKey(publicKeyBuffer)
|
ed25519Fulfillment.setPublicKey(publicKeyBuffer)
|
||||||
|
|
||||||
if (json) {
|
if (json) {
|
||||||
|
@ -9,7 +9,7 @@ function makeTransactionTemplate() {
|
|||||||
'inputs': [],
|
'inputs': [],
|
||||||
'metadata': null,
|
'metadata': null,
|
||||||
'asset': null,
|
'asset': null,
|
||||||
'version': '0.9',
|
'version': '1.0',
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -24,7 +24,7 @@ export default function signTransaction(transaction, ...privateKeys) {
|
|||||||
const privateKey = privateKeys[index]
|
const privateKey = privateKeys[index]
|
||||||
const privateKeyBuffer = new Buffer(base58.decode(privateKey))
|
const privateKeyBuffer = new Buffer(base58.decode(privateKey))
|
||||||
const serializedTransaction = serializeTransactionIntoCanonicalString(transaction)
|
const serializedTransaction = serializeTransactionIntoCanonicalString(transaction)
|
||||||
const ed25519Fulfillment = new cc.Ed25519()
|
const ed25519Fulfillment = new cc.Ed25519Sha256()
|
||||||
ed25519Fulfillment.sign(new Buffer(serializedTransaction), privateKeyBuffer)
|
ed25519Fulfillment.sign(new Buffer(serializedTransaction), privateKeyBuffer)
|
||||||
const fulfillmentUri = ed25519Fulfillment.serializeUri()
|
const fulfillmentUri = ed25519Fulfillment.serializeUri()
|
||||||
|
|
||||||
|
@ -19,7 +19,7 @@ export default function ccJsonLoad(conditionJson) {
|
|||||||
} else {
|
} else {
|
||||||
let fulfillment
|
let fulfillment
|
||||||
|
|
||||||
if (conditionJson.type_id === 2) {
|
if (conditionJson.type === 'threshold-sha-256') {
|
||||||
fulfillment = new cc.ThresholdSha256()
|
fulfillment = new cc.ThresholdSha256()
|
||||||
fulfillment.threshold = conditionJson.threshold
|
fulfillment.threshold = conditionJson.threshold
|
||||||
conditionJson.subfulfillments.forEach((subfulfillmentJson) => {
|
conditionJson.subfulfillments.forEach((subfulfillmentJson) => {
|
||||||
@ -32,13 +32,8 @@ export default function ccJsonLoad(conditionJson) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
if (conditionJson.type_id === 0) {
|
if (conditionJson.type === 'ed25519-sha-256') {
|
||||||
fulfillment = new cc.PreimageSha256()
|
fulfillment = new cc.Ed25519Sha256()
|
||||||
fulfillment.preimage = new Buffer(conditionJson.preimage)
|
|
||||||
}
|
|
||||||
|
|
||||||
if (conditionJson.type_id === 4) {
|
|
||||||
fulfillment = new cc.Ed25519()
|
|
||||||
fulfillment.publicKey = new Buffer(base58.decode(conditionJson.public_key))
|
fulfillment.publicKey = new Buffer(base58.decode(conditionJson.public_key))
|
||||||
if (conditionJson.signature) {
|
if (conditionJson.signature) {
|
||||||
fulfillment.signature = new Buffer(base58.decode(conditionJson.signature))
|
fulfillment.signature = new Buffer(base58.decode(conditionJson.signature))
|
||||||
|
@ -33,13 +33,10 @@ export default function ccJsonify(fulfillment) {
|
|||||||
if (fulfillment.getTypeId() === 2) {
|
if (fulfillment.getTypeId() === 2) {
|
||||||
return {
|
return {
|
||||||
'details': {
|
'details': {
|
||||||
'type_id': 2,
|
'type': 'threshold-sha-256',
|
||||||
'type': 'fulfillment',
|
|
||||||
'bitmask': fulfillment.getBitmask(),
|
|
||||||
'threshold': fulfillment.threshold,
|
'threshold': fulfillment.threshold,
|
||||||
'subfulfillments': fulfillment.subconditions.map((subcondition) => {
|
'subfulfillments': fulfillment.subconditions.map((subcondition) => {
|
||||||
const subconditionJson = ccJsonify(subcondition.body)
|
const subconditionJson = ccJsonify(subcondition.body)
|
||||||
subconditionJson.details.weight = 1
|
|
||||||
return subconditionJson.details
|
return subconditionJson.details
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
@ -48,13 +45,11 @@ export default function ccJsonify(fulfillment) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (fulfillment.getTypeId() === 4) {
|
if (fulfillment.getTypeId() === 4) {
|
||||||
jsonBody.details.type_id = 4
|
jsonBody.details.type = 'ed25519-sha-256'
|
||||||
jsonBody.details.bitmask = 32
|
|
||||||
|
|
||||||
if ('publicKey' in fulfillment) {
|
if ('publicKey' in fulfillment) {
|
||||||
jsonBody.details.signature = null
|
jsonBody.details.signature = null
|
||||||
jsonBody.details.public_key = base58.encode(fulfillment.publicKey)
|
jsonBody.details.public_key = base58.encode(fulfillment.publicKey)
|
||||||
jsonBody.details.type = 'fulfillment'
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
59
test/transaction/test_cryptoconditions.js
Normal file
59
test/transaction/test_cryptoconditions.js
Normal file
@ -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)))
|
||||||
|
})
|
Loading…
Reference in New Issue
Block a user