Skip to content

Commit 0a50a51

Browse files
committed
Update class constructors
1 parent 64432d3 commit 0a50a51

4 files changed

Lines changed: 54 additions & 25 deletions

File tree

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,11 @@ And this project includes below external libraries.
3535
* bitcoinj core v0.15.10
3636
* web3j core v4.8.7
3737

38-
Download and use 'ecdsa-keygen-java-1.1' in 'release' tab or [here](release/).
38+
Download and use 'ecdsa-keygen-java-1.3.jar' in 'release' tab or [here](release/).
39+
40+
```sh
41+
implementation files('./ecdsa-keygen-java-1.3.jar')
42+
```
3943

4044
## BTCKeyPair
4145

lib/src/main/java/com/wuin/ecdsakeyj/BTCKeyPair.java

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,39 +21,56 @@
2121
public class BTCKeyPair extends ECDSAKeyPair {
2222
private ECKey keypair;
2323

24-
public BTCKeyPair() {
24+
private BTCKeyPair() {
2525
String _priv = createPrivateKey();
2626
if (_priv != null) {
2727
this.priv = _priv;
2828
}
2929
createPublicKey();
3030
}
3131

32-
public BTCKeyPair(String priv, Boolean useSeed) {
33-
if (useSeed) {
34-
byte[] sh = Util.sha3(priv);
35-
byte[] shb = Base58.encode(sh).getBytes();
36-
shb = Arrays.copyOfRange(shb, 0, shb.length - 4);
37-
38-
BigInteger k = new BigInteger(shb);
39-
BigInteger n = new BigInteger("115792089237316195423570985008687907852837564279074904382605163141518161494336");
40-
41-
k = k.mod(n);
42-
k = k.add(new BigInteger("1"));
43-
String key = Util.bytesToHexString(k.toByteArray());
44-
45-
try {
46-
String _priv = encodeKey(key);
47-
this.priv = _priv;
48-
} catch (Exception e) {
49-
Util.raiseError("Fail to generate BTC Keypair from seed.");
50-
}
51-
} else {
52-
this.priv = priv;
32+
private BTCKeyPair(String priv) {
33+
this.priv = priv;
34+
createPublicKey();
35+
}
36+
37+
private BTCKeyPair(byte[] seed) {
38+
byte[] shb = Base58.encode(seed).getBytes();
39+
shb = Arrays.copyOfRange(shb, 0, shb.length - 4);
40+
41+
BigInteger k = new BigInteger(shb);
42+
BigInteger n = new BigInteger("115792089237316195423570985008687907852837564279074904382605163141518161494336");
43+
44+
k = k.mod(n);
45+
k = k.add(new BigInteger("1"));
46+
String key = Util.bytesToHexString(k.toByteArray());
47+
48+
try {
49+
String _priv = encodeKey(key);
50+
this.priv = _priv;
51+
} catch (Exception e) {
52+
Util.raiseError("Fail to generate BTC Keypair from seed.");
5353
}
5454
createPublicKey();
5555
}
5656

57+
public static BTCKeyPair create() {
58+
return new BTCKeyPair();
59+
}
60+
61+
public static BTCKeyPair fromPrivateKey(String priv) {
62+
return new BTCKeyPair(priv);
63+
}
64+
65+
public static BTCKeyPair fromSeed(String seed) {
66+
byte[] sh = Util.sha3(seed);
67+
return new BTCKeyPair(sh);
68+
}
69+
70+
public static BTCKeyPair fromSeed(byte[] seed) {
71+
return new BTCKeyPair(seed);
72+
}
73+
5774
private String createPrivateKey() {
5875
try {
5976
ECKey keypair = new ECKey();

lib/src/main/java/com/wuin/ecdsakeyj/ETHKeyPair.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,27 @@
2222
public class ETHKeyPair extends ECDSAKeyPair {
2323
private ECKeyPair keypair;
2424

25-
public ETHKeyPair() {
25+
private ETHKeyPair() {
2626
String _priv = createPrivateKey();
2727
if(_priv != null) {
2828
this.priv = _priv;
2929
}
3030
createPublicKey();
3131
}
3232

33-
public ETHKeyPair(String priv) {
33+
private ETHKeyPair(String priv) {
3434
super(priv);
3535
createPublicKey();
3636
}
3737

38+
public static ETHKeyPair create() {
39+
return new ETHKeyPair();
40+
}
41+
42+
public static ETHKeyPair fromPrivateKey(String priv) {
43+
return new ETHKeyPair(priv);
44+
}
45+
3846
private String createPrivateKey() {
3947
try {
4048
return new ECKey().getPrivateKeyAsHex();

release/ecdsa-keygen-java-1.3.jar

23.3 MB
Binary file not shown.

0 commit comments

Comments
 (0)