From 2451ed2ae65e30f45aed735ad66243465fe24627 Mon Sep 17 00:00:00 2001 From: manolodewiner Date: Wed, 21 Mar 2018 11:30:54 +0100 Subject: [PATCH] update endpoints --- src/connection.js | 67 +++++++++++----------------- src/transaction.js | 2 +- test/connection/test_connection.js | 28 +++--------- test/integration/test_integration.js | 66 +++++++++++---------------- 4 files changed, 58 insertions(+), 105 deletions(-) diff --git a/src/connection.js b/src/connection.js index 6e00495..75f391b 100644 --- a/src/connection.js +++ b/src/connection.js @@ -19,10 +19,11 @@ export default class Connection { getApiUrls(endpoint) { return this.path + { 'blocks': 'blocks', - 'blocksDetail': 'blocks/%(blockId)s', + 'blocksDetail': 'blocks/%(blockHeight)s', 'outputs': 'outputs', - 'statuses': 'statuses', 'transactions': 'transactions', + 'transactionsSync': 'transactions?mode=sync', + 'transactionsCommit': 'transactions?mode=commit', 'transactionsDetail': 'transactions/%(transactionId)s', 'assets': 'assets', 'metadata': 'metadata', @@ -38,24 +39,12 @@ export default class Connection { /** * @public - * @param blockId + * @param blockHeight */ - getBlock(blockId) { + getBlock(blockHeight) { return this._req(this.getApiUrls('blocksDetail'), { urlTemplateSpec: { - blockId - } - }) - } - - /** - * @public - * @param transactionId - */ - getStatus(transactionId) { - return this._req(this.getApiUrls('statuses'), { - query: { - transaction_id: transactionId + blockHeight } }) } @@ -77,11 +66,10 @@ export default class Connection { * @param transactionId * @param status */ - listBlocks(transactionId, status) { + listBlocks(transactionId) { return this._req(this.getApiUrls('blocks'), { query: { transaction_id: transactionId, - status } }) } @@ -133,27 +121,12 @@ export default class Connection { /** * @public - * @param txId - * @return {Promise} + * @param transaction */ - pollStatusAndFetchTransaction(txId) { - return new Promise((resolve, reject) => { - const timer = setInterval(() => { - this.getStatus(txId) - .then((res) => { - if (res.status === 'valid') { - clearInterval(timer) - this.getTransaction(txId) - .then((res_) => { - resolve(res_) - }) - } - }) - .catch((err) => { - clearInterval(timer) - reject(err) - }) - }, 500) + postTransaction(transaction) { + return this._req(this.getApiUrls('transactions'), { + method: 'POST', + jsonBody: transaction }) } @@ -161,8 +134,20 @@ export default class Connection { * @public * @param transaction */ - postTransaction(transaction) { - return this._req(this.getApiUrls('transactions'), { + postTransactionSync(transaction) { + return this._req(this.getApiUrls('transactionsSync'), { + method: 'POST', + jsonBody: transaction + }) + } + + /** + * @public + * @param transaction + */ + postTransactionCommit(transaction) { + return this._req(this.getApiUrls('transactionsCommit'), { + method: 'POST', jsonBody: transaction }) diff --git a/src/transaction.js b/src/transaction.js index 8c15f1c..d64e7dd 100644 --- a/src/transaction.js +++ b/src/transaction.js @@ -38,7 +38,7 @@ export default class Transaction { 'inputs': [], 'metadata': null, 'asset': null, - 'version': '1.0', + 'version': '2.0', } return txTemplate } diff --git a/test/connection/test_connection.js b/test/connection/test_connection.js index 5877563..4cdfa2b 100644 --- a/test/connection/test_connection.js +++ b/test/connection/test_connection.js @@ -24,9 +24,8 @@ test('Payload thrown at incorrect API_PATH', t => { test('Generate API URLS', t => { const endpoints = { 'blocks': 'blocks', - 'blocksDetail': 'blocks/%(blockId)s', + 'blocksDetail': 'blocks/%(blockHeight)s', 'outputs': 'outputs', - 'statuses': 'statuses', 'transactions': 'transactions', 'transactionsDetail': 'transactions/%(transactionId)s', 'assets': 'assets', @@ -59,30 +58,15 @@ test('Request with custom headers', t => { test('Get block for a block id', t => { const expectedPath = 'path' - const blockId = 'abc' + const blockHeight = 'abc' conn._req = sinon.spy() conn.getApiUrls = sinon.stub().returns(expectedPath) - conn.getBlock(blockId) + conn.getBlock(blockHeight) t.truthy(conn._req.calledWith( expectedPath, - { urlTemplateSpec: { blockId } } - )) -}) - - -test('Get status for a transaction id', t => { - const expectedPath = 'path' - const transactionId = 'abc' - - conn._req = sinon.spy() - conn.getApiUrls = sinon.stub().returns(expectedPath) - - conn.getStatus(transactionId) - t.truthy(conn._req.calledWith( - expectedPath, - { query: { transaction_id: transactionId } } + { urlTemplateSpec: { blockHeight } } )) }) @@ -105,18 +89,16 @@ test('Get transaction for a transaction id', t => { test('Get list of blocks for a transaction id', t => { const expectedPath = 'path' const transactionId = 'abc' - const status = 'status' conn._req = sinon.spy() conn.getApiUrls = sinon.stub().returns(expectedPath) - conn.listBlocks(transactionId, status) + conn.listBlocks(transactionId) t.truthy(conn._req.calledWith( expectedPath, { query: { transaction_id: transactionId, - status } } )) diff --git a/test/integration/test_integration.js b/test/integration/test_integration.js index 255ab55..f46e8ba 100644 --- a/test/integration/test_integration.js +++ b/test/integration/test_integration.js @@ -36,8 +36,7 @@ test('Valid CREATE transaction', t => { ) const txSigned = Transaction.signTransaction(tx, alice.privateKey) - return conn.postTransaction(txSigned) - .then(({ id }) => conn.pollStatusAndFetchTransaction(id)) + return conn.postTransactionSync(txSigned) .then(resTx => t.truthy(resTx)) }) @@ -55,8 +54,7 @@ test('Valid TRANSFER transaction with single Ed25519 input', t => { alice.privateKey ) - return conn.postTransaction(createTxSigned) - .then(({ id }) => conn.pollStatusAndFetchTransaction(id)) + return conn.postTransactionSync(createTxSigned) .then(() => { const transferTx = Transaction.makeTransferTransaction( [{ tx: createTxSigned, output_index: 0 }], @@ -67,8 +65,7 @@ test('Valid TRANSFER transaction with single Ed25519 input', t => { transferTx, alice.privateKey ) - return conn.postTransaction(transferTxSigned) - .then(({ id }) => conn.pollStatusAndFetchTransaction(id)) + return conn.postTransactionSync(transferTxSigned) .then(resTx => t.truthy(resTx)) }) }) @@ -87,8 +84,7 @@ test('Valid TRANSFER transaction with multiple Ed25519 inputs', t => { alice.privateKey ) - return conn.postTransaction(createTxSigned) - .then(({ 'id': txId }) => conn.pollStatusAndFetchTransaction(txId)) + return conn.postTransactionSync(createTxSigned) .then(() => { const transferTx = Transaction.makeTransferTransaction( [{ tx: createTxSigned, output_index: 0 }, { tx: createTxSigned, output_index: 1 }], @@ -100,8 +96,7 @@ test('Valid TRANSFER transaction with multiple Ed25519 inputs', t => { alice.privateKey, bob.privateKey ) - return conn.postTransaction(transferTxSigned) - .then(({ id }) => conn.pollStatusAndFetchTransaction(id)) + return conn.postTransactionSync(transferTxSigned) .then(resTx => t.truthy(resTx)) }) }) @@ -129,8 +124,7 @@ test('Valid TRANSFER transaction with multiple Ed25519 inputs from different tra alice.privateKey ) - return conn.postTransaction(createTxSigned) - .then(({ 'id': txId }) => conn.pollStatusAndFetchTransaction(txId)) + return conn.postTransactionSync(createTxSigned) .then(() => { const transferTx1 = Transaction.makeTransferTransaction( [{ tx: createTxSigned, output_index: 0 }], @@ -151,10 +145,8 @@ test('Valid TRANSFER transaction with multiple Ed25519 inputs from different tra bob.privateKey ) - return conn.postTransaction(transferTxSigned1) - .then(({ id }) => conn.pollStatusAndFetchTransaction(id)) - .then(conn.postTransaction(transferTxSigned2)) - .then(({ id }) => conn.pollStatusAndFetchTransaction(id)) + return conn.postTransactionSync(transferTxSigned1) + .then(conn.postTransactionSync(transferTxSigned2)) .then(() => { const transferTxMultipleInputs = Transaction.makeTransferTransaction( [{ tx: transferTxSigned1, output_index: 0 }, @@ -167,8 +159,7 @@ test('Valid TRANSFER transaction with multiple Ed25519 inputs from different tra carol.privateKey, trent.privateKey ) - return conn.postTransaction(transferTxSignedMultipleInputs) - .then(({ id }) => conn.pollStatusAndFetchTransaction(id)) + return conn.postTransactionSync(transferTxSignedMultipleInputs) .then(resTx => t.truthy(resTx)) }) }) @@ -208,10 +199,8 @@ test('Search for spent and unspent outputs of a given public key', t => { carol.privateKey, ) - return conn.postTransaction(createTxSigned) - .then(({ id }) => conn.pollStatusAndFetchTransaction(id)) - .then(() => conn.postTransaction(transferTxSigned)) - .then(({ id }) => conn.pollStatusAndFetchTransaction(id)) + return conn.postTransactionSync(createTxSigned) + .then(() => conn.postTransactionSync(transferTxSigned)) .then(() => conn.listOutputs(carol.publicKey)) // now listOutputs should return us outputs 0 and 1 (unfiltered) .then(outputs => t.truthy(outputs.length === 2)) @@ -250,10 +239,8 @@ test('Search for unspent outputs for a given public key', t => { carol.privateKey, ) - return conn.postTransaction(createTxSigned) - .then(({ id }) => conn.pollStatusAndFetchTransaction(id)) - .then(() => conn.postTransaction(transferTxSigned)) - .then(({ id }) => conn.pollStatusAndFetchTransaction(id)) + return conn.postTransactionSync(createTxSigned) + .then(() => conn.postTransactionSync(transferTxSigned)) // now listOutputs should return us outputs 0 and 2 (1 is spent) .then(() => conn.listOutputs(carol.publicKey, 'false')) .then(outputs => t.truthy(outputs.length === 2)) @@ -292,10 +279,8 @@ test('Search for spent outputs for a given public key', t => { carol.privateKey, ) - return conn.postTransaction(createTxSigned) - .then(({ id }) => conn.pollStatusAndFetchTransaction(id)) - .then(() => conn.postTransaction(transferTxSigned)) - .then(({ id }) => conn.pollStatusAndFetchTransaction(id)) + return conn.postTransactionSync(createTxSigned) + .then(() => conn.postTransactionSync(transferTxSigned)) // now listOutputs should only return us output 1 (0 and 2 are unspent) .then(() => conn.listOutputs(carol.publicKey, true)) .then(outputs => t.truthy(outputs.length === 1)) @@ -316,10 +301,10 @@ test('Search for an asset', t => { alice.privateKey ) - return conn.postTransaction(createTxSigned) - .then(({ id }) => conn.pollStatusAndFetchTransaction(id)) + return conn.postTransactionSync(createTxSigned) .then(() => conn.searchAssets(createTxSigned.asset.data.message)) .then(assets => t.truthy( + console.log('llllllllllllllllllll', createTxSigned.asset.data.message, assets.pop()), assets.pop(), createTxSigned.asset.data.message )) @@ -340,8 +325,7 @@ test('Search for metadata', t => { alice.privateKey ) - return conn.postTransaction(createTxSigned) - .then(({ id }) => conn.pollStatusAndFetchTransaction(id)) + return conn.postTransactionSync(createTxSigned) .then(() => conn.searchMetadata(createTxSigned.metadata.message)) .then(assets => t.truthy( assets.pop(), @@ -349,6 +333,7 @@ test('Search for metadata', t => { )) }) + test('Search blocks containing a transaction', t => { const conn = new Connection(API_PATH) @@ -363,9 +348,8 @@ test('Search blocks containing a transaction', t => { alice.privateKey ) - return conn.postTransaction(createTxSigned) - .then(({ id }) => conn.pollStatusAndFetchTransaction(id)) - .then(({ id }) => conn.listBlocks(id, 'VALID')) + return conn.postTransactionSync(createTxSigned) + .then(({ id }) => conn.listBlocks(id)) .then(blocks => conn.getBlock(blocks.pop())) .then(({ block: { transactions } }) => transactions.filter(({ id }) => id === createTxSigned.id)) .then(transactions => t.truthy(transactions.length === 1)) @@ -386,10 +370,12 @@ test('Search transaction containing an asset', t => { alice.privateKey ) - return conn.postTransaction(createTxSigned) - .then(({ id }) => conn.pollStatusAndFetchTransaction(id, 'CREATE')) + return conn.postTransactionSync(createTxSigned) .then(({ id }) => conn.listTransactions(id)) - .then(transactions => t.truthy(transactions.length === 1)) + .then(transactions => { + console.log('cccccccccccc', transactions, createTxSigned.id) + t.truthy(transactions.length === 1) + }) })