tronprotocol / tronweb

Javascript API Library for interacting with the TRON Network
MIT License
413 stars 271 forks source link

TronWeb and ethers lib generate different addresses #465

Closed gagarin55 closed 8 months ago

gagarin55 commented 8 months ago

Hello,

This documentation says: "The TRON address in Hex format removes the leading 41 to get the Ethereum address." https://developers.tron.network/docs/account#account-address-format

Following source code generates different addresses for same mnemonic phrase and derivation path:

import { HDNodeWallet, ethers } from "ethers";
import { TronWeb } from "tronweb";

  // MNEMONIC
  const mnemonic = "patch left empty genuine rain normal syrup yellow consider moon stock denial";
  const path = "m/44'/195'/0'/0/0";

  // TronWeb
  const keyPair = TronWeb.fromMnemonic(
    mnemonic,
    path
  );
  const tronAddress = keyPair.address;
  const tronHexAddress = TronWeb.address.toHex(tronAddress);
  // 4196bb8957ad294a8920ced289a5ac85872cee8b23 

  // ethers
  const masterNode_a = HDNodeWallet.fromMnemonic(ethers.Mnemonic.fromPhrase(mnemonic));
  const etherAddress = masterNode_a.derivePath(path).getAddress();
  // 0x0d31Fc78F7bCCB8C4D32B60E0ed395C0cbD2c483

packages.json

  "dependencies": {

    "ethers": "^6.6.0",

    "tronweb": "^6.0.0-beta.0"
  }

TronWeb returns address 4196bb8957ad294a8920ced289a5ac85872cee8b23 which without first byte is 96bb8957ad294a8920ced289a5ac85872cee8b23. But ethers lib returns 0x0d31Fc78F7bCCB8C4D32B60E0ed395C0cbD2c483

I suppose according to Tron documentation (https://developers.tron.network/docs/account#account-address-format) they must be the same ?

Correct me if I wrong. Thank you.

start940315 commented 8 months ago

You need to pass the path to HDNodeWallet.fromMnemonic as the second parameter. Because when you use HDNodeWallet.fromMnemonic, it already derived a default path for you. You can check out ethers code for more details.