Skip to content

Commit

Permalink
Using AccountInstance interface to pass around accounts
Browse files Browse the repository at this point in the history
  • Loading branch information
arietrouw committed Nov 15, 2024
1 parent b741f94 commit 6b53bea
Show file tree
Hide file tree
Showing 16 changed files with 50 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,25 +53,27 @@ class XyoAccountPrefsRepositoryTest {
}
}

@OptIn(ExperimentalStdlibApi::class)
@Test
fun testClearingExistingAccount() {
runBlocking {
val instance = XyoAccountPrefsRepository.getInstance(appContext)
val originalAddress = instance.getAccount().private.hex
val originalAddress = instance.getAccount().privateKey.toHexString()

instance.clearSavedAccountKey()

val refreshedAddress = instance.getAccount().private.hex
val refreshedAddress = instance.getAccount().privateKey.toHexString()

assert(originalAddress !== refreshedAddress)
}
}

@OptIn(ExperimentalStdlibApi::class)
@Test
fun testUpdatingAccountPreferences() {
runBlocking {
val instance = XyoAccountPrefsRepository.getInstance(appContext)
val originalAddress = instance.getAccount().private.hex
val originalAddress = instance.getAccount().privateKey.toHexString()

class UpdatedAccountPreferences : AccountPreferences {
override val fileName = "network-xyo-sdk-prefs-1"
Expand All @@ -93,12 +95,13 @@ class XyoAccountPrefsRepositoryTest {
updatedAccountPrefs.storagePath
)

val refreshedAddress = refreshedInstance.getAccount().private.hex
val refreshedAddress = refreshedInstance.getAccount().privateKey.toHexString()

assert(originalAddress !== refreshedAddress)
}
}

@OptIn(ExperimentalStdlibApi::class)
@Test
fun testAccountDeserialization() {
runBlocking {
Expand All @@ -111,7 +114,7 @@ class XyoAccountPrefsRepositoryTest {

// Deserialize the test account
val firstAccount = instance.getAccount()
assertEquals(firstAccount.private.hex, testAccount.private.hex)
assertEquals(firstAccount.privateKey.toHexString(), testAccount.private.hex)

// Sign with the test account
val firstBw = XyoBoundWitnessBuilder().witness(firstAccount, null).payloads(listOf(TestConstants.debugPayload)).build()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class AccountTest {
fun testRandomAccount() {
val account = Account.random()
assert(account.privateKey.count() == 32)
assert(account.publicKey.count() == 65)
assert(account.publicKey.count() == 64)
}

@OptIn(ExperimentalStdlibApi::class)
Expand Down
7 changes: 4 additions & 3 deletions sdk/src/main/java/network/xyo/client/XyoPanel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.os.Build
import android.util.Log
import androidx.annotation.RequiresApi
import kotlinx.coroutines.launch
import network.xyo.client.account.model.AccountInstance
import network.xyo.client.address.XyoAccount
import network.xyo.client.archivist.api.PostBoundWitnessesResult
import network.xyo.client.archivist.api.XyoArchivistApiClient
Expand All @@ -21,10 +22,10 @@ data class XyoPanelReportResult(val bw: XyoBoundWitnessJson, val apiResults: Lis
data class XyoPanelReportQueryResult(val bw: XyoBoundWitnessJson, val apiResults: List<PostQueryResult>?, val payloads: List<XyoPayload>?)

@RequiresApi(Build.VERSION_CODES.M)
class XyoPanel(val context: Context, private val archivists: List<XyoArchivistApiClient>?, private val witnesses: List<XyoWitness<XyoPayload>>?, private val nodeUrlsAndAccounts: ArrayList<Pair<String, XyoAccount?>>?) {
class XyoPanel(val context: Context, private val archivists: List<XyoArchivistApiClient>?, private val witnesses: List<XyoWitness<XyoPayload>>?, private val nodeUrlsAndAccounts: ArrayList<Pair<String, AccountInstance?>>?) {
var previousHash: String? = null
private var nodes: MutableList<NodeClient>? = null
var defaultAccount: XyoAccount? = null
var defaultAccount: AccountInstance? = null

@Deprecated("use constructors without deprecated archive field")
constructor(
Expand All @@ -50,7 +51,7 @@ class XyoPanel(val context: Context, private val archivists: List<XyoArchivistAp
constructor(
context: Context,
// ArrayList to not cause compiler confusion with other class constructor signatures
nodeUrlsAndAccounts: ArrayList<Pair<String, XyoAccount?>>,
nodeUrlsAndAccounts: ArrayList<Pair<String, AccountInstance?>>,
witnesses: List<XyoWitness<XyoPayload>>? = null
): this(
context,
Expand Down
7 changes: 4 additions & 3 deletions sdk/src/main/java/network/xyo/client/XyoWitness.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package network.xyo.client
import android.content.Context
import android.os.Build
import androidx.annotation.RequiresApi
import network.xyo.client.account.model.AccountInstance
import network.xyo.client.address.XyoAccount
import network.xyo.client.payload.XyoPayload

Expand All @@ -12,7 +13,7 @@ abstract class DeferredObserver<out T: XyoPayload> {

@RequiresApi(Build.VERSION_CODES.M)
open class XyoWitness<out T: XyoPayload> (
val address: XyoAccount = XyoAccount(),
val address: AccountInstance = XyoAccount(),
private val observer: ((context: Context, previousHash: String) -> T?)? = null,
var previousHash: String = "",
val deferredObserver: DeferredObserver<T>? = null
Expand All @@ -21,13 +22,13 @@ open class XyoWitness<out T: XyoPayload> (
constructor(
observer: ((context: Context, previousHash: String) -> T?)?,
previousHash: String = "",
account: XyoAccount = XyoAccount()
account: AccountInstance = XyoAccount()
): this(account, observer, previousHash, null)

constructor(
observer: DeferredObserver<T>?,
previousHash: String = "",
account: XyoAccount = XyoAccount()
account: AccountInstance = XyoAccount()
): this(account, null, previousHash, observer)

open suspend fun observe(context: Context): T? {
Expand Down
2 changes: 1 addition & 1 deletion sdk/src/main/java/network/xyo/client/account/Account.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import tech.figure.hdwallet.signer.ECDSASignature
import java.math.BigInteger
import java.security.SecureRandom

open class Account(private val _privateKey: PrivateKey, private var _previousHash: ByteArray? = null): AccountInstance {
open class Account private constructor (private val _privateKey: PrivateKey, private var _previousHash: ByteArray? = null): AccountInstance {

constructor(privateKey: ByteArray, previousHash: ByteArray? = null) : this(PrivateKey.fromBytes(privateKey, secp256k1Curve), previousHash) {}
constructor(privateKey: BigInteger, previousHash: ByteArray? = null) : this(privateKey.toByteArray(), previousHash) {}
Expand Down
1 change: 1 addition & 0 deletions sdk/src/main/java/network/xyo/client/address/XyoAccount.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class ECKeyPair(val private: BCECPrivateKey, val public: ECPoint)
val SECP256K1N = BigInteger("fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141", 16)

@RequiresApi(Build.VERSION_CODES.M)
@Deprecated("Use Account instead")
open class XyoAccount(privateKeyBytes: ByteArray? = null): AccountInstance {

private val keyPair: XyoKeyPair = XyoKeyPair(privateKeyBytes)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.os.Build
import androidx.annotation.RequiresApi
import network.xyo.client.XyoSerializable
import network.xyo.client.XyoWitness
import network.xyo.client.account.model.AccountInstance
import network.xyo.client.address.XyoAccount
import network.xyo.client.payload.XyoPayload

Expand All @@ -18,7 +19,7 @@ class QueryBoundWitnessBuilder : XyoBoundWitnessBuilder() {
return this
}

override fun witness(account: XyoAccount, previousHash: String?): QueryBoundWitnessBuilder {
override fun witness(account: AccountInstance, previousHash: String?): QueryBoundWitnessBuilder {
super.witness(account, previousHash)
return this
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import android.os.Build
import androidx.annotation.RequiresApi
import network.xyo.client.XyoSerializable
import network.xyo.client.XyoWitness
import network.xyo.client.account.hexStringToByteArray
import network.xyo.client.account.model.AccountInstance
import network.xyo.client.address.XyoAccount
import network.xyo.client.payload.XyoPayload
import network.xyo.client.payload.XyoValidationException

@RequiresApi(Build.VERSION_CODES.M)
open class XyoBoundWitnessBuilder {
protected var _witnesses = mutableListOf<XyoAccount>()
protected var _witnesses = mutableListOf<AccountInstance>()
protected var _previous_hashes = mutableListOf<String?>()
protected var _payload_hashes = mutableListOf<String>()
protected var _payload_schemas = mutableListOf<String>()
Expand All @@ -23,7 +25,7 @@ open class XyoBoundWitnessBuilder {
val addresses: List<String>
get() = _witnesses.map { witness -> witness.address.toHexString() }

open fun witness(account: XyoAccount, previousHash: String?): XyoBoundWitnessBuilder {
open fun witness(account: AccountInstance, previousHash: String?): XyoBoundWitnessBuilder {
_witnesses.add(account)
_previous_hashes.add(previousHash)
return this
Expand Down Expand Up @@ -62,7 +64,7 @@ open class XyoBoundWitnessBuilder {

private fun sign(hash: String): List<String> {
return _witnesses.map {
val sig = XyoSerializable.bytesToHex(it.private.sign(hash))
val sig = XyoSerializable.bytesToHex(it.sign(hexStringToByteArray(hash)))
sig
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import network.xyo.data.PrefsDataStoreProtos.PrefsDataStore
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.launch
import network.xyo.client.account.hexStringToByteArray
import network.xyo.client.account.model.AccountInstance
import network.xyo.client.address.XyoAccount
import network.xyo.client.settings.AccountPreferences
import network.xyo.client.xyoScope
Expand All @@ -26,20 +27,21 @@ class XyoAccountPrefsRepository(context: Context, private val _accountPreference
get() = _accountPreferences

@RequiresApi(Build.VERSION_CODES.M)
suspend fun getAccount(): XyoAccount {
suspend fun getAccount(): AccountInstance {
val saveKeyHex = getAccountKey()
return XyoAccount(hexStringToByteArray(saveKeyHex))
}

@OptIn(ExperimentalStdlibApi::class)
@RequiresApi(Build.VERSION_CODES.M)
suspend fun initializeAccount(account: XyoAccount): XyoAccount? {
suspend fun initializeAccount(account: AccountInstance): AccountInstance? {
var updatedKey: String? = null
val job = xyoScope.launch {
val savedKey = prefsDataStore.data.first().accountKey
if (savedKey.isNullOrEmpty()) {
// no saved key so save the passed in one
updatedKey = null
setAccountKey(account.private.hex)
setAccountKey(account.privateKey.toHexString())
} else {
updatedKey = null
Log.w("xyoClient", "Key already exists. Clear it first before initializing prefs with new account")
Expand All @@ -53,13 +55,14 @@ class XyoAccountPrefsRepository(context: Context, private val _accountPreference
}
}

@OptIn(ExperimentalStdlibApi::class)
@RequiresApi(Build.VERSION_CODES.M)
private suspend fun getAccountKey(): String {
val savedKey = prefsDataStore.data.first().accountKey
return if (savedKey.isEmpty()) {
val newAccount = XyoAccount()
setAccountKey(newAccount.private.hex)
newAccount.private.hex
val newAccount: AccountInstance = XyoAccount()
setAccountKey(newAccount.privateKey.toHexString())
newAccount.privateKey.toHexString()
} else {
return savedKey
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.suspendCancellableCoroutine
import kotlinx.coroutines.withContext
import network.xyo.client.XyoSerializable
import network.xyo.client.account.model.AccountInstance
import network.xyo.client.address.XyoAccount
import network.xyo.client.boundwitness.QueryBoundWitnessBuilder
import network.xyo.client.boundwitness.QueryBoundWitnessJson
Expand All @@ -21,12 +22,12 @@ import org.json.JSONObject
import java.io.IOException

@RequiresApi(Build.VERSION_CODES.M)
class NodeClient(private val url: String, private val accountToUse: XyoAccount?) {
class NodeClient(private val url: String, private val accountToUse: AccountInstance?) {

private val _internalAccount = XyoAccount()
private val okHttp = OkHttpClient()

private val account: XyoAccount
private val account: AccountInstance
get() {
if (this.accountToUse === null) {
println("WARNING: Anonymous Queries not allowed, but running anyway.")
Expand Down
3 changes: 2 additions & 1 deletion sdk/src/main/java/network/xyo/client/settings/Defaults.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ package network.xyo.client.settings
import android.content.Context
import android.os.Build
import androidx.annotation.RequiresApi
import network.xyo.client.account.model.AccountInstance
import network.xyo.client.address.XyoAccount
import network.xyo.client.datastore.XyoAccountPrefsRepository

open class DefaultXyoSdkSettings: SettingsInterface {
override val accountPreferences: AccountPreferences = DefaultAccountPreferences()

@RequiresApi(Build.VERSION_CODES.M)
override suspend fun getAccount(context: Context): XyoAccount? {
override suspend fun getAccount(context: Context): AccountInstance? {
val repository = XyoAccountPrefsRepository.getInstance(context)
return repository.getAccount()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package network.xyo.client.settings

import android.content.Context
import network.xyo.client.address.XyoAccount
import network.xyo.client.account.model.AccountInstance

interface SettingsInterface {
val accountPreferences: AccountPreferences
suspend fun getAccount(context: Context): XyoAccount?
suspend fun getAccount(context: Context): AccountInstance?
}

interface AccountPreferences {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ import kotlinx.coroutines.withContext
import network.xyo.app.xyo.sample.application.witness.WitnessHandlerInterface
import network.xyo.app.xyo.sample.application.witness.WitnessResult
import network.xyo.client.XyoPanel
import network.xyo.client.account.model.AccountInstance
import network.xyo.client.address.XyoAccount
import network.xyo.client.payload.XyoPayload

open class WitnessLocationHandler : WitnessHandlerInterface<List<XyoPayload?>> {
@RequiresApi(Build.VERSION_CODES.M)
override suspend fun witness(context: Context, nodeUrlsAndAccounts: ArrayList<Pair<String, XyoAccount?>>): WitnessResult<List<XyoPayload?>> {
override suspend fun witness(context: Context, nodeUrlsAndAccounts: ArrayList<Pair<String, AccountInstance?>>): WitnessResult<List<XyoPayload?>> {
val panel = XyoPanel(context, nodeUrlsAndAccounts, listOf(
XyoLocationWitness()
))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import android.util.Log
import androidx.annotation.RequiresApi
import network.xyo.client.DeferredObserver
import network.xyo.client.XyoWitness
import network.xyo.client.account.model.AccountInstance
import network.xyo.client.address.XyoAccount

class DeferredLocationObserver : DeferredObserver<XyoLocationPayload>() {
Expand All @@ -23,7 +24,7 @@ class DeferredLocationObserver : DeferredObserver<XyoLocationPayload>() {
}

@RequiresApi(Build.VERSION_CODES.M)
class XyoLocationWitness(address: XyoAccount = XyoAccount()) : XyoWitness<XyoLocationPayload>(
class XyoLocationWitness(address: AccountInstance = XyoAccount()) : XyoWitness<XyoLocationPayload>(
DeferredLocationObserver(),
"",
address
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import android.content.Context
import android.os.Build
import androidx.annotation.RequiresApi
import network.xyo.client.XyoWitness
import network.xyo.client.account.model.AccountInstance
import network.xyo.client.address.XyoAccount

@RequiresApi(Build.VERSION_CODES.M)
class XyoSystemInfoWitness(address: XyoAccount = XyoAccount()) : XyoWitness<XyoSystemInfoPayload>(
class XyoSystemInfoWitness(address: AccountInstance = XyoAccount()) : XyoWitness<XyoSystemInfoPayload>(
address,
fun (context: Context, _: String?): XyoSystemInfoPayload {
return XyoSystemInfoPayload.detect(context)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package network.xyo.app.xyo.sample.application.witness

import android.content.Context
import network.xyo.client.account.model.AccountInstance
import network.xyo.client.address.XyoAccount

interface WitnessHandlerInterface<out T> {
suspend fun witness(context: Context, nodeUrlsAndAccounts: ArrayList<Pair<String, XyoAccount?>>): WitnessResult<T>
suspend fun witness(context: Context, nodeUrlsAndAccounts: ArrayList<Pair<String, AccountInstance?>>): WitnessResult<T>
}

0 comments on commit 6b53bea

Please sign in to comment.