-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add coinbase wallet support * Add dapp name and icon to coinbase wallet sdk
- Loading branch information
Showing
15 changed files
with
802 additions
and
29 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
85 changes: 85 additions & 0 deletions
85
packages/react-celo/__tests__/connectors/coinbase-wallet.test.ts
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,85 @@ | ||
import { CeloContract } from '@celo/contractkit/lib/base'; | ||
import crypto from 'crypto'; | ||
import { generateTestingUtils } from 'eth-testing'; | ||
|
||
import { CoinbaseWalletConnector } from '../../src/connectors'; | ||
import { Alfajores, Baklava, localStorageKeys } from '../../src/constants'; | ||
import { getTypedStorageKey } from '../../src/utils/local-storage'; | ||
|
||
const ACCOUNT = '0xf61B443A155b07D2b2cAeA2d99715dC84E839EEf'; | ||
|
||
const testingUtils = generateTestingUtils({ | ||
providerType: 'default', | ||
verbose: false, | ||
}); | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-unsafe-return | ||
jest.mock('@coinbase/wallet-sdk', () => ({ | ||
...jest.requireActual('@coinbase/wallet-sdk'), | ||
CoinbaseWalletSDK: jest.fn(() => ({ | ||
makeWeb3Provider: () => testingUtils.getProvider(), | ||
})), | ||
})); | ||
|
||
describe('CoinbaseWalletConnector', () => { | ||
beforeAll(() => { | ||
// @ts-expect-error global override | ||
global.crypto = crypto; | ||
Object.defineProperty(global.self, 'crypto', { | ||
value: { | ||
getRandomValues: (arr: number[]) => crypto.randomBytes(arr.length), | ||
}, | ||
}); | ||
|
||
testingUtils.mockNotConnectedWallet(); | ||
testingUtils.mockAccounts([ACCOUNT]); | ||
testingUtils.mockRequestAccounts([ACCOUNT]); | ||
testingUtils.lowLevel.mockRequest('wallet_switchEthereumChain', {}); | ||
}); | ||
|
||
afterEach(() => { | ||
// Clear all mocks between tests | ||
testingUtils.clearAllMocks(); | ||
}); | ||
|
||
it('initialises', async () => { | ||
const connector = new CoinbaseWalletConnector( | ||
Alfajores, | ||
CeloContract.GoldToken | ||
); | ||
await connector.initialise(); | ||
expect(connector.account).toEqual(ACCOUNT); | ||
expect(connector.initialised).toBe(true); | ||
}); | ||
|
||
describe('when network change', () => { | ||
let connector: CoinbaseWalletConnector; | ||
beforeEach(() => { | ||
testingUtils.mockConnectedWallet([ACCOUNT]); | ||
connector = new CoinbaseWalletConnector( | ||
Alfajores, | ||
CeloContract.GoldToken | ||
); | ||
}); | ||
|
||
it('sets network in local storage and in connection', async () => { | ||
await connector.updateKitWithNetwork(Baklava); | ||
expect(getTypedStorageKey(localStorageKeys.lastUsedNetwork)).toEqual( | ||
Baklava.name | ||
); | ||
}); | ||
|
||
const callback = jest.fn(); | ||
|
||
it('reacts to network being changed from CoinbaseWallet side', async () => { | ||
connector.onNetworkChange(callback); | ||
|
||
// Seems to only work when init is called after the callback is set | ||
await connector.initialise(); | ||
|
||
testingUtils.mockChainChanged('0x1'); | ||
|
||
expect(callback).toHaveBeenLastCalledWith(1); | ||
}); | ||
}); | ||
}); |
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
22 changes: 22 additions & 0 deletions
22
packages/react-celo/src/components/icons/coinbase-wallet.tsx
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,22 @@ | ||
import React from 'react'; | ||
|
||
const COINBASE_WALLET: React.FC<React.SVGProps<SVGSVGElement>> = (props) => ( | ||
<svg | ||
xmlns="http://www.w3.org/2000/svg" | ||
width="1024" | ||
height="1024" | ||
viewBox="0 0 1024 1024" | ||
fill="none" | ||
{...props} | ||
> | ||
<rect width="1024" height="1024" fill="#0052FF" /> | ||
<path | ||
fillRule="evenodd" | ||
clipRule="evenodd" | ||
d="M152 512C152 710.823 313.177 872 512 872C710.823 872 872 710.823 872 512C872 313.177 710.823 152 512 152C313.177 152 152 313.177 152 512ZM420 396C406.745 396 396 406.745 396 420V604C396 617.255 406.745 628 420 628H604C617.255 628 628 617.255 628 604V420C628 406.745 617.255 396 604 396H420Z" | ||
fill="white" | ||
/> | ||
</svg> | ||
); | ||
|
||
export default COINBASE_WALLET; |
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,130 @@ | ||
import { CeloTokenContract } from '@celo/contractkit/lib/base'; | ||
import { | ||
MiniContractKit, | ||
newKit, | ||
newKitFromWeb3, | ||
} from '@celo/contractkit/lib/mini-kit'; | ||
import { | ||
CoinbaseWalletProvider, | ||
CoinbaseWalletSDK, | ||
} from '@coinbase/wallet-sdk'; | ||
|
||
import { localStorageKeys, WalletTypes } from '../constants'; | ||
import { Ethereum } from '../global'; | ||
import { Connector, Dapp, Maybe, Network } from '../types'; | ||
import { | ||
clearPreviousConfig, | ||
setTypedStorageKey, | ||
} from '../utils/local-storage'; | ||
import { switchToCeloNetwork } from '../utils/metamask'; | ||
import { persist, Web3Type } from './common'; | ||
|
||
export default class CoinbaseWalletConnector implements Connector { | ||
public initialised = false; | ||
public type = WalletTypes.CoinbaseWallet; | ||
public kit: MiniContractKit; | ||
public account: Maybe<string> = null; | ||
private onNetworkChangeCallback?: (chainId: number) => void; | ||
private onAddressChangeCallback?: (address: Maybe<string>) => void; | ||
private network: Network; | ||
|
||
private provider: CoinbaseWalletProvider | null = null; | ||
|
||
constructor( | ||
network: Network, | ||
public feeCurrency: CeloTokenContract, | ||
dapp?: Dapp | ||
) { | ||
this.kit = newKit(network.rpcUrl); | ||
this.network = network; | ||
|
||
const sdk = new CoinbaseWalletSDK({ | ||
appName: dapp?.name ?? '', | ||
appLogoUrl: dapp?.icon ?? '', | ||
}); | ||
this.provider = sdk.makeWeb3Provider(network.rpcUrl, network.chainId); | ||
} | ||
|
||
persist() { | ||
persist({ | ||
walletType: this.type, | ||
network: this.network, | ||
}); | ||
} | ||
|
||
async initialise(): Promise<this> { | ||
if (!this.provider) { | ||
throw new Error('Coinbase wallet provider not instantiated'); | ||
} | ||
const { default: Web3 } = await import('web3'); | ||
const web3 = new Web3(this.provider); | ||
const [defaultAccount]: string[] = await this.provider.request({ | ||
method: 'eth_requestAccounts', | ||
}); | ||
|
||
this.provider.removeListener('chainChanged', this.onChainChanged); | ||
this.provider.removeListener('accountsChanged', this.onAccountsChanged); | ||
|
||
await switchToCeloNetwork( | ||
this.kit, | ||
this.network, | ||
this.provider as unknown as Ethereum | ||
); | ||
|
||
this.provider.on('chainChanged', this.onChainChanged); | ||
this.provider.on('accountsChanged', this.onAccountsChanged); | ||
|
||
this.kit = newKitFromWeb3(web3 as unknown as Web3Type); | ||
|
||
this.kit.connection.defaultAccount = defaultAccount; | ||
this.account = defaultAccount ?? null; | ||
this.initialised = true; | ||
|
||
this.persist(); | ||
|
||
return this; | ||
} | ||
|
||
private onChainChanged = (chainIdHex: string) => { | ||
const chainId = parseInt(chainIdHex, 16); | ||
if (this.onNetworkChangeCallback && this.network.chainId !== chainId) { | ||
this.onNetworkChangeCallback(chainId); | ||
} | ||
}; | ||
|
||
private onAccountsChanged = (accounts: string[]) => { | ||
if (this.onAddressChangeCallback) { | ||
this.kit.connection.defaultAccount = accounts[0]; | ||
this.onAddressChangeCallback(accounts[0] ?? null); | ||
} | ||
}; | ||
|
||
supportsFeeCurrency() { | ||
return false; | ||
} | ||
|
||
async updateKitWithNetwork(network: Network): Promise<void> { | ||
setTypedStorageKey(localStorageKeys.lastUsedNetwork, network.name); | ||
this.network = network; | ||
await this.initialise(); | ||
} | ||
|
||
onNetworkChange(callback: (chainId: number) => void): void { | ||
this.onNetworkChangeCallback = callback; | ||
} | ||
|
||
onAddressChange(callback: (address: Maybe<string>) => void): void { | ||
this.onAddressChangeCallback = callback; | ||
} | ||
|
||
close(): void { | ||
clearPreviousConfig(); | ||
if (this.provider) { | ||
this.provider.removeListener('chainChanged', this.onChainChanged); | ||
this.provider.removeListener('accountsChanged', this.onAccountsChanged); | ||
} | ||
this.onNetworkChangeCallback = undefined; | ||
this.onAddressChangeCallback = undefined; | ||
return; | ||
} | ||
} |
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
44 changes: 44 additions & 0 deletions
44
packages/react-celo/src/hooks/use-coinbase-wallet-connector.ts
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,44 @@ | ||
import { useEffect } from 'react'; | ||
|
||
import { CoinbaseWalletConnector } from '../connectors'; | ||
import { Connector, Dapp, Maybe, Network } from '../types'; | ||
import { useCeloInternal } from '../use-celo'; | ||
|
||
export function useCoinbaseWalletConnector( | ||
onSubmit: (connector: Connector) => void | ||
): UseCoinbaseWalletConnector { | ||
const { | ||
network, | ||
feeCurrency, | ||
initConnector, | ||
initError: error, | ||
dapp, | ||
} = useCeloInternal(); | ||
|
||
useEffect(() => { | ||
let stale; | ||
void (async () => { | ||
const connector = new CoinbaseWalletConnector(network, feeCurrency, dapp); | ||
try { | ||
await initConnector(connector); | ||
if (!stale) { | ||
onSubmit(connector); | ||
} | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
})(); | ||
|
||
return () => { | ||
stale = true; | ||
}; | ||
}, [initConnector, network, dapp, onSubmit, feeCurrency]); | ||
|
||
return { error, dapp, network }; | ||
} | ||
|
||
export interface UseCoinbaseWalletConnector { | ||
error: Maybe<Error>; | ||
network: Network; | ||
dapp: Dapp; | ||
} |
Oops, something went wrong.