()
| 220 | }; |
| 221 | |
| 222 | export async function resolveOrGenerateWalletKey(): Promise<WalletResolution> { |
| 223 | // 1. Previously saved wallet |
| 224 | const saved = await loadSavedWallet(); |
| 225 | if (saved) { |
| 226 | const account = privateKeyToAccount(saved as `0x${string}`); |
| 227 | |
| 228 | // Load mnemonic if it exists (Solana support enabled via /wallet solana) |
| 229 | const mnemonic = await loadMnemonic(); |
| 230 | if (mnemonic) { |
| 231 | const solanaKeyBytes = deriveSolanaKeyBytes(mnemonic); |
| 232 | return { |
| 233 | key: saved, |
| 234 | address: account.address, |
| 235 | source: "saved", |
| 236 | mnemonic, |
| 237 | solanaPrivateKeyBytes: solanaKeyBytes, |
| 238 | }; |
| 239 | } |
| 240 | |
| 241 | return { key: saved, address: account.address, source: "saved" }; |
| 242 | } |
| 243 | |
| 244 | // 2. Environment variable |
| 245 | const envKey = process["env"].BLOCKRUN_WALLET_KEY; |
| 246 | if (typeof envKey === "string" && envKey.startsWith("0x") && envKey.length === 66) { |
| 247 | const account = privateKeyToAccount(envKey as `0x${string}`); |
| 248 | |
| 249 | // Load mnemonic if it exists (Solana support enabled via /wallet solana) |
| 250 | const mnemonic = await loadMnemonic(); |
| 251 | if (mnemonic) { |
| 252 | const solanaKeyBytes = deriveSolanaKeyBytes(mnemonic); |
| 253 | return { |
| 254 | key: envKey, |
| 255 | address: account.address, |
| 256 | source: "env", |
| 257 | mnemonic, |
| 258 | solanaPrivateKeyBytes: solanaKeyBytes, |
| 259 | }; |
| 260 | } |
| 261 | |
| 262 | return { key: envKey, address: account.address, source: "env" }; |
| 263 | } |
| 264 | |
| 265 | // 3. Auto-generate with BIP-39 mnemonic (new users get both chains) |
| 266 | const result = await generateAndSaveWallet(); |
| 267 | return { |
| 268 | key: result.key, |
| 269 | address: result.address, |
| 270 | source: "generated", |
| 271 | mnemonic: result.mnemonic, |
| 272 | solanaPrivateKeyBytes: result.solanaPrivateKeyBytes, |
| 273 | }; |
| 274 | } |
| 275 | |
| 276 | /** |
| 277 | * Recover wallet.key from existing mnemonic. |
no test coverage detected