Usage Example

Usage

Encrypt and Upload file


// account Alice: as the publisher of the file (file uploader). 
//
import {
  AccountManager,
  FuLinkHDWallet,
  Account,
  createWallet,
  loadWallet,
  verifyPassword,
  existDefaultAccount,
  isBlank,
  restoreWalletDataByMnemonic,
  getPolicyGasFee,
  type FileInfo,
  getWalletDefaultAccount,
  FileCategory,
  uploadFilesByCreatePolicy,
  getUploadedFiles,
  createAccountIfNotExist,
  getOtherShareFiles,
  getFileDetails,
  applyForFilesUsagePermission,
  getFilesPendingApprovalAsPublisher,
  refusalApplicationForUseFiles,
  getPolicyTokenCost,
  approvalApplicationForUseFiles,
  getApprovedFilesAsPublisher,
  getApprovedFilesAsUser,
  getFileContentByFileIdAsUser,
  getPublishedPoliciesInfo,
  uploadFilesBySelectPolicy,
  getFilesByStatus,
  getMnemonic,
  getDefaultAccountPrivateKey,
  logoutWallet,
} from "@fulink_network/fulink-sdk";

import assert from "assert-ts";

import { BigNumber, ethers } from "ethers";
import { nanoid } from "nanoid";
import Web3 from "web3";

// Declaring and intializing the mnemonic and password variables.
const password: string = "1";

//first We create Alice's wallet and account by password
const fuLinkHDWallet1: FuLinkHDWallet = await createWallet(password);

assert(fuLinkHDWallet1);

// after we created the wallet, we can loadWallet by password
const fuLinkHDWallet2: FuLinkHDWallet | null = await loadWallet(password);
assert(fuLinkHDWallet2);

const fuLinkHDWallet = fuLinkHDWallet2 as FuLinkHDWallet;

assert(fuLinkHDWallet1 === fuLinkHDWallet);

//also, We can verify whether the user's password is correct
const correct: boolean = await verifyPassword(password);

assert(correct);

// We can also determine if the user has created an account locally
const hasAnAccountInLocal: boolean = await existDefaultAccount();
assert(hasAnAccountInLocal);

// we can get the account by user password that we have created
const accountAlice: Account = (await getWalletDefaultAccount(password)) as Account;
assert(accountAlice);

// Note: We only support one account currently.

//Now we can encrypt and upload a file for others to apply for download

//1. read a file
const plainText = 'file-content....';
const enc = new TextEncoder(); // always utf-8
const historyContent: Uint8Array = enc.encode(plainText);

//1.Alice upload file
const fileList: FileInfo[] = [
  {
    name: `history-${nanoid()}.pdf`,
    fileBinaryArrayBuffer: historyContent.buffer,
  },
];

//2. Alice encrypt and update a file to the ipfs network
await uploadFilesByCreatePolicy(accountAlice, FileCategory.History, fileList);

//3. We can get the file just uploaded
const resultList = (await getUploadedFiles(accountAlice, undefined, 1, 1000)) as object;

console.log("resultList: ", resultList);
console.log('resultList["total"]>0 ', resultList["total"] > 0);
assert(resultList && resultList["total"] > 0);

let fileIndex = -1;
for (let index = 0; index < resultList["list"].length; index++) {
  const element = resultList["list"][index];
  if (element["file_name"] === fileList[0]["name"]) {
    fileIndex = index;
    break;
  }
}
assert(fileIndex >= 0);
const uploadFileInfo = resultList["list"][fileIndex];

assert(uploadFileInfo["owner_id"] === accountAlice.id);

Request and Grant the access

Last updated