Post signed data to Arweave using @liteseed/sdk
In this tutorial, we will walkthrough the process of uploading data to Arweave using the @liteseed/sdk
.
Installing the required packages
Section titled “Installing the required packages”This tutorial requires the installation of the @liteseed/sdk
.
npm install @liteseed/sdk
@liteseed/sdk
is the official js library for interacting with the Liteseed for js/ts.
Uploading Data
Section titled “Uploading Data”The sdk makes it very easy to upload data.
Initializing the SDK
Section titled “Initializing the SDK”First, you must create a new instance of Liteseed with the private key (JWK) you will use to sign and upload the data.
import { readFileSync } from "fs";import { Liteseed } from "@liteseed/sdk";
const jwk = JSON.parse(readFileSync("./arweave.json").toString());const liteseed = new Liteseed(jwk);
Signing the data
Section titled “Signing the data”You can convert any data into a special arweave transaction using signData
.
const data = readFileSync("image.jpeg"); const dataItem = await liteseed.signData({ data });
Store the id in a database, so you can query your data later easily.
Posting the signed data
Section titled “Posting the signed data”Next, you must pay for the upload to complete the process. You can check for the price of upload as follows.
const receipt = await liteseed.postSignedData({ dataItem }); console.log(receipt);
The receipt object looks something like the following The response would look something like this
{ "id": "kscLYZ3UHdCUZAimSr6wrSl5v-ll8uOQr_G8ZQ4osYA", "dataCaches": ["api.liteseed.xyz"], "fastFinalityIndexes": ["api.liteseed.xyz"], "deadlineHeight": 1470374, "owner": "...", "version": "1.0.0"}
Make the payment
Section titled “Make the payment”Finally, you need to simply make the payment.
const payment = await liteseed.sendPayment({ dataItem }); console.log(payment);
The Complete Example
Section titled “The Complete Example”Here’s the complete code for uploading a data-item using the @liteseed/sdk
.
import { readFileSync } from "node:fs";import { Liteseed } from "@liteseed/sdk";
const jwk = JSON.parse(readFileSync("./arweave.json").toString());const liteseed = new Liteseed(jwk);const data = readFileSync("image.jpeg");
async function upload(data) { const dataItem = await liteseed.signData({ data }); const receipt = await liteseed.postSignedData({ dataItem }); console.log(receipt); const payment = await liteseed.sendPayment({ dataItem }); console.log(payment);}
upload(data);