Skip to content

Commit

Permalink
Version 0.4.0-1.4-rc: Removed serialization ¯\_(ツ)_/¯. (#8)
Browse files Browse the repository at this point in the history
Co-authored-by: Salomon BRYS <[email protected]>
  • Loading branch information
SalomonBrys and Salomon BRYS authored Aug 6, 2020
1 parent 8cd8c0c commit eb5478e
Show file tree
Hide file tree
Showing 8 changed files with 11 additions and 78 deletions.
4 changes: 1 addition & 3 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@ import org.gradle.internal.impldep.org.apache.http.impl.auth.BasicScheme

plugins {
kotlin("multiplatform") version "1.4.0-rc"
kotlin("plugin.serialization") version "1.4.0-rc"
`maven-publish`
}

val currentOs = org.gradle.internal.os.OperatingSystem.current()

group = "fr.acinq.bitcoink"
version = "0.3.0-1.4-rc"
version = "0.4.0-1.4-rc"

repositories {
mavenLocal()
Expand Down Expand Up @@ -44,7 +43,6 @@ kotlin {
val commonMain by getting {
dependencies {
implementation(kotlin("stdlib-common"))
implementation("org.jetbrains.kotlinx:kotlinx-serialization-runtime:1.0-M1-1.4.0-rc")
api("fr.acinq.secp256k1:secp256k1:$secp256k1KmpVersion")
}
}
Expand Down
11 changes: 5 additions & 6 deletions src/commonMain/kotlin/fr/acinq/bitcoin/Block.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ package fr.acinq.bitcoin
import fr.acinq.bitcoin.io.Input
import fr.acinq.bitcoin.io.Output
import fr.acinq.secp256k1.Hex
import kotlinx.serialization.Serializable
import kotlinx.serialization.Transient
import kotlin.jvm.JvmField
import kotlin.jvm.JvmStatic

Expand All @@ -35,19 +33,18 @@ import kotlin.jvm.JvmStatic
* @param nonce The nonce used to generate this block… to allow variations of the header and compute different hashes
*/
@OptIn(ExperimentalUnsignedTypes::class)
@Serializable
public data class BlockHeader(
@JvmField val version: Long,
@JvmField val hashPreviousBlock: ByteVector32,
@JvmField val hashMerkleRoot: ByteVector32,
@JvmField val time: Long,
@JvmField val bits: Long,
@JvmField val nonce: Long
) {
@JvmField @Transient
) : BtcSerializable<BlockHeader> {
@JvmField
public val hash: ByteVector32 = ByteVector32(Crypto.hash256(write(this)))

@JvmField @Transient
@JvmField
public val blockId: ByteVector32 = hash.reversed()

public fun setVersion(input: Long): BlockHeader = this.copy(version = input)
Expand Down Expand Up @@ -164,6 +161,8 @@ public data class BlockHeader(
return target.endodeCompact(false)
}
}

override fun serializer(): BtcSerializer<BlockHeader> = Companion
}

/**
Expand Down
46 changes: 0 additions & 46 deletions src/commonMain/kotlin/fr/acinq/bitcoin/ByteVector.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,10 @@
package fr.acinq.bitcoin

import fr.acinq.secp256k1.Hex
import kotlinx.serialization.KSerializer
import kotlinx.serialization.Serializable
import kotlinx.serialization.descriptors.PrimitiveKind
import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
import kotlin.experimental.or
import kotlin.jvm.JvmField
import kotlin.jvm.JvmStatic

@Serializable(with = ByteVector.Serializer::class)
public open class ByteVector(internal val bytes: ByteArray, internal val offset: Int, private val size: Int) {
public constructor(bytes: ByteArray) : this(bytes, 0, bytes.size)
public constructor(input: String) : this(Hex.decode(input))
Expand Down Expand Up @@ -144,21 +136,8 @@ public open class ByteVector(internal val bytes: ByteArray, internal val offset:
@JvmField
public val empty: ByteVector = ByteVector(ByteArray(0))
}

public object Serializer: KSerializer<ByteVector> {
override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("ByteVector", PrimitiveKind.STRING)

override fun serialize(encoder: Encoder, value: ByteVector) {
encoder.encodeString(value.toHex())
}

override fun deserialize(decoder: Decoder): ByteVector {
return ByteVector(decoder.decodeString())
}
}
}

@Serializable(with = ByteVector32.Serializer::class)
public class ByteVector32(bytes: ByteArray, offset: Int) : ByteVector(bytes, offset, 32) {
public constructor(bytes: ByteArray) : this(bytes, 0)
public constructor(input: String) : this(Hex.decode(input), 0)
Expand All @@ -182,21 +161,8 @@ public class ByteVector32(bytes: ByteArray, offset: Int) : ByteVector(bytes, off
@JvmStatic
public fun fromValidHex(input: String): ByteVector32 = ByteVector32(input)
}

public object Serializer: KSerializer<ByteVector32> {
override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("ByteVector32", PrimitiveKind.STRING)

override fun serialize(encoder: Encoder, value: ByteVector32) {
encoder.encodeString(value.toHex())
}

override fun deserialize(decoder: Decoder): ByteVector32 {
return ByteVector32(decoder.decodeString())
}
}
}

@Serializable(with = ByteVector64.Serializer::class)
public class ByteVector64(bytes: ByteArray, offset: Int) : ByteVector(bytes, offset, 64) {
public constructor(bytes: ByteArray) : this(bytes, 0)
public constructor(input: String) : this(Hex.decode(input), 0)
Expand All @@ -208,18 +174,6 @@ public class ByteVector64(bytes: ByteArray, offset: Int) : ByteVector(bytes, off
@JvmStatic
public fun fromValidHex(input: String): ByteVector64 = ByteVector64(input)
}

public object Serializer: KSerializer<ByteVector64> {
override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("ByteVector64", PrimitiveKind.STRING)

override fun serialize(encoder: Encoder, value: ByteVector64) {
encoder.encodeString(value.toHex())
}

override fun deserialize(decoder: Decoder): ByteVector64 {
return ByteVector64(decoder.decodeString())
}
}
}

public fun ByteArray.byteVector(): ByteVector = ByteVector(this)
Expand Down
9 changes: 2 additions & 7 deletions src/commonMain/kotlin/fr/acinq/bitcoin/DeterministicWallet.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ import fr.acinq.bitcoin.DeterministicWallet.hardened
import fr.acinq.bitcoin.crypto.Pack
import fr.acinq.bitcoin.io.ByteArrayInput
import fr.acinq.bitcoin.io.ByteArrayOutput
import kotlinx.serialization.Serializable
import kotlinx.serialization.Transient
import kotlin.jvm.JvmField
import kotlin.jvm.JvmStatic

Expand All @@ -38,22 +36,20 @@ public object DeterministicWallet {
@JvmStatic
public fun isHardened(index: Long): Boolean = index >= hardenedKeyIndex

@Serializable
public data class ExtendedPrivateKey(
@JvmField val secretkeybytes: ByteVector32,
@JvmField val chaincode: ByteVector32,
@JvmField val depth: Int,
@JvmField val path: KeyPath,
@JvmField val parent: Long
) {
@JvmField @Transient
@JvmField
val privateKey: PrivateKey = PrivateKey(secretkeybytes)

@JvmField @Transient
@JvmField
val publicKey: PublicKey = privateKey.publicKey()
}

@Serializable
public data class ExtendedPublicKey(
@JvmField val publickeybytes: ByteVector,
@JvmField val chaincode: ByteVector32,
Expand Down Expand Up @@ -272,7 +268,6 @@ public object DeterministicWallet {
public const val vpub: Int = 0x045f1cf6
}

@Serializable
public data class KeyPath(@JvmField val path: List<Long>) {
public constructor(path: String) : this(computePath(path))

Expand Down
2 changes: 0 additions & 2 deletions src/commonMain/kotlin/fr/acinq/bitcoin/PrivateKey.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@ package fr.acinq.bitcoin

import fr.acinq.secp256k1.Hex
import fr.acinq.secp256k1.Secp256k1
import kotlinx.serialization.Serializable
import kotlin.jvm.JvmField
import kotlin.jvm.JvmStatic

@Serializable
public data class PrivateKey(@JvmField val value: ByteVector32) {
public constructor(data: ByteArray) : this(
when {
Expand Down
2 changes: 0 additions & 2 deletions src/commonMain/kotlin/fr/acinq/bitcoin/PublicKey.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@ package fr.acinq.bitcoin

import fr.acinq.secp256k1.Hex
import fr.acinq.secp256k1.Secp256k1
import kotlinx.serialization.Serializable
import kotlin.jvm.JvmField
import kotlin.jvm.JvmStatic

@Serializable
public data class PublicKey(@JvmField val value: ByteVector) {
public constructor(data: ByteArray) : this(ByteVector(data))

Expand Down
2 changes: 0 additions & 2 deletions src/commonMain/kotlin/fr/acinq/bitcoin/Satoshi.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package fr.acinq.bitcoin

import kotlinx.serialization.Serializable

@Serializable
public data class Satoshi(val sat: Long) : Comparable<Satoshi> {
// @formatter:off
public operator fun plus(other: Satoshi): Satoshi = Satoshi(sat + other.sat)
Expand Down
13 changes: 3 additions & 10 deletions src/commonMain/kotlin/fr/acinq/bitcoin/Transaction.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ import fr.acinq.bitcoin.io.ByteArrayOutput
import fr.acinq.bitcoin.io.Input
import fr.acinq.bitcoin.io.Output
import fr.acinq.secp256k1.Hex
import kotlinx.serialization.Serializable
import kotlinx.serialization.Transient
import kotlin.jvm.JvmField
import kotlin.jvm.JvmStatic

Expand All @@ -33,7 +31,6 @@ import kotlin.jvm.JvmStatic
* @param index index of the output in tx that we want to refer to
*/
@OptIn(ExperimentalUnsignedTypes::class)
@Serializable
public data class OutPoint(@JvmField val hash: ByteVector32, @JvmField val index: Long) : BtcSerializable<OutPoint> {
public constructor(hash: ByteArray, index: Long) : this(hash.byteVector32(), index)

Expand All @@ -47,7 +44,7 @@ public data class OutPoint(@JvmField val hash: ByteVector32, @JvmField val index
*
* @return the id of the transaction this output belongs to
*/
@JvmField @Transient
@JvmField
public val txid: ByteVector32 = hash.reversed()

public val isCoinbase: Boolean get() = isCoinbase(this)
Expand Down Expand Up @@ -79,7 +76,6 @@ public data class OutPoint(@JvmField val hash: ByteVector32, @JvmField val index
override fun serializer(): BtcSerializer<OutPoint> = OutPoint
}

@Serializable
public data class ScriptWitness(@JvmField val stack: List<ByteVector>) : BtcSerializable<ScriptWitness> {
public constructor() : this(listOf())

Expand Down Expand Up @@ -128,7 +124,6 @@ public data class ScriptWitness(@JvmField val stack: List<ByteVector>) : BtcSeri
* @param witness Transaction witness (i.e. what is in sig script for standard transactions).
*/
@OptIn(ExperimentalUnsignedTypes::class)
@Serializable
public data class TxIn(
@JvmField val outPoint: OutPoint,
@JvmField val signatureScript: ByteVector,
Expand Down Expand Up @@ -215,7 +210,6 @@ public data class TxIn(
}

@OptIn(ExperimentalUnsignedTypes::class)
@Serializable
public data class TxOut(@JvmField val amount: Satoshi, @JvmField val publicKeyScript: ByteVector) : BtcSerializable<TxOut> {

public constructor(amount: Satoshi, publicKeyScript: ByteArray) : this(amount, publicKeyScript.byteVector())
Expand Down Expand Up @@ -264,7 +258,6 @@ public data class TxOut(@JvmField val amount: Satoshi, @JvmField val publicKeySc
}

@OptIn(ExperimentalUnsignedTypes::class)
@Serializable
public data class Transaction(
@JvmField val version: Long,
@JvmField val txIn: List<TxIn>,
Expand All @@ -275,7 +268,7 @@ public data class Transaction(

public val hasWitness: Boolean get() = txIn.any { it.hasWitness }

@JvmField @Transient
@JvmField
public val hash: ByteVector32 = ByteVector32(
Crypto.hash256(
Transaction.write(
Expand All @@ -285,7 +278,7 @@ public data class Transaction(
)
)

@JvmField @Transient
@JvmField
public val txid: ByteVector32 = hash.reversed()

/**
Expand Down

0 comments on commit eb5478e

Please sign in to comment.