Skip to content

Commit

Permalink
Expose Extrinsic encoding without a length
Browse files Browse the repository at this point in the history
  • Loading branch information
valentunn committed Nov 29, 2024
1 parent 5e7891c commit 53d0bf2
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ import io.novasama.substrate_sdk_android.runtime.definitions.types.Type
import io.novasama.substrate_sdk_android.runtime.definitions.types.bytes
import io.novasama.substrate_sdk_android.runtime.definitions.types.errors.EncodeDecodeException
import io.novasama.substrate_sdk_android.runtime.definitions.types.toByteArray
import io.novasama.substrate_sdk_android.runtime.definitions.types.useScaleWriter
import io.novasama.substrate_sdk_android.scale.dataType.byte
import io.novasama.substrate_sdk_android.scale.dataType.compactInt
import io.novasama.substrate_sdk_android.scale.dataType.toByteArray
import io.novasama.substrate_sdk_android.scale.utils.directWrite

private val SIGNED_MASK = 0b1000_0000.toUByte()

Expand Down Expand Up @@ -68,17 +71,26 @@ object Extrinsic : Type<Extrinsic.Instance>("ExtrinsicsDecoder") {
runtime: RuntimeSnapshot,
value: Instance
) {
val callBytes = GenericCall.toByteArray(runtime, value.call)
encode(scaleCodecWriter, runtime, value, encodeLength = true)
}

encode(scaleCodecWriter, runtime, value.signature, callBytes)
fun encodeWithoutLength(
scaleCodecWriter: ScaleCodecWriter,
runtime: RuntimeSnapshot,
value: Instance
) {
encode(scaleCodecWriter, runtime, value, encodeLength = false)
}

private fun encode(
scaleCodecWriter: ScaleCodecWriter,
runtime: RuntimeSnapshot,
signature: Signature?,
callBytes: ByteArray
value: Instance,
encodeLength: Boolean,
) {
val signature = value.signature
val callBytes = GenericCall.toByteArray(runtime, value.call)

val isSigned = signature != null

val extrinsicVersion = runtime.metadata.extrinsic.version.toInt().toUByte()
Expand All @@ -98,7 +110,11 @@ object Extrinsic : Type<Extrinsic.Instance>("ExtrinsicsDecoder") {

val extrinsicBodyBytes = byteArrayOf(encodedVersion) + signatureWrapperBytes + callBytes

Bytes.encode(scaleCodecWriter, runtime, extrinsicBodyBytes)
if (encodeLength) {
Bytes.encode(scaleCodecWriter, runtime, extrinsicBodyBytes)
} else {
scaleCodecWriter.directWrite(extrinsicBodyBytes)
}
}

override fun isValidInstance(instance: Any?): Boolean {
Expand Down Expand Up @@ -126,3 +142,7 @@ object Extrinsic : Type<Extrinsic.Instance>("ExtrinsicsDecoder") {
throw EncodeDecodeException("Cannot resolve $name type, which is required to work with Extrinsic")
}
}

fun Extrinsic.toBytesWithoutLength(runtime: RuntimeSnapshot, value: Extrinsic.Instance): ByteArray {
return useScaleWriter { encodeWithoutLength(this, runtime, value) }
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package io.novasama.substrate_sdk_android.runtime.extrinsic.signer

import io.novasama.substrate_sdk_android.runtime.RuntimeSnapshot
import io.novasama.substrate_sdk_android.runtime.definitions.types.generics.Extrinsic
import io.novasama.substrate_sdk_android.runtime.definitions.types.generics.toBytesWithoutLength
import io.novasama.substrate_sdk_android.runtime.definitions.types.toHex
import io.novasama.substrate_sdk_android.runtime.definitions.types.toHexUntyped

Expand All @@ -12,6 +13,8 @@ interface SendableExtrinsic {
val signatureHex: String

val extrinsicHex: String

val bytesWithoutLength: ByteArray
}

fun SendableExtrinsic(
Expand All @@ -30,6 +33,10 @@ private class RealSendableExtrinsic(
createExtrinsicHex()
}

override val bytesWithoutLength: ByteArray by lazy {
Extrinsic.toBytesWithoutLength(runtime, extrinsic)
}

override val signatureHex by lazy {
createSignatureHex()
}
Expand Down

0 comments on commit 53d0bf2

Please sign in to comment.