1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-25 11:28:51 +01:00
metamask-extension/test/unit/app/controllers/transactions/tx-utils.test.js

178 lines
5.2 KiB
JavaScript
Raw Normal View History

import { strict as assert } from 'assert';
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 () {
describe('#validateTxParams', function () {
it('does not throw for positive values', function () {
const sample = {
from: '0x1678a085c290ebd122dc42cba69373b5953b831d',
to: '0xc42edfcc21ed14dda456aa0756c153f7985d8813',
value: '0x01',
};
txUtils.validateTxParams(sample);
});
2017-03-07 20:34:11 +01:00
it('throws for invalid params value', function () {
assert.throws(() => txUtils.validateTxParams(), {
message: 'Invalid transaction params: must be an object.',
});
assert.throws(() => txUtils.validateTxParams(null), {
message: 'Invalid transaction params: must be an object.',
});
assert.throws(() => txUtils.validateTxParams(true), {
message: 'Invalid transaction params: must be an object.',
});
assert.throws(() => txUtils.validateTxParams([]), {
message: 'Invalid transaction params: must be an object.',
});
});
it('throws for missing "to" and "data"', function () {
const sample = {
from: '0x1678a085c290ebd122dc42cba69373b5953b831d',
value: '0x01',
};
assert.throws(() => txUtils.validateTxParams(sample), {
message:
'Invalid transaction params: must specify "data" for contract deployments, or "to" (and optionally "data") for all other types of transactions.',
});
});
it('throws for negative values', function () {
const sample = {
from: '0x1678a085c290ebd122dc42cba69373b5953b831d',
to: '0xc42edfcc21ed14dda456aa0756c153f7985d8813',
value: '-0x01',
};
assert.throws(() => txUtils.validateTxParams(sample), {
message: 'Invalid transaction value "-0x01": not a positive number.',
});
});
});
2017-03-30 23:23:23 +02:00
describe('#normalizeTxParams', function () {
it('should normalize txParams', function () {
2018-07-03 00:49:33 +02:00
const txParams = {
chainId: '0x1',
from: 'a7df1beDBF813f57096dF77FCd515f0B3900e402',
to: null,
data: '68656c6c6f20776f726c64',
random: 'hello world',
};
let normalizedTxParams = txUtils.normalizeTxParams(txParams);
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',
);
2020-11-03 00:41:28 +01:00
assert.equal(
normalizedTxParams.data.slice(0, 2),
'0x',
'data should be hex-prefixed',
);
2020-11-03 00:41:28 +01:00
assert.ok(
!('random' in normalizedTxParams),
'there should be no random key in normalizedTxParams',
);
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-05-04 23:35:10 +02:00
describe('#validateRecipient', function () {
it('removes recipient for txParams with 0x when contract data is provided', function () {
const zeroRecipientDataTxParams = {
from: '0x1678a085c290ebd122dc42cba69373b5953b831d',
to: '0x',
data: 'bytecode',
};
2020-11-03 00:41:28 +01:00
const sanitizedTxParams = txUtils.validateRecipient(
zeroRecipientDataTxParams,
);
2020-11-03 00:41:28 +01:00
assert.deepEqual(
sanitizedTxParams,
{
from: '0x1678a085c290ebd122dc42cba69373b5953b831d',
data: 'bytecode',
},
'no recipient with 0x',
);
});
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);
2020-11-03 00:41:28 +01:00
},
Error,
'Invalid recipient address',
);
});
});
describe('#validateFrom', function () {
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);
2020-11-03 00:41:28 +01:00
},
Error,
`Invalid from address ${txParams.from} not a string`,
);
// where from is array
txParams.from = [];
2020-11-03 00:41:28 +01:00
assert.throws(
() => {
txUtils.validateFrom(txParams);
2020-11-03 00:41:28 +01:00
},
Error,
`Invalid from address ${txParams.from} not a string`,
);
// where from is a object
txParams.from = {};
2020-11-03 00:41:28 +01:00
assert.throws(
() => {
txUtils.validateFrom(txParams);
2020-11-03 00:41:28 +01:00
},
Error,
`Invalid from address ${txParams.from} not a string`,
);
// 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);
2020-11-03 00:41:28 +01:00
},
Error,
`Invalid from address`,
);
// should run
txParams.from = '0x1678a085c290ebd122dc42cba69373b5953b831d';
txUtils.validateFrom(txParams);
});
});
});