Fenguoz / tron-php

Support TRON's TRX and TRC20, which include functions such as address creation, balance query, transaction transfer, query the latest blockchain, query information based on the blockchain, and query information based on the transaction hash
MIT License
194 stars 65 forks source link

generateAddress(),大家注意了,这个方法生成的钱包私钥跟地址不匹配。下面是我改写的方法 #35

Closed bigdllmask1333 closed 1 year ago

bigdllmask1333 commented 2 years ago
/**
 * 生成钱包地址
 * 中文文档:https://tronapi.gitbook.io/trx/
 */
public function generateAddress()
{
    $tron = new \IEXBase\TronAPI\Tron();
    $generateAddress = $tron->generateAddress(); // or createAddress()
    $address = (array)($generateAddress)->getRawData();
    $retdata['privateKey'] = $address['private_key'];
    $retdata['address'] = $address['address_base58'];
    $retdata['hexAddress'] = $address['address_hex'];
    return json(['code' => 1, 'data' => $retdata]);
}
fuckwow commented 1 year ago

非常感谢,不过也可以直接改原文 因为PHP8 所以返回是 ADDRESS类型 调整为

/**生成地址
 * @return Address
 * @throws TronErrorException
 * @throws TronException
 */
public function generateAddress(): Address
{
    $attempts = 0;
    $validAddress = false;
    $tron = new \IEXBase\TronAPI\Tron();

    do {
        if ($attempts++ === 5) {
            throw new TronErrorException('Could not generate valid key');
        }

        $generateAddress = $tron->generateAddress(); // or createAddress()
        $address = (array)($generateAddress)->getRawData();
        $retdata['privateKey'] = $address['private_key'];
        $retdata['address'] = $address['address_base58'];
        $retdata['hexAddress'] = $address['address_hex'];
        $address = new Address($address['address_base58'], $address['private_key'], $address['address_hex']);
        $validAddress = $this->validateAddress($address);
    } while (!$validAddress);

    return $address;
}
yeshili commented 1 year ago

php8.02无法安装改项目,头疼,老师提示版本出错 !