File tree Expand file tree Collapse file tree 8 files changed +94
-102
lines changed Expand file tree Collapse file tree 8 files changed +94
-102
lines changed Original file line number Diff line number Diff line change 1
1
import type Common from '@ethereumjs/common'
2
2
import type { BigNumber } from 'ethers'
3
- import type { CustomNetwork } from './types'
3
+ import type { CustomNetwork , EIP1193Provider , RPCResponse } from './types'
4
4
import type { TransactionRequest } from '@ethersproject/providers'
5
5
6
6
/**
@@ -77,3 +77,30 @@ export const bigNumberFieldsToStrings = (
77
77
} ) ,
78
78
transaction
79
79
) as StringifiedTransactionRequest
80
+
81
+ /**
82
+ * Helper method for hardware wallets to build an object
83
+ * with a request method used for making rpc requests.
84
+ * @param getRpcUrl - callback used to get the current chain's rpc url
85
+ * @returns An object with a request method
86
+ * to be called when making rpc requests
87
+ */
88
+ export const getHardwareWalletProvider = (
89
+ getRpcUrl : ( ) => string
90
+ ) : { request : EIP1193Provider [ 'request' ] } => ( {
91
+ request : ( { method, params } ) =>
92
+ fetch ( getRpcUrl ( ) , {
93
+ method : 'POST' ,
94
+ body : JSON . stringify ( {
95
+ id : '42' ,
96
+ method,
97
+ params
98
+ } )
99
+ } ) . then ( async res => {
100
+ const response = ( await res . json ( ) ) as RPCResponse
101
+ if ( 'error' in response ) {
102
+ throw response . error
103
+ }
104
+ return response . result
105
+ } )
106
+ } )
Original file line number Diff line number Diff line change @@ -53,7 +53,8 @@ import type {
53
53
Chain ,
54
54
TokenSymbol ,
55
55
CustomNetwork ,
56
- TransactionObject
56
+ TransactionObject ,
57
+ RPCResponse
57
58
} from './types'
58
59
59
60
export { ProviderRpcErrorCode } from './types'
@@ -62,7 +63,7 @@ export { createEIP1193Provider } from './eip-1193'
62
63
export { default as accountSelect } from './account-select'
63
64
export { entryModal } from './entry-modal'
64
65
export { SofiaProLight , SofiaProRegular , SofiaProSemiBold } from './fonts'
65
- export { getCommon , bigNumberFieldsToStrings } from './hdwallets'
66
+ export { getCommon , bigNumberFieldsToStrings , getHardwareWalletProvider } from './hdwallets'
66
67
67
68
export type {
68
69
RequestPatch ,
@@ -119,5 +120,6 @@ export type {
119
120
Chain ,
120
121
TokenSymbol ,
121
122
CustomNetwork ,
122
- TransactionObject
123
+ TransactionObject ,
124
+ RPCResponse
123
125
}
Original file line number Diff line number Diff line change @@ -466,3 +466,10 @@ export interface BootstrapNode {
466
466
location : string
467
467
comment : string
468
468
}
469
+
470
+ export interface RPCResponse {
471
+ id : number ,
472
+ jsonrpc : string
473
+ error ?: { code : number , message : string }
474
+ result ?: any
475
+ }
Original file line number Diff line number Diff line change 220
220
221
221
const signature = await signer .signTransaction ({
222
222
to: ' ' ,
223
- value: 1000000000000000
223
+ value: 100000000000000
224
224
})
225
+
225
226
console .log (signature)
226
227
}
227
228
229
+ let toAddress
230
+ const sendTransaction = async (provider ) => {
231
+ const ethersProvider = new ethers.providers.Web3Provider (provider, ' any' )
232
+
233
+ const signer = ethersProvider .getSigner ()
234
+
235
+ const txn = await signer .sendTransaction ({
236
+ to: toAddress,
237
+ value: 100000000000000
238
+ })
239
+
240
+ const receipt = await txn .wait ()
241
+ console .log (receipt)
242
+ }
243
+
228
244
const signMessage = async (provider , address ) => {
229
245
const ethersProvider = new ethers.providers.Web3Provider (provider, ' any' )
230
246
441
457
< / button>
442
458
< / div>
443
459
460
+ < div>
461
+ < input
462
+ type= " text"
463
+ class = " text-input"
464
+ placeholder= " 0x..."
465
+ bind: value= {toAddress}
466
+ / >
467
+ < button
468
+ on: click= {sendTransaction (provider)}
469
+ >
470
+ Send Transaction
471
+ < / button>
472
+ < / div>
473
+
444
474
< div class = " sign-transaction" >
445
475
< textarea
446
476
bind: value= {transactionObject}
Original file line number Diff line number Diff line change @@ -3,8 +3,7 @@ import type {
3
3
Account ,
4
4
Asset ,
5
5
Chain ,
6
- WalletInit ,
7
- EIP1193Provider
6
+ WalletInit
8
7
} from '@web3-onboard/common'
9
8
10
9
import type { StaticJsonRpcProvider } from '@ethersproject/providers'
@@ -65,7 +64,8 @@ function keepkey(): WalletInit {
65
64
createEIP1193Provider,
66
65
ProviderRpcError,
67
66
entryModal,
68
- bigNumberFieldsToStrings
67
+ bigNumberFieldsToStrings,
68
+ getHardwareWalletProvider
69
69
} = await import ( '@web3-onboard/common' )
70
70
71
71
const { utils } = await import ( 'ethers' )
@@ -282,27 +282,9 @@ function keepkey(): WalletInit {
282
282
return signature
283
283
}
284
284
285
- const request : EIP1193Provider [ 'request' ] = async ( {
286
- method,
287
- params
288
- } ) => {
289
- const response = await fetch ( currentChain . rpcUrl , {
290
- method : 'POST' ,
291
- body : JSON . stringify ( {
292
- id : '42' ,
293
- method,
294
- params
295
- } )
296
- } ) . then ( res => res . json ( ) )
297
-
298
- if ( response . result ) {
299
- return response . result
300
- } else {
301
- throw response . error
302
- }
303
- }
304
-
305
- const keepKeyProvider = { request }
285
+ const keepKeyProvider = getHardwareWalletProvider (
286
+ ( ) => currentChain . rpcUrl
287
+ )
306
288
307
289
const provider = createEIP1193Provider ( keepKeyProvider , {
308
290
eth_requestAccounts : async ( ) => {
Original file line number Diff line number Diff line change @@ -101,7 +101,8 @@ function keystone({
101
101
ProviderRpcError,
102
102
ProviderRpcErrorCode,
103
103
getCommon,
104
- bigNumberFieldsToStrings
104
+ bigNumberFieldsToStrings,
105
+ getHardwareWalletProvider
105
106
} = await import ( '@web3-onboard/common' )
106
107
107
108
const keyring = AirGappedKeyring . getEmptyKeyring ( )
@@ -150,30 +151,9 @@ function keystone({
150
151
return keyring . signMessage ( account . address , message )
151
152
}
152
153
153
- const request = async ( {
154
- method,
155
- params
156
- } : {
157
- method : string
158
- params : any
159
- } ) => {
160
- const response = await fetch ( currentChain . rpcUrl , {
161
- method : 'POST' ,
162
- body : JSON . stringify ( {
163
- id : '42' ,
164
- method,
165
- params
166
- } )
167
- } ) . then ( res => res . json ( ) )
168
-
169
- if ( response . result ) {
170
- return response . result
171
- } else {
172
- throw response . error
173
- }
174
- }
175
-
176
- const keystoneProvider = { request }
154
+ const keystoneProvider = getHardwareWalletProvider (
155
+ ( ) => currentChain . rpcUrl
156
+ )
177
157
178
158
const provider = createEIP1193Provider ( keystoneProvider , {
179
159
eth_requestAccounts : async ( ) => {
Original file line number Diff line number Diff line change @@ -5,8 +5,7 @@ import type {
5
5
Chain ,
6
6
CustomNetwork ,
7
7
WalletInit ,
8
- GetInterfaceHelpers ,
9
- EIP1193Provider
8
+ GetInterfaceHelpers
10
9
} from '@web3-onboard/common'
11
10
12
11
// these cannot be dynamically imported
@@ -130,7 +129,8 @@ function ledger({
130
129
createEIP1193Provider,
131
130
ProviderRpcError,
132
131
getCommon,
133
- bigNumberFieldsToStrings
132
+ bigNumberFieldsToStrings,
133
+ getHardwareWalletProvider
134
134
} = await import ( '@web3-onboard/common' )
135
135
136
136
const { TransactionFactory : Transaction , Capability } = await import (
@@ -222,28 +222,10 @@ function ledger({
222
222
return `0x${ result [ 'r' ] } ${ result [ 's' ] } ${ v } `
223
223
} )
224
224
}
225
-
226
- const request : EIP1193Provider [ 'request' ] = async ( {
227
- method,
228
- params
229
- } ) => {
230
- const response = await fetch ( currentChain . rpcUrl , {
231
- method : 'POST' ,
232
- body : JSON . stringify ( {
233
- id : '42' ,
234
- method,
235
- params
236
- } )
237
- } ) . then ( res => res . json ( ) )
238
-
239
- if ( response . result ) {
240
- return response . result
241
- } else {
242
- throw response . error
243
- }
244
- }
245
-
246
- const ledgerProvider = { request }
225
+
226
+ const ledgerProvider = getHardwareWalletProvider (
227
+ ( ) => currentChain ?. rpcUrl
228
+ )
247
229
248
230
const provider = createEIP1193Provider ( ledgerProvider , {
249
231
eth_requestAccounts : async ( ) => {
Original file line number Diff line number Diff line change @@ -3,7 +3,6 @@ import type {
3
3
Asset ,
4
4
Chain ,
5
5
CustomNetwork ,
6
- EIP1193Provider ,
7
6
ScanAccountsOptions ,
8
7
TransactionObject ,
9
8
WalletInit
@@ -123,7 +122,8 @@ function trezor(options: TrezorOptions): WalletInit {
123
122
bigNumberFieldsToStrings,
124
123
createEIP1193Provider,
125
124
ProviderRpcError,
126
- getCommon
125
+ getCommon,
126
+ getHardwareWalletProvider
127
127
} = await import ( '@web3-onboard/common' )
128
128
const ethUtil = await import ( 'ethereumjs-util' )
129
129
const { compress } = ( await import ( 'eth-crypto' ) ) . publicKey
@@ -443,27 +443,9 @@ function trezor(options: TrezorOptions): WalletInit {
443
443
} )
444
444
}
445
445
446
- const request : EIP1193Provider [ 'request' ] = async ( {
447
- method,
448
- params
449
- } ) => {
450
- const response = await fetch ( currentChain . rpcUrl , {
451
- method : 'POST' ,
452
- body : JSON . stringify ( {
453
- id : '42' ,
454
- method,
455
- params
456
- } )
457
- } ) . then ( res => res . json ( ) )
458
-
459
- if ( response . result ) {
460
- return response . result
461
- } else {
462
- throw response . error
463
- }
464
- }
465
-
466
- const trezorProvider = { request }
446
+ const trezorProvider = getHardwareWalletProvider (
447
+ ( ) => currentChain ?. rpcUrl
448
+ )
467
449
468
450
const provider = createEIP1193Provider ( trezorProvider , {
469
451
eth_requestAccounts : async ( ) => {
You can’t perform that action at this time.
0 commit comments