From 865d8dcda9e2f4b7cc9b9a2dc121c2181806805f Mon Sep 17 00:00:00 2001 From: Evan Saulpaugh Date: Sun, 9 Jun 2019 12:06:46 -0500 Subject: [PATCH] use url-safe base64 for eip-778 add toString(int) to RLPItem --- .../com/esaulpaugh/headlong/rlp/RLPItem.java | 4 +++ .../headlong/rlp/eip778/Record.java | 36 +++++++++++-------- .../headlong/rlp/eip778/EIP778Test.java | 12 ++++--- 3 files changed, 33 insertions(+), 19 deletions(-) diff --git a/src/main/java/com/esaulpaugh/headlong/rlp/RLPItem.java b/src/main/java/com/esaulpaugh/headlong/rlp/RLPItem.java index f5f7b57cf..482d8995b 100644 --- a/src/main/java/com/esaulpaugh/headlong/rlp/RLPItem.java +++ b/src/main/java/com/esaulpaugh/headlong/rlp/RLPItem.java @@ -266,4 +266,8 @@ public String toString() { throw new RuntimeException(e); } } + + public String toString(int encoding) { + return Strings.encode(buffer, index, encodingLength(), encoding); + } } diff --git a/src/main/java/com/esaulpaugh/headlong/rlp/eip778/Record.java b/src/main/java/com/esaulpaugh/headlong/rlp/eip778/Record.java index 9e2079402..e21f8ff98 100644 --- a/src/main/java/com/esaulpaugh/headlong/rlp/eip778/Record.java +++ b/src/main/java/com/esaulpaugh/headlong/rlp/eip778/Record.java @@ -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 @@ -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(); @@ -52,26 +58,26 @@ 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 { @@ -79,16 +85,16 @@ public RLPList getContent() throws DecodeException { } 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; } @@ -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); } } diff --git a/src/test/java/com/esaulpaugh/headlong/rlp/eip778/EIP778Test.java b/src/test/java/com/esaulpaugh/headlong/rlp/eip778/EIP778Test.java index bce459047..089a61ae3 100644 --- a/src/test/java/com/esaulpaugh/headlong/rlp/eip778/EIP778Test.java +++ b/src/test/java/com/esaulpaugh/headlong/rlp/eip778/EIP778Test.java @@ -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 @@ -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); @@ -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 @@ -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); }