Skip to content

bcode-tech/BcodeSDK

Repository files navigation

Bcode SDK

Bcode SDK is a set of tool to help developer interact with Bcode API.

Written in Typescript and tested with Jest

Installation

From source code:

npm install git+https://github.com/bcode-tech/bcode-sdk.git

From npm:

npm install --save @bcode-tech/bcode-sdk

Usage

First you need to call the constructor, if you want to generate a new Wallet every time you can implemented like this:

const { BcodeSDK } = require("@bcode-tech/bcode-sdk")

(async () => {

    const sdk = new BcodeSDK({
        apiKey: <your-api-key>,
        config: { env: <environment>, debugMode: true },
    });

    await sdk.init()
})()

Otherwise if you already have a private key that you want to use you can use:

const { BcodeSDK } = require("@bcode-tech/bcode-sdk")

(async () => {

    const sdk = new BcodeSDK({
        apiKey: <your-api-key>,
        privateKey: <private-key>,
        config: { env: <environment>, debugMode: true },
    });

    await sdk.init()
})()

It's possible to change private key later:

const { BcodeSDK } = require("@bcode-tech/bcode-sdk")

(async () => {

    const sdk = new BcodeSDK({
        apiKey: <your-api-key>,
        privateKey: <private-key>,
        config: { env: <environment>, debugMode: true },
    });

    await sdk.init()

    sdk.setPrivateKey("private_key")
})()

Constructor config object parameters:

Param Default value Options
env MUMBAI MUMBAI, POLYGON
debugMode false true, false

Test

For running test you need to create privateKeys.json, with an array of at least 3 privateKeys to submit to test script.

Functions

Initilize user wallet and configure


Set private key after creation, to enable auth logic

Params

Param Default value
privateKey Wallet private key

Set mnemonic phrase after creation, to enable auth logic

Params

Param Default value
mnemonic Wallet mnemonic phrase

Reset wallet to null and delete private key


Generate new wallet after creation or after deleting previous one


Returns authToken received from Bcode API to authenticate call, it depends on API Key used.


Returns apiKey used in contructor, just for reference.


Returns address for generated wallet.

Return Bcode Token (PTK) balance of current wallet or the specified address.

Params

Param Default value
address user address

Return MATIC balance of the current wallet or the specidfied address.

Params

Param Default value
address user address

Function for request minting of CustomERC20 token.

Param Descriptioon
amount Token quantity to mint
contractAddress Address of CustomERC20 token contract

Prepare transaction that need to be executed through meta transaction.

Param Default value Description
contractObj Array with Bcode NFT Contract Array of interested contract addresses
functionName Current user address Wallet address of NFT owner
params Array Array of original prams for contract function

Example:

const tx = await sdk.prepareTransaction(
  {
    address: config[`TEST_META_TX_${env}`],
    abi: testMetaTxAbi,
    name: "TestMetaTransaction",
    version: "0.0.1",
  },
  "increment",
  []
));

Prepare transaction that need to be executed through meta transaction.

Param Description
tx Array of interested contract addresses
optionals Object with webhookUrl, secret and metadata

Example:

type MetaTransaction = {
  contractAddress: string;
  userAddress: string;
  functionSignature: string;
  r: string;
  s: string;
  v: any;
};

type Optionals = {
  webhookUrl: string | null;
  metadata: { [key: string]: any } | null;
  secret: string | null;
};

async executeTransaction(tx: MetaTransaction, optionals: Optionals | null)

Prepare transaction that need to be executed through meta transaction in async mode. API return a requestId needed to refetch transaction status later.

Param Description
tx Array of interested contract addresses
optionals Object with webhookUrl, secret and metadata

Example:

type MetaTransaction = {
  contractAddress: string;
  userAddress: string;
  functionSignature: string;
  r: string;
  s: string;
  v: any;
};

type Optionals = {
  webhookUrl: string | null;
  metadata: { [key: string]: any } | null;
  secret: string | null;
};

async executeAsyncTransaction(tx: MetaTransaction, optionals: Optionals | null)

Request meta transaction notarization, return notarization receipt.

Param Description
hash Hash of document to notarize
optionals Object with webhookUrl, secret and metadata

Example:

type Optionals = {
  webhookUrl: string | null;
  metadata: { [key: string]: any } | null;
  secret: string | null;
};

async notarizeHash(hash: string, optionals: Optionals | null)

Function for request mint of an NFT.

Param Default value Description
amount none Amount of NFT to mint
uri none URI of NFT resources
contractAddres Bcode NFT Contract Address of CustomNFT contract
webhookUrl null URL of webhook endpoint on which receive a response

Function for send NFT. It has a built-in permit request to allow Bcode to transfer NFT for user wallet.

Param Default value Description
to none Receiver of NFT
tokenId none TokenID of NFT to transfer
contractAddress Bcode NFT Contract Address of CustomNFT contract

Function that returns all NFT for the passes contract addresses for the given owner address.

Param Default value Description
contractAddresses Array with Bcode NFT Contract Array of interested contract addresses
ownerAddress Current user address Wallet address of NFT owner

Function that check if fetched JWT is still valid


Returns Bcode API service version, just to check if the service is available

Utility

Inside the BcodeSDK we had embed a set of tools to handle critic logic easily.

Hash

Hash allow the devloper to generate BcodeAPI compatible hash with SHA256 algorithm.

import { Hash } from "bcode-sdk";

const hash = Hash.fromString("try-me");

const imageHash = Hash.fromBuffer(Buffer.from("try-me"));

QRCode

BcodeSDK has integrate QRCode library, to easy generate QRCode of string.

import { QRCode } from "bcode-sdk";

QRCode.fromString("try-me").print();

QRCode.fromString("try-me").buffer();

Made with ❤️ by Bcode

About

SDK to interact with Pablock API

Resources

License

Security policy

Stars

Watchers

Forks

Packages

 
 
 

Contributors