diff --git a/packages/common/src/hdwallets.ts b/packages/common/src/hdwallets.ts index 7afd50b92..4f42024cb 100644 --- a/packages/common/src/hdwallets.ts +++ b/packages/common/src/hdwallets.ts @@ -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' /** @@ -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 + }) +}) diff --git a/packages/common/src/index.ts b/packages/common/src/index.ts index 693820822..2ee0924ad 100644 --- a/packages/common/src/index.ts +++ b/packages/common/src/index.ts @@ -53,7 +53,8 @@ import type { Chain, TokenSymbol, CustomNetwork, - TransactionObject + TransactionObject, + RPCResponse } from './types' export { ProviderRpcErrorCode } from './types' @@ -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, @@ -119,5 +120,6 @@ export type { Chain, TokenSymbol, CustomNetwork, - TransactionObject + TransactionObject, + RPCResponse } diff --git a/packages/common/src/types.ts b/packages/common/src/types.ts index ed14165f2..b6796de1b 100644 --- a/packages/common/src/types.ts +++ b/packages/common/src/types.ts @@ -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 +} \ No newline at end of file diff --git a/packages/demo/src/App.svelte b/packages/demo/src/App.svelte index 272f1e4f1..b540aad32 100644 --- a/packages/demo/src/App.svelte +++ b/packages/demo/src/App.svelte @@ -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') @@ -441,6 +457,20 @@ +