Skip to content

Commit

Permalink
fix: address #65 make verify return boolean
Browse files Browse the repository at this point in the history
  • Loading branch information
OR13 committed Feb 6, 2021
1 parent f0fc5d1 commit d9f3183
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 45 deletions.
2 changes: 1 addition & 1 deletion packages/secp256k1/src/ES256K-R.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ it('sign / verify', async () => {
);
const jws = await sign(message, privateKeyJwk);
const verified = await verify(jws, publicKeyJwk);
expect(verified).toEqual(message);
expect(verified).toEqual(true);
});

it('signDetached / recoverPublicKey', async () => {
Expand Down
19 changes: 5 additions & 14 deletions packages/secp256k1/src/ES256K-R.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const sign = async (
payload: any,
privateKeyJwk: any,
header: any = { alg: 'ES256K-R' }
) => {
):Promise<string> => {
const privateKeyUInt8Array = await privateKeyUInt8ArrayFromJwk(privateKeyJwk);

const encodedHeader = base64url.encode(JSON.stringify(header));
Expand All @@ -34,7 +34,7 @@ export const sign = async (
return `${encodedHeader}.${encodedPayload}.${encodedSignature}`;
};

export const verify = async (jws: string, publicKeyJwk: any) => {
export const verify = async (jws: string, publicKeyJwk: any):Promise<boolean> => {
const publicKeyUInt8Array = await publicKeyUInt8ArrayFromJwk(publicKeyJwk);
const [encodedHeader, encodedPayload, encodedSignature] = jws.split('.');

Expand All @@ -58,17 +58,8 @@ export const verify = async (jws: string, publicKeyJwk: any) => {
messageHashUInt8Array,
publicKeyUInt8Array
);
if (verified) {
return JSON.parse(base64url.decode(encodedPayload));
}
const erroObject = {
signature: signatureUInt8Array.toString('hex'),
message: messageHashUInt8Array.toString('hex'),
publicKey: publicKeyUInt8Array.toString('hex'),
};
throw new Error(
'ECDSA Verify Failed: ' + JSON.stringify(erroObject, null, 2)
);

return verified;
};

export const signDetached = async (
Expand All @@ -79,7 +70,7 @@ export const signDetached = async (
b64: false,
crit: ['b64'],
}
) => {
):Promise<string> => {
const privateKeyUInt8Array = await privateKeyUInt8ArrayFromJwk(privateKeyJwk);
const encodedHeader = base64url.encode(JSON.stringify(header));
const toBeSignedBuffer = Buffer.concat([
Expand Down
2 changes: 1 addition & 1 deletion packages/secp256k1/src/ES256K.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ it('compact sign / verify ', async () => {
jose.JWK.asKey(keypair.publicKeyJwk as any)
);
expect(_verified2).toEqual(example.payload);
expect(_verified).toEqual(example.payload);
expect(_verified).toEqual(true);
});

it('detached sign / verify', async () => {
Expand Down
37 changes: 10 additions & 27 deletions packages/secp256k1/src/ES256K.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export const signDetached = async (
b64: false,
crit: ['b64'],
}
) => {
): Promise<string> => {
const privateKeyUInt8Array = await privateKeyUInt8ArrayFromJwk(privateKeyJwk);

const encodedHeader = base64url.encode(JSON.stringify(header));
Expand Down Expand Up @@ -72,7 +72,7 @@ export const verifyDetached = async (
jws: string,
payload: Buffer,
publicKeyJwk: ISecp256k1PublicKeyJwk
) => {
): Promise<boolean> => {
if (jws.indexOf('..') === -1) {
throw new JWSVerificationFailed('not a valid rfc7797 jws.');
}
Expand Down Expand Up @@ -109,25 +109,15 @@ export const verifyDetached = async (
publicKeyUInt8Array
);

if (verified) {
return true;
}
const erroObject = {
signature: signatureUInt8Array.toString('hex'),
// message: messageHashUInt8Array.toString('hex'),
// publicKey: publicKeyUInt8Array.toString('hex'),
};
throw new JWSVerificationFailed(
'ECDSA Verify Failed: ' + JSON.stringify(erroObject, null, 2)
);
return verified;
};

/** Produce a normal ES256K JWS */
export const sign = async (
payload: any,
privateKeyJwk: ISecp256k1PrivateKeyJwk,
header: IJWSHeader = { alg: 'ES256K' }
) => {
): Promise<string> => {
const privateKeyUInt8Array = await privateKeyUInt8ArrayFromJwk(privateKeyJwk);

const encodedHeader = base64url.encode(JSON.stringify(header));
Expand Down Expand Up @@ -155,7 +145,7 @@ export const sign = async (
export const verify = async (
jws: string,
publicKeyJwk: ISecp256k1PublicKeyJwk
) => {
): Promise<boolean> => {
const publicKeyUInt8Array = await publicKeyUInt8ArrayFromJwk(publicKeyJwk);
const [encodedHeader, encodedPayload, encodedSignature] = jws.split('.');
const toBeSigned = `${encodedHeader}.${encodedPayload}`;
Expand All @@ -176,21 +166,14 @@ export const verify = async (
messageHashUInt8Array,
publicKeyUInt8Array
);
if (verified) {
return JSON.parse(base64url.decode(encodedPayload));
}
const erroObject = {
signature: signatureUInt8Array.toString('hex'),
message: messageHashUInt8Array.toString('hex'),
publicKey: publicKeyUInt8Array.toString('hex'),
};
throw new JWSVerificationFailed(
'ECDSA Verify Failed: ' + JSON.stringify(erroObject, null, 2)
);

return verified;


};

/** decode a JWS (without verifying it) */
export const decode = (jws: string, options = { complete: false }) => {
export const decode = (jws: string, options = { complete: false }): any => {
const [encodedHeader, encodedPayload, encodedSignature] = jws.split('.');

if (options.complete) {
Expand Down
5 changes: 3 additions & 2 deletions packages/secp256k1/src/__tests__/jose.santity.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ it('interop', async () => {
expect(theirVerification).toEqual(payload);
const ourJws = await ES256K.sign(payload, privateKeyJwk);
const ourVerification = await ES256K.verify(ourJws, publicKeyJwk);
expect(ourVerification).toEqual(payload);
expect(ourVerification).toEqual(true);

const theirVerificationOfOurs = await JWS.verify(
ourJws,
Expand All @@ -35,8 +35,9 @@ it('interop', async () => {
theirJws,
publicKeyJwk
);
expect(ourVerificationOfTheirs).toEqual(payload);
expect(ourVerificationOfTheirs).toEqual(true);
} catch (e) {

errorCount++;
}
count++;
Expand Down

0 comments on commit d9f3183

Please sign in to comment.