Skip to content

Commit f77896d

Browse files
committed
Blocks & Utilities
- Added ability to construct Transactions from Streams - Added streaming constructor to VarInt - Added functionality to Block. It used to be mostly a stub. - Minor documentation fixes
1 parent dafeb07 commit f77896d

20 files changed

+407
-14
lines changed

build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ javadoc {
2121
}
2222

2323
java {
24-
sourceCompatibility = JavaVersion.toVersion("1.8")
2524
withJavadocJar()
2625
withSourcesJar()
2726
}
@@ -109,3 +108,5 @@ javadoc {
109108
}
110109

111110

111+
sourceCompatibility = JavaVersion.VERSION_11
112+
targetCompatibility = JavaVersion.VERSION_11

src/main/java/org/twostack/bitcoin4j/PrivateKey.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,18 @@
2222
import org.twostack.bitcoin4j.transaction.ReadUtils;
2323

2424
import java.math.BigInteger;
25+
import java.util.Arrays;
2526

2627
public class PrivateKey {
2728

2829
ECKey key;
2930
boolean _hasCompressedPubKey;
3031
NetworkType _networkType;
3132

33+
public PrivateKey() {
34+
this(new ECKey(), true, NetworkType.MAIN);
35+
}
36+
3237
public PrivateKey(ECKey key){
3338
this(key, true, NetworkType.MAIN);
3439
}
@@ -44,6 +49,9 @@ public byte[] sign(byte[] buffer){
4449
return sig.encodeToDER();
4550
}
4651

52+
public String toWIF(){
53+
return key.getPrivateKeyAsWiF(_networkType);
54+
}
4755

4856
//FIXME: We can use DumpedPrivateKey to replace the internals here
4957
public static PrivateKey fromWIF(String wif) throws InvalidKeyException {
@@ -127,7 +135,18 @@ private static NetworkType decodeNetworkType(String wifKey) throws InvalidKeyExc
127135
}
128136
}
129137

138+
/**
139+
* @return the PublicKey corresponding to this PrivateKey
140+
*/
130141
public PublicKey getPublicKey() {
131142
return PublicKey.fromHex(Utils.HEX.encode(key.getPubKey()));
132143
}
144+
145+
146+
/**
147+
* @return the ECKey backing this private key.
148+
*/
149+
public ECKey getKey() {
150+
return this.key;
151+
}
133152
}

src/main/java/org/twostack/bitcoin4j/VarInt.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import com.google.common.primitives.Ints;
2121

2222
import java.io.ByteArrayInputStream;
23+
import java.io.IOException;
24+
import java.io.InputStream;
2325

2426
/**
2527
* A variable-length encoded unsigned integer using Satoshi's encoding (a.k.a. "CompactSize").
@@ -63,7 +65,7 @@ public VarInt(byte[] buf, int offset) {
6365
}
6466
}
6567

66-
public static VarInt fromStream(ByteArrayInputStream stream) {
68+
public static VarInt fromStream(InputStream stream) throws IOException {
6769
int first = 0xFF & stream.read();
6870
long value;
6971
if (first < 253) {

0 commit comments

Comments
 (0)