Skip to content

Commit

Permalink
add constants, minor refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelliao committed Sep 23, 2017
1 parent a3f5673 commit 7e25ef4
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ public final class BitcoinConstants {
*/
public static final long NETWORK_SERVICES = 1L;

/**
* Legal base58 characters.
*/
public static final String BASE58_CHARS = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";

/**
* Network ID: 0x00 = main network.
*/
Expand All @@ -50,6 +55,12 @@ public final class BitcoinConstants {
public static final byte PRIVATE_KEY_SUFFIX = 0x01;
public static final byte[] PRIVATE_KEY_SUFFIX_ARRAY = { PRIVATE_KEY_SUFFIX };

public static final int MAINNET_BIP32_PUBLIC = 0x0488b21e;
public static final int MAINNET_BIP32_PRIVATE = 0x0488ade4;

public static final int TESTNET_BIP32_PUBLIC = 0x043587cf;
public static final int TESTNET_BIP32_PRIVATE = 0x04358394;

/**
* Minimum value of private key.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.util.Scanner;

import com.itranswarp.bitcoin.constant.BitcoinConstants;

/**
* Generate pretty address starts with specific word. e.g. "BTC".
*
Expand All @@ -14,14 +16,25 @@
*/
public class PrettyAddressGenerator {

static final long NUM_OF_ADDR = 10;
static final long NUM_OF_ADDR = 3;

public static void main(String[] args) {
try (Scanner scanner = new Scanner(System.in)) {
System.out.print("Enter the prefix (e.g. BTC): ");
String prefix = "1" + scanner.nextLine();
String input = scanner.nextLine();
if (input.length() == 0) {
System.err.println("No prefix specified.");
System.exit(1);
}
for (int i = 0; i < input.length(); i++) {
if (BitcoinConstants.BASE58_CHARS.indexOf(input.charAt(i)) == (-1)) {
System.err.println("Invalid char: " + input.charAt(i));
System.exit(1);
}
}
String prefix = "1" + input;
System.out.println("Find address starts with prefix: " + prefix);
final long step = 1 << Math.min(60, prefix.length() * 11);
final long step = (long) Math.pow(58, input.length() - 1);
long n = 0;
long found = 0;
for (;;) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
import java.math.BigInteger;
import java.util.Arrays;

import com.itranswarp.bitcoin.constant.BitcoinConstants;

public class Base58Utils {

static final char[] ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz".toCharArray();
static final char[] ALPHABET = BitcoinConstants.BASE58_CHARS.toCharArray();
static final char ENCODED_ZERO = ALPHABET[0];
static final int[] INDEXES = new int[128];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,28 @@ public static byte[] reverse(byte[] msgHash) {
return Arrays.reverse(msgHash);
}

/**
* Convert int to 4 bytes.
*
* @param value
* Int value.
* @return 4 bytes.
*/
public static byte[] intToByteArray(int value) {
return new byte[] { (byte) (value >> 24), (byte) (value >> 16), (byte) (value >> 8), (byte) value };
}

/**
* Convert 4 bytes to int.
*
* @param bs
* Byte array with length of 4.
* @return int value.
*/
public static int bytesToInt(byte[] bs) {
if (bs == null || bs.length != 4) {
throw new IllegalArgumentException("Invalid bytes.");
}
return bs[0] << 24 | (bs[1] & 0xff) << 16 | (bs[2] & 0xff) << 8 | (bs[3] & 0xff);
}
}

0 comments on commit 7e25ef4

Please sign in to comment.