1.0.9 • Published 1 year ago

@krebitdao/reputation-passport v1.0.9

Weekly downloads
-
License
ISC
Repository
-
Last release
1 year ago

reputation-passport

Krebit Reputation Passport SDK

Docs

This repository hosts the Krebit Reputation Passport sdk, based on W3C Ethereum EIP712 Signature 2021 Draft.

It provides functions for creating off-chain Verifiable-Credentials in Ceramic that can be verified on-chain with krebit-contracts.

Overview

Installation

$ npm install -s @krebitdao/reputation-passport

Read-Only Passport

import krebit from '@krebitdao/reputation-passport';

const passport = new krebit.core.Passport({
  ceramicUrl: 'https://ceramic-clay.3boxlabs.com'
});
passport.read(address);

const profile = await passport.getProfile();
console.log('profile: ', profile);

const credentials = await passport.getCredentials();
console.log('credentials: ', credentials);

const reputation = await passport.getReputation();
console.log('reputation: ', reputation);

const stamps = await passport.getStamps(10, 'DigitalProperty');
console.log('stamps: ', stamps);

Initialize Ethereum Provider

import krebit from '@krebitdao/reputation-passport';

// Example on Browser:
const connectWeb3 = async () => {
  if (!window) return;
  const ethereum = (window as any).ethereum;

  if (!ethereum) return;

  const addresses = await ethereum.request({
    method: 'eth_requestAccounts'
  });
  const address = addresses[0];
  const ethProvider = await Krebit.lib.ethereum.getWeb3Provider();
  const wallet = ethProvider.getSigner();
  return { address, wallet, ethProvider };
};

// NODE JS Example:
export const connect = async () => {
  try {
    const ethProvider = Krebit.lib.ethereum.getProvider();

    let wallet: ethers.Wallet;

    try {
      // Create wallet from ethereum seed
      const unlockedWallet = ethers.Wallet.fromMnemonic(SERVER_ETHEREUM_SEED);
      // Connect wallet with provider for signing the transaction
      wallet = unlockedWallet.connect(ethProvider);
    } catch (error) {
      console.error('Failed to use local Wallet: ', error);
    }
    if (wallet && wallet.address) {
      console.log('address: ', wallet.address);
      ethProvider.setWallet(wallet);
      return { wallet, ethProvider };
    }
    return undefined;
  } catch (error) {
    throw new Error(error);
  }
};

Initialize Issuer

import krebit from '@krebitdao/reputation-passport';
const { wallet, ethProvider } = await connect();

const Issuer = new krebit.core.Krebit({
  wallet,
  ethProvider,
  network: 'mumbai',
  address: wallet.address,
  ceramicUrl: 'https://ceramic-clay.3boxlabs.com'
});
const did = await Issuer.connect();

Issue Credential

const getClaim = async (toAddress: string) => {
  const badgeValue = {
    communityId: 'My Community',
    name: 'Community Badge Name',
    imageUrl: 'ipfs://asdf',
    description: 'Badge for users that meet some criteria',
    skills: [{ skillId: 'participation', score: 100 }],
    xp: '1'
  };

  const expirationDate = new Date();
  const expiresYears = 3;
  expirationDate.setFullYear(expirationDate.getFullYear() + expiresYears);
  console.log('expirationDate: ', expirationDate);

  const claim = {
    id: `quest-123`,
    ethereumAddress: toAddress,
    did: `did:pkh:eip155:1:${toAddress}`
    type: 'questBadge',
    value: badgeValue,
    tags: ['quest', 'badge', 'Community'],
    typeSchema: 'https://github.com/KrebitDAO/schemas/questBadge',
    expirationDate: new Date(expirationDate).toISOString()
  };
};

const claim = await getClaim(toAddress);
const issuedCredential = await Issuer.issue(claim);

Verify Credential

console.log(
  'Verifying credential:',
  await Issuer.checkCredential(issuedCredential)
);

Add Credential to My Passport

import krebit from '@krebitdao/reputation-passport';
const { wallet, ethProvider } = await connectWeb3();

const passport = new krebit.core.Passport({
  ethProvider: ethProvider.provider,
  address,
  ceramicUrl: 'https://ceramic-clay.3boxlabs.com'
});
await passport.connect();

const addedCredentialId = await passport.addCredential(issuedCredential);
console.log('addedCredentialId: ', addedCredentialId);

Issue Ecrypted Credential

With Lit protocol:

import krebit from '@krebitdao/reputation-passport';
import LitJsSdk from "@lit-protocol/sdk-browser"; // Added Lit

const { wallet, ethProvider } = await connectWeb3();

const Issuer = new krebit.core.Krebit({
        wallet,
        ethProvider: ethProvider.provider,
        address,
        ceramicUrl: 'https://ceramic-clay.3boxlabs.com',
        litSdk: LitJsSdk // Added Lit
      });

const getEncryptedClaim = async (toAddress: string) => {
  const privateValue = {
    secretValue: 'My Secret',
  };

  const expirationDate = new Date();
  const expiresYears = 3;
  expirationDate.setFullYear(expirationDate.getFullYear() + expiresYears);
  console.log('expirationDate: ', expirationDate);

  const claim = {
    id: `custom-123`,
    ethereumAddress: toAddress,
    type: 'custom',
    value: privateValue,
    tags: ['tag1', 'tag2'],
    typeSchema: '<type url>',
    expirationDate: new Date(expirationDate).toISOString()
    encrypt: 'lit' as 'lit' // Added Lit
  };
};

const claim = await getEncryptedClaim(toAddress);
const issuedCredential = await Issuer.issue(claim);

Decrypt Credential

const decrypted = await Issuer.decryptClaim(issuedCredential);
console.log('Decrypted:', decrypted);

Learn More

The guides in the docs site will teach about different concepts of the Krebit Protocol.

Contribute

Krebit Protocol exists thanks to its contributors. There are many ways you can participate and help build public goods. Check out the Krebit Gitcoin Grants!

License

Krebit Reputation Passport is released under the ISC License.