Update of Private EC Key to Public. We support ED25519 and more formats.

This commit is contained in:
David C Goldenberg 2026-07-18 19:48:25 -04:00
parent 323774b9ae
commit f28add3dbd
2 changed files with 45 additions and 9 deletions

View File

@ -14,6 +14,24 @@ import { validatePrivateKey, makeSureIsHex} from "../lib/Bitcoin.mjs";
// import { toHex } from "crypto-api/src/encoder/hex.mjs";
// const curves = ["secp256k1", "ed25519", "curve25519", "p521", "p384", "p256", "p224", "p192"];
const OUTPUT_OPTIONS = ["Public Key Only", "Private,Public", "Public,Private"];
/**
* Format the output based on the passed in option from OUTPUT_OPTIONS
* @param {*} public_key
* @param {*} private_key
* @param {*} option
* @returns
*/
function formatOutput(privateKey, publicKey, option) {
if (option === "Public Key Only") {
return publicKey;
} else if (option === "Private,Public") {
return privateKey+","+publicKey;
} else if (option === "Public,Private") {
return publicKey+","+privateKey;
}
}
/**
* Class that takes in a private key, and returns the public key, either in compressed or uncompressed form(s).
*/
@ -35,6 +53,16 @@ class PrivateECKeyToPublic extends Operation {
"name": "Compressed",
"type": "boolean",
"value": true
},
{
"name": "Curve",
"type": "option",
"value": ["secp256k1", "ed25519"]
},
{
"name": "Output Option",
"type": "option",
"value": OUTPUT_OPTIONS
}
];
this.checks = [
@ -67,11 +95,19 @@ class PrivateECKeyToPublic extends Operation {
throw new OperationError("Error with the input as private key. Error is:\n\t" + privKeyCheck);
}
const processedInput = makeSureIsHex(input);
const ecContext = ec.ec("secp256k1");
const key = ecContext.keyFromPrivate(processedInput);
const pubkey = key.getPublic(args[0], "hex");
return pubkey;
if (args[1] === "secp256k1") {
const ecContext = ec.ec("secp256k1");
const key = ecContext.keyFromPrivate(processedInput);
const pubkey = key.getPublic(args[0], "hex");
return formatOutput(processedInput, pubkey, args[2]);
} else if (args[1] === "ed25519") {
const ed = new ec.eddsa("ed25519");
const publicKeyHex = ed.keyFromSecret(processedInput).getPublic("hex");
return formatOutput(processedInput, publicKeyHex, args[2]);
}
}
}

View File

@ -17,7 +17,7 @@ TestRegister.addTests([
recipeConfig: [
{
"op": "Private EC Key to Public Key",
"args": [true],
"args": [true, "secp256k1", "Public Key Only"]
},
],
},
@ -28,7 +28,7 @@ TestRegister.addTests([
recipeConfig: [
{
"op": "Private EC Key to Public Key",
"args": [false],
"args": [false, "secp256k1", "Public Key Only"]
},
],
},
@ -43,7 +43,7 @@ TestRegister.addTests([
},
{
"op": "Private EC Key to Public Key",
"args": [true],
"args": [true, "secp256k1", "Public Key Only"]
},
],
},
@ -54,7 +54,7 @@ TestRegister.addTests([
recipeConfig: [
{
"op": "Private EC Key to Public Key",
"args": [false],
"args": [false, "secp256k1", "Public Key Only"]
},
],
},
@ -69,7 +69,7 @@ TestRegister.addTests([
},
{
"op": "Private EC Key to Public Key",
"args": [true],
"args": [false, "secp256k1", "Public Key Only"]
},
],
}