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

Connect wallet with ledger live app #36

Merged
merged 18 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from 10 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
1 change: 1 addition & 0 deletions dapp/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
VITE_USE_TESTNET=true
kkosiorowska marked this conversation as resolved.
Show resolved Hide resolved
19 changes: 15 additions & 4 deletions dapp/manifest-ledger-live-app.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,15 @@
"apiVersion": "2.0.0",
"manifestVersion": "1",
"branch": "stable",
"categories": ["buy"],
"currencies": ["bitcoin", "bitcoin_testnet"],
"categories": [
"buy"
],
"currencies": [
"bitcoin",
"bitcoin_testnet",
"ethereum",
"ethereum_goerli"
],
"content": {
"shortDescription": {
"en": "Bitcoin Liquid Staking"
Expand All @@ -18,6 +25,10 @@
"en": "Bitcoin Liquid Staking"
}
},
"permissions": [],
"domains": ["http://*"]
"permissions": [
"account.request"
],
"domains": [
"http://*"
]
}
26 changes: 19 additions & 7 deletions dapp/src/DApp.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,37 @@
import React from "react"
import { ChakraProvider, Button, Box } from "@chakra-ui/react"
import React, { useContext } from "react"
import { ChakraProvider, Box, Text } from "@chakra-ui/react"
import { useDetectThemeMode } from "./hooks"
import { LedgerWalletAPIProvider } from "./providers"
import theme from "./theme"
import {
LedgerLiveAppContext,
LedgerLiveAppProvider,
} from "./contexts/LedgerLiveAppContext"
import Navbar from "./components/Navbar"

function DApp() {
useDetectThemeMode()

const { btcAccount, ethAccount } = useContext(LedgerLiveAppContext)

r-czajkowski marked this conversation as resolved.
Show resolved Hide resolved
return (
<Box p={4}>
<Box>
<Navbar />
<h1>Ledger live - Acre dApp</h1>
<Button>Test button</Button>
{btcAccount && <Text>Account: {btcAccount.address}</Text>}
{ethAccount && <Text>Account: {ethAccount.address}</Text>}
</Box>
)
}

function DAppProviders() {
return (
<LedgerWalletAPIProvider>
<ChakraProvider theme={theme}>
<DApp />
</ChakraProvider>
<LedgerLiveAppProvider>
<ChakraProvider theme={theme}>
<DApp />
</ChakraProvider>
</LedgerLiveAppProvider>
</LedgerWalletAPIProvider>
)
}
Expand Down
16 changes: 16 additions & 0 deletions dapp/src/assets/bitcoin.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 16 additions & 0 deletions dapp/src/assets/ethereum.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions dapp/src/assets/info.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
77 changes: 77 additions & 0 deletions dapp/src/components/Navbar/ConnectWallet.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import React, { useContext } from "react"
import { Box, Button, Image, Text } from "@chakra-ui/react"
import { Account } from "@ledgerhq/wallet-api-client"
import BitcoinIcon from "../../assets/bitcoin.svg"
import EthereumIcon from "../../assets/ethereum.svg"
import InfoIcon from "../../assets/info.svg"
import { BITCOIN } from "../../constants"
import {
useRequestBitcoinAccount,
useRequestEthereumAccount,
} from "../../hooks"
import { LedgerLiveAppContext } from "../../contexts/LedgerLiveAppContext"
import { formatSatoshiAmount, truncateAddress } from "../../utils"

export type ConnectButtonsProps = {
leftIcon: string
rightIcon: string
account: Account | null
requestAccount: () => Promise<void>
}

function ConnectButton({
leftIcon,
rightIcon,
account,
requestAccount,
}: ConnectButtonsProps) {
const styles = !account ? { color: "error", borderColor: "error" } : undefined
return (
<Button
variant="outline"
sx={styles}
leftIcon={<Image src={leftIcon} />}
// TODO: Add a tooltip here
rightIcon={!account ? <Image src={rightIcon} /> : undefined}
onClick={requestAccount}
>
{account ? truncateAddress(account.address) : "Not connected"}
</Button>
)
}

export default function ConnectWallet() {
const requestBitcoinAccount = useRequestBitcoinAccount()
const requestEthereumAccount = useRequestEthereumAccount()
r-czajkowski marked this conversation as resolved.
Show resolved Hide resolved
const { btcAccount } = useContext(LedgerLiveAppContext)
r-czajkowski marked this conversation as resolved.
Show resolved Hide resolved

return (
<Box display="flex" alignItems="center" gap="4">
<Box display="flex" gap="2">
<Text>Balance</Text>
<Text as="b">
{!btcAccount || btcAccount?.balance.isZero()
? "0.00"
: formatSatoshiAmount(btcAccount.balance.toString())}
</Text>
<Text>{BITCOIN.token}</Text>
r-czajkowski marked this conversation as resolved.
Show resolved Hide resolved
</Box>
<ConnectButton
leftIcon={BitcoinIcon}
rightIcon={InfoIcon}
account={requestBitcoinAccount.account}
requestAccount={async () => {
await requestBitcoinAccount.requestAccount()
}}
/>
<ConnectButton
leftIcon={EthereumIcon}
rightIcon={InfoIcon}
account={requestEthereumAccount.account}
requestAccount={async () => {
await requestEthereumAccount.requestAccount()
}}
/>
</Box>
)
}
11 changes: 11 additions & 0 deletions dapp/src/components/Navbar/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import React from "react"
import { Box } from "@chakra-ui/react"
import ConnectWallet from "./ConnectWallet"

export default function Navbar() {
return (
<Box p={4} display="flex" justifyContent="end">
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just flagging, we can use Flex component from Chakra <Flex p={4} justify="end">.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the next PR I use here HStack. I'm not quite sure which is better. But let's move the discussion there.

https://github.com/thesis/acre/pull/39/files#diff-1be1f6c0a32d0f6e752f271df1c943de18e74dc12767dc65676d0a36b6c2233aR9-R20

<ConnectWallet />
</Box>
)
}
10 changes: 10 additions & 0 deletions dapp/src/constants/chains.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export const BITCOIN = {
name: "Bitcoin",
token: "BTC",
}
r-czajkowski marked this conversation as resolved.
Show resolved Hide resolved

export const CURRENCY_ID_BITCOIN =
import.meta.env.VITE_USE_TESTNET === "true" ? "bitcoin_testnet" : "bitcoin"

export const CURRENCY_ID_ETHEREUM =
import.meta.env.VITE_USE_TESTNET === "true" ? "ethereum_goerli" : "ethereum"
1 change: 1 addition & 0 deletions dapp/src/constants/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./chains"
42 changes: 42 additions & 0 deletions dapp/src/contexts/LedgerLiveAppContext.tsx
r-czajkowski marked this conversation as resolved.
Show resolved Hide resolved
r-czajkowski marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { Account } from "@ledgerhq/wallet-api-client"
import React, { createContext, useMemo, useState } from "react"

type LedgerLiveAppContextValue = {
btcAccount: Account | undefined
setBtcAccount: React.Dispatch<React.SetStateAction<Account | undefined>>
ethAccount: Account | undefined
setEthAccount: React.Dispatch<React.SetStateAction<Account | undefined>>
}

export const LedgerLiveAppContext = createContext<LedgerLiveAppContextValue>({
btcAccount: undefined,
setBtcAccount: () => {},
ethAccount: undefined,
setEthAccount: () => {},
})
r-czajkowski marked this conversation as resolved.
Show resolved Hide resolved

export function LedgerLiveAppProvider({
children,
}: {
children: React.ReactNode
}): React.ReactElement {
const [btcAccount, setBtcAccount] = useState<Account | undefined>(undefined)
const [ethAccount, setEthAccount] = useState<Account | undefined>(undefined)

const contextValue: LedgerLiveAppContextValue =
useMemo<LedgerLiveAppContextValue>(
() => ({
btcAccount,
setBtcAccount,
ethAccount,
setEthAccount,
}),
[btcAccount, setBtcAccount, ethAccount, setEthAccount],
)

return (
<LedgerLiveAppContext.Provider value={contextValue}>
{children}
</LedgerLiveAppContext.Provider>
)
}
2 changes: 2 additions & 0 deletions dapp/src/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export * from "./useDetectThemeMode"
export * from "./useRequestBitcoinAccount"
export * from "./useRequestEthereumAccount"
21 changes: 21 additions & 0 deletions dapp/src/hooks/useRequestBitcoinAccount.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { useRequestAccount } from "@ledgerhq/wallet-api-client-react"
import { useCallback, useContext, useEffect } from "react"
import { LedgerLiveAppContext } from "../contexts/LedgerLiveAppContext"
import { CURRENCY_ID_BITCOIN } from "../constants"
import { UseRequestAccountReturn } from "../types"

export function useRequestBitcoinAccount(): UseRequestAccountReturn {
const { setBtcAccount } = useContext(LedgerLiveAppContext)
const requestAccountResponse = useRequestAccount()
const { account, requestAccount } = requestAccountResponse

useEffect(() => {
setBtcAccount(account || undefined)
}, [account, setBtcAccount])

const requestBitcoinAccount = useCallback(async () => {
await requestAccount({ currencyIds: [CURRENCY_ID_BITCOIN] })
}, [requestAccount])

return { ...requestAccountResponse, requestAccount: requestBitcoinAccount }
}
21 changes: 21 additions & 0 deletions dapp/src/hooks/useRequestEthereumAccount.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { useRequestAccount } from "@ledgerhq/wallet-api-client-react"
import { useCallback, useContext, useEffect } from "react"
import { LedgerLiveAppContext } from "../contexts/LedgerLiveAppContext"
import { CURRENCY_ID_ETHEREUM } from "../constants"
import { UseRequestAccountReturn } from "../types"

export function useRequestEthereumAccount(): UseRequestAccountReturn {
const { setEthAccount } = useContext(LedgerLiveAppContext)
const requestAccountResponse = useRequestAccount()
const { account, requestAccount } = requestAccountResponse

useEffect(() => {
setEthAccount(account || undefined)
}, [account, setEthAccount])

const requestEthereumAccount = useCallback(async () => {
await requestAccount({ currencyIds: [CURRENCY_ID_ETHEREUM] })
}, [requestAccount])

return { ...requestAccountResponse, requestAccount: requestEthereumAccount }
}
7 changes: 7 additions & 0 deletions dapp/src/theme/Button.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,18 @@ import { mode } from "@chakra-ui/theme-tools"
import type { StyleFunctionProps } from "@chakra-ui/styled-system"

const Button = {
baseStyle: {
rounded: "none",
},
variants: {
solid: (props: StyleFunctionProps) => ({
backgroundColor: mode("black", "purple")(props),
color: "white",
}),
outline: (props: StyleFunctionProps) => ({
color: mode("black", "grey.80")(props),
borderColor: mode("black", "grey.50")(props),
}),
},
}

Expand Down
12 changes: 11 additions & 1 deletion dapp/src/theme/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
import { extendTheme } from "@chakra-ui/react"
import { StyleFunctionProps, extendTheme } from "@chakra-ui/react"
import { mode } from "@chakra-ui/theme-tools"
import Button from "./Button"
import { colors } from "./utils"

const defaultTheme = {
colors,
styles: {
global: (props: StyleFunctionProps) => ({
"html, body, #root, #root > div": {
backgroundColor: mode("lightGrey", "darkGrey")(props),
color: mode("black", "grey.80")(props),
minHeight: "100vh",
},
r-czajkowski marked this conversation as resolved.
Show resolved Hide resolved
}),
},
components: {
Button,
},
Expand Down
7 changes: 7 additions & 0 deletions dapp/src/theme/utils/colors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,11 @@ export const colors = {
white: "#FFF",
black: "#000",
purple: "#7D00FF",
error: "#F00",
grey: {
50: "rgba(255, 255, 255, 0.50)",
80: "rgba(255, 255, 255, 0.80)",
},
lightGrey: "#ECECEC",
darkGrey: "#1A1B1D",
r-czajkowski marked this conversation as resolved.
Show resolved Hide resolved
}
1 change: 1 addition & 0 deletions dapp/src/types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./ledger-live-app"
13 changes: 13 additions & 0 deletions dapp/src/types/ledger-live-app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Account, WalletAPIClient } from "@ledgerhq/wallet-api-client"

type UseRequestAccount = {
pending: boolean
account: Account | null
error: unknown
}

type RequestAccountParams = Parameters<WalletAPIClient["account"]["request"]>

export type UseRequestAccountReturn = {
requestAccount: (...params: RequestAccountParams) => Promise<void>
} & UseRequestAccount
3 changes: 3 additions & 0 deletions dapp/src/utils/address.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function truncateAddress(address: string): string {
return `${address.slice(0, 6)}…${address.slice(-5)}`
}
2 changes: 2 additions & 0 deletions dapp/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./numbers"
export * from "./address"
Loading
Loading