Skip to content

Commit dbc61e6

Browse files
author
Nick A
committed
Minor bug fix for Transaction ID (bytes weren't reversed) and a new test to make sure the Transaction ID corresponds to its hash
1 parent e2abb79 commit dbc61e6

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

src/main/java/org/twostack/bitcoin4j/transaction/Transaction.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030

3131
public class Transaction {
3232

33-
3433
private long version = 1;
3534
private long nLockTime = 0;
3635
private ArrayList<TransactionInput> inputs = new ArrayList<>();
@@ -40,7 +39,7 @@ public class Transaction {
4039
public static final long NLOCKTIME_MAX_VALUE = 4294967295L;
4140

4241
public static final long MAX_COINS = 21000000;
43-
/// max amount of satoshis in circulation
42+
/// max amount of bitcoins in circulation
4443

4544
private static final int SMALLEST_UNIT_EXPONENT = 8;
4645
private static final long COIN_VALUE = LongMath.pow(10, SMALLEST_UNIT_EXPONENT);
@@ -247,11 +246,11 @@ public List<TransactionOutput> getOutputs() {
247246
}
248247

249248
public String getTransactionId(){
250-
if (txHash == null){
249+
byte[] idBytes = getTransactionIdBytes();
250+
if (idBytes.length == 0) {
251251
return "";
252252
}
253-
254-
return Utils.HEX.encode(txHash.array());
253+
return Utils.HEX.encode(idBytes);
255254
}
256255

257256
public byte[] getTransactionIdBytes(){

src/test/java/org/twostack/bitcoin4j/transaction/TransactionTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,12 @@ public void TestSerializeDeserialize() throws IOException {
6767
assertEquals(tx1hex, HEX.encode(transaction.serialize()));
6868
}
6969

70+
@Test
71+
public void TestTransactionIdMatchesTransactionHash() {
72+
Transaction transaction = Transaction.fromHex(tx1hex);
73+
assertEquals(tx1id, transaction.getTransactionId());
74+
}
75+
7076
@Test
7177
public void can_create_and_sign_transaction() throws InvalidKeyException, TransactionException, IOException, SigHashException {
7278

0 commit comments

Comments
 (0)