Skip to content

Commit

Permalink
use url-safe base64 for eip-778
Browse files Browse the repository at this point in the history
add toString(int) to RLPItem
  • Loading branch information
esaulpaugh committed Jun 9, 2019
1 parent 446adde commit 865d8dc
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 19 deletions.
4 changes: 4 additions & 0 deletions src/main/java/com/esaulpaugh/headlong/rlp/RLPItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -266,4 +266,8 @@ public String toString() {
throw new RuntimeException(e);
}
}

public String toString(int encoding) {
return Strings.encode(buffer, index, encodingLength(), encoding);
}
}
36 changes: 21 additions & 15 deletions src/main/java/com/esaulpaugh/headlong/rlp/eip778/Record.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,17 @@
*/
package com.esaulpaugh.headlong.rlp.eip778;

import com.esaulpaugh.headlong.rlp.*;
import com.esaulpaugh.headlong.rlp.RLPDecoder;
import com.esaulpaugh.headlong.rlp.RLPEncoder;
import com.esaulpaugh.headlong.rlp.RLPItem;
import com.esaulpaugh.headlong.rlp.RLPList;
import com.esaulpaugh.headlong.rlp.RLPListIterator;
import com.esaulpaugh.headlong.rlp.exception.DecodeException;

import java.security.SignatureException;

import static com.esaulpaugh.headlong.rlp.RLPDecoder.RLP_STRICT;
import static com.esaulpaugh.headlong.util.Strings.HEX;
import static com.esaulpaugh.headlong.util.Strings.BASE_64_URL_SAFE;

/**
* Implementation of EIP 778: Ethereum Node Records (ENR), https://eips.ethereum.org/EIPS/eip-778
Expand All @@ -30,7 +34,9 @@ public final class Record {

private static final int MAX_RECORD_LEN = 300;

private final RLPList record;
private static final String ENR_PREFIX = "enr:";

private final RLPList rlp;

public Record(long seq, KeyValuePair[] pairs, Signer signer) {
final int signatureLen = signer.signatureLength();
Expand All @@ -52,43 +58,43 @@ public Record(long seq, KeyValuePair[] pairs, Signer signer) {
RLPEncoder.insertRecordSignature(signature, record, recordPrefixLen);

try {
this.record = RLP_STRICT.wrapList(record);
this.rlp = RLP_STRICT.wrapList(record);
} catch (DecodeException e) {
throw new Error(e); // shouldn't happen
}
}

private Record(RLPList record) {
this.record = record;
private Record(RLPList recordRLP) {
this.rlp = recordRLP;
}

public static Record decode(byte[] record) throws DecodeException {
return new Record(RLP_STRICT.wrapList(record));
}

public RLPList getRecord() {
return record;
public RLPList getRLP() {
return rlp;
}

public RLPItem getSignature() throws DecodeException {
return getRecord().iterator(RLP_STRICT).next();
return getRLP().iterator(RLP_STRICT).next();
}

public RLPList getContent() throws DecodeException {
return RLP_STRICT.wrapList(getContentBytes(getSignature().endIndex));
}

public long getSeq() throws DecodeException {
RLPListIterator iter = new RLPListIterator(getRecord(), RLP_STRICT);
RLPListIterator iter = new RLPListIterator(getRLP(), RLP_STRICT);
iter.next();
return iter.next().asLong();
}

private byte[] getContentBytes(int index) {
int contentDataLen = record.encodingLength() - index;
int contentDataLen = rlp.encodingLength() - index;
byte[] content = new byte[RLPEncoder.prefixLength(contentDataLen) + contentDataLen];
int prefixLen = RLPEncoder.insertListPrefix(contentDataLen, content, 0);
record.exportRange(index, index + contentDataLen, content, prefixLen);
rlp.exportRange(index, index + contentDataLen, content, prefixLen);
return content;
}

Expand All @@ -110,18 +116,18 @@ public interface Verifier {

@Override
public int hashCode() {
return record.hashCode();
return rlp.hashCode();
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
return record.equals(((Record) o).record);
return rlp.equals(((Record) o).rlp);
}

@Override
public String toString() {
return record.asString(HEX);
return ENR_PREFIX + rlp.toString(BASE_64_URL_SAFE);
}
}
12 changes: 8 additions & 4 deletions src/test/java/com/esaulpaugh/headlong/rlp/eip778/EIP778Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,13 @@

public class EIP778Test {

private static final String ENR_STRING = "enr:-IS4QHCYrYZbAKWCBRlAy5zzaDZXJBGkcnh4MHcBFZntXNFrdvJjX04jRzjzCBOonrkTfj499SZuOh8R33Ls8RRcy5wBgmlkgnY0gmlwhH8AAAGJc2VjcDI1NmsxoQPKY0yuDUmstAHYpMa2_oxVtw0RW_QAdpzBQA8yWM0xOIN1ZHCCdl8";

private static final Record.Signer SIGNER = new Record.Signer() {

private final byte[] SIG = FastHex.decode(
"7098ad865b00a582051940cb9cf36836572411a47278783077011599ed5cd16b" +
"76f2635f4e234738f30813a89eb9137e3e3df5266e3a1f11df72ecf1145ccb9c"
"7098ad865b00a582051940cb9cf36836572411a47278783077011599ed5cd16b"
+ "76f2635f4e234738f30813a89eb9137e3e3df5266e3a1f11df72ecf1145ccb9c"
);

@Override
Expand Down Expand Up @@ -83,7 +85,7 @@ public void testEip778() throws DecodeException, SignatureException {

Assert.assertEquals(VECTOR.getSignature(), record.getSignature());
Assert.assertEquals(VECTOR.getContent(), record.getContent());
Assert.assertEquals(VECTOR.getRecord(), record.getRecord());
Assert.assertEquals(VECTOR.getRLP(), record.getRLP());
Assert.assertEquals(VECTOR.toString(), record.toString());
Assert.assertEquals(VECTOR, record);

Expand All @@ -103,6 +105,8 @@ public void testEip778() throws DecodeException, SignatureException {
decodedPairs[i++] = new KeyValuePair(iter.next().data(), iter.next().data());
}
Assert.assertArrayEquals(pairs, decodedPairs);

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

@Test
Expand All @@ -115,7 +119,7 @@ public void nineLengths() {
do {
if(temp >= 0) {
Record r = new Record(temp, pairs, SIGNER);
int len = r.getRecord().encodingLength();
int len = r.getRLP().encodingLength();
System.out.println(temp + " -> " + len);
recordLengths.add(len);
}
Expand Down

0 comments on commit 865d8dc

Please sign in to comment.