Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ReadOnly Wallet #1

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/lib/wallet/adapters/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,15 @@ export async function adapterToIWallet(adapter: WalletAdapter): Promise<Connecta
case WalletAdapter.XDefi:
const { XDefi } = await import("./xdefi");
return XDefi;
case WalletAdapter.Readonly:
const { ReadOnly } = await import("./readonly");
return ReadOnly;
default:
return null;
}
}

export const WALLETS: WalletAdapter[] = [WalletAdapter.Sonar, /*WalletAdapter.MetaMask,*/ WalletAdapter.Keplr, WalletAdapter.Leap, WalletAdapter.Station, WalletAdapter.XDefi];
export const WALLETS: WalletAdapter[] = [WalletAdapter.Sonar, /*WalletAdapter.MetaMask,*/ WalletAdapter.Keplr, WalletAdapter.Leap, WalletAdapter.Station, WalletAdapter.XDefi, WalletAdapter.Readonly];

let hasSetupEventListeners = false;
export function setupEventListeners(): void {
Expand Down
42 changes: 42 additions & 0 deletions src/lib/wallet/adapters/readonly.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { get, writable } from "svelte/store";
import type { GasPrice, StdFee } from "@cosmjs/stargate";
import type { TendermintClient } from "@cosmjs/tendermint-rpc";
import type { AccountData as CosmAccountData, EncodeObject } from "@cosmjs/proto-signing";
import { WalletAdapter, type AccountData, type WalletMetadata, ConnectionError, type ISigner } from "./types";
import { Eye } from "lucide-svelte";

export let readonlySignerAddress = writable<string | null>(null);

export class ReadOnly implements ISigner {
private constructor(private acc: AccountData) { }

public static async connect(chain: string): Promise<ReadOnly> {
const address = get(readonlySignerAddress);
if (!address) throw new Error("Readonly Signer Address is null");
//TODO: Missing wallet input from user
const account: AccountData = {
address,
pubkey: { type: '', value: '' }
}

return new ReadOnly(account);
}
public disconnect() { /* noop */ }

public static metadata: WalletMetadata = {
adapter: WalletAdapter.Readonly,
name: 'Read Only',
logo: Eye,
canSign: false,
}

public getMetadata(): WalletMetadata { return ReadOnly.metadata; }

public static async isInstalled(): Promise<boolean> { return true; }

public account(): AccountData { return this.acc; }

public async sign(_client: TendermintClient, _msgs: EncodeObject[], _fee: StdFee, _memo?: string | undefined): Promise<Uint8Array> {
throw new Error("Readonly signer cannot sign");
}
}
3 changes: 2 additions & 1 deletion src/lib/wallet/adapters/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type { XDefi } from "./xdefi";
import type { StdFee } from "@cosmjs/stargate";
import type { TendermintClient } from "@cosmjs/tendermint-rpc";
import type { Pubkey } from "@cosmjs/amino";
import type { ReadOnly } from "./readonly";

export interface AccountData {
address: string;
Expand Down Expand Up @@ -43,7 +44,7 @@ type ISignerStatic<T> = {
isInstalled(): Promise<boolean>;
metadata: WalletMetadata;
};
export type Connectable = ISignerStatic<Keplr> | ISignerStatic<Sonar> | ISignerStatic<Leap> | ISignerStatic<MetaMask> | ISignerStatic<Station> | ISignerStatic<XDefi>;
export type Connectable = ISignerStatic<Keplr> | ISignerStatic<Sonar> | ISignerStatic<Leap> | ISignerStatic<MetaMask> | ISignerStatic<Station> | ISignerStatic<XDefi> | ISignerStatic<ReadOnly>;

export interface ISigner {
disconnect: () => void;
Expand Down
4 changes: 3 additions & 1 deletion src/routes/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import { TxStep, broadcastTx, simulate } from "$lib/helpers/transaction";
import { writable } from "svelte/store";
import type { EncodeObject } from "@cosmjs/proto-signing";
import { readonlySignerAddress } from "$lib/wallet/adapters/readonly";

const status = writable<TxStep>(TxStep.None);

Expand Down Expand Up @@ -64,8 +65,9 @@
</div>
<h3 class="text-xl">WalletWidget</h3>
<p>A component to allow the user to connect their wallet.</p>
<div class="w-full flex items-center justify-center m-4">
<div class="w-full flex flex-col items-center justify-center m-4 gap-2">
<WalletWidget />
<input type="text" bind:value={$readonlySignerAddress} class="p-2 border rounded-lg" placeholder="Readonly Address" />
</div>
<h3 class="text-xl">Example Transaction</h3>
<div class="flex flex-col items-start">
Expand Down