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

Getting URL error, when instantiating with Wallet::with_http_client() #33

Closed
sammyshakes opened this issue May 11, 2023 · 3 comments
Closed

Comments

@sammyshakes
Copy link

sammyshakes commented May 11, 2023

Info

Currently when attempting to instantiate a wallet with the with_http_client() method, it is returning an error if a port is not included in the rpc url.

example: when url is without a port number, such as: https://testnet.era.zksync.dev, an error is returned,
but when url includes a port number, like https://testnet.era.zksync.dev:443, a wallet is successfully instantiated.

Unfortunately many rpc urls do not contain a port number (i.e infura links), these rpc urls are unable to successfully instantiate a wallet.

Here is code that illustrates the issue: (can be pulled from this repo)

Cargo.toml:

[dependencies]
zksync = {git = "https://github.com/matter-labs/zksync-era.git"}
zksync_eth_signer = {git = "https://github.com/matter-labs/zksync-era.git"}

main.rs:

use zksync::{
    self,
    signer::Signer,
    wallet::Wallet,
    zksync_types::{L2ChainId, PackedEthSignature, H256},
};
use zksync_eth_signer::PrivateKeySigner;

const CHAIN: u16 = 280;

// Switch these comments to suppress error
const RPC_URL: &str = "https://testnet.era.zksync.dev";
// const RPC_URL: &str = "https://testnet.era.zksync.dev:443";

fn main() {
    let mut eth_private_key = H256::default();
    eth_private_key.randomize();

    let eth_signer = PrivateKeySigner::new(eth_private_key);
    let address_from_pk = PackedEthSignature::address_from_private_key(&eth_private_key).unwrap();
    let signer = Signer::new(eth_signer, address_from_pk, L2ChainId(CHAIN));

    // getting port error retrieving this wallet, if no port provided
    let wallet = Wallet::with_http_client(RPC_URL, signer);
    println!("{:#?} wallet", wallet);
}

When the above code is run:

cargo run

Output:

Err(
    RpcError(
        Transport(
            Url(
                "Port number is missing in the URL",
            ),
        ),
    ),
) wallet

To Reproduce Error:

Clone Repo

Clone rpc-url-issue

# Clone repo
git clone https://github.com/sammyshakes/rpc-url-issue.git

# Navigate to folder
cd rpc-url-issue

#execute
cargo run
@mm-zk
Copy link
Collaborator

mm-zk commented May 12, 2023

This error comes from jsonrpsee library, that Wallet uses underneath.

paritytech/jsonrpsee#1048

@sammyshakes
Copy link
Author

ok thanks for this, since we cannot modify the jsonrpsee library, i have included some code to force in the default port:

use zksync::{
    self,
    signer::Signer,
    wallet::Wallet,
    zksync_types::{L2ChainId, PackedEthSignature, H256},
};
use zksync_eth_signer::PrivateKeySigner;

//added this crate:  url = "2.3.1"
use url::Url;

const CHAIN: u16 = 280;

const RPC_URL: &str = "https://testnet.era.zksync.dev";
// const RPC_URL: &str = "https://testnet.era.zksync.dev:443";

fn main() {
    let mut eth_private_key = H256::default();
    eth_private_key.randomize();

    let eth_signer = PrivateKeySigner::new(eth_private_key);
    let address_from_pk = PackedEthSignature::address_from_private_key(&eth_private_key).unwrap();
    let signer = Signer::new(eth_signer, address_from_pk, L2ChainId(CHAIN));

    //-------------------------------------------///
    // Force default URL

    // Parse the URL string into a Url object
    let url = Url::parse(RPC_URL).expect("Failed to parse URL");

    // grab default port
    let default_port = url.scheme() == "https" && url.port().is_none();
    let port = url
        .port()
        .unwrap_or_else(|| if default_port { 443 } else { 0 });
    
    // Format complete url
    let complete_url = format!(
        "{}://{}:{}{}",
        url.scheme(),
        url.host_str().unwrap(),
        port,
        url.path()
    );

    println!("Complete URL: {}", complete_url);
    let url_str = complete_url.as_str();

    //-------------------------------------------///

    // getting port error retrieving this wallet, if no port provided
    let wallet = Wallet::with_http_client(url_str, signer);
    println!("{:#?} wallet", wallet);
}

@popzxc
Copy link
Member

popzxc commented Oct 27, 2023

Hello! Development of SDK in this monorepo is discontinued, this SDK currently exists for some internal needs only and will be removed soon.
Please use a newer official SDK for Rust: https://era.zksync.io/docs/api/rust/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants