Skip to content

Commit

Permalink
Merge pull request #26 from XYOracleNetwork/feature/dollar-sign-meta-…
Browse files Browse the repository at this point in the history
…signatures

Feature/dollar sign meta signatures
  • Loading branch information
jonesmac authored Nov 26, 2024
2 parents 4141214 + b54ccc4 commit ceeaf35
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import network.xyo.client.datastore.previous_hash_store.PreviousHashStorePrefsRe
import network.xyo.client.lib.XyoSerializable
import network.xyo.client.node.client.NodeClient
import network.xyo.client.payload.XyoPayload
import org.json.JSONObject
import org.junit.Before
import org.junit.Rule
import org.junit.Test
Expand Down Expand Up @@ -71,15 +72,29 @@ class XyoBoundWitnessTest {
}

@Test
fun testBoundWitnessHash() {
fun testBoundWitnessMeta() {
runBlocking {
val bw = XyoBoundWitnessBuilder(appContext).signer(Account.random()).payloads(listOf(
TestPayload1()
)).build()
val hashableFields = bw.getBodyJson()
assert(bw._hash !== null)
assert(bw._hash!! == XyoSerializable.sha256String(hashableFields))
assert(bw._hash!! == hashableFields.hash())
assert(bw.rootHash() == XyoSerializable.sha256String(bw))
assert(bw.hash() == bw.getBodyJson().hash())
assert(bw.meta.client == "android")
assert(bw.meta.signatures?.size == 1)
}
}

@Test
fun testBoundWitnessMetaSerialization() {
runBlocking {
val bw = XyoBoundWitnessBuilder(appContext).signer(Account.random()).payloads(listOf(
TestPayload1()
)).build()
val serializedBw = XyoSerializable.toJson(bw)
val bwJson = JSONObject(serializedBw)
val meta = bwJson.get("\$meta") as JSONObject
assert(meta.get("client") == "android")
assertNotNull(meta.get("signatures"))
}
}

Expand All @@ -93,7 +108,7 @@ class XyoBoundWitnessTest {
val bw2 = XyoBoundWitnessBuilder(appContext).signer(testAccount).payloads(listOf(
TestPayload1()
)).build()
assert(bw2.previous_hashes.first() == bw._hash)
assert(bw2.previous_hashes.first() == bw.hash())
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class XyoSdkTest {
val bw = result.bw

val result2 = panel.reportAsyncQuery()
assert(result2.bw.previous_hashes.contains(bw._hash))
assert(result2.bw.previous_hashes.contains(bw.hash()))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import kotlinx.coroutines.runBlocking
import network.xyo.client.lib.TestConstants
import network.xyo.client.account.Account
import network.xyo.client.boundwitness.XyoBoundWitnessBodyJson
import network.xyo.client.boundwitness.XyoBoundWitnessJson
import network.xyo.client.datastore.previous_hash_store.PreviousHashStorePrefsRepository
import network.xyo.client.witness.types.WitnessResult
import network.xyo.client.payload.XyoPayload
Expand Down Expand Up @@ -48,10 +49,10 @@ class WitnessLocationHandlerTest {
@Test
fun testObserve() {
runBlocking {
var firstBw: XyoBoundWitnessBodyJson? = null
var firstBw: XyoBoundWitnessJson? = null
val result1 = WitnessLocationHandler().witness(appContext.applicationContext, arrayListOf(Pair(apiDomainBeta, null)))
when (result1) {
is WitnessResult.Success<Triple<XyoBoundWitnessBodyJson?, XyoPayload?, XyoPayload?>> -> {
is WitnessResult.Success<Triple<XyoBoundWitnessJson?, XyoPayload?, XyoPayload?>> -> {
firstBw = result1.data.first
assertInstanceOf<XyoBoundWitnessBodyJson>(firstBw)
assertInstanceOf<XyoLocationPayload>(result1.data.second)
Expand All @@ -62,12 +63,12 @@ class WitnessLocationHandlerTest {
}
}

var secondBw: XyoBoundWitnessBodyJson? = null
var secondBw: XyoBoundWitnessJson? = null
val result2 = WitnessLocationHandler().witness(appContext.applicationContext, arrayListOf(Pair(apiDomainBeta, null)))
when (result2) {
is WitnessResult.Success<Triple<XyoBoundWitnessBodyJson?, XyoPayload?, XyoPayload?>> -> {
is WitnessResult.Success<Triple<XyoBoundWitnessJson?, XyoPayload?, XyoPayload?>> -> {
secondBw = result2.data.first
assertInstanceOf<XyoBoundWitnessBodyJson>(secondBw)
assertInstanceOf<XyoBoundWitnessJson>(secondBw)
assertInstanceOf<XyoLocationPayload>(result2.data.second)
assertInstanceOf<XyoLocationPayloadRaw>(result2.data.third)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ class XyoPanelTest {
val bw = result.bw

val result2 = panel.reportAsyncQuery()
assert(result2.bw.previous_hashes.contains(bw._hash))
assert(result2.bw.previous_hashes.contains(bw.hash()))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package network.xyo.client.boundwitness
import com.squareup.moshi.JsonClass

@JsonClass(generateAdapter = true)
open class QueryBoundWitnessJson: XyoBoundWitnessJson(), XyoBoundWitnessMetaInterface {
open class QueryBoundWitnessJson: XyoBoundWitnessJson() {
var query: String? = null

// override to return a bound witness json body that has query in its hashable fields
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ open class XyoBoundWitnessBuilder(private val context: Context) {
bw.addresses = addresses

// update underscore fields
bw._client = "android"
bw.meta.client = "android"

// construct fields involved in hashing
constructHashableFieldsFields()
Expand All @@ -92,8 +92,7 @@ open class XyoBoundWitnessBuilder(private val context: Context) {
// in the serialized version of the bw because they will invalidate the hash
val hashable = hashableFields()
val hash = XyoSerializable.sha256String(hashable)
bw._signatures = this.sign(hash)
bw._hash = hash
bw.meta.signatures = this.sign(hash)
}

open suspend fun build(): XyoBoundWitnessJson {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,33 @@
package network.xyo.client.boundwitness

import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass

@JsonClass(generateAdapter = true)
open class XyoBoundWitnessJson: XyoBoundWitnessBodyJson(), XyoBoundWitnessMetaInterface {
override var _signatures: List<String>? = null
override var _client: String? = null
override var _hash: String? = null
class XyoBoundWitnessMeta: XyoBoundWitnessMetaInterface {
override var signatures: List<String>? = null
override var client: String? = null
}

@JsonClass(generateAdapter = true)
open class XyoBoundWitnessJson: XyoBoundWitnessBodyJson() {
@Json(ignore = true)
val _meta: XyoBoundWitnessMeta = XyoBoundWitnessMeta()

@Json(name = "\$meta")
var meta: XyoBoundWitnessMeta
get() = _meta
set(value) = Unit

override fun hash(): String {
return getBodyJson().hash()
}

fun rootHash(): String {
return sha256String(this)
}

open fun getBodyJson(): XyoBoundWitnessBodyJson {
return this
return XyoBoundWitnessBodyJson(this.addresses, this.previous_hashes, this.payload_hashes, this.payload_schemas, this.timestamp)
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package network.xyo.client.boundwitness

import network.xyo.client.payload.Payload

interface XyoBoundWitnessMetaInterface : Payload {
var _hash: String?
var _signatures: List<String>?
var _client: String?
interface XyoBoundWitnessMetaInterface {
var signatures: List<String>?
var client: String?
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import network.xyo.client.witness.types.WitnessHandlerInterface
import network.xyo.client.witness.types.WitnessResult
import network.xyo.client.witness.XyoPanel
import network.xyo.client.account.model.AccountInstance
import network.xyo.client.boundwitness.XyoBoundWitnessBodyJson
import network.xyo.client.boundwitness.XyoBoundWitnessJson
import network.xyo.client.payload.XyoPayload
import network.xyo.client.settings.XyoSdk

open class WitnessLocationHandler : WitnessHandlerInterface<Triple<XyoBoundWitnessBodyJson?, XyoPayload?, XyoPayload?>> {
open class WitnessLocationHandler : WitnessHandlerInterface<Triple<XyoBoundWitnessJson?, XyoPayload?, XyoPayload?>> {
@RequiresApi(Build.VERSION_CODES.M)
override suspend fun witness(context: Context, nodeUrlsAndAccounts: ArrayList<Pair<String, AccountInstance?>>): WitnessResult<Triple<XyoBoundWitnessBodyJson?, XyoPayload?, XyoPayload?>> {
override suspend fun witness(context: Context, nodeUrlsAndAccounts: ArrayList<Pair<String, AccountInstance?>>): WitnessResult<Triple<XyoBoundWitnessJson?, XyoPayload?, XyoPayload?>> {
val account = XyoSdk.getInstance(context.applicationContext).getAccount()
val panel = XyoPanel(context, account, nodeUrlsAndAccounts, listOf(
XyoLocationWitness(account)
Expand All @@ -26,11 +26,11 @@ open class WitnessLocationHandler : WitnessHandlerInterface<Triple<XyoBoundWitne

@OptIn(ExperimentalCoroutinesApi::class)
@RequiresApi(Build.VERSION_CODES.M)
private suspend fun getLocation(panel: XyoPanel): WitnessResult<Triple<XyoBoundWitnessBodyJson?, XyoPayload?, XyoPayload?>> {
private suspend fun getLocation(panel: XyoPanel): WitnessResult<Triple<XyoBoundWitnessJson?, XyoPayload?, XyoPayload?>> {
return withContext(Dispatchers.IO) {
var locationPayload: XyoPayload? = null
var locationPayloadRaw: XyoPayload? = null
var bw: XyoBoundWitnessBodyJson? = null
var bw: XyoBoundWitnessJson? = null
val errors: MutableList<Error> = mutableListOf()
panel.let {
it.reportAsyncQuery().let { result ->
Expand Down

0 comments on commit ceeaf35

Please sign in to comment.