web3p / web3.php

A php interface for interacting with the Ethereum blockchain and ecosystem. Native ABI parsing and smart contract interactions.
MIT License
1.15k stars 540 forks source link

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

Open accountnujen opened 7 months ago

accountnujen commented 7 months ago

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 commented 7 months ago
    $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