SolDriver

This page describes documentation specific to the Solana ChainDriver named as SolDriver created by the Blockin team. It is implemented using bs58 and tweetnacl libraries, and it is targeted for use by Phantom wallet.

For BitBadges, it uses the official BitBadges API.

Installation and Initialization

First, npm install blockin-sol-driver

Then, import via import SolDriver from 'blockin-sol-driver';

To initialize a new instance, run:

const SolanaDriver = new SolDriver(CHAIN_ID);

Currently, native Solana assets are not supported with this driver, only BitBadges.

Signatures

See here for an example of implementing signatures in a compatible format using Phantom wallet.

const getProvider = () => {
  if ('phantom' in window) {
    const phantomWindow = window as any;
    const provider = phantomWindow.phantom?.solana;
    setSolanaProvider(provider);
    if (provider?.isPhantom) {
      return provider;
    }

    window.open('https://phantom.app/', '_blank');
  }
};

const signChallenge = async (message: string) => {
  const encodedMessage = new TextEncoder().encode(message);
  const provider = solanaProvider;
  const signedMessage = await provider.request({
    method: "signMessage",
    params: {
      message: encodedMessage,
      display: "utf8",
    },
  });

  return { message: message, signature: signedMessage.signature.toString('hex') };
}

BitBadges

Note to use the BitBadges API and query badges, you must set a valid environment variable for process.env.BITBADGES_API_KEY.

Scheme

For BitBadges, we use the collectionId.

BitBadges assetIds must be in the form of [{ start: 1, end: 10 }, ....]

ownershipTimes (if blank, we query current time) can either be a valid JS Date string or in UNIX milliseconds via [{ start: 1, end: 1000000 }]

{
  collectionId: 1,
  assetIds: [{ start: 1, end: 1 }],
  mustOwnAmounts: [{ start: 0, end: 0 }],
  chain: 'BitBadges',
}

Ownership Times

For BitBadges, we support checking ownership time via their time-based balances feature. Note this is an ownership rights feature, it queries if the user has ownership rights for time XYZ currently, not that they had rights at time XYZ for time XYZ. See here.

To actually query if they owned at the current time, you must query the blockchain state at that time or provide a snapshot of it at that time.

Snapshots

You can provide snapshots for BitBadges badges via the verifyChallenge options. Snapshot maps are expected to be in the form of OffChainBalancesMap<NumberType>. See https://bafybeiejae7ylsndxcpxfrfctdlzh2my7ts5hk6fxhxverib7vei3wjn4a.ipfs.dweb.link/.

Signing a Blockin Message

const getProvider = () => {
  if ('phantom' in window) {
    const phantomWindow = window as any;
    const provider = phantomWindow.phantom?.solana;

    if (provider?.isPhantom) {
      return provider;
    }

    window.open('https://phantom.app/', '_blank');
  }
};

const signChallenge = async (message: string) => { 
  //message is the stringified blockin sign in message
  const encodedMessage = new TextEncoder().encode(message);
  const provider = await getProvider();
  const signedMessage = await provider.request({
    method: "signMessage",
    params: {
      message: encodedMessage,
      display: "utf8",
    },
  });
  const originalBytes = new Uint8Array(Buffer.from(message, 'utf8'));
  const signatureBytes = new Uint8Array(Buffer.from(signedMessage.signature, 'hex'));

  return { originalBytes: originalBytes, signatureBytes: signatureBytes, message: 'Success!' };
}

Last updated