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

calimero-sdk

v0.0.11

Published

Javascript library to interact with Calimero private shards

Downloads

8

Readme

Calimero SDK

A JavaScript library for building decentralized applications, interacting with Calimero private shards, and managing NEAR wallets.

Calimero Documentation

Getting started

add calimero-sdk dependency to your project.

NPM

$ npm i calimero-sdk

Yarn

$ yarn add calimero-sdk

Usage

CalimeroSdk class

| Method | Description | Paremeters | Return | | ------------- | :------------- | :--------------- | :--------- | | init ( config ) | Initialize a new instance of CalimeroSdk. | shardId : string calimeroUrl : string walletUrl : string calimeroWebSdkService : string | CalimeroSdk : Object | | connect() |Connect to a Calimero private shard using NEAR wallet and near-api-js | | Connection : CalimeroConnection config : CalimeroConfig|

WalletConnection class

| Method | Description | Parameters | Return | | ------------- | :------------- | :------------- | :----------- | | new WalletConnection ( calimero, appPrefix ) | Creates a new wallet connection instance which extends near-api-js WalletConnection | calimero : CalimeroSdk appPrefix : String | WalletConnection : WalletConnection | | requestSignIn( successUrl ) | Connect wallet with a Calimero private shard and sync account | successUrl : SignInOptions | Promise : void |  | requestSignTransactions (transactions,callbackUrl) | Sign and approve requested transactions with wallet redirect | transactions : string callbackUrl : string Promise : void | | addFunctionKey ( contractAddress, methodNames, allowance, xApiKey) | Create and add function call key with allocated allowance for calling contract methods inside the Calimero private shard | contractAddress : string methodNames : string[] allowance : BN xApiKey : stringPromise : void |

AddFunctionKey paremeters in depth

contractAddress : string - Address of account / contract located in the Calimero private shard. methodName : string[] - String array of change functions available in smart contract. allowance : BN - Amount allowed to spend with function key. BN simbolises big number (yoctoNEAR). xApiKey : string - Calimero Auth token key. Can be created from Calimero Console token page or fetched from local storage under 'AUTH_TOKEN_KEY'.

Examples

Initialise new CalimeroSdk instance

ReactJS example with environment variables.

# calimeroSdk.js

import { CalimeroSdk } from "calimero-auth-sdk";

export default CalimeroSdk.init({
  shardId: process.env.REACT_APP_CALIMERO_SHARD_ID,
  walletUrl: process.env.REACT_APP_WALLET_ENDPOINT_URL,
  calimeroUrl: process.env.REACT_APP_CALIMERO_ENDPOINT_URL,
  calimeroWebSdkService: process.env.REACT_APP_CALIMERO_WEB_SDK_SERVICE_URL,
});

Initialise new WalletConnection instance

ReactJS example with environment variables.

# walletConnection.js

import { WalletConnection } from "calimero-sdk";
import calimeroSdk from "./calimeroSdk";

export const walletConnection = async () => {
  const calimero = await calimeroSdk.connect();
  return new WalletConnection(calimero, "calimero");
};

export default walletConnection;

Create simple login flow with HTML and JavaScript in ReactJS

# index.js

import React, { useEffect, useState } from "react";
import calimeroSdk from "./calimeroSdk";
import walletConnection from "./walletConnection";

export default function Dashboard() {
  const [isSignedIn, setIsSignedIn] = useState(false);
  const [calimero, setCalimero] = useState();
  const [walletConnectionObject, setWalletConnectionObject] = useState();
  const PrivateComponent = () => (
        <div>
        <button onClick={() => walletConnectionObject.signOut() }> Logout </button>
        </div>
  );

  const PublicComponent = () => (
        <div>
        <button onClick={ () => walletConnectionObject.requestSignIn({}) }> Login </button>
        </div>
  );

  useEffect(() => {
    const initialiseWalletConnection = async () => {
      setIsSignedIn(walletConnectionObject.isSignedIn());
    };
    if (walletConnectionObject) {
      initialiseWalletConnection();
    }
  }, [walletConnectionObject]);

  useEffect(() => {
    const initializeCalimero = async () => {
      setCalimero(calimeroSdk);
      const wallet = await walletConnection();
      setWalletConnectionObject(wallet);
    };

    if (!calimero || !walletConnectionObject) {
      initializeCalimero();
    }
  }, [calimero, walletConnectionObject]);

  return isSignedIn ? <PrivateComponent /> : <PublicComponent />;
}

Additional notes

This library is designed to be used in conjunction with Calimero private shards, and requires that you have a NEAR wallet with a valid key pair and access to the Calimero Console. You will also need to have a valid xApiKey to interact with the Calimero private shard.

Please refer to the Calimero documentation and Calimero examples for further information and guidance on using the Calimero SDK.

Calimero Token

Calimero tokens are used to grant the user access to selected Calimero private shard RPC endpoints.

Generating Calimero Token

Calimero tokens are generated based on the user's account ID and the selected Calimero private shard ID. The token data also includes a start and expiry date. The maximum lifetime of a token is 30 days. The token data is sent to the NEAR wallet to be signed as a message, which does not require a blockchain transaction or gas usage. Once the authorization service verifies that the user's account ID has signed the message (containing token data), a Calimero Authorization Token is issued, allowing the user access to Calimero private shard RPC endpoints.

Usage

  • Create a CalimeroTokenData instance with the required data: accountId and shardId. The duration of the token is specified by the from and to fields, which are optional and default to Date.now() and Date.now() + MAX_CALIMERO_TOKEN_DURATION, respectively. Create CalimeroTokenData with the required data: accountId and shardId. Duration of the token:from and to fields are optional and default to Date.now() and Date.now() + MAX_CALIMERO_TOKEN_DURATION, respectively.
  • Serialize the data using calimeroTokenDataInstance.serialize() and use this as the message parameter to be signed by My NEAR Wallet. Store the data received from My NEAR Wallet in a WalletData instance.
  • Create a CalimeroToken with the WalletData and CalimeroTokenData instances: calimeroToken = new CalimeroToken(walletData, calimeroTokenData).
  • Verify the signed message by calling calimeroToken.verify(). This will check if the token is valid with respect to Date.now() and if the signature matches the public key of the user's account ID.
  • Access to the Calimero private shard RPC endpoint should be granted to tokens that return true from calimeroToken.verify().

| Method | Description | Parameters | Return | | ------------------------------------- | :----------------------------------| :------------------------------------ | :---------------- | | CalimeroToken ( walletData, tokenData ) | Creates instance of Calimero Token with required wallet and token data. | walletData : walletData tokenData : CalimeroTokenData | constructor | | CalimeroToken.isDurationValid() | Check if token has expired. |  | boolean | | CalimeroToken.isSignatureValid() | Verifies the that user's signature is valid. |  | boolean | | CalimeroToken.verify() | Verify the signed message. |  | boolean | | WalletData ( accountId, message, message, blockId, publicKey, signature) | Creates an instance of WalletData with data obtained from the NEAR wallet. | accountId : string message : string blockId: stringpublicKey : string signature : string constructor | WalletData.isSignatureValid() | Verifies that the user's signature is valid. | | boolean | | CalimeroTokenData ( accountId, shardId, from, to ) | Creates an instance of Calimero Token Data. | accountId : string shardId : string from (optional) : Date to (optional) : Date constructor | | CalimeroTokenData.isDurationValid() | Checks if the expiry time is between 0 and 30 days. | | boolean |

License

This repository is distributed under the terms of both the MIT license and the Apache License (Version 2.0). See LICENSE and LICENSE-APACHE for details.