PHP implements mnemonic conversion to TRX, ETH private key and wallet address

TRX mnemonic transfer address online is full of Java, js or other language development examples. A simple function needs to rely on other environments to realize it, which means that it cannot be tolerated. After all, PHP is the best language in the world. [dog head]

1. Knowledge preparation

To transfer the mnemonic phrase to the TRX private key and address, you first need to know the relationship between the mnemonic phrase and the private key wallet address.

It roughly means that in digital currency, all transactions are signed by the private key to confirm the identity, but the private key is irregular, which is not conducive to memory, so some easy-to-remember phrases (mnemonic), mnemonic The word is converted from the private key according to some specifications, which realize the mutual conversion between the mnemonic word and the private key.

PHP implements mnemonic conversion to TRX, ETH private key and wallet address
Create Wallet Instructions

2. TRX mnemonic to private key and address ideas (other chains are also an idea)

First, some netizens wrote a php method to create BTC, LTC, ETH mnemonic words, private keys and address usage examples (transfer) . We can convert the mnemonic words into TRX private keys according to this idea.

PHP implements mnemonic conversion to TRX, ETH private key and wallet address
Create mnemonic and generate BTC private key
PHP implements mnemonic conversion to TRX, ETH private key and wallet address
Create mnemonic and generate ETH private key and address
PHP implements mnemonic conversion to TRX, ETH private key and wallet address
Create mnemonic and generate LTC private key and address

Through the above three cases, we found that when the mnemonic is transferred to a private key in a different chain, it only needs to call different BIP44 options. So in theory, all our chains that comply with the BIP44 specification can convert mnemonic words into private keys according to this method.

Corresponding codes of different chains in the BIP44 specification: Registered coin types for BIP-0044

To transfer the wallet address is to use the generation rules of different chains to convert the private key into a public key, and the public key generates an address again. So to implement address transfer, you only need to find the development kits of different chains.

Relationship between private key, public key and address Relationship between private key, public key and address

3. Implementation method

trx This package I use here

composer require fenguoz/tron-php
PHP implements mnemonic conversion to TRX, ETH private key and wallet address
    //const URI = 'https://api.shasta.trongrid.io'; // shasta testnet
    const URI = 'https://api.trongrid.io'; // mainnet
    const TRON_PRO_API_KEY = 'xxxxxxxxxx'; // mainnet
    const CONTRACT = [
        'contract_address' => 'TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t', // USDT TRC20
        'decimals' => 6,
    ];

    private $trx;
    private $trx20;

    /**
     * @throws TronErrorException
     */
    private function getTRX(): TRX
    {
        if ($this->trx==null){
            $api = new Api(new Client(['base_uri' => self::URI,'headers'=>['TRON-PRO-API-KEY'=>self::TRON_PRO_API_KEY]]));
            $this->trx = new Trx($api);
        }
        return $this->trx;
    }

    /**
     * 私钥转地址
     * @throws TronErrorException
     */
    public function privateKeyToAddress($privateKey): TronAddress
    {
        return $this->getTRX()->privateKeyToAddress($privateKey);
    }

    /**
     * @throws TronErrorException
     * @throws Exception
     * trc 助记词转地址
     */
    public function trxMnemonicToAddress($mnemonic): TronAddress
    {
        $seedGenerator = new Bip39SeedGenerator();
        // 通过助记词生成种子,传入可选加密串'hello'
        $seed = $seedGenerator->getSeed($mnemonic);
        $hdFactory = new HierarchicalKeyFactory();
        $master = $hdFactory->fromEntropy($seed);
        $hardened = $master->derivePath("44'/195'/0'/0/0");
        $pri = $hardened->getPrivateKey()->getHex();
        return $this->privateKeyToAddress($pri);
    }

When developing and debugging, you can use this website to test and compare the generated results: Mnemonic Code Converter

4. Tips

Mnemonics and private keys are very important and sensitive data. Once leaked, it may cause serious property damage. Please use it with caution.

5. Reference materials

  1. Simple use of Bit-Wasp/bitcoin-php: create a wallet + make a transaction
  2. PHP creates BTC, LTC, ETH mnemonic, private key and address usage examples
  3. Mnemonic Code Converter
  4. Understand BIP32, BIP44, BIP39 involved in developing HD wallet

The ranking is not in particular order and is based only on the order in which the browsers are closed. [laughing and crying]

Guess you like

Origin blog.csdn.net/wwj256/article/details/129718835