From 6fa2b2e7e5771dabc4981425360b283caccfd2ef Mon Sep 17 00:00:00 2001 From: Scott Sadler Date: Fri, 23 Jun 2017 10:24:23 +0200 Subject: [PATCH] encode public_keys array for threshold conditions --- src/transaction/makeOutput.js | 14 ++++++++++--- test/transaction/test_cryptoconditions.js | 25 ++++++++++++++--------- 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/src/transaction/makeOutput.js b/src/transaction/makeOutput.js index 820f3c3..3e48895 100644 --- a/src/transaction/makeOutput.js +++ b/src/transaction/makeOutput.js @@ -10,10 +10,18 @@ export default function makeOutput(condition, amount = '1') { if (typeof amount !== '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 { - 'amount': amount, condition, - 'public_keys': condition.details.hasOwnProperty('public_key') ? - [condition.details.public_key] : [], + 'amount': amount, + 'public_keys': publicKeys, } } diff --git a/test/transaction/test_cryptoconditions.js b/test/transaction/test_cryptoconditions.js index a60b87c..34288e9 100644 --- a/test/transaction/test_cryptoconditions.js +++ b/test/transaction/test_cryptoconditions.js @@ -21,19 +21,24 @@ test('Threshold condition encoding', t => { const publicKey = '4zvwRjXUKGfvwnParsHAS3HuSVzV5cA4McphgmoCtajS' const condition = Transaction.makeThresholdCondition( 1, [Transaction.makeEd25519Condition(publicKey, false)]) + const output = Transaction.makeOutput(condition) const target = { - details: { - type: 'threshold-sha-256', - threshold: 1, - subfulfillments: [{ - type: 'ed25519-sha-256', - public_key: publicKey, - signature: null, - }] + condition: { + 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', }, - 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) })