2020-04-20 17:01:00 +02:00
|
|
|
import { strict as assert } from 'assert'
|
2020-01-09 04:34:58 +01:00
|
|
|
import * as txUtils from '../../../../../app/scripts/controllers/transactions/lib/util'
|
2017-03-07 20:34:11 +01:00
|
|
|
|
2017-05-04 23:35:10 +02:00
|
|
|
describe('txUtils', function () {
|
2018-04-06 20:07:20 +02:00
|
|
|
describe('#validateTxParams', function () {
|
|
|
|
it('does not throw for positive values', function () {
|
2019-12-03 15:52:01 +01:00
|
|
|
const sample = {
|
2018-04-06 20:07:20 +02:00
|
|
|
from: '0x1678a085c290ebd122dc42cba69373b5953b831d',
|
|
|
|
value: '0x01',
|
|
|
|
}
|
|
|
|
txUtils.validateTxParams(sample)
|
|
|
|
})
|
2017-03-07 20:34:11 +01:00
|
|
|
|
2018-04-06 20:07:20 +02:00
|
|
|
it('returns error for negative values', function () {
|
2019-12-03 15:52:01 +01:00
|
|
|
const sample = {
|
2018-04-06 20:07:20 +02:00
|
|
|
from: '0x1678a085c290ebd122dc42cba69373b5953b831d',
|
|
|
|
value: '-0x01',
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
txUtils.validateTxParams(sample)
|
|
|
|
} catch (err) {
|
|
|
|
assert.ok(err, 'error')
|
2017-03-30 23:23:23 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-02-11 17:51:13 +01:00
|
|
|
describe('#normalizeTxParams', function () {
|
|
|
|
it('should normalize txParams', function () {
|
2018-07-03 00:49:33 +02:00
|
|
|
const txParams = {
|
2018-04-06 20:07:20 +02:00
|
|
|
chainId: '0x1',
|
|
|
|
from: 'a7df1beDBF813f57096dF77FCd515f0B3900e402',
|
|
|
|
to: null,
|
|
|
|
data: '68656c6c6f20776f726c64',
|
|
|
|
random: 'hello world',
|
|
|
|
}
|
|
|
|
|
|
|
|
let normalizedTxParams = txUtils.normalizeTxParams(txParams)
|
|
|
|
|
2020-04-20 17:01:00 +02:00
|
|
|
assert.ok(!normalizedTxParams.chainId, 'there should be no chainId')
|
|
|
|
assert.ok(!normalizedTxParams.to, 'there should be no to address if null')
|
2020-11-03 00:41:28 +01:00
|
|
|
assert.equal(
|
|
|
|
normalizedTxParams.from.slice(0, 2),
|
|
|
|
'0x',
|
|
|
|
'from should be hex-prefixed',
|
|
|
|
)
|
|
|
|
assert.equal(
|
|
|
|
normalizedTxParams.data.slice(0, 2),
|
|
|
|
'0x',
|
|
|
|
'data should be hex-prefixed',
|
|
|
|
)
|
|
|
|
assert.ok(
|
|
|
|
!('random' in normalizedTxParams),
|
|
|
|
'there should be no random key in normalizedTxParams',
|
|
|
|
)
|
2018-04-06 20:07:20 +02:00
|
|
|
|
|
|
|
txParams.to = 'a7df1beDBF813f57096dF77FCd515f0B3900e402'
|
|
|
|
normalizedTxParams = txUtils.normalizeTxParams(txParams)
|
2020-11-03 00:41:28 +01:00
|
|
|
assert.equal(
|
|
|
|
normalizedTxParams.to.slice(0, 2),
|
|
|
|
'0x',
|
|
|
|
'to should be hex-prefixed',
|
|
|
|
)
|
2017-03-07 20:34:11 +01:00
|
|
|
})
|
2018-04-06 20:07:20 +02:00
|
|
|
})
|
2017-05-04 23:35:10 +02:00
|
|
|
|
2020-02-11 17:51:13 +01:00
|
|
|
describe('#validateRecipient', function () {
|
2018-04-06 20:07:20 +02:00
|
|
|
it('removes recipient for txParams with 0x when contract data is provided', function () {
|
2020-04-20 17:01:00 +02:00
|
|
|
const zeroRecipientDataTxParams = {
|
2018-04-06 20:07:20 +02:00
|
|
|
from: '0x1678a085c290ebd122dc42cba69373b5953b831d',
|
|
|
|
to: '0x',
|
|
|
|
data: 'bytecode',
|
|
|
|
}
|
2020-11-03 00:41:28 +01:00
|
|
|
const sanitizedTxParams = txUtils.validateRecipient(
|
|
|
|
zeroRecipientDataTxParams,
|
|
|
|
)
|
|
|
|
assert.deepEqual(
|
|
|
|
sanitizedTxParams,
|
|
|
|
{
|
|
|
|
from: '0x1678a085c290ebd122dc42cba69373b5953b831d',
|
|
|
|
data: 'bytecode',
|
|
|
|
},
|
|
|
|
'no recipient with 0x',
|
|
|
|
)
|
2017-03-08 07:28:02 +01:00
|
|
|
})
|
|
|
|
|
2018-04-06 20:07:20 +02:00
|
|
|
it('should error when recipient is 0x', function () {
|
|
|
|
const zeroRecipientTxParams = {
|
|
|
|
from: '0x1678a085c290ebd122dc42cba69373b5953b831d',
|
|
|
|
to: '0x',
|
|
|
|
}
|
2020-11-03 00:41:28 +01:00
|
|
|
assert.throws(
|
|
|
|
() => {
|
|
|
|
txUtils.validateRecipient(zeroRecipientTxParams)
|
|
|
|
},
|
|
|
|
Error,
|
|
|
|
'Invalid recipient address',
|
|
|
|
)
|
2017-03-08 07:28:02 +01:00
|
|
|
})
|
2017-03-07 20:34:11 +01:00
|
|
|
})
|
2018-04-06 20:07:20 +02:00
|
|
|
|
2020-02-11 17:51:13 +01:00
|
|
|
describe('#validateFrom', function () {
|
2018-04-06 20:07:20 +02:00
|
|
|
it('should error when from is not a hex string', function () {
|
|
|
|
// where from is undefined
|
|
|
|
const txParams = {}
|
2020-11-03 00:41:28 +01:00
|
|
|
assert.throws(
|
|
|
|
() => {
|
|
|
|
txUtils.validateFrom(txParams)
|
|
|
|
},
|
|
|
|
Error,
|
|
|
|
`Invalid from address ${txParams.from} not a string`,
|
|
|
|
)
|
2018-04-06 20:07:20 +02:00
|
|
|
|
|
|
|
// where from is array
|
|
|
|
txParams.from = []
|
2020-11-03 00:41:28 +01:00
|
|
|
assert.throws(
|
|
|
|
() => {
|
|
|
|
txUtils.validateFrom(txParams)
|
|
|
|
},
|
|
|
|
Error,
|
|
|
|
`Invalid from address ${txParams.from} not a string`,
|
|
|
|
)
|
2018-04-06 20:07:20 +02:00
|
|
|
|
|
|
|
// where from is a object
|
|
|
|
txParams.from = {}
|
2020-11-03 00:41:28 +01:00
|
|
|
assert.throws(
|
|
|
|
() => {
|
|
|
|
txUtils.validateFrom(txParams)
|
|
|
|
},
|
|
|
|
Error,
|
|
|
|
`Invalid from address ${txParams.from} not a string`,
|
|
|
|
)
|
2018-04-06 20:07:20 +02:00
|
|
|
|
|
|
|
// where from is a invalid address
|
|
|
|
txParams.from = 'im going to fail'
|
2020-11-03 00:41:28 +01:00
|
|
|
assert.throws(
|
|
|
|
() => {
|
|
|
|
txUtils.validateFrom(txParams)
|
|
|
|
},
|
|
|
|
Error,
|
|
|
|
`Invalid from address`,
|
|
|
|
)
|
2018-04-06 20:07:20 +02:00
|
|
|
|
|
|
|
// should run
|
2018-07-03 00:49:33 +02:00
|
|
|
txParams.from = '0x1678a085c290ebd122dc42cba69373b5953b831d'
|
2018-04-06 20:07:20 +02:00
|
|
|
txUtils.validateFrom(txParams)
|
2019-07-31 22:17:11 +02:00
|
|
|
})
|
2018-04-06 20:07:20 +02:00
|
|
|
})
|
2018-04-28 02:12:52 +02:00
|
|
|
})
|