Skip to content

Commit 0d284c7

Browse files
authored
Merge pull request #35 from bigchaindb/1.0-rc-compatiblity
Updated data models for 1.0
2 parents f1f52cd + ede11da commit 0d284c7

File tree

5 files changed

+148
-10
lines changed

5 files changed

+148
-10
lines changed

.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ before_install:
1818
-e BIGCHAINDB_KEYPAIR_PRIVATE=5C5Cknco7YxBRP9AgB1cbUVTL4FAcooxErLygw1DeG2D
1919
-e BIGCHAINDB_DATABASE_BACKEND=mongodb
2020
-e BIGCHAINDB_DATABASE_HOST=172.17.0.1
21-
bigchaindb/bigchaindb:0.10.2
21+
bigchaindb/bigchaindb:master
2222
start
2323

2424
script: yarn test

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,12 @@
4242
"babel-runtime": "^6.22.0",
4343
"cross-env": "^5.0.1",
4444
"eslint": "^3.14.1",
45-
"eslint-config-ascribe": "^3.0.1",
45+
"eslint-config-ascribe": "^3.0.4",
4646
"eslint-plugin-import": "^2.2.0",
4747
"husky": "^0.13.4",
4848
"release-it": "^2.7.3",
4949
"rimraf": "^2.5.4",
50+
"sinon": "^2.3.4",
5051
"webpack": "^2.2.1"
5152
},
5253
"dependencies": {

src/transaction/makeOutput.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@
33
* Create an Output from a Condition.
44
* Note: Assumes the given Condition was generated from a single public key (e.g. a Ed25519 Condition)
55
* @param {object} condition Condition (e.g. a Ed25519 Condition from `makeEd25519Condition()`)
6-
* @param {number} amount Amount of the output
6+
* @param {string} amount Amount of the output
77
* @returns {object} An Output usable in a Transaction
88
*/
9-
export default function makeOutput(condition, amount = 1) {
9+
export default function makeOutput(condition, amount = '1') {
10+
if (typeof amount !== 'string') {
11+
throw new TypeError('`amount` must be of type string')
12+
}
1013
return {
1114
'amount': amount,
1215
condition,

src/transaction/makeTransferTransaction.js

+8-6
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,27 @@ import makeTransaction from './makeTransaction'
1414
* For `TRANSFER` Transactions, this should usually just be a list of
1515
* Outputs wrapping Ed25519 Conditions generated from the public keys of
1616
* the recipients.
17-
* @param {...number} fulfilledOutputs Indices of the Outputs in `unspentTransaction` that this
17+
* @param {...number} OutputIndices Indices of the Outputs in `unspentTransaction` that this
1818
* Transaction fulfills.
19-
* Note that the public keys listed in the fulfilled Outputs
20-
* must be used (and in the same order) to sign the Transaction
19+
* Note that listed public keys listed must be used (and in
20+
* the same order) to sign the Transaction
2121
* (`signTransaction()`).
2222
* @returns {object} Unsigned transaction -- make sure to call signTransaction() on it before
2323
* sending it off!
2424
*/
25+
// TODO:
26+
// - Make `metadata` optional argument
2527
export default function makeTransferTransaction(
2628
unspentTransaction,
2729
metadata,
2830
outputs,
29-
...fulfilledOutputs
31+
...outputIndices
3032
) {
31-
const inputs = fulfilledOutputs.map((outputIndex) => {
33+
const inputs = outputIndices.map((outputIndex) => {
3234
const fulfilledOutput = unspentTransaction.outputs[outputIndex]
3335
const transactionLink = {
3436
'output': outputIndex,
35-
'txid': unspentTransaction.id,
37+
'transaction_id': unspentTransaction.id,
3638
}
3739

3840
return makeInputTemplate(fulfilledOutput.public_keys, transactionLink)

test/transaction/test_transaction.js

+132
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
import test from 'ava'
2+
import sinon from 'sinon'
3+
4+
import { Transaction, Ed25519Keypair } from '../../src'
5+
import * as makeTransaction from '../../src/transaction/makeTransaction' // eslint-disable-line
6+
import makeInputTemplate from '../../src/transaction/makeInputTemplate'
7+
8+
9+
// TODO: Find out if ava has something like conftest, if so put this there.
10+
const alice = new Ed25519Keypair()
11+
const aliceCondition = Transaction.makeEd25519Condition(alice.publicKey)
12+
const aliceOutput = Transaction.makeOutput(aliceCondition)
13+
const assetMessage = { assetMessage: 'assetMessage' }
14+
const metaDataMessage = { metaDataMessage: 'metaDataMessage' }
15+
const createTx = Transaction.makeCreateTransaction(
16+
assetMessage,
17+
metaDataMessage,
18+
[aliceOutput],
19+
alice.publicKey
20+
)
21+
const transferTx = Transaction.makeTransferTransaction(
22+
createTx,
23+
metaDataMessage,
24+
[aliceOutput],
25+
0
26+
)
27+
28+
29+
test('Create valid output with default amount', t => {
30+
const condition = {
31+
details: {
32+
public_key: 'abc'
33+
}
34+
}
35+
const expected = {
36+
amount: '1',
37+
condition,
38+
public_keys: ['abc']
39+
}
40+
const res = Transaction.makeOutput(condition)
41+
t.deepEqual(res, expected)
42+
})
43+
44+
45+
test('Create valid output with custom amount', t => {
46+
const condition = {
47+
details: {
48+
public_key: 'abc'
49+
}
50+
}
51+
const customAmount = '1337'
52+
const expected = {
53+
amount: customAmount,
54+
condition,
55+
public_keys: ['abc']
56+
}
57+
const res = Transaction.makeOutput(condition, customAmount)
58+
t.deepEqual(res, expected)
59+
})
60+
61+
test('Pass condition not based on public_keys to makeOutput', t => {
62+
const condition = {
63+
details: {
64+
idea: 'just pretend this is e.g. a hashlock'
65+
}
66+
}
67+
const expected = {
68+
amount: '1',
69+
condition,
70+
public_keys: []
71+
}
72+
const res = Transaction.makeOutput(condition)
73+
t.deepEqual(res, expected)
74+
})
75+
76+
77+
test('makeOutput throws TypeError with incorrect amount type', t => {
78+
t.throws(() => Transaction.makeOutput({}, 1337), TypeError)
79+
})
80+
81+
82+
test('Create TRANSFER transaction based on CREATE transaction', t => {
83+
sinon.spy(makeTransaction, 'default')
84+
85+
Transaction.makeTransferTransaction(
86+
createTx,
87+
metaDataMessage,
88+
[aliceOutput],
89+
0
90+
)
91+
const expected = [
92+
'TRANSFER',
93+
{ id: createTx.id },
94+
metaDataMessage,
95+
[aliceOutput],
96+
[makeInputTemplate(
97+
[alice.publicKey],
98+
{ output: 0, transaction_id: createTx.id }
99+
)]
100+
]
101+
102+
// NOTE: `src/transaction/makeTransaction` is `export default`, hence we
103+
// can only mock `makeTransaction.default` with a hack:
104+
// See: https://stackoverflow.com/a/33676328/1263876
105+
t.truthy(makeTransaction.default.calledWith(...expected))
106+
makeTransaction.default.restore()
107+
})
108+
109+
110+
test('Create TRANSFER transaction based on TRANSFER transaction', t => {
111+
sinon.spy(makeTransaction, 'default')
112+
113+
Transaction.makeTransferTransaction(
114+
transferTx,
115+
metaDataMessage,
116+
[aliceOutput],
117+
0
118+
)
119+
const expected = [
120+
'TRANSFER',
121+
{ id: transferTx.asset.id },
122+
metaDataMessage,
123+
[aliceOutput],
124+
[makeInputTemplate(
125+
[alice.publicKey],
126+
{ output: 0, transaction_id: transferTx.id }
127+
)]
128+
]
129+
130+
t.truthy(makeTransaction.default.calledWith(...expected))
131+
makeTransaction.default.restore()
132+
})

0 commit comments

Comments
 (0)