Skip to content

Commit

Permalink
Remove unnecessary conversions to hex format (#643)
Browse files Browse the repository at this point in the history
Signed-off-by: Frank Hinek <[email protected]>
  • Loading branch information
frankhinek authored Dec 6, 2023
1 parent bf1f40a commit b8901e9
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 11 deletions.
7 changes: 2 additions & 5 deletions src/jose/algorithms/signing/ed25519.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,9 @@ export const ed25519: SignatureAlgorithm = {
sign: async (content: Uint8Array, privateJwk: PrivateJwk): Promise<Uint8Array> => {
validateKey(privateJwk);

const contentHex = Ed25519.etc.bytesToHex(content);
const privateKeyBytes = Encoder.base64UrlToBytes(privateJwk.d);
const privateKeyHex = Ed25519.etc.bytesToHex(privateKeyBytes);

return Ed25519.signAsync(contentHex, privateKeyHex);
return Ed25519.signAsync(content, privateKeyBytes);
},

verify: async (content: Uint8Array, signature: Uint8Array, publicJwk: PublicJwk): Promise<boolean> => {
Expand All @@ -44,8 +42,7 @@ export const ed25519: SignatureAlgorithm = {

generateKeyPair: async (): Promise<{publicJwk: PublicJwk, privateJwk: PrivateJwk}> => {
const privateKeyBytes = Ed25519.utils.randomPrivateKey();
const privateKeyHex = Ed25519.etc.bytesToHex(privateKeyBytes);
const publicKeyBytes = await Ed25519.getPublicKeyAsync(privateKeyHex);
const publicKeyBytes = await Ed25519.getPublicKeyAsync(privateKeyBytes);

const d = Encoder.bytesToBase64Url(privateKeyBytes);

Expand Down
9 changes: 3 additions & 6 deletions src/utils/secp256k1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@ export class Secp256k1 {
// ensure public key is in uncompressed format so we can convert it into both x and y value
let uncompressedPublicKeyBytes;
if (publicKeyBytes.byteLength === 33) {
// this means given key is compressed
const publicKeyHex = secp256k1.etc.bytesToHex(publicKeyBytes);
const curvePoints = secp256k1.ProjectivePoint.fromHex(publicKeyHex);
// this means given key is compressed
const curvePoints = secp256k1.ProjectivePoint.fromHex(publicKeyBytes);
uncompressedPublicKeyBytes = curvePoints.toRawBytes(false); // isCompressed = false
} else {
uncompressedPublicKeyBytes = publicKeyBytes;
Expand Down Expand Up @@ -96,11 +95,9 @@ export class Secp256k1 {
// the underlying lib expects us to hash the content ourselves:
// https://github.com/paulmillr/noble-secp256k1/blob/97aa518b9c12563544ea87eba471b32ecf179916/index.ts#L1160
const hashedContent = await sha256.encode(content);
const hashedContentHex = secp256k1.etc.bytesToHex(hashedContent);
const privateKeyBytes = Secp256k1.privateJwkToBytes(privateJwk);
const privateKeyHex = secp256k1.etc.bytesToHex(privateKeyBytes);

return (await secp256k1.signAsync(hashedContentHex, privateKeyHex, )).toCompactRawBytes();
return (await secp256k1.signAsync(hashedContent, privateKeyBytes)).toCompactRawBytes();
}

/**
Expand Down

0 comments on commit b8901e9

Please sign in to comment.