Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove unnecessary conversions to hex format #643

Merged
merged 1 commit into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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