-
-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add metamask snap cosmos wallet integration (#148)
- Loading branch information
Showing
17 changed files
with
175 additions
and
232 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
packages/graz/src/actions/wallet/cosmos-metamask-snap/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { CosmosSnap, installSnap, isSnapInstalled } from "@cosmsnap/snapper"; | ||
|
||
import { useGrazInternalStore } from "../../../store"; | ||
import type { KnownKeys, Wallet } from "../../../types/wallet"; | ||
import type { ChainId } from "../../../utils/multi-chain"; | ||
|
||
const metamaskSnapCosmosKeysMap: KnownKeys = {}; | ||
|
||
export const getMetamaskSnapCosmos = (): Wallet => { | ||
const ethereum = window.ethereum; | ||
let cosmos = window.cosmos; | ||
if (ethereum) { | ||
const init = async () => { | ||
const clientVersion = await ethereum.request({ | ||
method: "web3_clientVersion", | ||
}); | ||
|
||
const isMetamask = (clientVersion as string).includes("MetaMask"); | ||
|
||
if (!isMetamask) throw new Error("Metamask is not installed"); | ||
|
||
if (typeof window.okxwallet !== "undefined") { | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-expect-error | ||
if (window.okxwallet.isOkxWallet) { | ||
throw new Error("You have OKX Wallet installed. Please disable and reload the page to use Metamask Snap."); | ||
} | ||
} | ||
const version = (clientVersion as string).split("MetaMask/v")[1]?.split(".")[0]; | ||
const isSupportMMSnap = Number(version) >= 11; | ||
if (!isSupportMMSnap) throw new Error("Metamask Snap is not supported in this version"); | ||
|
||
const installedSnap = await isSnapInstalled(); | ||
if (!installedSnap) await installSnap(); | ||
window.cosmos = new CosmosSnap(); | ||
cosmos = window.cosmos; | ||
return true; | ||
}; | ||
|
||
const enable = async (chainId: ChainId) => { | ||
const installedSnap = await isSnapInstalled(); | ||
if (!installedSnap) await installSnap(); | ||
}; | ||
|
||
const getOfflineSignerAuto = async (chainId: string) => { | ||
const key = await cosmos.getKey(chainId); | ||
if (key.isNanoLedger) return cosmos.getOfflineSignerOnlyAmino(chainId); | ||
return cosmos.getOfflineSigner(chainId); | ||
}; | ||
|
||
return { | ||
init, | ||
enable, | ||
getOfflineSigner: (chainId: string) => cosmos.getOfflineSigner(chainId), | ||
experimentalSuggestChain: async (chainInfo) => { | ||
await cosmos.experimentalSuggestChain(chainInfo); | ||
}, | ||
signAmino: async (chainId, signer, signDoc) => { | ||
return cosmos.signAmino(chainId, signer, signDoc); | ||
}, | ||
getKey: async (chainId) => { | ||
if (typeof metamaskSnapCosmosKeysMap[chainId] !== "undefined") return metamaskSnapCosmosKeysMap[chainId]!; | ||
|
||
return cosmos.getKey(chainId); | ||
}, | ||
getOfflineSignerAuto, | ||
getOfflineSignerOnlyAmino: (chainId) => { | ||
return cosmos.getOfflineSignerOnlyAmino(chainId); | ||
}, | ||
signDirect: async (chainId, signer, signDoc) => { | ||
// @ts-expect-error - signDoc is not the same as SignDoc | ||
return cosmos.signDirect(chainId, signer, signDoc); | ||
}, | ||
signArbitrary: async (chainId, signer, data) => { | ||
return cosmos.signArbitrary(chainId, signer, data); | ||
}, | ||
disable: async (chainId) => { | ||
chainId && (await cosmos.deleteChain(chainId)); | ||
}, | ||
}; | ||
} | ||
useGrazInternalStore.getState()._notFoundFn(); | ||
throw new Error("window.ethereum is not defined"); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.