Skip to content

Commit

Permalink
Merge pull request #90 from andyYuanFZM/master
Browse files Browse the repository at this point in the history
增加以0x开头的YCC地址格式
  • Loading branch information
andyYuanFZM authored May 31, 2022
2 parents 4019fa2 + 8faddcb commit 42db337
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 3 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@
<artifactId>bitcoinj-core</artifactId>
<version>0.14.7</version>
</dependency>

<dependency>
<groupId>org.web3j</groupId>
<artifactId>core</artifactId>
<version>5.0.0</version>
</dependency>

<dependency>
<groupId>org.apache.httpcomponents</groupId>
Expand Down
21 changes: 20 additions & 1 deletion src/main/java/cn/chain33/javasdk/client/Account.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
import cn.chain33.javasdk.utils.TransactionUtil;

import java.io.*;
import java.math.BigInteger;

public class Account {

/**
*
* @description 在本地创建账户信息
* @return 账户信息(私钥,公钥,地址)
*
Expand All @@ -32,6 +32,25 @@ public AccountInfo newAccountLocal() {
accountInfo.setAddress(TransactionUtil.genAddress(publicKeyByte));
return accountInfo;
}

/**
* @description 在本地创建账户信息(以太坊形式,地址格式以0x开头)
* @return 账户信息(私钥,公钥,地址)
*
*/
public AccountInfo newAccountLocalYCC() {
AccountInfo accountInfo = new AccountInfo();

// 生成私钥匙
String privateKey = TransactionUtil.generatorPrivateKeyString();
accountInfo.setPrivateKey(privateKey);
// 生成公钥匙
BigInteger publicKey = TransactionUtil.getHexPubKeyFromPrivKeyForYCC(privateKey);
accountInfo.setPublicKey(publicKey.toString(16));
// 生成地址
accountInfo.setAddress(TransactionUtil.genAddressForYCC(publicKey));
return accountInfo;
}

/**
* 本地创建账户信息,加密输出到指定路径
Expand Down
27 changes: 26 additions & 1 deletion src/main/java/cn/chain33/javasdk/utils/TransactionUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.SecureRandom;
import java.util.List;
import java.util.Random;

import org.bitcoinj.core.ECKey;
import org.bitcoinj.core.Sha256Hash;
import org.web3j.crypto.ECKeyPair;
import org.web3j.crypto.Keys;
import org.bouncycastle.asn1.sec.SECNamedCurves;
import org.bouncycastle.asn1.x9.X9ECParameters;
import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
Expand Down Expand Up @@ -120,7 +123,6 @@ public static byte[] byteMerger(byte[] byte_1, byte[] byte_2) {
}

/**
*
* @description 通过公钥生成地址
* @param pubKey
* 公钥
Expand All @@ -133,6 +135,17 @@ public static String genAddress(byte[] pubKey) {
address.setHash160(ripemd160);
return addressToString(address);
}

/**
* @description 通过公钥生成YCC格式地址(以太坊形式,以0x开头的地址)
* @param pubKey 公钥
* @return 地址
*/
public static String genAddressForYCC(BigInteger pubKey) {
//通过公钥生成钱包地址
String address = Keys.getAddress(pubKey);
return "0x" + address;
}

/**
* 将evm地址转成base58编码地址
Expand Down Expand Up @@ -568,6 +581,18 @@ public static String getHexPubKeyFromPrivKey(String privateKey) {
String pubKeyStr = HexUtil.toHexString(pubKey);
return pubKeyStr;
}


/**
* 通过私钥生成YCC格式公钥
* @param privateKey
* @return
*/
public static BigInteger getHexPubKeyFromPrivKeyForYCC(String privateKey) {
ECKeyPair keyPair = ECKeyPair.create(HexUtil.fromHexString(privateKey));
BigInteger pubKeyStr = keyPair.getPublicKey();
return pubKeyStr;
}

/**
* 构造交易
Expand Down
13 changes: 12 additions & 1 deletion src/test/java/cn/chain33/javasdk/model/AccountTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ public class AccountTest {
Account account = new Account();

/**
*
* @description 直接创建账户 (私钥,公钥,地址)
*
*/
Expand All @@ -41,6 +40,18 @@ public void createAccountLocal() {
System.out.println("Address is:" + accountInfo.getAddress());
}

/**
* @description 直接创建YCC账户 (私钥,公钥,地址),地址格式以0x开头,以太坊的形式
*
*/
@Test
public void createAccountLocalForYCC() {
AccountInfo accountInfo = account.newAccountLocalYCC();
System.out.println("privateKey is:" + accountInfo.getPrivateKey());
System.out.println("publicKey is:" + accountInfo.getPublicKey());
System.out.println("Address is:" + accountInfo.getAddress());
}

/**
* 根据seed创建私钥,公钥,地址
* @throws UnreadableWalletException
Expand Down

0 comments on commit 42db337

Please sign in to comment.