-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathswitch-active-wallet.ts
More file actions
87 lines (76 loc) · 3.39 KB
/
Copy pathswitch-active-wallet.ts
File metadata and controls
87 lines (76 loc) · 3.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/**
* Demo: Switch active wallet via the agent-wallet SDK.
*
* This example shows how to use the active wallet feature programmatically:
* 1. Resolve a ConfigWalletProvider
* 2. List all wallets and show which one is active
* 3. Switch the active wallet
* 4. Sign a message using the active wallet (no wallet ID needed)
*
* Prerequisites:
* - agent-wallet init
* - agent-wallet add (add at least two wallets)
*
* Usage:
* AGENT_WALLET_PASSWORD=<your-password> npx tsx examples/switch-active-wallet.ts
*/
import { ConfigWalletProvider, resolveWalletProvider } from "../src/index.js";
// --- Configuration ---
const SECRETS_DIR =
process.env.AGENT_WALLET_DIR ?? `${process.env.HOME}/.agent-wallet`;
const NETWORK = process.env.AGENT_WALLET_NETWORK ?? "eip155";
async function main() {
// ----------------------------------------------------------------
// Step 1: Create provider (decrypts all keys, then discards password)
// ----------------------------------------------------------------
const provider = resolveWalletProvider({ dir: SECRETS_DIR, network: NETWORK });
if (!(provider instanceof ConfigWalletProvider)) {
throw new Error("switch-active-wallet.ts requires a config-backed wallet directory.");
}
// ----------------------------------------------------------------
// Step 2: List wallets and show current active wallet
// ----------------------------------------------------------------
const wallets = await provider.listWallets();
const activeId = provider.getActiveId();
console.log("Available wallets:");
for (const [walletId, conf, isActive] of wallets) {
const marker = isActive ? " *" : "";
console.log(` - ${walletId} (${conf.type})${marker}`);
}
console.log(`\nActive wallet: ${activeId ?? "(none)"}`);
console.log();
// ----------------------------------------------------------------
// Step 3: Sign a message using the active wallet (no ID needed)
// ----------------------------------------------------------------
if (activeId) {
const wallet = await provider.getActiveWallet();
const address = await wallet.getAddress();
const sig = await wallet.signMessage(Buffer.from("Hello from active wallet!"));
console.log(`Signed with active wallet '${activeId}':`);
console.log(` Address: ${address}`);
console.log(` Signature: ${sig}`);
console.log();
}
// ----------------------------------------------------------------
// Step 4: Switch active wallet
// ----------------------------------------------------------------
if (wallets.length < 2) {
console.log("Add at least 2 wallets to demo switching.");
return;
}
// Pick a wallet that is NOT the current active one
const newActive = wallets.find(([walletId]) => walletId !== activeId)![0];
provider.setActive(newActive);
console.log(`Switched active wallet to '${newActive}'`);
console.log();
// ----------------------------------------------------------------
// Step 5: Sign again with the new active wallet
// ----------------------------------------------------------------
const wallet = await provider.getActiveWallet();
const address = await wallet.getAddress();
const sig = await wallet.signMessage(Buffer.from("Hello from active wallet!"));
console.log(`Signed with new active wallet '${newActive}':`);
console.log(` Address: ${address}`);
console.log(` Signature: ${sig}`);
}
main().catch(console.error);