UNPKG

jsonld-signatures-merkleproof2019

Version:

A jsonld signature implementation to support MerkleProof2019 verification in Verifiable Credential context

40 lines (35 loc) 1.86 kB
import type { IDidDocumentPublicKey } from '@decentralized-identity/did-common-typescript'; import { publicKeyUInt8ArrayFromJwk, publicKeyUInt8ArrayFromMultibase } from '../utils/keyUtils.js'; import { computeBitcoinAddressFromPublicKey, computeEthereumAddressFromPublicKey } from '../utils/issuingAddress.js'; import getText from '../helpers/getText.js'; import VerifierError from '../models/VerifierError.js'; import { SupportedChains } from '@blockcerts/explorer-lookup'; import type { IBlockchainObject } from '@blockcerts/explorer-lookup'; import type { ISecp256k1PublicKeyJwk } from '../utils/keyUtils'; export default async function deriveIssuingAddressFromPublicKey (verificationMethodPublicKey: IDidDocumentPublicKey, chain: IBlockchainObject): Promise<string> { let publicKey; if ('publicKeyJwk' in verificationMethodPublicKey) { publicKey = publicKeyUInt8ArrayFromJwk(verificationMethodPublicKey.publicKeyJwk as ISecp256k1PublicKeyJwk); } else if ('publicKeyMultibase' in verificationMethodPublicKey) { publicKey = await publicKeyUInt8ArrayFromMultibase(verificationMethodPublicKey); } const baseError = getText('errors', 'identityErrorBaseMessage'); let address: string = ''; switch (chain.code) { case SupportedChains.Bitcoin: case SupportedChains.Mocknet: case SupportedChains.Testnet: address = computeBitcoinAddressFromPublicKey(publicKey, chain); break; case SupportedChains.Ethmain: case SupportedChains.Ethropst: case SupportedChains.Ethrinkeby: case SupportedChains.Ethgoerli: case SupportedChains.Ethsepolia: address = computeEthereumAddressFromPublicKey(publicKey, chain); break; default: throw new VerifierError('deriveIssuingAddressFromPublicKey', `${baseError} - ${getText('errors', 'deriveIssuingAddressFromPublicKey')}`); } return address; }