This repository has been archived by the owner on Feb 21, 2024. It is now read-only.
forked from shapeshift/web
-
Notifications
You must be signed in to change notification settings - Fork 0
Xdefi Wallet Context Provider #1
Open
Da-Colon
wants to merge
1
commit into
develop
Choose a base branch
from
xdefi-wallet-provider
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { createContext } from 'react' | ||
|
||
import { ActionTypes } from './actions' | ||
import { InitialState } from './types' | ||
|
||
export interface IXDEFIProviderContext { | ||
state: InitialState | ||
dispatch: React.Dispatch<ActionTypes> | ||
} | ||
|
||
export const XDEFIProviderContext = createContext<IXDEFIProviderContext | null>(null) |
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,138 @@ | ||
/* eslint-disable no-console */ | ||
import React, { useCallback, useEffect, useMemo, useReducer } from 'react' | ||
|
||
import { useWallet } from '../../hooks/useWallet/useWallet' | ||
import { KeyManager } from '../WalletProvider/KeyManager' | ||
import { getLocalWalletDeviceId, getLocalWalletType } from '../WalletProvider/local-wallet' | ||
import { ActionTypes, XDEFIProviderActions } from './actions' | ||
import { InitialState, IXfiBitcoinProvider, IXfiLitecoinProvider } from './types' | ||
import { IXDEFIProviderContext, XDEFIProviderContext } from './XDEFIContext' | ||
|
||
const initialState: InitialState = { | ||
ethereumWallet: null, | ||
xfiBitcoinProvider: null, | ||
xfiLitecoinProvider: null, | ||
isWalletLoading: false, | ||
} | ||
|
||
export const reducer = (state: InitialState, action: ActionTypes) => { | ||
switch (action.type) { | ||
case XDEFIProviderActions.XDEFI_CONNECTED: { | ||
return { | ||
...state, | ||
...action.payload, | ||
isWalletLoading: false, | ||
} | ||
} | ||
case XDEFIProviderActions.XDEFI_NOT_CONNECTED: { | ||
return { | ||
...initialState, | ||
...action.payload, | ||
isWalletLoading: false, | ||
} | ||
} | ||
case XDEFIProviderActions.RESET_STATE: { | ||
return { | ||
...initialState, | ||
isWalletLoading: false, | ||
} | ||
} | ||
default: | ||
return state | ||
} | ||
} | ||
|
||
const getInitialState = () => { | ||
const localWalletType = getLocalWalletType() | ||
const localWalletDeviceId = getLocalWalletDeviceId() | ||
if (localWalletType && localWalletDeviceId) { | ||
return { | ||
...initialState, | ||
isWalletLoading: true, | ||
} | ||
} | ||
return initialState | ||
} | ||
|
||
const injectedAccount = async ( | ||
xfiProvider: IXfiBitcoinProvider | IXfiLitecoinProvider, | ||
): Promise<string[]> => { | ||
return new Promise(resolve => { | ||
xfiProvider.request( | ||
{ method: 'request_accounts', params: [] }, | ||
(error: any, accounts: string[]) => { | ||
if (!error && accounts.length) { | ||
resolve(accounts) | ||
} | ||
if (error) { | ||
resolve([]) | ||
} | ||
}, | ||
) | ||
}) | ||
} | ||
|
||
export const XDEFIWalletProvider = ({ children }: { children: React.ReactNode }): JSX.Element => { | ||
const [state, dispatch] = useReducer(reducer, getInitialState()) | ||
const { | ||
state: { wallet }, | ||
} = useWallet() | ||
const localWalletType = getLocalWalletType() | ||
const localWalletDeviceId = getLocalWalletDeviceId() | ||
|
||
const load = useCallback(() => { | ||
if (localWalletType && localWalletDeviceId) { | ||
switch (localWalletType) { | ||
case KeyManager.XDefi: { | ||
;(async () => { | ||
const xfi = (window as any).xfi | ||
const xfiBitcoinProvider: IXfiBitcoinProvider = xfi['bitcoin'] | ||
const xfiLitecoinProvider: IXfiLitecoinProvider = xfi['litecoin'] | ||
const xfiBitcoinAccounts = await injectedAccount(xfiBitcoinProvider) | ||
const xfiLItecoinAccounts = await injectedAccount(xfiLitecoinProvider) | ||
|
||
dispatch({ | ||
type: XDEFIProviderActions.XDEFI_CONNECTED, | ||
payload: { | ||
ethereumWallet: wallet!, | ||
xfiBitcoinProvider: { | ||
...xfiBitcoinProvider, | ||
accounts: xfiBitcoinAccounts, | ||
}, | ||
xfiLitecoinProvider: { | ||
...xfiLitecoinProvider, | ||
accounts: xfiLItecoinAccounts, | ||
}, | ||
}, | ||
}) | ||
})() | ||
break | ||
} | ||
default: { | ||
dispatch({ | ||
type: XDEFIProviderActions.XDEFI_NOT_CONNECTED, | ||
payload: { | ||
ethereumWallet: wallet!, | ||
}, | ||
}) | ||
} | ||
} | ||
} else { | ||
dispatch({ | ||
type: XDEFIProviderActions.RESET_STATE, | ||
}) | ||
} | ||
}, [localWalletType, localWalletDeviceId, wallet]) | ||
|
||
useEffect(() => load(), [load]) | ||
|
||
const value: IXDEFIProviderContext = useMemo( | ||
() => ({ | ||
state, | ||
dispatch, | ||
}), | ||
[state], | ||
) | ||
|
||
return <XDEFIProviderContext.Provider value={value}>{children}</XDEFIProviderContext.Provider> | ||
} |
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,28 @@ | ||
import { HDWallet } from '@shapeshiftoss/hdwallet-core' | ||
|
||
import { IXfiBitcoinProvider, IXfiLitecoinProvider } from './types' | ||
|
||
export enum XDEFIProviderActions { | ||
XDEFI_CONNECTED = 'XDEFI_CONNECTED', | ||
XDEFI_NOT_CONNECTED = 'XDEFI_NOT_CONNECTED', | ||
RESET_STATE = 'RESET_STATE', | ||
} | ||
|
||
export type ActionTypes = | ||
| { | ||
type: XDEFIProviderActions.XDEFI_CONNECTED | ||
payload: { | ||
ethereumWallet: HDWallet | ||
xfiBitcoinProvider: IXfiBitcoinProvider | ||
xfiLitecoinProvider: IXfiLitecoinProvider | ||
} | ||
} | ||
| { | ||
type: XDEFIProviderActions.XDEFI_NOT_CONNECTED | ||
payload: { | ||
ethereumWallet: HDWallet | ||
} | ||
} | ||
| { | ||
type: XDEFIProviderActions.RESET_STATE | ||
} |
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,6 @@ | ||
import { useContext } from 'react' | ||
|
||
import { IXDEFIProviderContext, XDEFIProviderContext } from '../XDEFIContext' | ||
|
||
export const useXDEFIProvider = (): IXDEFIProviderContext => | ||
useContext(XDEFIProviderContext as React.Context<IXDEFIProviderContext>) |
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,27 @@ | ||
import { HDWallet } from '@shapeshiftoss/hdwallet-core' | ||
|
||
import { AnyFunction } from '../../types/common' | ||
|
||
export interface InitialState { | ||
ethereumWallet: HDWallet | null | ||
xfiBitcoinProvider: IXfiBitcoinProvider | null | ||
xfiLitecoinProvider: IXfiLitecoinProvider | null | ||
isWalletLoading: boolean | ||
} | ||
|
||
export interface IXfiBitcoinProvider { | ||
accounts: string[] | ||
chainId: string | ||
network: string | ||
request: (e: { method: string; params: any[] }, cb: AnyFunction) => Promise<any> | ||
signTransaction: (e: any) => Promise<void> | ||
transfer: (e: any) => Promise<void> | ||
} | ||
|
||
export interface IXfiLitecoinProvider { | ||
accounts: string[] | ||
chainId: string | ||
network: string | ||
request: (e: { method: string; params: any[] }, cb: AnyFunction) => void | ||
transfer: (e: any) => void | ||
} | ||
Comment on lines
+12
to
+27
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is set up so that when a XDEFI wallet is connected then it will grab the accounts of the needed wallets, in this case For bitcoin it exposes the For litecoin it exposes the These will allow you to interact with these chains |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The state interface that is provided by the
useXDEFIProvider
hook.The etereumWallet is the Wallet created when connecting via Shapeshift's interface. this is for easy access to the other wallets when connected to any other ethereum wallet.