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

The examples haven't been updated in 5 years #338

Open
accountnujen opened this issue Dec 9, 2023 · 1 comment
Open

The examples haven't been updated in 5 years #338

accountnujen opened this issue Dec 9, 2023 · 1 comment

Comments

@accountnujen
Copy link

The examples haven't been updated in 5 years. It's kind of impossible to create accounts anymore. At least in quicknode and infura you can't newAccount.

If I want to see the wallet balance, which query should I run?
Or if I want to send a transaction, what should I write?

@rhinogroup
Copy link

    $eth->getBalance($from_address, function ($err, $balance) use ($from_address) {
        if ($err !== null) {
            echo '<br>' . __LINE__ . ' -- Error: ' . $err->getMessage();
            return;
        }
        echo '<br>' . __LINE__ . ' -- From Address: ' . $from_address . ' Balance: ' . $balance . PHP_EOL;
    });

This will get you a balance according to the ../vendor/web3p/web3.php/examples/sendTransaction.php

Although I can't get the sendTransaction function to work, it does provide me the balances to both send and receive accounts.

I seem to remember reading somewhere that it is no longer possible to create a newAccount on the Ethereum blockchain. Deprecated? Not sure. I use the following code to create an account:

function create_eth_address(){
    $config = [
        'private_key_type' => OPENSSL_KEYTYPE_EC,
        'curve_name' => 'secp256k1'
    ];
    $res = openssl_pkey_new($config);

    if (!$res) {
        $error[__LINE__] = 'ERROR: Fail to generate private key. -> ' . openssl_error_string();
        // debug($error, "\nFILE: ".__FILE__." -- \nLINE: " . __LINE__. " -- error: ", "");
        exit;
    }

    openssl_pkey_export($res, $priv_key);

    $key_detail = openssl_pkey_get_details($res);

    $pub_key = $key_detail["key"];

    $priv_pem = PEM::fromString($priv_key);

    $ec_priv_key = ECPrivateKey::fromPEM($priv_pem);

    $ec_priv_seq = $ec_priv_key->toASN1();

    $priv_key_hex = bin2hex($ec_priv_seq->at(1)->asOctetString()->string());
    $priv_key_len = strlen($priv_key_hex) / 2;
    $pub_key_hex = bin2hex($ec_priv_seq->at(3)->asTagged()->asExplicit()->asBitString()->string());
    $pub_key_len = strlen($pub_key_hex) / 2;

    $pub_key_hex_2 = substr($pub_key_hex, 2);
    $pub_key_len_2 = strlen($pub_key_hex_2) / 2;

    $hash = Keccak::hash(hex2bin($pub_key_hex_2), 256);

    $pubkey = '0x' . substr($hash, -40);
    $privkey = '0x' . $priv_key_hex;

   // Store your new pubkey and privkey here.
}

You can check your new address on any eth explorer

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

2 participants