From b741f9436074a6bd1b6b318e2905eeaa2f3b8317 Mon Sep 17 00:00:00 2001 From: Arie Trouw Date: Fri, 15 Nov 2024 09:38:18 -0800 Subject: [PATCH] XyoAccount implements AccountInstance --- .../client/XyoAccountPrefsRepositoryTest.kt | 5 ++-- .../java/network/xyo/client/XyoAccountTest.kt | 6 +++-- .../network/xyo/client/address/XyoAccount.kt | 27 ++++++++++++++----- .../boundwitness/XyoBoundWitnessBuilder.kt | 3 ++- 4 files changed, 30 insertions(+), 11 deletions(-) diff --git a/sdk/src/androidTest/java/network/xyo/client/XyoAccountPrefsRepositoryTest.kt b/sdk/src/androidTest/java/network/xyo/client/XyoAccountPrefsRepositoryTest.kt index d4f9978..773409e 100644 --- a/sdk/src/androidTest/java/network/xyo/client/XyoAccountPrefsRepositoryTest.kt +++ b/sdk/src/androidTest/java/network/xyo/client/XyoAccountPrefsRepositoryTest.kt @@ -25,6 +25,7 @@ class XyoAccountPrefsRepositoryTest { this.appContext = InstrumentationRegistry.getInstrumentation().targetContext } + @OptIn(ExperimentalStdlibApi::class) @Test fun testAccountPersistence() { runBlocking { @@ -38,7 +39,7 @@ class XyoAccountPrefsRepositoryTest { ) ) panel.resolveNodes() - val generatedAddress = panel.defaultAccount?.address?.hex + val generatedAddress = panel.defaultAccount?.address?.toHexString() assertNotEquals(generatedAddress, null) val panel2 = XyoPanel( @@ -47,7 +48,7 @@ class XyoAccountPrefsRepositoryTest { ) ) panel2.resolveNodes() - val secondGeneratedAddress = panel2.defaultAccount?.address?.hex + val secondGeneratedAddress = panel2.defaultAccount?.address?.toHexString() assertEquals(generatedAddress, secondGeneratedAddress) } } diff --git a/sdk/src/androidTest/java/network/xyo/client/XyoAccountTest.kt b/sdk/src/androidTest/java/network/xyo/client/XyoAccountTest.kt index 2a45d96..90c5153 100644 --- a/sdk/src/androidTest/java/network/xyo/client/XyoAccountTest.kt +++ b/sdk/src/androidTest/java/network/xyo/client/XyoAccountTest.kt @@ -13,6 +13,7 @@ class XyoAccountTest { val testVectorAddressKeccakHash = "0889fa0b3d5bb98e749c7bf75e7a847447e7fec41011ae7d32d768f86605ba03" val testVectorAddress = "5e7a847447e7fec41011ae7d32d768f86605ba03" + @OptIn(ExperimentalStdlibApi::class) @Test fun testAddress() { val account = XyoAccount(XyoSerializable.hexToBytes(testVectorPrivate)) @@ -20,7 +21,7 @@ class XyoAccountTest { assertEquals(testVectorPrivate, account.private.hex) assertEquals(testVectorPublic, account.public.hex) assertEquals(testVectorAddressKeccakHash, account.public.keccak256.hex) - assertEquals(testVectorAddress, account.address.hex) + assertEquals(testVectorAddress, account.address.toHexString()) assertEquals(testVectorSignature, signature) } @@ -31,12 +32,13 @@ class XyoAccountTest { assertEquals(testVectorPrivate, privateHex) } + @OptIn(ExperimentalStdlibApi::class) @Test fun testInitWithGenerate() { val account = XyoAccount() val privateHex = account.private.hex val publicHex = account.public.hex - val addressHex = account.address.hex + val addressHex = account.address.toHexString() assertNotNull(privateHex) assertNotNull(publicHex) assertNotNull(addressHex) diff --git a/sdk/src/main/java/network/xyo/client/address/XyoAccount.kt b/sdk/src/main/java/network/xyo/client/address/XyoAccount.kt index 25fa27f..3bbcc5b 100644 --- a/sdk/src/main/java/network/xyo/client/address/XyoAccount.kt +++ b/sdk/src/main/java/network/xyo/client/address/XyoAccount.kt @@ -2,6 +2,7 @@ package network.xyo.client.address import android.os.Build import androidx.annotation.RequiresApi +import network.xyo.client.account.model.AccountInstance import org.spongycastle.jcajce.provider.asymmetric.ec.BCECPrivateKey import org.spongycastle.math.ec.ECPoint import java.math.BigInteger @@ -11,22 +12,36 @@ class ECKeyPair(val private: BCECPrivateKey, val public: ECPoint) val SECP256K1N = BigInteger("fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141", 16) @RequiresApi(Build.VERSION_CODES.M) -open class XyoAccount(privateKeyBytes: ByteArray? = null) { +open class XyoAccount(privateKeyBytes: ByteArray? = null): AccountInstance { private val keyPair: XyoKeyPair = XyoKeyPair(privateKeyBytes) + @Deprecated("Use .privateKey instead") val private: XyoPrivateKey get() { return this.keyPair.private } + @Deprecated("Use .publicKey instead") val public: XyoPublicKey get() { return this.keyPair.public } - - open val address: XyoAddressValue - get() { - return public.address - } + override val previousHash: ByteArray? + get() = null + override val privateKey: ByteArray + get() = private.bytes + override val publicKey: ByteArray + get() = public.bytes + + override fun sign(hash: ByteArray): ByteArray { + return private.sign(hash) + } + + override val address: ByteArray + get() = public.address.bytes + + override fun verify(msg: ByteArray, signature: ByteArray): Boolean { + return public.verify(msg, signature) + } } \ No newline at end of file diff --git a/sdk/src/main/java/network/xyo/client/boundwitness/XyoBoundWitnessBuilder.kt b/sdk/src/main/java/network/xyo/client/boundwitness/XyoBoundWitnessBuilder.kt index 760afd9..f4dbca8 100644 --- a/sdk/src/main/java/network/xyo/client/boundwitness/XyoBoundWitnessBuilder.kt +++ b/sdk/src/main/java/network/xyo/client/boundwitness/XyoBoundWitnessBuilder.kt @@ -19,8 +19,9 @@ open class XyoBoundWitnessBuilder { var _timestamp: Long? = null + @OptIn(ExperimentalStdlibApi::class) val addresses: List - get() = _witnesses.map { witness -> witness.address.hex } + get() = _witnesses.map { witness -> witness.address.toHexString() } open fun witness(account: XyoAccount, previousHash: String?): XyoBoundWitnessBuilder { _witnesses.add(account)