From f51f952481425719da2caa17e5c1f2fe9e7745d2 Mon Sep 17 00:00:00 2001 From: Russel Date: Sun, 23 Jun 2024 06:41:36 +0200 Subject: [PATCH] extrinsic builder memo --- Example/Podfile.lock | 4 +- SubstrateSdk.podspec | 2 +- .../Classes/Extrinsic/ExtrinsicBuilder.swift | 69 +++++++++++++++++++ .../Extrinsic/ExtrinsicBuilderSnapshot.swift | 30 ++++++++ .../Extrinsic/ExtrinsicSignatureParams.swift | 13 ++++ 5 files changed, 115 insertions(+), 3 deletions(-) create mode 100644 SubstrateSdk/Classes/Extrinsic/ExtrinsicBuilderSnapshot.swift create mode 100644 SubstrateSdk/Classes/Extrinsic/ExtrinsicSignatureParams.swift diff --git a/Example/Podfile.lock b/Example/Podfile.lock index 1663b86..4a06695 100644 --- a/Example/Podfile.lock +++ b/Example/Podfile.lock @@ -25,7 +25,7 @@ PODS: - scrypt.c (0.1.1) - secp256k1.c (0.1.2) - Starscream (4.0.4) - - SubstrateSdk (3.1.1): + - SubstrateSdk (3.2.0): - BigInt (~> 5.0) - IrohaCrypto/ed25519 (~> 0.9.0) - IrohaCrypto/Scrypt (~> 0.9.0) @@ -87,7 +87,7 @@ SPEC CHECKSUMS: scrypt.c: b42ae06183251329d2b2c620c226fb541a4a3592 secp256k1.c: db47b726585d80f027423682eb369729e61b3b20 Starscream: 5178aed56b316f13fa3bc55694e583d35dd414d9 - SubstrateSdk: d51ad2eeab63e4603adeb5850bc7c095682fd591 + SubstrateSdk: a4190b1ec093bd09490c7ff20e1e03e2d4c45760 SwiftLint: 4fa9579c63416865179bc416f0a92d55f009600d TweetNacl: 3abf4d1d2082b0114e7a67410e300892448951e6 xxHash-Swift: 30bd6a7507b3b7348a277c49b1cb6346c2905ec7 diff --git a/SubstrateSdk.podspec b/SubstrateSdk.podspec index 4dd58ad..9e1fb1f 100644 --- a/SubstrateSdk.podspec +++ b/SubstrateSdk.podspec @@ -8,7 +8,7 @@ Pod::Spec.new do |s| s.name = 'SubstrateSdk' - s.version = '3.1.1' + s.version = '3.2.0' s.summary = 'Utility library that implements clients specific logic to interact with substrate based networks' s.homepage = 'https://github.com/nova-wallet/substrate-sdk-ios' diff --git a/SubstrateSdk/Classes/Extrinsic/ExtrinsicBuilder.swift b/SubstrateSdk/Classes/Extrinsic/ExtrinsicBuilder.swift index 4d8830c..761a5de 100644 --- a/SubstrateSdk/Classes/Extrinsic/ExtrinsicBuilder.swift +++ b/SubstrateSdk/Classes/Extrinsic/ExtrinsicBuilder.swift @@ -40,6 +40,13 @@ public protocol ExtrinsicBuilderProtocol: AnyObject { ) throws -> Data func build(encodingBy encoder: DynamicScaleEncoding, metadata: RuntimeMetadataProtocol) throws -> Data + + func buildExtrinsicSignatureParams( + encodingBy encoder: DynamicScaleEncoding, + metadata: RuntimeMetadataProtocol + ) throws -> ExtrinsicSignatureParams + + func makeMemo() -> ExtrinsicBuilderMemoProtocol } public extension ExtrinsicBuilderProtocol { @@ -86,6 +93,24 @@ public class ExtrinsicBuilder { self.nonce = 0 self.calls = [] } + + init(memo: ExtrinsicBuilderMemo) { + self.specVersion = memo.specVersion + self.transactionVersion = memo.transactionVersion + self.genesisHash = memo.genesisHash + self.calls = memo.calls + self.blockHash = memo.blockHash + self.address = memo.address + self.nonce = memo.nonce + self.era = memo.era + self.tip = memo.tip + self.signature = memo.signature + self.signaturePayloadFormat = memo.signaturePayloadFormat + self.metadataHash = memo.metadataHash + self.batchType = memo.batchType + self.runtimeJsonContext = memo.runtimeJsonContext + self.additionalExtensions = memo.additionalExtensions + } private func prepareExtrinsicCall(for metadata: RuntimeMetadataProtocol) throws -> JSON { guard !calls.isEmpty else { @@ -411,4 +436,48 @@ extension ExtrinsicBuilder: ExtrinsicBuilderProtocol { return try encoder.encode() } + + public func buildExtrinsicSignatureParams( + encodingBy encoder: DynamicScaleEncoding, + metadata: RuntimeMetadataProtocol + ) throws -> ExtrinsicSignatureParams { + let callEncoder = encoder.newEncoder() + let call = try prepareExtrinsicCall(for: metadata) + try callEncoder.append(json: call, type: KnownType.call.name) + let encodedCall = try callEncoder.encode() + + let includedInExtrinsicExtraEncoder = encoder.newEncoder() + try appendExtraToPayload(encodingBy: includedInExtrinsicExtraEncoder) + let encodedExtrinsicExtra = try includedInExtrinsicExtraEncoder.encode() + + let includedInSignatureExtraEncoder = encoder.newEncoder() + try appendAdditionalSigned(encodingBy: includedInSignatureExtraEncoder, metadata: metadata) + let encodedSignatureExtra = try includedInExtrinsicExtraEncoder.encode() + + return ExtrinsicSignatureParams( + encodedCall: encodedCall, + includedInExtrinsicExtra: encodedExtrinsicExtra, + includedInSignatureExtra: encodedSignatureExtra + ) + } + + public func makeMemo() -> ExtrinsicBuilderMemoProtocol { + ExtrinsicBuilderMemo( + specVersion: specVersion, + transactionVersion: transactionVersion, + genesisHash: genesisHash, + calls: calls, + blockHash: blockHash, + address: address, + nonce: nonce, + era: era, + tip: tip, + signature: signature, + signaturePayloadFormat: signaturePayloadFormat, + metadataHash: metadataHash, + batchType: batchType, + runtimeJsonContext: runtimeJsonContext, + additionalExtensions: additionalExtensions + ) + } } diff --git a/SubstrateSdk/Classes/Extrinsic/ExtrinsicBuilderSnapshot.swift b/SubstrateSdk/Classes/Extrinsic/ExtrinsicBuilderSnapshot.swift new file mode 100644 index 0000000..18c99a2 --- /dev/null +++ b/SubstrateSdk/Classes/Extrinsic/ExtrinsicBuilderSnapshot.swift @@ -0,0 +1,30 @@ +import Foundation +import BigInt + +public protocol ExtrinsicBuilderMemoProtocol { + func restoreBuilder() -> ExtrinsicBuilderProtocol +} + +struct ExtrinsicBuilderMemo { + let specVersion: UInt32 + let transactionVersion: UInt32 + let genesisHash: String + let calls: [JSON] + let blockHash: String + let address: JSON? + let nonce: UInt32 + let era: Era + let tip: BigUInt + let signature: ExtrinsicSignature? + let signaturePayloadFormat: ExtrinsicSignaturePayloadFormat + let metadataHash: Data? + let batchType: ExtrinsicBatch + let runtimeJsonContext: RuntimeJsonContext? + let additionalExtensions: [ExtrinsicSignedExtending] +} + +extension ExtrinsicBuilderMemo: ExtrinsicBuilderMemoProtocol { + func restoreBuilder() -> ExtrinsicBuilderProtocol { + ExtrinsicBuilder(memo: self) + } +} diff --git a/SubstrateSdk/Classes/Extrinsic/ExtrinsicSignatureParams.swift b/SubstrateSdk/Classes/Extrinsic/ExtrinsicSignatureParams.swift new file mode 100644 index 0000000..52b6076 --- /dev/null +++ b/SubstrateSdk/Classes/Extrinsic/ExtrinsicSignatureParams.swift @@ -0,0 +1,13 @@ +import Foundation + +public struct ExtrinsicSignatureParams { + public let encodedCall: Data + public let includedInExtrinsicExtra: Data + public let includedInSignatureExtra: Data + + public init(encodedCall: Data, includedInExtrinsicExtra: Data, includedInSignatureExtra: Data) { + self.encodedCall = encodedCall + self.includedInExtrinsicExtra = includedInExtrinsicExtra + self.includedInSignatureExtra = includedInSignatureExtra + } +}