-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add new storage provider ArDrive Turbo (#158)
* feat: added turbo storage provider
- Loading branch information
1 parent
7d6edb9
commit 35cec1a
Showing
4 changed files
with
522 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.