Ethereum addresses are composed of the prefix "0x", a common identifier for hexadecimal, concatenated with the rightmost 20 bytes of the Keccak-256 hash (big endian) of the ECDSA public key (the curve used is the so-called secp256k1, the same as Bitcoin)
There are 2 types of account in ETH network, externally owned account (EOA) and contract account. Remember account just simply mean an address.
Externally owned account (EOA)
Contract account
<?php
use kornrunner\Keccak;
use BitWasp\Bitcoin\Key\Factory\PrivateKeyFactory;
use BitWasp\Bitcoin\Crypto\Random\Random;
include_once "../libraries/vendor/autoload.php";
include_once("html_iframe_header.php");
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
try {
$privKeyFactory = new PrivateKeyFactory();
if (!$_POST['input'] OR ctype_xdigit($_POST['input']))
{
if (!$_POST['input'])
{
$rbg = new Random();
$privateKey = $privKeyFactory->generateUncompressed($rbg);
} else {
$privateKey = $privKeyFactory->fromHexUncompressed($_POST['input']);
}
}
$publicKey = $privateKey->getPublicKey();
$madeUpEthAddress = substr($publicKey->getHex(), 2);
$hash = Keccak::hash(hex2bin($madeUpEthAddress), 256);
// Ethereum address has 20 bytes length. (40 hex characters long)
// We only need the last 20 bytes as Ethereum address
$ethAddress = '0x' . substr($hash, -40);
?>
<div class="table-responsive">
<table border=0 class='table'>
<tr style='background-color:#f0f0f0'><td>Address</td><td><?php echo $ethAddress?></td></tr>
<tr><td>Private Key Hex</td><td><?php echo $privateKey->getHex()?></td></tr>
<tr style='background-color:#f0f0f0'><td>Public Key Hex</td><td><?php echo $publicKey->getHex()?></td></tr>
</table>
</div>
<?php
} catch (Exception $e) {
$errmsg .= "Problem found. " . $e->getMessage();
}
}
if ($errmsg) {
?>
<div class="alert alert-danger">
<strong>Error!</strong> <?php echo $errmsg?>
</div>
<?php
}
?>
<form action='' method='post'>
<div class="form-group">
<label for="input">Private Key (Hex):</label>
<input class="form-control" type='text' name='input' id='input' value='<?php echo $_POST['input']?>'>
<small>Put empty if you want system assign you a random private key.</small>
</div>
<input type='submit' class="btn btn-success btn-block"/>
</form>
<?php
include_once("html_iframe_footer.php");
Contract address will be auto generated (without private key) when contract-type tx publish to ETH network.
<?php
use kornrunner\Keccak;
use Web3p\RLP\RLP;
include_once "../libraries/vendor/autoload.php";
include_once("html_iframe_header.php");
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
try {
$rlp = new RLP;
$normalizedAddress = "0x" . ltrim($_POST['address'], "0x");
$encoded = $rlp->encode([$normalizedAddress, (int)$_POST['nonce']]);
$hash = Keccak::hash(hex2bin($encoded->toString('hex')), 256);
$ethAddress = '0x' . substr($hash, -40);
?>
<div class="table-responsive">
<table border=0 class='table'>
<tr style='background-color:#f0f0f0'><td>Address</td><td><?php echo $ethAddress?></td></tr>
</table>
</div>
<?php
} catch (Exception $e) {
$errmsg .= "Problem found. " . $e->getMessage();
}
}
if ($errmsg) {
?>
<div class="alert alert-danger">
<strong>Error!</strong> <?php echo $errmsg?>
</div>
<?php
}
?>
<form action='' method='post'>
<div class="form-group">
<label for="address">Sender Address:</label>
<input class="form-control" type='text' name='address' id='address' value='<?php echo $_POST['address']?>'>
</div>
<div class="form-group">
<label for="nonce">Nonce:</label>
<input class="form-control" type='text' name='nonce' id='nonce' value='<?php echo $_POST['nonce']?>'>
</div>
<input type='submit' class="btn btn-success btn-block"/>
</form>
<?php
include_once("html_iframe_footer.php");
You can call JSON-RPC with getcode method. if the address is representing an EOA you will get 0x as response otherwise you will get the contract's bytecode.
<?php
include_once "../libraries/vendor/autoload.php";
$supportChains = ['1'=>"Ethereum Mainnet", '3'=>"Ethereum Testnet Ropsten"];
include_once("html_iframe_header.php");
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
try {
if (!preg_match('/^https\:\/\/([a-z]+)\.infura\.io/', $_POST['url'])) {
throw new Exception("Please provide valid INFURA full URL with project ID.");
}
$ch = curl_init();
$params = [];
$params['jsonrpc']= "2.0";
$params['method'] = 'eth_getCode';
$params['params'] = [$_POST['address'],'latest'];
$params['id'] = $_POST['chain'];
curl_setopt($ch, CURLOPT_URL,$_POST['url']);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$req = json_encode($params));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, ["Content-Type: application/json"]);
$resp = curl_exec($ch);
if ($resp === false) {
throw new Exception("curl_exec return false");
}
if (strlen($err = @curl_error($ch)) > 0) {
$errno = @curl_errno($ch);
throw new Exception( "{$err} ({$errno})" );
}
$result = json_decode($resp,true);
$result = $result['result'];
curl_close ($ch);
?>
<div class="alert alert-success">
<h6 class="mt-3">Request</h6>
<textarea class="form-control" rows="5" id="comment" readonly><?php echo $req;?></textarea>
<h6 class="mt-3">Response</h6>
<textarea class="form-control" rows="1" id="comment" readonly><?php echo $resp;?></textarea>
<h6 class="mt-3">Result</h6>
<?php
if ($result == '0x' or $result == '0x0') {
echo "{$_POST['address']} is EOA address.";
} else if (substr($result,0,2) == '0x' AND strlen($result) >= 3) {
echo "{$_POST['address']} is contract address.";
}
?>
</div>
<?php
} catch (Exception $e) {
$errmsg .= "Problem found. " . $e->getMessage();
}
}
if ($errmsg) {
?>
<div class="alert alert-danger">
<strong>Error!</strong> <?php echo $errmsg?>
</div>
<?php
}
?>
<form id='this_form' action='?action=submit' method='post'>
<div class="form-group">
<label for="chain">Chain:</label>
<select id="chain" name="chain" class="form-control" >
<?php
foreach($supportChains as $k=>$v) {
echo "<option value='{$k}'".($k == $_POST['chain'] ? " selected": "").">{$v}</option>";
}
?>
</select>
</div>
<div class="form-group">
<label for="url">INFURA full URL (with project ID):</label>
<input class="form-control" type='text' name='url' id='url' value='<?php echo $_POST['url']?>'>
<small>e.g https://mainnet.infura.io/v3/11223344556677889900aabbccdd</small>
</div>
<div class="form-group">
<label for="address">Address:</label>
<input class="form-control" type='text' name='address' id='address' value='<?php echo $_POST['address']?>'>
</div>
<input type='submit' class="btn btn-success btn-block"/>
</form>
<?php
include_once("html_iframe_footer.php");