Skip to content

Commit

Permalink
Refactor versioned signing
Browse files Browse the repository at this point in the history
  • Loading branch information
valentunn committed Dec 16, 2024
1 parent 98ce821 commit 364e21c
Show file tree
Hide file tree
Showing 16 changed files with 441 additions and 370 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.novasama.substrate_sdk_android.runtime.definitions.types.generics

import io.novasama.substrate_sdk_android.encrypt.EncryptionType
import io.novasama.substrate_sdk_android.runtime.AccountId
import io.novasama.substrate_sdk_android.runtime.definitions.types.composite.DictEnum
import io.novasama.substrate_sdk_android.runtime.extrinsic.v5.transactionExtension.extensions.verifySignature.VerifySignature
import java.util.Locale
Expand All @@ -17,13 +18,13 @@ fun Extrinsic.Instance.signatureInstance(): Any? {
}
}

fun Extrinsic.Instance.signerIdentifier(): Any? {
fun Extrinsic.Instance.signer(): AccountId? {
return when (val type = type) {
Extrinsic.ExtrinsicType.Bare -> null

is Extrinsic.ExtrinsicType.GeneralTransaction -> type.findSignerIdentifierInExplicits()
is Extrinsic.ExtrinsicType.GeneralTransaction -> type.findSignerInExplicits()

is Extrinsic.ExtrinsicType.Signed -> type.accountIdentifier
is Extrinsic.ExtrinsicType.Signed -> extractAccountId(type.accountIdentifier)
}
}

Expand All @@ -37,9 +38,9 @@ fun Extrinsic.Instance.explicits(): ExtrinsicPayloadExtrasInstance? {
}
}

private fun Extrinsic.ExtrinsicType.GeneralTransaction.findSignerIdentifierInExplicits(): Any? {
private fun Extrinsic.ExtrinsicType.GeneralTransaction.findSignerInExplicits(): AccountId? {
val explicit = extensionExplicits[VerifySignature.ID]
return VerifySignature.getSignatureFromExplicit(explicit)?.account
return VerifySignature.getSignatureFromExplicit(explicit)?.accountId
}

fun Extrinsic.Instance.tryExtractMultiSignature(): MultiSignature? {
Expand All @@ -59,6 +60,20 @@ private fun Extrinsic.ExtrinsicType.GeneralTransaction.findSignatureInExplicits(
return VerifySignature.getSignatureFromExplicit(explicit)?.signature
}

fun extractAccountId(dynamicInstance: Any?): AccountId =
when (dynamicInstance) {
// MultiAddress
is DictEnum.Entry<*> -> {
require(dynamicInstance.name == "Id")

dynamicInstance.value as AccountId
}
// GenericAccountId or EthereumAddress
is ByteArray -> dynamicInstance

else -> error("Cannot extract account id")
}

private val EncryptionType.multiSignatureName
get() = rawName.capitalize(Locale.ROOT)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import io.novasama.substrate_sdk_android.runtime.AccountId
import io.novasama.substrate_sdk_android.runtime.RuntimeSnapshot
import io.novasama.substrate_sdk_android.runtime.definitions.types.generics.Era
import io.novasama.substrate_sdk_android.runtime.definitions.types.generics.GenericCall
import io.novasama.substrate_sdk_android.runtime.extrinsic.builder.ExtrinsicBuilder
import io.novasama.substrate_sdk_android.runtime.extrinsic.v5.transactionExtension.extensions.ChargeTransactionPayment
import io.novasama.substrate_sdk_android.runtime.extrinsic.v5.transactionExtension.extensions.CheckGenesis
import io.novasama.substrate_sdk_android.runtime.extrinsic.v5.transactionExtension.extensions.CheckMortality
Expand All @@ -14,6 +15,7 @@ import io.novasama.substrate_sdk_android.runtime.extrinsic.v5.transactionExtensi
import io.novasama.substrate_sdk_android.runtime.extrinsic.v5.transactionExtension.extensions.checkMetadataHash.CheckMetadataHashMode
import io.novasama.substrate_sdk_android.runtime.extrinsic.v5.transactionExtension.extensions.verifySignature.GeneralTransactionSigner
import io.novasama.substrate_sdk_android.runtime.extrinsic.v5.transactionExtension.extensions.verifySignature.VerifySignature
import io.novasama.substrate_sdk_android.runtime.extrinsic.v5.transactionExtension.extensions.verifySignature.VerifySignatureMode
import io.novasama.substrate_sdk_android.runtime.metadata.call
import io.novasama.substrate_sdk_android.runtime.metadata.module
import io.novasama.substrate_sdk_android.wsrpc.request.runtime.chain.RuntimeVersion
Expand Down Expand Up @@ -58,13 +60,15 @@ fun ExtrinsicBuilder(
era: Era = Era.Immortal,
tip: BigInteger = DEFAULT_TIP,
checkMetadataHash: CheckMetadataHashMode = CheckMetadataHashMode.Disabled,
extrinsicVersion: ExtrinsicVersion = ExtrinsicVersion.V4,
extrinsicVersion: ExtrinsicVersion = ExtrinsicVersion.V4(VerifySignatureMode.from(signer, accountId)),
batchMode: BatchMode = BatchMode.BATCH,
): ExtrinsicBuilder {
return ExtrinsicBuilder(runtime, extrinsicVersion, batchMode).apply {
if (accountId != null && signer != null) {
setTransactionExtension(VerifySignature.enabled(signer, accountId))
if (extrinsicVersion is ExtrinsicVersion.V5) {
val mode = VerifySignatureMode.from(signer, accountId)
setTransactionExtension(VerifySignature(mode))
}

setTransactionExtension(CheckNonce(nonce))
setTransactionExtension(CheckMortality(era, blockHash))
setTransactionExtension(CheckGenesis(genesisHash))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
package io.novasama.substrate_sdk_android.runtime.extrinsic

import io.novasama.substrate_sdk_android.runtime.extrinsic.v5.transactionExtension.extensions.verifySignature.VerifySignatureMode

private const val EXTENSIONS_VERSION_DEFAULT: Byte = 0

sealed class ExtrinsicVersion {

object V4 : ExtrinsicVersion()
class V4(
val verifySignatureMode: VerifySignatureMode,
): ExtrinsicVersion()

class V5(val extensionVersion: Byte = EXTENSIONS_VERSION_DEFAULT) : ExtrinsicVersion()
class V5(
val extensionVersion: Byte = EXTENSIONS_VERSION_DEFAULT,
) : ExtrinsicVersion()
}
Loading

0 comments on commit 364e21c

Please sign in to comment.