Skip to content

Commit

Permalink
Merge branch '2.x' into contract-options-typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
nivida authored Jul 16, 2019
2 parents 0a0b2e7 + f2ac320 commit 22c5571
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 19 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [2.0.0-alpha.1]

### Added

- Length check of the PK added to the ``fromPrivateKey`` method of the ``Account`` model (#2928)

### Fixed

- miner.startMining fixed (#2877)
Expand Down
9 changes: 9 additions & 0 deletions packages/web3-eth-accounts/src/models/Account.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,15 @@ export default class Account {
* @returns {Account}
*/
static fromPrivateKey(privateKey, accounts = {}) {
if (!privateKey.startsWith('0x')) {
privateKey = '0x' + privateKey;
}

// 64 hex characters + hex-prefix
if (privateKey.length !== 66) {
throw new Error("Private key must be 32 bytes long");
}

return new Account(EthLibAccount.fromPrivate(privateKey), accounts);
}

Expand Down
49 changes: 30 additions & 19 deletions packages/web3-eth-accounts/tests/src/models/AccountTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ jest.mock('../../../src/Accounts');
* AccountTest test
*/
describe('AccountTest', () => {
let account, accountsMock, transactionSignerMock;
let account, accountsMock, transactionSignerMock, mockKey;

beforeEach(() => {
mockKey = '0x0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef';
transactionSignerMock = new TransactionSigner();

new Accounts();
Expand Down Expand Up @@ -55,6 +56,20 @@ describe('AccountTest', () => {
expect(accountsMock.signTransaction).toHaveBeenCalledWith({}, 'pk', callback);
});

it('calls fromPrivateKey with incorrect private key length and throws error', () => {
expect(() => {
Account.fromPrivateKey('asdfasdf')
}).toThrow('Private key must be 32 bytes long');
});

it('calls fromPrivateKey with incorrect private key prefix and throws error', () => {
mockKey = '0z0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef';

expect(() => {
Account.fromPrivateKey(mockKey)
}).toThrow('Private key must be 32 bytes long');
});

it('calls sign with non-strict hex and returns the expected string', () => {
isHexStrict.mockReturnValue(false);

Expand Down Expand Up @@ -152,17 +167,15 @@ describe('AccountTest', () => {
final: jest.fn()
};

decipher.update.mockReturnValueOnce(Buffer.from('0'));
decipher.update.mockReturnValueOnce(Buffer.from(mockKey.slice(2,34), 'hex'));

decipher.final.mockReturnValueOnce(Buffer.from('0'));
decipher.final.mockReturnValueOnce(Buffer.from(mockKey.slice(34,66), 'hex'));

createDecipheriv.mockReturnValueOnce(decipher);

expect(Account.fromV3Keystore(json, 'password', false)).toBeInstanceOf(Account);

expect(fromPrivate).toHaveBeenLastCalledWith(
`0x${Buffer.concat([Buffer.from('0'), Buffer.from('0')]).toString('hex')}`
);
expect(fromPrivate).toHaveBeenLastCalledWith(mockKey);

expect(scrypt).toHaveBeenCalledWith(
Buffer.from('password'),
Expand Down Expand Up @@ -220,19 +233,17 @@ describe('AccountTest', () => {
final: jest.fn()
};

decipher.update.mockReturnValueOnce(Buffer.from('0'));
decipher.update.mockReturnValueOnce(Buffer.from(mockKey.slice(2,34), 'hex'));

decipher.final.mockReturnValueOnce(Buffer.from('0'));
decipher.final.mockReturnValueOnce(Buffer.from(mockKey.slice(34,66), 'hex'));

createDecipheriv.mockReturnValueOnce(decipher);

pbkdf2Sync.mockReturnValueOnce(Buffer.from('00000000000000000000000000000000'));

expect(Account.fromV3Keystore(json, 'password', false)).toBeInstanceOf(Account);

expect(fromPrivate).toHaveBeenCalledWith(
`0x${Buffer.concat([Buffer.from('0'), Buffer.from('0')]).toString('hex')}`
);
expect(fromPrivate).toHaveBeenCalledWith(mockKey);

expect(pbkdf2Sync).toHaveBeenCalledWith(
Buffer.from('password'),
Expand Down Expand Up @@ -349,7 +360,7 @@ describe('AccountTest', () => {

uuid.v4.mockReturnValueOnce(0);

expect(Account.fromPrivateKey('pk').toV3Keystore('password', options)).toEqual({
expect(Account.fromPrivateKey(mockKey).toV3Keystore('password', options)).toEqual({
version: 3,
id: 0,
address: 'a',
Expand All @@ -369,7 +380,7 @@ describe('AccountTest', () => {
}
});

expect(fromPrivate).toHaveBeenCalledWith('pk');
expect(fromPrivate).toHaveBeenCalledWith(mockKey);

expect(randomBytes).toHaveBeenNthCalledWith(1, 32);

Expand Down Expand Up @@ -426,7 +437,7 @@ describe('AccountTest', () => {

uuid.v4.mockReturnValueOnce(0);

expect(Account.fromPrivateKey('pk').toV3Keystore('password', options)).toEqual({
expect(Account.fromPrivateKey(mockKey).toV3Keystore('password', options)).toEqual({
version: 3,
id: 0,
address: 'a',
Expand All @@ -445,7 +456,7 @@ describe('AccountTest', () => {
}
});

expect(fromPrivate).toHaveBeenCalledWith('pk');
expect(fromPrivate).toHaveBeenCalledWith(mockKey);

expect(randomBytes).toHaveBeenNthCalledWith(1, 32);

Expand Down Expand Up @@ -484,10 +495,10 @@ describe('AccountTest', () => {
randomBytes.mockReturnValue(Buffer.from('random'));

expect(() => {
Account.fromPrivateKey('pk').toV3Keystore('password', {kdf: 'nope'});
Account.fromPrivateKey(mockKey).toV3Keystore('password', {kdf: 'nope'});
}).toThrow('Unsupported kdf');

expect(fromPrivate).toHaveBeenCalledWith('pk');
expect(fromPrivate).toHaveBeenCalledWith(mockKey);

expect(randomBytes).toHaveBeenNthCalledWith(1, 32);

Expand All @@ -511,10 +522,10 @@ describe('AccountTest', () => {
keccak256.mockReturnValueOnce('0xmac');

expect(() => {
Account.fromPrivateKey('pk').toV3Keystore('password', options);
Account.fromPrivateKey(mockKey).toV3Keystore('password', options);
}).toThrow('Unsupported cipher');

expect(fromPrivate).toHaveBeenCalledWith('pk');
expect(fromPrivate).toHaveBeenCalledWith(mockKey);

expect(randomBytes).toHaveBeenNthCalledWith(1, 32);

Expand Down

0 comments on commit 22c5571

Please sign in to comment.