Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accounts Creation - 4.x rewrite #4577

Merged
merged 20 commits into from
Dec 6, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
addressing feedback
  • Loading branch information
Alex committed Dec 2, 2021

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit 225ef71974ecc5efdc76597eeed46c0ef010ebf7
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -344,3 +344,4 @@ Released with 1.0.0-beta.37 code base.
### web3-eth-accounts

1. `create` function does not take in the optional parameter `entropy`
luu-alex marked this conversation as resolved.
Show resolved Hide resolved
2. `ignoreLength` will be removed as an optional parameter for `privateKeyToAccount`
2 changes: 1 addition & 1 deletion packages/web3-eth-accounts/package.json
Original file line number Diff line number Diff line change
@@ -40,6 +40,6 @@
},
"dependencies": {
"ethereum-cryptography": "^0.2.0",
"web3-utils": "4.0.0-alpha.0"
"web3-utils": "^4.0.0-alpha.0"
}
}
22 changes: 14 additions & 8 deletions packages/web3-eth-accounts/src/account.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { utils, getPublicKey } from 'ethereum-cryptography/secp256k1';
import { toChecksumAddress, bytesToHex, sha3Raw } from 'web3-utils';
import { PrivateKeyError } from './errors';
import { toChecksumAddress, bytesToHex, sha3Raw, HexString } from 'web3-utils';
import { InvalidPrivateKeyError, PrivateKeyLengthError } from './errors';
// Will be added later
export const encrypt = (): boolean => true;

@@ -14,24 +14,30 @@ export const signTransaction = (): boolean => true;
* Get address from private key
*/
export const privateKeyToAccount = (
luu-alex marked this conversation as resolved.
Show resolved Hide resolved
privateKey: string | Uint8Array,
privateKey: string | Buffer,
): {
address: string;
privateKey: string;
signTransaction: () => boolean; // From 1.x
sign: () => boolean;
encrypt: () => boolean;
} => {
if (!privateKey) {
throw new InvalidPrivateKeyError(privateKey);
}
luu-alex marked this conversation as resolved.
Show resolved Hide resolved

const stringPrivateKey =
luu-alex marked this conversation as resolved.
Show resolved Hide resolved
typeof privateKey === 'object' ? Buffer.from(privateKey).toString('hex') : privateKey;
luu-alex marked this conversation as resolved.
Show resolved Hide resolved

const updatedKey = stringPrivateKey.startsWith('0x')
luu-alex marked this conversation as resolved.
Show resolved Hide resolved
? stringPrivateKey.slice(2)
: stringPrivateKey;

// Must be 64 hex characters
luu-alex marked this conversation as resolved.
Show resolved Hide resolved
if (updatedKey.length !== 64) {
throw new PrivateKeyError(updatedKey);
throw new PrivateKeyLengthError(updatedKey);
}

const publicKey = getPublicKey(updatedKey);

const publicKeyString = `0x${publicKey.slice(2)}`;
luu-alex marked this conversation as resolved.
Show resolved Hide resolved
@@ -45,11 +51,11 @@ export const privateKeyToAccount = (
* Returns a random hex string by the given bytes size
*/
export const create = (): {
address: string;
address: HexString;
privateKey: string;
signTransaction: Function; // From 1.x
sign: Function;
encrypt: Function;
signTransaction: () => boolean; // From 1.x
sign: () => boolean;
encrypt: () => boolean;
} => {
const privateKey = utils.randomPrivateKey();
const address = getPublicKey(privateKey);
8 changes: 7 additions & 1 deletion packages/web3-eth-accounts/src/errors.ts
Original file line number Diff line number Diff line change
@@ -14,8 +14,14 @@ export abstract class Web3Error extends Error {
}
}

export class PrivateKeyError extends Web3Error {
export class PrivateKeyLengthError extends Web3Error {
public constructor(value: string) {
super(value, 'Private key must be 32 bytes long');
}
}

export class InvalidPrivateKeyError extends Web3Error {
public constructor(value: string) {
super(value, 'not a valid string or buffer');
}
}
1 change: 0 additions & 1 deletion packages/web3-eth-accounts/src/types.ts

This file was deleted.

7 changes: 7 additions & 0 deletions packages/web3-eth-accounts/test/fixtures/account.ts
Original file line number Diff line number Diff line change
@@ -22,3 +22,10 @@ export const validPrivateKeytoAccountData: [string, any][] = [
},
],
];

export const invalidPrivateKeytoAccountData: [any, string][] = [
['', 'Invalid value given "". Error: not a valid string or buffer.'],
[Buffer.from([]), 'Invalid value given "". Error: Private key must be 32 bytes long.'],
[undefined, 'Invalid value given "undefined". Error: not a valid string or buffer.'],
[null, 'Invalid value given "null". Error: not a valid string or buffer.'],
];
23 changes: 13 additions & 10 deletions packages/web3-eth-accounts/test/unit/account.test.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
import { isHexStrict } from 'web3-utils';
import { create, privateKeyToAccount } from '../../src/account';
import { validPrivateKeytoAccountData } from '../fixtures/account';
import { validPrivateKeytoAccountData, invalidPrivateKeytoAccountData } from '../fixtures/account';

describe('accounts', () => {
describe('create', () => {
describe('valid cases', () => {
it('%s', () => {
for (let i = 0; i < 10; i += 1) {
const account = create();
expect(typeof account.privateKey).toBe('string');
expect(typeof account.address).toBe('string');
expect(isHexStrict(account.address)).toBe(true);
expect(typeof account.encrypt).toBe('function');
expect(typeof account.sign).toBe('function');
expect(typeof account.signTransaction).toBe('function');
}
const account = create();
expect(typeof account.privateKey).toBe('string');
expect(typeof account.address).toBe('string');
expect(isHexStrict(account.address)).toBe(true);
expect(typeof account.encrypt).toBe('function');
expect(typeof account.sign).toBe('function');
expect(typeof account.signTransaction).toBe('function');
});
});
});
@@ -25,5 +23,10 @@ describe('accounts', () => {
expect(privateKeyToAccount(input)).toEqual(output);
});
});
describe('invalid cases', () => {
it.each(invalidPrivateKeytoAccountData)('%s', (input, output) => {
expect(() => privateKeyToAccount(input)).toThrow(output);
});
});
});
});