Skip to main content

RariMe JS-SDK reference

@rarimo/rarime-connector

The @rarimo/rarime-connector package provides an interface for interaction with the RariMe MetaMask snap. It is the recommended way of interacting with the identity protocol. Please use other packages only for custom integrations or low-level utilities.

Usage example:

import {
type CreateProofRequestParams,
enableSnap,
isMetamaskInstalled as detectMetamaskInstalled,
isSnapInstalled as detectSnapInstalled,
type SaveCredentialsRequestParams,
type SnapConnector,
type W3CCredential,
type ZKPProofResponse,
} from '@rarimo/rarime-connector'

let connector: SnapConnector

let isMetamaskInstalled = false
let isSnapInstalled = false

const isLocalSnap = (snapId: string) => snapId.startsWith('local:')

/**
* create identity and return did if it doesn't exist
* or return the existing one
*/
const createIdentity = async () => {
if (!connector) throw new TypeError('Connector is not defined')

// split by ':' if we want to get base58 part of did only,
// else we get did in 'did:iden3:[...]' format
return (await connector.createIdentity())?.split(':')?.[2]
}

const createProof = async (params: CreateProofRequestParams) => {
if (!connector) throw new TypeError('Connector is not defined')

return connector.createProof(params)
}

const checkMetamaskInstalled = async () => {
isMetamaskInstalled = await detectMetamaskInstalled()

return isMetamaskInstalled
}

const checkSnapInstalled = async () => {
isSnapInstalled = await detectSnapInstalled()

return isSnapInstalled
}

const connectOrInstallSnap = async () => {
const snap = await enableSnap()
connector = await snap.getConnector()
}

const checkSnapStatus = async () => {
isMetamaskInstalled = await checkMetamaskExists()
isSnapInstalled = await checkSnapExists()

return {
isMetamaskInstalled,
isSnapInstalled,
}
}

const init = async () => {
try {
const { isMetamaskInstalled, isSnapInstalled } = await checkSnapStatus()

if (!isMetamaskInstalled) throw new TypeError('Metamask is not installed')

if (!isSnapInstalled) {
await connectOrInstallSnap()
}

// Now you are able to create identity / generate proof or verifiable credentials by using corresponding methods, e. g.
const did = await createIdentity() // did:iden3:[...]
} catch (error) {
log.error(error)
}
}

init()