use of org.ethereum.beacon.discovery.schema.NodeRecord in project teku by ConsenSys.
the class NodeRecordConverterTest method shouldConvertRealEnrToDiscoveryPeer.
@Test
public void shouldConvertRealEnrToDiscoveryPeer() throws Exception {
final String enr = "-Iu4QMmfe-EkDnVX6k5i2LFTiDQ-q4-Cb1I01iRI-wbCD_r4Z8eujNCgZDmZXb1ZOPi1LfJaNx3Bd0QUK9wqBjwUXJQBgmlkgnY0gmlwhH8AAAGJc2VjcDI1NmsxoQO4btn3R6f6mZY_OeOxdrRenoYxCKLRReo6TnbY0JNRlIN0Y3CCIyiDdWRwgiMo";
final NodeRecord nodeRecord = NodeRecordFactory.DEFAULT.fromBase64(enr);
final DiscoveryPeer expectedPeer = new DiscoveryPeer(Bytes.fromHexString("0x03B86ED9F747A7FA99963F39E3B176B45E9E863108A2D145EA3A4E76D8D0935194"), new InetSocketAddress(InetAddress.getByAddress(new byte[] { 127, 0, 0, 1 }), 9000), Optional.empty(), ATTNETS, SYNCNETS);
assertThat(CONVERTER.convertToDiscoveryPeer(nodeRecord, SCHEMA_DEFINITIONS)).contains(expectedPeer);
}
use of org.ethereum.beacon.discovery.schema.NodeRecord in project besu by hyperledger.
the class PeerDiscoveryAgentTest method testNodeRecordCreated.
@Test
public void testNodeRecordCreated() {
KeyPair keyPair = SIGNATURE_ALGORITHM.get().createKeyPair(SIGNATURE_ALGORITHM.get().createPrivateKey(Bytes32.fromHexString("0xb71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")));
final MockPeerDiscoveryAgent agent = helper.startDiscoveryAgent(helper.agentBuilder().nodeKey(NodeKeyUtils.createFrom(keyPair)).advertisedHost("127.0.0.1").bindPort(30303));
assertThat(agent.getAdvertisedPeer().isPresent()).isTrue();
assertThat(agent.getAdvertisedPeer().get().getNodeRecord().isPresent()).isTrue();
final NodeRecord nodeRecord = agent.getAdvertisedPeer().get().getNodeRecord().get();
assertThat(nodeRecord.getNodeId()).isNotNull();
assertThat(nodeRecord.getIdentityScheme()).isNotNull();
assertThat(nodeRecord.getSignature()).isNotNull();
assertThat(nodeRecord.getSeq()).isNotNull();
assertThat(nodeRecord.get("eth")).isNotNull();
assertThat(nodeRecord.get("eth")).isEqualTo(Collections.singletonList(Collections.singletonList(Bytes.EMPTY)));
assertThat(nodeRecord.asEnr()).isEqualTo("enr:-JC4QOfroMOa1sB6ajxcBKdWn3s9S4Ojl33pbRm72S5FnCwyZfskmjkJvZznQaWNTrOHrnKxw1R9xMm9rl" + "EGOcsOyscBg2V0aMLBgIJpZIJ2NIJpcIR_AAABiXNlY3AyNTZrMaEDymNMrg1JrLQB2KTGtv6MVbcNEV" + "v0AHacwUAPMljNMTiDdGNwAoN1ZHCCdl8");
}
use of org.ethereum.beacon.discovery.schema.NodeRecord in project besu by hyperledger.
the class ENRResponsePacketDataTest method readFrom.
@Test
public void readFrom() {
final Bytes requestHash = Bytes.fromHexStringLenient("0x1234");
final Bytes nodeId = Bytes.fromHexString("a448f24c6d18e575453db13171562b71999873db5b286df957af199ec94617f7");
final Bytes privateKey = Bytes.fromHexString("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291");
NodeRecord nodeRecord = NodeRecordFactory.DEFAULT.createFromValues(UInt64.ONE, new EnrField(EnrField.ID, IdentitySchema.V4), new EnrField(EnrField.IP_V4, Bytes.fromHexString("0x7F000001")), new EnrField(EnrField.UDP, 30303), new EnrField(EnrField.PKEY_SECP256K1, Functions.derivePublicKeyFromPrivate(privateKey)));
nodeRecord.sign(privateKey);
assertThat(nodeRecord.getNodeId()).isEqualTo(nodeId);
assertThat(nodeRecord.asEnr()).isEqualTo("enr:-IS4QHCYrYZbAKWCBRlAy5zzaDZXJBGkcnh4MHcBFZntXNFrdvJjX04jRzjzCBOonrkTfj499SZuOh8R33L" + "s8RRcy5wBgmlkgnY0gmlwhH8AAAGJc2VjcDI1NmsxoQPKY0yuDUmstAHYpMa2_oxVtw0RW_QAdpzBQA8y" + "WM0xOIN1ZHCCdl8");
BytesValueRLPOutput out = new BytesValueRLPOutput();
out.startList();
out.writeBytes(requestHash);
out.writeRLPBytes(nodeRecord.serialize());
out.endList();
final Bytes encoded = out.encoded();
final ENRResponsePacketData deserialized = ENRResponsePacketData.readFrom(RLP.input(encoded));
assertThat(deserialized.getRequestHash()).isEqualTo(requestHash);
assertThat(deserialized.getEnr()).isEqualTo(nodeRecord);
}
use of org.ethereum.beacon.discovery.schema.NodeRecord in project besu by hyperledger.
the class ENRResponsePacketDataTest method readFrom_invalidSignature.
@Test
public void readFrom_invalidSignature() {
final Bytes requestHash = Bytes.fromHexStringLenient("0x1234");
final Bytes privateKey = Bytes.fromHexString("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f292");
NodeRecord nodeRecord = NodeRecordFactory.DEFAULT.createFromValues(UInt64.ONE, new EnrField(EnrField.ID, IdentitySchema.V4), new EnrField(EnrField.IP_V4, Bytes.fromHexString("0x7F000001")), new EnrField(EnrField.UDP, 30303), new EnrField(EnrField.PKEY_SECP256K1, Functions.derivePublicKeyFromPrivate(privateKey)));
nodeRecord.sign(privateKey);
nodeRecord.set(EnrField.UDP, 1234);
BytesValueRLPOutput out = new BytesValueRLPOutput();
out.startList();
out.writeBytes(requestHash);
out.writeRLPBytes(nodeRecord.serialize());
out.endList();
final Bytes encoded = out.encoded();
final ENRResponsePacketData deserialized = ENRResponsePacketData.readFrom(RLP.input(encoded));
assertThat(deserialized.getRequestHash()).isEqualTo(requestHash);
assertThat(deserialized.getEnr()).isEqualTo(nodeRecord);
assertThat(deserialized.getEnr().isValid()).isFalse();
}
use of org.ethereum.beacon.discovery.schema.NodeRecord in project besu by hyperledger.
the class ENRResponsePacketDataTest method writeTo.
@Test
public void writeTo() {
final Bytes requestHash = Bytes.fromHexStringLenient("0x1234");
final Bytes nodeId = Bytes.fromHexString("a448f24c6d18e575453db13171562b71999873db5b286df957af199ec94617f7");
final Bytes privateKey = Bytes.fromHexString("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291");
NodeRecord nodeRecord = NodeRecordFactory.DEFAULT.createFromValues(UInt64.ONE, new EnrField(EnrField.ID, IdentitySchema.V4), new EnrField(EnrField.IP_V4, Bytes.fromHexString("0x7F000001")), new EnrField(EnrField.UDP, 30303), new EnrField(EnrField.PKEY_SECP256K1, Functions.derivePublicKeyFromPrivate(privateKey)));
nodeRecord.sign(privateKey);
assertThat(nodeRecord.getNodeId()).isEqualTo(nodeId);
assertThat(nodeRecord.asEnr()).isEqualTo("enr:-IS4QHCYrYZbAKWCBRlAy5zzaDZXJBGkcnh4MHcBFZntXNFrdvJjX04jRzjzCBOonrkTfj499SZuOh8R33L" + "s8RRcy5wBgmlkgnY0gmlwhH8AAAGJc2VjcDI1NmsxoQPKY0yuDUmstAHYpMa2_oxVtw0RW_QAdpzBQA8y" + "WM0xOIN1ZHCCdl8");
BytesValueRLPOutput out = new BytesValueRLPOutput();
final ENRResponsePacketData packet = ENRResponsePacketData.create(requestHash, nodeRecord);
packet.writeTo(out);
final Bytes encoded = out.encoded();
final ENRResponsePacketData deserialized = ENRResponsePacketData.readFrom(RLP.input(encoded));
assertThat(deserialized.getRequestHash()).isEqualTo(requestHash);
assertThat(deserialized.getEnr()).isEqualTo(nodeRecord);
}
Aggregations