如何在不使用API​​的情况下生成Trx钱包

时间:2019-11-09 18:30:56

标签: php cryptography tron

基于TRX documents和GitHub中的一些搜索,我试图离线生成钱包,但由于某些原因,我无法使用API​​。

基于Trx文档,我应该执行以下步骤:

  
      
  1. 生成密钥对并提取公共密钥(代表其x,y坐标的64字节字节数组)。
  2.   
  3. 使用sha3-256函数散列公共密钥,并提取结果的最后20个字节。
  4.   
  5. 将0x41添加到字节数组的开头。初始地址的长度应为21个字节。
  6.   
  7. 使用sha256函数对地址进行两次哈希处理,并将前4个字节作为验证码。
  8.   
  9. 将验证码添加到初始地址的末尾,并通过base58编码获得base58check格式的地址。
  10.   
  11. 已编码的Mainnet地址以T开头,长度为34个字节。
  12.   
     

请注意:采用的sha3协议为KECCAK-256。

我在GitHub中找到mattvb91/tron-trx-php,并且该存储库中有一个钱包生成器方法/src/Wallet.php,但是生成的密钥验证返回错误异常,验证失败。

我尝试重新编码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;
            }
        }

这是我的问题:

  1. validateAddress方法具有错误异常
  2. 当我在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生成了Hash和...的keyPair和iexbase/tron-api支持类。

1 个答案:

答案 0 :(得分:1)

我可以调试并解决问题,并与您分享解决方案

无处解决

这行代码具有错误异常

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)修复

并解决差异

在密钥对数组中,我从私有密钥十六进制的第一行中删除了0x,并且可以访问Tron Link Extension中的钱包,不记得要进行钱包转换了激活