Skip to content

Commit

Permalink
Test Vectors for Wallet - Made Public address compressed
Browse files Browse the repository at this point in the history
  • Loading branch information
arietrouw committed Dec 3, 2024
1 parent 684f97e commit 26ce067
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package network.xyo.client.account

import android.util.Log
import org.junit.Test

data class TestVector(
val path: String,
val address: String,
val privateKey: String,
val publicKey: String
) {}

class WalletTestVectors {
val phrase = "later puppy sound rebuild rebuild noise ozone amazing hope broccoli crystal grief"
val testVectors = arrayOf(
TestVector(
"m/44'/60'/0'/0/0",
"e46c258c74c7c1df33d7caa4c2c664dc0843ab3f",
"96a7705eedbb701a03ee235911253fd3eb80e48a06106c0bf957d42b72bd8efa",
"03a9f10779cb44e73a1983b8225ce9de96ff63cbc8a2900db102fa55a38a14b206"))

@OptIn(ExperimentalStdlibApi::class)
@Test
fun testWallet() {
val index = 0
val vector = this.testVectors[index]
val wallet = Wallet.fromMnemonic(phrase, vector.path)
Log.i("privateKey", wallet.privateKey.toHexString())
val calcPrivateKey = wallet.privateKey.toHexString()
val calcPublicKey = wallet.publicKey.toHexString()
val calcAddress = wallet.address.toHexString()
assert(calcPrivateKey == vector.privateKey)
assert(calcPublicKey == vector.publicKey)
assert(calcAddress == vector.address)
}
}
4 changes: 3 additions & 1 deletion sdk/src/main/java/network/xyo/client/account/Account.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ open class Account private constructor (private val _privateKey: PrivateKey, pri
constructor(privateKey: ByteArray, previousHash: ByteArray? = null) : this(PrivateKey.fromBytes(privateKey, secp256k1Curve), previousHash) {}
constructor(privateKey: BigInteger, previousHash: ByteArray? = null) : this(privateKey.toByteArray(), previousHash) {}

private val _address = addressFromPublicKey(publicKey)
private val _address = addressFromPublicKey(publicKeyUncompressed)

final override val address: ByteArray
get() = _address
Expand All @@ -27,6 +27,8 @@ open class Account private constructor (private val _privateKey: PrivateKey, pri
final override val privateKey: ByteArray
get() = _privateKey.key.toBytesPadded(32)
final override val publicKey: ByteArray
get() = _privateKey.toPublicKey().compressed()
final override val publicKeyUncompressed: ByteArray
get() = _privateKey.toPublicKey().key.toBytesPadded(64)

override suspend fun sign(hash: ByteArray): ByteArray {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ interface AccountInstance: PrivateKeyInstance {
val previousHash: ByteArray?
val privateKey: ByteArray
val publicKey: ByteArray
val publicKeyUncompressed: ByteArray
}

0 comments on commit 26ce067

Please sign in to comment.