UNPKG

@biconomy/permission-context-builder

Version:

ERC-7715 Permission Context Builder utility package.

68 lines 2.44 kB
import bs58 from 'bs58'; import { encodePacked } from 'viem'; import { SignerType } from '../types/signers.js'; export const encodeSecp256k1PublicKeyToDID = (publicKey) => { // Remove '0x' prefix if present publicKey = publicKey.startsWith('0x') ? publicKey.slice(2) : publicKey; // Convert publicKey to Buffer const publicKeyBuffer = Buffer.from(publicKey, 'hex'); // Base58 encode the address const encodedPublicKey = bs58.encode(publicKeyBuffer); // Construct the did:key return `did:key:zQ3s${encodedPublicKey}`; }; export function encodeSigners(signers) { let encoded = encodePacked(['uint8'], [signers.length]); // signer.data = decoded public key from DID for (const signer of signers) { encoded = encodePacked(['bytes', 'uint8', 'bytes'], [encoded, signer.type, signer.data]); } return encoded; } export var KEY_TYPES; (function (KEY_TYPES) { KEY_TYPES["secp256k1"] = "secp256k1"; KEY_TYPES["secp256r1"] = "secp256r1"; })(KEY_TYPES || (KEY_TYPES = {})); export const decodeDIDToPublicKey = (did) => { // Define the DID prefix to key type mapping const didPrefixToKeyType = { 'did:key:zQ3s': KEY_TYPES.secp256k1, 'did:key:zDn': KEY_TYPES.secp256r1 }; // Find the matching key type prefix const matchingPrefix = Object.keys(didPrefixToKeyType).find(prefix => did.startsWith(prefix)); if (!matchingPrefix) { throw new Error('Invalid DID format. Unsupported key type.'); } // Extract the Base58 encoded part const encodedPart = did.slice(matchingPrefix.length); // Decode the Base58 string const decodedBuffer = bs58.decode(encodedPart); // Convert the Buffer to a hex string const publicKey = Buffer.from(decodedBuffer).toString('hex'); // Add the '0x' prefix const formattedPublicKey = `0x${publicKey}`; const keyType = didPrefixToKeyType[matchingPrefix]; return { key: formattedPublicKey, keyType }; }; export const getSignerType = (keyType) => { switch (keyType) { case KEY_TYPES.secp256k1: return SignerType.EOA; case KEY_TYPES.secp256r1: return SignerType.PASSKEY; default: return SignerType.EOA; } }; export function bigIntReplacer(_key, value) { if (typeof value === 'bigint') { return value.toString(); } return value; } //# sourceMappingURL=methods.js.map