Skip to content

Commit

Permalink
XyoAccount implements AccountInstance
Browse files Browse the repository at this point in the history
  • Loading branch information
arietrouw committed Nov 15, 2024
1 parent f8db538 commit b741f94
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class XyoAccountPrefsRepositoryTest {
this.appContext = InstrumentationRegistry.getInstrumentation().targetContext
}

@OptIn(ExperimentalStdlibApi::class)
@Test
fun testAccountPersistence() {
runBlocking {
Expand All @@ -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(
Expand All @@ -47,7 +48,7 @@ class XyoAccountPrefsRepositoryTest {
)
)
panel2.resolveNodes()
val secondGeneratedAddress = panel2.defaultAccount?.address?.hex
val secondGeneratedAddress = panel2.defaultAccount?.address?.toHexString()
assertEquals(generatedAddress, secondGeneratedAddress)
}
}
Expand Down
6 changes: 4 additions & 2 deletions sdk/src/androidTest/java/network/xyo/client/XyoAccountTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ class XyoAccountTest {
val testVectorAddressKeccakHash = "0889fa0b3d5bb98e749c7bf75e7a847447e7fec41011ae7d32d768f86605ba03"
val testVectorAddress = "5e7a847447e7fec41011ae7d32d768f86605ba03"

@OptIn(ExperimentalStdlibApi::class)
@Test
fun testAddress() {
val account = XyoAccount(XyoSerializable.hexToBytes(testVectorPrivate))
val signature = XyoSerializable.bytesToHex(account.private.sign(testVectorHash))
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)
}

Expand All @@ -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)
Expand Down
27 changes: 21 additions & 6 deletions sdk/src/main/java/network/xyo/client/address/XyoAccount.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ open class XyoBoundWitnessBuilder {

var _timestamp: Long? = null

@OptIn(ExperimentalStdlibApi::class)
val addresses: List<String>
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)
Expand Down

0 comments on commit b741f94

Please sign in to comment.