Skip to content

Commit

Permalink
allow wallet selection in directly in lucid instantiation
Browse files Browse the repository at this point in the history
  • Loading branch information
alessandrokonrad committed Jan 22, 2025
1 parent 5e13430 commit 87c6db5
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
8 changes: 4 additions & 4 deletions examples/emulate_something.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
13 changes: 13 additions & 0 deletions src/core/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,19 @@ export interface Wallet {
submit(tx: string): Promise<string>;
}

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;

Expand Down
16 changes: 15 additions & 1 deletion src/lucid/lucid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -52,9 +53,10 @@ export class Lucid {
};

constructor(
{ provider, network }: {
{ provider, network, wallet }: {
provider?: Provider;
network?: Network;
wallet?: WalletSelection;
} = {},
) {
if (provider) this.provider = provider;
Expand Down Expand Up @@ -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<T extends unknown[] = Data[]>(
Expand Down

0 comments on commit 87c6db5

Please sign in to comment.