Skip to content

Commit

Permalink
prevent external files from calling sha256 hash method
Browse files Browse the repository at this point in the history
  • Loading branch information
jonesmac committed Nov 26, 2024
1 parent e014902 commit 684f97e
Show file tree
Hide file tree
Showing 6 changed files with 8 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ class XyoBoundWitnessTest {
val bw = XyoBoundWitnessBuilder(appContext).signer(Account.random()).payloads(listOf(
TestPayload1()
)).build()
assert(bw.rootHash() == XyoSerializable.sha256String(bw))
assert(bw.dataHash() == bw.getBodyJson().dataHash())
assert(bw.meta.client == "android")
assert(bw.meta.signatures?.size == 1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class LocationWitnessTest {
val panel = XyoPanel(appContext, Account.random(), arrayListOf(Pair("${TestConstants.nodeUrlBeta}/Archivist", null)), listOf(XyoLocationWitness()))
val result = panel.reportAsyncQuery()
result.payloads?.forEach{ payload ->
val hash = XyoSerializable.sha256String(payload)
val hash = payload.dataHash()
assert(result.bw.payload_hashes.contains(hash))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class QueryBoundWitnessBuilder(context: Context) : XyoBoundWitnessBuilder(contex
private lateinit var queryHash: String

fun query(query: XyoPayload): QueryBoundWitnessBuilder {
this.queryHash = XyoSerializable.sha256String(query)
this.queryHash = query.dataHash()
this.payload(query.schema, query)
return this
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ open class XyoBoundWitnessBuilder(private val context: Context) {
// Note: Once fields are hashed, do not update class properties that are expected
// in the serialized version of the bw because they will invalidate the hash
val hashable = hashableFields()
val hash = XyoSerializable.sha256String(hashable)
val hash = hashable.dataHash()
bw.meta.signatures = this.sign(hash)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package network.xyo.client.boundwitness

import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass
import network.xyo.client.lib.XyoSerializable

@JsonClass(generateAdapter = true)
class XyoBoundWitnessMeta: XyoBoundWitnessMetaInterface {
class XyoBoundWitnessMeta: XyoBoundWitnessMetaInterface, XyoSerializable() {
override var signatures: List<String>? = null
override var client: String? = null
}
Expand Down
5 changes: 3 additions & 2 deletions sdk/src/main/java/network/xyo/client/lib/XyoSerializable.kt
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,13 @@ abstract class XyoSerializable: Serializable {
return md.digest()
}

fun <T: XyoSerializable>sha256(obj: T): ByteArray {
protected fun <T: XyoSerializable>sha256(obj: T): ByteArray {
val json = toJson(obj, true)
return sha256(json)
}

fun <T: XyoSerializable>sha256String(obj: T): String {
@JvmStatic
protected fun <T: XyoSerializable>sha256String(obj: T): String {
val shaBytes = sha256(obj)
return bytesToHex(shaBytes)
}
Expand Down

0 comments on commit 684f97e

Please sign in to comment.