From cd8b71401d89dc6114011149eebc9f8ae81c3751 Mon Sep 17 00:00:00 2001
From: Naomi Plasterer <naomi@xmtp.com>
Date: Sun, 3 Nov 2024 12:29:28 -0800
Subject: [PATCH] fix up the wallet address

---
 .../xmtp/android/library/messages/PrivateKey.kt    | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)

diff --git a/library/src/main/java/org/xmtp/android/library/messages/PrivateKey.kt b/library/src/main/java/org/xmtp/android/library/messages/PrivateKey.kt
index f67a0d4e4..dcf9b5007 100644
--- a/library/src/main/java/org/xmtp/android/library/messages/PrivateKey.kt
+++ b/library/src/main/java/org/xmtp/android/library/messages/PrivateKey.kt
@@ -4,12 +4,15 @@ import com.google.protobuf.kotlin.toByteString
 import kotlinx.coroutines.runBlocking
 import org.web3j.crypto.ECKeyPair
 import org.web3j.crypto.Hash
+import org.web3j.crypto.Keys
 import org.web3j.crypto.Sign
 import org.xmtp.android.library.KeyUtil
 import org.xmtp.android.library.SigningKey
+import org.xmtp.android.library.toHex
 import org.xmtp.proto.message.contents.PublicKeyOuterClass
 import org.xmtp.proto.message.contents.SignatureOuterClass
 import java.security.SecureRandom
+import java.util.Arrays
 import java.util.Date
 
 typealias PrivateKey = org.xmtp.proto.message.contents.PrivateKeyOuterClass.PrivateKey
@@ -96,7 +99,16 @@ fun PrivateKey.generate(): PrivateKey {
 }
 
 val PrivateKey.walletAddress: String
-    get() = publicKey.walletAddress
+    get() {
+        val address = Keys.getAddress(
+            Arrays.copyOfRange(
+                publicKey.secp256K1Uncompressed.bytes.toByteArray(),
+                1,
+                publicKey.secp256K1Uncompressed.bytes.toByteArray().size
+            )
+        )
+        return Keys.toChecksumAddress(address.toHex())
+    }
 
 fun PrivateKey.sign(key: PublicKeyOuterClass.UnsignedPublicKey): PublicKeyOuterClass.SignedPublicKey {
     val bytes = key.toByteArray()