import { turnkeyServer } from "./turnkey";
import { DEFAULT_ETHEREUM_ACCOUNTS } from "@turnkey/sdk-browser";
async function createMultiSigWallet(
userId,
userEmail,
userPasskeyChallenge,
userPasskeyAttestation
) {
// Create a sub-organization with two root users and a threshold of 2
const subOrg = await turnkeyServer.createSubOrganization({
organizationId: process.env.TURNKEY_ORGANIZATION_ID,
subOrganizationName: `Multi-Sig Wallet for ${userEmail}`,
rootUsers: [
// First root user - the end user with their passkey
{
userName: "End User",
userEmail: userEmail,
apiKeys: [],
authenticators: [
{
authenticatorName: "User Passkey",
challenge: userPasskeyChallenge,
attestation: userPasskeyAttestation,
},
],
},
// Second root user - your application's service account
{
userName: "Application Service",
userEmail: "service@yourapp.com",
apiKeys: [
{
apiKeyName: "Service API Key",
publicKey: process.env.SERVICE_API_PUBLIC_KEY,
curveType: "API_KEY_CURVE_SECP256K1",
},
],
authenticators: [],
},
],
// This is the key setting - requiring both users to approve
rootQuorumThreshold: 2,
wallet: {
walletName: "Shared Wallet",
accounts: DEFAULT_ETHEREUM_ACCOUNTS,
},
});
// Save the sub-organization ID in your database
await db.users.update({
where: { id: userId },
data: { turnkeySubOrgId: subOrg.organizationId },
});
return subOrg;
}