Implement first eth hd derivation test (#858)
* implement first eth hd test * lint * added gerald test address * test now passes * started implementing sith local secp256k1 * Pass publicKey through on verify * Some test fixes * renamed tweakadd * added suggestions * remove logs and complete test for tweakAdd * lint * reviewed * solve linting problems * relocate hd package * Apply suggestions from code review Co-authored-by: Jaco Greeff <jacogr@gmail.com> * added asset for tolegacyseed * yarn.lock and linting * remove isUint8 util * fix tests and added tests * fixed test Co-authored-by: Jaco Greeff <jacogr@gmail.com>
This commit is contained in:
co-authored by
Jaco Greeff
parent
c7e474cdc6
commit
c46cd5030e
@@ -5,7 +5,8 @@ import type { Keypair, KeypairType } from '@polkadot/util-crypto/types';
|
||||
import type { KeyringInstance, KeyringOptions, KeyringPair, KeyringPair$Json, KeyringPair$JsonEncodingTypes, KeyringPair$Meta } from './types';
|
||||
|
||||
import { assert, hexToU8a, isHex, isUndefined, stringToU8a } from '@polkadot/util';
|
||||
import { base64Decode, decodeAddress, encodeAddress, keyExtractSuri, keyFromPath, mnemonicToLegacySeed, mnemonicToMiniSecret, naclKeypairFromSeed as naclFromSeed, schnorrkelKeypairFromSeed as schnorrkelFromSeed, secp256k1KeypairFromSeed as secp256k1FromSeed } from '@polkadot/util-crypto';
|
||||
import { base64Decode, decodeAddress, encodeAddress, ethereumEncode, keyExtractSuri, keyFromPath, mnemonicToLegacySeed, mnemonicToMiniSecret, naclKeypairFromSeed as naclFromSeed, schnorrkelKeypairFromSeed as schnorrkelFromSeed, secp256k1KeypairFromSeed as secp256k1FromSeed } from '@polkadot/util-crypto';
|
||||
import { HDKeyEth } from '@polkadot/util-crypto/hd';
|
||||
|
||||
import { DEV_PHRASE } from './defaults';
|
||||
import { createPair } from './pair';
|
||||
@@ -46,7 +47,7 @@ export class Keyring implements KeyringInstance {
|
||||
constructor (options: KeyringOptions = {}) {
|
||||
options.type = options.type || 'ed25519';
|
||||
|
||||
assert(options && ['ecdsa', 'ethereum', 'ed25519', 'sr25519'].includes(options.type || 'undefined'), `Expected a keyring type of either 'ed25519', 'sr25519' or 'ecdsa', found '${options.type}`);
|
||||
assert(options && ['ecdsa', 'ethereum', 'ed25519', 'sr25519'].includes(options.type || 'undefined'), `Expected a keyring type of either 'ed25519', 'sr25519', 'ethereum' or 'ecdsa', found '${options.type}`);
|
||||
|
||||
this.#pairs = new Pairs();
|
||||
this.#ss58 = options.ss58Format;
|
||||
@@ -172,7 +173,7 @@ export class Keyring implements KeyringInstance {
|
||||
const suri = _suri.startsWith('//')
|
||||
? `${DEV_PHRASE}${_suri}`
|
||||
: _suri;
|
||||
const { password, path, phrase } = keyExtractSuri(suri);
|
||||
const { derivePath, password, path, phrase } = keyExtractSuri(suri);
|
||||
let seed: Uint8Array;
|
||||
|
||||
if (isHex(phrase, 256)) {
|
||||
@@ -183,7 +184,7 @@ export class Keyring implements KeyringInstance {
|
||||
|
||||
if ([12, 15, 18, 21, 24].includes(parts.length)) {
|
||||
seed = type === 'ethereum'
|
||||
? mnemonicToLegacySeed(phrase)
|
||||
? mnemonicToLegacySeed(phrase, '', false, 64)
|
||||
: mnemonicToMiniSecret(phrase, password);
|
||||
} else {
|
||||
assert(str.length <= 32, 'specified phrase is not a valid mnemonic and is invalid as a raw seed at > 32 bytes');
|
||||
@@ -192,8 +193,18 @@ export class Keyring implements KeyringInstance {
|
||||
}
|
||||
}
|
||||
|
||||
// FIXME Need to support Ethereum-type derivation paths
|
||||
const derived = keyFromPath(keypairFromSeed[type](seed), path, type);
|
||||
let derived: Keypair;
|
||||
|
||||
if (type === 'ethereum') {
|
||||
const key = HDKeyEth.fromMasterSeed(seed);
|
||||
const child = key.derive(derivePath.substring(1));
|
||||
|
||||
assert(child.publicKey && child.privateKey, 'Unable to derive HD key from path');
|
||||
|
||||
derived = { publicKey: child.publicKey, secretKey: child.privateKey };
|
||||
} else {
|
||||
derived = keyFromPath(keypairFromSeed[type](seed), path, type);
|
||||
}
|
||||
|
||||
return createPair({ toSS58: this.encodeAddress, type }, derived, meta, null);
|
||||
}
|
||||
@@ -203,7 +214,11 @@ export class Keyring implements KeyringInstance {
|
||||
* @description Encodes the input into an ss58 representation
|
||||
*/
|
||||
public encodeAddress = (address: Uint8Array | string, ss58Format?: number): string => {
|
||||
return encodeAddress(address, isUndefined(ss58Format) ? this.#ss58 : ss58Format);
|
||||
if (this.type === 'ethereum') {
|
||||
return ethereumEncode(address);
|
||||
} else {
|
||||
return encodeAddress(address, isUndefined(ss58Format) ? this.#ss58 : ss58Format);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user