Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integrate updated HDWallet #260

Merged
merged 1 commit into from
Oct 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ dependencies {

implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"

implementation 'com.github.horizontalsystems:hd-wallet-kit-android:ff1e364'
implementation 'com.github.horizontalsystems:hd-wallet-kit-android:a450abb'

implementation 'androidx.appcompat:appcompat:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.2'
Expand Down
2 changes: 1 addition & 1 deletion ethereumkit/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ dependencies {

implementation 'org.bouncycastle:bcpkix-jdk15on:1.65'

implementation 'com.github.horizontalsystems:hd-wallet-kit-android:ff1e364'
implementation 'com.github.horizontalsystems:hd-wallet-kit-android:a450abb'

implementation 'io.reactivex.rxjava2:rxjava:2.2.19'
implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import io.horizontalsystems.ethereumkit.api.jsonrpc.models.RpcTransactionReceipt
import io.horizontalsystems.ethereumkit.api.models.AccountState
import io.horizontalsystems.ethereumkit.api.models.EthereumKitState
import io.horizontalsystems.ethereumkit.api.storage.ApiStorage
import io.horizontalsystems.ethereumkit.core.signer.Signer
import io.horizontalsystems.ethereumkit.core.storage.Eip20Storage
import io.horizontalsystems.ethereumkit.core.storage.TransactionStorage
import io.horizontalsystems.ethereumkit.core.storage.TransactionSyncerStateStorage
Expand All @@ -25,7 +26,6 @@ import io.horizontalsystems.ethereumkit.network.*
import io.horizontalsystems.ethereumkit.transactionsyncers.EthereumTransactionSyncer
import io.horizontalsystems.ethereumkit.transactionsyncers.InternalTransactionSyncer
import io.horizontalsystems.ethereumkit.transactionsyncers.TransactionSyncManager
import io.horizontalsystems.hdwalletkit.HDWallet
import io.horizontalsystems.hdwalletkit.Mnemonic
import io.reactivex.BackpressureStrategy
import io.reactivex.Flowable
Expand Down Expand Up @@ -358,7 +358,7 @@ class EthereumKit(
walletId: String
): EthereumKit {
val seed = Mnemonic().toSeed(words, passphrase)
val privateKey = privateKey(seed, chain)
val privateKey = Signer.privateKey(seed, chain)
val address = ethereumAddress(privateKey)
return getInstance(application, address, chain, rpcSource, transactionSource, walletId)
}
Expand Down Expand Up @@ -434,11 +434,6 @@ class EthereumKit(
return ethereumKit
}

fun privateKey(seed: ByteArray, chain: Chain): BigInteger {
val hdWallet = HDWallet(seed, chain.coinType)
return hdWallet.privateKey(0, 0, true).privKey
}

fun clear(context: Context, chain: Chain, walletId: String) {
EthereumDatabaseManager.clear(context, chain, walletId)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ package io.horizontalsystems.ethereumkit.core.signer

import io.horizontalsystems.ethereumkit.core.TransactionBuilder
import io.horizontalsystems.ethereumkit.core.TransactionSigner
import io.horizontalsystems.ethereumkit.core.hexStringToByteArrayOrNull
import io.horizontalsystems.ethereumkit.core.signer.Signer.PrivateKeyValidationError.InvalidDataLength
import io.horizontalsystems.ethereumkit.core.signer.Signer.PrivateKeyValidationError.InvalidDataString
import io.horizontalsystems.ethereumkit.crypto.CryptoUtils
import io.horizontalsystems.ethereumkit.crypto.EIP712Encoder
import io.horizontalsystems.ethereumkit.crypto.TypedData
import io.horizontalsystems.ethereumkit.models.*
import io.horizontalsystems.ethereumkit.spv.core.toBigInteger
import io.horizontalsystems.hdwalletkit.HDWallet
import io.horizontalsystems.hdwalletkit.Mnemonic
import java.math.BigInteger
Expand Down Expand Up @@ -57,9 +61,8 @@ class Signer(
}

companion object {
fun getInstance(seed: ByteArray, chain: Chain): Signer {
val privateKey = privateKey(seed, chain)
val address = ethereumAddress(privateKey)
fun getInstance(privateKey: BigInteger, chain: Chain): Signer {
val address = address(privateKey)

val transactionSigner = TransactionSigner(privateKey, chain.id)
val transactionBuilder = TransactionBuilder(address, chain.id)
Expand All @@ -68,6 +71,10 @@ class Signer(
return Signer(transactionBuilder, transactionSigner, ethSigner)
}

fun getInstance(seed: ByteArray, chain: Chain): Signer {
return getInstance(privateKey(seed, chain), chain)
}

fun address(
words: List<String>,
passphrase: String = "",
Expand All @@ -78,7 +85,7 @@ class Signer(

fun address(seed: ByteArray, chain: Chain): Address {
val privateKey = privateKey(seed, chain)
return ethereumAddress(privateKey)
return address(privateKey)
}

fun privateKey(
Expand All @@ -89,16 +96,31 @@ class Signer(
return privateKey(Mnemonic().toSeed(words, passphrase), chain)
}

@Throws(InvalidDataString::class, InvalidDataLength::class)
fun privateKey(value: String): BigInteger {
val data = value.hexStringToByteArrayOrNull()
?: throw InvalidDataString()
if (data.size != 32) {
throw InvalidDataLength()
}
return data.toBigInteger()
}

fun privateKey(seed: ByteArray, chain: Chain): BigInteger {
val hdWallet = HDWallet(seed, chain.coinType)
val hdWallet = HDWallet(seed, chain.coinType, HDWallet.Purpose.BIP44)
return hdWallet.privateKey(0, 0, true).privKey
}

fun ethereumAddress(privateKey: BigInteger): Address {
fun address(privateKey: BigInteger): Address {
val publicKey =
CryptoUtils.ecKeyFromPrivate(privateKey).publicKeyPoint.getEncoded(false).drop(1)
.toByteArray()
return Address(CryptoUtils.sha3(publicKey).takeLast(20).toByteArray())
}
}

open class PrivateKeyValidationError : Exception() {
class InvalidDataString : PrivateKeyValidationError()
class InvalidDataLength : PrivateKeyValidationError()
}
}