-
Notifications
You must be signed in to change notification settings - Fork 91
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Updated data models for 1.0 #35
Changes from 8 commits
0ed3aa3
27d8dab
a14dbba
b824158
ed18384
9183205
3389726
ac0a124
42b4002
ede11da
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do you mean specifically? L25 or the L24-25 comment? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove TODO |
||
export default function makeCreateTransaction(asset, metadata, outputs, ...issuers) { | ||
const assetDefinition = { | ||
'data': asset || null, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,10 +3,13 @@ | |
* Create an Output from a 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 {number} amount Amount of the output | ||
* @param {string} amount Amount of the output | ||
* @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 { | ||
'amount': amount, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of stringifying, strings have to be passed directly now. |
||
condition, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I just noticed that I should maybe not do this whole private function thing. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If someone knows how to mock a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
unspentTransaction, | ||
metadata, | ||
outputs, | ||
...fulfilledOutputs | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. rename to: outputIndexes |
||
) { | ||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It might be nice to have a function |
||
: 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,12 @@ import makeTransaction from './makeTransaction' | |
* @returns {object} Unsigned transaction -- make sure to call signTransaction() on it before | ||
* sending it off! | ||
*/ | ||
export default function makeTransferTransaction( | ||
unspentTransaction, | ||
metadata, | ||
outputs, | ||
...fulfilledOutputs | ||
) { | ||
const inputs = fulfilledOutputs.map((outputIndex) => { | ||
const fulfilledOutput = unspentTransaction.outputs[outputIndex] | ||
const transactionLink = { | ||
'output': outputIndex, | ||
'txid': 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) | ||
// TODO: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TODOs in master? |
||
// - 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(...args) { | ||
return makeTransaction(..._makeTransferTransaction(...args)) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
import test from 'ava' | ||
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() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use output of respective functions as strings/dicts here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Created an issue: #45 |
||
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 => { | ||
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) | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm guessing we want to change this to 1.0.0rc1 at some point
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup