Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zgorizzo69/4c16bd75288a5381228bb2b7c10a07c3 to your computer and use it in GitHub Desktop.
Save zgorizzo69/4c16bd75288a5381228bb2b7c10a07c3 to your computer and use it in GitHub Desktop.
from a priv key in hex to pem encoding and decoding back to hex for both public and private key using EC secp256k1
const jose = require("jose");
const ethers = require("ethers");
const {
JWE, // JSON Web Encryption (JWE)
JWK, // JSON Web Key (JWK)
JWKS, // JSON Web Key Set (JWKS)
JWS, // JSON Web Signature (JWS)
JWT, // JSON Web Token (JWT)
errors // errors utilized by jose
} = jose;
// Generate the private key base on secp256k1 elliptic curve
const privkeyGen = jose.JWK.generateSync("EC", "secp256k1");
const privkeyhex = Buffer.from(privkeyGen.toJWK(true).d, "base64").toString(
"hex"
);
// util function to create the jose JWK
function hex2base64url(dataHex) {
const buffer = Buffer.from(dataHex, "hex");
const base64 = buffer.toString("base64");
const base64url = base64
.replace(/\+/g, "-")
.replace(/\//g, "_")
.replace(/=/g, "");
return base64url;
}
// get an hex encoded secp256k1 EC private key to jose.JWK.ECKey
function getJWKfromHex(_privKey) {
let privKey = _privKey;
const s = new ethers.utils.SigningKey(privKey);
let pubKey = s.publicKey;
// remove 0x and 0x04 to be used in jose library
privKey = privKey.replace("0x", "");
pubKey = pubKey.replace("0x04", "");
return jose.JWK.asKey({
crv: "secp256k1",
kty: "EC",
d: hex2base64url(privKey),
x: hex2base64url(pubKey.substr(0, 64)),
y: hex2base64url(pubKey.substr(64, 64))
});
}
// extract and pem encode the publicKey from the private key(jose.JWK.ECKey)
function getPubKeyPem(privateKey) {
const pubKeyPEM = privateKey.toPEM(false);
return Buffer.from(pubKeyPEM, "utf8").toString();
}
// pem encode the private key (jose.JWK.ECKey)
function getPrivKeyPem(privateKey) {
const privKeyPEM = privateKey.toPEM(true);
return Buffer.from(privKeyPEM, "utf8").toString();
}
const key = getJWKfromHex(privkeyhex);
if (jose.JWK.isKey(key)) {
console.log(`-- ${JSON.stringify(key)} is a jwk.key `);
}
const ComputedPrivKey = Buffer.from(key.d, "base64").toString("hex");
if (ComputedPrivKey === privkeyhex) {
console.log(
`-- ComputedPrivKey:${ComputedPrivKey} equals to generated priv key ${privkeyhex} `
);
}
// using ethers to get compressed and long pubkey
const ethWallet = new ethers.Wallet(`0x${ComputedPrivKey}`);
const pubkeyhex = ethWallet.signingKey.publicKey;
const privkeypem = getPrivKeyPem(key);
const privkeyhexFromEthers = ethWallet.privateKey;
const pubkeypem = getPubKeyPem(key);
console.log(
`
-----------------------HEX-------------------
privkey :${privkeyhexFromEthers}
pubkey long:${pubkeyhex}
pubkey short:${ethWallet.signingKey.compressedPublicKey}
eth address:${ethWallet.address}
-----------------------PEM-------------------
${privkeypem}
${pubkeypem}
`
);
const computedPubkeyPem = JWK.asKey(pubkeypem);
const computedPubKeyHex = `${Buffer.from(
computedPubkeyPem.x,
"base64"
).toString("hex")}${Buffer.from(computedPubkeyPem.y, "base64").toString(
"hex"
)}`;
console.log(
` ---------MATCH PUB KEY ${pubkeyhex === `0x04${computedPubKeyHex}`}--------
${pubkeyhex}
0x04${computedPubKeyHex}
`
);
const computedPrivkeyPem = JWK.asKey(privkeypem);
const computedPrivKeyHex = Buffer.from(computedPrivkeyPem.d, "base64").toString(
"hex"
);
console.log(
` ---------MATCH PRIV KEY ${privkeyhexFromEthers ===
`0x${computedPrivKeyHex}`}--------
${privkeyhexFromEthers}
0x${computedPrivKeyHex}
`
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment