Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
Funkatronics committed Sep 12, 2023
0 parents commit bea3dde
Show file tree
Hide file tree
Showing 29 changed files with 1,812 additions and 0 deletions.
67 changes: 67 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
name: Kotlin CI

on:
push:
branches: [ main ]
pull_request:
branches: [ main, develop ]
release:
types: [ published ]

jobs:
build:
runs-on: macos-latest

steps:
- name: Checkout
uses: actions/checkout@v3

- name: Validate Gradle Wrapper
uses: gradle/wrapper-validation-action@v1

- name: Configure JDK
uses: actions/setup-java@v3
with:
java-version: '11'
distribution: 'temurin'
cache: gradle

# Build
- name: Grant execute permission for gradlew
run: chmod +x gradlew
- name: Test
run: ./gradlew build

- name: Save Test Reports
if: failure()
uses: actions/upload-artifact@v3
with:
name: test-reports
path: '**/build/reports'

publish:
runs-on: macos-latest
needs: build
if: ${{ github.event_name == 'release' }}

steps:
- name: Checkout
uses: actions/checkout@v3

- name: Configure JDK
uses: actions/setup-java@v3
with:
java-version: '11'
distribution: 'temurin'
cache: gradle

- name: Grant execute permission for gradlew
run: chmod +x gradlew
- name: Publish to Maven Central
run: ./gradlew clean publishAllPublicationsToMavenCentral --stacktrace
env:
ORG_GRADLE_PROJECT_mavenCentralUsername: ${{ secrets.SONATYPE_NEXUS_USERNAME }}
ORG_GRADLE_PROJECT_mavenCentralPassword: ${{ secrets.SONATYPE_NEXUS_PASSWORD }}
ORG_GRADLE_PROJECT_signingInMemoryKey: ${{ secrets.ARTIFACT_SIGNING_PRIVATE_KEY }}
ORG_GRADLE_PROJECT_signingInMemoryKeyId: ${{ secrets.ARTIFACT_SIGNING_PRIVATE_KEY_ID }}
ORG_GRADLE_PROJECT_signingInMemoryKeyPassword: ${{ secrets.ARTIFACT_SIGNING_PRIVATE_KEY_PASSWORD }}
34 changes: 34 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Compiled class file
*.class

# Log file
*.log

# BlueJ files
*.ctxt

# Mobile Tools for Java (J2ME)
.mtj.tmp/

# Package Files #
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar

# Build Files
/build
*/build
/.gradle

# IDE Files
/.idea

# Local Only Files
local.properties

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Web3Kt
Multiplatform Web3 Library


8 changes: 8 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
plugins {
kotlin("multiplatform") version "1.9.0" apply false
id("com.vanniktech.maven.publish") version "0.25.3" apply false
}

//tasks.register("clean", Delete::class) {
// delete(rootProject.buildDir)
//}
44 changes: 44 additions & 0 deletions core/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
plugins {
kotlin("multiplatform")
kotlin("plugin.serialization") version "1.9.0"
id("com.vanniktech.maven.publish")
}

val artifactId: String by project

kotlin {
jvm {
jvmToolchain(11)
withJava()
testRuns["test"].executionTask.configure {
useJUnitPlatform()
}
}
listOf(
iosX64(),
iosArm64(),
iosSimulatorArm64(),
macosX64(),
macosArm64()
).forEach {
it.binaries.framework {
baseName = artifactId
}
}
sourceSets {
val commonMain by getting {
dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.0-RC")
}
}
val commonTest by getting {
dependencies {
implementation(kotlin("test"))
}
}
}
}

mavenPublishing {
coordinates(group as String, artifactId, version as String)
}
23 changes: 23 additions & 0 deletions core/src/commonMain/kotlin/com/funkatronics/publickey/PublicKey.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.funkatronics.publickey

/**
* PublicKey Interface
*
* @author Funkatronics
*/
interface PublicKey {
/**
* byte length of the public key
*/
val length: Number

/**
* the bytes making up the public key
*/
val bytes: ByteArray

/**
* returns a string representation of the Public Key
*/
fun string(): String
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.funkatronics.serialization

import kotlinx.serialization.KSerializer
import kotlinx.serialization.builtins.ByteArraySerializer
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder

open class ByteStringSerializer(val length: Int) : KSerializer<ByteArray> {
override val descriptor: SerialDescriptor = ByteArraySerializer().descriptor

override fun deserialize(decoder: Decoder) =
ByteArray(length) {
decoder.decodeByte()
}

override fun serialize(encoder: Encoder, value: ByteArray) {
value.forEach { encoder.encodeByte(it) }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package com.funkatronics.serialization

import com.funkatronics.util.asVarint
import kotlinx.serialization.KSerializer
import kotlinx.serialization.builtins.ByteArraySerializer
import kotlinx.serialization.builtins.ListSerializer
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder

class CompactArraySerializer<T>(val valueSerializer: KSerializer<T>) : KSerializer<Collection<T>> {
override val descriptor: SerialDescriptor = ListSerializer(valueSerializer).descriptor

override fun deserialize(decoder: Decoder): Collection<T> {
var size = 0 // decoder.decodeByte().toInt()
var shift = 0
do {
val b = decoder.decodeByte().toInt() and 0xFF
size = ((b and 0x7f) shl shift) or size
shift += 7
} while (b and 0x80 != 0)

return List(size) {
decoder.decodeSerializableValue(valueSerializer)
}
}

override fun serialize(encoder: Encoder, value: Collection<T>) {
value.size.asVarint().forEach { encoder.encodeByte(it) }
value.forEach { encoder.encodeSerializableValue(valueSerializer, it) }
}
}

object CompactByteArraySerializer : KSerializer<ByteArray> {
override val descriptor: SerialDescriptor = ByteArraySerializer().descriptor

override fun deserialize(decoder: Decoder): ByteArray {
var size = 0 // decoder.decodeByte().toInt()
var shift = 0
do {
val b = decoder.decodeByte().toInt() and 0xFF
size = ((b and 0x7f) shl shift) or size
shift += 7
} while (b and 0x80 != 0)

return ByteArray(size) {
decoder.decodeByte()
}
}

override fun serialize(encoder: Encoder, value: ByteArray) {
value.size.asVarint().forEach { encoder.encodeByte(it) }
value.forEach { encoder.encodeByte(it) }
}
}

object CompactSignatureArraySerializer : KSerializer<List<ByteArray>> {
override val descriptor: SerialDescriptor = ByteArraySerializer().descriptor

override fun deserialize(decoder: Decoder): List<ByteArray> {
var size = 0 // decoder.decodeByte().toInt()
var shift = 0
do {
val b = decoder.decodeByte().toInt() and 0xFF
size = ((b and 0x7f) shl shift) or size
shift += 7
} while (b and 0x80 != 0)

return List(size) {
ByteArray(64) {
decoder.decodeByte()
}
}
}

override fun serialize(encoder: Encoder, value: List<ByteArray>) {
value.size.asVarint().forEach { encoder.encodeByte(it) }
value.forEach { it.forEach { encoder.encodeByte(it) } }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.funkatronics.signer

abstract class Ed25519Signer : Signer {
override val ownerLength = 32
override val signatureLength = 64
}
8 changes: 8 additions & 0 deletions core/src/commonMain/kotlin/com/funkatronics/signer/Signer.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.funkatronics.signer

interface Signer {
val publicKey: ByteArray
val ownerLength: Number
val signatureLength: Number
suspend fun signPayload(payload: ByteArray): ByteArray
}
39 changes: 39 additions & 0 deletions core/src/commonMain/kotlin/com/funkatronics/util/Varint.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.funkatronics.util

import kotlin.experimental.and
import kotlin.math.ceil

object Varint {

fun encode(value: Int): ByteArray {
if (value == 0) return byteArrayOf(0)
var num = value
val encodedSize = ceil((Int.SIZE_BITS - value.countLeadingZeroBits()) / 7f).toInt()
return ByteArray(encodedSize) {
(num and 0x7F or if (num < 128) 0 else 128).toByte().also {
num /= 128
}
}
}

fun encode(value: Long): ByteArray {
if (value == 0L) return byteArrayOf(0)
var num = value
val encodedSize = ceil((Long.SIZE_BITS - value.countLeadingZeroBits()) / 7f).toInt()
return ByteArray(encodedSize) {
(num and 0x7F or if (num < 128) 0 else 128).toByte().also {
num /= 128
}
}
}

fun decode(bytes: ByteArray): Long =
bytes.takeWhile { it and 0x80.toByte() < 0 }.run {
this + bytes[this.size]
}.foldIndexed(0L) { index, value, byte ->
((byte and 0x7f).toLong() shl (7*index)) or value
}
}

fun Int.asVarint() = Varint.encode(this)
fun Long.asVarint() = Varint.encode(this)

0 comments on commit bea3dde

Please sign in to comment.