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

Enable login with ledger #306

Merged
merged 1 commit into from
Apr 10, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
30 changes: 27 additions & 3 deletions apps/web/src/composables/ledger.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { BitcoinLedgerSigner, EthersLedgerSigner } from '@casimir/wallets'
import { ethers } from 'ethers'
import { MessageInit, TransactionInit } from '@/interfaces/index'
import { Currency, ProviderString } from '@casimir/types'
import useEnvironment from '@/composables/environment'
import useEthers from '@/composables/ethers'
import useAuth from '@/composables/auth'

const { getMessage, login } = useAuth()

export default function useLedger() {
const { ethereumURL, ledgerType, speculosURL } = useEnvironment()
Expand Down Expand Up @@ -35,6 +39,25 @@ export default function useLedger() {
return await signer.getAddress()
}

async function loginWithLedger(provider: ProviderString, address: string, currency: Currency) {
try {
const { message } = await (await getMessage(provider, address)).json()
const signer = getEthersLedgerSigner()
const signature = await signer.signMessage(message)
const loginResponse = await login({
provider,
address,
message: message.toString(),
signedMessage: signature,
currency
})
return await loginResponse.json()
} catch (err) {
console.log('Error logging in: ', err)
return err
}
}

async function sendLedgerTransaction({ from, to, value, currency }: TransactionInit) {
if (currency === 'ETH') {
const signer = getEthersLedgerSigner()
Expand Down Expand Up @@ -73,11 +96,12 @@ export default function useLedger() {
}

return {
signLedgerMessage,
sendLedgerTransaction,
getBitcoinLedgerAddress,
getEthersLedgerAddress,
getBitcoinLedgerSigner,
getEthersLedgerAddress,
getEthersLedgerSigner,
loginWithLedger,
signLedgerMessage,
sendLedgerTransaction,
}
}
6 changes: 4 additions & 2 deletions apps/web/src/composables/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export default function useWallet() {
const { ethereumURL } = useEnvironment()
const { ethersProviderList, getEthersAddress, getEthersBalance, sendEthersTransaction, signEthersMessage, loginWithEthers, getEthersBrowserProviderSelectedCurrency, switchEthersNetwork } = useEthers()
const { solanaProviderList, getSolanaAddress, sendSolanaTransaction, signSolanaMessage } = useSolana()
const { getBitcoinLedgerAddress, getEthersLedgerAddress, sendLedgerTransaction, signLedgerMessage } = useLedger()
const { getBitcoinLedgerAddress, getEthersLedgerAddress, loginWithLedger, sendLedgerTransaction, signLedgerMessage } = useLedger()
const { getTrezorAddress, sendTrezorTransaction, signTrezorMessage } = useTrezor()
const { getWalletConnectAddress, sendWalletConnectTransaction, signWalletConnectMessage } = useWalletConnect()
const { user, getUser, setUser, addAccount, removeAccount, updatePrimaryAddress } = useUsers()
Expand Down Expand Up @@ -118,7 +118,7 @@ export default function useWallet() {
if (!loggedIn.value) {
loadingUserWallets.value = true
const connectedAddress = await getConnectedAddressFromProvider(provider, currency)
const connectedCurrency = await detectCurrencyInProvider(provider) as Currency
const connectedCurrency = currency ? currency : await detectCurrencyInProvider(provider) as Currency
const loginResponse = await loginWithWallet(provider, connectedAddress, connectedCurrency)
if (!loginResponse?.error) {
// STEP 2
Expand Down Expand Up @@ -210,6 +210,8 @@ export default function useWallet() {
async function loginWithWallet(provider: ProviderString, address: string, currency: Currency) {
if (ethersProviderList.includes(provider)) {
return await loginWithEthers(provider, address, currency)
} else if (provider === 'Ledger') {
return await loginWithLedger(provider, address, currency)
} else {
// TODO: Implement this for other providers
console.log('Sign up not yet supported for this wallet provider')
Expand Down