1
0
mirror of https://github.com/bigchaindb/js-bigchaindb-driver.git synced 2025-02-14 21:10:32 +01:00

encode public_keys array for threshold conditions

This commit is contained in:
Scott Sadler 2017-06-23 10:24:23 +02:00
parent 52c3505b32
commit 6fa2b2e7e5
2 changed files with 26 additions and 13 deletions

View File

@ -10,10 +10,18 @@ export default function makeOutput(condition, amount = '1') {
if (typeof amount !== 'string') { if (typeof amount !== 'string') {
throw new TypeError('`amount` must be of type string') throw new TypeError('`amount` must be of type string')
} }
const publicKeys = []
const getPublicKeys = details => {
if (details.type === 'ed25519-sha-256') {
publicKeys.push(details.public_key)
} else if (details.type === 'threshold-sha-256') {
details.subfulfillments.map(getPublicKeys)
}
}
getPublicKeys(condition.details)
return { return {
'amount': amount,
condition, condition,
'public_keys': condition.details.hasOwnProperty('public_key') ? 'amount': amount,
[condition.details.public_key] : [], 'public_keys': publicKeys,
} }
} }

View File

@ -21,19 +21,24 @@ test('Threshold condition encoding', t => {
const publicKey = '4zvwRjXUKGfvwnParsHAS3HuSVzV5cA4McphgmoCtajS' const publicKey = '4zvwRjXUKGfvwnParsHAS3HuSVzV5cA4McphgmoCtajS'
const condition = Transaction.makeThresholdCondition( const condition = Transaction.makeThresholdCondition(
1, [Transaction.makeEd25519Condition(publicKey, false)]) 1, [Transaction.makeEd25519Condition(publicKey, false)])
const output = Transaction.makeOutput(condition)
const target = { const target = {
details: { condition: {
type: 'threshold-sha-256', details: {
threshold: 1, type: 'threshold-sha-256',
subfulfillments: [{ threshold: 1,
type: 'ed25519-sha-256', subfulfillments: [{
public_key: publicKey, type: 'ed25519-sha-256',
signature: null, public_key: publicKey,
}] signature: null,
}]
},
uri: 'ni:///sha-256;VBIfZSoBprUQy-LVNAzaZ2q-eyWbrcPKtBg1PuNXIpQ?fpt=threshold-sha-256&cost=132096&subtypes=ed25519-sha-256',
}, },
uri: 'ni:///sha-256;VBIfZSoBprUQy-LVNAzaZ2q-eyWbrcPKtBg1PuNXIpQ?fpt=threshold-sha-256&cost=132096&subtypes=ed25519-sha-256' amount: '1',
public_keys: [publicKey]
} }
t.deepEqual(target, condition) t.deepEqual(target, output)
}) })