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:
parent
852a79220d
commit
ca521e033f
7
.prettierrc
Normal file
7
.prettierrc
Normal file
@ -0,0 +1,7 @@
|
||||
{
|
||||
"semi": true,
|
||||
"singleQuote": false,
|
||||
"tabWidth": 2,
|
||||
"bracketSpacing": true,
|
||||
"trailingComma": "none"
|
||||
}
|
2
dist/bundle.js
vendored
2
dist/bundle.js
vendored
File diff suppressed because one or more lines are too long
18
index.js
18
index.js
@ -1,11 +1,21 @@
|
||||
import { generateKeyPair, generateMnemonic } from "./key-derivation.service.js";
|
||||
import {
|
||||
generateKeyPair,
|
||||
generateMnemonic,
|
||||
newGenKeypair
|
||||
} from "./key-derivation.js";
|
||||
|
||||
(async () => {
|
||||
const mnemonic = await generateMnemonic();
|
||||
console.log(mnemonic);
|
||||
// const mnemonic = await generateMnemonic();
|
||||
// console.log(mnemonic);
|
||||
console.log("start");
|
||||
|
||||
const mnemonic =
|
||||
"digital radio analyst fine casino have mass blood potato hat web capital prefer debate fee differ spray cloud";
|
||||
|
||||
const { publicKey, privateKey } = await generateKeyPair(mnemonic);
|
||||
console.log(publicKey, privateKey);
|
||||
console.log(publicKey.keyType, privateKey.keyType);
|
||||
console.log(publicKey.asHexString, privateKey.asHexString);
|
||||
console.log(publicKey.asByteArray, privateKey.asByteArray);
|
||||
|
||||
console.log("done");
|
||||
})();
|
||||
|
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")
|
||||
})
|
||||
};
|
||||
}
|
@ -1,27 +0,0 @@
|
||||
import {
|
||||
generateMnemonic as generateMnemonic_bip39,
|
||||
mnemonicToSeed,
|
||||
} from "web-bip39";
|
||||
import wordlist from "web-bip39/wordlists/english";
|
||||
import elliptic from "elliptic";
|
||||
|
||||
const MNEMONIC_STRENGTH = 192;
|
||||
const CURVE = "curve25519"
|
||||
|
||||
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: keyPair.getPublic("hex"),
|
||||
privateKey: keyPair.getPrivate("hex"),
|
||||
}
|
||||
}
|
||||
|
||||
export function generateMnemonic() {
|
||||
return generateMnemonic_bip39(wordlist, MNEMONIC_STRENGTH);
|
||||
}
|
62
key.js
Normal file
62
key.js
Normal file
@ -0,0 +1,62 @@
|
||||
/**
|
||||
* Represents a cryptographic key.
|
||||
*/
|
||||
export class Key {
|
||||
#key = new Uint8Array(1);
|
||||
|
||||
/**
|
||||
* Creates a new Key instance.
|
||||
*
|
||||
* @param {string} type - The type of key, either "public" or "private".
|
||||
* @param {Object} details - Details about the key.
|
||||
* @param {Uint8Array} [details.fromByteArray] - The key as a byte array.
|
||||
* @param {string} [details.fromHexString] - The key as a hexadecimal string.
|
||||
* @throws {TypeError} If the type is not "public" or "private", or if details are provided but do not contain a valid key representation.
|
||||
*/
|
||||
constructor(type, details) {
|
||||
if (type.toLowerCase() !== "public" && type.toLowerCase() !== "private")
|
||||
throw new TypeError(
|
||||
"Required property 'type' may only take 'public' or 'private' as values"
|
||||
);
|
||||
else this.keyType = type;
|
||||
|
||||
if (!details || (!details.fromByteArray && !details.fromHexString))
|
||||
throw new TypeError(
|
||||
"Missing required property 'fromByteArray' or 'fromHexString' in parameter 'details'"
|
||||
);
|
||||
|
||||
if (details.fromByteArray && details.fromHexString)
|
||||
console.warn(
|
||||
"Both 'fromByteArray' and 'fromHexString' present. Value of 'fromHexString' will be used."
|
||||
);
|
||||
|
||||
if (details.fromByteArray) {
|
||||
this.#key = details.fromByteArray;
|
||||
} else if (details.fromHexString) {
|
||||
this.#key = Uint8Array.from(
|
||||
details.fromHexString.match(/.{1,2}/g).map((byte) => parseInt(byte, 16))
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the key as a hexadecimal string.
|
||||
*
|
||||
* @returns {string} The key as a hexadecimal string.
|
||||
*/
|
||||
get asHexString() {
|
||||
return this.#key.reduce(
|
||||
(str, byte) => str + byte.toString(16).padStart(2, "0"),
|
||||
""
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the key as a byte array.
|
||||
*
|
||||
* @returns {Uint8Array} The key as a byte array.
|
||||
*/
|
||||
get asByteArray() {
|
||||
return this.#key;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user