Skip to content

Commit

Permalink
add okx wallet
Browse files Browse the repository at this point in the history
  • Loading branch information
codingki committed Sep 9, 2024
1 parent 1596bf1 commit d947461
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 8 deletions.
1 change: 1 addition & 0 deletions packages/graz/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
"zustand": "^4.5.2"
},
"devDependencies": {
"@initia/initia-registry-types": "^0.0.20",
"@types/node": "^18.17.15",
"@types/react": "^18.2.21",
"@types/react-dom": "^18.2.7",
Expand Down
45 changes: 45 additions & 0 deletions packages/graz/src/actions/wallet/initia.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import type { OfflineDirectSigner } from "@cosmjs/proto-signing";
import type { DeliverTxResponse } from "@cosmjs/stargate";
import type { Chain } from "@initia/initia-registry-types";

import { useGrazInternalStore } from "../../store";
import type { SignAminoParams, SignDirectParams, Wallet } from "../../types/wallet";
import { clearSession } from ".";

export interface InitiaWallet {
version: string;
getAddress: (chainId: string) => Promise<string>;
signAndBroadcast: (chainId: string, txBody: Uint8Array) => Promise<DeliverTxResponse>;
getOfflineSigner: (chainId: string) => OfflineDirectSigner;
requestAddInitiaLayer: (chain: Partial<Chain>) => Promise<void>;
signArbitrary: (data: string | Uint8Array) => Promise<string>;
verifyArbitrary: (data: string | Uint8Array, signature: string) => Promise<boolean>;
}

export const getInitia = (): Wallet => {
if (typeof window.initia !== "undefined") {
const initia = window.initia;

const enable = async () => {};
const getKey = async (chainId: string) => {};
const getOfflineSigner = (chainId: string) => {};
const getOfflineSignerAuto = async (chainId: string) => {};
const getOfflineSignerOnlyAmino = (chainId: string) => {};
const experimentalSuggestChain = (chain: Partial<Chain>) => {};
const signDirect = async (...args: SignDirectParams) => {};
const signAmino = async (...args: SignAminoParams) => {};

const subscription: (reconnect: () => void) => () => void = (reconnect) => {
const listener = () => {
clearSession();
reconnect();
};
window.addEventListener("initia_keystorechange", listener);
return () => {
window.removeEventListener("initia_keystorechange", listener);
};
};
}
useGrazInternalStore.getState()._notFoundFn();
throw new Error("window.initia is not defined");
};
46 changes: 46 additions & 0 deletions packages/graz/src/actions/wallet/okx.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import type { KeplrIntereactionOptions } from "@keplr-wallet/types";

import { useGrazInternalStore } from "../../store";
import type { Wallet } from "../../types/wallet";
import { clearSession } from ".";
/**
* Function to return okxwallet object (which is {@link Wallet}) and throws and error if it does not exist on `window`.
*
* @example
* ```ts
* try {
* const okxWallet = getOkx();
* } catch (error: Error) {
* console.error(error.message);
* }
* ```
*
* @see https://www.okx.com/web3/build/docs/sdks/chains/cosmos/provider
*/
export const getOkx = (): Wallet => {
if (typeof window.okxwallet?.keplr !== "undefined") {
const okxWallet = window.okxwallet.keplr;
const subscription: (reconnect: () => void) => () => void = (reconnect) => {
const listener = () => {
clearSession();
reconnect();
};
window.okxwallet?.on("accountsChanged", listener);
return () => {
window.okxwallet?.removeListener("accountsChanged", listener);
};
};

const setDefaultOptions = (options: KeplrIntereactionOptions) => {
okxWallet.defaultOptions = options;
};
const res = Object.assign(okxWallet, {
subscription,
setDefaultOptions,
});
return res;
}

useGrazInternalStore.getState()._notFoundFn();
throw new Error("window.okxwallet.keplr is not defined");
};
7 changes: 6 additions & 1 deletion packages/graz/types/global.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import type Station from "@terra-money/station-connector";

import { InitiaWallet } from "../src/actions/wallet/initia";

type KeplrWindow = import("@keplr-wallet/types").Window;
type VectisWindow = import("@vectis/extension-client").VectisWindow;

Expand All @@ -17,10 +19,13 @@ declare global {
};
};
ethereum?: import("@metamask/providers").MetaMaskInpageProvider;
okxwallet?: import("@metamask/providers").BaseProvider;
okxwallet?: import("@metamask/providers").BaseProvider & {
keplr: KeplrWindow["keplr"];
};
station?: Station;
xfi?: {
keplr: KeplrWindow["keplr"];
};
initia?: InitiaWallet;
}
}
24 changes: 17 additions & 7 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d947461

Please sign in to comment.