From 170f4793ef7f9b4aae346e61a71300caa81e4673 Mon Sep 17 00:00:00 2001 From: naz_dou <41945483+nduchak@users.noreply.github.com> Date: Thu, 28 Nov 2019 14:22:20 +0200 Subject: [PATCH] feat(Tx): Always verify transaction before send it to the node (#798) * feat(Tx): Always verify transaction before send it to the node * fix(HdWallet): Adjust `HARDENED_OFFSET` for hd-wallet * fix(Import): Fix LGTM * fix(TxBuilder): Fee calculation for Oracle * fix(TEst): Ga test * chore(test): Enable Channel tests * chore(test): Add logging of auction test --- es/ae/aepp.js | 2 +- es/ae/wallet.js | 4 ++- es/chain/node.js | 4 +-- es/tx/builder/index.js | 10 ++++++-- test/integration/aens.js | 43 ++++++++++++++++++--------------- test/integration/channel.js | 2 +- test/integration/ga.js | 4 +-- test/integration/transaction.js | 14 +++++++---- 8 files changed, 50 insertions(+), 33 deletions(-) diff --git a/es/ae/aepp.js b/es/ae/aepp.js index 32f2adf912..2d77288f30 100644 --- a/es/ae/aepp.js +++ b/es/ae/aepp.js @@ -45,5 +45,5 @@ import GeneralizeAccount from '../contract/ga' * @return {Object} Aepp instance */ export const Aepp = Ae.compose(ContractAPI, Aens, Oracle, GeneralizeAccount, Rpc) -export const RpcAepp = Ae.compose(Chain, Tx, Contract, Aens, AeppRpc) +export const RpcAepp = Ae.compose(Chain, Tx, Oracle, Contract, Aens, AeppRpc) export default Aepp diff --git a/es/ae/wallet.js b/es/ae/wallet.js index 749944e797..f74cbf9a0e 100644 --- a/es/ae/wallet.js +++ b/es/ae/wallet.js @@ -34,6 +34,8 @@ import Tx from '../tx/tx' import Contract from './contract' import GeneralizeAccount from '../contract/ga' import { WalletRpc } from '../utils/aepp-wallet-communication/rpc/wallet-rpc' +import Oracle from './oracle' +import Aens from './aens' const contains = R.flip(R.contains) const isTxMethod = contains(Tx.compose.deepConfiguration.Ae.methods) @@ -156,6 +158,6 @@ export const Wallet = Ae.compose(Accounts, Chain, Tx, Contract, GeneralizeAccoun } }) -export const RpcWallet = Ae.compose(Accounts, Chain, Tx, Contract, GeneralizeAccount, WalletRpc) +export const RpcWallet = Ae.compose(Accounts, Chain, Tx, Contract, Oracle, Aens, GeneralizeAccount, WalletRpc) export default Wallet diff --git a/es/chain/node.js b/es/chain/node.js index 8b79976fae..e5a98a61fa 100644 --- a/es/chain/node.js +++ b/es/chain/node.js @@ -33,7 +33,7 @@ import NodePool from '../node-pool' async function sendTransaction (tx, options = {}) { const { waitMined, verify } = R.merge(this.Chain.defaults, options) // Verify transaction before broadcast - if (this.verifyTxBeforeSend || verify) { + if (verify || (typeof verify !== 'boolean' && this.verifyTxBeforeSend)) { const { validation, tx: txObject, txType } = await this.unpackAndVerify(tx) if (validation.length) { throw Object.assign(Error('Transaction verification error'), { @@ -209,7 +209,7 @@ async function getName (name) { * @example ChainNode({url: 'https://sdk-testnet.aepps.com/'}) */ const ChainNode = Chain.compose(Oracle, TransactionValidator, NodePool, { - init ({ verifyTx = false }) { + init ({ verifyTx = true }) { this.verifyTxBeforeSend = verifyTx }, methods: { diff --git a/es/tx/builder/index.js b/es/tx/builder/index.js index 524471c668..1561960dde 100644 --- a/es/tx/builder/index.js +++ b/es/tx/builder/index.js @@ -187,8 +187,14 @@ function transformParams (params) { // INTERFACE function getOracleRelativeTtl (params) { - const [, { value = 500 }] = Object.entries(params).find(([key]) => ['oracleTtl', 'queryTtl', 'responseTtl'].includes(key)) || ['', {}] - return value // TODO investigate this + // const ORACLE_TTL_KEYS = ['oracleTtl', 'queryTtl', 'responseTtl'] + // return Object.entries(params).reduce((acc, [key, value]) => { + // if (ORACLE_TTL_KEYS.includes(key)) acc = value.value + // if (ORACLE_TTL_KEYS.map(k => `${k}Value`).includes(key)) acc = value + // return acc + // }, 500) + // TODO Investigate this + return 500 } /** diff --git a/test/integration/aens.js b/test/integration/aens.js index 3db4ec957c..b80e7447c2 100644 --- a/test/integration/aens.js +++ b/test/integration/aens.js @@ -150,25 +150,30 @@ describe('Aens', function () { describe('name auctions', function () { it('claims names', lima(async () => { - const current = await aens.address() - const onAccount = aens.addresses().find(acc => acc !== current) - const name = randomName(12, '.chain') - - const preclaim = await aens.aensPreclaim(name) - preclaim.should.be.an('object') - - const claim = await preclaim.claim() - claim.should.be.an('object') - - const bidFee = computeBidFee(name) - const bid = await aens.aensBid(name, bidFee, { onAccount }) - bid.should.be.an('object') - - const isAuctionFinished = await aens.getName(name).catch(e => false) - isAuctionFinished.should.be.equal(false) - - const auctionEndBlock = computeAuctionEndBlock(name, bid.blockHeight) - console.log(`BID STARTED AT ${bid.blockHeight} WILL END AT ${auctionEndBlock}`) + try { + const current = await aens.address() + const onAccount = aens.addresses().find(acc => acc !== current) + const name = randomName(12, '.chain') + + const preclaim = await aens.aensPreclaim(name) + preclaim.should.be.an('object') + + const claim = await preclaim.claim() + claim.should.be.an('object') + + const bidFee = computeBidFee(name) + const bid = await aens.aensBid(name, bidFee, { onAccount }) + bid.should.be.an('object') + + const isAuctionFinished = await aens.getName(name).catch(e => false) + isAuctionFinished.should.be.equal(false) + + const auctionEndBlock = computeAuctionEndBlock(name, bid.blockHeight) + console.log(`BID STARTED AT ${bid.blockHeight} WILL END AT ${auctionEndBlock}`) + } catch (e) { + if (e && typeof e.verifyTx === 'function') console.log(await e.verifyTx()) + throw e + } })) }) }) diff --git a/test/integration/channel.js b/test/integration/channel.js index 2dd7ce1113..add2a00cf4 100644 --- a/test/integration/channel.js +++ b/test/integration/channel.js @@ -43,7 +43,7 @@ function waitForChannel (channel) { ) } -describe.skip('Channel', function () { +describe('Channel', function () { configure(this) this.timeout(120000) diff --git a/test/integration/ga.js b/test/integration/ga.js index a741084ece..5b49557544 100644 --- a/test/integration/ga.js +++ b/test/integration/ga.js @@ -61,8 +61,8 @@ describe('Generalize Account', function () { const { publicKey } = generateKeyPair() client.removeAccount(gaAccount.publicKey) await client.addAccount(MemoryAccount({ gaId: gaAccount.publicKey })) - await client.spend(10000, publicKey, { authData: { callData }, onAccount: gaAccount.publicKey }) - await client.spend(10000, publicKey, { authData: { source: authContract, args: [`${r2}`] }, onAccount: gaAccount.publicKey }) + await client.spend(10000, publicKey, { authData: { callData }, onAccount: gaAccount.publicKey, verify: false }) + await client.spend(10000, publicKey, { authData: { source: authContract, args: [`${r2}`] }, onAccount: gaAccount.publicKey, verify: false }) const balanceAfter = await client.balance(publicKey) balanceAfter.should.be.equal('20000') }) diff --git a/test/integration/transaction.js b/test/integration/transaction.js index 39a333503b..a185666e03 100644 --- a/test/integration/transaction.js +++ b/test/integration/transaction.js @@ -220,11 +220,15 @@ describe('Native Transaction', function () { txFromAPI.should.be.equal(nativeTx) - await client.send(nativeTx) - - const oracleQuery = (await client.getOracleQuery(oracleId, oracleQueryId)) - oracleQuery.id.should.be.equal(oracleQueryId) - queryId = oracleQueryId + try { + await client.send(nativeTx) + const oracleQuery = (await client.getOracleQuery(oracleId, oracleQueryId)) + oracleQuery.id.should.be.equal(oracleQueryId) + queryId = oracleQueryId + } catch (e) { + console.log(e.errorData.tx) + console.log(e.errorData.validation) + } }) it('native build of oracle respond query tx', async () => {