-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
328 additions
and
30 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { | ||
createExternalWallet, | ||
IBBNProvider, | ||
useChainConnector, | ||
useWidgetState, | ||
} from "@babylonlabs-io/bbn-wallet-connect"; | ||
import { | ||
CosmosProvider, | ||
useTomoProviders, | ||
useTomoWalletConnect, | ||
useTomoWalletState, | ||
useWalletList, | ||
} from "@tomo-inc/wallet-connect-sdk"; | ||
import { memo, useCallback, useEffect, useMemo } from "react"; | ||
|
||
const createProvider = (provider: CosmosProvider): IBBNProvider => { | ||
return { | ||
connectWallet: async () => void provider.connectWallet(), | ||
getAddress: () => provider.getAddress(), | ||
getPublicKeyHex: async () => "", | ||
getSigningStargateClient: () => provider.getSigningStargateClient(), | ||
getBalance: (searchDenom) => provider.getBalance(searchDenom), | ||
}; | ||
}; | ||
|
||
export const TomoBBNConnector = memo(() => { | ||
const tomoWalletState = useTomoWalletState(); | ||
const walletList = useWalletList(); | ||
const { cosmosProvider: connectedProvider } = useTomoProviders(); | ||
const tomoWalletConnect = useTomoWalletConnect(); | ||
|
||
const { visible } = useWidgetState(); | ||
const connector = useChainConnector("BBN"); | ||
|
||
const connectedWallet = useMemo(() => { | ||
const { connected, walletId } = tomoWalletState.cosmos ?? {}; | ||
|
||
return connected && walletId | ||
? (walletList.find((wallet: any) => wallet.id === walletId) ?? null) | ||
: null; | ||
}, [tomoWalletState.cosmos, walletList]); | ||
|
||
const connect = useCallback( | ||
async (btcWallet: any, btcProvider: CosmosProvider) => { | ||
if (!connector) return; | ||
|
||
const wallet = createExternalWallet({ | ||
id: "tomo-bbn-connector", | ||
name: btcWallet.name, | ||
icon: btcWallet.img, | ||
provider: createProvider(btcProvider), | ||
}); | ||
|
||
await connector.connect(wallet); | ||
}, | ||
[connector], | ||
); | ||
|
||
useEffect(() => { | ||
if (visible && connectedWallet && connectedProvider) { | ||
connect(connectedWallet, connectedProvider); | ||
} | ||
}, [visible, connectedWallet, connectedProvider, connect]); | ||
|
||
useEffect(() => { | ||
if (!connector) return; | ||
|
||
const unsubscribe = connector.on("disconnect", (wallet) => { | ||
if (wallet.id === "tomo-bbn-connector") { | ||
tomoWalletConnect.disconnect(); | ||
} | ||
}); | ||
|
||
return unsubscribe; | ||
}, [connector, tomoWalletConnect]); | ||
|
||
return null; | ||
}); | ||
|
||
TomoBBNConnector.displayName = "TomoBBNConnector"; |
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,104 @@ | ||
import { | ||
createExternalWallet, | ||
IBTCProvider, | ||
useChainConnector, | ||
useWidgetState, | ||
} from "@babylonlabs-io/bbn-wallet-connect"; | ||
import { | ||
BTCProvider, | ||
useTomoProviders, | ||
useTomoWalletConnect, | ||
useTomoWalletState, | ||
useWalletList, | ||
} from "@tomo-inc/wallet-connect-sdk"; | ||
import { memo, useCallback, useEffect, useMemo } from "react"; | ||
|
||
const createProvider = (provider: BTCProvider): IBTCProvider => { | ||
return { | ||
connectWallet: async () => void provider.connectWallet(), | ||
getAddress: () => provider.getAddress(), | ||
getPublicKeyHex: () => provider.getPublicKeyHex(), | ||
signPsbt: (psbtHex: string) => provider.signPsbt(psbtHex), | ||
signPsbts: (psbtsHexes: string[]) => provider.signPsbts(psbtsHexes), | ||
getNetwork: () => provider.getNetwork(), | ||
signMessageBIP322: (message: string) => provider.signMessageBIP322(message), | ||
signMessage: (message: string, type: "ecdsa" | "bip322-simple") => | ||
provider.signMessage(message, type), | ||
on: (eventName: string, callBack: () => void) => | ||
provider.on(eventName, callBack), | ||
off: (eventName: string, callBack: () => void) => | ||
provider.off(eventName, callBack), | ||
getBalance: () => provider.getBalance(), | ||
getNetworkFees: () => provider.getNetworkFees(), | ||
pushTx: (txHex: string) => provider.pushTx(txHex), | ||
getUtxos: (address: string, amount?: number) => | ||
provider.getUtxos(address, amount), | ||
getBTCTipHeight: () => provider.getBTCTipHeight(), | ||
getInscriptions: () => | ||
provider | ||
.getInscriptions() | ||
.then((result) => | ||
result.list.map((ordinal) => ({ | ||
txid: ordinal.inscriptionId, | ||
vout: ordinal.outputValue, | ||
})), | ||
) | ||
.catch((e) => []), | ||
}; | ||
}; | ||
|
||
export const TomoBTCConnector = memo(() => { | ||
const tomoWalletState = useTomoWalletState(); | ||
const walletList = useWalletList(); | ||
const { bitcoinProvider: connectedProvider } = useTomoProviders(); | ||
const tomoWalletConnect = useTomoWalletConnect(); | ||
|
||
const { visible } = useWidgetState(); | ||
const connector = useChainConnector("BTC"); | ||
|
||
const connectedWallet = useMemo(() => { | ||
const { connected, walletId } = tomoWalletState.bitcoin ?? {}; | ||
|
||
return connected && walletId | ||
? (walletList.find((wallet: any) => wallet.id === walletId) ?? null) | ||
: null; | ||
}, [tomoWalletState.bitcoin, walletList]); | ||
|
||
const connect = useCallback( | ||
async (btcWallet: any, btcProvider: BTCProvider) => { | ||
if (!connector) return; | ||
|
||
const wallet = createExternalWallet({ | ||
id: "tomo-btc-connector", | ||
name: btcWallet.name, | ||
icon: btcWallet.img, | ||
provider: createProvider(btcProvider), | ||
}); | ||
|
||
await connector.connect(wallet); | ||
}, | ||
[connector], | ||
); | ||
|
||
useEffect(() => { | ||
if (visible && connectedWallet && connectedProvider) { | ||
connect(connectedWallet, connectedProvider); | ||
} | ||
}, [visible, connectedWallet, connectedProvider, connect]); | ||
|
||
useEffect(() => { | ||
if (!connector) return; | ||
|
||
const unsubscribe = connector.on("disconnect", (wallet) => { | ||
if (wallet.id === "tomo-btc-connector") { | ||
tomoWalletConnect.disconnect(); | ||
} | ||
}); | ||
|
||
return unsubscribe; | ||
}, [connector, tomoWalletConnect]); | ||
|
||
return null; | ||
}); | ||
|
||
TomoBTCConnector.displayName = "TomoBTCConnector"; |
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,38 @@ | ||
import { Text } from "@babylonlabs-io/bbn-core-ui"; | ||
import { | ||
useWidgetState, | ||
WalletButton, | ||
} from "@babylonlabs-io/bbn-wallet-connect"; | ||
import { useTomoModalControl } from "@tomo-inc/wallet-connect-sdk"; | ||
import { useCallback } from "react"; | ||
|
||
import logo from "./tomo.png"; | ||
|
||
const CHAINS = { | ||
bitcoin: "BTC", | ||
cosmos: "BBN", | ||
}; | ||
|
||
export const ConnectButton = ({ | ||
chainName, | ||
}: { | ||
chainName: "bitcoin" | "cosmos"; | ||
}) => { | ||
const tomoModal = useTomoModalControl(); | ||
const { displayWallets } = useWidgetState(); | ||
|
||
const open = useCallback(async () => { | ||
const result = await tomoModal.open(chainName); | ||
|
||
if (!result) { | ||
displayWallets?.(CHAINS[chainName]); | ||
} | ||
}, [tomoModal, chainName, displayWallets]); | ||
|
||
return ( | ||
<div className="sticky inset-x-0 bottom-0 bg-[#ffffff] pt-10"> | ||
<Text className="mb-4">More wallets with Tomo Connect</Text> | ||
<WalletButton logo={logo.src} name="Tomo Connect" onClick={open} /> | ||
</div> | ||
); | ||
}; |
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,49 @@ | ||
import { TomoContextProvider } from "@tomo-inc/wallet-connect-sdk"; | ||
import "@tomo-inc/wallet-connect-sdk/style.css"; | ||
import { useTheme } from "next-themes"; | ||
import { type PropsWithChildren } from "react"; | ||
|
||
import { getNetworkConfig } from "@/config/network.config"; | ||
import { bbnDevnet } from "@/config/wallet/babylon"; | ||
|
||
export const TomoConnectionProvider = ({ children }: PropsWithChildren) => { | ||
const { resolvedTheme } = useTheme(); | ||
|
||
const { mempoolApiUrl, network, networkName } = getNetworkConfig(); | ||
const bitcoinChain = { | ||
id: 1, | ||
name: networkName, | ||
type: "bitcoin" as any, | ||
network: network, | ||
backendUrls: { | ||
mempoolUrl: mempoolApiUrl + "/api/", | ||
}, | ||
}; | ||
|
||
return ( | ||
<TomoContextProvider | ||
bitcoinChains={[bitcoinChain]} | ||
chainTypes={["bitcoin", "cosmos"]} | ||
cosmosChains={[ | ||
{ | ||
id: 2, | ||
name: "Babylon Devnet 7", | ||
type: "cosmos", | ||
network: "devnet-7", | ||
modularData: bbnDevnet, | ||
backendUrls: { | ||
rpcRrl: "https://rpc.devnet.babylonlabs.io", | ||
}, | ||
logo: "https://raw.githubusercontent.com/chainapsis/keplr-chain-registry/main/images/bbn-dev/chain.png", | ||
}, | ||
]} | ||
style={{ | ||
rounded: "medium", | ||
theme: resolvedTheme as "dark" | "light", | ||
primaryColor: "#FF7C2A", | ||
}} | ||
> | ||
{children} | ||
</TomoContextProvider> | ||
); | ||
}; |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.