Skip to content

Commit

Permalink
Remove legacy sign method (#171)
Browse files Browse the repository at this point in the history
* remove legacy sign method

* bump to the latest xmtp

* fix the 23

* fix the signing logic

* fix linter

* fix tests
  • Loading branch information
nplasterer authored Feb 5, 2024
1 parent 5aab3d4 commit 8a019c7
Show file tree
Hide file tree
Showing 12 changed files with 21 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,34 +26,10 @@ data class WalletConnectV2Account(
)

override suspend fun sign(data: ByteArray): SignatureOuterClass.Signature? {
return signLegacy(String(data))
return sign(String(data))
}

override fun sign(text: String): ByteArray {
val (parentChain, chainId, account) = session.namespaces.getValue(chain).accounts[0].split(":")
val requestParams = session.namespaces.getValue(chain).methods.find { method ->
method == "personal_sign"
}?.let { method ->
Modal.Params.Request(
sessionTopic = session.topic,
method = method,
params = getPersonalSignBody(text, account),
chainId = "$parentChain:$chainId"
)
}

runCatching {
runBlocking {
requestMethod(requestParams!!, sendSessionRequestDeepLink).first().getOrThrow()
}
}.onSuccess {
return it
}.onFailure {}

return byteArrayOf()
}

override suspend fun signLegacy(message: String): SignatureOuterClass.Signature? {
override suspend fun sign(message: String): SignatureOuterClass.Signature? {
val (parentChain, chainId, account) = session.namespaces.getValue(chain).accounts[0].split(":")
val requestParams = session.namespaces.getValue(chain).methods.find { method ->
method == "personal_sign"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,8 @@ class FakeWallet : SigningKey {
return signature
}

override fun sign(text: String): ByteArray {
return privateKeyBuilder.sign(text)
}

override suspend fun signLegacy(message: String): Signature {
val signature = privateKeyBuilder.signLegacy(message)
override suspend fun sign(message: String): Signature {
val signature = privateKeyBuilder.sign(message)
return signature
}

Expand Down
17 changes: 10 additions & 7 deletions library/src/main/java/org/xmtp/android/library/Client.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import org.xmtp.android.library.messages.encrypted
import org.xmtp.android.library.messages.ensureWalletSignature
import org.xmtp.android.library.messages.generate
import org.xmtp.android.library.messages.getPublicKeyBundle
import org.xmtp.android.library.messages.rawData
import org.xmtp.android.library.messages.recoverWalletSignerPublicKey
import org.xmtp.android.library.messages.toPublicKeyBundle
import org.xmtp.android.library.messages.toV2
Expand Down Expand Up @@ -335,14 +336,16 @@ class Client() {
null
}

if (v3Client?.textToSign() == null) {
v3Client?.registerIdentity(null)
} else if (account != null) {
v3Client.textToSign()?.let {
v3Client.registerIdentity(account.sign(it))
if (v3Client != null) {
if (v3Client.textToSign() == null) {
v3Client.registerIdentity(null)
} else if (account != null) {
v3Client.textToSign()?.let {
v3Client.registerIdentity(account.sign(it)?.rawData)
}
} else {
throw XMTPException("No signer passed but signer was required.")
}
} else {
Log.i(TAG, "No signer passed but signer was required.")
}

return v3Client
Expand Down
6 changes: 2 additions & 4 deletions library/src/main/java/org/xmtp/android/library/SigningKey.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ interface SigningKey {

suspend fun sign(data: ByteArray): SignatureOuterClass.Signature?

suspend fun signLegacy(message: String): SignatureOuterClass.Signature?

fun sign(text: String): ByteArray
suspend fun sign(message: String): SignatureOuterClass.Signature?
}

/**
Expand Down Expand Up @@ -52,7 +50,7 @@ fun SigningKey.createIdentity(
val signatureClass = Signature.newBuilder().build()
val signatureText = signatureClass.createIdentityText(key = slimKey.toByteArray())
val digest = signatureClass.ethHash(message = signatureText)
val signature = runBlocking { signLegacy(signatureText) } ?: throw XMTPException("Illegal signature")
val signature = runBlocking { sign(signatureText) } ?: throw XMTPException("Illegal signature")

val signatureData = KeyUtil.getSignatureData(signature.rawData.toByteString().toByteArray())
val publicKey = Sign.recoverFromSignature(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ fun EncryptedPrivateKeyBundle.decrypted(
}

val signature = runBlocking {
key.signLegacy(
key.sign(
message = Signature.newBuilder().build()
.enableIdentityText(key = v1.walletPreKey.toByteArray()),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import org.xmtp.android.library.KeyUtil
import org.xmtp.android.library.SigningKey
import org.xmtp.proto.message.contents.PublicKeyOuterClass
import org.xmtp.proto.message.contents.SignatureOuterClass
import java.nio.charset.StandardCharsets
import java.security.SecureRandom
import java.util.Date

Expand Down Expand Up @@ -93,16 +92,7 @@ class PrivateKeyBuilder : SigningKey {
}.build()
}

override fun sign(text: String): ByteArray {
val messageBytes: ByteArray = text.toByteArray(StandardCharsets.UTF_8)
val signature = Sign.signPrefixedMessage(
messageBytes,
ECKeyPair.create(privateKey.secp256K1.bytes.toByteArray())
)
return signature.r + signature.s + signature.v
}

override suspend fun signLegacy(message: String): SignatureOuterClass.Signature {
override suspend fun sign(message: String): SignatureOuterClass.Signature {
val digest = Signature.newBuilder().build().ethHash(message)
return sign(digest)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ fun PrivateKeyBundle.encrypted(

val signature =
runBlocking {
key.signLegacy(
key.sign(
message = Signature.newBuilder().build().enableIdentityText(key = walletPreKey)
)
} ?: throw XMTPException("Illegal signature")
Expand Down
Binary file modified library/src/main/jniLibs/arm64-v8a/libuniffi_xmtpv3.so
Binary file not shown.
Binary file modified library/src/main/jniLibs/armeabi-v7a/libuniffi_xmtpv3.so
Binary file not shown.
Binary file modified library/src/main/jniLibs/x86/libuniffi_xmtpv3.so
Binary file not shown.
Binary file modified library/src/main/jniLibs/x86_64/libuniffi_xmtpv3.so
Binary file not shown.
8 changes: 2 additions & 6 deletions library/src/test/java/org/xmtp/android/library/TestHelpers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,8 @@ class FakeWallet : SigningKey {
return signature
}

override fun sign(text: String): ByteArray {
return privateKeyBuilder.sign(text)
}

override suspend fun signLegacy(message: String): Signature {
val signature = privateKeyBuilder.signLegacy(message)
override suspend fun sign(message: String): Signature {
val signature = privateKeyBuilder.sign(message)
return signature
}

Expand Down

0 comments on commit 8a019c7

Please sign in to comment.