Skip to content

Commit

Permalink
refactor!: enable verification in deep props instead of extra variable
Browse files Browse the repository at this point in the history
  • Loading branch information
davidyuk committed Jun 24, 2021
1 parent b8611fc commit 989b36f
Show file tree
Hide file tree
Showing 8 changed files with 13 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/ae/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import { AE_AMOUNT_FORMATS } from '../utils/amount-formatter'
* @rtype (tx: String, options: Object) => Promise[String]
* @param {String} tx - Transaction
* @param {Object} [options={}] options - Options
* @param {Object} [options.verify] verify - Verify transaction before broadcast, throw error if not valid
* @param {Object} [options.verify=true] verify - Verify transaction before broadcast, throw error if not valid
* @return {Object} Transaction
*/
async function send (tx, options = {}) {
Expand Down
4 changes: 2 additions & 2 deletions src/chain/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import { required } from '@stamp/required'
* @return {Object} Chain instance
*/
const Chain = stampit({
deepProps: { Ae: { defaults: { waitMined: true } } },
deepProps: { Ae: { defaults: { waitMined: true, verify: true } } },
statics: { waitMined (bool) { return this.deepProps({ Ae: { defaults: { waitMined: bool } } }) } }
}, required({
methods: {
Expand Down Expand Up @@ -74,7 +74,7 @@ const Chain = stampit({
* @rtype (tx: String, options?: Object) => tx: Promise[Object]|txHash: Promise[String]
* @param {String} tx - Transaction to submit
* @param {String} [options={}] - Options to pass to the implementation
* @param {String} [options.verify = false] - Verify transaction before broadcast.
* @param {String} [options.verify=true] - Verify transaction before broadcast.
* @return {Object} Transaction
*/

Expand Down
6 changes: 1 addition & 5 deletions src/chain/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ import { DRY_RUN_ACCOUNT, NAME_ID_KEY } from '../tx/builder/schema'

async function sendTransaction (tx, options = {}) {
const { waitMined, verify } = R.merge(this.Ae.defaults, options)
// Verify transaction before broadcast
if (verify || (typeof verify !== 'boolean' && this.verifyTxBeforeSend)) {
if (verify) {
const { validation, tx: txObject, txType } = await this.unpackAndVerify(tx)
if (validation.length) {
throw Object.assign(new Error('Transaction verification error: ' + JSON.stringify(validation)), {
Expand Down Expand Up @@ -260,9 +259,6 @@ async function resolveName (nameOrId, prefix, { verify, resolveByNode } = {}) {
* @example ChainNode({url: 'https://testnet.aeternity.io/'})
*/
const ChainNode = Chain.compose(TransactionValidator, NodePool, {
init ({ verifyTx = true }) {
this.verifyTxBeforeSend = verifyTx
},
methods: {
sendTransaction,
balance,
Expand Down
4 changes: 2 additions & 2 deletions test/integration/aens.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ describe('Aens', function () {
const { pointers } = await aens.getName(name)
pointers.length.should.be.equal(0)
try {
await aens.spend(100, name, { onAccount, verify: true })
await aens.spend(100, name, { onAccount })
} catch (e) {
e.message.should.be.equal(`Name ${name} don't have pointers for ak`)
}
Expand Down Expand Up @@ -118,7 +118,7 @@ contract Identity =
it('Spend by name', async () => {
const current = await aens.address()
const onAccount = aens.addresses().find(acc => acc !== current)
await aens.spend(100, name, { onAccount, verify: true })
await aens.spend(100, name, { onAccount })
})

it('transfers names', async () => {
Expand Down
2 changes: 1 addition & 1 deletion test/integration/contract.js
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ describe('Contract', function () {
await contract.poll(deployed.transaction, { interval: 50, attempts: 1200 })
expect(deployed.result).to.be.equal(undefined)
deployed.txData.should.not.be.equal(undefined)
const result = await deployed.call('getArg', ['42'], { waitMined: false, verify: false })
const result = await deployed.call('getArg', ['42'], { waitMined: false })
expect(result.result).to.be.equal(undefined)
result.txData.should.not.be.equal(undefined)
await contract.poll(result.hash, { interval: 50, attempts: 1200 })
Expand Down
11 changes: 4 additions & 7 deletions test/integration/ga.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,8 @@ describe('Generalize Account', function () {
})

it('Fail on make GA on already GA account', async () => {
try {
await client.createGeneralizeAccount('authorize', authContract)
} catch (e) {
e.message.should.be.equal(`Account ${gaAccount.publicKey} is already GA`)
}
await client.createGeneralizeAccount('authorize', authContract)
.should.be.rejectedWith(`Account ${gaAccount.publicKey} is already GA`)
})

it('Init MemoryAccount for GA and Spend using GA', async () => {
Expand All @@ -59,8 +56,8 @@ describe('Generalize Account', function () {

const r = () => Math.floor(Math.random() * 20).toString()
const callData = await client.contractEncodeCall(authContract, 'authorize', [r()])
await client.spend(10000, publicKey, { authData: { callData }, onAccount: gaAccount.publicKey, verify: false })
await client.spend(10000, publicKey, { authData: { source: authContract, args: [r()] }, onAccount: gaAccount.publicKey, verify: false })
await client.spend(10000, publicKey, { authData: { callData }, onAccount: gaAccount.publicKey })
await client.spend(10000, publicKey, { authData: { source: authContract, args: [r()] }, onAccount: gaAccount.publicKey })
const balanceAfter = await client.balance(publicKey)
balanceAfter.should.be.equal('20000')
})
Expand Down
2 changes: 1 addition & 1 deletion test/integration/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ describe('Native Transaction', function () {
const nativeTx = await clientNative.oracleRegisterTx(params)

txFromAPI.should.be.equal(nativeTx)
await clientNative.send(nativeTx, { verify: true })
await clientNative.send(nativeTx)

const oId = (await client.api.getOracleByPubkey(oracleId)).id
oId.should.be.equal(oracleId)
Expand Down
2 changes: 1 addition & 1 deletion test/integration/txVerification.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ describe('Verify Transaction', function () {
})

try {
await client.send(spendTx, { verify: true })
await client.send(spendTx)
} catch ({ errorData }) {
const atLeastOneError = !!errorData.validation.length
atLeastOneError.should.be.equal(true)
Expand Down

0 comments on commit 989b36f

Please sign in to comment.