js-bigchaindb-driver/test/transaction/test_cryptoconditions.js

86 lines
2.6 KiB
JavaScript
Raw Normal View History

import test from 'ava'
import cc from 'js-crypto-conditions'
2017-06-22 16:13:59 +02:00
import { Ed25519Keypair, Transaction } from '../../src'
2017-06-08 14:09:23 +02:00
2017-06-20 11:35:25 +02:00
test('Ed25519 condition encoding', t => {
2017-06-20 10:45:12 +02:00
const publicKey = '4zvwRjXUKGfvwnParsHAS3HuSVzV5cA4McphgmoCtajS'
const target = {
details: {
2017-06-20 11:35:25 +02:00
type: 'ed25519-sha-256',
2017-06-20 10:45:12 +02:00
public_key: publicKey,
},
uri: 'ni:///sha-256;uLdVX7FEjLWVDkAkfMAkEqPPwFqZj7qfiGE152t_x5c?fpt=ed25519-sha-256&cost=131072'
}
2017-06-20 11:35:25 +02:00
t.deepEqual(target, Transaction.makeEd25519Condition(publicKey))
})
2017-06-22 16:13:59 +02:00
2017-06-20 11:35:25 +02:00
test('Threshold condition encoding', t => {
const publicKey = '4zvwRjXUKGfvwnParsHAS3HuSVzV5cA4McphgmoCtajS'
2017-06-23 10:30:08 +02:00
const ed25519 = Transaction.makeEd25519Condition(publicKey, false)
2017-06-20 11:35:25 +02:00
const condition = Transaction.makeThresholdCondition(
2017-07-05 17:13:05 +02:00
1, [ed25519, ed25519])
const output = Transaction.makeOutput(condition)
2017-06-20 11:35:25 +02:00
const target = {
condition: {
details: {
type: 'threshold-sha-256',
threshold: 1,
subconditions: [
2017-06-23 10:30:08 +02:00
{
type: 'ed25519-sha-256',
public_key: publicKey,
},
{
type: 'ed25519-sha-256',
public_key: publicKey,
}
]
},
2017-06-23 10:30:08 +02:00
uri: 'ni:///sha-256;xTeBhQj7ae5Tym7cp83fwtkesQnhdwNwDEMIYwnf2g0?fpt=threshold-sha-256&cost=133120&subtypes=ed25519-sha-256',
2017-06-20 11:35:25 +02:00
},
amount: '1',
public_keys: [publicKey]
2017-06-20 11:35:25 +02:00
}
t.deepEqual(target, output)
2017-06-20 10:45:12 +02:00
})
2017-06-22 16:13:59 +02:00
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,
2017-07-05 17:13:05 +02:00
txCreate.outputs[0].condition.uri,
new Buffer(msg)))
})
2017-07-05 15:09:33 +02:00
test('CryptoConditions JSON load', t => {
const cond = Transaction.ccJsonLoad({
type: 'threshold-sha-256',
threshold: 1,
subconditions: [{
type: 'ed25519-sha-256',
public_key: 'a'
},
{
hash: 'a'
2017-07-05 15:09:33 +02:00
}],
})
t.truthy(cond.subconditions.length === 2)
2017-07-05 15:09:33 +02:00
})