如何使用PHP生成以太坊錢包和金鑰對?

2021-09-13 03:19:50 字數 3804 閱讀 5399

本文將提供有關如何生成ecdsa私鑰的指南,然後使用php7.0++匯出到以太坊錢包位址。

你可以找到以下工作實現:

composer.json

}

generateethereumwallet.php

<?php

require_once "vendor/autoload.php";

use sop\cryptotypes\asymmetric\ec\ecpublickey;

use sop\cryptotypes\asymmetric\ec\ecprivatekey;

use sop\cryptoencoding\pem;

use kornrunner\keccak;

$config = [

'private_key_type' => openssl_keytype_ec,

'curve_name' => 'secp256k1'

];$res = openssl_pkey_new($config);

if (!$res)

// 生成私鑰

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);

// 然後將其轉換為asn1結構

$ec_priv_seq = $ec_priv_key->toasn1();

// hex中的私鑰和公鑰

$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()->a***plicit()->asbitstring()->string());

$pub_key_len = strlen($pub_key_hex) / 2;

// 從公鑰匯出以太坊位址

// 每個ec公鑰始終以0x04開頭,

// 我們需要刪除前導0x04才能正確hash它

$pub_key_hex_2 = substr($pub_key_hex, 2);

$pub_key_len_2 = strlen($pub_key_hex_2) / 2;

// hash

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

// 以太坊位址長度為20個位元組。 (40個十六進製制字元長)

// 我們只需要最後20個位元組作為以太坊位址

$wallet_address = '0x' . substr($hash, -40);

$wallet_private_key = '0x' . $priv_key_hex;

echo "\r\n eth wallet address: " . $wallet_address;

echo "\r\n private key: " . $wallet_private_key;

該**需要php 7.0++,openssl擴充套件和php composer。需要使用php composer來安裝第三方軟體包。

$ composer install
以太坊標準是使用secp256k1曲線生成私鑰。在我的教程中,我使用openssl函式生成pem格式的橢圓曲線私鑰,如下所示:

$config = [

'private_key_type' => openssl_keytype_ec,

'curve_name' => 'secp256k1'

];$res = openssl_pkey_new($config);

if (!$res)

// generate private key

openssl_pkey_export($res, $priv_key);

// pem format

$priv_pem = pem::fromstring($priv_key);

之後我需要將私鑰轉換為asn1序列,下面是ans1序列的結構。

ecprivatekey ::= sequence  (ecprivkeyver1),

privatekey octet string,

parameters [0] ecparameters } optional,

publickey [1] bit string optional

}

下面的**是我如何從ans1序列結構中查詢十六進製制字串中的公鑰和私鑰。

// then convert it to asn1 structure

$ec_priv_seq = $ec_priv_key->toasn1();

// private key & public key in hex

$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()->a***plicit()->asbitstring()->string());

$pub_key_len = strlen($pub_key_hex) / 2;

以太坊錢包位址來自公鑰。每個ec公鑰始終以0x04開頭。為了獲得以太坊錢包位址的正確雜湊值,我們需要刪除前導0x04。

$pub_key_hex_2 = substr($pub_key_hex, 2);

$pub_key_len_2 = strlen($pub_key_hex_2) / 2;

我們繼續使用ec公鑰的keccak256雜湊。 以太坊錢包位址長度為20個位元組,長度為40個字元,因此我們只需要雜湊資料的最後20個位元組。

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

$wallet_address = '0x' . substr($hash, -40);

$wallet_private_key = '0x' . $priv_key_hex;

現在你有$wallter_address**儲存你新生成的以太坊錢包位址,**$wallet_private_key是你以太坊錢包位址的私鑰。

$ php generateethereumwallet.php
eth wallet address: 0xb2...

private key: 0x73...

php以太坊,主要是介紹使用php進行智慧型合約開發互動,進行賬號建立、交易、轉賬、代幣開發以及過濾器和交易等內容。

使用PHP生成以太坊錢包和金鑰對

你可以找到以下工作實現 composer.json generateethereumwallet.php require once vendor autoload.php use sop cryptotypes asymmetric ec ecpublickey use sop cryptotype...

geth 以太坊錢包 以太坊錢包Geth使用命令

鏈客,有問必答!一 啟動以太坊錢包geth 開啟乙個控制台,執行同步區塊命令 同步測試鏈 geth fast cache 512 rpc rpcapi personal,db,eth,net,web3 testnet datadir e projecttestgeth 如果為了讓區域網中其他節點訪問...

geth 以太坊錢包 以太坊錢包Geth使用命令

一 啟動以太坊錢包geth 開啟乙個控制台,執行同步區塊命令 同步測試鏈 geth fast cache 512 rpc rpcapi personal,db,eth,net,web3 testnet datadir e project testgeth 如果為了讓區域網中其他節點訪問到服務,請設定...