-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: virtual wallet support + Metamask integration (#227)
- Loading branch information
Showing
12 changed files
with
293 additions
and
44 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,22 @@ | ||
export const isWalletObj = (wallet: any): boolean => { | ||
try { | ||
import { fullWalletKeys, virtualWalletKeys } from "../types" | ||
|
||
function createWalletGuard<T>(keys: (keyof T)[]) { | ||
return function hasKeys(obj: unknown): obj is T { | ||
return ( | ||
wallet && | ||
[ | ||
// wallet's must have methods/members, see IStarknetWindowObject | ||
"request", | ||
"on", | ||
"off", | ||
"version", | ||
"id", | ||
"name", | ||
"icon", | ||
].every((key) => key in wallet) | ||
obj !== null && typeof obj === "object" && keys.every((key) => key in obj) | ||
) | ||
} | ||
} | ||
|
||
const isFullWallet = createWalletGuard(fullWalletKeys) | ||
|
||
const isVirtualWallet = createWalletGuard(virtualWalletKeys) | ||
|
||
function isWalletObject(wallet: unknown): boolean { | ||
try { | ||
return isFullWallet(wallet) || isVirtualWallet(wallet) | ||
} catch (err) {} | ||
return false | ||
} | ||
|
||
export { isVirtualWallet, isFullWallet, isWalletObject } |
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,31 @@ | ||
import type { VirtualWallet } from "../../types" | ||
import { metaMaskVirtualWallet } from "./metaMaskVirtualWallet" | ||
import type { StarknetWindowObject } from "@starknet-io/types-js" | ||
|
||
const virtualWallets: VirtualWallet[] = [metaMaskVirtualWallet] | ||
|
||
function initiateVirtualWallets(windowObject: Record<string, unknown>) { | ||
virtualWallets.forEach(async (virtualWallet) => { | ||
const hasSupport = await virtualWallet.hasSupport(windowObject) | ||
if (hasSupport) { | ||
windowObject[virtualWallet.windowKey] = virtualWallet | ||
} | ||
}) | ||
} | ||
|
||
const virtualWalletsMap: Record<string, StarknetWindowObject> = {} | ||
|
||
async function resolveVirtualWallet( | ||
windowObject: Record<string, unknown>, | ||
virtualWallet: VirtualWallet, | ||
) { | ||
let wallet: StarknetWindowObject = virtualWalletsMap[virtualWallet.id] | ||
if (!wallet) { | ||
wallet = await virtualWallet.loadWallet(windowObject) | ||
virtualWalletsMap[virtualWallet.id] = wallet | ||
} | ||
|
||
return wallet | ||
} | ||
|
||
export { initiateVirtualWallets, resolveVirtualWallet } |
Oops, something went wrong.