-
Notifications
You must be signed in to change notification settings - Fork 2
/
shadow.ts
27 lines (24 loc) · 1012 Bytes
/
shadow.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import { ShadowFile, ShdwDrive } from "@shadow-drive/sdk"
import { clusterApiUrl, Connection, Keypair, PublicKey } from "@solana/web3.js";
import { SECRET, SHADOW_DIRECTORY } from "../config/constants";
import * as anchor from "@project-serum/anchor";
// Using maininet for image uploads even when testing
// TODO Expiriment with Shadow on Devnet
const connection = new Connection(clusterApiUrl('mainnet-beta'), 'confirmed');
const acctPubKey = new PublicKey(SHADOW_DIRECTORY);
const keypair = Keypair.fromSecretKey(new Uint8Array(SECRET));
const wallet = new anchor.Wallet(keypair);
interface Args {
fileName: string;
image: Buffer;
}
export async function uploadImage({ fileName, image }: Args): Promise<string> {
const drive = await new ShdwDrive(connection, wallet).init();
// UPLOAD IMAGE
const fileToUpload: ShadowFile = {
name: fileName,
file: image
};
const uri = (await drive.uploadFile(acctPubKey, fileToUpload)).finalized_locations[0];
return uri;
}