LibertyDSNP / eth-sdk-ts

TypeScript SDK for DSNP on Ethereum
Apache License 2.0
9 stars 1 forks source link

DSNP Ethereum TypeScript SDK

Latest Release

Actions Status

Easy to use SDK for the DSNP

Quick Start

Install the package

First, install the SDK package with the following command:

npm install @dsnp/sdk

Configure the SDK

The configuration can be set at runtime with the following:

// Node
const { setConfig } = require("@dsnp/sdk");
const { Wallet, providers } = require("ethers");

setConfig({
  signer: new Wallet("key"),
  provider: new providers.JsonRpcProvider("https://...")
});
// Do something with the SDK
// TypeScript
import { Wallet, providers } from "ethers";
import { setConfig } from "@dsnp/sdk";

setConfig({
  signer: new Wallet("key"),
  provider: new providers.JsonRpcProvider("https://...")
});
// Do something with the SDK

Contract Configuration

Contracts are self discoverable via log messages for test networks, but public networks should use specific contract addresses:

Liberty Testnet
Rinkeby Testnet
{
  dsnpStartBlockNumber: 9211311,
  contracts: {
    Publisher: "0xeF7B5d418128fB8C1645Dd31270BE2cCAF9015e4",
    Beacon: "0xe3B7Fb9c43F9E62910Ae2763AA64aec07ec8F308",
    BeaconFactory: "0xC1F8593D46356B98c5DC7f7E8DF35247A68ED7D8",
    Identity: "0xa067CEa2859d27CA83700c7E17414f111C1BF561",
    IdentityCloneFactory: "0xDf962f3C24863A0fb8fb77B3144E31fE2859b9B8",
    Registry: "0x5d8266342aAfe19CB8EC25A6637f385893389A35",
  }
}
Ropsten Testnet
{
  dsnpStartBlockNumber: 10959123,
  contracts: {
    Publisher: "0x9828b9c8E8863267508eB0235370Eb26914D6a78",
    Beacon: "0x307748fF8c3547a6768B0CD37c1b0F35fFB0ca47",
    BeaconFactory: "0x024a03CFE1e8EE563382C08C1aB359830c39Cf20",
    Identity: "0x33707b57CE4Af9f970fb04a4D6CFF15B8342D938",
    IdentityCloneFactory: "0x61F57538a2621Dd2ba36E513e11cDe4f5936bCe9",
    Registry: "0xEBF48cE1EE0e727C2E23cb977799B93fD2EbFfda",
  }
}

Storing Activity Content and Batch Files

Storage solutions can be added so as long it matches the StoreInterface.

interface StoreInterface {
  putStream: (targetPath: string, doWriteToStream: WriteStreamCallback) => Promise<URL>;
}

Configuration is set like so:

config
  .setConfig({
    store: MyStoreModule, // for modules
    store: new MyStore(), // for classes
   });

See Config Documentation for details on additional options.

Example Stores

An example implementations of storage can be found under the examples folder

Usage

Once the SDK is installed and configured, the following code can be used to post a batch on the chain:

// Node
const publisher = require("@dsnp/sdk/core/contracts/publisher");

publisher.publish([{ fileHash, fileUrl, announcementType }]);
// TypeScript
import publisher from "@dsnp/sdk/core/contracts/publisher";

publisher.publish([{ fileHash, fileUrl, announcementType }]);

Documentation

See Documentation or generate documentation locally via npm run doc.

How to Install

Run npm install @dsnp/sdk

How to Build

Run npm run build

How to Compile Documentation

Documentation is deployed on merge to main to GitHub Pages: https://libertydsnp.github.io/sdk-ts/

Environment Variables

Name Description
RPC_URL url of node to make calls to
TESTING_PRIVATE_KEY Only used in testing - private key of account you are sending transactions from

Testing

How to Test

  1. Check out the contracts repo and follow the instructions to start a hardhat test node and deploy the contracts.
    • Match sure you match the version in ./package.json!
  2. In the SDK create a .env file with the following content.
    RPC_URL=http://localhost:8545
    TESTING_PRIVATE_KEY=ac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80
  3. Replace the value of TESTING_PRIVATE_KEY with the value of LOCAL_NETWORK_ACCOUNT_PRIVATE_KEY in the .env from the contracts repo, or use what is in .github/workflows/main.yml. It may be same as above.
  4. Ensure that the contracts version you would like to use is the correct version. The version of the @dsnp/contracts package is specified in the package.json
  5. In the contracts repo run: npm run hardhat -- node
  6. In the contracts repo run: npm run deploy:localhost
  7. In the sdk repo run: npm run test

Test Writing Utilities