Skip to content

Commit

Permalink
Merge 2b43929 into 54c6650
Browse files Browse the repository at this point in the history
  • Loading branch information
nivida committed Apr 26, 2019
2 parents 54c6650 + 2b43929 commit 7dc299f
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 36 deletions.
14 changes: 13 additions & 1 deletion packages/web3-core-method/lib/methods/AbstractMethod.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,17 @@ export default class AbstractMethod {
this.beforeExecution(this.moduleInstance);

if (this.parameters.length !== this.parametersAmount) {
throw new Error(
const error = new Error(
`Invalid Arguments length: expected: ${this.parametersAmount}, given: ${this.parameters.length}`
);

if (this.callback) {
this.callback(error, null);

return;
}

throw error;
}

try {
Expand All @@ -93,12 +101,16 @@ export default class AbstractMethod {

if (this.callback) {
this.callback(false, response);

return;
}

return response;
} catch (error) {
if (this.callback) {
this.callback(error, null);

return;
}

throw error;
Expand Down
57 changes: 44 additions & 13 deletions packages/web3-core-method/tests/lib/methods/AbstractMethodTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,17 @@ jest.mock('web3-providers');
* AbstractMethod test
*/
describe('AbstractMethodTest', () => {
let abstractMethod, moduleInstanceMock, providerMock, callback;
let abstractMethod, moduleInstanceMock, providerMock;

beforeEach(() => {
callback = jest.fn();

new WebsocketProvider('host', {});
providerMock = WebsocketProvider.mock.instances[0];
providerMock.send = jest.fn();

moduleInstanceMock = {};

abstractMethod = new AbstractMethod('RPC_TEST', 0, Utils, formatters, moduleInstanceMock);
abstractMethod.callback = callback;
abstractMethod.callback = false;
abstractMethod.beforeExecution = jest.fn();
});

Expand All @@ -41,7 +39,7 @@ describe('AbstractMethodTest', () => {

expect(abstractMethod.parameters).toEqual([]);

expect(abstractMethod.callback).toEqual(callback);
expect(abstractMethod.callback).toEqual(false);
});

it('setArguments throws error on missing arguments', () => {
Expand Down Expand Up @@ -138,29 +136,47 @@ describe('AbstractMethodTest', () => {

expect(providerMock.send).toHaveBeenCalledWith(abstractMethod.rpcMethod, abstractMethod.parameters);

expect(abstractMethod.callback).toHaveBeenCalledWith(false, '0x00');

expect(abstractMethod.beforeExecution).toHaveBeenCalledWith(moduleInstanceMock);

expect(abstractMethod.afterExecution).toHaveBeenCalledWith('0x0');
});

it('calls execute and throws an error on sending the request to the connected node', async () => {
it('calls execute and returns a rejected promise on sending the request to the connected node', async () => {
providerMock.send = jest.fn(() => {
return Promise.reject(new Error('ERROR ON SEND'));
});

abstractMethod.callback = false;
moduleInstanceMock.currentProvider = providerMock;
await expect(abstractMethod.execute(moduleInstanceMock)).rejects.toThrow('ERROR ON SEND');

expect(providerMock.send).toHaveBeenCalledWith(abstractMethod.rpcMethod, abstractMethod.parameters);

expect(abstractMethod.callback).toHaveBeenCalledWith(new Error('ERROR ON SEND'), null);

expect(abstractMethod.beforeExecution).toHaveBeenCalledWith(moduleInstanceMock);
});

it('calls execute and throws an error because of a invalid parameters length', async () => {
it('calls execute and throws an error on sending the request to the connected node', (done) => {
providerMock.send = jest.fn(() => {
return Promise.reject(new Error('ERROR ON SEND'));
});

abstractMethod.callback = jest.fn((error, response) => {
expect(error).toEqual(new Error('ERROR ON SEND'));

expect(response).toEqual(null);

expect(providerMock.send).toHaveBeenCalledWith(abstractMethod.rpcMethod, abstractMethod.parameters);

expect(abstractMethod.beforeExecution).toHaveBeenCalledWith(moduleInstanceMock);

done();
});

moduleInstanceMock.currentProvider = providerMock;
abstractMethod.execute(moduleInstanceMock);
});

it('calls execute and returns a rejected promise because of a invalid parameters length', async () => {
abstractMethod.parametersAmount = 0;
abstractMethod.parameters = [true];

Expand All @@ -171,6 +187,23 @@ describe('AbstractMethodTest', () => {
expect(abstractMethod.beforeExecution).toHaveBeenCalledWith(moduleInstanceMock);
});

it('calls execute and throws an error because of a invalid parameters length', (done) => {
abstractMethod.parametersAmount = 0;
abstractMethod.parameters = [true];

abstractMethod.callback = jest.fn((error, response) => {
expect(error).toEqual(new Error('Invalid Arguments length: expected: 0, given: 1'));

expect(response).toEqual(null);

expect(abstractMethod.beforeExecution).toHaveBeenCalledWith(moduleInstanceMock);

done();
});

abstractMethod.execute(moduleInstanceMock);
});

it('calls execute and it returns null', async () => {
providerMock.send.mockReturnValueOnce(Promise.resolve(null));

Expand All @@ -182,8 +215,6 @@ describe('AbstractMethodTest', () => {

expect(providerMock.send).toHaveBeenCalledWith(abstractMethod.rpcMethod, abstractMethod.parameters);

expect(abstractMethod.callback).toHaveBeenCalledWith(false, null);

expect(abstractMethod.beforeExecution).toHaveBeenCalledWith(moduleInstanceMock);
});
});
4 changes: 4 additions & 0 deletions packages/web3-eth/src/methods/EthSignMethod.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,16 @@ export default class EthSignMethod extends SignMethod {

if (this.callback) {
this.callback(false, signedMessage);

return;
}

return signedMessage;
} catch (error) {
if (this.callback) {
this.callback(error, null);

return;
}

throw error;
Expand Down
50 changes: 43 additions & 7 deletions packages/web3-eth/tests/src/methods/EthSignMethodTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,20 @@ describe('EthSignMethodTest', () => {
formatters.inputSignFormatter.mockReturnValue('string');

method = new EthSignMethod(Utils, formatters, moduleInstanceMock);
method.callback = jest.fn();
method.parameters = ['nope', '0x0'];
});

it('constructor check', () => {
expect(method).toBeInstanceOf(SignMethod);
});

it('calls execute with wallets defined', async () => {
it('calls execute with wallets defined and returns a resolved Promise', async () => {
accountsMock.sign.mockReturnValueOnce('0x00');

const response = await method.execute();

expect(response).toEqual('0x00');

expect(method.callback).toHaveBeenCalledWith(false, '0x00');

expect(method.parameters[0]).toEqual('0x0');

expect(method.parameters[1]).toEqual('string');
Expand All @@ -57,7 +54,31 @@ describe('EthSignMethodTest', () => {
expect(accountsMock.sign).toHaveBeenCalledWith('string', '0x0');
});

it('calls execute with wallets defined but accounts.sign throws an error', async () => {
it('calls execute with wallets defined and the callback gets called', (done) => {
accountsMock.sign.mockReturnValueOnce('0x00');

method.callback = jest.fn((error, response) => {
expect(error).toEqual(false);

expect(response).toEqual('0x00');

expect(method.parameters[0]).toEqual('0x0');

expect(method.parameters[1]).toEqual('string');

expect(formatters.inputAddressFormatter).toHaveBeenCalledWith('0x0');

expect(formatters.inputSignFormatter).toHaveBeenCalledWith('nope');

expect(accountsMock.sign).toHaveBeenCalledWith('string', '0x0');

done();
});

method.execute();
});

it('calls execute with wallets defined but accounts.sign returns a rejected Promise', async () => {
const error = new Error('SIGN ERROR');
accountsMock.sign = jest.fn(() => {
throw error;
Expand All @@ -68,12 +89,27 @@ describe('EthSignMethodTest', () => {
} catch (error2) {
expect(error2).toEqual(error);

expect(method.callback).toHaveBeenCalledWith(error, null);

expect(accountsMock.sign).toHaveBeenCalledWith('string', '0x0');
}
});

it('calls execute with wallets defined but accounts.sign throws an error', (done) => {
const error = new Error('SIGN ERROR');
accountsMock.sign = jest.fn(() => {
throw error;
});

method.callback = jest.fn((error, response) => {
expect(error).toEqual(error);

expect(response).toEqual(null);

done();
});

method.execute();
});

it('calls execute and the account does not exist in the eth-accounts wallet', async () => {
accountsMock.wallet = {nope: {privateKey: '0x0'}};

Expand Down
18 changes: 9 additions & 9 deletions packages/web3-utils/src/Utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,9 @@ export const isAddress = (address, chainId = null) => {
*
* @returns {string} address without prefix
*/
export const stripHexPrefix = (str) => {
return str.slice(0, 2) === '0x' ? str.slice(2) : str
}
export const stripHexPrefix = (string) => {
return string.slice(0, 2) === '0x' ? string.slice(2) : string;
};

/**
* Checks if the given string is a checksummed address
Expand All @@ -138,14 +138,14 @@ export const stripHexPrefix = (str) => {
* @returns {Boolean}
*/
export const checkAddressChecksum = (address, chainId = null) => {
const stripAddress = stripHexPrefix(address).toLowerCase()
const prefix = chainId != null ? (chainId.toString() + '0x') : ''
const keccakHash = Hash.keccak256(prefix + stripAddress).toString('hex').replace(/^0x/i, '');
const stripAddress = stripHexPrefix(address).toLowerCase();
const prefix = chainId != null ? chainId.toString() + '0x' : '';
const keccakHash = Hash.keccak256(prefix + stripAddress)
.toString('hex')
.replace(/^0x/i, '');

for (let i = 0; i < stripAddress.length; i++) {
let output = parseInt(keccakHash[i], 16) >= 8 ?
stripAddress[i].toUpperCase() :
stripAddress[i];
let output = parseInt(keccakHash[i], 16) >= 8 ? stripAddress[i].toUpperCase() : stripAddress[i];
if (stripHexPrefix(address)[i] !== output) {
return false;
}
Expand Down
12 changes: 6 additions & 6 deletions packages/web3-utils/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,17 +256,17 @@ export const toChecksumAddress = (address, chainId = null) => {
throw new Error(`Given address "${address}" is not a valid Ethereum address.`);

const stripAddress = stripHexPrefix(address).toLowerCase();
const prefix = chainId != null ? (chainId.toString() + '0x') : '';
const keccakHash = Hash.keccak256(prefix + stripAddress).toString('hex').replace(/^0x/i, '');
const prefix = chainId != null ? chainId.toString() + '0x' : '';
const keccakHash = Hash.keccak256(prefix + stripAddress)
.toString('hex')
.replace(/^0x/i, '');
let checksumAddress = '0x';

for (let i = 0; i < stripAddress.length; i++)
checksumAddress += parseInt(keccakHash[i], 16) >= 8 ?
stripAddress[i].toUpperCase() :
stripAddress[i];
checksumAddress += parseInt(keccakHash[i], 16) >= 8 ? stripAddress[i].toUpperCase() : stripAddress[i];

return checksumAddress;
}
};

// aliases
export const keccak256 = utils.keccak256;
Expand Down

0 comments on commit 7dc299f

Please sign in to comment.