Skip to content

PubKeyInfo类的getAddress方法没有判断publicKey的前缀,3.0版本的SDK获取address会出现问题。 #32

@limingxie

Description

@limingxie

使用mnemonicService.generatePrivateKeyByMnemonic方法创建PkeyInfo对象后,
pkeyInfo.getPublicKey().getAddress()会获得错误的address
因为 PubKeyInfo的getAddress方法没有判断前缀直接截取的publicKey所以SDK 3.0版本获得的Address是错误的。

public String getAddress(){
    EccTypeEnums eccTypeEnums = EccTypeEnums.getEccByName(eccName);
    CryptoKeyPair cryptoKeyPair = KeyUtils.getCryptKeyPair(eccTypeEnums);
    return Numeric.toHexString(cryptoKeyPair.getAddress(Arrays.copyOfRange(publicKey,1,publicKey.length))); //这里直接截取了
}

//添加了PubKey的长度判断
==>
public String getAddress(){
    EccTypeEnums eccTypeEnums = EccTypeEnums.getEccByName(eccName);
    CryptoKeyPair cryptoKeyPair = KeyUtils.getCryptKeyPair(eccTypeEnums);
    if (Numeric.toHexString(publicKey).length() == CryptoKeyPair.PUBLIC_KEY_LENGTH_IN_HEX){
        return Numeric.toHexString(cryptoKeyPair.getAddress(publicKey));
    } else {
        return Numeric.toHexString(cryptoKeyPair.getAddress(Arrays.copyOfRange(publicKey,1,publicKey.length)));
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions