Usage Example
Usage
SelectNetworks
When nulink supports multiple networks, users can set their preferred network using the following method:
import {setCurrentNetworkKey} from "@nulink_network/nulink-sdk";
//The currently available network: NETWORK_LIST.Horus
await setCurrentNetworkKey(NETWORK_LIST.Horus)
Set Data Storage
Since the purpose of nulink pre is to encrypt and share data (files), it inevitably involves uploading (storing) and downloading (retrieving) files. Therefore, it is necessary to set up the methods for accessing files. The process is as follows:
first, we need to import package
import {
StorageManager,
DataCallback,
setIPFSData,
getIPFSData,
} from '@nulink_network/nulink-sdk'Then, you need to implement the setData and getData methods of the DataCallback structure. Since we need to upload data (files) in batches in our use case, while usually retrieving them one by one, we need to implement the setData method for batch uploading and the getData method for retrieving data individually.
After setting up the callback functions, when we upload files using the uploadDataByCreatePolicy method in pre, the callback function setData you have set will be automatically invoked. Later, when we retrieve files using getDataContentAsUser, the getData function will be automatically called.
The setData and getData types are as follows:
Notes:
More details:
For more information, you can refer to this example: upload.backend.ts
If the account parameter is not needed, you can refer to this example: ipfs.ts
With these preparations done, we can now proceed to the code for the pre process
Create Wallet for Alice
First, In order to use the pre process, we need to first create an account.
Alice upload data/files for encrypted
Next, we will upload data using the account we just created, which we'll refer to as "Alice."
Bob requests to use the data/files uploaded by Alice
Then: Bob, far away on the other side of the ocean, wants to use Alice's uploaded data.
1.create wallet for Bob
2.Bob views the details of the data that Alice just uploaded by checking the online information of other users' uploaded data.
3.Bob is particularly interested in the data uploaded by Alice. So, Bob requests to use the data that Alice has just uploaded.
4.Alice reviews the usage requests from others for the data she uploaded.
At first, Alice rejected Bob's request to use the data.
5.Bob finds this file to be very useful, so he makes a second request.
Alice approves Bob's request to use the file
1.Alice is moved by Bob's persistence and after much consideration, she finally agrees to Bob's usage request.Since we need to send approval transactions to the current blockchain network, we first need to assess the gas fees as well as the service fees for requesting others' usage.
2.As it is an on-chain transaction, there might be some delay. At this point, we need to wait for the on-chain transaction to be successfully confirmed by web3 before Alice can view her latest list of approved documents.
Bob checks his list of requested files, and he can use the data now
1.At this point, Bob checks his list of requested files and discovers that Alice has approved his request to use the file. So, Bob obtains the details of the document for future reference and downloading.
2.At this point, Bob downloads and views the data.
Alice obtain the on-chain policy information published by herself
Also, Whenever Alice approves a file request, an on-chain policy is created. Alice can also obtain the on-chain policy information published by herself
Alice upload file by select Published Policy
1.Alice also can encrypt and update a file to the ipfs network by select an existing on-chain policy
Bob get data no need approve by Alice's published policys
2.Bob can directly download Alice's associated policy upload file without waiting for Alice's approval, because the associated policy has already been created and does not need repeated approval.
Restore wallet by mnemonic
For wallet more info, you can also restore wallet by mnemonic
API docs
More examples
Last updated
Was this helpful?