Skip to content

Commit

Permalink
Some De-Smurfing
Browse files Browse the repository at this point in the history
  • Loading branch information
arietrouw committed Dec 6, 2024
1 parent bbda03b commit c26ee7b
Show file tree
Hide file tree
Showing 22 changed files with 379 additions and 63 deletions.
6 changes: 5 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ buildscript {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:8.7.2'
classpath 'com.android.tools.build:gradle:8.7.3'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.9.24"
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1'
classpath("de.mannodermaus.gradle.plugins:android-junit5:1.11.0.0")
Expand All @@ -15,6 +15,10 @@ buildscript {
}
}

plugins {
id 'org.jetbrains.kotlin.plugin.serialization' version '1.7.10' apply false
}

allprojects {
repositories {
google()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import network.xyo.client.account.Account
import network.xyo.client.account.Wallet
import network.xyo.client.account.model.AccountInstance
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.Assertions.*

@OptIn(ExperimentalStdlibApi::class)
class BoundWitnessBuilderTest {

@BeforeEach
fun setUp() {
// Ensure previousHash = null for test accounts
Account.previousHashStore = MemoryPreviousHashStore()
}

@Test
fun build_returns_expected_hash() {
for (testCase in boundWitnessSequenceTestCases) {
// Create accounts
val signers = mutableListOf<AccountInstance>()
for ((i, mnemonic) in testCase.mnemonics.withIndex()) {
val path = testCase.paths[i]
val account = try {
Wallet.fromMnemonic(mnemonic, path)
} catch (e: Exception) {
null
}
if (account != null) {
signers.add(account)
} else {
fail("Error creating account from mnemonic")
}
}

assertEquals(
testCase.addresses.size,
signers.size,
"Incorrect number of accounts created."
)

val expectedAddresses = testCase.addresses.map { it.lowercase() }
val actualAddresses = signers.map { it.address.toHexString().lowercase() }
assertEquals(
expectedAddresses,
actualAddresses,
"Incorrect addresses when creating accounts."
)

/*
// Ensure correct initial account state
for ((i, expectedPreviousHash) in testCase.previousHashes.withIndex()) {
val signer = signers.getOrNull(i)
assertNotNull(signer, "Signer at index $i should not be null.")
assertEquals(
expectedPreviousHash,
signer.previousHash,
"Incorrect previous hash for account at index $i"
)
}
// Build the Bound Witness (BW)
val bw = BoundWitnessBuilder()
.signers(signers)
.payloads(testCase.payloads)
val (bwJson, _) = bw.build()
val hash = PayloadBuilder.dataHash(bwJson.typedPayload)
val rootHash = PayloadBuilder.hash(bwJson.typedPayload)
// Ensure the BW is correct
assertEquals(
testCase.dataHash,
hash.toHex(),
"Incorrect data hash in BW"
)
for ((i, expectedPayloadHash) in testCase.payloadHashes.withIndex()) {
val actualPayloadHash = bwJson.typedPayload.payload_hashes.getOrNull(i)
assertNotNull(actualPayloadHash, "Payload hash at index $i should not be null.")
assertEquals(
expectedPayloadHash,
actualPayloadHash,
"Incorrect payload hash in BW at index $i"
)
}
for ((i, payload) in testCase.payloads.withIndex()) {
val actualSchema = bwJson.typedPayload.payload_schemas.getOrNull(i)
assertNotNull(actualSchema, "Payload schema at index $i should not be null.")
assertEquals(
payload.schema,
actualSchema,
"Incorrect payload schema in BW at index $i"
)
}
// Ensure correct ending account state
for (signer in signers) {
assertEquals(
hash.toHex(),
signer.previousHash,
"Incorrect previous hash for account ${signer.address}"
)
}
*/
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
import network.xyo.client.payload.Payload

class IdPayload(val salt: String): Payload {
constructor(salt: UInt) : this("${salt}")
override var schema = "network.xyo.id"
}

data class BoundWitnessSequenceTestCase(
val mnemonics: List<String>,
val paths: List<String>,
val addresses: List<String>,
val payloads: List<Payload>,
val payloadHashes: List<String>,
val previousHashes: List<String?>,
val dataHash: String
)

// Define the PayloadsWithHashes data class
data class PayloadsWithHashes(
val payloads: List<Payload>,
val payloadHashes: List<String>
)

// Initialize payload sequences
val payloadSequences: List<PayloadsWithHashes> = listOf(
PayloadsWithHashes(
payloads = listOf(IdPayload(0u)),
payloadHashes = listOf(
"ada56ff753c0c9b2ce5e1f823eda9ac53501db2843d8883d6cf6869c18ef7f65"
)
),
PayloadsWithHashes(
payloads = listOf(IdPayload(1u)),
payloadHashes = listOf(
"3a3b8deca568ff820b0b7c8714fbdf82b40fb54f4b15aca8745e06b81291558e"
)
),
PayloadsWithHashes(
payloads = listOf(IdPayload(2u), IdPayload(3u)),
payloadHashes = listOf(
"1a40207fab71fc184e88557d5bee6196cbbb49f11f73cda85000555a628a8f0a",
"c4bce9b4d3239fcc9a248251d1bef1ba7677e3c0c2c43ce909a6668885b519e6"
)
),
PayloadsWithHashes(
payloads = listOf(IdPayload(4u), IdPayload(5u)),
payloadHashes = listOf(
"59c0374dd801ae64ddddba27320ca028d7bd4b3d460f6674c7da1b4aa9c956d6",
"5d9b8e84bc824280fcbb6290904c2edbb401d626ad9789717c0a23d1cab937b0"
)
)
)

// Define wallet mnemonics, paths, and addresses
const val wallet1Mnemonic =
"report door cry include salad horn recipe luxury access pledge husband maple busy double olive"
const val wallet1Path = "m/44'/60'/0'/0/0"
const val wallet1Address = "25524Ca99764D76CA27604Bb9727f6e2f27C4533"

const val wallet2Mnemonic =
"turn you orphan sauce act patient village entire lava transfer height sense enroll quit idle"
const val wallet2Path = "m/44'/60'/0'/0/0"
const val wallet2Address = "FdCeD2c3549289049BeBf743fB721Df211633fBF"

// Initialize BoundWitnessSequenceTestCase instances
val boundWitnessSequenceTestCase1 = BoundWitnessSequenceTestCase(
mnemonics = listOf(wallet1Mnemonic),
paths = listOf(wallet1Path),
addresses = listOf(wallet1Address),
payloads = payloadSequences[0].payloads,
payloadHashes = payloadSequences[0].payloadHashes,
previousHashes = listOf(null),
dataHash = "750113b9826ba94b622667b06cd8467f1330837581c28907c16160fec20d0a4b"
)

val boundWitnessSequenceTestCase2 = BoundWitnessSequenceTestCase(
mnemonics = listOf(wallet2Mnemonic),
paths = listOf(wallet2Path),
addresses = listOf(wallet2Address),
payloads = payloadSequences[1].payloads,
payloadHashes = payloadSequences[1].payloadHashes,
previousHashes = listOf(null),
dataHash = "bacd010d79126a154339e59c11c5b46be032c3bef65626f83bcafe968dc6dd1b"
)

val boundWitnessSequenceTestCase3 = BoundWitnessSequenceTestCase(
mnemonics = listOf(wallet1Mnemonic, wallet2Mnemonic),
paths = listOf(wallet1Path, wallet2Path),
addresses = listOf(wallet1Address, wallet2Address),
payloads = payloadSequences[2].payloads,
payloadHashes = payloadSequences[2].payloadHashes,
previousHashes = listOf(
"750113b9826ba94b622667b06cd8467f1330837581c28907c16160fec20d0a4b",
"bacd010d79126a154339e59c11c5b46be032c3bef65626f83bcafe968dc6dd1b"
),
dataHash = "73245ef73517913f4b57c12d56d81199968ecd8fbefea9ddc474f43dd6cfa8c8"
)

val boundWitnessSequenceTestCase4 = BoundWitnessSequenceTestCase(
mnemonics = listOf(wallet1Mnemonic, wallet2Mnemonic),
paths = listOf(wallet1Path, wallet2Path),
addresses = listOf(wallet1Address, wallet2Address),
payloads = payloadSequences[3].payloads,
payloadHashes = payloadSequences[3].payloadHashes,
previousHashes = listOf(
"73245ef73517913f4b57c12d56d81199968ecd8fbefea9ddc474f43dd6cfa8c8",
"73245ef73517913f4b57c12d56d81199968ecd8fbefea9ddc474f43dd6cfa8c8"
),
dataHash = "210d86ea43d82b85a49b77959a8ee4e6016ff7036254cfa39953befc66073010"
)

// Aggregate all test cases into a list
val boundWitnessSequenceTestCases = listOf(
boundWitnessSequenceTestCase1,
boundWitnessSequenceTestCase2,
boundWitnessSequenceTestCase3,
boundWitnessSequenceTestCase4
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import network.xyo.client.payload.Payload

class TestPayload1SubObject {
var number_value = 2
var string_value = "yo"
}

class TestPayload1: Payload {
override var schema ="network.xyo.test"
var timestamp = 1_618_603_439_107
var number_field = 1
var object_field = TestPayload1SubObject()
var string_field = "there"
}

val testPayload1 = TestPayload1()
val testPayload1Hash: String = "c915c56dd93b5e0db509d1a63ca540cfb211e11f03039b05e19712267bb8b6db"
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import network.xyo.client.payload.Payload

class TestPayload2SubObject {
var number_value = 2
var string_value = "yo"
var optional_field: String? = null
}

class TestPayload2: Payload {
override var schema ="network.xyo.test"
var timestamp = 1_618_603_439_107
var object_field = TestPayload2SubObject()
var string_field = "there"
var number_field = 1
}

val testPayload2 = TestPayload2()
val testPayload2Hash: String = "c915c56dd93b5e0db509d1a63ca540cfb211e11f03039b05e19712267bb8b6db"
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import network.xyo.client.account.Account
import network.xyo.client.archivist.wrapper.ArchivistWrapper
import network.xyo.client.node.client.DiscoverPayload
import network.xyo.client.datastore.previous_hash_store.PreviousHashStorePrefsRepository
import network.xyo.client.lib.XyoSerializable
import network.xyo.client.lib.JsonSerializable
import network.xyo.client.node.client.NodeClient
import network.xyo.client.payload.XyoPayload
import org.json.JSONObject
Expand Down Expand Up @@ -75,7 +75,7 @@ class XyoBoundWitnessTest {
@Test
fun testBoundWitnessMeta() {
runBlocking {
val bw = XyoBoundWitnessBuilder(appContext).signer(Account.random()).payloads(listOf(
val bw = BoundWitnessBuilder().signer(Account.random()).payloads(listOf(
TestPayload1()
)).build()
assert(bw.dataHash() == bw.getBodyJson().dataHash())
Expand All @@ -87,10 +87,10 @@ class XyoBoundWitnessTest {
@Test
fun testBoundWitnessMetaSerialization() {
runBlocking {
val bw = XyoBoundWitnessBuilder(appContext).signer(Account.random()).payloads(listOf(
val bw = BoundWitnessBuilder().signer(Account.random()).payloads(listOf(
TestPayload1()
)).build()
val serializedBw = XyoSerializable.toJson(bw)
val serializedBw = JsonSerializable.toJson(bw)
val bwJson = JSONObject(serializedBw)
val meta = bwJson.get("\$meta") as JSONObject
assert(meta.get("client") == "android")
Expand All @@ -102,10 +102,10 @@ class XyoBoundWitnessTest {
fun testBoundWitnessPreviousHash() {
runBlocking {
val testAccount = Account.random()
val bw = XyoBoundWitnessBuilder(appContext).signer(testAccount).payloads(listOf(
val bw = BoundWitnessBuilder().signer(testAccount).payloads(listOf(
TestPayload1()
)).build()
val bw2 = XyoBoundWitnessBuilder(appContext).signer(testAccount).payloads(listOf(
val bw2 = BoundWitnessBuilder().signer(testAccount).payloads(listOf(
TestPayload1()
)).build()
assert(bw2.previous_hashes.first() == bw.dataHash())
Expand All @@ -118,7 +118,7 @@ class XyoBoundWitnessTest {
val client = ArchivistWrapper(NodeClient("$apiDomainBeta/Archivist", null, appContext))
val testAccount = Account.random()
val testPayload = TestPayload1()
val bw = XyoBoundWitnessBuilder(appContext).signer(testAccount).payloads(listOf(testPayload)).build()
val bw = BoundWitnessBuilder().signer(testAccount).payloads(listOf(testPayload)).build()
client.insert(listOf(bw, testPayload))

val bwHash = bw.dataHash()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import kotlinx.coroutines.launch
import network.xyo.client.lib.BasicPayload
import network.xyo.client.lib.TestConstants
import network.xyo.client.account.Account
import network.xyo.client.boundwitness.XyoBoundWitnessBuilder
import network.xyo.client.lib.XyoSerializable
import network.xyo.client.boundwitness.BoundWitnessBuilder
import network.xyo.client.lib.JsonSerializable
import network.xyo.client.witness.XyoWitness
import org.junit.Before
import org.junit.Rule
Expand Down Expand Up @@ -80,12 +80,12 @@ class XyoPayloadTest {
@Test
fun testRoundTripPayload() {
val payload = TestPayload1()
val payloadJsonString = XyoSerializable.toJson(payload)
val payloadJsonString = JsonSerializable.toJson(payload)

// ensure the schema is properly serialized from the base class
assert(payloadJsonString.contains("network.xyo.test"))

val payloadMirrored = XyoSerializable.fromJson(payloadJsonString, TestPayload1())
val payloadMirrored = JsonSerializable.fromJson(payloadJsonString, TestPayload1())
assertNotNull(payloadMirrored)
if (payloadMirrored != null) {
assertEquals(payload.schema, payloadMirrored.schema)
Expand All @@ -103,12 +103,12 @@ class XyoPayloadTest {
CoroutineScope(Dispatchers.Main).launch {
val response = arrayListOf(witness.observe(appContext))
val payloads = response.mapNotNull { payload -> payload }.flatten()
val bwJson = XyoBoundWitnessBuilder(appContext)
val bwJson = BoundWitnessBuilder()
.payloads(payloads)
.signer(Account.random())
.build()
val bwJsonString = XyoSerializable.toJson(bwJson)
val bwMirrored = XyoSerializable.fromJson(bwJsonString, bwJson)
val bwJsonString = JsonSerializable.toJson(bwJson)
val bwMirrored = JsonSerializable.fromJson(bwJsonString, bwJson)
assertNotNull(bwMirrored)
}
}
Expand Down
Loading

0 comments on commit c26ee7b

Please sign in to comment.