From 87c6db5454e01f08f9e38b9a04870a823694eba8 Mon Sep 17 00:00:00 2001 From: alessandrokonrad Date: Wed, 22 Jan 2025 23:08:30 +0100 Subject: [PATCH] allow wallet selection in directly in lucid instantiation --- examples/emulate_something.ts | 8 ++++---- src/core/types.ts | 13 +++++++++++++ src/lucid/lucid.ts | 16 +++++++++++++++- 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/examples/emulate_something.ts b/examples/emulate_something.ts index 0796473d..e7a694fc 100644 --- a/examples/emulate_something.ts +++ b/examples/emulate_something.ts @@ -12,10 +12,10 @@ const { payment } = Addresses.inspect(address); const emulator = new Emulator([{ address, assets: { lovelace: 3000000000n } }]); -const lucid = new Lucid({ provider: emulator }) - .selectWalletFromPrivateKey( - privateKey, - ); +const lucid = new Lucid({ + provider: emulator, + wallet: { PrivateKey: privateKey }, +}); const mintingPolicy = lucid.newScript({ type: "All", diff --git a/src/core/types.ts b/src/core/types.ts index 7123f46b..b82af425 100644 --- a/src/core/types.ts +++ b/src/core/types.ts @@ -90,6 +90,19 @@ export interface Wallet { submit(tx: string): Promise; } +export type WalletSelection = + | { PrivateKey: string } + | { Api: WalletApi } + | { + Seed: { + seed: string; + options?: { type?: "Base" | "Enterprise"; index?: number }; + }; + } + | { + ReadOnly: ReadOnlyWallet; + }; + // deno-lint-ignore no-explicit-any export type Json = any; diff --git a/src/lucid/lucid.ts b/src/lucid/lucid.ts index eee67b3e..7b78ccdd 100644 --- a/src/lucid/lucid.ts +++ b/src/lucid/lucid.ts @@ -28,6 +28,7 @@ import { Utxo, Wallet, WalletApi, + WalletSelection, } from "../mod.ts"; import { signMessage, verifyMessage } from "../misc/sign_message.ts"; import { resolveInstructions } from "./mod.ts"; @@ -52,9 +53,10 @@ export class Lucid { }; constructor( - { provider, network }: { + { provider, network, wallet }: { provider?: Provider; network?: Network; + wallet?: WalletSelection; } = {}, ) { if (provider) this.provider = provider; @@ -113,6 +115,18 @@ export class Lucid { (slots - slotConfig.zeroSlot) * slotConfig.slotLength; }, }; + + if (wallet) { + if ("PrivateKey" in wallet) { + this.selectWalletFromPrivateKey(wallet.PrivateKey); + } else if ("Seed" in wallet) { + this.selectWalletFromSeed(wallet.Seed.seed, wallet.Seed.options); + } else if ("Api" in wallet) { + this.selectWalletFromApi(wallet.Api); + } else if ("ReadOnly" in wallet) { + this.selectReadOnlyWallet(wallet.ReadOnly); + } + } } newScript(