39
39
40
40
public class BIP38 {
41
41
42
+ private static NetworkParameters params = MainNetParams .get ();
43
+
42
44
static {
43
45
Security .addProvider (new org .bouncycastle .jce .provider .BouncyCastleProvider ());
44
46
}
45
47
46
48
private static final X9ECParameters CURVE = SECNamedCurves .getByName ("secp256k1" );
47
49
50
+ public static void setNetParams (NetworkParameters p ) {
51
+ params = p ;
52
+ }
53
+
48
54
/**
49
55
* Generates an encrypted key with EC multiplication.
50
56
* Only uncompressed format for now.
@@ -81,7 +87,7 @@ public static GeneratedKey encryptedKeyFromIntermediate(byte[] intermediate) thr
81
87
ECPoint p = CURVE .getCurve ().decodePoint (passPoint );
82
88
ECPoint pk = p .multiply (new BigInteger (1 , factorB ));
83
89
byte [] generatedAddress = Utils .sha256ripe160 (pk .getEncoded ());
84
- String addStr = new Address (MainNetParams . get () , generatedAddress ).toString ();
90
+ String addStr = new Address (params , generatedAddress ).toString ();
85
91
byte [] add = addStr .getBytes ();
86
92
byte [] addressHash = Arrays .copyOfRange (Utils .doubleHash (add , 0 , add .length ), 0 , 4 );
87
93
@@ -155,9 +161,9 @@ private static String confirm(byte flagByte, byte[] addressHash, byte[] ownerEnt
155
161
*/
156
162
public static boolean verify (String passphrase , GeneratedKey generatedKey )
157
163
throws AddressFormatException , UnsupportedEncodingException , GeneralSecurityException {
158
- DumpedPrivateKey dk = new DumpedPrivateKey (MainNetParams . get () , decrypt (passphrase , generatedKey .key ));
164
+ DumpedPrivateKey dk = new DumpedPrivateKey (params , decrypt (passphrase , generatedKey .key ));
159
165
ECKey key = dk .getKey ();
160
- String address = key .toAddress (MainNetParams . get () ).toString ();
166
+ String address = key .toAddress (params ).toString ();
161
167
162
168
return address .equals (generatedKey .address );
163
169
}
@@ -295,7 +301,7 @@ public static String decryptEC(String passphrase, byte[] encryptedKey) throws Un
295
301
BigInteger pk = new BigInteger (1 , passFactor ).multiply (new BigInteger (1 , factorB )).remainder (n );
296
302
297
303
ECKey privKey = new ECKey (pk , null );
298
- return privKey .getPrivateKeyEncoded (MainNetParams . get () ).toString ();
304
+ return privKey .getPrivateKeyEncoded (params ).toString ();
299
305
}
300
306
301
307
/**
@@ -310,22 +316,6 @@ public static String decryptEC(String passphrase, byte[] encryptedKey) throws Un
310
316
*/
311
317
public static String encryptNoEC (String passphrase , String encodedPrivateKey , boolean isCompressed )
312
318
throws GeneralSecurityException , UnsupportedEncodingException , AddressFormatException {
313
- return encryptNoEC (passphrase , encodedPrivateKey , isCompressed , MainNetParams .get ());
314
- }
315
-
316
- /**
317
- * Encrypts a key without using EC multiplication.
318
- * @param encodedPrivateKey
319
- * @param passphrase
320
- * @param isCompressed
321
- * @param params
322
- * @return
323
- * @throws GeneralSecurityException
324
- * @throws UnsupportedEncodingException
325
- * @throws AddressFormatException
326
- */
327
- public static String encryptNoEC (String passphrase , String encodedPrivateKey , boolean isCompressed , NetworkParameters params )
328
- throws GeneralSecurityException , UnsupportedEncodingException , AddressFormatException {
329
319
330
320
DumpedPrivateKey dk = new DumpedPrivateKey (params , encodedPrivateKey );
331
321
@@ -365,19 +355,6 @@ public static String encryptNoEC(String passphrase, String encodedPrivateKey, bo
365
355
* @throws GeneralSecurityException
366
356
*/
367
357
public static String decryptNoEC (String passphrase , byte [] encryptedKey ) throws UnsupportedEncodingException , GeneralSecurityException {
368
- return decryptNoEC (passphrase , encryptedKey , MainNetParams .get ());
369
- }
370
-
371
- /**
372
- * Decrypts a key that was encrypted without EC multiplication.
373
- * @param passphrase
374
- * @param encryptedKey
375
- * @param params
376
- * @return the key, Base58-encoded
377
- * @throws UnsupportedEncodingException
378
- * @throws GeneralSecurityException
379
- */
380
- public static String decryptNoEC (String passphrase , byte [] encryptedKey , NetworkParameters params ) throws UnsupportedEncodingException , GeneralSecurityException {
381
358
382
359
byte [] addressHash = Arrays .copyOfRange (encryptedKey , 3 , 7 );
383
360
byte [] scryptKey = SCrypt .scrypt (passphrase .getBytes ("UTF8" ), addressHash , 16384 , 8 , 8 , 64 );
0 commit comments