Skip to content

Commit

Permalink
feat(Tx): Always verify transaction before send it to the node (#798)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
nduchak authored Nov 28, 2019
1 parent eee46d0 commit 170f479
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 33 deletions.
2 changes: 1 addition & 1 deletion es/ae/aepp.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 3 additions & 1 deletion es/ae/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
4 changes: 2 additions & 2 deletions es/chain/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'), {
Expand Down Expand Up @@ -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: {
Expand Down
10 changes: 8 additions & 2 deletions es/tx/builder/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

/**
Expand Down
43 changes: 24 additions & 19 deletions test/integration/aens.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}))
})
})
2 changes: 1 addition & 1 deletion test/integration/channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ function waitForChannel (channel) {
)
}

describe.skip('Channel', function () {
describe('Channel', function () {
configure(this)
this.timeout(120000)

Expand Down
4 changes: 2 additions & 2 deletions test/integration/ga.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
})
Expand Down
14 changes: 9 additions & 5 deletions test/integration/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down

0 comments on commit 170f479

Please sign in to comment.