-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Accounts Creation - 4.x rewrite (#4577)
* started creating a few functions for account create * updating packages * adding create method for accounts * updated toCheckSumAddress and added testcases * added create and fromprivate functions for account * removing keythereum and implementing privatekeys with ethereum-cryptography * from private function complete and few tests added * cleaning up and updating changelog * rename fromPrivate to privateKeyToAccount * linter * fixing lint warnings * addressing feedback * fixing up * fixing function name * addressing feedback * addressing feedback * update changelog * fixing testcases
- Loading branch information
Alex
authored
Dec 6, 2021
1 parent
c919c9b
commit 9d7ef2f
Showing
22 changed files
with
276 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { utils, getPublicKey } from 'ethereum-cryptography/secp256k1'; | ||
import { | ||
toChecksumAddress, | ||
bytesToHex, | ||
sha3Raw, | ||
HexString, | ||
isBuffer, | ||
isValidString, | ||
} from 'web3-utils'; | ||
import { InvalidPrivateKeyError, PrivateKeyLengthError } from 'web3-common'; | ||
|
||
// TODO Will be added later | ||
export const encrypt = (): boolean => true; | ||
|
||
// TODO Will be added later | ||
export const sign = (): boolean => true; | ||
|
||
// TODO Will be added later | ||
export const signTransaction = (): boolean => true; | ||
|
||
/** | ||
* Get account from private key | ||
*/ | ||
export const privateKeyToAccount = ( | ||
privateKey: string | Buffer, | ||
): { | ||
address: string; | ||
privateKey: string; | ||
signTransaction: () => boolean; // From 1.x | ||
sign: () => boolean; | ||
encrypt: () => boolean; | ||
} => { | ||
if (!(isValidString(privateKey) || isBuffer(privateKey))) { | ||
throw new InvalidPrivateKeyError(privateKey); | ||
} | ||
|
||
const stringPrivateKey = Buffer.isBuffer(privateKey) | ||
? Buffer.from(privateKey).toString('hex') | ||
: privateKey; | ||
|
||
const stringPrivateKeyNoPrefix = stringPrivateKey.startsWith('0x') | ||
? stringPrivateKey.slice(2) | ||
: stringPrivateKey; | ||
|
||
// TODO Replace with isHexString32Bytes function in web3-eth PR: | ||
// Must be 64 hex characters | ||
if (stringPrivateKeyNoPrefix.length !== 64) { | ||
throw new PrivateKeyLengthError(stringPrivateKeyNoPrefix); | ||
} | ||
|
||
const publicKey = getPublicKey(stringPrivateKeyNoPrefix); | ||
|
||
const publicKeyString = `0x${publicKey.slice(2)}`; | ||
const publicHash = sha3Raw(publicKeyString); | ||
const publicHashHex = bytesToHex(publicHash); | ||
const address = toChecksumAddress(publicHashHex.slice(-40)); // To get the address, take the last 20 bytes of the public hash | ||
return { address, privateKey: stringPrivateKey, signTransaction, sign, encrypt }; | ||
}; | ||
|
||
/** | ||
* Returns an acoount | ||
*/ | ||
export const create = (): { | ||
address: HexString; | ||
privateKey: string; | ||
signTransaction: () => boolean; // From 1.x | ||
sign: () => boolean; | ||
encrypt: () => boolean; | ||
} => { | ||
const privateKey = utils.randomPrivateKey(); | ||
const address = getPublicKey(privateKey); | ||
return { | ||
privateKey: `0x${Buffer.from(privateKey).toString('hex')}`, | ||
address: `0x${Buffer.from(address).toString('hex')}`, | ||
signTransaction, | ||
sign, | ||
encrypt, | ||
}; | ||
}; |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './account'; |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { sign, signTransaction, encrypt } from '../../src/account'; | ||
|
||
export const validPrivateKeytoAccountData: [string, any][] = [ | ||
[ | ||
'0x348ce564d427a3311b6536bbcff9390d69395b06ed6c486954e971d960fe8709', | ||
{ | ||
address: '0xb8CE9ab6943e0eCED004cDe8e3bBed6568B2Fa01', | ||
privateKey: '0x348ce564d427a3311b6536bbcff9390d69395b06ed6c486954e971d960fe8709', | ||
sign, | ||
signTransaction, | ||
encrypt, | ||
}, | ||
], | ||
[ | ||
'0x9e93921f9bca358a96aa66efcccbde12850473be95f63c1453e29656feafeb35', | ||
{ | ||
address: '0x118C2E5F57FD62C2B5b46a5ae9216F4FF4011a07', | ||
privateKey: '0x9e93921f9bca358a96aa66efcccbde12850473be95f63c1453e29656feafeb35', | ||
sign, | ||
signTransaction, | ||
encrypt, | ||
}, | ||
], | ||
]; | ||
|
||
export const invalidPrivateKeytoAccountData: [any, string][] = [ | ||
['', 'Invalid value given "". Error: Private key must be 32 bytes.'], | ||
[Buffer.from([]), 'Invalid value given "". Error: Private key must be 32 bytes.'], | ||
[undefined, 'Invalid value given "undefined". Error: not a valid string or buffer.'], | ||
[null, 'Invalid value given "null". Error: not a valid string or buffer.'], | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { isHexStrict } from 'web3-utils'; | ||
import { create, privateKeyToAccount } from '../../src/account'; | ||
import { validPrivateKeytoAccountData, invalidPrivateKeytoAccountData } from '../fixtures/account'; | ||
|
||
describe('accounts', () => { | ||
describe('create', () => { | ||
describe('valid cases', () => { | ||
it('%s', () => { | ||
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'); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('privateKeyToAccount', () => { | ||
describe('valid cases', () => { | ||
it.each(validPrivateKeytoAccountData)('%s', (input, output) => { | ||
expect(privateKeyToAccount(input)).toEqual(output); | ||
}); | ||
}); | ||
describe('invalid cases', () => { | ||
it.each(invalidPrivateKeytoAccountData)('%s', (input, output) => { | ||
expect(() => privateKeyToAccount(input)).toThrow(output); | ||
}); | ||
}); | ||
}); | ||
}); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { randomBytes as cryptoRandomBytes } from 'crypto'; | ||
|
||
/** | ||
* Returns a random byte array by the given bytes size | ||
* | ||
* @param {Number} size | ||
* @returns {Buffer} | ||
*/ | ||
export const randomBytes = (byteSize: number): Buffer => { | ||
const randomValues = | ||
typeof window !== 'undefined' && window.crypto && window.crypto.getRandomValues | ||
? window.crypto.getRandomValues(new Uint8Array(byteSize)) | ||
: cryptoRandomBytes(byteSize); | ||
return Buffer.from(randomValues); | ||
}; | ||
|
||
|
||
/** | ||
* Returns a random hex string by the given bytes size | ||
* | ||
* @param {Number} size | ||
* @returns {string} | ||
*/ | ||
export const randomHex = (byteSize: number): string => `0x${randomBytes(byteSize).toString('hex')}`; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const randomHexData: number[] = [2,4,8,16,32,64,128,256] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.