https://github.com/sorbin/tuskscript
TuskScript is a lightweight TypeScript library that simplifies data storage and retrieval on the Walrus network for both Web2 and Web3 applications.
https://github.com/sorbin/tuskscript
data-availability npm sui walrus
Last synced: 4 months ago
JSON representation
TuskScript is a lightweight TypeScript library that simplifies data storage and retrieval on the Walrus network for both Web2 and Web3 applications.
- Host: GitHub
- URL: https://github.com/sorbin/tuskscript
- Owner: Sorbin
- Created: 2024-08-30T09:29:27.000Z (about 1 year ago)
- Default Branch: master
- Last Pushed: 2024-08-30T23:40:40.000Z (about 1 year ago)
- Last Synced: 2025-05-26T07:00:52.606Z (5 months ago)
- Topics: data-availability, npm, sui, walrus
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/tuskscript
- Size: 285 KB
- Stars: 2
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
![]()
# tuskscript
`tuskscript` is a TypeScript-based npm package that simplifies development for the Walrus network. It provides an easy-to-use API for storing and retrieving data, allowing seamless integration with both Web2 and Web3 applications. Whether you are building decentralized apps (dApps) or traditional applications, TuskScript enables you to leverage the power of the Walrus network with minimal effort.
## Installation
Use the package manager npm to install `tuskscript`.
```bash
npm i tuskscript
```## Usage
Below is a quick example of how to use TuskScript to store and retrieve data on the Walrus network.
### Storing Data
Start by creating a new instance of `WalrusClient`. You can store any JavaScript object, and the API will return details about the stored blob. If the data is new, it returns a `NewBlob`. If the data has been previously stored, it returns an `ExistingBlob`.
```typescript
import {
WalrusClient,
NewBlob,
ExistingBlob
} from "tuskscript";const client = new WalrusClient();
// const publisher = "..."
// const aggregator = "...";
// const client = new WalrusClient(publisher, aggregator);const data = {
name: "Walrus",
language: "Move",
chain: "Sui",
};// Store data as a new blob
const result: NewBlob = await client.store(data);
console.log(result);
/*
{
newlyCreated: {
blobObject: {
id: '0xd3ee449b225ae4bfdd3f361982883b1f6312ab25a512a431bad99c87ff12c844',
storedEpoch: 0,
blobId: 'BmDTorm0NtVbVZcFXlQaKQ2xLiJ7HvxPuoIoE3hC0Pk',
size: 49,
erasureCodeType: 'RedStuff',
certifiedEpoch: 0,
storage: {}
},
encodedSize: 65023000,
cost: 3175000
}
}
*/// Storing the same data again returns an ExistingBlob
const existingResult: ExistingBlob = await client.store(data);
console.log(existingResult);
/*
{
alreadyCertified: {
blobId: 'BmDTorm0NtVbVZcFXlQaKQ2xLiJ7HvxPuoIoE3hC0Pk',
event: {
txDigest: 'EW7pCYu7DNfPYpS5j3c8gVA2w3hNiP7dyYARbAxCSSbK',
eventSeq: '0'
},
endEpoch: 1
}
}
*/
```### Retrieving Data
You can retrieve stored data by its `blobId`. By default, data is retrieved as a `Blob`, but you can also parse it directly into TypeScript types.
```typescript
import { WalrusClient } from "tuskscript";const client = new WalrusClient();
const blobId = "BmDTorm0NtVbVZcFXlQaKQ2xLiJ7HvxPuoIoE3hC0Pk";// Retrieve data as a Blob object
const blob: Blob = await client.retrieve(blobId);
const data = JSON.parse(await blob.text());
console.log(data);
/*
{
"name": "Walrus",
"language": "Move",
"chain": "Sui"
}
*/// Retrieve data directly as a JavaScript object
const directData = await client.retrieve(blobId, { asBlob: false });
console.log(directData);
/*
{
"name": "Walrus",
"language": "Move",
"chain": "Sui"
}
*/// Retrieve data with specified content type
const jsonBlob = await client.retrieve(blobId, { type: "application/json" });
```### Low-Level Services
If you need more granular control over the data storage and retrieval process, you can use the low-level `store` and `retrieve` functions:
```typescript
import { store, retrieve } from "tuskscript";const publisher = "...";
const aggregator = "...";const data = {
name: "Walrus",
language: "Move",
chain: "Sui",
};// Store data with additional options
const result = await store({
publisher,
data,
epoch: 5
});// Retrieve data using low-level retrieval
const blobId = "BmDTorm0NtVbVZcFXlQaKQ2xLiJ7HvxPuoIoE3hC0Pk";
const retrievedData = await retrieve({
aggregator,
id: blobId,
});
```## Contributing
Pull requests are welcome. For major changes, please open an issue first
to discuss what you would like to change.Please make sure to update tests as appropriate.
## License
[MIT](https://choosealicense.com/licenses/mit/)