mirror of
https://github.com/bigchaindb/js-bigchaindb-driver.git
synced 2025-01-03 18:35:13 +01:00
Merge pull request #35 from bigchaindb/1.0-rc-compatiblity
Updated data models for 1.0
This commit is contained in:
commit
0d284c75dc
@ -18,7 +18,7 @@ before_install:
|
|||||||
-e BIGCHAINDB_KEYPAIR_PRIVATE=5C5Cknco7YxBRP9AgB1cbUVTL4FAcooxErLygw1DeG2D
|
-e BIGCHAINDB_KEYPAIR_PRIVATE=5C5Cknco7YxBRP9AgB1cbUVTL4FAcooxErLygw1DeG2D
|
||||||
-e BIGCHAINDB_DATABASE_BACKEND=mongodb
|
-e BIGCHAINDB_DATABASE_BACKEND=mongodb
|
||||||
-e BIGCHAINDB_DATABASE_HOST=172.17.0.1
|
-e BIGCHAINDB_DATABASE_HOST=172.17.0.1
|
||||||
bigchaindb/bigchaindb:0.10.2
|
bigchaindb/bigchaindb:master
|
||||||
start
|
start
|
||||||
|
|
||||||
script: yarn test
|
script: yarn test
|
||||||
|
@ -42,11 +42,12 @@
|
|||||||
"babel-runtime": "^6.22.0",
|
"babel-runtime": "^6.22.0",
|
||||||
"cross-env": "^5.0.1",
|
"cross-env": "^5.0.1",
|
||||||
"eslint": "^3.14.1",
|
"eslint": "^3.14.1",
|
||||||
"eslint-config-ascribe": "^3.0.1",
|
"eslint-config-ascribe": "^3.0.4",
|
||||||
"eslint-plugin-import": "^2.2.0",
|
"eslint-plugin-import": "^2.2.0",
|
||||||
"husky": "^0.13.4",
|
"husky": "^0.13.4",
|
||||||
"release-it": "^2.7.3",
|
"release-it": "^2.7.3",
|
||||||
"rimraf": "^2.5.4",
|
"rimraf": "^2.5.4",
|
||||||
|
"sinon": "^2.3.4",
|
||||||
"webpack": "^2.2.1"
|
"webpack": "^2.2.1"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
@ -3,10 +3,13 @@
|
|||||||
* Create an Output from a Condition.
|
* Create an Output from a Condition.
|
||||||
* Note: Assumes the given Condition was generated from a single public key (e.g. a Ed25519 Condition)
|
* Note: Assumes the given Condition was generated from a single public key (e.g. a Ed25519 Condition)
|
||||||
* @param {object} condition Condition (e.g. a Ed25519 Condition from `makeEd25519Condition()`)
|
* @param {object} condition Condition (e.g. a Ed25519 Condition from `makeEd25519Condition()`)
|
||||||
* @param {number} amount Amount of the output
|
* @param {string} amount Amount of the output
|
||||||
* @returns {object} An Output usable in a Transaction
|
* @returns {object} An Output usable in a Transaction
|
||||||
*/
|
*/
|
||||||
export default function makeOutput(condition, amount = 1) {
|
export default function makeOutput(condition, amount = '1') {
|
||||||
|
if (typeof amount !== 'string') {
|
||||||
|
throw new TypeError('`amount` must be of type string')
|
||||||
|
}
|
||||||
return {
|
return {
|
||||||
'amount': amount,
|
'amount': amount,
|
||||||
condition,
|
condition,
|
||||||
|
@ -14,25 +14,27 @@ import makeTransaction from './makeTransaction'
|
|||||||
* For `TRANSFER` Transactions, this should usually just be a list of
|
* For `TRANSFER` Transactions, this should usually just be a list of
|
||||||
* Outputs wrapping Ed25519 Conditions generated from the public keys of
|
* Outputs wrapping Ed25519 Conditions generated from the public keys of
|
||||||
* the recipients.
|
* the recipients.
|
||||||
* @param {...number} fulfilledOutputs Indices of the Outputs in `unspentTransaction` that this
|
* @param {...number} OutputIndices Indices of the Outputs in `unspentTransaction` that this
|
||||||
* Transaction fulfills.
|
* Transaction fulfills.
|
||||||
* Note that the public keys listed in the fulfilled Outputs
|
* Note that listed public keys listed must be used (and in
|
||||||
* must be used (and in the same order) to sign the Transaction
|
* the same order) to sign the Transaction
|
||||||
* (`signTransaction()`).
|
* (`signTransaction()`).
|
||||||
* @returns {object} Unsigned transaction -- make sure to call signTransaction() on it before
|
* @returns {object} Unsigned transaction -- make sure to call signTransaction() on it before
|
||||||
* sending it off!
|
* sending it off!
|
||||||
*/
|
*/
|
||||||
|
// TODO:
|
||||||
|
// - Make `metadata` optional argument
|
||||||
export default function makeTransferTransaction(
|
export default function makeTransferTransaction(
|
||||||
unspentTransaction,
|
unspentTransaction,
|
||||||
metadata,
|
metadata,
|
||||||
outputs,
|
outputs,
|
||||||
...fulfilledOutputs
|
...outputIndices
|
||||||
) {
|
) {
|
||||||
const inputs = fulfilledOutputs.map((outputIndex) => {
|
const inputs = outputIndices.map((outputIndex) => {
|
||||||
const fulfilledOutput = unspentTransaction.outputs[outputIndex]
|
const fulfilledOutput = unspentTransaction.outputs[outputIndex]
|
||||||
const transactionLink = {
|
const transactionLink = {
|
||||||
'output': outputIndex,
|
'output': outputIndex,
|
||||||
'txid': unspentTransaction.id,
|
'transaction_id': unspentTransaction.id,
|
||||||
}
|
}
|
||||||
|
|
||||||
return makeInputTemplate(fulfilledOutput.public_keys, transactionLink)
|
return makeInputTemplate(fulfilledOutput.public_keys, transactionLink)
|
||||||
|
132
test/transaction/test_transaction.js
Normal file
132
test/transaction/test_transaction.js
Normal file
@ -0,0 +1,132 @@
|
|||||||
|
import test from 'ava'
|
||||||
|
import sinon from 'sinon'
|
||||||
|
|
||||||
|
import { Transaction, Ed25519Keypair } from '../../src'
|
||||||
|
import * as makeTransaction from '../../src/transaction/makeTransaction' // eslint-disable-line
|
||||||
|
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 => {
|
||||||
|
const condition = {
|
||||||
|
details: {
|
||||||
|
public_key: 'abc'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const expected = {
|
||||||
|
amount: '1',
|
||||||
|
condition,
|
||||||
|
public_keys: ['abc']
|
||||||
|
}
|
||||||
|
const res = Transaction.makeOutput(condition)
|
||||||
|
t.deepEqual(res, expected)
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
|
test('Create valid output with custom amount', t => {
|
||||||
|
const condition = {
|
||||||
|
details: {
|
||||||
|
public_key: 'abc'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const customAmount = '1337'
|
||||||
|
const expected = {
|
||||||
|
amount: customAmount,
|
||||||
|
condition,
|
||||||
|
public_keys: ['abc']
|
||||||
|
}
|
||||||
|
const res = Transaction.makeOutput(condition, customAmount)
|
||||||
|
t.deepEqual(res, expected)
|
||||||
|
})
|
||||||
|
|
||||||
|
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)
|
||||||
|
t.deepEqual(res, expected)
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
|
test('makeOutput throws TypeError with incorrect amount type', t => {
|
||||||
|
t.throws(() => Transaction.makeOutput({}, 1337), TypeError)
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
|
test('Create TRANSFER transaction based on CREATE transaction', t => {
|
||||||
|
sinon.spy(makeTransaction, 'default')
|
||||||
|
|
||||||
|
Transaction.makeTransferTransaction(
|
||||||
|
createTx,
|
||||||
|
metaDataMessage,
|
||||||
|
[aliceOutput],
|
||||||
|
0
|
||||||
|
)
|
||||||
|
const expected = [
|
||||||
|
'TRANSFER',
|
||||||
|
{ id: createTx.id },
|
||||||
|
metaDataMessage,
|
||||||
|
[aliceOutput],
|
||||||
|
[makeInputTemplate(
|
||||||
|
[alice.publicKey],
|
||||||
|
{ output: 0, transaction_id: createTx.id }
|
||||||
|
)]
|
||||||
|
]
|
||||||
|
|
||||||
|
// NOTE: `src/transaction/makeTransaction` is `export default`, hence we
|
||||||
|
// can only mock `makeTransaction.default` with a hack:
|
||||||
|
// See: https://stackoverflow.com/a/33676328/1263876
|
||||||
|
t.truthy(makeTransaction.default.calledWith(...expected))
|
||||||
|
makeTransaction.default.restore()
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
|
test('Create TRANSFER transaction based on TRANSFER transaction', t => {
|
||||||
|
sinon.spy(makeTransaction, 'default')
|
||||||
|
|
||||||
|
Transaction.makeTransferTransaction(
|
||||||
|
transferTx,
|
||||||
|
metaDataMessage,
|
||||||
|
[aliceOutput],
|
||||||
|
0
|
||||||
|
)
|
||||||
|
const expected = [
|
||||||
|
'TRANSFER',
|
||||||
|
{ id: transferTx.asset.id },
|
||||||
|
metaDataMessage,
|
||||||
|
[aliceOutput],
|
||||||
|
[makeInputTemplate(
|
||||||
|
[alice.publicKey],
|
||||||
|
{ output: 0, transaction_id: transferTx.id }
|
||||||
|
)]
|
||||||
|
]
|
||||||
|
|
||||||
|
t.truthy(makeTransaction.default.calledWith(...expected))
|
||||||
|
makeTransaction.default.restore()
|
||||||
|
})
|
Loading…
Reference in New Issue
Block a user