How to add or remove a user from a FunWallet
Add or remove wallet users.
Overview
If you ever want to share access to your FunWallet, you can add a user to your FunWallet. In this guide, I’m going to show you how to add a user to your FunWallet. Then I will show you how to remove that same user. This will build off of knowledge in the Quick Start but I’ll quickly walk through all the steps again.
Import required objects
import {FunWallet,configureEnvironment,Auth,generatePrivateKey,} from "@funkit/core";
Configuring the FunWallet environment
To begin, we need to set up the environment for your FunWallet. We’ll be using the Goerli testnet and a gasless sponsor. Please visit our dashboard to receive your own production apiKey.
await configureEnvironment({chain: "goerli",gasSponsor: {sponsorAddress: "0xCB5D0b4569A39C217c243a436AC3feEe5dFeb9Ad",},apiKey: "ZrhepzWGxm74D0sqstuhT6dGrJxhoy8SZIToX6I3",});
It is important to note that the use of a gasSponsor is tied to your apiKey.
Creating Auth with a private key
We’ll use a private key as the authentication method and I’ll provide us with one.
const privateKey = generatePrivateKey();const auth = new Auth({ privateKey });
Creating FunWallet with Auth
With the Auth instance that we created in the step before, we are now able to initialize and create your FunWallet.
const funWallet = new FunWallet({users: [{ userId: auth.getAddress() }],uniqueId: auth.getWalletUniqueId(),});
Adding a user
Now that we’ve created a FunWallet, we can add another user to our wallet. I’m going to use another public wallet address that I own.
const NEW_OWNER_ADDRESS = "0x1111111111111111111111111111111111111111";
Then we create an operation where we pass in this address to add it as a user.
const addOwnerOp = await funWallet.addOwner(auth, userId, {ownerId: NEW_OWNER_ADDRESS,});await funWallet.executeOperation(auth, addOwnerOp);
Removing a user
We can also remove a user that we just added in a similar fashion.
const removeOwnerOperation = await funWallet.removeOwner(auth,await auth.getAddress(),{ ownerId: NEW_OWNER_ADDRESS });await funWallet.executeOperation(auth, removeOwnerOperation);
Summary
Putting together all the pieces we have addAndRemoveUser() that you can use to add a user.
single-auth.js
import { FunWallet, Auth, configureEnvironment, GlobalEnvOption, generatePrivateKey } from "@funkit/core"// Import keysconst PRIVATE_KEY = generatePrivateKey()const API_KEY = "ZrhepzWGxm74D0sqstuhT6dGrJxhoy8SZIToX6I3"const NEW_OWNER_ADDRESS = "0x1111111111111111111111111111111111111111"// Configure environment optionsconst options: GlobalEnvOption = {chain: "goerli",gasSponsor: {sponsorAddress: "0xCB5D0b4569A39C217c243a436AC3feEe5dFeb9Ad"},apiKey: API_KEY}const addAndRemoveUser = async () => {await configureEnvironment(options)const auth = new Auth({ privateKey: PRIVATE_KEY })const userId = await auth.getUserId()const funWallet = new FunWallet({users: [{ userId: userId }],uniqueId: await auth.getWalletUniqueId(Math.random())})const addUserOp = await funWallet.addOwner(auth, userId, { ownerId: NEW_OWNER_ADDRESS })const add_receipt = await funWallet.executeOperation(auth, addUserOp)console.log("Add User", add_receipt)const removeOwnerOperation = await funWallet.removeOwner(auth, await auth.getAddress(), { ownerId: NEW_OWNER_ADDRESS })const remove_receipt = await funWallet.executeOperation(auth, removeOwnerOperation)console.log("Remove User", remove_receipt)}addAndRemoveUser()