jsonld-signatures-merkleproof2019
Version:
A jsonld signature implementation to support MerkleProof2019 verification in Verifiable Credential context
20 lines (19 loc) • 948 B
JavaScript
import * as bitcoin from 'bitcoinjs-lib';
import { ec } from 'elliptic';
import { keccak256 } from 'js-sha3';
import { Buffer as BufferPolyfill } from 'buffer';
export function computeBitcoinAddressFromPublicKey(publicKey, chain) {
return bitcoin.payments.p2pkh({ pubkey: publicKey, network: bitcoin.networks[chain.code] }).address;
}
export function computeEthereumAddressFromPublicKey(publicKey, chain) {
const publicKeyString = publicKey.toString('hex');
const ellipticCurve = new ec('secp256k1');
// Decode public key
const key = ellipticCurve.keyFromPublic(publicKeyString, 'hex');
// Convert to uncompressed format
const publicKeyUncompressed = key.getPublic().encode('hex').slice(2);
const buffer = typeof Buffer === 'undefined' ? BufferPolyfill : Buffer;
// Now apply keccak
const address = keccak256(buffer.from(publicKeyUncompressed, 'hex')).slice(64 - 40);
return `0x${address.toString()}`;
}