Options
All
  • Public
  • Public/Protected
  • All
Menu

@stacks/profile

Functions for manipulating user profiles.

Installation

npm install @stacks/profile

Get profile from token

import { extractProfile } from '@stacks/profile';

// Token received after signin in browser using auth or connect package
const token = '<insert profile token here>';

const profile = extractProfile(token);
// profile

Verify profile token

import { verifyProfileToken } from '@stacks/profile';

// Token received after signin in browser using auth or connect package
const token = '<insert profile token here>';
const publicKey = '<insert public key>';

const decodedToken = verifyProfileToken(token, publicKey);
// decodedToken if verified successfully
// Otherwise throws an error if token verification fails

Make zonefile

import { makeProfileZoneFile } from '@stacks/profile';

const fileUrl = 'https://mq9.s3.amazonaws.com/naval.id/profile.json';
const origin = 'naval.id';
const zoneFile = makeProfileZoneFile(origin, fileUrl);
// zoneFile contents

Profile to token

import { signProfileToken, verifyProfileToken, extractProfile } from '@stacks/profile';

// Token received after signin in browser using auth or connect
const token = '<insert profile token here>';
const profile = extractProfile(token);
// warning: Do not expose your private key by hard coding in code. Use env variables to load private keys.  
const privateKey = '<private key>'; // process.env.privateKey
const publicKey = '<public key>';

const signedToken = signProfileToken(profile, privateKey);
const decodedToken = verifyProfileToken(signedToken, publicKey);
// decodedToken if verified successfully
// Otherwise throws an error if token verification fails

Profile Validation

import { extractProfile, Profile } from '@stacks/profile';

// Token received after signin in browser using auth or connect
const token = '<insert profile token here>';
// warning: Do not expose your private key by hard coding in code. Use env variables to load private keys.
const privateKey = '<private key>'; // process.env.privateKey
const publicKey = '<public key>';

const profile = extractProfile(token);

const profileObject = new Profile(profile);
console.log(profileObject);

const validationResults = Profile.validateSchema(profile);
console.log(validationResults.valid);

const profileJson = profileObject.toJSON();
console.log(profileJson);

const tokenRecords = profileObject.toToken(privateKey);
console.log(tokenRecords);

const profileFromToken = Profile.fromToken(tokenRecords, publicKey);
console.log(profileFromToken);

Index

Functions

extractProfile

  • extractProfile(token: string, publicKeyOrAddress?: string | null): Record<string, any>
  • Extracts a profile from an encoded token and optionally verifies it, if publicKeyOrAddress is provided.

    throws

    {Error} - if the token isn't signed by the provided publicKeyOrAddress

    Parameters

    • token: string

      the token to be extracted

    • publicKeyOrAddress: string | null = null

      the public key or address of the keypair that is thought to have signed the token

    Returns Record<string, any>

    • the profile extracted from the encoded token

signProfileToken

  • signProfileToken(profile: any, privateKey: string, subject?: any, issuer?: any, signingAlgorithm?: string, issuedAt?: Date, expiresAt?: Date): string
  • Signs a profile token

    Parameters

    • profile: any

      the JSON of the profile to be signed

    • privateKey: string

      the signing private key

    • Optional subject: any

      the entity that the information is about

    • Optional issuer: any

      the entity that is issuing the token

    • signingAlgorithm: string = 'ES256K'

      the signing algorithm to use

    • issuedAt: Date = ...

      the time of issuance of the token

    • expiresAt: Date = ...

      the time of expiration of the token

    Returns string

    • the signed profile token

verifyProfileToken

  • verifyProfileToken(token: string, publicKeyOrAddress: string): TokenInterface
  • Verifies a profile token

    throws

    {Error} - throws an error if token verification fails

    Parameters

    • token: string

      the token to be verified

    • publicKeyOrAddress: string

      the public key or address of the keypair that is thought to have signed the token

    Returns TokenInterface

    • the verified, decoded profile token

wrapProfileToken

  • wrapProfileToken(token: string): { decodedToken: TokenInterface; token: string }
  • Wraps a token for a profile token file

    Parameters

    • token: string

      the token to be wrapped

    Returns { decodedToken: TokenInterface; token: string }

    • including token and decodedToken
    • decodedToken: TokenInterface
    • token: string

Generated using TypeDoc