Skip to content

Commit cbf9838

Browse files
committed
removed netparams from method signatures, made it global
1 parent 99409ed commit cbf9838

File tree

2 files changed

+13
-34
lines changed

2 files changed

+13
-34
lines changed

src/main/java/com/fruitcat/bitcoin/BIP38.java

Lines changed: 10 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,18 @@
3939

4040
public class BIP38 {
4141

42+
private static NetworkParameters params = MainNetParams.get();
43+
4244
static {
4345
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
4446
}
4547

4648
private static final X9ECParameters CURVE = SECNamedCurves.getByName("secp256k1");
4749

50+
public static void setNetParams(NetworkParameters p) {
51+
params = p;
52+
}
53+
4854
/**
4955
* Generates an encrypted key with EC multiplication.
5056
* Only uncompressed format for now.
@@ -81,7 +87,7 @@ public static GeneratedKey encryptedKeyFromIntermediate(byte[] intermediate) thr
8187
ECPoint p = CURVE.getCurve().decodePoint(passPoint);
8288
ECPoint pk = p.multiply(new BigInteger(1, factorB));
8389
byte[] generatedAddress = Utils.sha256ripe160(pk.getEncoded());
84-
String addStr = new Address(MainNetParams.get(), generatedAddress).toString();
90+
String addStr = new Address(params, generatedAddress).toString();
8591
byte[] add = addStr.getBytes();
8692
byte[] addressHash = Arrays.copyOfRange(Utils.doubleHash(add, 0, add.length), 0, 4);
8793

@@ -155,9 +161,9 @@ private static String confirm(byte flagByte, byte[] addressHash, byte[] ownerEnt
155161
*/
156162
public static boolean verify(String passphrase, GeneratedKey generatedKey)
157163
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));
159165
ECKey key = dk.getKey();
160-
String address = key.toAddress(MainNetParams.get()).toString();
166+
String address = key.toAddress(params).toString();
161167

162168
return address.equals(generatedKey.address);
163169
}
@@ -295,7 +301,7 @@ public static String decryptEC(String passphrase, byte[] encryptedKey) throws Un
295301
BigInteger pk = new BigInteger(1, passFactor).multiply(new BigInteger(1, factorB)).remainder(n);
296302

297303
ECKey privKey = new ECKey(pk, null);
298-
return privKey.getPrivateKeyEncoded(MainNetParams.get()).toString();
304+
return privKey.getPrivateKeyEncoded(params).toString();
299305
}
300306

301307
/**
@@ -310,22 +316,6 @@ public static String decryptEC(String passphrase, byte[] encryptedKey) throws Un
310316
*/
311317
public static String encryptNoEC(String passphrase, String encodedPrivateKey, boolean isCompressed)
312318
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 {
329319

330320
DumpedPrivateKey dk = new DumpedPrivateKey(params, encodedPrivateKey);
331321

@@ -365,19 +355,6 @@ public static String encryptNoEC(String passphrase, String encodedPrivateKey, bo
365355
* @throws GeneralSecurityException
366356
*/
367357
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 {
381358

382359
byte[] addressHash = Arrays.copyOfRange(encryptedKey, 3, 7);
383360
byte[] scryptKey = SCrypt.scrypt(passphrase.getBytes("UTF8"), addressHash, 16384, 8, 8, 64);

src/test/java/com/fruitcat/bitcoin/BIP38Test.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package com.fruitcat.bitcoin;
22

33
import com.google.bitcoin.core.Base58;
4+
import com.google.bitcoin.params.MainNetParams;
45
import org.testng.annotations.Test;
5-
import static org.testng.Assert.assertEquals;
66

77
import java.util.Arrays;
88
import java.util.Random;
99

10+
import static org.testng.Assert.assertEquals;
11+
1012
/**
1113
* Unit tests
1214
*

0 commit comments

Comments
 (0)