Skip to content

Commit

Permalink
Merge pull request #60 from novasamatech/feature/support-metadata-proof
Browse files Browse the repository at this point in the history
Add parameters generation for extrinsic proof
  • Loading branch information
ERussel authored Jul 1, 2024
2 parents 86b26af + b0c45d8 commit 625b110
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 3 deletions.
4 changes: 2 additions & 2 deletions Example/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion SubstrateSdk.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
69 changes: 69 additions & 0 deletions SubstrateSdk/Classes/Extrinsic/ExtrinsicBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -87,6 +94,24 @@ public class ExtrinsicBuilder {
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 {
throw ExtrinsicBuilderError.missingCall
Expand Down Expand Up @@ -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 includedInSignatureExtraEncoder.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
)
}
}
30 changes: 30 additions & 0 deletions SubstrateSdk/Classes/Extrinsic/ExtrinsicBuilderSnapshot.swift
Original file line number Diff line number Diff line change
@@ -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)
}
}
13 changes: 13 additions & 0 deletions SubstrateSdk/Classes/Extrinsic/ExtrinsicSignatureParams.swift
Original file line number Diff line number Diff line change
@@ -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
}
}

0 comments on commit 625b110

Please sign in to comment.