1
0
mirror of https://github.com/bigchaindb/js-bigchaindb-driver.git synced 2024-11-22 09:46:58 +01:00

fulfills.txid => fulfills.transaction_id + tests

This commit is contained in:
tim 2017-06-19 13:27:39 +02:00
parent a14dbbaebd
commit b8241589df
3 changed files with 102 additions and 17 deletions

View File

@ -21,6 +21,8 @@ import makeTransaction from './makeTransaction'
* @returns {object} Unsigned transaction -- make sure to call signTransaction() on it before
* sending it off!
*/
// TODO: `outputs` should throw or include output in array if no array was
// passed
export default function makeCreateTransaction(asset, metadata, outputs, ...issuers) {
const assetDefinition = {
'data': asset || null,

View File

@ -2,6 +2,32 @@ import makeInputTemplate from './makeInputTemplate'
import makeTransaction from './makeTransaction'
// TODO: Can we remove `export` here somehow, but still be able to import the
// function for tests?
export function _makeTransferTransaction(
unspentTransaction,
metadata,
outputs,
...fulfilledOutputs
) {
const inputs = fulfilledOutputs.map((outputIndex) => {
const fulfilledOutput = unspentTransaction.outputs[outputIndex]
const transactionLink = {
'output': outputIndex,
'transaction_id': unspentTransaction.id,
}
return makeInputTemplate(fulfilledOutput.public_keys, transactionLink)
})
const assetLink = {
'id': unspentTransaction.operation === 'CREATE' ? unspentTransaction.id
: unspentTransaction.asset.id
}
return ['TRANSFER', assetLink, metadata, outputs, inputs]
}
/**
* @public
* Generate a `TRANSFER` transaction holding the `asset`, `metadata`, and `outputs`, that fulfills
@ -22,26 +48,17 @@ import makeTransaction from './makeTransaction'
* @returns {object} Unsigned transaction -- make sure to call signTransaction() on it before
* sending it off!
*/
// TODO:
// - Make `metadata` optional argument
// - Rename `fulfilledOutputs`, e.g. inputs
// TODO: `outputs` should throw or include output in array if no array was
// passed
export default function makeTransferTransaction(
unspentTransaction,
metadata,
outputs,
...fulfilledOutputs
) {
const inputs = fulfilledOutputs.map((outputIndex) => {
const fulfilledOutput = unspentTransaction.outputs[outputIndex]
const transactionLink = {
'output': outputIndex,
'transaction_id': unspentTransaction.id,
}
return makeInputTemplate(fulfilledOutput.public_keys, transactionLink)
})
const assetLink = {
'id': unspentTransaction.operation === 'CREATE' ? unspentTransaction.id
: unspentTransaction.asset.id
}
return makeTransaction('TRANSFER', assetLink, metadata, outputs, inputs)
return makeTransaction(..._makeTransferTransaction(...arguments))
}

View File

@ -1,5 +1,27 @@
import test from 'ava'
import { Transaction } from '../../src'
import { Transaction, Ed25519Keypair } from '../../src'
import { _makeTransferTransaction } from '../../src/transaction/makeTransferTransaction'
import makeInputTemplate from '../../src/transaction/makeInputTemplate'
// TODO: Find out if ava has something like conftest, if so put this there.
const alice = new Ed25519Keypair()
const aliceCondition = Transaction.makeEd25519Condition(alice.publicKey)
const aliceOutput = Transaction.makeOutput(aliceCondition)
const assetMessage = { assetMessage: 'assetMessage' }
const metaDataMessage = { metaDataMessage: 'metaDataMessage' }
const createTx = Transaction.makeCreateTransaction(
assetMessage,
metaDataMessage,
[aliceOutput],
alice.publicKey
)
const transferTx = Transaction.makeTransferTransaction(
createTx,
metaDataMessage,
[aliceOutput],
0
)
test('Create valid output with default amount', t => {
@ -53,3 +75,47 @@ test('Pass condition not based on public_keys to makeOutput', t => {
test('makeOutput throws TypeError with incorrect amount type', t => {
t.throws(() => Transaction.makeOutput({}, 1337), TypeError)
})
test('Create TRANSFER transaction based on CREATE transaction', t => {
const testTx = _makeTransferTransaction(
createTx,
metaDataMessage,
[aliceOutput],
0
)
const expected = [
'TRANSFER',
{id: createTx.id },
metaDataMessage,
[aliceOutput],
[makeInputTemplate(
[alice.publicKey],
{ output: 0, transaction_id: createTx.id }
)]
]
t.deepEqual(testTx, expected)
})
test('Create TRANSFER transaction based on TRANSFER transaction', t => {
const testTx = _makeTransferTransaction(
transferTx,
metaDataMessage,
[aliceOutput],
0
)
const expected = [
'TRANSFER',
{ id: transferTx.asset.id },
metaDataMessage,
[aliceOutput],
[makeInputTemplate(
[alice.publicKey],
{ output: 0, transaction_id: transferTx.id }
)]
]
t.deepEqual(testTx, expected)
})