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

Multiplatform setup #63

Closed
wants to merge 12 commits into from
Closed
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
1 change: 0 additions & 1 deletion abi/.gitignore

This file was deleted.

13 changes: 10 additions & 3 deletions abi/build.gradle
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
dependencies {
implementation "com.squareup.moshi:moshi:$moshi_version"
}
apply plugin: 'kotlinx-serialization'

kotlin.sourceSets {
commonMain.dependencies {
api "org.jetbrains.kotlinx:kotlinx-serialization-runtime-common:$serialization_version"
}
jvmMain.dependencies {
api "org.jetbrains.kotlinx:kotlinx-serialization-runtime:$serialization_version"
}
}
18 changes: 18 additions & 0 deletions abi/common/src/org/kethereum/abi/EthereumABI.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.kethereum.abi

import kotlinx.serialization.json.Json
import kotlinx.serialization.list
import org.kethereum.abi.model.EthereumFunction

private fun parse(abi: String): List<EthereumFunction> {
// We use .nonstrict to avoid errors with unknown keys or others
return Json.nonstrict.parse(EthereumFunction.serializer().list, abi)
}

class EthereumABI(val methodList: List<EthereumFunction>) {
constructor(abiString: String) : this(parse(abiString))
}

fun EthereumABI.toFunctionSignaturesString() = methodList.filter { it.name != null }.joinToString("\n") { function ->
function.name + "(" + function.inputs?.joinToString(",") { it.type + " " + it.name } + ")"
}
19 changes: 19 additions & 0 deletions abi/common/src/org/kethereum/abi/model/EthereumFunction.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.kethereum.abi.model

import kotlinx.serialization.Optional
import kotlinx.serialization.Serializable

@Serializable
data class EthereumFunctionParameter(
val name: String,
val type: String
)

@Serializable
data class EthereumFunction(
@Optional val constant: Boolean = false,
@Optional val inputs: List<EthereumFunctionParameter>? = null,
@Optional val name: String? = null,
@Optional val id: String? = null,
@Optional val result: String? = null
)
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
[{
package org.kethereum.abi

val peepethAbi = """
[{
"constant": false,
"inputs": [{
"name": "_followee",
Expand Down Expand Up @@ -344,4 +347,5 @@
"inputs": [],
"name": "PeepethEvent",
"type": "event"
}]
}]
""".trimIndent()
13 changes: 13 additions & 0 deletions abi/common/test/TheParser.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.kethereum.abi

import kotlin.test.Test
import kotlin.test.assertEquals

class TheParser {

@Test
fun convertingTransactionRPC2KethereumTransactionWorks() {
val parsed = EthereumABI(peepethAbi)
assertEquals(parsed.methodList.size, 31)
}
}
24 changes: 0 additions & 24 deletions abi/src/main/kotlin/org/kethereum/abi/EthereumABI.kt

This file was deleted.

14 changes: 0 additions & 14 deletions abi/src/main/kotlin/org/kethereum/abi/model/EthereumFunction.kt

This file was deleted.

14 changes: 0 additions & 14 deletions abi/src/test/kotlin/org/kethereum/abi/TheParser.kt

This file was deleted.

13 changes: 9 additions & 4 deletions base58/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
dependencies {
implementation project(path: ':hashes')
testImplementation "com.github.walleth:khex:$khex_version"

kotlin.sourceSets {
commonMain.dependencies {
api project(':hashes')
api project(':model')
}

commonTest.dependencies {
api "com.github.walleth:khex:$khex_version"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ package org.kethereum.encodings
*/

import org.kethereum.hashes.sha256
import java.util.*
import org.kethereum.model.extensions.copy
import kotlin.jvm.JvmName

private const val ENCODED_ZERO = '1'
private const val CHECKSUM_SIZE = 4
Expand Down Expand Up @@ -70,7 +71,6 @@ fun ByteArray.encodeToBase58String(): String {
* @return the decoded data bytes
* @throws NumberFormatException if the string is not a valid base58 string
*/
@Throws(NumberFormatException::class)
fun String.decodeBase58(): ByteArray {
if (isEmpty()) {
return ByteArray(0)
Expand Down Expand Up @@ -105,7 +105,7 @@ fun String.decodeBase58(): ByteArray {
++outputStart
}
// Return decoded data (including original number of leading zeros).
return Arrays.copyOfRange(decoded, outputStart - zeros, decoded.size)
return decoded.copyOfRange(outputStart - zeros, decoded.size)
}

/**
Expand Down Expand Up @@ -138,9 +138,9 @@ private fun divmod(number: ByteArray, firstDigit: UInt, base: UInt, divisor: UIn
* @return the base58-encoded string
*/
fun ByteArray.encodeToBase58WithChecksum() = ByteArray(size + CHECKSUM_SIZE).apply {
System.arraycopy(this@encodeToBase58WithChecksum, 0, this, 0, [email protected])
this@encodeToBase58WithChecksum.copy(0, this, 0, [email protected])
val checksum = [email protected]().sha256()
System.arraycopy(checksum, 0, this, [email protected], CHECKSUM_SIZE)
checksum.copy(0, this, [email protected], CHECKSUM_SIZE)

}.encodeToBase58String()

Expand All @@ -149,14 +149,14 @@ fun String.decodeBase58WithChecksum(): ByteArray {
if (rawBytes.size < CHECKSUM_SIZE) {
throw Exception("Too short for checksum: $this l: ${rawBytes.size}")
}
val checksum = Arrays.copyOfRange(rawBytes, rawBytes.size - CHECKSUM_SIZE, rawBytes.size)
val checksum = rawBytes.copyOfRange(rawBytes.size - CHECKSUM_SIZE, rawBytes.size)

val payload = Arrays.copyOfRange(rawBytes, 0, rawBytes.size - CHECKSUM_SIZE)
val payload = rawBytes.copyOfRange(0, rawBytes.size - CHECKSUM_SIZE)

val hash = payload.sha256().sha256()
val computedChecksum = Arrays.copyOfRange(hash, 0, CHECKSUM_SIZE)
val computedChecksum = hash.copyOfRange(0, CHECKSUM_SIZE)

if (Arrays.equals(checksum, computedChecksum)) {
if (checksum.contentEquals(computedChecksum)) {
return payload
} else {
throw Exception("Checksum mismatch: $this ")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package org.kethereum.encodings

import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
import org.walleth.khex.hexToByteArray
import org.kethereum.model.extensions.hexToByteArray
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue

class Base58Test {

// Tests from https://github.com/bitcoin/bitcoin/blob/master/src/test/data/base58_encode_decode.json
val TEST_VECTORS = mapOf(
private val testVectors = mapOf(
"" to "",
"61" to "2g",
"626262" to "a3gV",
Expand All @@ -24,15 +25,15 @@ class Base58Test {

@Test
fun encodingToBase58Works() {
TEST_VECTORS.forEach {
assertThat(it.key.hexToByteArray().encodeToBase58String()).isEqualTo(it.value)
testVectors.forEach {
assertEquals(it.key.hexToByteArray().encodeToBase58String(), it.value)
}
}

@Test
fun decodingFromBase58Works() {
TEST_VECTORS.forEach {
assertThat(it.value.decodeBase58()).isEqualTo(it.key.hexToByteArray())
testVectors.forEach {
assertTrue(it.value.decodeBase58().contentEquals(it.key.hexToByteArray()))
}
}
}
31 changes: 18 additions & 13 deletions bip32/build.gradle
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
dependencies {
implementation project(path: ':bip44')
implementation project(path: ':base58')
implementation project(path: ':crypto')
implementation project(path: ':crypto_api')
implementation project(path: ':hashes')
implementation project(path: ':extensions')
implementation project(path: ':model')
apply plugin: 'kotlinx-serialization'

testImplementation "com.github.walleth:khex:$khex_version"
testImplementation project(path: ':crypto_impl_bouncycastle')

testImplementation project(":test_data")
}
kotlin.sourceSets {
commonMain.dependencies {
api "org.jetbrains.kotlinx:kotlinx-serialization-runtime-common:$serialization_version"

api project(':bip44')
api project(':base58')
api project(':crypto')
api project(':crypto_api')
api project(':hashes')
api project(':model')
}
jvmTest.dependencies {
api project(':test_data')
api project(':crypto_impl_bouncycastle')
api "org.jetbrains.kotlinx:kotlinx-serialization-runtime:$serialization_version"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,26 @@

package org.kethereum.bip32

import kotlinx.io.ByteBuffer
import kotlinx.io.ByteOrder
import org.kethereum.bip32.model.CHAINCODE_SIZE
import org.kethereum.bip32.model.ExtendedKey
import org.kethereum.bip32.model.Seed
import org.kethereum.bip44.BIP44
import org.kethereum.bip44.BIP44Element
import org.kethereum.crypto.*
import org.kethereum.extensions.toBytesPadded
import org.kethereum.hashes.ripemd160
import org.kethereum.hashes.sha256
import org.kethereum.model.ECKeyPair
import org.kethereum.model.PRIVATE_KEY_SIZE
import org.kethereum.model.PrivateKey
import java.math.BigInteger
import java.nio.ByteBuffer
import java.nio.ByteOrder
import java.security.InvalidKeyException
import java.security.KeyException
import java.security.NoSuchAlgorithmException
import java.security.NoSuchProviderException
import java.util.*
import org.kethereum.model.exceptions.InvalidKeyException
import org.kethereum.model.exceptions.KeyException
import org.kethereum.model.exceptions.NoSuchAlgorithmException
import org.kethereum.model.exceptions.NoSuchProviderException
import org.kethereum.model.extensions.toBytesPadded
import org.kethereum.model.number.BigInteger
import kotlin.jvm.JvmName

fun Seed.toKey(pathString: String) = BIP44(pathString).path
.fold(toExtendedKey()) { current, bip44Element ->
Expand Down Expand Up @@ -74,8 +74,8 @@ fun ExtendedKey.generateChildKey(element: BIP44Element): ExtendedKey {
.array()
}
val lr = mac.generate(extended)
val l = Arrays.copyOfRange(lr, 0, PRIVATE_KEY_SIZE)
val r = Arrays.copyOfRange(lr, PRIVATE_KEY_SIZE, PRIVATE_KEY_SIZE + CHAINCODE_SIZE)
val l = lr.copyOfRange(0, PRIVATE_KEY_SIZE)
val r = lr.copyOfRange(PRIVATE_KEY_SIZE, PRIVATE_KEY_SIZE + CHAINCODE_SIZE)

val m = BigInteger(1, l)
if (m >= CURVE.n) {
Expand Down
Loading