diff --git a/src/index.js b/src/index.js index a70da488..b5cae3ec 100755 --- a/src/index.js +++ b/src/index.js @@ -675,7 +675,7 @@ async function handleJavascriptPort(arg) { case "generateKeys": { const userLang = getUserLanguage(); const [randomWords, hexRandomWords] = mnemonic.generateRandom(userLang); - const privateKey = ecc.seedPrivate(hexRandomWords); + const privateKey = mnemonic.seedPrivate(hexRandomWords); const publicKey = ecc.privateToPublic(privateKey); return { @@ -690,7 +690,7 @@ async function handleJavascriptPort(arg) { } case "getAccountFrom12Words": { const { passphrase } = arg.data; - const privateKey = ecc.seedPrivate(mnemonic.toSeedHex(passphrase)); + const privateKey = mnemonic.seedPrivate(mnemonic.toSeedHex(passphrase)); if (!ecc.isValidPrivate(privateKey)) { return { error: "error.invalidKey" }; diff --git a/src/scripts/mnemonic.js b/src/scripts/mnemonic.js index ef98bd3c..00f6ac6b 100755 --- a/src/scripts/mnemonic.js +++ b/src/scripts/mnemonic.js @@ -1,4 +1,6 @@ import * as bip39 from 'bip39' +import { sha256, PrivateKey } from 'eosjs/dist/eosjs-key-conversions' +import { KeyType, privateKeyToString } from 'eosjs/dist/eosjs-numeric' /*** * Generates a random mnemonic @@ -28,6 +30,21 @@ function toSeedHex (mnemonic) { return bip39.mnemonicToSeedSync(mnemonic).toString('hex') } +/** + * Derive an EOS private key from a seed string. + * + * Replaces ecc.seedPrivate, which is a no-op stub in eosjs-ecc-migration + * (it only logs "Method deprecated" and returns undefined). Reproduces the + * legacy eosjs-ecc behaviour exactly: PrivateKey(sha256(seed)), returned as a + * legacy WIF string, so keys match what existing accounts registered with. + * @param {string} seed + * @returns {string} legacy WIF private key + */ +function seedPrivate (seed) { + const data = Uint8Array.from(sha256(seed)) + return PrivateKey.fromString(privateKeyToString({ type: KeyType.k1, data })).toLegacyString() +} + // ========================================= // Exports // ========================================= @@ -35,4 +52,5 @@ function toSeedHex (mnemonic) { export default { generateRandom, toSeedHex, + seedPrivate, }