Skip to content
This repository has been archived by the owner on Mar 24, 2021. It is now read-only.

TronClient.java generateAddress() error! #16

Open
mtdp opened this issue Mar 17, 2021 · 2 comments
Open

TronClient.java generateAddress() error! #16

mtdp opened this issue Mar 17, 2021 · 2 comments

Comments

@mtdp
Copy link

mtdp commented Mar 17, 2021

TronClient 生成地址,是由私钥生成的?

@bingozwb
Copy link

地址都是由私钥算出来的 不知道你的疑惑是不是这个
image
org.tron.tronj.client.TronClient#generateAddress该方法注释写的是返回address 但实际返回的是priKey
该方法中间的一段代码是由priKey计算地址(Hex格式地址) 在这个方法中这段代码是无用的

这是我生成address&pk的方法:

    /**
     * create new address with address&pk
     *
     * @author bingo
     */
    public Map<String, Object> getNewAddressPk() {
        Map<String, Object> data = new LinkedHashMap<>();
        String privateKey = TronClient.generateAddress();
        SECP256K1.PublicKey publicKey = SECP256K1.PublicKey.create(SECP256K1.PrivateKey.create(privateKey));
        // pk to address
        Keccak.Digest256 digest = new Keccak.Digest256();
        digest.update(publicKey.getEncoded(), 0, 64);
        byte[] raw = digest.digest();
        byte[] rawAddr = new byte[21];
        rawAddr[0] = 0x41;
        System.arraycopy(raw, 12, rawAddr, 1, 20);
        String address = Base58Check.bytesToBase58(rawAddr);

        log.info("create address: {}", address);
        data.put("address", address);
        data.put("pk", privateKey);
        return data;
    }

@mtdp
Copy link
Author

mtdp commented Mar 23, 2021

地址都是由私钥算出来的 不知道你的疑惑是不是这个
image
org.tron.tronj.client.TronClient#generateAddress该方法注释写的是返回address 但实际返回的是priKey
该方法中间的一段代码是由priKey计算地址(Hex格式地址) 在这个方法中这段代码是无用的

这是我生成address&pk的方法:

    /**
     * create new address with address&pk
     *
     * @author bingo
     */
    public Map<String, Object> getNewAddressPk() {
        Map<String, Object> data = new LinkedHashMap<>();
        String privateKey = TronClient.generateAddress();
        SECP256K1.PublicKey publicKey = SECP256K1.PublicKey.create(SECP256K1.PrivateKey.create(privateKey));
        // pk to address
        Keccak.Digest256 digest = new Keccak.Digest256();
        digest.update(publicKey.getEncoded(), 0, 64);
        byte[] raw = digest.digest();
        byte[] rawAddr = new byte[21];
        rawAddr[0] = 0x41;
        System.arraycopy(raw, 12, rawAddr, 1, 20);
        String address = Base58Check.bytesToBase58(rawAddr);

        log.info("create address: {}", address);
        data.put("address", address);
        data.put("pk", privateKey);
        return data;
    }

感谢回复,这个方法名称是要生成地址,实际是返回私钥的hex,我的问题是这个。明白地址是从公钥推导出来的,可以把返回修改成 return Base58Check.bytesToBase58(rawAddr);

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants