How to add multiple users to a FunWallet

Add multiple users to your wallet.

Overview

If you want to create a shared wallet, it is difficult to create an easy and secure solution. In this guide, I’m going to show you how to create a FunWallet that has 2 users. 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.

Setting up two users

Now that we’ve set up the environment, we need to define the two users of the FunWallet.

For the first user, lets use a private key like in the Quick Start. For the second user, I’m going to pick a public address that I own.

//User 1
const privateKey = generatePrivateKey();
const auth = new Auth({ privateKey });
//User 2
const NEW_USER_ADDRESS = "0x1111111111111111111111111111111111111111";

Creating FunWallet with two users

With the two users that we created in the step before, we are now able to initialize your FunWallet. Here are the FunWallet constructor parameters

  1. users - This is an User[] that holds all users that can access your FunWallet. Since we’re using 1 private key and a public address, our User[] has 2 instances.

  2. uniqueId - This is a random seed that is generated from our Auth instance. The purpose of this seed is to generate the address of our FunWallet.

const funWallet = new FunWallet({
users: [{ userId: await auth.getUserId() }, { userId: NEW_USER_ADDRESS }],
uniqueId: await auth.getWalletUniqueId(Math.random()),
});

Note that uniqueId can be either a 32 byte id or a public address. Both are acceptable user identifiers.

userId exists in order to create more abstract notions of users. This feature enables a multi-sig user without the overhead of deploying an additional contract.

Summary

Putting together all the pieces we have createWallet() that you can use to create your FunWallet with two users.

,
connect.js
import { FunWallet, Auth, configureEnvironment, GlobalEnvOption, generatePrivateKey } from "@funkit/core"
// Import keys
const PRIVATE_KEY = generatePrivateKey()
const API_KEY = "ZrhepzWGxm74D0sqstuhT6dGrJxhoy8SZIToX6I3"
const NEW_USER_ADDRESS = "0x1111111111111111111111111111111111111111"
// Configure environment options
const options: GlobalEnvOption = {
chain: "goerli",
gasSponsor: {
sponsorAddress: "0xCB5D0b4569A39C217c243a436AC3feEe5dFeb9Ad"
},
apiKey: API_KEY
}
const createWallet = async () => {
await configureEnvironment(options)
const auth = new Auth({ privateKey: PRIVATE_KEY })
const userId = await auth.getUserId()
const funWallet = new FunWallet({
users: [{ userId: userId }, { userId: NEW_USER_ADDRESS }],
uniqueId: await auth.getWalletUniqueId(Math.random())
})
const createWalletOp = await funWallet.create(auth, userId)
const receipt = await funWallet.executeOperation(auth, createWalletOp)
console.log("Initialize wallet with multiple users", receipt)
}
createWallet()