Jlm test from seed (#909)
* added gerald test address * add test for fromSeed * Update packages/util-crypto/src/secp256k1/keypair/fromSeed.spec.ts Co-authored-by: Jaco Greeff <jacogr@gmail.com> * coorected createfromuri to accept eth priv key * Update packages/keyring/src/keyring.ts Co-authored-by: Jaco Greeff <jacogr@gmail.com> * add isPhraeHex Co-authored-by: Jaco Greeff <jacogr@gmail.com>
This commit is contained in:
co-authored by
Jaco Greeff
parent
47b79675da
commit
04bbf28a57
@@ -306,6 +306,7 @@ describe('keypair', (): void => {
|
||||
describe('ethereum', (): void => {
|
||||
// combine mnemonic with derivation path
|
||||
const PHRASE = 'seed sock milk update focus rotate barely fade car face mechanic mercy' + '/m/44\'/60\'/0\'/0/0';
|
||||
const PRIV_KEY_ONE = '0x070dc3117300011918e26b02176945cc15c3d548cf49fd8418d97f93af699e46';
|
||||
const ETH_ADDRESS_ONE = '0x31ea8795EE32D782C8ff41a5C68Dcbf0F5B27f6d';
|
||||
|
||||
let keyring: Keyring;
|
||||
@@ -314,6 +315,22 @@ describe('keypair', (): void => {
|
||||
keyring = new Keyring({ type: 'ethereum' });
|
||||
});
|
||||
|
||||
it('creates with dev phrase from the private key', (): void => {
|
||||
const pair = keyring.addFromSeed(hexToU8a(PRIV_KEY_ONE));
|
||||
|
||||
expect(
|
||||
pair.address
|
||||
).toEqual(ETH_ADDRESS_ONE);
|
||||
});
|
||||
|
||||
it('creates with dev phrase from the private key in createFromUri', (): void => {
|
||||
const pair = keyring.createFromUri(PRIV_KEY_ONE);
|
||||
|
||||
expect(
|
||||
pair.address
|
||||
).toEqual(ETH_ADDRESS_ONE);
|
||||
});
|
||||
|
||||
it('creates with dev phrase with derivation path specified', (): void => {
|
||||
const pair = keyring.createFromUri(PHRASE);
|
||||
|
||||
|
||||
@@ -175,11 +175,12 @@ export class Keyring implements KeyringInstance {
|
||||
: _suri;
|
||||
const { derivePath, password, path, phrase } = keyExtractSuri(suri);
|
||||
let seed: Uint8Array;
|
||||
const isPhraseHex = isHex(phrase, 256);
|
||||
|
||||
if (isHex(phrase, 256)) {
|
||||
if (isPhraseHex) {
|
||||
seed = hexToU8a(phrase);
|
||||
} else {
|
||||
const str = phrase as string;
|
||||
const str = phrase;
|
||||
const parts = str.split(' ');
|
||||
|
||||
if ([12, 15, 18, 21, 24].includes(parts.length)) {
|
||||
@@ -194,7 +195,9 @@ export class Keyring implements KeyringInstance {
|
||||
}
|
||||
|
||||
const derived = type === 'ethereum'
|
||||
? hdEthereum(seed, derivePath.substring(1))
|
||||
? isPhraseHex
|
||||
? keypairFromSeed[type](seed) // for eth, if the private key is provided as suri, it must be derived only once
|
||||
: hdEthereum(seed, derivePath.substring(1))
|
||||
: keyFromPath(keypairFromSeed[type](seed), path, type);
|
||||
|
||||
return createPair({ toSS58: this.encodeAddress, type }, derived, meta, null);
|
||||
|
||||
Reference in New Issue
Block a user