Skip to content

Commit

Permalink
refactor(crypto): expose compress/decompress as part of the DidKeyPlu…
Browse files Browse the repository at this point in the history
…gin interface
  • Loading branch information
matthieusieben committed Feb 13, 2024
1 parent ef961a9 commit 64270cf
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 24 deletions.
28 changes: 8 additions & 20 deletions packages/crypto/src/did.ts
Original file line number Diff line number Diff line change
@@ -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 = {
Expand All @@ -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,
Expand All @@ -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')
)
Expand Down
9 changes: 7 additions & 2 deletions packages/crypto/src/p256/plugin.ts
Original file line number Diff line number Diff line change
@@ -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
9 changes: 7 additions & 2 deletions packages/crypto/src/secp256k1/plugin.ts
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions packages/crypto/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ export type DidKeyPlugin = {
data: Uint8Array,
opts?: VerifyOptions,
) => Promise<boolean>

compressPubkey: (uncompressed: Uint8Array) => Uint8Array
decompressPubkey: (compressed: Uint8Array) => Uint8Array
}

export type VerifyOptions = {
Expand Down

0 comments on commit 64270cf

Please sign in to comment.