You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuweni.apache.org by to...@apache.org on 2020/10/13 05:44:12 UTC
[incubator-tuweni] branch master updated: Add public key to address
transformation (#154)
This is an automated email from the ASF dual-hosted git repository.
toulmean pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-tuweni.git
The following commit(s) were added to refs/heads/master by this push:
new 3492f09 Add public key to address transformation (#154)
3492f09 is described below
commit 3492f0937416ca0f308f9fdf33b42fa99832ee3d
Author: Antoine Toulme <at...@users.noreply.github.com>
AuthorDate: Mon Oct 12 22:43:59 2020 -0700
Add public key to address transformation (#154)
---
.../main/java/org/apache/tuweni/eth/Address.java | 24 +++++++++++++++
.../apache/tuweni/eth/PublicKeyToAddressTest.java | 35 ++++++++++++++++++++++
2 files changed, 59 insertions(+)
diff --git a/eth/src/main/java/org/apache/tuweni/eth/Address.java b/eth/src/main/java/org/apache/tuweni/eth/Address.java
index 6e23759..b7d426c 100644
--- a/eth/src/main/java/org/apache/tuweni/eth/Address.java
+++ b/eth/src/main/java/org/apache/tuweni/eth/Address.java
@@ -17,6 +17,7 @@ import static java.util.Objects.requireNonNull;
import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.bytes.DelegatingBytes;
+import org.apache.tuweni.crypto.SECP256K1;
/**
* An Ethereum account address.
@@ -24,6 +25,29 @@ import org.apache.tuweni.bytes.DelegatingBytes;
public final class Address extends DelegatingBytes {
/**
+ * Transform a public key into an Ethereum address.
+ *
+ * @param publicKey the public key
+ * @return the address
+ */
+ public static Address fromPublicKey(SECP256K1.PublicKey publicKey) {
+ requireNonNull(publicKey);
+ return fromPublicKeyBytes(publicKey.bytes());
+ }
+
+ /**
+ * Transform a public key into an Ethereum address.
+ *
+ * @param bytes the bytes of the public key
+ * @return the address
+ */
+ public static Address fromPublicKeyBytes(Bytes bytes) {
+ requireNonNull(bytes);
+ Bytes value = org.apache.tuweni.crypto.Hash.keccak256(bytes);
+ return new Address(value.slice(12));
+ }
+
+ /**
* Create an address from Bytes.
*
* <p>
diff --git a/eth/src/test/java/org/apache/tuweni/eth/PublicKeyToAddressTest.java b/eth/src/test/java/org/apache/tuweni/eth/PublicKeyToAddressTest.java
new file mode 100644
index 0000000..47f5e37
--- /dev/null
+++ b/eth/src/test/java/org/apache/tuweni/eth/PublicKeyToAddressTest.java
@@ -0,0 +1,35 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE
+ * file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file
+ * to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
+ * License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
+ * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations under the License.
+ */
+package org.apache.tuweni.eth;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import org.apache.tuweni.bytes.Bytes32;
+import org.apache.tuweni.crypto.SECP256K1;
+import org.apache.tuweni.junit.BouncyCastleExtension;
+
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+
+@ExtendWith(BouncyCastleExtension.class)
+class PublicKeyToAddressTest {
+
+ @Test
+ public void testTransformFromSecretKeyToAddress() {
+ SECP256K1.SecretKey key = SECP256K1.SecretKey
+ .fromBytes(Bytes32.fromHexString("4ee50b74f1f903a80df52c4f6b43a17bc1319636e203f3fe9c09294f74907849"));
+ SECP256K1.PublicKey pk = SECP256K1.KeyPair.fromSecretKey(key).publicKey();
+ Address addr = Address.fromPublicKeyBytes(pk.bytes());
+ assertEquals(Address.fromHexString("0x25851ab5f8151a68d0014fd508609bbf6b4d6d1d"), addr);
+ }
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@tuweni.apache.org
For additional commands, e-mail: commits-help@tuweni.apache.org