|
21 | 21 | public class BTCKeyPair extends ECDSAKeyPair { |
22 | 22 | private ECKey keypair; |
23 | 23 |
|
24 | | - public BTCKeyPair() { |
| 24 | + private BTCKeyPair() { |
25 | 25 | String _priv = createPrivateKey(); |
26 | 26 | if (_priv != null) { |
27 | 27 | this.priv = _priv; |
28 | 28 | } |
29 | 29 | createPublicKey(); |
30 | 30 | } |
31 | 31 |
|
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."); |
53 | 53 | } |
54 | 54 | createPublicKey(); |
55 | 55 | } |
56 | 56 |
|
| 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 | + |
57 | 74 | private String createPrivateKey() { |
58 | 75 | try { |
59 | 76 | ECKey keypair = new ECKey(); |
|
0 commit comments