UNPKG

@stacks/profile

Version:

Library for Stacks profiles

103 lines 3.75 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.extractProfile = exports.verifyProfileToken = exports.wrapProfileToken = exports.signProfileToken = void 0; const jsontokens_1 = require("jsontokens"); const common_1 = require("@stacks/common"); const transactions_1 = require("@stacks/transactions"); function signProfileToken(profile, privateKey, subject, issuer, signingAlgorithm = 'ES256K', issuedAt = new Date(), expiresAt = (0, common_1.nextYear)()) { if (signingAlgorithm !== 'ES256K') { throw new Error('Signing algorithm not supported'); } const publicKey = jsontokens_1.SECP256K1Client.derivePublicKey(privateKey); if (!subject) { subject = { publicKey }; } if (!issuer) { issuer = { publicKey }; } const tokenSigner = new jsontokens_1.TokenSigner(signingAlgorithm, privateKey); const payload = { jti: (0, common_1.makeUUID4)(), iat: issuedAt.toISOString(), exp: expiresAt.toISOString(), subject, issuer, claim: profile, }; return tokenSigner.sign(payload); } exports.signProfileToken = signProfileToken; function wrapProfileToken(token) { return { token, decodedToken: (0, jsontokens_1.decodeToken)(token), }; } exports.wrapProfileToken = wrapProfileToken; function verifyProfileToken(token, publicKeyOrAddress) { const decodedToken = (0, jsontokens_1.decodeToken)(token); const payload = decodedToken.payload; if (typeof payload === 'string') { throw new Error('Unexpected token payload type of string'); } if (payload.hasOwnProperty('subject') && payload.subject) { if (!payload.subject.hasOwnProperty('publicKey')) { throw new Error("Token doesn't have a subject public key"); } } else { throw new Error("Token doesn't have a subject"); } if (payload.hasOwnProperty('issuer') && payload.issuer) { if (!payload.issuer.hasOwnProperty('publicKey')) { throw new Error("Token doesn't have an issuer public key"); } } else { throw new Error("Token doesn't have an issuer"); } if (!payload.hasOwnProperty('claim')) { throw new Error("Token doesn't have a claim"); } const issuerPublicKey = payload.issuer.publicKey; const address = (0, transactions_1.getAddressFromPublicKey)(issuerPublicKey); if (publicKeyOrAddress === issuerPublicKey) { } else if (publicKeyOrAddress === address) { } else { throw new Error('Token issuer public key does not match the verifying value'); } const tokenVerifier = new jsontokens_1.TokenVerifier(decodedToken.header.alg, issuerPublicKey); if (!tokenVerifier) { throw new Error('Invalid token verifier'); } const tokenVerified = tokenVerifier.verify(token); if (!tokenVerified) { throw new Error('Token verification failed'); } return decodedToken; } exports.verifyProfileToken = verifyProfileToken; function extractProfile(token, publicKeyOrAddress = null) { let decodedToken; if (publicKeyOrAddress) { decodedToken = verifyProfileToken(token, publicKeyOrAddress); } else { decodedToken = (0, jsontokens_1.decodeToken)(token); } let profile = {}; if (decodedToken.hasOwnProperty('payload')) { const payload = decodedToken.payload; if (typeof payload === 'string') { throw new Error('Unexpected token payload type of string'); } if (payload.hasOwnProperty('claim')) { profile = payload.claim; } } return profile; } exports.extractProfile = extractProfile; //# sourceMappingURL=profileTokens.js.map