Skip to content

Commit

Permalink
Add Ktlint to common-bitcoin, common-ethereum, debug, and `euro…
Browse files Browse the repository at this point in the history
…token`
  • Loading branch information
InvictusRMC committed Feb 19, 2024
1 parent 69347c3 commit 655ef2c
Show file tree
Hide file tree
Showing 36 changed files with 647 additions and 396 deletions.
9 changes: 9 additions & 0 deletions common-bitcoin/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
plugins {
id 'com.android.library'
id 'org.jetbrains.kotlin.android'
id 'org.jlleitschuh.gradle.ktlint'
}

ktlint {
version = "$ktlint_version"
android = true
outputToConsole = true
ignoreFailures = false
verbose = true
}

android {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ class BitcoinMultiSigWallet(
return transaction
}

fun startWithdraw(value: Coin, receiveAddress: Address): Transaction {
fun startWithdraw(
value: Coin,
receiveAddress: Address
): Transaction {
val transaction = Transaction(params)

val relayValue = Coin.valueOf(1000)
Expand Down Expand Up @@ -60,7 +63,10 @@ class BitcoinMultiSigWallet(
return transaction
}

fun endWithdraw(transaction: Transaction, signatures: List<TransactionSignature>): Transaction {
fun endWithdraw(
transaction: Transaction,
signatures: List<TransactionSignature>
): Transaction {
for (transactionInput in transaction.inputs) {
transactionInput.scriptSig = ScriptBuilder.createMultiSigInputScript(signatures)
}
Expand All @@ -71,7 +77,8 @@ class BitcoinMultiSigWallet(
fun hash(transaction: Transaction): List<Sha256Hash> {
val transactionHashes = mutableListOf<Sha256Hash>()
transaction.inputs.forEachIndexed { index, _ ->
val transactionHash = transaction.hashForSignature(index, outputScript, Transaction.SigHash.ALL, false)
val transactionHash =
transaction.hashForSignature(index, outputScript, Transaction.SigHash.ALL, false)
transactionHashes.add(transactionHash)
}
return transactionHashes
Expand Down
9 changes: 9 additions & 0 deletions common-ethereum/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
plugins {
id 'com.android.library'
id 'org.jetbrains.kotlin.android'
id 'org.jlleitschuh.gradle.ktlint'
}

ktlint {
version = "$ktlint_version"
android = true
outputToConsole = true
ignoreFailures = false
verbose = true
}

android {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package nl.tudelft.trustchain.common.ethereum

import kotlinx.coroutines.delay
import nl.tudelft.trustchain.common.ethereum.BuildConfig
import nl.tudelft.trustchain.common.ethereum.contracts.geth.MultiSigWallet
import org.ethereum.geth.*

Expand Down Expand Up @@ -38,8 +37,16 @@ class EthereumGethMultiSigWallet(gethWallet: EthereumGethWallet) {
return gethNode.peersInfo.size() > 0L
}

class MySigner(private val account: Account?, private val keyStore: KeyStore?, private val password: String?, private val chainId: BigInt?) : Signer {
override fun sign(address: Address?, transaction: Transaction?): Transaction {
class MySigner(
private val account: Account?,
private val keyStore: KeyStore?,
private val password: String?,
private val chainId: BigInt?
) : Signer {
override fun sign(
address: Address?,
transaction: Transaction?
): Transaction {
return keyStore!!.signTxPassphrase(account, password, transaction, chainId)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ package nl.tudelft.trustchain.common.ethereum

import org.ethereum.geth.*

class EthereumGethWallet(val nodeConfig: NodeConfig, keyFile: String, nodeDirectory: String, keyStoreDirectory: String, val password: String) {
class EthereumGethWallet(
val nodeConfig: NodeConfig,
keyFile: String,
nodeDirectory: String,
keyStoreDirectory: String,
val password: String
) {
val context: Context = Geth.newContext()
val node: Node
val keyStore: KeyStore
Expand All @@ -13,11 +19,12 @@ class EthereumGethWallet(val nodeConfig: NodeConfig, keyFile: String, nodeDirect
node.start()

// Initialize the keystore.
keyStore = KeyStore(
keyStoreDirectory,
Geth.LightScryptN,
Geth.LightScryptP
)
keyStore =
KeyStore(
keyStoreDirectory,
Geth.LightScryptN,
Geth.LightScryptP
)

// Remove all existing accounts.
for (i in keyStore.accounts.size() - 1 downTo 0) {
Expand All @@ -37,15 +44,25 @@ class EthereumGethWallet(val nodeConfig: NodeConfig, keyFile: String, nodeDirect
return node.ethereumClient.getBalanceAt(context, getAddress(), -1)
}

fun sendTo(address: Address, amount: BigInt) {
val transaction = Transaction(
node.ethereumClient.getPendingNonceAt(context, getAddress()), // Nonce.
address, // Receive address.
amount, // Amount.
10000000, // Gas limit.
BigInt(1), // Gas price.
ByteArray(0)
) // Data.
fun sendTo(
address: Address,
amount: BigInt
) {
val transaction =
// Data.
Transaction(
// Nonce.
node.ethereumClient.getPendingNonceAt(context, getAddress()),
// Receive address.
address,
// Amount.
amount,
// Gas limit.
10000000,
BigInt(1),
// Gas price.
ByteArray(0)
)
val signedTransaction = sign(transaction)
send(signedTransaction)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ import java.math.BigInteger
import java.security.SecureRandom

class EthereumWalletService {

companion object {

private var globalWeb3jWallet: EthereumWeb3jWallet? = null
private var lastDir: File? = null

Expand Down Expand Up @@ -50,12 +48,13 @@ class EthereumWalletService {
)

// Create wallet
globalWeb3jWallet = EthereumWeb3jWallet(
web3j,
context.cacheDir,
getWalletKeys(context),
getWalletPassword(context)
)
globalWeb3jWallet =
EthereumWeb3jWallet(
web3j,
context.cacheDir,
getWalletKeys(context),
getWalletPassword(context)
)

return globalWeb3jWallet!!
}
Expand All @@ -65,7 +64,7 @@ class EthereumWalletService {
private const val SHARED_PREF_KEY_PUBLIC_KEY = "web3j_wallet_public_key"

private fun getWalletPassword(context: Context): String {
val preferences = context.getSharedPreferences("web3j_wallet", Context.MODE_PRIVATE);
val preferences = context.getSharedPreferences("web3j_wallet", Context.MODE_PRIVATE)

var password = preferences.getString(SHARED_PREF_KEY_WALLET_PASSWORD, null)
if (password == null) {
Expand All @@ -84,7 +83,7 @@ class EthereumWalletService {
}

private fun getWalletKeys(context: Context): ECKeyPair {
val preferences = context.getSharedPreferences("web3j_wallet", Context.MODE_PRIVATE);
val preferences = context.getSharedPreferences("web3j_wallet", Context.MODE_PRIVATE)

val privateKey = preferences.getString(SHARED_PREF_KEY_PRIVATE_KEY, null)
val publicKey = preferences.getString(SHARED_PREF_KEY_PUBLIC_KEY, null)
Expand All @@ -106,7 +105,5 @@ class EthereumWalletService {
ECKeyPair(BigInteger(privateKey), BigInteger(publicKey))
}
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ import org.web3j.protocol.core.DefaultBlockParameter
import org.web3j.tx.gas.StaticGasProvider
import java.math.BigInteger

class EthereumWeb3jMultiSigWallet(val web3j: Web3j, val address: String, wallet: EthereumWeb3jWallet) {
class EthereumWeb3jMultiSigWallet(
val web3j: Web3j,
val address: String,
wallet: EthereumWeb3jWallet
) {
var credentials = wallet.credentials
var contractGasProvider = StaticGasProvider(BigInteger.valueOf(4), BigInteger.valueOf(8000000))
private var contractBound: Boolean = false
Expand All @@ -18,12 +22,20 @@ class EthereumWeb3jMultiSigWallet(val web3j: Web3j, val address: String, wallet:

suspend fun balance(): BigInteger {
ensureContractBound()
return web3j.ethGetBalance(boundMultiSigWallet.contractAddress, DefaultBlockParameter.valueOf("latest")).sendAsync().get().balance
return web3j.ethGetBalance(
boundMultiSigWallet.contractAddress,
DefaultBlockParameter.valueOf("latest")
).sendAsync().get().balance
}

suspend fun withdraw(destination: String, value: BigInteger): String {
suspend fun withdraw(
destination: String,
value: BigInteger
): String {
if (value > balance()) return ""
val receipt = boundMultiSigWallet.submitTransaction(destination, value, byteArrayOf()).sendAsync().get()
val receipt =
boundMultiSigWallet.submitTransaction(destination, value, byteArrayOf()).sendAsync()
.get()
return receipt.transactionHash
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ class EthereumWeb3jWallet(
keyPair: ECKeyPair,
password: String
) {

val credentials: Credentials

init {
Expand All @@ -28,30 +27,38 @@ class EthereumWeb3jWallet(
}

fun balance(): BigInteger {
return web3j.ethGetBalance(credentials.address, DefaultBlockParameter.valueOf("latest")).sendAsync().get().balance
return web3j.ethGetBalance(credentials.address, DefaultBlockParameter.valueOf("latest"))
.sendAsync().get().balance
}

fun nonce(): BigInteger {
val ethGetTransactionCount = web3j.ethGetTransactionCount(
address(), DefaultBlockParameterName.LATEST
).sendAsync().get()
val ethGetTransactionCount =
web3j.ethGetTransactionCount(
address(),
DefaultBlockParameterName.LATEST
).sendAsync().get()
return ethGetTransactionCount.transactionCount
}

fun send(receiveAddress: String, value: BigInteger) {
val rawTransaction = RawTransaction.createEtherTransaction(
nonce(),
BigInteger.valueOf(4),
BigInteger.valueOf(8000000),
receiveAddress,
value
)
val signedMessage = Numeric.toHexString(
TransactionEncoder.signMessage(
rawTransaction,
credentials
fun send(
receiveAddress: String,
value: BigInteger
) {
val rawTransaction =
RawTransaction.createEtherTransaction(
nonce(),
BigInteger.valueOf(4),
BigInteger.valueOf(8000000),
receiveAddress,
value
)
val signedMessage =
Numeric.toHexString(
TransactionEncoder.signMessage(
rawTransaction,
credentials
)
)
)
web3j.ethSendRawTransaction(signedMessage).sendAsync().get()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import java.util.*
private const val PASSWORD_LENGTH = 31
private val PASSWORD_CHAR_POOL: List<Char> = ('a'..'z') + ('A'..'Z') + ('0'..'9') + '!'

fun generateWalletPassword(random: Random) = (1..PASSWORD_LENGTH)
.map { random.nextInt(PASSWORD_CHAR_POOL.size) }
.map(PASSWORD_CHAR_POOL::get)
.joinToString("")
fun generateWalletPassword(random: Random) =
(1..PASSWORD_LENGTH)
.map { random.nextInt(PASSWORD_CHAR_POOL.size) }
.map(PASSWORD_CHAR_POOL::get)
.joinToString("")
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ import nl.tudelft.trustchain.common.ethereum.utils.generateWalletPassword
import org.junit.Test
import java.security.SecureRandom

private const val PASSWORD_REGEX = "[a-zA-Z0-9!]+";
private const val PASSWORD_REGEX = "[a-zA-Z0-9!]+"

class WalletPasswordUtilsTest {

@Test
fun generatedWalletPassword_isCorrect() {
// given
Expand All @@ -19,5 +18,4 @@ class WalletPasswordUtilsTest {
// then
assert(password.matches(Regex(PASSWORD_REGEX)))
}

}
9 changes: 9 additions & 0 deletions debug/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'org.jlleitschuh.gradle.ktlint'

ktlint {
version = "$ktlint_version"
android = true
outputToConsole = true
ignoreFailures = false
verbose = true
}

android {
defaultConfig {
Expand Down
Loading

0 comments on commit 655ef2c

Please sign in to comment.