Skip to content

Commit

Permalink
feat: add eth and sol uploading
Browse files Browse the repository at this point in the history
  • Loading branch information
fedellen committed Oct 3, 2024
1 parent bc29701 commit cb0eee0
Show file tree
Hide file tree
Showing 4 changed files with 887 additions and 210 deletions.
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v20.12.2
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"test": "echo \"TODO: add tests\" && exit 0"
},
"dependencies": {
"@ardrive/turbo-sdk": "^1.10.0",
"@ardrive/turbo-sdk": "1.19.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-router-dom": "^6.20.1"
Expand Down
128 changes: 116 additions & 12 deletions src/pages/UploadPage.tsx
Original file line number Diff line number Diff line change
@@ -1,38 +1,130 @@
import { InputHTMLAttributes, useEffect, useState } from "react";
import { InputHTMLAttributes, useState } from "react";
import { ErrMsgCallbackAsProps } from "../types";
import { ardriveAppUrl, turboConfig } from "../constants";
import { Page } from "./Page";
import {
TurboAuthenticatedClient,
TurboFactory,
ArconnectSigner,
SolanaWalletAdapter,
} from "@ardrive/turbo-sdk/web";
import { getArconnect } from "../utils/arconnect";
import { NewToArDriveInfo } from "../components/NewToArDriveInfo";

import { Eip1193Provider, ethers } from "ethers";
import { PublicKey } from "@solana/web3.js";

declare global {
interface Window {
ethereum: {
isMetaMask?: boolean;
providers?: Array<Eip1193Provider & { isMetaMask: boolean }>;
request: (args: {
method: string;
params?: Array<unknown>;
}) => Promise<unknown>;
on: (eventName: string, callback: (...args: unknown[]) => void) => void;
};
solana: {
connect: () => Promise<{ publicKey: PublicKey }>;
signMessage: (message: Uint8Array) => Promise<{ signature: Uint8Array }>;
};
}
}
interface ExtendedFileInputProps extends InputHTMLAttributes<HTMLInputElement> {
webkitdirectory?: boolean;
}

function UploadForm({ errorCallback }: ErrMsgCallbackAsProps) {
const [selectedFiles, setSelectedFiles] = useState<FileList | null>(null);
const [currentWallet, setCurrentWallet] = useState<string | null>(null);

const [turbo, setTurbo] = useState<undefined | TurboAuthenticatedClient>(
undefined,
);
const [sending, setSending] = useState<boolean>(false);

useEffect(() => {
getArconnect().then(async () => {
const connectEthWallet = async () => {
try {
// Check if MetaMask is installed

if (window.ethereum) {
// Find the MetaMask provider in the list
const metaMaskProvider = window.ethereum.providers?.find(
(provider) => provider.isMetaMask,
);

const provider = new ethers.BrowserProvider(
metaMaskProvider ?? window.ethereum,
); // Use the MetaMask provider
const signer = await provider.getSigner();

setTurbo(
TurboFactory.authenticated({
token: "ethereum",
walletAdapter: {
getSigner: () => signer,
},
}),
);
setCurrentWallet("ethereum");
}
} catch (err) {
console.error(err);
}
};

const connectSolWallet = async () => {
try {
// Check if Phantom is installed
if (window.solana) {
const provider = window.solana;

const publicKey = await provider.connect();
console.log("publicKey", publicKey);

const pkey = new PublicKey(publicKey.publicKey.toString());
console.log("pkey", pkey);

const wallet: SolanaWalletAdapter = {
publicKey: pkey,
signMessage: async (message: Uint8Array) => {
// Call Phantom's signMessage method
const { signature } = await provider.signMessage(message);

return signature;
},
};

setTurbo(
TurboFactory.authenticated({
token: "solana",
walletAdapter: wallet,
}),
);
setCurrentWallet("solana");
}
} catch (err) {
console.error(err);
}
};

const connectArWallet = async () => {
try {
await getArconnect();
const arconnect_signer = new ArconnectSigner(window.arweaveWallet);

const turbo = TurboFactory.authenticated({
signer: arconnect_signer,
...turboConfig,
});
setTurbo(turbo);
});
}, []);
setTurbo(
TurboFactory.authenticated({
token: "arweave",
signer: arconnect_signer,
...turboConfig,
}),
);
setCurrentWallet("arweave");
} catch (err) {
console.error(err);
}
};

// TODO: Query params if needed
// const location = useLocation();
Expand All @@ -49,17 +141,19 @@ function UploadForm({ errorCallback }: ErrMsgCallbackAsProps) {
return;
}

console.log("turbo", turbo);
setSending(true);
turbo
.uploadFolder({
files: Array.from(selectedFiles),
maxConcurrentUploads: 1,
})
.catch((err: unknown) => {
console.log("err", err);
console.error("err", err);
errorCallback(
`Error uploading: ${err instanceof Error ? err.message : err}`,
);
// throw err;
})
.then((res: unknown) => {
console.log("res", res);
Expand Down Expand Up @@ -113,6 +207,16 @@ function UploadForm({ errorCallback }: ErrMsgCallbackAsProps) {
</button>
</form>

<div>
<h3>Connect Wallet </h3>
<p>Current wallet type: {currentWallet ? currentWallet : "None"}</p>
<div>
<button onClick={connectEthWallet}>Connect Ethereum Wallet</button>
<button onClick={connectSolWallet}>Connect Solana Wallet</button>
<button onClick={connectArWallet}>Connect Arweave Wallet</button>
</div>
</div>

<NewToArDriveInfo />
</>
);
Expand Down
Loading

0 comments on commit cb0eee0

Please sign in to comment.