Java Code Examples for org.fisco.bcos.web3j.crypto.Keys#getAddress()

The following examples show how to use org.fisco.bcos.web3j.crypto.Keys#getAddress() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ImportCertTest.java    From WeBASE-Node-Manager with Apache License 2.0 6 votes vote down vote up
@Test
public void testPubAddress() throws IOException, CertificateException, IllegalAccessException, InstantiationException {
    /**
     * @param: nodeCert
     * 只有节点证书才是ECC椭圆曲线,获取pub的方法和区块链的一致
     * 其余的agency chain 的crt都是rsa方法,使用大素数方法计算,不一样
     */
    // need crt file
    InputStream node = new ClassPathResource("node.crt").getInputStream();
    CertificateFactory cf = CertificateFactory.getInstance("X.509");
    X509Certificate nodeCert = (X509Certificate) cf.generateCertificate(node);
    // rsa算法的公钥和ecc的不一样
    ECPublicKeyImpl pub = (ECPublicKeyImpl) nodeCert.getPublicKey();
    byte[] pubBytes = pub.getEncodedPublicValue();
    String publicKey = Numeric.toHexStringNoPrefix(pubBytes);
    String address = Keys.getAddress(publicKey);
    byte[] addByteArray = Keys.getAddress(pubBytes);
    System.out.println("byte[] : pub ");
    System.out.println(pubBytes);
    System.out.println("====================================");
    System.out.println(publicKey); // 04e5e7efc9e8d5bed699313d5a0cd5b024b3c11811d50473b987b9429c2f6379742c88249a7a8ea64ab0e6f2b69fb8bb280454f28471e38621bea8f38be45bc42d
    System.out.println("byte[] to pub to address ");
    System.out.println(address); // f7b2c352e9a872d37a427601c162671202416dbc
    System.out.println("包含开头的04");
    System.out.println(byteToHex(addByteArray));
}
 
Example 2
Source File: KeyStoreService.java    From WeBASE-Transaction with Apache License 2.0 6 votes vote down vote up
/**
 * get KeyStoreInfo.
 * 
 * @return
 */
public KeyStoreInfo getKey() throws BaseException {
    try {
        ECKeyPair keyPair = Keys.createEcKeyPair();
        String publicKey = Numeric.toHexStringWithPrefixZeroPadded(keyPair.getPublicKey(),
                PUBLIC_KEY_LENGTH_IN_HEX);
        String privateKey = Numeric.toHexStringNoPrefix(keyPair.getPrivateKey());
        String address = "0x" + Keys.getAddress(publicKey);

        KeyStoreInfo keyStoreInfo = new KeyStoreInfo();
        keyStoreInfo.setPublicKey(publicKey);
        keyStoreInfo.setPrivateKey(privateKey);
        keyStoreInfo.setAddress(address);

        return keyStoreInfo;
    } catch (Exception e) {
        log.error("createEcKeyPair fail.");
        throw new BaseException(ConstantCode.SYSTEM_ERROR);
    }
}
 
Example 3
Source File: KeyStoreService.java    From WeBASE-Front with Apache License 2.0 5 votes vote down vote up
/**
 * convert ECKeyPair to KeyStoreInfo.
 * default aes true
 */
private KeyStoreInfo keyPair2KeyStoreInfo(ECKeyPair keyPair, String userName) {
    String publicKey = Numeric
            .toHexStringWithPrefixZeroPadded(keyPair.getPublicKey(), PUBLIC_KEY_LENGTH_IN_HEX);
    String privateKey = Numeric.toHexStringNoPrefix(keyPair.getPrivateKey());
    String address = "0x" + Keys.getAddress(keyPair.getPublicKey());
    log.debug("publicKey:{} privateKey:{} address:{}", publicKey, privateKey, address);
    KeyStoreInfo keyStoreInfo = new KeyStoreInfo();
    keyStoreInfo.setPublicKey(publicKey);
    keyStoreInfo.setAddress(address);
    keyStoreInfo.setPrivateKey(privateKey);
    keyStoreInfo.setUserName(userName);
    return keyStoreInfo;
}
 
Example 4
Source File: BcosApp.java    From evidenceSample with Apache License 2.0 5 votes vote down vote up
public String verifySignedMessage(String message, String signatureData) throws SignatureException {
    Sign.SignatureData signatureData1 = Tools.stringToSignatureData(signatureData);
    try {
        return "0x" + Keys.getAddress(Sign.signedMessageToKey(message.getBytes(), signatureData1));
    } catch (SignatureException e) {
        throw e;
    }
}
 
Example 5
Source File: CertTools.java    From WeBASE-Node-Manager with Apache License 2.0 4 votes vote down vote up
public static String getAddress(PublicKey key) {
    String publicKey = getPublicKeyString(key);
    return Keys.getAddress(publicKey);
}
 
Example 6
Source File: Web3Tools.java    From WeBASE-Node-Manager with Apache License 2.0 2 votes vote down vote up
/**
 * get address from public key
 * 2019/11/27 support guomi
 * @param publicKey
 * @return
 */
public static String getAddressByPublicKey(String publicKey) {
    String address = "0x" + Keys.getAddress(publicKey);
    return address;
}