2018-08-10 12:49:26 +02:00
|
|
|
// Copyright BigchainDB GmbH and BigchainDB contributors
|
|
|
|
// SPDX-License-Identifier: (Apache-2.0 AND CC-BY-4.0)
|
|
|
|
// Code is Apache-2.0 and docs are CC-BY-4.0
|
|
|
|
|
2017-06-16 14:12:02 +02:00
|
|
|
import test from 'ava'
|
2017-06-20 14:55:58 +02:00
|
|
|
import sinon from 'sinon'
|
|
|
|
|
2017-06-20 16:17:43 +02:00
|
|
|
import { Transaction } from '../../src'
|
2017-06-19 13:27:39 +02:00
|
|
|
|
2017-06-19 17:50:09 +02:00
|
|
|
import {
|
|
|
|
alice,
|
|
|
|
aliceOutput,
|
2017-06-20 16:17:43 +02:00
|
|
|
metaData,
|
2017-06-19 13:27:39 +02:00
|
|
|
createTx,
|
2017-06-19 17:50:09 +02:00
|
|
|
transferTx
|
|
|
|
} from '../constants'
|
2017-06-16 14:12:02 +02:00
|
|
|
|
|
|
|
|
|
|
|
test('Create valid output with default amount', t => {
|
|
|
|
const condition = {
|
|
|
|
details: {
|
2017-06-23 10:44:55 +02:00
|
|
|
type: 'ed25519-sha-256',
|
2017-06-16 14:12:02 +02:00
|
|
|
public_key: 'abc'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const expected = {
|
|
|
|
amount: '1',
|
|
|
|
condition,
|
|
|
|
public_keys: ['abc']
|
|
|
|
}
|
|
|
|
const res = Transaction.makeOutput(condition)
|
2017-06-20 13:34:14 +02:00
|
|
|
t.deepEqual(res, expected)
|
2017-06-16 14:12:02 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
test('Create valid output with custom amount', t => {
|
|
|
|
const condition = {
|
|
|
|
details: {
|
2017-06-23 10:44:55 +02:00
|
|
|
type: 'ed25519-sha-256',
|
2017-06-16 14:12:02 +02:00
|
|
|
public_key: 'abc'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const customAmount = '1337'
|
|
|
|
const expected = {
|
|
|
|
amount: customAmount,
|
|
|
|
condition,
|
|
|
|
public_keys: ['abc']
|
|
|
|
}
|
|
|
|
const res = Transaction.makeOutput(condition, customAmount)
|
2017-06-20 13:34:14 +02:00
|
|
|
t.deepEqual(res, expected)
|
2017-06-16 14:12:02 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
test('Pass condition not based on public_keys to makeOutput', t => {
|
|
|
|
const condition = {
|
|
|
|
details: {
|
|
|
|
idea: 'just pretend this is e.g. a hashlock'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const expected = {
|
|
|
|
amount: '1',
|
|
|
|
condition,
|
|
|
|
public_keys: []
|
|
|
|
}
|
|
|
|
const res = Transaction.makeOutput(condition)
|
2017-06-20 13:34:14 +02:00
|
|
|
t.deepEqual(res, expected)
|
2017-06-16 14:12:02 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
test('makeOutput throws TypeError with incorrect amount type', t => {
|
|
|
|
t.throws(() => Transaction.makeOutput({}, 1337), TypeError)
|
|
|
|
})
|
2017-06-19 13:27:39 +02:00
|
|
|
|
|
|
|
|
|
|
|
test('Create TRANSFER transaction based on CREATE transaction', t => {
|
2018-01-17 09:20:04 +01:00
|
|
|
sinon.spy(Transaction, 'makeTransaction')
|
2017-06-20 14:55:58 +02:00
|
|
|
Transaction.makeTransferTransaction(
|
2017-09-19 21:47:33 +02:00
|
|
|
[{ tx: createTx, output_index: 0 }],
|
2017-06-19 13:27:39 +02:00
|
|
|
[aliceOutput],
|
2017-09-19 21:47:33 +02:00
|
|
|
metaData
|
2017-06-19 13:27:39 +02:00
|
|
|
)
|
|
|
|
const expected = [
|
|
|
|
'TRANSFER',
|
2017-06-20 11:03:01 +02:00
|
|
|
{ id: createTx.id },
|
2017-06-20 16:17:43 +02:00
|
|
|
metaData,
|
2017-06-19 13:27:39 +02:00
|
|
|
[aliceOutput],
|
2017-11-02 21:19:19 +01:00
|
|
|
[Transaction.makeInputTemplate(
|
2017-06-19 13:27:39 +02:00
|
|
|
[alice.publicKey],
|
2017-06-29 11:40:29 +02:00
|
|
|
{ output_index: 0, transaction_id: createTx.id }
|
2017-06-19 13:27:39 +02:00
|
|
|
)]
|
|
|
|
]
|
2017-06-20 14:55:58 +02:00
|
|
|
// NOTE: `src/transaction/makeTransaction` is `export default`, hence we
|
|
|
|
// can only mock `makeTransaction.default` with a hack:
|
|
|
|
// See: https://stackoverflow.com/a/33676328/1263876
|
2018-01-15 18:11:16 +01:00
|
|
|
t.truthy(Transaction.makeTransaction.calledWith(...expected))
|
2018-01-17 09:20:04 +01:00
|
|
|
Transaction.makeTransaction.restore()
|
2017-06-19 13:27:39 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
test('Create TRANSFER transaction based on TRANSFER transaction', t => {
|
2018-01-17 09:20:04 +01:00
|
|
|
sinon.spy(Transaction, 'makeTransaction')
|
2017-06-20 14:55:58 +02:00
|
|
|
|
|
|
|
Transaction.makeTransferTransaction(
|
2017-09-19 21:47:33 +02:00
|
|
|
[{ tx: transferTx, output_index: 0 }],
|
2017-06-19 13:27:39 +02:00
|
|
|
[aliceOutput],
|
2017-09-19 21:47:33 +02:00
|
|
|
metaData
|
2017-06-19 13:27:39 +02:00
|
|
|
)
|
|
|
|
const expected = [
|
|
|
|
'TRANSFER',
|
|
|
|
{ id: transferTx.asset.id },
|
2017-06-20 16:17:43 +02:00
|
|
|
metaData,
|
2017-06-19 13:27:39 +02:00
|
|
|
[aliceOutput],
|
2017-11-02 21:19:19 +01:00
|
|
|
[Transaction.makeInputTemplate(
|
2017-06-19 13:27:39 +02:00
|
|
|
[alice.publicKey],
|
2017-06-29 11:40:29 +02:00
|
|
|
{ output_index: 0, transaction_id: transferTx.id }
|
2017-06-19 13:27:39 +02:00
|
|
|
)]
|
|
|
|
]
|
|
|
|
|
2018-01-17 09:20:04 +01:00
|
|
|
t.truthy(Transaction.makeTransaction.calledWith(...expected))
|
|
|
|
Transaction.makeTransaction.restore()
|
2017-06-19 13:27:39 +02:00
|
|
|
})
|