Skip to content

Commit

Permalink
Fix duplication of constants in the crypto package (#819)
Browse files Browse the repository at this point in the history
  • Loading branch information
MarshalX authored Apr 18, 2023
1 parent 6e2a3d3 commit 7d95e75
Showing 1 changed file with 4 additions and 8 deletions.
12 changes: 4 additions & 8 deletions packages/crypto/src/did.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,19 @@ import * as uint8arrays from 'uint8arrays'
import * as p256 from './p256/encoding'
import * as secp from './secp256k1/encoding'
import plugins from './plugins'
import { P256_JWT_ALG, SECP256K1_JWT_ALG } from './const'

export const DID_KEY_BASE58_PREFIX = 'did:key:z'
import { P256_JWT_ALG, SECP256K1_JWT_ALG, BASE58_DID_PREFIX } from './const'

export type ParsedDidKey = {
jwtAlg: string
keyBytes: Uint8Array
}

export const parseDidKey = (did: string): ParsedDidKey => {
if (!did.startsWith(DID_KEY_BASE58_PREFIX)) {
if (!did.startsWith(BASE58_DID_PREFIX)) {
throw new Error(`Incorrect prefix for did:key: ${did}`)
}
const prefixedBytes = uint8arrays.fromString(
did.slice(DID_KEY_BASE58_PREFIX.length),
did.slice(BASE58_DID_PREFIX.length),
'base58btc',
)
const plugin = plugins.find((p) => hasPrefix(prefixedBytes, p.prefix))
Expand Down Expand Up @@ -46,9 +44,7 @@ export const formatDidKey = (jwtAlg: string, keyBytes: Uint8Array): string => {
keyBytes = secp.compressPubkey(keyBytes)
}
const prefixedBytes = uint8arrays.concat([plugin.prefix, keyBytes])
return (
DID_KEY_BASE58_PREFIX + uint8arrays.toString(prefixedBytes, 'base58btc')
)
return BASE58_DID_PREFIX + uint8arrays.toString(prefixedBytes, 'base58btc')
}

const hasPrefix = (bytes: Uint8Array, prefix: Uint8Array): boolean => {
Expand Down

0 comments on commit 7d95e75

Please sign in to comment.