check description
+ key-derivation/getPairFromPrivate() M key-derivation/generateKeypair to use class Key + class Key for interop between different zkl modules
This commit is contained in:
63
key-derivation.js
Normal file
63
key-derivation.js
Normal file
@@ -0,0 +1,63 @@
|
||||
import {
|
||||
generateMnemonic as generateMnemonic_bip39,
|
||||
mnemonicToSeed
|
||||
} from "web-bip39";
|
||||
import wordlist from "web-bip39/wordlists/english";
|
||||
import elliptic from "elliptic";
|
||||
import { Key } from "./key";
|
||||
|
||||
const MNEMONIC_STRENGTH = 192;
|
||||
//const CURVE = "curve25519"
|
||||
const CURVE = "secp256k1";
|
||||
|
||||
/**
|
||||
* Generates a new key pair from a mnemonic.
|
||||
*
|
||||
* @param {string} mnemonic - The mnemonic to generate the key pair from.
|
||||
* @returns {Promise<{ publicKey: Key; privateKey: Key }>} A promise that resolves to an object containing the public and private keys.
|
||||
*/
|
||||
export async function generateKeypair(mnemonic) {
|
||||
const seed = (await mnemonicToSeed(mnemonic)).toString("hex");
|
||||
|
||||
const ec = new elliptic.ec(CURVE);
|
||||
const keypair = ec.genKeyPair({
|
||||
entropy: seed.slice(0, 32)
|
||||
});
|
||||
|
||||
return {
|
||||
publicKey: new Key("public", { fromHexString: keypair.getPublic("hex") }),
|
||||
privateKey: new Key("private", {
|
||||
fromHexString: keypair.getPrivate("hex")
|
||||
})
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a new mnemonic.
|
||||
*
|
||||
* @returns {string} The generated mnemonic.
|
||||
*/
|
||||
export function generateMnemonic() {
|
||||
return generateMnemonic_bip39(wordlist, MNEMONIC_STRENGTH);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a key pair from a private key.
|
||||
*
|
||||
* @param {Key} key - The private key to get the key pair from.
|
||||
* @returns {{ publicKey: Key; privateKey: Key }} An object containing the public and private keys.
|
||||
* @throws {TypeError} If the provided key is not a private key.
|
||||
*/
|
||||
export function getPairFromPrivate(key) {
|
||||
if (key.keyType != "private")
|
||||
throw new TypeError("Required property 'key' must be a private key");
|
||||
const ec = new elliptic.ec(CURVE);
|
||||
const keypair = ec.keyFromPrivate(key.asHexString);
|
||||
|
||||
return {
|
||||
publicKey: new Key("public", { fromHexString: keypair.getPublic("hex") }),
|
||||
privateKey: new Key("private", {
|
||||
fromHexString: keypair.getPrivate("hex")
|
||||
})
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user