0

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

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

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

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

mattvb91/tron-trx-php在 GitHub 和这个存储库中找到了一个钱包生成器方法/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

附加信息

我曾经为 Hash 和ionux/phactor生成 keyPair 和iexbase/tron-apiSupport 类...

4

1 回答 1

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 修复(例外:stripos():非字符串针将来会被解释为字符串。使用显式 chr() 调用来保留当前行为 #770

并解决差异

在密钥对数组中,我0x从第一个私钥十六进制中删除,我可以访问 Tron 链接扩展中的钱包不记得有钱包激活的过渡

于 2019-11-09T23:47:37.230 回答