Skip to content

Commit

Permalink
chore: address #64; add base58 <-> jwk conversion helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
OR13 committed Feb 6, 2021
1 parent d9f3183 commit 809e1c2
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 7 deletions.
9 changes: 6 additions & 3 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> => {
): Promise<string> => {
const privateKeyUInt8Array = await privateKeyUInt8ArrayFromJwk(privateKeyJwk);

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

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

Expand Down Expand Up @@ -70,7 +73,7 @@ export const signDetached = async (
b64: false,
crit: ['b64'],
}
):Promise<string> => {
): Promise<string> => {
const privateKeyUInt8Array = await privateKeyUInt8ArrayFromJwk(privateKeyJwk);
const encodedHeader = base64url.encode(JSON.stringify(header));
const toBeSignedBuffer = Buffer.concat([
Expand Down
4 changes: 1 addition & 3 deletions packages/secp256k1/src/ES256K.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export const sign = async (
payload: any,
privateKeyJwk: ISecp256k1PrivateKeyJwk,
header: IJWSHeader = { alg: 'ES256K' }
): Promise<string> => {
): Promise<string> => {
const privateKeyUInt8Array = await privateKeyUInt8ArrayFromJwk(privateKeyJwk);

const encodedHeader = base64url.encode(JSON.stringify(header));
Expand Down Expand Up @@ -168,8 +168,6 @@ export const verify = async (
);

return verified;


};

/** decode a JWS (without verifying it) */
Expand Down
1 change: 0 additions & 1 deletion packages/secp256k1/src/__tests__/jose.santity.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ it('interop', async () => {
);
expect(ourVerificationOfTheirs).toEqual(true);
} catch (e) {

errorCount++;
}
count++;
Expand Down
27 changes: 27 additions & 0 deletions packages/secp256k1/src/keyUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,30 @@ describe('publicKeyHexFromJwk', () => {
);
});
});


describe('publicKeyJwkFromPublicKeyBase58', () => {
it('should convert a publicKeyBase58 to a publicKeyJwk', async () => {
const _publicKeyJwk = await keyUtils.publicKeyJwkFromPublicKeyBase58(
example.keypair['application/did+ld+json'].publicKeyBase58
);
delete _publicKeyJwk.kid;
expect(_publicKeyJwk).toEqual(
example.keypair['application/did+json'].publicKeyJwk
);
});
});


describe('privateKeyJwkFromPrivateKeyBase58', () => {
it('should convert a privateKeyBase58 to a privateKeyJwk', async () => {
const _privateKeyJwk = await keyUtils.privateKeyJwkFromPrivateKeyBase58(
example.keypair['application/did+ld+json'].privateKeyBase58
);
delete _privateKeyJwk.kid;
expect(_privateKeyJwk).toEqual(
example.keypair['application/did+json'].privateKeyJwk
);
});
});

11 changes: 11 additions & 0 deletions packages/secp256k1/src/keyUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,3 +211,14 @@ export const publicKeyHexFromPrivateKeyHex = (privateKeyHex: string) => {
);
return Buffer.from(publicKey).toString('hex');
};


export const publicKeyJwkFromPublicKeyBase58 = (publicKeybase58: string)=>{
return publicKeyJwkFromPublicKeyHex(bs58.decode(publicKeybase58).toString('hex'));
}

export const privateKeyJwkFromPrivateKeyBase58 = (privateKeyBase58: string)=>{
return privateKeyJwkFromPrivateKeyHex(bs58.decode(privateKeyBase58).toString('hex'));
}


0 comments on commit 809e1c2

Please sign in to comment.