Skip to content

Commit 40efc3e

Browse files
committed
add BIP69InputComparatorGeneric + BIP69OutputComparatorGeneric
1 parent 704a8f0 commit 40efc3e

File tree

4 files changed

+128
-93
lines changed

4 files changed

+128
-93
lines changed

java/com/samourai/wallet/bip69/BIP69InputComparator.java

Lines changed: 7 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2,54 +2,14 @@
22

33
import org.bitcoinj.core.TransactionInput;
44

5-
import java.util.Comparator;
6-
7-
public class BIP69InputComparator implements Comparator<TransactionInput> {
8-
9-
public int compare(TransactionInput i1, TransactionInput i2) {
10-
byte[] h1 = i1.getOutpoint().getHash().getBytes();
11-
byte[] h2 = i2.getOutpoint().getHash().getBytes();
12-
13-
long index1 = i1.getOutpoint().getIndex();
14-
long index2 = i2.getOutpoint().getIndex();
15-
16-
return compare(h1, h2, index1, index2);
5+
public class BIP69InputComparator extends BIP69InputComparatorGeneric<TransactionInput> {
6+
@Override
7+
protected byte[] getHash(TransactionInput i) {
8+
return i.getOutpoint().getHash().getBytes();
179
}
1810

19-
public int compare(byte[] h1, byte[] h2, long index1, long index2) {
20-
21-
final int BEFORE = -1;
22-
final int EQUAL = 0;
23-
final int AFTER = 1;
24-
25-
int pos = 0;
26-
while(pos < h1.length && pos < h2.length) {
27-
28-
byte b1 = h1[pos];
29-
byte b2 = h2[pos];
30-
31-
if((b1 & 0xff) < (b2 & 0xff)) {
32-
return BEFORE;
33-
}
34-
else if((b1 & 0xff) > (b2 & 0xff)) {
35-
return AFTER;
36-
}
37-
else {
38-
pos++;
39-
}
40-
41-
}
42-
43-
if(index1 < index2) {
44-
return BEFORE;
45-
}
46-
else if(index1 > index2) {
47-
return AFTER;
48-
}
49-
else {
50-
return EQUAL;
51-
}
52-
11+
@Override
12+
protected long getIndex(TransactionInput i) {
13+
return i.getOutpoint().getIndex();
5314
}
54-
5515
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.samourai.wallet.bip69;
2+
3+
import java.util.Comparator;
4+
5+
public abstract class BIP69InputComparatorGeneric<T> implements Comparator<T> {
6+
protected abstract byte[] getHash(T i);
7+
protected abstract long getIndex(T i);
8+
9+
public int compare(T i1, T i2) {
10+
byte[] h1 = getHash(i1);
11+
byte[] h2 = getHash(i2);
12+
13+
long index1 = getIndex(i1);
14+
long index2 = getIndex(i2);
15+
16+
return compare(h1, h2, index1, index2);
17+
}
18+
19+
public int compare(byte[] h1, byte[] h2, long index1, long index2) {
20+
21+
final int BEFORE = -1;
22+
final int EQUAL = 0;
23+
final int AFTER = 1;
24+
25+
int pos = 0;
26+
while(pos < h1.length && pos < h2.length) {
27+
28+
byte b1 = h1[pos];
29+
byte b2 = h2[pos];
30+
31+
if((b1 & 0xff) < (b2 & 0xff)) {
32+
return BEFORE;
33+
}
34+
else if((b1 & 0xff) > (b2 & 0xff)) {
35+
return AFTER;
36+
}
37+
else {
38+
pos++;
39+
}
40+
41+
}
42+
43+
if(index1 < index2) {
44+
return BEFORE;
45+
}
46+
else if(index1 > index2) {
47+
return AFTER;
48+
}
49+
else {
50+
return EQUAL;
51+
}
52+
53+
}
54+
55+
}

java/com/samourai/wallet/bip69/BIP69OutputComparator.java

Lines changed: 8 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,52 +2,14 @@
22

33
import org.bitcoinj.core.TransactionOutput;
44

5-
import java.util.Comparator;
6-
7-
public class BIP69OutputComparator implements Comparator<TransactionOutput> {
8-
9-
public int compare(TransactionOutput o1, TransactionOutput o2) {
10-
11-
final int BEFORE = -1;
12-
final int EQUAL = 0;
13-
final int AFTER = 1;
14-
15-
if(o1.getValue().compareTo(o2.getValue()) > 0) {
16-
return AFTER;
17-
}
18-
else if(o1.getValue().compareTo(o2.getValue()) < 0) {
19-
return BEFORE;
20-
}
21-
else {
22-
23-
byte[] b1 = o1.getScriptBytes();
24-
byte[] b2 = o2.getScriptBytes();
25-
26-
int pos = 0;
27-
while(pos < b1.length && pos < b2.length) {
28-
29-
if((b1[pos] & 0xff) < (b2[pos] & 0xff)) {
30-
return BEFORE;
31-
}
32-
else if((b1[pos] & 0xff) > (b2[pos] & 0xff)) {
33-
return AFTER;
34-
}
35-
36-
pos++;
37-
}
38-
39-
if(b1.length < b2.length) {
40-
return BEFORE;
41-
}
42-
else if(b1.length > b2.length) {
43-
return AFTER;
44-
}
45-
else {
46-
return EQUAL;
47-
}
48-
49-
}
50-
5+
public class BIP69OutputComparator extends BIP69OutputComparatorGeneric<TransactionOutput> {
6+
@Override
7+
protected long getValue(TransactionOutput i) {
8+
return i.getValue().getValue();
519
}
5210

11+
@Override
12+
protected byte[] getScriptBytes(TransactionOutput i) {
13+
return i.getScriptBytes();
14+
}
5315
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.samourai.wallet.bip69;
2+
3+
import com.google.common.primitives.Longs;
4+
5+
import java.util.Comparator;
6+
7+
public abstract class BIP69OutputComparatorGeneric<T> implements Comparator<T> {
8+
protected abstract byte[] getScriptBytes(T i);
9+
protected abstract long getValue(T i);
10+
11+
public int compare(T o1, T o2) {
12+
13+
final int BEFORE = -1;
14+
final int EQUAL = 0;
15+
final int AFTER = 1;
16+
17+
long o1Value = getValue(o1);
18+
long o2Value = getValue(o2);
19+
20+
if(Longs.compare(o1Value, o2Value) > 0) {
21+
return AFTER;
22+
}
23+
else if(Longs.compare(o1Value, o2Value) < 0) {
24+
return BEFORE;
25+
}
26+
else {
27+
28+
byte[] b1 = getScriptBytes(o1);
29+
byte[] b2 = getScriptBytes(o2);
30+
31+
int pos = 0;
32+
while(pos < b1.length && pos < b2.length) {
33+
34+
if((b1[pos] & 0xff) < (b2[pos] & 0xff)) {
35+
return BEFORE;
36+
}
37+
else if((b1[pos] & 0xff) > (b2[pos] & 0xff)) {
38+
return AFTER;
39+
}
40+
41+
pos++;
42+
}
43+
44+
if(b1.length < b2.length) {
45+
return BEFORE;
46+
}
47+
else if(b1.length > b2.length) {
48+
return AFTER;
49+
}
50+
else {
51+
return EQUAL;
52+
}
53+
54+
}
55+
56+
}
57+
58+
}

0 commit comments

Comments
 (0)