From 64270cfec179fe4e7453cc15348ea8d04b2f05c3 Mon Sep 17 00:00:00 2001 From: Matthieu Sieben Date: Fri, 8 Dec 2023 15:23:42 +0100 Subject: [PATCH] refactor(crypto): expose compress/decompress as part of the DidKeyPlugin interface --- packages/crypto/src/did.ts | 28 +++++++------------------ packages/crypto/src/p256/plugin.ts | 9 ++++++-- packages/crypto/src/secp256k1/plugin.ts | 9 ++++++-- packages/crypto/src/types.ts | 3 +++ 4 files changed, 25 insertions(+), 24 deletions(-) diff --git a/packages/crypto/src/did.ts b/packages/crypto/src/did.ts index 3dca93582fe..a8e4f03a7c1 100644 --- a/packages/crypto/src/did.ts +++ b/packages/crypto/src/did.ts @@ -1,14 +1,7 @@ import * as uint8arrays from 'uint8arrays' -import { - BASE58_MULTIBASE_PREFIX, - DID_KEY_PREFIX, - P256_JWT_ALG, - SECP256K1_JWT_ALG, -} from './const' -import * as p256 from './p256/encoding' +import { BASE58_MULTIBASE_PREFIX, DID_KEY_PREFIX } from './const' import plugins from './plugins' -import * as secp from './secp256k1/encoding' import { extractMultikey, extractPrefixedBytes, hasPrefix } from './utils' export type ParsedMultikey = { @@ -22,12 +15,9 @@ export const parseMultikey = (multikey: string): ParsedMultikey => { if (!plugin) { throw new Error('Unsupported key type') } - let keyBytes = prefixedBytes.slice(plugin.prefix.length) - if (plugin.jwtAlg === P256_JWT_ALG) { - keyBytes = p256.decompressPubkey(keyBytes) - } else if (plugin.jwtAlg === SECP256K1_JWT_ALG) { - keyBytes = secp.decompressPubkey(keyBytes) - } + const keyBytes = plugin.decompressPubkey( + prefixedBytes.slice(plugin.prefix.length), + ) return { jwtAlg: plugin.jwtAlg, keyBytes, @@ -42,12 +32,10 @@ export const formatMultikey = ( if (!plugin) { throw new Error('Unsupported key type') } - if (jwtAlg === P256_JWT_ALG) { - keyBytes = p256.compressPubkey(keyBytes) - } else if (jwtAlg === SECP256K1_JWT_ALG) { - keyBytes = secp.compressPubkey(keyBytes) - } - const prefixedBytes = uint8arrays.concat([plugin.prefix, keyBytes]) + const prefixedBytes = uint8arrays.concat([ + plugin.prefix, + plugin.compressPubkey(keyBytes), + ]) return ( BASE58_MULTIBASE_PREFIX + uint8arrays.toString(prefixedBytes, 'base58btc') ) diff --git a/packages/crypto/src/p256/plugin.ts b/packages/crypto/src/p256/plugin.ts index d2c304d0fde..4fcbafc00ca 100644 --- a/packages/crypto/src/p256/plugin.ts +++ b/packages/crypto/src/p256/plugin.ts @@ -1,11 +1,16 @@ -import * as operations from './operations' +import { verifyDidSig } from './operations' +import { compressPubkey, decompressPubkey } from './encoding' + import { DidKeyPlugin } from '../types' import { P256_DID_PREFIX, P256_JWT_ALG } from '../const' export const p256Plugin: DidKeyPlugin = { prefix: P256_DID_PREFIX, jwtAlg: P256_JWT_ALG, - verifySignature: operations.verifyDidSig, + verifySignature: verifyDidSig, + + compressPubkey, + decompressPubkey, } export default p256Plugin diff --git a/packages/crypto/src/secp256k1/plugin.ts b/packages/crypto/src/secp256k1/plugin.ts index 5184a3778fa..1e8c622b752 100644 --- a/packages/crypto/src/secp256k1/plugin.ts +++ b/packages/crypto/src/secp256k1/plugin.ts @@ -1,11 +1,16 @@ -import * as operations from './operations' +import { verifyDidSig } from './operations' +import { compressPubkey, decompressPubkey } from './encoding' + import { DidKeyPlugin } from '../types' import { SECP256K1_DID_PREFIX, SECP256K1_JWT_ALG } from '../const' export const secp256k1Plugin: DidKeyPlugin = { prefix: SECP256K1_DID_PREFIX, jwtAlg: SECP256K1_JWT_ALG, - verifySignature: operations.verifyDidSig, + verifySignature: verifyDidSig, + + compressPubkey, + decompressPubkey, } export default secp256k1Plugin diff --git a/packages/crypto/src/types.ts b/packages/crypto/src/types.ts index b664b6d4d30..c6f87b99bd3 100644 --- a/packages/crypto/src/types.ts +++ b/packages/crypto/src/types.ts @@ -22,6 +22,9 @@ export type DidKeyPlugin = { data: Uint8Array, opts?: VerifyOptions, ) => Promise + + compressPubkey: (uncompressed: Uint8Array) => Uint8Array + decompressPubkey: (compressed: Uint8Array) => Uint8Array } export type VerifyOptions = {