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

Issue #44 Multiple inputs for transfer transaction

This commit is contained in:
Rashad Ibrahimov 2017-09-19 23:47:33 +04:00 committed by tim
parent a8250d632c
commit e0f11ec415
5 changed files with 35 additions and 49 deletions

View File

@ -5,45 +5,41 @@ import makeTransaction from './makeTransaction'
/**
* @public
* Generate a `TRANSFER` transaction holding the `asset`, `metadata`, and `outputs`, that fulfills
* the `fulfilledOutputs` of `unspentTransaction`.
* @param {object} unspentTransaction Previous Transaction you have control over (i.e. can fulfill
* its Output Condition)
* @param {object} metadata Metadata for the Transaction
* the `fulfilledOutputs` of each `unspentTransaction`.
* @param {object[]} unspentOutputs Array of unspent Transactions' Outputs.
* Each item contains Transaction itself
* and index of unspent Output for that Transaction.
* @param {object[]} outputs Array of Output objects to add to the Transaction.
* Think of these as the recipients of the asset after the transaction.
* For `TRANSFER` Transactions, this should usually just be a list of
* Outputs wrapping Ed25519 Conditions generated from the public keys of
* the recipients.
* @param {...number} OutputIndices Indices of the Outputs in `unspentTransaction` that this
* Transaction fulfills.
* Note that listed public keys listed must be used (and in
* the same order) to sign the Transaction
* (`signTransaction()`).
* @param {object} metadata Metadata for the Transaction - optional
* @returns {object} Unsigned transaction -- make sure to call signTransaction() on it before
* sending it off!
*/
// TODO:
// - Make `metadata` optional argument
export default function makeTransferTransaction(
unspentTransaction,
metadata,
unspentOutputs,
outputs,
...outputIndices
metadata
) {
const inputs = outputIndices.map((outputIndex) => {
const fulfilledOutput = unspentTransaction.outputs[outputIndex]
const inputs = unspentOutputs.map((unspentOutput) => {
const { tx, output_index } = unspentOutput
const fulfilledOutput = tx.outputs[output_index]
const transactionLink = {
'output_index': outputIndex,
'transaction_id': unspentTransaction.id,
'output_index': output_index,
'transaction_id': tx.id,
}
return makeInputTemplate(fulfilledOutput.public_keys, transactionLink)
})
const assetLink = {
'id': unspentTransaction.operation === 'CREATE' ? unspentTransaction.id
: unspentTransaction.asset.id
'id': unspentOutputs[0].tx.operation === 'CREATE' ? unspentOutputs[0].tx.id
: unspentOutputs[0].tx.asset.id
}
return makeTransaction('TRANSFER', assetLink, metadata, outputs, inputs)
const meta = metadata || null
return makeTransaction('TRANSFER', assetLink, meta, outputs, inputs)
}

View File

@ -17,10 +17,9 @@ export const createTx = Transaction.makeCreateTransaction(
alice.publicKey
)
export const transferTx = Transaction.makeTransferTransaction(
createTx,
metaData,
[{ tx: createTx, output_index: 0 }],
[aliceOutput],
0
metaData
)
export const bob = new Ed25519Keypair()

View File

@ -59,10 +59,9 @@ test('Valid TRANSFER transaction with single Ed25519 input', t => {
.then(({ id }) => conn.pollStatusAndFetchTransaction(id))
.then(() => {
const transferTx = Transaction.makeTransferTransaction(
createTxSigned,
metaData,
[{ tx: createTxSigned, output_index: 0 }],
[aliceOutput],
0
metaData
)
const transferTxSigned = Transaction.signTransaction(
transferTx,
@ -92,11 +91,9 @@ test('Valid TRANSFER transaction with multiple Ed25519 inputs', t => {
.then(({ 'id': txId }) => conn.pollStatusAndFetchTransaction(txId))
.then(() => {
const transferTx = Transaction.makeTransferTransaction(
createTxSigned,
metaData,
[{ tx: createTxSigned, output_index: 0 }, { tx: createTxSigned, output_index: 1 }],
[Transaction.makeOutput(aliceCondition, '2')],
0,
1
metaData
)
const transferTxSigned = Transaction.signTransaction(
transferTx,
@ -134,10 +131,9 @@ test('Search for spent and unspent outputs of a given public key', t => {
// We spent output 1 (of 0, 1)
const transferTx = Transaction.makeTransferTransaction(
createTxSigned,
metaData,
[{ tx: createTxSigned, output_index: 1 }],
[trentOutput],
1
metaData
)
const transferTxSigned = Transaction.signTransaction(
transferTx,
@ -177,10 +173,9 @@ test('Search for unspent outputs for a given public key', t => {
// We spent output 1 (of 0, 1, 2)
const transferTx = Transaction.makeTransferTransaction(
createTxSigned,
metaData,
[{ tx: createTxSigned, output_index: 1 }],
[trentOutput],
1
metaData
)
const transferTxSigned = Transaction.signTransaction(
transferTx,
@ -220,10 +215,9 @@ test('Search for spent outputs for a given public key', t => {
// We spent output 1 (of 0, 1, 2)
const transferTx = Transaction.makeTransferTransaction(
createTxSigned,
metaData,
[{ tx: createTxSigned, output_index: 1 }],
[trentOutput],
1
metaData
)
const transferTxSigned = Transaction.signTransaction(
transferTx,

View File

@ -56,10 +56,9 @@ test('Fulfillment correctly formed', t => {
alice.publicKey
)
const txTransfer = Transaction.makeTransferTransaction(
txCreate,
{},
[{ tx: txCreate, output_index: 0 }],
[Transaction.makeOutput(Transaction.makeEd25519Condition(alice.publicKey))],
[0]
{}
)
const msg = Transaction.serializeTransactionIntoCanonicalString(txTransfer)
const txSigned = Transaction.signTransaction(txTransfer, alice.privateKey)

View File

@ -73,10 +73,9 @@ test('Create TRANSFER transaction based on CREATE transaction', t => {
sinon.spy(makeTransaction, 'default')
Transaction.makeTransferTransaction(
createTx,
metaData,
[{ tx: createTx, output_index: 0 }],
[aliceOutput],
0
metaData
)
const expected = [
'TRANSFER',
@ -101,10 +100,9 @@ test('Create TRANSFER transaction based on TRANSFER transaction', t => {
sinon.spy(makeTransaction, 'default')
Transaction.makeTransferTransaction(
transferTx,
metaData,
[{ tx: transferTx, output_index: 0 }],
[aliceOutput],
0
metaData
)
const expected = [
'TRANSFER',