tomusdrw / rust-web3

Ethereum JSON-RPC multi-transport client. Rust implementation of web3 library. ENS address: rust-web3.eth
MIT License
1.45k stars 471 forks source link

EIP-1193 crash if no ethereum object #545

Closed SionoiS closed 3 years ago

SionoiS commented 3 years ago

Would be cool if it returned a nice error message when the user has no compatible wallet installed with their browser instead of crashing.

tomusdrw commented 3 years ago

Can you provide a code sample? Which line exactly crashes the app?

SionoiS commented 3 years ago

The ethereum object is undefined.

index-31f41f846045dd9b.js:664 Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'on')
    at imports.wbg.__wbg_on_0f85c0c3add89018 (index-31f41f846045dd9b.js:664)
    at web3::transports::eip_1193::Provider::on::haa90b8451281dff2 (index-31f41f846045dd9b_bg.wasm:0x82b319)
    at web3::transports::eip_1193::ProviderAndListeners::on::h67c1a9a03ee86ffa (index-31f41f846045dd9b_bg.wasm:0x69629c)
    at web3::transports::eip_1193::Eip1193::new::h1777e4e309433e74 (index-31f41f846045dd9b_bg.wasm:0x52cd75)
    at web_app::utils::web3::Web3Service::new::he41951d6e5f8c511 (index-31f41f846045dd9b_bg.wasm:0x4db8ad)
    at web_app::main::h44cf0902015ad00d (index-31f41f846045dd9b_bg.wasm:0x605eb5)
    at core::ops::function::FnOnce::call_once::h1a851f6194f89d40 (index-31f41f846045dd9b_bg.wasm:0xa198ef)
    at std::sys_common::backtrace::__rust_begin_short_backtrace::he2f30625ce35cc33 (index-31f41f846045dd9b_bg.wasm:0xa0908c)
    at std::rt::lang_start::{{closure}}::h300b60a332ccf4f7 (index-31f41f846045dd9b_bg.wasm:0x9cbbaf)
    at std::rt::lang_start_internal::h6c264208183e517c (index-31f41f846045dd9b_bg.wasm:0x73d00a)

It should return an error when the ethereum object is not present.

That's line https://github.com/tomusdrw/rust-web3/blob/2e2d1dfa906aa2dff6312b97d0d5ba804e9c8c6e/src/transports/eip_1193.rs#L225

tomusdrw commented 3 years ago

Sorry, but this is not specific enough for me. What do you mean by "it should return an error"? Which part of the code should error out?

The way I see it is we could change the bindgen to return Result<Option<Provider>, JsValue> here and then force the user to handle non-existence in case of using Provider::default().

Can you please share the exact code you are using and how you expect this to be handled?

SionoiS commented 3 years ago

Yes, returning an Option is fine, no need for an error.

 pub fn default() -> Result<Option<Self>, JsValue> { 

instead of https://github.com/tomusdrw/rust-web3/blob/2e2d1dfa906aa2dff6312b97d0d5ba804e9c8c6e/src/transports/eip_1193.rs#L233 and

fn get_provider_js() -> Result<Option<Provider>, JsValue>;

instead of https://github.com/tomusdrw/rust-web3/blob/2e2d1dfa906aa2dff6312b97d0d5ba804e9c8c6e/src/transports/eip_1193.rs#L281

I will send a PR.