npm create wagmicd <your project name>npm i @secret-network/share-documentimport { http, createConfig } from 'wagmi'
import { polygon } from 'wagmi/chains'
import { coinbaseWallet, injected, walletConnect } from 'wagmi/connectors'
export const config = createConfig({
chains: [polygon], // Add Polygon chain
connectors: [
injected(),
coinbaseWallet({ appName: 'Create Wagmi' }),
// walletConnect({ projectId: process.env.NEXT_PUBLIC_WC_PROJECT_ID }),
],
ssr: true,
transports: {
[polygon.id]: http(), // Add polygon chain
},
})
declare module 'wagmi' {
interface Register {
config: typeof config
}
}ENVIRONMENT= "mainnet" ENVIRONMENT can use the following variables depending on which dev environment you are using:
"mainnet" | "testnet" | "local"import { scroll } from "viem/chains";
import { Config, EvmChain } from "@secret-network/share-document"
const config = new Config({
sourceChain: EvmChain.SCROLL,
customChain: scroll
});'use client'
import { useAccount, useConnect, useDisconnect, useWalletClient } from 'wagmi'
import { Config, SecretDocumentClient, Environment, MetaMaskWallet, PinataStorage } from "@secret-network/share-document";
import { useEffect, useState } from 'react';
function App() {
const { address } = useAccount();
const { connectors, connect, status, error } = useConnect();
const { disconnect } = useDisconnect();
const { data: walletClient } = useWalletClient();
const [client, setClient] = useState<SecretDocumentClient>();
const [downloadFileId, setDownloadFileId] = useState('');
const [shareFileId, setShareFileId] = useState('');
const [secretAddress, setSecretAddress] = useState('');
}// Initialize user wallet for the SDK
useEffect(() => {
const initializeClient = async () => {
if (!walletClient) return;
const config = new Config({ env: Environment.MAINNET });
config.useEvmWallet({ client: walletClient });
console.log("config: ", config);
const wallet = await MetaMaskWallet.create(window.ethereum, address || "");
config.useSecretWallet(wallet);
const gateway = "https://gateway.pinata.cloud";
const accessToken = "Your JWT token";
const pinataStorage = new PinataStorage(gateway, accessToken);
config.useStorage(pinataStorage);
const secretClient = new SecretDocumentClient(config);
setClient(secretClient);
console.log("client: ", secretClient);
};
initializeClient();
}, [walletClient, address]);// Store File to Secret Network
const storeFile = async () => {
if (!client) {
console.error('Client is not initialized');
return;
}
try {
const file = new File(["Hello, world!"], "hello.txt", { type: "text/plain" });
const res = await client.storeDocument().fromFile(file);
console.log('File stored successfully:', res);
} catch (error) {
console.error('Error storing file:', error);
}
}; // View file
const viewFile = async () => {
if (!client) {
console.error('Client is not initialized');
return;
}
try {
const res = await client.viewDocument().getAllFileIds();
console.log('File viewed successfully:', res);
} catch (error) {
console.error('Error viewing file:', error);
}
};// Download file
const downloadFile = async (fileId: string) => {
if (!client) {
console.error('Client is not initialized');
return;
}
try {
const uint8Array = await client.viewDocument().download(fileId);
// Convert Uint8Array to Blob
const blob = new Blob([uint8Array], { type: 'application/octet-stream' });
// Create a link element
const link = document.createElement('a');
link.href = URL.createObjectURL(blob);
link.download = 'downloaded_file'; // Set the desired file name here
// Append the link to the body
document.body.appendChild(link);
// Programmatically click the link to trigger the download
link.click();
// Remove the link from the document
document.body.removeChild(link);
console.log('File downloaded successfully');
} catch (error) {
console.error('Error downloading file:', error);
}
};// Share file
const shareFile = async (fileId: string, secretAddress: string) => {
if (!client) {
console.error('Client is not initialized');
return;
}
try {
const shareDocument = client.shareDocument(fileId);
// Get existing file access
// const fileAccess = await shareDocument.getFileAccess();
// console.log('Existing file access:', fileAccess);
// Share viewing access to a file
const addViewingRes = await shareDocument.addViewing([secretAddress]);
console.log('Viewing access added:', addViewingRes);
// Delete viewing access to a file
// const deleteViewingRes = await shareDocument.deleteViewing([secretAddress]);
// console.log('Viewing access deleted:', deleteViewingRes);
// Transfer the ownership
// const changeOwnerRes = await shareDocument.changeOwner(secretAddress);
// console.log('Ownership transferred:', changeOwnerRes);
// All in one share operation
const shareRes = await shareDocument.share({
// changeOwner: secretAddress,
addViewing: [secretAddress],
// deleteViewing: [secretAddress],
});
console.log('All-in-one share operation completed:', shareRes);
} catch (error) {
console.error('Error sharing file:', error);
}
};