Skip to content

Commit

Permalink
removed netparams from method signatures, made it global
Browse files Browse the repository at this point in the history
  • Loading branch information
dbasch committed Jan 24, 2014
1 parent 99409ed commit cbf9838
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 34 deletions.
43 changes: 10 additions & 33 deletions src/main/java/com/fruitcat/bitcoin/BIP38.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,18 @@

public class BIP38 {

private static NetworkParameters params = MainNetParams.get();

static {
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
}

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

public static void setNetParams(NetworkParameters p) {
params = p;
}

/**
* Generates an encrypted key with EC multiplication.
* Only uncompressed format for now.
Expand Down Expand Up @@ -81,7 +87,7 @@ public static GeneratedKey encryptedKeyFromIntermediate(byte[] intermediate) thr
ECPoint p = CURVE.getCurve().decodePoint(passPoint);
ECPoint pk = p.multiply(new BigInteger(1, factorB));
byte[] generatedAddress = Utils.sha256ripe160(pk.getEncoded());
String addStr = new Address(MainNetParams.get(), generatedAddress).toString();
String addStr = new Address(params, generatedAddress).toString();
byte[] add = addStr.getBytes();
byte[] addressHash = Arrays.copyOfRange(Utils.doubleHash(add, 0, add.length), 0, 4);

Expand Down Expand Up @@ -155,9 +161,9 @@ private static String confirm(byte flagByte, byte[] addressHash, byte[] ownerEnt
*/
public static boolean verify(String passphrase, GeneratedKey generatedKey)
throws AddressFormatException, UnsupportedEncodingException, GeneralSecurityException {
DumpedPrivateKey dk = new DumpedPrivateKey(MainNetParams.get(), decrypt(passphrase, generatedKey.key));
DumpedPrivateKey dk = new DumpedPrivateKey(params, decrypt(passphrase, generatedKey.key));
ECKey key = dk.getKey();
String address = key.toAddress(MainNetParams.get()).toString();
String address = key.toAddress(params).toString();

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

ECKey privKey = new ECKey(pk, null);
return privKey.getPrivateKeyEncoded(MainNetParams.get()).toString();
return privKey.getPrivateKeyEncoded(params).toString();
}

/**
Expand All @@ -310,22 +316,6 @@ public static String decryptEC(String passphrase, byte[] encryptedKey) throws Un
*/
public static String encryptNoEC(String passphrase, String encodedPrivateKey, boolean isCompressed)
throws GeneralSecurityException, UnsupportedEncodingException, AddressFormatException {
return encryptNoEC(passphrase, encodedPrivateKey, isCompressed, MainNetParams.get());
}

/**
* Encrypts a key without using EC multiplication.
* @param encodedPrivateKey
* @param passphrase
* @param isCompressed
* @param params
* @return
* @throws GeneralSecurityException
* @throws UnsupportedEncodingException
* @throws AddressFormatException
*/
public static String encryptNoEC(String passphrase, String encodedPrivateKey, boolean isCompressed, NetworkParameters params)
throws GeneralSecurityException, UnsupportedEncodingException, AddressFormatException {

DumpedPrivateKey dk = new DumpedPrivateKey(params, encodedPrivateKey);

Expand Down Expand Up @@ -365,19 +355,6 @@ public static String encryptNoEC(String passphrase, String encodedPrivateKey, bo
* @throws GeneralSecurityException
*/
public static String decryptNoEC(String passphrase, byte[] encryptedKey) throws UnsupportedEncodingException, GeneralSecurityException {
return decryptNoEC(passphrase, encryptedKey, MainNetParams.get());
}

/**
* Decrypts a key that was encrypted without EC multiplication.
* @param passphrase
* @param encryptedKey
* @param params
* @return the key, Base58-encoded
* @throws UnsupportedEncodingException
* @throws GeneralSecurityException
*/
public static String decryptNoEC(String passphrase, byte[] encryptedKey, NetworkParameters params) throws UnsupportedEncodingException, GeneralSecurityException {

byte[] addressHash = Arrays.copyOfRange(encryptedKey, 3, 7);
byte[] scryptKey = SCrypt.scrypt(passphrase.getBytes("UTF8"), addressHash, 16384, 8, 8, 64);
Expand Down
4 changes: 3 additions & 1 deletion src/test/java/com/fruitcat/bitcoin/BIP38Test.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.fruitcat.bitcoin;

import com.google.bitcoin.core.Base58;
import com.google.bitcoin.params.MainNetParams;
import org.testng.annotations.Test;
import static org.testng.Assert.assertEquals;

import java.util.Arrays;
import java.util.Random;

import static org.testng.Assert.assertEquals;

/**
* Unit tests
*
Expand Down

0 comments on commit cbf9838

Please sign in to comment.