Skip to content

Commit

Permalink
Update class constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
wyuinche committed Dec 23, 2021
1 parent 64432d3 commit 0a50a51
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 25 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ And this project includes below external libraries.
* bitcoinj core v0.15.10
* web3j core v4.8.7

Download and use 'ecdsa-keygen-java-1.1' in 'release' tab or [here](release/).
Download and use 'ecdsa-keygen-java-1.3.jar' in 'release' tab or [here](release/).

```sh
implementation files('./ecdsa-keygen-java-1.3.jar')
```

## BTCKeyPair

Expand Down
61 changes: 39 additions & 22 deletions lib/src/main/java/com/wuin/ecdsakeyj/BTCKeyPair.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,39 +21,56 @@
public class BTCKeyPair extends ECDSAKeyPair {
private ECKey keypair;

public BTCKeyPair() {
private BTCKeyPair() {
String _priv = createPrivateKey();
if (_priv != null) {
this.priv = _priv;
}
createPublicKey();
}

public BTCKeyPair(String priv, Boolean useSeed) {
if (useSeed) {
byte[] sh = Util.sha3(priv);
byte[] shb = Base58.encode(sh).getBytes();
shb = Arrays.copyOfRange(shb, 0, shb.length - 4);

BigInteger k = new BigInteger(shb);
BigInteger n = new BigInteger("115792089237316195423570985008687907852837564279074904382605163141518161494336");

k = k.mod(n);
k = k.add(new BigInteger("1"));
String key = Util.bytesToHexString(k.toByteArray());
try {
String _priv = encodeKey(key);
this.priv = _priv;
} catch (Exception e) {
Util.raiseError("Fail to generate BTC Keypair from seed.");
}
} else {
this.priv = priv;
private BTCKeyPair(String priv) {
this.priv = priv;
createPublicKey();
}

private BTCKeyPair(byte[] seed) {
byte[] shb = Base58.encode(seed).getBytes();
shb = Arrays.copyOfRange(shb, 0, shb.length - 4);

BigInteger k = new BigInteger(shb);
BigInteger n = new BigInteger("115792089237316195423570985008687907852837564279074904382605163141518161494336");

k = k.mod(n);
k = k.add(new BigInteger("1"));
String key = Util.bytesToHexString(k.toByteArray());

try {
String _priv = encodeKey(key);
this.priv = _priv;
} catch (Exception e) {
Util.raiseError("Fail to generate BTC Keypair from seed.");
}
createPublicKey();
}

public static BTCKeyPair create() {
return new BTCKeyPair();
}

public static BTCKeyPair fromPrivateKey(String priv) {
return new BTCKeyPair(priv);
}

public static BTCKeyPair fromSeed(String seed) {
byte[] sh = Util.sha3(seed);
return new BTCKeyPair(sh);
}

public static BTCKeyPair fromSeed(byte[] seed) {
return new BTCKeyPair(seed);
}

private String createPrivateKey() {
try {
ECKey keypair = new ECKey();
Expand Down
12 changes: 10 additions & 2 deletions lib/src/main/java/com/wuin/ecdsakeyj/ETHKeyPair.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,27 @@
public class ETHKeyPair extends ECDSAKeyPair {
private ECKeyPair keypair;

public ETHKeyPair() {
private ETHKeyPair() {
String _priv = createPrivateKey();
if(_priv != null) {
this.priv = _priv;
}
createPublicKey();
}

public ETHKeyPair(String priv) {
private ETHKeyPair(String priv) {
super(priv);
createPublicKey();
}

public static ETHKeyPair create() {
return new ETHKeyPair();
}

public static ETHKeyPair fromPrivateKey(String priv) {
return new ETHKeyPair(priv);
}

private String createPrivateKey() {
try {
return new ECKey().getPrivateKeyAsHex();
Expand Down
Binary file added release/ecdsa-keygen-java-1.3.jar
Binary file not shown.

0 comments on commit 0a50a51

Please sign in to comment.