getPortraitHash()

The getPortraitHash() function is a method that can be used to get the hash of a User Portrait or Collective Portrait, if there is one. The hash is a unique identifier for a Portrait. If there is no hash set, the function will return null.

Syntax

getPortraitHash(identity: Identity): string

Parameters

  • identity: A required parameter of type Identity. Identity is of type:

    • UserPortraitAddress – Ethereum address

    • UserPortraitENSName – ENS name

    • CollectivePortraitName – Collective Portrait Name (a-z, 0-9, min. 3 chars, max. 64 chars.)

Return Value

This function returns a string value that represents the IPFS hash of a Portrait, if there is one. If there is no hash set, the function returns null.

Usage

This function is useful in situations where you need to retrieve the unique identifier for a Portrait. The function takes an identity as input, which can be either an Ethereum address, an ENS name, or a Collective Portrait name. The function determines the type of the input identity and retrieves the IPFS hash accordingly.

The function uses the following logic to determine the type of identity and retrieve the portrait hash:

  • If the input identity is an Ethereum address, it uses the getPortraitHash() method of the user object to retrieve the portrait hash.

  • If the input identity is a valid ENS name, it resolves the name using the default provider and uses the getPortraitHash() method of the user object to retrieve the portrait hash.

  • If the input identity is a Collective Portrait Name, it uses the getPortraitHash() method of the collective object to retrieve the hash.

  • If the input identity is none of the above types, the function returns null.

Example

import { Portrait } from 'portrait-sdk';

const portrait = new Portrait()

const identity = 'vitalik.eth'; // ENS name

const portraitHash = portrait.getPortraitHash(identity);

if (portraitHash) {
  console.log(`The Portrait IPFS hash for ${identity} is ${portraitHash}`);
} else {
  console.log(`${identity} does not have a Portrait`);
}

In this example, the getPortraitHash() function is used to retrieve the Portrait hash for the given ENS name. If the address has a Portrait, the function logs the IPFS hash to the console. If the address does not have a Portrait, the function logs a message indicating that the address does not have a Portrait.

Last updated