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

Fixes RPC request for hd wallets #1072

Merged
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
29 changes: 28 additions & 1 deletion packages/common/src/hdwallets.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type Common from '@ethereumjs/common'
import type { BigNumber } from 'ethers'
import type { CustomNetwork } from './types'
import type { CustomNetwork, EIP1193Provider, RPCResponse } from './types'
import type { TransactionRequest } from '@ethersproject/providers'

/**
Expand Down Expand Up @@ -77,3 +77,30 @@ export const bigNumberFieldsToStrings = (
}),
transaction
) as StringifiedTransactionRequest

/**
* Helper method for hardware wallets to build an object
* with a request method used for making rpc requests.
* @param getRpcUrl - callback used to get the current chain's rpc url
* @returns An object with a request method
* to be called when making rpc requests
*/
export const getHardwareWalletProvider = (
getRpcUrl: () => string
): { request: EIP1193Provider['request'] } => ({
request: ({ method, params }) =>
fetch(getRpcUrl(), {
method: 'POST',
body: JSON.stringify({
id: '42',
method,
params
})
}).then(async res => {
const response = (await res.json()) as RPCResponse
if ('error' in response) {
throw response.error
}
return response.result
})
})
8 changes: 5 additions & 3 deletions packages/common/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ import type {
Chain,
TokenSymbol,
CustomNetwork,
TransactionObject
TransactionObject,
RPCResponse
} from './types'

export { ProviderRpcErrorCode } from './types'
Expand All @@ -62,7 +63,7 @@ export { createEIP1193Provider } from './eip-1193'
export { default as accountSelect } from './account-select'
export { entryModal } from './entry-modal'
export { SofiaProLight, SofiaProRegular, SofiaProSemiBold } from './fonts'
export { getCommon, bigNumberFieldsToStrings } from './hdwallets'
export { getCommon, bigNumberFieldsToStrings, getHardwareWalletProvider } from './hdwallets'

export type {
RequestPatch,
Expand Down Expand Up @@ -119,5 +120,6 @@ export type {
Chain,
TokenSymbol,
CustomNetwork,
TransactionObject
TransactionObject,
RPCResponse
}
7 changes: 7 additions & 0 deletions packages/common/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -466,3 +466,10 @@ export interface BootstrapNode {
location: string
comment: string
}

export interface RPCResponse {
id: number,
jsonrpc: string
error?: { code: number, message: string}
result?: any
}
32 changes: 31 additions & 1 deletion packages/demo/src/App.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -220,11 +220,27 @@

const signature = await signer.signTransaction({
to: '',
value: 1000000000000000
value: 100000000000000
})

console.log(signature)
}

let toAddress
const sendTransaction = async (provider) => {
const ethersProvider = new ethers.providers.Web3Provider(provider, 'any')

const signer = ethersProvider.getSigner()

const txn = await signer.sendTransaction({
to: toAddress,
value: 100000000000000
})

const receipt = await txn.wait()
console.log(receipt)
}

const signMessage = async (provider, address) => {
const ethersProvider = new ethers.providers.Web3Provider(provider, 'any')

Expand Down Expand Up @@ -441,6 +457,20 @@
</button>
</div>

<div>
<input
type="text"
class="text-input"
placeholder="0x..."
bind:value={toAddress}
/>
<button
on:click={sendTransaction(provider)}
>
Send Transaction
</button>
</div>

<div class="sign-transaction">
<textarea
bind:value={transactionObject}
Expand Down
30 changes: 6 additions & 24 deletions packages/keepkey/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import type {
Account,
Asset,
Chain,
WalletInit,
EIP1193Provider
WalletInit
} from '@web3-onboard/common'

import type { StaticJsonRpcProvider } from '@ethersproject/providers'
Expand Down Expand Up @@ -65,7 +64,8 @@ function keepkey(): WalletInit {
createEIP1193Provider,
ProviderRpcError,
entryModal,
bigNumberFieldsToStrings
bigNumberFieldsToStrings,
getHardwareWalletProvider
} = await import('@web3-onboard/common')

const { utils } = await import('ethers')
Expand Down Expand Up @@ -282,27 +282,9 @@ function keepkey(): WalletInit {
return signature
}

const request: EIP1193Provider['request'] = async ({
method,
params
}) => {
const response = await fetch(currentChain.rpcUrl, {
method: 'POST',
body: JSON.stringify({
id: '42',
method,
params
})
}).then(res => res.json())

if (response.result) {
return response.result
} else {
throw response.error
}
}

const keepKeyProvider = { request }
const keepKeyProvider = getHardwareWalletProvider(
() => currentChain.rpcUrl
)

const provider = createEIP1193Provider(keepKeyProvider, {
eth_requestAccounts: async () => {
Expand Down
30 changes: 5 additions & 25 deletions packages/keystone/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ function keystone({
ProviderRpcError,
ProviderRpcErrorCode,
getCommon,
bigNumberFieldsToStrings
bigNumberFieldsToStrings,
getHardwareWalletProvider
} = await import('@web3-onboard/common')

const keyring = AirGappedKeyring.getEmptyKeyring()
Expand Down Expand Up @@ -150,30 +151,9 @@ function keystone({
return keyring.signMessage(account.address, message)
}

const request = async ({
method,
params
}: {
method: string
params: any
}) => {
const response = await fetch(currentChain.rpcUrl, {
method: 'POST',
body: JSON.stringify({
id: '42',
method,
params
})
}).then(res => res.json())

if (response.result) {
return response.result
} else {
throw response.error
}
}

const keystoneProvider = { request }
const keystoneProvider = getHardwareWalletProvider(
() => currentChain.rpcUrl
)

const provider = createEIP1193Provider(keystoneProvider, {
eth_requestAccounts: async () => {
Expand Down
32 changes: 7 additions & 25 deletions packages/ledger/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import type {
Chain,
CustomNetwork,
WalletInit,
GetInterfaceHelpers,
EIP1193Provider
GetInterfaceHelpers
} from '@web3-onboard/common'

// these cannot be dynamically imported
Expand Down Expand Up @@ -130,7 +129,8 @@ function ledger({
createEIP1193Provider,
ProviderRpcError,
getCommon,
bigNumberFieldsToStrings
bigNumberFieldsToStrings,
getHardwareWalletProvider
} = await import('@web3-onboard/common')

const { TransactionFactory: Transaction, Capability } = await import(
Expand Down Expand Up @@ -222,28 +222,10 @@ function ledger({
return `0x${result['r']}${result['s']}${v}`
})
}

const request: EIP1193Provider['request'] = async ({
method,
params
}) => {
const response = await fetch(currentChain.rpcUrl, {
method: 'POST',
body: JSON.stringify({
id: '42',
method,
params
})
}).then(res => res.json())

if (response.result) {
return response.result
} else {
throw response.error
}
}

const ledgerProvider = { request }

const ledgerProvider = getHardwareWalletProvider(
() => currentChain?.rpcUrl
)

const provider = createEIP1193Provider(ledgerProvider, {
eth_requestAccounts: async () => {
Expand Down
28 changes: 5 additions & 23 deletions packages/trezor/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import type {
Asset,
Chain,
CustomNetwork,
EIP1193Provider,
ScanAccountsOptions,
TransactionObject,
WalletInit
Expand Down Expand Up @@ -123,7 +122,8 @@ function trezor(options: TrezorOptions): WalletInit {
bigNumberFieldsToStrings,
createEIP1193Provider,
ProviderRpcError,
getCommon
getCommon,
getHardwareWalletProvider
} = await import('@web3-onboard/common')
const ethUtil = await import('ethereumjs-util')
const { compress } = (await import('eth-crypto')).publicKey
Expand Down Expand Up @@ -443,27 +443,9 @@ function trezor(options: TrezorOptions): WalletInit {
})
}

const request: EIP1193Provider['request'] = async ({
method,
params
}) => {
const response = await fetch(currentChain.rpcUrl, {
method: 'POST',
body: JSON.stringify({
id: '42',
method,
params
})
}).then(res => res.json())

if (response.result) {
return response.result
} else {
throw response.error
}
}

const trezorProvider = { request }
const trezorProvider = getHardwareWalletProvider(
() => currentChain?.rpcUrl
)

const provider = createEIP1193Provider(trezorProvider, {
eth_requestAccounts: async () => {
Expand Down