1.1.1 • Published 1 year ago

@bcode-tech/bcode-sdk v1.1.1

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

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:

ParamDefault valueOptions
envAMOYAMOY, POLYGON
debugModefalsetrue, 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

init()

Initilize user wallet and configure


setPrivateKey()

Set private key after creation, to enable auth logic

Params

ParamDefault value
privateKeyWallet private key

setMnemonicPhrase()

Set mnemonic phrase after creation, to enable auth logic

Params

ParamDefault value
mnemonicWallet mnemonic phrase

resetWallet()

Reset wallet to null and delete private key


regenerateWallet()

Generate new wallet after creation or after deleting previous one


getAuthToken()

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


getAPIKey()

Returns apiKey used in contructor, just for reference.


getWalletAddress(address)

Returns address for generated wallet.

getBcodeTokenBalance(address)

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

Params

ParamDefault value
addressuser address

getMaticBalance()

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

Params

ParamDefault value
addressuser address

requestToken(amount, contractAddress)

Function for request minting of CustomERC20 token.

ParamDescriptioon
amountToken quantity to mint
contractAddressAddress of CustomERC20 token contract

prepareTransaction(contractObj, functionName, params)

Prepare transaction that need to be executed through meta transaction.

ParamDefault valueDescription
contractObjArray with Bcode NFT ContractArray of interested contract addresses
functionNameCurrent user addressWallet address of NFT owner
paramsArrayArray 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",
  []
));

executeTransaction(tx, optionals)

Prepare transaction that need to be executed through meta transaction.

ParamDescription
txArray of interested contract addresses
optionalsObject 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)

executeAsyncTransaction(tx, optionals)

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

ParamDescription
txArray of interested contract addresses
optionalsObject 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)

notarizeHash(hash, optionals)

Request meta transaction notarization, return notarization receipt.

ParamDescription
hashHash of document to notarize
optionalsObject 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)

mintBcodeNFT(amount, uri, contractAddress, webhookUrl)

Function for request mint of an NFT.

ParamDefault valueDescription
amountnoneAmount of NFT to mint
urinoneURI of NFT resources
contractAddresBcode NFT ContractAddress of CustomNFT contract
webhookUrlnullURL of webhook endpoint on which receive a response

sendBcodeNFT(to, tokenId, deadline, contractAddress)

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

ParamDefault valueDescription
tononeReceiver of NFT
tokenIdnoneTokenID of NFT to transfer
contractAddressBcode NFT ContractAddress of CustomNFT contract

getOwnedNFT(contractAddresses, ownerAddress)

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

ParamDefault valueDescription
contractAddressesArray with Bcode NFT ContractArray of interested contract addresses
ownerAddressCurrent user addressWallet address of NFT owner

checkJWTValidity()

Function that check if fetched JWT is still valid


getAPIVersion()

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