Skip to content

Commit

Permalink
Private key usage updated
Browse files Browse the repository at this point in the history
  • Loading branch information
arietrouw committed Dec 3, 2024
1 parent 26ce067 commit 60eb9b7
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ class AccountTest {
fun testRandomAccount() {
val account = Account.random()
assert(account.privateKey.count() == 32)
assert(account.publicKey.count() == 64)
assert(account.publicKey.count() == 33)
assert(account.publicKeyUncompressed.count() == 64)
}

@OptIn(ExperimentalStdlibApi::class)
Expand All @@ -34,8 +35,9 @@ class AccountTest {
runBlocking {
val account = Account.fromPrivateKey(hexStringToByteArray(testVectorPrivateKey))
assert(account.privateKey.count() == 32)
assert(account.publicKey.count() == 64)
assert(account.publicKey.toHexString() == testVectorPublicKey)
assert(account.publicKey.count() == 33)
assert(account.publicKeyUncompressed.count() == 64)
assert(account.publicKeyUncompressed.toHexString() == testVectorPublicKey)
assert(account.address.toHexString() == testVectorAddress)
val signature = account.sign(hexStringToByteArray(testVectorHash))
assert(signature.toHexString() == testVectorSignature)
Expand All @@ -51,7 +53,7 @@ class AccountTest {
val account = Account.fromPrivateKey(address)
account.sign(hexStringToByteArray(testVectorHash))

val savedAddressInStore = Account.addressFromPublicKey(account.publicKey)
val savedAddressInStore = Account.addressFromUncompressedPublicKey(account.publicKeyUncompressed)
val previousHashInStore = Account.previousHashStore?.getItem(savedAddressInStore)?.toHexString()
assert(previousHashInStore == testVectorHash)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,4 @@ class WalletTest {
Log.i("privateKey", wallet.privateKey.toHexString())
assert(wallet.privateKey.toHexString() == privateKeyVectors[1])
}

@OptIn(ExperimentalStdlibApi::class)
@Test
fun testWalletWithoutPath() {
val wallet = Wallet.fromMnemonic(words)
Log.i("privateKey", wallet.privateKey.toHexString())
assert(wallet.privateKey.toHexString() == privateKeyVectors[1])
}
}
5 changes: 3 additions & 2 deletions 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(publicKeyUncompressed)
private val _address = addressFromUncompressedPublicKey(publicKeyUncompressed)

final override val address: ByteArray
get() = _address
Expand Down Expand Up @@ -60,7 +60,8 @@ open class Account private constructor (private val _privateKey: PrivateKey, pri
return fromPrivateKey(generatePrivateKeyBytes())
}

fun addressFromPublicKey(key: ByteArray): ByteArray {
fun addressFromUncompressedPublicKey(key: ByteArray): ByteArray {
assert(key.size == 64, ) { "Invalid Key Length" }
val publicKeyHash = toKeccak(key)
return publicKeyHash.copyOfRange(12, publicKeyHash.size)
}
Expand Down
4 changes: 3 additions & 1 deletion sdk/src/main/java/network/xyo/client/account/Wallet.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ open class Wallet(private val _extKey: ExtKey, previousHash: ByteArray? = null):

companion object: WalletStatic<WalletInstance> {

val defaultPath = "m/44'/60'/0'/0/0"

override var previousHashStore: PreviousHashStore? = null

override fun fromExtendedKey(key: ExtKey): WalletInstance {
Expand All @@ -34,7 +36,7 @@ open class Wallet(private val _extKey: ExtKey, previousHash: ByteArray? = null):
override fun fromMnemonic(mnemonic: MnemonicWords, path: String?): WalletInstance {
val root = fromSeed(mnemonic.toSeed("".toCharArray()))
return if (path === null) {
root.derivePath("m/44'/0'/0'/0/0")
root.derivePath(defaultPath)
} else {
root.derivePath(path)
}
Expand Down

0 comments on commit 60eb9b7

Please sign in to comment.