Skip to content

Commit

Permalink
feat: add new storage provider ArDrive Turbo (#158)
Browse files Browse the repository at this point in the history
* feat: added turbo storage provider
  • Loading branch information
troykessler authored Oct 30, 2024
1 parent 7d6edb9 commit 35cec1a
Show file tree
Hide file tree
Showing 4 changed files with 522 additions and 10 deletions.
3 changes: 2 additions & 1 deletion common/protocol/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
"semver": "^7.5.3",
"tslog": "^3.2.2",
"unique-names-generator": "^4.6.0",
"uuid": "^9.0.0"
"uuid": "^9.0.0",
"@ardrive/turbo-sdk": "^1.14.1"
},
"devDependencies": {
"@types/clone": "^2.1.1",
Expand Down
79 changes: 79 additions & 0 deletions common/protocol/src/reactors/storageProviders/Turbo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { Readable } from "node:stream";

import {
privateKeyFromKyveMnemonic,
TurboAuthenticatedClient,
TurboFactory,
TurboUnauthenticatedClient,
} from "@ardrive/turbo-sdk";
import axios from "axios";

import { BundleTag, IStorageProvider } from "../../types";

export class Turbo implements IStorageProvider {
public name = "Turbo";
public coinDecimals = 12;

private readonly mnemonic: string;

constructor(mnemonic: string) {
if (!mnemonic) {
throw new Error("Mnemonic is empty.");
}

this.mnemonic = mnemonic;
}

private async authenticatedTurbo(): Promise<TurboAuthenticatedClient> {
return TurboFactory.authenticated({
privateKey: await privateKeyFromKyveMnemonic(this.mnemonic),
token: "kyve",
});
}

private unauthenticatedTurbo(): TurboUnauthenticatedClient {
return TurboFactory.unauthenticated({
token: "kyve",
});
}

async getAddress() {
return (await this.authenticatedTurbo()).signer.getNativeAddress();
}

async getBalance() {
const { winc } = await (await this.authenticatedTurbo()).getBalance();
return winc;
}

async getPrice(bytes: number) {
const [{ winc }] = await this.unauthenticatedTurbo().getUploadCosts({
bytes: [bytes],
});
return winc;
}

public async saveBundle(dataBuffer: Buffer, tags: BundleTag[]) {
const { id } = await (
await this.authenticatedTurbo()
).uploadFile({
fileStreamFactory: () => Readable.from(dataBuffer),
fileSizeFactory: () => dataBuffer.byteLength,
dataItemOpts: { tags },
});

return {
storageId: id,
storageData: dataBuffer,
};
}

async retrieveBundle(storageId: string, timeout: number) {
const { data: storageData } = await axios.get(
`https://arweave.net/${storageId}`,
{ responseType: "arraybuffer", timeout }
);

return { storageId, storageData };
}
}
4 changes: 4 additions & 0 deletions common/protocol/src/reactors/storageProviders/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { IStorageProvider, Validator } from "../..";
import { Arweave } from "./Arweave";
import { Bundlr } from "./Bundlr";
import { Kyve } from "./Kyve";
import { Turbo } from "./Turbo";
import { NoStorageProvider } from "./NoStorageProvider";

/**
Expand All @@ -12,6 +13,7 @@ import { NoStorageProvider } from "./NoStorageProvider";
* 1 - Arweave
* 2 - Bundlr
* 3 - Kyve
* 4 - Turbo
* x - NoStorageProvider (default)
*
* @method storageProviderFactory
Expand All @@ -25,6 +27,8 @@ export function storageProviderFactory(this: Validator): IStorageProvider {
return new Bundlr(this.storagePriv);
case 3:
return new Kyve(this.chainId, this.poolId, this.staker, this.valaccount);
case 4:
return new Turbo(this.storagePriv || this.valaccount);
default:
return new NoStorageProvider();
}
Expand Down
Loading

0 comments on commit 35cec1a

Please sign in to comment.