npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@nakasar/identity-sdk

v0.2.1

Published

Interact with BlockChain Identities.

Downloads

10

Readme

TOKENY IDENTITY SDK

This package facilitate the interaction with Identities stored in the BlockChain.

Specifications

Functionality

  • Deploy identities
  • List and Manage keys on an Identity
  • List and manage Claims on an Identity.
  • Issue claims for an Identity.
  • Fetch claim data using their URI.
  • Request access to claim private data using their URI.
  • Use access grants to access private data of claims.

Usage

Installation

Install with npm install @nakasar/identity-sdk

Then require with:

const IdentitySDK = require('@nakasar/identity-sdk');

BlockChain Provider

To interact with the BlockChain, you will need to instantiate a Provider.

The SDK is using Ethers to connect with Ethereum network. Thus, any provider supported by Ethers can be used with the SDK. This means any standard web3 provider should by supported.

Connect to a default provider:

// You can use any standard network name
//  - "homestead"
//  - "rinkeby"
//  - "ropsten"
//  - "kovan"

let provider = ethers.getDefaultProvider('ropsten');

Connect to JSON RPC:

// When using the JSON-RPC API, the network will be automatically detected


// Default: http://localhost:8545
let httpProvider = new ethers.providers.JsonRpcProvider();

Connect to any Web3 Provider:

// When using a Web3 provider, the network will be automatically detected

// e.g. HTTP provider
let currentProvider = new web3.providers.HttpProvider('http://localhost:8545');

let web3Provider = new ethers.providers.Web3Provider(currentProvider);

Connect to metamask:

// The network will be automatically detected; if the network is
// changed in MetaMask, it causes a page refresh.

let provider = new ethers.providers.Web3Provider(web3.currentProvider);

Please refer to the Ethers Providers Documentation for more information.

Configuration

Providers

By default, unsecured providers are not allowed. The SDK will refuse to fetch data on these endpoints. A claim that has an uri which is not an HTTPS endpoint won't be retrieved.

Allow unsecured endpoints with:

const IdentitySDK = require('@nakasar/identity-sdk');
IdentitySDK.config({ allowUnsecuredProviders: true });

SignerModule

Many interaction with identities, and especially claims, require to sign a challenge message. Functions requiring these signatures expect a SignerModule as argument.

A SignerModule must expose a .getPublicKey() and a .signMessage(message: string) functions.

This is, for instance, a valid SignerModule:

const jsrasign = require('jsrasign');

const signer = {
    getPublicKey: async () => ({
        key: "-----BEGIN CERTIFICATE----- my_super_public_key -----END CERTIFICATE-----",
        type: "X.509",
        signingMethod: "SHA-256",
    }),
    signMessage: async (message) => {
        const signer = new jsrsasign.Signature({ alg: 'SHA256withRSA' });
        signer.init("-----BEGIN CERTIFICATE----- my_super_PRIVATE_no_really_super_secret_PRIVATE_key -----END CERTIFICATE-----");
        signer.updateString(message);
        return signer.sign();
    },
};

It can be used in functions such as Claim.requestAccess():

claim.requestAccess(IdentitySDK.utils.enums.AccessGrantType.PERSISTENT, signer);

Examples

Load a contract

const IdentitySDK = require('@nakasar/identity-sdk');

const ethers = require('ethers');

const provider = new ethers.providers.JsonRpcProvider();

(async () => {
  const identity = new IdentitySDK.Identity(); // Create the Identity Object

  console.log(identity.instantiateAtAddress('0xadD92F8Ef0729E969c5a98Ea5740c9b644B362e3', provider)); // Get the instance of the Identity

  console.log(await identity.instance.getClaimIdsByType(1)); // Call directly a function from the Contract.
})();

Get claims of an Identity

const IdentitySDK = require('@nakasar/identity-sdk');

const Ethers = require('ethers');

const provider = new Ethers.providers.JsonRpcProvider();

(async () => {
  const identity = new IdentitySDK.Identity('0xadD92F8Ef0729E969c5a98Ea5740c9b644B362e3', provider);

  const claims = await identity.getClaimsByType(1);

  console.log(claims);
})();

Get keys of an Identity

const IdentitySDK = require('@nakasar/identity-sdk');

const Ethers = require('ethers');

const provider = new Ethers.providers.JsonRpcProvider();

(async () => {
  const identity = new IdentitySDK.Identity('0xadD92F8Ef0729E969c5a98Ea5740c9b644B362e3', provider);

  const keys = await identity.getKeysByPurpose(IdentitySDK.utils.enums.KeyPurpose.CLAIM);
  
  console.log(keys);
  
  console.log(await identity.getKeyPurpose(keys[0].key));
})();

Deploy an identity

const IdentitySDK = require('@nakasar/identity-sdk');

const Ethers = require('ethers');

const provider = new Ethers.providers.JsonRpcProvider();

const CLAIM_ISSUER_PRIVATE_KEY = 'issuer_private_key';
const claimIssuerWallet = new Ethers.Wallet(CLAIM_ISSUER_PRIVATE_KEY, provider);

const TOKENY_PRIVATE_KEY = 'tokeny_private_key';
const tokenyWallet = new Ethers.Wallet(TOKENY_PRIVATE_KEY, provider);

(async () => {
  // Deploy a new Identity
  const identity = await IdentitySDK.Identity.deployNew(tokenyWallet);

  await identity.addKey(IdentitySDK.utils.crypto.keccak256(claimIssuerWallet.address), IdentitySDK.utils.enums.KeyPurpose.CLAIM, IdentitySDK.utils.enums.KeyType.ECDSA);
  
  identity.useProvider(claimIssuerWallet);

  await identity.addClaim(IdentitySDK.utils.enums.ClaimType.KYC, IdentitySDK.utils.enums.ClaimScheme.SOME, claimIssuerWallet.address, "a signature", "what a lot of data", "http://localhost:8080/claims/666");
})();

Get details of a claim

const IdentitySDK = require('@nakasar/identity-sdk');

const Ethers = require('ethers');

const provider = new Ethers.providers.JsonRpcProvider();

(async () => {
    IdentitySDK.config({ allowUnsecuredProviders: true });

    const identity = new IdentitySDK.Identity('0xadD92F8Ef0729E969c5a98Ea5740c9b644B362e3', provider);

    const claims = await identity.getClaimsByType(IdentitySDK.utils.enums.ClaimType.KYC);

    const claim = new IdentitySDK.Claim(claims[0]);

    await claim.populate();

    console.log(claim);
    
    /*
    Claim {
      data: '0x65773261724950755a302f626e5a744e327961676676376139462f6a3672744a4e3761666a52414c6871493d',
      id: '0x3c6532cc1f4d1a44de8f58d4bde617bef8e744168bf92d783a0e1b66e7c6a44a',
      issuer: '0x8c78fF753c63ea0e8CA1FcA9997A132bC3e6a8F1',
      scheme: 1,
      type: 1,
      uri: 'http://localhost:8080/claims/b701e350-2a08-11e9-ac7e-517ddf10b60e',
      issuanceDate: 2019-02-06T12:14:12.996Z,
      emissionDate: 2019-02-06T12:15:02.039Z,
      status: 'PENDING',
      publicData: { result: 'clear' } }
     */
})();

Development

Don't forget to npm install first.

Build with npm run build.

This will build package into the dist/ folder from the TypeScript sources. This will also build the TypeDoc website into docs/type_doc.

Lint with npm run lint