簡體   English   中英

如何在不使用 API 的情況下生成 Trx 錢包

[英]how to generate Trx wallet without using API

基於TRX 文檔和 GitHub 中的一些搜索我嘗試離線生成錢包,但由於某些原因我無法使用 API。

根據 Trx 文檔,我應該執行以下步驟:

  1. 生成密鑰對並提取公鑰(表示其 x,y 坐標的 64 字節字節數組)。
  2. Hash 公鑰使用 sha3-256 function 並提取結果的最后 20 個字節。
  3. 將 0x41 添加到字節數組的開頭。 起始地址的長度應為 21 個字節。
  4. Hash 地址兩次使用sha256 function 並取前4個字節作為驗證碼。
  5. 將驗證碼添加到初始地址的末尾,通過base58編碼得到一個base58check格式的地址。
  6. 編碼的主網地址以 T 開頭,長度為 34 個字節。

請注意:采用的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支持類...

我可以調試並解決問題並與您分享解決方案

無處可尋

這行代碼有一個錯誤異常

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 鏈接擴展中的錢包不記得有錢包激活的轉換

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM