Skip to content

Commit

Permalink
Merge tag '0.4.1' into develop
Browse files Browse the repository at this point in the history
[release] 0.4.1
  • Loading branch information
mirceanis committed Mar 19, 2020
2 parents 98551eb + 39cc96a commit 4857eab
Show file tree
Hide file tree
Showing 28 changed files with 134 additions and 82 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
* 0.4.1
* fix - okhttp dependency ( 4ac0e8d1 )( 4f2839f4 )

* 0.4.0
* feat - easy configuration with infuraProjectID ( #8 )
* feat - find networks by name after configuration ( #9 )
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ allprojects {
In your module `build.gradle` file, add:

```groovy
def uport_kotlin_common_version = "0.4.0"
def uport_kotlin_common_version = "0.4.1"
dependencies {
//...
// core lib
Expand Down
23 changes: 14 additions & 9 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,24 @@
buildscript {

ext {
kotlin_version = '1.3.50'
kotlin_serialization_version = '0.12.0'
coroutines_version = "1.3.0"
kotlin_version = '1.3.70'
kotlin_serialization_version = '0.20.0'
coroutines_version = "1.3.5"

junit_version = "4.12"
mockk_version = "1.9.3"
assertk_version = "0.13"
detekt_version = "1.0.0-RC14"
detekt_version = "1.6.0"
jacoco_version = "0.8.4"

okhttp_version = "3.14.1"
okhttp_version = "4.4.1"

spongycastle_version = "1.58.0.0"
kethereum_version = "0.76.2"
khex_version = "1.0.0-RC3"
kethereum_version = "0.81.4"
khex_version = "1.0.0-RC6"
khash_version = "1.0.0-RC5"

current_release_version = "0.4.0"
current_release_version = "0.4.1"
}

repositories {
Expand Down Expand Up @@ -51,7 +52,7 @@ detekt {
"$projectDir"
)
//config = "${projectDir}/detekt.yml"
filters = ".*test.*,.*/resources/.*,.*/tmp/.*,.*/build/.*"
// exclude = ".*test.*,.*/resources/.*,.*/tmp/.*,.*/build/.*"
parallel = true
reports {
xml {
Expand All @@ -65,6 +66,10 @@ detekt {
}
}

tasks.withType(io.gitlab.arturbosch.detekt.Detekt) {
exclude("**/test/**")
}

allprojects {

apply plugin: 'jacoco'
Expand Down
5 changes: 3 additions & 2 deletions core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutines_version"

implementation "com.squareup.okhttp3:okhttp:$okhttp_version"
api "com.github.komputing.khex:extensions-jvm:$khex_version"
api "com.squareup.okhttp3:okhttp:$okhttp_version"
api "com.github.komputing.khex:extensions:$khex_version"
api "com.github.komputing.kethereum:extensions_kotlin:$kethereum_version"
implementation "com.madgag.spongycastle:core:$spongycastle_version"

testImplementation "junit:junit:$junit_version"
Expand Down
10 changes: 10 additions & 0 deletions core/src/main/java/me/uport/sdk/core/Extensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,18 @@

package me.uport.sdk.core

import org.kethereum.extensions.hexToBigInteger
import org.komputing.khex.extensions.clean0xPrefix
import org.komputing.khex.extensions.hexToByteArray
import org.komputing.khex.extensions.prepend0xPrefix
import org.komputing.khex.model.HexString
import org.spongycastle.util.encoders.Base64

fun String.clean0xPrefix() = HexString(this).clean0xPrefix().string
fun String.prepend0xPrefix() = HexString(this).prepend0xPrefix().string
fun String.hexToBigInteger() = HexString(this).hexToBigInteger()
fun String.hexToByteArray() = HexString(this).hexToByteArray()

//using spongy castle implementation because the android one can't be used properly in tests
/**
* Creates a base64 representation of the given byteArray, without padding
Expand Down
20 changes: 10 additions & 10 deletions core/src/main/java/me/uport/sdk/core/HttpClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package me.uport.sdk.core

import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import okhttp3.MediaType
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.RequestBody
import okhttp3.RequestBody.Companion.toRequestBody
import java.io.IOException

/**
Expand All @@ -24,9 +24,9 @@ class HttpClient {
* @throws [IOException] if the request could not be executed due to cancellation, disconnect or timeout
*/
suspend fun urlPost(url: String, jsonBody: String, authToken: String? = null): String {
val contentType = MediaType.parse("application/json")
val contentType = "application/json".toMediaTypeOrNull()

val body = RequestBody.create(contentType, jsonBody)
val body = jsonBody.toRequestBody(contentType)
val request = Request.Builder().apply {
url(url)
addHeader("Accept", "application/json")
Expand All @@ -37,13 +37,13 @@ class HttpClient {
post(body)
}.build()

val response = okClient.newCall(request).execute()
val response = withContext(Dispatchers.IO) { okClient.newCall(request).execute() }

if (response.isSuccessful) {
return withContext(Dispatchers.IO) { response.body()?.string() }
return withContext(Dispatchers.IO) { response.body?.string() }
?: throw IOException("got a null response body")
} else {
val code = response.code()
val code = response.code
throw IOException("server responded with HTTP $code")
}
}
Expand All @@ -63,13 +63,13 @@ class HttpClient {
}
}.build()

val response = okClient.newCall(request).execute()
val response = withContext(Dispatchers.IO) { okClient.newCall(request).execute() }

if (response.isSuccessful) {
return withContext(Dispatchers.IO) { response.body()?.string() }
return withContext(Dispatchers.IO) { response.body?.string() }
?: throw IOException("got a null response body")
} else {
val code = response.code()
val code = response.code
throw IOException("server responded with HTTP $code")
}
}
Expand Down
11 changes: 8 additions & 3 deletions core/src/main/java/me/uport/sdk/core/Networks.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package me.uport.sdk.core

import org.komputing.khex.extensions.clean0xPrefix
import org.komputing.khex.extensions.prepend0xPrefix
import org.komputing.khex.model.HexString

/**
* Convenience singleton that holds URLs and addresses for different eth networks.
Expand Down Expand Up @@ -116,12 +117,11 @@ object Networks {
fun registerNetwork(network: EthNetwork) {
val normalizedId = cleanId(network.networkId)

//TODO: check if [network] has necessary fields
NETWORK_CONFIG[normalizedId] = network
}

/**
* Gets an [EthNetwork] based on a [networkId] or [name]
* Gets an [EthNetwork] based on a networkId or name
*/
fun get(nameOrId: String): EthNetwork {
val cleanNetId = cleanId(nameOrId)
Expand All @@ -139,7 +139,12 @@ object Networks {
return NETWORK_CONFIG.values.find { it.name.toLowerCase() == queryName }
}

private fun cleanId(id: String) = id.clean0xPrefix().trimStart('0').prepend0xPrefix()
private fun cleanId(id: String) : String {
val str = HexString(id)
.clean0xPrefix().string
.trimStart('0')
return HexString(str).prepend0xPrefix().string
}

}

Expand Down
4 changes: 2 additions & 2 deletions jsonrpc/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ dependencies {
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutines_version"
api "org.jetbrains.kotlinx:kotlinx-serialization-runtime:$kotlin_serialization_version"

api "com.github.komputing.kethereum:extensions:$kethereum_version"
api "com.github.komputing.khex:extensions-jvm:$khex_version"
api "com.github.komputing.kethereum:extensions_kotlin:$kethereum_version"
api "com.github.komputing.khex:extensions:$khex_version"

api project(":core")

Expand Down
26 changes: 16 additions & 10 deletions jsonrpc/src/main/java/me/uport/sdk/jsonrpc/JsonRPC.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

package me.uport.sdk.jsonrpc

import kotlinx.serialization.builtins.list
import kotlinx.serialization.builtins.serializer
import kotlinx.serialization.json.Json
import kotlinx.serialization.list
import kotlinx.serialization.serializer
import kotlinx.serialization.json.JsonConfiguration
import me.uport.sdk.core.HttpClient
import me.uport.sdk.core.hexToBigInteger
import me.uport.sdk.jsonrpc.model.JsonRpcLogItem
import me.uport.sdk.jsonrpc.model.TransactionInformation
import me.uport.sdk.jsonrpc.model.TransactionReceipt
Expand All @@ -15,7 +17,6 @@ import me.uport.sdk.jsonrpc.model.request.EthCallParams
import me.uport.sdk.jsonrpc.model.request.JsonRpcLogsRequestParams
import me.uport.sdk.jsonrpc.model.request.JsonRpcRequest
import me.uport.sdk.jsonrpc.model.response.JsonRpcResponse
import org.kethereum.extensions.hexToBigInteger
import java.io.IOException
import java.math.BigInteger

Expand All @@ -27,6 +28,14 @@ open class JsonRPC(
private val httpClient: HttpClient = HttpClient()
) {

private val lenientJson = Json(
JsonConfiguration.Stable.copy(
isLenient = true,
ignoreUnknownKeys = true,
useArrayPolymorphism = true
)
)

//=============================
// eth_call
//=============================
Expand Down Expand Up @@ -90,7 +99,7 @@ open class JsonRPC(

val rawResult = httpClient.urlPost(rpcEndpoint, payloadRequest)

val parsedResponse = Json.nonstrict.parse(
val parsedResponse = lenientJson.parse(
JsonRpcResponse.serializer(JsonRpcLogItem.serializer().list),
rawResult
)
Expand Down Expand Up @@ -201,7 +210,7 @@ open class JsonRPC(

val rawResult = httpClient.urlPost(rpcEndpoint, payloadRequest)

val parsedResponse = Json.nonstrict.parse(
val parsedResponse = lenientJson.parse(
JsonRpcResponse.serializer(TransactionReceipt.serializer()),
rawResult
)
Expand Down Expand Up @@ -235,7 +244,7 @@ open class JsonRPC(

val rawResult = httpClient.urlPost(rpcEndpoint, payloadRequest)

val parsedResponse = Json.nonstrict.parse(
val parsedResponse = lenientJson.parse(
JsonRpcResponse.serializer(TransactionInformation.serializer()),
rawResult
)
Expand Down Expand Up @@ -287,7 +296,7 @@ open class JsonRPC(
private suspend fun jsonRpcGenericCall(url: String, payloadRequest: String): String {
val rawResult = httpClient.urlPost(url, payloadRequest)

val parsedResponse = Json.nonstrict.parse(
val parsedResponse = lenientJson.parse(
JsonRpcResponse.serializer(String.serializer()),
rawResult
)
Expand All @@ -298,6 +307,3 @@ open class JsonRPC(
}

}



Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ data class JsonRpcLogItem(
val blockHash: String,
val logIndex: BigInteger,
val removed: Boolean
)
)
29 changes: 17 additions & 12 deletions jsonrpc/src/main/java/me/uport/sdk/jsonrpc/model/Serializers.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package me.uport.sdk.jsonrpc.model

import kotlinx.serialization.*
import kotlinx.serialization.internal.StringDescriptor
import org.kethereum.extensions.hexToBigInteger
import kotlinx.serialization.Decoder
import kotlinx.serialization.Encoder
import kotlinx.serialization.KSerializer
import kotlinx.serialization.PrimitiveDescriptor
import kotlinx.serialization.PrimitiveKind
import kotlinx.serialization.SerialDescriptor
import kotlinx.serialization.Serializer
import me.uport.sdk.core.clean0xPrefix
import me.uport.sdk.core.hexToBigInteger
import me.uport.sdk.core.hexToByteArray
import org.kethereum.extensions.toHexString
import org.komputing.khex.extensions.clean0xPrefix
import org.komputing.khex.extensions.hexToByteArray
import org.komputing.khex.extensions.toHexString
import java.math.BigInteger

Expand All @@ -16,10 +21,10 @@ import java.math.BigInteger
@Serializer(forClass = BigInteger::class)
object BigIntegerSerializer : KSerializer<BigInteger> {
override val descriptor: SerialDescriptor =
StringDescriptor.withName("BigIntegerSerializer")
PrimitiveDescriptor("BigIntegerSerializer", PrimitiveKind.STRING)

override fun serialize(encoder: Encoder, obj: BigInteger) {
encoder.encodeString(obj.toHexString())
override fun serialize(encoder: Encoder, value: BigInteger) {
encoder.encodeString(value.toHexString())
}

override fun deserialize(decoder: Decoder): BigInteger = decoder
Expand All @@ -42,10 +47,10 @@ object BigIntegerSerializer : KSerializer<BigInteger> {
@Serializer(forClass = ByteArray::class)
object ByteArraySerializer : KSerializer<ByteArray> {
override val descriptor: SerialDescriptor =
StringDescriptor.withName("ByteArraySerializer")
PrimitiveDescriptor("ByteArraySerializer", PrimitiveKind.STRING)

override fun serialize(encoder: Encoder, obj: ByteArray) {
encoder.encodeString(obj.toHexString())
override fun serialize(encoder: Encoder, value: ByteArray) {
encoder.encodeString(value.toHexString())
}

override fun deserialize(decoder: Decoder): ByteArray = decoder
Expand All @@ -58,4 +63,4 @@ object ByteArraySerializer : KSerializer<ByteArray> {
}
}
.hexToByteArray()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ data class TransactionInformation(
)

{
@Suppress("ComplexMethod")
@Generated
override fun equals(other: Any?): Boolean {
if (this === other) return true
Expand Down Expand Up @@ -74,6 +75,4 @@ data class TransactionInformation(
result = 31 * result + s.hashCode()
return result
}


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ data class TransactionReceipt(
val logs: List<JsonRpcLogItem?>? = null,
val logsBloom: String? = "",
val status: BigInteger = BigInteger.ZERO
)
)
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ package me.uport.sdk.jsonrpc.model.exceptions
class JsonRpcInvalidArgumentException(
override val code : Int,
override val message : String
) : JsonRpcException(code, message)
) : JsonRpcException(code, message)
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ import kotlinx.serialization.Serializable
internal class EthCallParams(
private val to: String,
private val data: String
)
)
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ internal class JsonRpcLogsRequestParams(
private val toBlock: BigInteger,
private val address: String,
private val topics: List<String?>
)
)
Loading

0 comments on commit 4857eab

Please sign in to comment.