Skip to content

Commit

Permalink
Prepare for room (remove default constructors and nested class)
Browse files Browse the repository at this point in the history
  • Loading branch information
ligi committed Oct 4, 2017
1 parent 4a9f0fd commit fb9123c
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,25 @@ package org.kethereum.functions
import org.kethereum.functions.rlp.RLPList
import org.kethereum.functions.rlp.encode
import org.kethereum.functions.rlp.toRLP
import org.kethereum.model.SignatureData
import org.kethereum.model.Transaction
import org.walleth.khex.hexToByteArray

fun Transaction.encodeRLP() = RLPList(listOf(
fun Transaction.encodeRLP(withSignature: SignatureData? = null) = RLPList(listOf(
nonce!!.toRLP(),
gasPrice.toRLP(),
gasLimit.toRLP(),
(to?.hex?.let { it } ?: "0x").hexToByteArray().toRLP(),
value.toRLP(),
input.toByteArray().toRLP()
).let {
val _signatureData = this.signatureData
if (_signatureData == null) {
if (withSignature == null) {
it
} else {
it.plus(listOf(
_signatureData.v.toRLP(),
_signatureData.r.toRLP(),
_signatureData.s.toRLP()
withSignature.v.toRLP(),
withSignature.r.toRLP(),
withSignature.s.toRLP()
))
}
}).encode()
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package org.kethereum.functions
import org.assertj.core.api.Assertions.assertThat
import org.junit.Test
import org.kethereum.model.Address
import org.kethereum.model.Transaction
import org.kethereum.model.createTransactionWithDefaults
import java.math.BigInteger

class TheTransactionFun {
Expand All @@ -19,13 +19,13 @@ class TheTransactionFun {

@Test
fun weCanParseTokenTransferValue() {
val createTokenTransferTransactionInput = Transaction(BigInteger.valueOf(103), someAddress, someAddress, input = createTokenTransferTransactionInput(someAddress, BigInteger("10")))
val createTokenTransferTransactionInput = createTransactionWithDefaults(BigInteger.valueOf(103), someAddress, someAddress, input = createTokenTransferTransactionInput(someAddress, BigInteger("10")))
assertThat(createTokenTransferTransactionInput.getTokenTransferValue()).isEqualTo(BigInteger("10"))
}

@Test
fun weCanParseTokenTransferTo() {
val createTokenTransferTransactionInput = Transaction(BigInteger.valueOf(103), someAddress, someAddress, input = createTokenTransferTransactionInput(anotherAddress, BigInteger("10")))
val createTokenTransferTransactionInput = createTransactionWithDefaults(BigInteger.valueOf(103), someAddress, someAddress, input = createTokenTransferTransactionInput(anotherAddress, BigInteger("10")))
assertThat(createTokenTransferTransactionInput.getTokenTransferTo()).isEqualTo(anotherAddress)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import org.junit.Test
import org.kethereum.functions.encodeRLP
import org.kethereum.model.Address
import org.kethereum.model.SignatureData
import org.kethereum.model.Transaction
import org.kethereum.model.createTransactionWithDefaults
import org.walleth.khex.hexToByteArray
import org.walleth.khex.toHexString
import transactionTestData
Expand All @@ -23,22 +23,22 @@ class TheTransactionEncoder {
if (current.map.containsKey("rlp") && current.map.containsKey("transaction")) {
val rlp = current.map["rlp"] as String
val transactionMap = (current["transaction"] as JsonObject).map
val transaction = Transaction(
val signatureData = SignatureData(
r = transactionMap["r"].getBigInteger(),
s = transactionMap["s"].getBigInteger(),
v = (transactionMap["v"] as String).hexToByteArray().first()

)
val transaction = createTransactionWithDefaults(
gasLimit = transactionMap["gasLimit"].getBigInteger(),
gasPrice = transactionMap["gasPrice"].getBigInteger(),
value = transactionMap["value"].getBigInteger(),
nonce = transactionMap["nonce"].getBigInteger(),

to = Address((transactionMap["to"] as String)),
input = (transactionMap["data"] as String).hexToByteArray().toList(),
from = Address("0x0"),
signatureData = SignatureData(
r = transactionMap["r"].getBigInteger(),
s = transactionMap["s"].getBigInteger(),
v = (transactionMap["v"] as String).hexToByteArray().first()

))
val encodedRLPString = transaction.encodeRLP().toHexString()
from = Address("0x0"))
val encodedRLPString = transaction.encodeRLP(signatureData).toHexString()
if (encodedRLPString != rlp) {
throw (Exception("error in " + it + "\n" + rlp + "\n" + encodedRLPString))
}
Expand Down
25 changes: 18 additions & 7 deletions model/src/main/java/org/kethereum/model/Transaction.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,22 @@ import java.math.BigInteger
data class Transaction(val value: BigInteger,
val from: Address,
val to: Address?,
val creationEpochSecond: Long? = null,
val creationEpochSecond: Long?,
var nonce: BigInteger?,
var gasPrice: BigInteger,
var gasLimit: BigInteger,
var txHash: String?,
var input: List<Byte>)

var nonce: BigInteger? = null,
var gasPrice: BigInteger = DEFAULT_GAS_PRICE,
var gasLimit: BigInteger = DEFAULT_GAS_LIMIT,
var txHash: String? = null,
var input: List<Byte> = emptyList(),
var signatureData: SignatureData? = null)
// we cannot use default values in the data class when we want to use it with room
fun createTransactionWithDefaults(
value: BigInteger,
from: Address,
to: Address?,
creationEpochSecond: Long? = null,
nonce: BigInteger? = null,
gasPrice: BigInteger = DEFAULT_GAS_PRICE,
gasLimit: BigInteger = DEFAULT_GAS_LIMIT,
txHash: String? = null,
input: List<Byte> = emptyList()
) = Transaction(value, from, to, creationEpochSecond, nonce, gasPrice, gasLimit, txHash, input)
4 changes: 2 additions & 2 deletions rpc/src/main/java/org/kethereum/rpc/Converter.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package org.kethereum.rpc

import org.kethereum.model.Address
import org.kethereum.model.Transaction
import org.kethereum.model.createTransactionWithDefaults
import org.kethereum.rpc.model.TransactionRPC
import org.walleth.khex.hexToByteArray
import java.math.BigInteger

fun TransactionRPC.toKethereumTransaction() = Transaction(
fun TransactionRPC.toKethereumTransaction() = createTransactionWithDefaults(
value = value.hexToBigInteger(),
from = Address(from),
to = to?.let { Address(it) },
Expand Down

0 comments on commit fb9123c

Please sign in to comment.