@crustnetwork/crustbags-sdk
TypeScript icon, indicating that this package has built-in type declarations

0.0.2 • Public • Published

CrustBags SDK

SDK for interacting with CrustBags contract

Installation

npm install @crustnetwork/crustbags-sdk @ton/ton @ton/core @ton/crypto

Usage

Place Storage Order

import CrustBagssdk from '@crustnetwork/crustbags-sdk'
import { default_storage_period } from "@crustnetwork/crustbags-sdk/src/CrustBags";
import { Address, toNano } from "@ton/core";
import { mnemonicToPrivateKey } from "@ton/crypto";
import { TonClient, WalletContractV4 } from "@ton/ton";

const tc = new TonClient({
    endpoint: "https://testnet.toncenter.com/api/v2/jsonRPC",
});
const CrustBagsAddress = Address.parse(
    "EQBOOMNqG0rvNm6vFGfR4qZl48BTDw_gYefVI4DQ70t9GoPC"
);
const CrustBags = CrustBagssdk.CrustBags.createFromAddress(CrustBagsAddress);

const openCrustBags = tc.open(CrustBags);
const keyPair = await mnemonicToPrivateKey(["your", "mnemonic"]);
const wallet = tc.open(
    WalletContractV4.create({ workchain: 0, publicKey: keyPair.publicKey })
);
await openCrustBags.sendPlaceStorageOrder(
    wallet.sender(keyPair.secretKey),
    tonrrentHash, // torrentHash or bagId
    1024n, // fileSize
    merkleHash, // file merkle tree root hash
    toNano("0.1"), // storageFee
    default_storage_period // storage period time
);

API Reference

merkle tree

import { merkle } from '@crustnetwork/crustbags-sdk'

const readAsBlob = async (file: string) => {
  return new Promise<Blob>((resolve, reject) => {
    let chunks: Buffer[] = [];
    fs.createReadStream(file)
      .on("error", reject)
      .on("data", (data: Buffer) => {
        chunks.push(data);
      })
      .on("end", () => resolve(new Blob(chunks)));
  });
};

const mt = new merkle.MerkleTree();
const blob = await readAsBlob("test.txt");
// generate merkle tree
await mt.genTree(blob);
// merkle tree root
const merkleRoot = mt.tree![0];
// getMerkle proofs
const dataProofs = await mt.getDataAndProofs(blob, 1)

Tonutils

Wraped for the Tonutils-storage api Require env TON_STORAGE_UTILS_API=http://localhost:7721

import { tonutils } from '@crustnetwork/crustbags-sdk'

// create bag
const bagId = await tonutils.createBag('path', 'description')

Readme

Keywords

none

Package Sidebar

Install

npm i @crustnetwork/crustbags-sdk

Weekly Downloads

1

Version

0.0.2

License

Apache-2.0

Unpacked Size

47.4 kB

Total Files

18

Last publish

Collaborators

  • eericxu
  • crustbuild