如何在不使用 API 的情况下生成 Trx 钱包
how to generate Trx wallet without using API
基于 TRX documents 和 GitHub 中的一些搜索 我尝试离线生成钱包,但由于某些原因我无法使用 API。
根据 Trx 文档,我应该执行以下步骤:
- Generate a key pair and extract the public key (a 64-byte byte array representing its x,y coordinates).
- Hash the public key using sha3-256 function and extract the last 20 bytes of the result.
- Add 0x41 to the beginning of the byte array. The length of the initial address should be 21 bytes.
- Hash the address twice using sha256 function and take the first 4 bytes as verification code.
- Add the verification code to the end of the initial address and get an address in base58check format through base58 encoding.
- An encoded Mainnet address begins with T and is 34 bytes in length.
Please note: the sha3 protocol adopted is KECCAK-256.
我在 GitHub 中找到 mattvb91/tron-trx-php
并且这个存储库有一个钱包生成器方法 /src/Wallet.php
但是生成的密钥验证 return 一个错误异常和验证失败。
我尝试重新编写 mattvb91/tron-trx-php
钱包生成器方法并创建我的钱包生成器
class walletGenerator
{
private $addressPrefix = "41";
private $addressPrefixByte = 0x41;
private $addressSize = 34;
public function __construct()
{
}
public function generateWallet()
{
$key = new Key();
$odd = false;
while (!$odd)
{
$keyPair = $key->GenerateKeypair();
if(strlen($keyPair['public_key']) % 2 == 0) $odd = true;
}
$privateKeyHex = $keyPair['private_key_hex'];
$pubKeyHex = $keyPair['public_key'];
$pubKeyBin = hex2bin($pubKeyHex);
$addressHex = $this->getAddressHex($pubKeyBin);
$addressBin = hex2bin($addressHex);
$addressBase58 = $this->getBase58CheckAddress($addressBin);
$validAddress = $this->validateAddress($addressBase58);
}
private function getAddressHex($pubKeyBin)
{
if (strlen($pubKeyBin) == 65) {
$pubKeyBin = substr($pubKeyBin, 1);
}
$hash = Keccak::hash($pubKeyBin , 256);
$hash = substr($hash, 24);
return $this->addressPrefix . $hash;
}
private function getBase58CheckAddress($addressBin){
$hash0 = Hash::SHA256($addressBin);
$hash1 = Hash::SHA256($hash0);
$checksum = substr($hash1, 0, 4);
$checksum = $addressBin . $checksum;
$result = Base58::encode(Crypto::bin2bc($checksum));
return $result;
}
private function validateAddress($walletAddress){
if(strlen($walletAddress) !== $this->addressSize) return false;
$address = Base58Check::decode($walletAddress , false , 0 , false);
$utf8 = hex2bin($address);
if(strlen($utf8) !== 25) return false;
if(strpos($utf8 , $this->addressPrefixByte) !== 0) return false; // strpos(): Non-string needles will be interpreted as strings in the future. Use an explicit chr() call to preserve the current behavior
$checkSum = substr($utf8, 21);
$address = substr($utf8, 0, 21);
$hash0 = Hash::SHA256($address);
$hash1 = Hash::SHA256($hash0);
$checkSum1 = substr($hash1, 0, 4);
if ($checkSum === $checkSum1) {
return true;
}
}
这是我的问题:
validateAddress
方法出现错误异常
- 当我在 Trx 钱包中手动添加私钥时,检测到的钱包地址与生成的地址不同。
if(strpos($utf8 , $this->addressPrefixByte) !== 0) return false; // strpos(): Non-string needles will be interpreted as strings in the future. Use an explicit chr() call to preserve the current behavior
补充信息
我使用 ionux/phactor
生成密钥对,iexbase/tron-api
支持 类 哈希和 ...
我可以调试并解决问题并与您分享解决方案
无处可寻
这行代码出现错误异常
if(strpos($utf8 , $this->addressPrefixByte) !== 0) return false; // strpos(): Non-string needles will be interpreted as strings in the future. Use an explicit chr() call to preserve the current behavior
这个问题是因为 PHP 7.3 的错误并用 PHP 7.2 ( Exception: stripos(): Non-string needles will be interpreted as strings in the future. Use an explicit chr() call to preserve the current behavior #770 )
修复
并解决差异
在密钥对数组中,我从 Private Key Hex 的第一个中删除 0x
然后我可以访问 Tron 中的钱包 Link Extension don记得要有激活钱包的过渡
基于 TRX documents 和 GitHub 中的一些搜索 我尝试离线生成钱包,但由于某些原因我无法使用 API。
根据 Trx 文档,我应该执行以下步骤:
- Generate a key pair and extract the public key (a 64-byte byte array representing its x,y coordinates).
- Hash the public key using sha3-256 function and extract the last 20 bytes of the result.
- Add 0x41 to the beginning of the byte array. The length of the initial address should be 21 bytes.
- Hash the address twice using sha256 function and take the first 4 bytes as verification code.
- Add the verification code to the end of the initial address and get an address in base58check format through base58 encoding.
- An encoded Mainnet address begins with T and is 34 bytes in length.
Please note: the sha3 protocol adopted is KECCAK-256.
我在 GitHub 中找到 mattvb91/tron-trx-php
并且这个存储库有一个钱包生成器方法 /src/Wallet.php
但是生成的密钥验证 return 一个错误异常和验证失败。
我尝试重新编写 mattvb91/tron-trx-php
钱包生成器方法并创建我的钱包生成器
class walletGenerator
{
private $addressPrefix = "41";
private $addressPrefixByte = 0x41;
private $addressSize = 34;
public function __construct()
{
}
public function generateWallet()
{
$key = new Key();
$odd = false;
while (!$odd)
{
$keyPair = $key->GenerateKeypair();
if(strlen($keyPair['public_key']) % 2 == 0) $odd = true;
}
$privateKeyHex = $keyPair['private_key_hex'];
$pubKeyHex = $keyPair['public_key'];
$pubKeyBin = hex2bin($pubKeyHex);
$addressHex = $this->getAddressHex($pubKeyBin);
$addressBin = hex2bin($addressHex);
$addressBase58 = $this->getBase58CheckAddress($addressBin);
$validAddress = $this->validateAddress($addressBase58);
}
private function getAddressHex($pubKeyBin)
{
if (strlen($pubKeyBin) == 65) {
$pubKeyBin = substr($pubKeyBin, 1);
}
$hash = Keccak::hash($pubKeyBin , 256);
$hash = substr($hash, 24);
return $this->addressPrefix . $hash;
}
private function getBase58CheckAddress($addressBin){
$hash0 = Hash::SHA256($addressBin);
$hash1 = Hash::SHA256($hash0);
$checksum = substr($hash1, 0, 4);
$checksum = $addressBin . $checksum;
$result = Base58::encode(Crypto::bin2bc($checksum));
return $result;
}
private function validateAddress($walletAddress){
if(strlen($walletAddress) !== $this->addressSize) return false;
$address = Base58Check::decode($walletAddress , false , 0 , false);
$utf8 = hex2bin($address);
if(strlen($utf8) !== 25) return false;
if(strpos($utf8 , $this->addressPrefixByte) !== 0) return false; // strpos(): Non-string needles will be interpreted as strings in the future. Use an explicit chr() call to preserve the current behavior
$checkSum = substr($utf8, 21);
$address = substr($utf8, 0, 21);
$hash0 = Hash::SHA256($address);
$hash1 = Hash::SHA256($hash0);
$checkSum1 = substr($hash1, 0, 4);
if ($checkSum === $checkSum1) {
return true;
}
}
这是我的问题:
validateAddress
方法出现错误异常- 当我在 Trx 钱包中手动添加私钥时,检测到的钱包地址与生成的地址不同。
if(strpos($utf8 , $this->addressPrefixByte) !== 0) return false; // strpos(): Non-string needles will be interpreted as strings in the future. Use an explicit chr() call to preserve the current behavior
补充信息
我使用 ionux/phactor
生成密钥对,iexbase/tron-api
支持 类 哈希和 ...
我可以调试并解决问题并与您分享解决方案
无处可寻
这行代码出现错误异常
if(strpos($utf8 , $this->addressPrefixByte) !== 0) return false; // strpos(): Non-string needles will be interpreted as strings in the future. Use an explicit chr() call to preserve the current behavior
这个问题是因为 PHP 7.3 的错误并用 PHP 7.2 ( Exception: stripos(): Non-string needles will be interpreted as strings in the future. Use an explicit chr() call to preserve the current behavior #770 )
修复并解决差异
在密钥对数组中,我从 Private Key Hex 的第一个中删除 0x
然后我可以访问 Tron 中的钱包 Link Extension don记得要有激活钱包的过渡