Skip to content

Commit

Permalink
Created a new transport
Browse files Browse the repository at this point in the history
Removed WindowProvider
  • Loading branch information
nigeon committed Nov 20, 2023
1 parent da84921 commit 890e93d
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 16 deletions.
4 changes: 2 additions & 2 deletions packages/rainbowkit-wallets/src/lib/inputsWallet.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { createRoot } from 'react-dom/client'
import { WalletClient } from 'viem'
import { WindowProvider } from 'wagmi'
import { CreateWalletModal } from '../components/CreateWalletModal'
import localStorageWallet from './localStorageWallet'
import { PublicClient } from 'wagmi'

/**
* This class is used to create a wallet from a form
Expand All @@ -11,7 +11,7 @@ export class inputsWallet extends localStorageWallet {
private data: any
private cancel: boolean = false

async create(provider: WindowProvider): Promise<WalletClient> {
async create(provider: PublicClient): Promise<WalletClient> {
// Create a div to render the modal
const myDiv = document.createElement('div')
myDiv.setAttribute('id', 'myDiv' + Math.random())
Expand Down
37 changes: 32 additions & 5 deletions packages/rainbowkit-wallets/src/lib/localStorageWallet.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { WindowProvider } from '@wagmi/connectors'
import { Buffer } from 'buffer'
import { createWalletClient, custom, keccak256, WalletClient, publicActions } from 'viem'
import { privateKeyToAccount } from 'viem/accounts'
import { mainnet } from 'wagmi'
import { PublicClient, mainnet } from 'wagmi'

export default class localStorageWallet {
static storageItemName = 'localstorage-wallet-seed'

public static async getWallet(provider: WindowProvider): Promise<WalletClient | undefined> {
public static async getWallet(provider: PublicClient): Promise<WalletClient | undefined> {
try {
const value: string = localStorage.getItem(this.storageItemName) as string
if (!value) return undefined
Expand All @@ -20,7 +19,7 @@ export default class localStorageWallet {
return undefined
}

public static async createWallet(data: string | string[], provider: WindowProvider): Promise<WalletClient> {
public static async createWallet(data: string | string[], provider: PublicClient): Promise<WalletClient> {
const inputs = Array.isArray(data) ? data : [data]
const hash = inputs.reduce((acc, curr) => acc + curr, '')

Expand All @@ -30,7 +29,35 @@ export default class localStorageWallet {
const client = createWalletClient({
account,
chain: mainnet,
transport: custom(provider),
transport: custom({
async request({ method, params }) {
switch (method) {
case 'eth_accounts':
return [account.address]
case 'net_version':
case 'eth_chainId':
return `0x${mainnet.id}`
case 'personal_sign': {
const [message, address] = params
return (await account.signMessage({ message })) || '0x'
}
case 'eth_sign': {
const [address, messageHash] = params
return (await account.signMessage({ message: messageHash })) || '0x'
}
case 'eth_signTypedData':
case 'eth_signTypedData_v4': {
const [address, typedData] = params
const parsedTypedData = typeof typedData === 'string' ? JSON.parse(typedData) : typedData

const signature = await account.signTypedData(parsedTypedData)
return signature || '0x'
}
default:
provider.request({ method, params })
}
},
}),
}).extend(publicActions)

return client
Expand Down
4 changes: 2 additions & 2 deletions packages/rainbowkit-wallets/src/lib/oAuthWallet.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { WindowProvider } from '@wagmi/connectors'
import { WalletClient } from 'viem'
import localStorageWallet from './localStorageWallet'
import { PublicClient } from 'wagmi'

/**
* This class is used to create a wallet from an external service (OAuth)
Expand All @@ -18,7 +18,7 @@ export class oAuthWallet extends localStorageWallet {
this.oAuthServiceProvider = oAuthServiceProvider
}

async create(provider: WindowProvider): Promise<WalletClient> {
async create(provider: PublicClient): Promise<WalletClient> {
// Open the login popup
const url = this.oAuthServiceUrl + (this.oAuthServiceProvider ? `?provider=${this.oAuthServiceProvider}` : '')
this.openLoginPopup(url)
Expand Down
4 changes: 2 additions & 2 deletions packages/rainbowkit-wallets/src/wagmi/inputsConnector.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { WindowProvider } from '@wagmi/connectors'
import { inputsWallet } from '../lib/inputsWallet'
import { localStorageConnector } from './localStorageConnector'
import { PublicClient } from 'wagmi'

const IS_SERVER = typeof window === 'undefined'

Expand All @@ -10,7 +10,7 @@ export class inputsConnector extends localStorageConnector {
readonly name = 'Inputs'

protected async createWallet() {
const provider = (await this.getProvider()) as WindowProvider
const provider = (await this.getProvider()) as PublicClient
let wallet = await inputsWallet.getWallet(provider)
if (!wallet) {
const w = new inputsWallet()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
WalletClient,
} from 'viem'
import { mainnet } from 'viem/chains'
import { ConnectorData, ConnectorNotFoundError, WindowProvider } from 'wagmi'
import { ConnectorData, ConnectorNotFoundError, PublicClient } from 'wagmi'
import { Connector, normalizeChainId } from '@wagmi/connectors'
import localStorageWallet from '../lib/localStorageWallet'

Expand All @@ -24,7 +24,7 @@ export class localStorageConnector extends Connector {
readonly name: string = 'localStorage'

protected chainId: number | undefined
protected provider: WindowProvider | undefined
protected provider: PublicClient | undefined
protected wallet: WalletClient | undefined

constructor(config: { chains: Chain[]; options: any }) {
Expand Down
5 changes: 2 additions & 3 deletions packages/rainbowkit-wallets/src/wagmi/oAuthConnector.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { WindowProvider } from '@wagmi/connectors'
import { Chain } from 'wagmi'
import { Chain, PublicClient } from 'wagmi'
import { oAuthWallet } from '../lib/oAuthWallet'
import { localStorageConnector } from './localStorageConnector'

Expand Down Expand Up @@ -28,7 +27,7 @@ export class oAuthConnector extends localStorageConnector {
}

protected async createWallet() {
const provider = (await this.getProvider()) as WindowProvider
const provider = (await this.getProvider()) as PublicClient
let wallet = await oAuthWallet.getWallet(provider)

if (!wallet) {
Expand Down

0 comments on commit 890e93d

Please sign in to comment.