import { AeonClient, AeonSigner } from "@aeon-protocol/aeon-sdk"; // Assuming AeonClient is available from the SDK
import { Wallet } from "ethers"; // Example: if AeonSigner is created from an ethers Wallet
// Assume 'aeonSigner' is an initialized AeonSigner instance
// const privateKeyHex = "your-private-key-hex-without-0x";
// const aeonSigner = new AeonSigner({ privateKey: Buffer.from(privateKeyHex, "hex") });
// Assume 'client' is an initialized AeonClient instance
// const client = new AeonClient({ /* ... AeonClient config ... */ });
async function joinWorkspaceWithClient(
client: AeonClient,
aeonSigner: AeonSigner, // AeonSigner needed to generate the signature
workspaceId: string
) {
try {
console.log(
`Generating signature to accept invitation for workspace: ${workspaceId}...`
);
// 1. Generate the signature using AeonSigner
const signature = await aeonSigner.signAcceptWorkspaceMessage(workspaceId);
console.log(`Signature obtained: ${signature}`);
console.log(
`Attempting to join workspace via AeonClient: ${workspaceId} with signature...`
);
// 2. Call acceptWorkspaceInvite on AeonClient with the workspaceId and signature
await client.acceptWorkspaceInvite(workspaceId, signature);
console.log(`Successfully joined workspace: ${workspaceId}`);
// Additional logic here, e.g., fetching workspace details or roles
} catch (error) {
console.error(
`Failed to join workspace ${workspaceId} via AeonClient:`,
error
);
// Handle specific errors if needed
}
}
// Example usage:
// const privateKey = Wallet.createRandom().privateKey.slice(2); // Example private key
// const signerInstance = new AeonSigner({ privateKey: Buffer.from(privateKey, "hex") });
// const clientInstance = new AeonClient({ /* ... appropriate AeonClient configuration ... */ });
// const workspaceToJoin = "some-unique-workspace-id"; // Replace with actual workspace ID
// joinWorkspaceWithClient(clientInstance, signerInstance, workspaceToJoin);