Skip to content

Commit

Permalink
add alternate Record constructor;
Browse files Browse the repository at this point in the history
update test;
  • Loading branch information
esaulpaugh committed Dec 6, 2019
1 parent 39971da commit 859cba6
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
5 changes: 5 additions & 0 deletions src/main/java/com/esaulpaugh/headlong/rlp/eip778/Record.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.esaulpaugh.headlong.util.Strings;

import java.security.SignatureException;
import java.util.List;

import static com.esaulpaugh.headlong.rlp.RLPDecoder.RLP_STRICT;
import static com.esaulpaugh.headlong.util.Strings.BASE_64_URL_SAFE;
Expand All @@ -39,6 +40,10 @@ public final class Record {

private final RLPList rlp;

public Record(long seq, List<KeyValuePair> pairs, Signer signer) {
this(seq, pairs.toArray(KeyValuePair.EMPTY_ARRAY), signer);
}

public Record(long seq, KeyValuePair[] pairs, Signer signer) {
final int signatureLen = signer.signatureLength();
final int signatureItemLen = RLPEncoder.prefixLength(signatureLen) + signatureLen;
Expand Down
14 changes: 7 additions & 7 deletions src/test/java/com/esaulpaugh/headlong/rlp/eip778/EIP778Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@
import org.junit.jupiter.api.Test;

import java.security.SignatureException;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import static com.esaulpaugh.headlong.rlp.eip778.KeyValuePair.*;
import static com.esaulpaugh.headlong.util.Strings.HEX;
import static com.esaulpaugh.headlong.util.Strings.UTF_8;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class EIP778Test {
Expand Down Expand Up @@ -67,12 +68,12 @@ public byte[] sign(byte[] message, int off, int len) {

@Test
public void testEip778() throws DecodeException, SignatureException {
final KeyValuePair[] pairs = new KeyValuePair[] {
final List<KeyValuePair> pairs = Arrays.asList(
new KeyValuePair(IP, "7f000001", HEX),
new KeyValuePair(UDP, "765f", HEX),
new KeyValuePair(ID, "v4", UTF_8),
new KeyValuePair(SECP256K1, "03ca634cae0d49acb401d8a4c6b6fe8c55b70d115bf400769cc1400f3258cd3138", HEX)
};
);

Record record = new Record(1L, pairs, SIGNER);

Expand All @@ -92,13 +93,12 @@ public void testEip778() throws DecodeException, SignatureException {

assertEquals(1L, seq);

KeyValuePair[] decodedPairs = new KeyValuePair[pairs.length];
KeyValuePair[] arr = pairs.toArray(EMPTY_ARRAY);
Arrays.sort(arr);
int i = 0;
while (iter.hasNext()) {
decodedPairs[i++] = new KeyValuePair(iter.next().asBytes(), iter.next().asBytes());
assertEquals(arr[i++], new KeyValuePair(iter.next().asBytes(), iter.next().asBytes()));
}
assertArrayEquals(pairs, decodedPairs);

assertEquals(ENR_STRING, record.toString());
}

Expand Down

0 comments on commit 859cba6

Please sign in to comment.