@biconomy/permission-context-builder
Version:
ERC-7715 Permission Context Builder utility package.
65 lines • 2.52 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getSignerType = exports.decodeDIDToPublicKey = exports.KEY_TYPES = exports.encodeSecp256k1PublicKeyToDID = void 0;
exports.encodeSigners = encodeSigners;
exports.bigIntReplacer = bigIntReplacer;
const bs58_1 = require("bs58");
const viem_1 = require("viem");
const signers_1 = require("../types/signers.js");
const encodeSecp256k1PublicKeyToDID = (publicKey) => {
publicKey = publicKey.startsWith('0x') ? publicKey.slice(2) : publicKey;
const publicKeyBuffer = Buffer.from(publicKey, 'hex');
const encodedPublicKey = bs58_1.default.encode(publicKeyBuffer);
return `did:key:zQ3s${encodedPublicKey}`;
};
exports.encodeSecp256k1PublicKeyToDID = encodeSecp256k1PublicKeyToDID;
function encodeSigners(signers) {
let encoded = (0, viem_1.encodePacked)(['uint8'], [signers.length]);
for (const signer of signers) {
encoded = (0, viem_1.encodePacked)(['bytes', 'uint8', 'bytes'], [encoded, signer.type, signer.data]);
}
return encoded;
}
var KEY_TYPES;
(function (KEY_TYPES) {
KEY_TYPES["secp256k1"] = "secp256k1";
KEY_TYPES["secp256r1"] = "secp256r1";
})(KEY_TYPES || (exports.KEY_TYPES = KEY_TYPES = {}));
const decodeDIDToPublicKey = (did) => {
const didPrefixToKeyType = {
'did:key:zQ3s': KEY_TYPES.secp256k1,
'did:key:zDn': KEY_TYPES.secp256r1
};
const matchingPrefix = Object.keys(didPrefixToKeyType).find(prefix => did.startsWith(prefix));
if (!matchingPrefix) {
throw new Error('Invalid DID format. Unsupported key type.');
}
const encodedPart = did.slice(matchingPrefix.length);
const decodedBuffer = bs58_1.default.decode(encodedPart);
const publicKey = Buffer.from(decodedBuffer).toString('hex');
const formattedPublicKey = `0x${publicKey}`;
const keyType = didPrefixToKeyType[matchingPrefix];
return {
key: formattedPublicKey,
keyType
};
};
exports.decodeDIDToPublicKey = decodeDIDToPublicKey;
const getSignerType = (keyType) => {
switch (keyType) {
case KEY_TYPES.secp256k1:
return signers_1.SignerType.EOA;
case KEY_TYPES.secp256r1:
return signers_1.SignerType.PASSKEY;
default:
return signers_1.SignerType.EOA;
}
};
exports.getSignerType = getSignerType;
function bigIntReplacer(_key, value) {
if (typeof value === 'bigint') {
return value.toString();
}
return value;
}
//# sourceMappingURL=methods.js.map