Skip to content

Commit

Permalink
Merge pull request #60 from input-output-hk/feature/var-int
Browse files Browse the repository at this point in the history
Feature - VarInt
  • Loading branch information
hamada147 authored Feb 22, 2023
2 parents b95ea7b + f2f92a1 commit fdd672d
Show file tree
Hide file tree
Showing 9 changed files with 341 additions and 7 deletions.
7 changes: 0 additions & 7 deletions Hashing/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -243,10 +243,3 @@ tasks.withType<DokkaTask> {
// }
// }
// }

// TODO(Just to make CI take less time as Hashing module is taking too much time)
tasks.whenTaskAdded {
if (this.name.contains("test") || this.name.contains("Test")) {
this.enabled = false
}
}
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,4 @@ include(":base-asymmetric-encryption")
include(":rsa")
include(":secp256k1")
include(":ecdsa")
include(":varint")
28 changes: 28 additions & 0 deletions varint/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Apollo - VarInt

[![Kotlin](https://img.shields.io/badge/kotlin-1.7.21-blue.svg?logo=kotlin)](http://kotlinlang.org)
[![Build](https://github.com/input-output-hk/atala-prism-apollo/actions/workflows/pull-request.yml/badge.svg)](https://github.com/input-output-hk/atala-prism-apollo/actions/workflows/pull-request.yml)
[![Deployment](https://github.com/input-output-hk/atala-prism-apollo/actions/workflows/Deployment.yml/badge.svg)](https://github.com/input-output-hk/atala-prism-apollo/actions/workflows/Deployment.yml)

![android](https://camo.githubusercontent.com/b1d9ad56ab51c4ad1417e9a5ad2a8fe63bcc4755e584ec7defef83755c23f923/687474703a2f2f696d672e736869656c64732e696f2f62616467652f706c6174666f726d2d616e64726f69642d3645444238442e7376673f7374796c653d666c6174)
![apple-silicon](https://camo.githubusercontent.com/a92c841ffd377756a144d5723ff04ecec886953d40ac03baa738590514714921/687474703a2f2f696d672e736869656c64732e696f2f62616467652f737570706f72742d2535424170706c6553696c69636f6e2535442d3433424246462e7376673f7374796c653d666c6174)
![ios](https://camo.githubusercontent.com/1fec6f0d044c5e1d73656bfceed9a78fd4121b17e82a2705d2a47f6fd1f0e3e5/687474703a2f2f696d672e736869656c64732e696f2f62616467652f706c6174666f726d2d696f732d4344434443442e7376673f7374796c653d666c6174)
![jvm](https://camo.githubusercontent.com/700f5dcd442fd835875568c038ae5cd53518c80ae5a0cf12c7c5cf4743b5225b/687474703a2f2f696d672e736869656c64732e696f2f62616467652f706c6174666f726d2d6a766d2d4442343133442e7376673f7374796c653d666c6174)
![js](https://camo.githubusercontent.com/3e0a143e39915184b54b60a2ecedec75e801f396d34b5b366c94ec3604f7e6bd/687474703a2f2f696d672e736869656c64732e696f2f62616467652f706c6174666f726d2d6a732d4638444235442e7376673f7374796c653d666c6174)
![getNode-js](https://camo.githubusercontent.com/d08fda729ceebcae0f23c83499ca8f06105350f037661ac9a4cc7f58edfdbca9/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f706c6174666f726d2d6e6f64656a732d3638613036332e7376673f7374796c653d666c6174)
![macos](https://camo.githubusercontent.com/1b8313498db244646b38a4480186ae2b25464e5e8d71a1920c52b2be5212b909/687474703a2f2f696d672e736869656c64732e696f2f62616467652f706c6174666f726d2d6d61636f732d3131313131312e7376673f7374796c653d666c6174)
![tvos](https://camo.githubusercontent.com/4ac08d7fb1bcb8ef26388cd2bf53b49626e1ab7cbda581162a946dd43e6a2726/687474703a2f2f696d672e736869656c64732e696f2f62616467652f706c6174666f726d2d74766f732d3830383038302e7376673f7374796c653d666c6174)
![watchos](https://camo.githubusercontent.com/135dbadae40f9cabe7a3a040f9380fb485cff36c90909f3c1ae36b81c304426b/687474703a2f2f696d672e736869656c64732e696f2f62616467652f706c6174666f726d2d77617463686f732d4330433043302e7376673f7374796c653d666c6174)

![Atala Prism Logo](Logo.png)

Apollo VarInt is Kotlin Multiplatform library implementation of [VarInt](https://github.com/multiformats/unsigned-varint) unsigned varint (VARiable INTeger) format.

## Usage

```kotlin
val originValue = 1234774
val byteBuffer = Buffer()
VarInt.write(originValue, byteBuffer)
val value: Int = VarInt.read(byteBuffer)
```
215 changes: 215 additions & 0 deletions varint/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
import org.gradle.internal.os.OperatingSystem
import org.jetbrains.dokka.gradle.DokkaTask
import org.jetbrains.kotlin.gradle.targets.js.webpack.KotlinWebpackOutput.Target

version = rootProject.version
val currentModuleName = "ApolloVarInt"
val os: OperatingSystem = OperatingSystem.current()

plugins {
kotlin("multiplatform")
kotlin("native.cocoapods")
id("com.android.library")
id("org.jetbrains.dokka")
}

kotlin {
android {
publishAllLibraryVariants()
}
jvm {
compilations.all {
kotlinOptions {
jvmTarget = "11"
}
}
testRuns["test"].executionTask.configure {
useJUnitPlatform()
}
}
if (os.isMacOsX) {
ios()
// tvos()
// watchos()
// macosX64()
if (System.getProperty("os.arch") != "x86_64") { // M1Chip
iosSimulatorArm64()
// tvosSimulatorArm64()
// watchosSimulatorArm64()
// macosArm64()
}
}
// if (os.isWindows) {
// // mingwX86() // it depend on kotlinx-datetime lib to support this platform before we can support it as well
// mingwX64()
// }
js(IR) {
this.moduleName = currentModuleName
this.binaries.library()
this.useCommonJs()
this.compilations["main"].packageJson {
this.version = rootProject.version.toString()
}
this.compilations["test"].packageJson {
this.version = rootProject.version.toString()
}
browser {
this.webpackTask {
this.output.library = currentModuleName
this.output.libraryTarget = Target.VAR
}
this.commonWebpackConfig {
this.cssSupport {
this.enabled = true
}
}
this.testTask {
this.useKarma {
this.useChromeHeadless()
}
}
}
nodejs {
this.testTask {
this.useKarma {
this.useChromeHeadless()
}
}
}
}

if (os.isMacOsX) {
cocoapods {
this.summary = "ApolloVarInt is VarInt"
this.version = rootProject.version.toString()
this.authors = "IOG"
this.ios.deploymentTarget = "13.0"
this.osx.deploymentTarget = "12.0"
this.tvos.deploymentTarget = "13.0"
this.watchos.deploymentTarget = "8.0"
framework {
this.baseName = currentModuleName
}
}
}

sourceSets {
val commonMain by getting {
dependencies {
implementation("com.squareup.okio:okio:3.2.0")
}
}
val commonTest by getting {
dependencies {
implementation(kotlin("test"))
}
}
val jvmMain by getting
val jvmTest by getting {
dependencies {
implementation("junit:junit:4.13.2")
}
}
val androidMain by getting
val androidTest by getting {
dependencies {
implementation("junit:junit:4.13.2")
}
}
val jsMain by getting
val jsTest by getting
if (os.isMacOsX) {
val iosMain by getting
val iosTest by getting
// val tvosMain by getting
// val tvosTest by getting
// val watchosMain by getting
// val watchosTest by getting
// val macosX64Main by getting
// val macosX64Test by getting
if (System.getProperty("os.arch") != "x86_64") { // M1Chip
val iosSimulatorArm64Main by getting {
this.dependsOn(iosMain)
}
val iosSimulatorArm64Test by getting {
this.dependsOn(iosTest)
}
// val tvosSimulatorArm64Main by getting {
// this.dependsOn(tvosMain)
// }
// val tvosSimulatorArm64Test by getting {
// this.dependsOn(tvosTest)
// }
// val watchosSimulatorArm64Main by getting {
// this.dependsOn(watchosMain)
// }
// val watchosSimulatorArm64Test by getting {
// this.dependsOn(watchosTest)
// }
// val macosArm64Main by getting {
// this.dependsOn(macosX64Main)
// }
// val macosArm64Test by getting {
// this.dependsOn(macosX64Test)
// }
}
}
// if (os.isWindows) {
// // val mingwX86Main by getting // it depend on kotlinx-datetime lib to support this platform before we can support it as well
// // val mingwX86Test by getting // it depend on kotlinx-datetime lib to support this platform before we can support it as well
// val mingwX64Main by getting
// val mingwX64Test by getting
// }
}
}

android {
compileSdk = 32
sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml")
defaultConfig {
minSdk = 21
targetSdk = 32
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}
/**
* Because Software Components will not be created automatically for Maven publishing from
* Android Gradle Plugin 8.0. To opt-in to the future behavior, set the Gradle property android.
* disableAutomaticComponentCreation=true in the `gradle.properties` file or use the new
* publishing DSL.
*/
publishing {
multipleVariants {
withSourcesJar()
withJavadocJar()
allVariants()
}
}
}

// Dokka implementation
tasks.withType<DokkaTask> {
moduleName.set(project.name)
moduleVersion.set(rootProject.version.toString())
description = """
This is a Kotlin Multiplatform Library for ECDSA
""".trimIndent()
dokkaSourceSets {
// TODO: Figure out how to include files to the documentations
named("commonMain") {
includes.from("Module.md", "docs/Module.md")
}
}
}

// afterEvaluate {
// tasks.withType<AbstractTestTask> {
// testLogging {
// events("passed", "skipped", "failed", "standard_out", "standard_error")
// showExceptions = true
// showStackTraces = true
// }
// }
// }
2 changes: 2 additions & 0 deletions varint/src/androidMain/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest package="io.iohk.atala.prism.apollo.varint" />
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package io.iohk.atala.prism.apollo.varint

import okio.Buffer

object VarInt : VarIntInterface {
override fun write(value: Int, byteBuffer: Buffer) {
var value = value
while ((value and -0x80).toLong() != 0L) {
byteBuffer.writeByte(value and 0x7F or 0x80)
value = value ushr 7
}
byteBuffer.writeByte(value and 0x7F)
}

override fun read(byteBuffer: Buffer): Int {
var value = 0
var i = 0
var b = 0
while (byteBuffer.size > 0 && byteBuffer.readByte().toInt().also { b = it } and 0x80 != 0) {
value = value or (b and 0x7F shl i)
i += 7
if (i > 35) {
throw IllegalArgumentException("Variable length quantity is too long")
}
}
value = value or (b shl i)
return value
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package io.iohk.atala.prism.apollo.varint

import okio.Buffer

interface VarIntInterface {
fun write(value: Int, byteBuffer: Buffer)
fun read(byteBuffer: Buffer): Int
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.iohk.atala.prism.apollo.varint

import okio.Buffer
import kotlin.test.Test
import kotlin.test.assertEquals

class VarIntTests {
@Test
fun testVarInt() {
val origin = 1234774
val byteBuffer = Buffer()
VarInt.write(origin, byteBuffer)
val value: Int = VarInt.read(byteBuffer)
assertEquals(origin, value)
}
}
42 changes: 42 additions & 0 deletions varint/varint.podspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
Pod::Spec.new do |spec|
spec.name = 'varint'
spec.version = '1.0.0-alpha'
spec.homepage = ''
spec.source = { :http=> ''}
spec.authors = 'IOG'
spec.license = ''
spec.summary = 'ApolloVarInt is VarInt'
spec.vendored_frameworks = 'build/cocoapods/framework/ApolloVarInt.framework'
spec.libraries = 'c++'
spec.ios.deployment_target = '13.0'
spec.osx.deployment_target = '12.0'
spec.tvos.deployment_target = '13.0'
spec.watchos.deployment_target = '8.0'


spec.pod_target_xcconfig = {
'KOTLIN_PROJECT_PATH' => ':varint',
'PRODUCT_MODULE_NAME' => 'ApolloVarInt',
}

spec.script_phases = [
{
:name => 'Build varint',
:execution_position => :before_compile,
:shell_path => '/bin/sh',
:script => <<-SCRIPT
if [ "YES" = "$OVERRIDE_KOTLIN_BUILD_IDE_SUPPORTED" ]; then
echo "Skipping Gradle build task invocation due to OVERRIDE_KOTLIN_BUILD_IDE_SUPPORTED environment variable set to \"YES\""
exit 0
fi
set -ev
REPO_ROOT="$PODS_TARGET_SRCROOT"
"$REPO_ROOT/../gradlew" -p "$REPO_ROOT" $KOTLIN_PROJECT_PATH:syncFramework \
-Pkotlin.native.cocoapods.platform=$PLATFORM_NAME \
-Pkotlin.native.cocoapods.archs="$ARCHS" \
-Pkotlin.native.cocoapods.configuration="$CONFIGURATION"
SCRIPT
}
]

end

0 comments on commit fdd672d

Please sign in to comment.