From 4c4b403ec1a5c05047942e2de44429b01c0dfa4d Mon Sep 17 00:00:00 2001 From: Francisco Gindre Date: Tue, 2 Jul 2024 12:26:10 -0300 Subject: [PATCH] Swift Format --- .../FrostCompanionTests.swift | 2 - .../FrostCompanionUITests.swift | 1 - .../FrostCompanionUITestsLaunchTests.swift | 1 - .../Sources/FrostSwift/Coordinator.swift | 71 ++- FrostSwift/Sources/FrostSwift/Error.swift | 3 +- .../Sources/FrostSwift/FFIConversion.swift | 17 +- FrostSwift/Sources/FrostSwift/FROST.swift | 19 +- .../FrostSwift/SigningParticipant.swift | 12 +- .../Sources/FrostSwift/TrustedDealer.swift | 6 +- .../FrostSwiftFFI/frost_uniffi_sdk.swift | 481 +++++++++--------- FrostSwift/Tests/FrostSwift/ModelTests.swift | 7 +- ...rustedDealerSignatureIntegrationTest.swift | 8 +- .../Tests/FrostSwift/TrustedDealerTests.swift | 10 +- .../NotRedPallasTests/NotRedPallasTests.swift | 7 +- .../NotRedPallasTests/RedPallasTests.swift | 6 +- 15 files changed, 307 insertions(+), 344 deletions(-) diff --git a/Examples/FrostCompanion/FrostCompanionTests/FrostCompanionTests.swift b/Examples/FrostCompanion/FrostCompanionTests/FrostCompanionTests.swift index 9e1673b..c4e7dc6 100644 --- a/Examples/FrostCompanion/FrostCompanionTests/FrostCompanionTests.swift +++ b/Examples/FrostCompanion/FrostCompanionTests/FrostCompanionTests.swift @@ -8,7 +8,6 @@ import XCTest final class FrostCompanionTests: XCTestCase { - override func setUpWithError() throws { // Put setup code here. This method is called before the invocation of each test method in the class. } @@ -31,5 +30,4 @@ final class FrostCompanionTests: XCTestCase { // Put the code you want to measure the time of here. } } - } diff --git a/Examples/FrostCompanion/FrostCompanionUITests/FrostCompanionUITests.swift b/Examples/FrostCompanion/FrostCompanionUITests/FrostCompanionUITests.swift index a065d81..1fec060 100644 --- a/Examples/FrostCompanion/FrostCompanionUITests/FrostCompanionUITests.swift +++ b/Examples/FrostCompanion/FrostCompanionUITests/FrostCompanionUITests.swift @@ -8,7 +8,6 @@ import XCTest final class FrostCompanionUITests: XCTestCase { - override func setUpWithError() throws { // Put setup code here. This method is called before the invocation of each test method in the class. diff --git a/Examples/FrostCompanion/FrostCompanionUITests/FrostCompanionUITestsLaunchTests.swift b/Examples/FrostCompanion/FrostCompanionUITests/FrostCompanionUITestsLaunchTests.swift index 436c364..d1ecb2d 100644 --- a/Examples/FrostCompanion/FrostCompanionUITests/FrostCompanionUITestsLaunchTests.swift +++ b/Examples/FrostCompanion/FrostCompanionUITests/FrostCompanionUITestsLaunchTests.swift @@ -8,7 +8,6 @@ import XCTest final class FrostCompanionUITestsLaunchTests: XCTestCase { - override class var runsForEachTargetApplicationUIConfiguration: Bool { true } diff --git a/FrostSwift/Sources/FrostSwift/Coordinator.swift b/FrostSwift/Sources/FrostSwift/Coordinator.swift index 175a4cc..98a69b0 100644 --- a/FrostSwift/Sources/FrostSwift/Coordinator.swift +++ b/FrostSwift/Sources/FrostSwift/Coordinator.swift @@ -7,8 +7,8 @@ import Foundation import FrostSwiftFFI -enum FROSTCoordinatorError: Error { +enum FROSTCoordinatorError: Error { case repeatedCommitmentFromIdentifier(Identifier) case repeatedSignatureShareFromIdentifier(Identifier) case incorrectNumberOfCommitments(min: UInt16, max: UInt16, found: UInt16) @@ -41,7 +41,7 @@ enum FROSTCoordinatorError: Error { self = .signatureShareDeserializationError case .PublicKeyPackageDeserializationError: self = .publicKeyPackageDeserializationError - case .SignatureShareAggregationFailed(message: let message): + case let .SignatureShareAggregationFailed(message: message): self = .signatureShareAggregationFailed(message: message) case .InvalidRandomizer: self = .invalidRandomizer @@ -111,32 +111,32 @@ public actor NonSigningCoordinator: FROSTCoordinator { throw FROSTCoordinatorError.repeatedCommitmentFromIdentifier(commitment.identifier) } - self.commitments[commitment.identifier] = commitment + commitments[commitment.identifier] = commitment } public func createSigningPackage() throws -> Round2Configuration { - guard self.round2Config?.signingPackage == nil else { + guard round2Config?.signingPackage == nil else { throw FROSTCoordinatorError.signingPackageAlreadyCreated } try validateNumberOfCommitments() - let package = SigningPackage( - package: try newSigningPackage( - message: self.message, - commitments: self.commitments.values.map { $0.commitment } + let package = try SigningPackage( + package: newSigningPackage( + message: message, + commitments: commitments.values.map { $0.commitment } ) ) let randomizedParams = try RandomizedParams( - publicKey: self.publicKeyPackage, + publicKey: publicKeyPackage, signingPackage: package ) let randomizer = try randomizedParams.randomizer() let config = Round2Configuration(signingPackage: package, randomizer: randomizer) - self.round2Config = config + round2Config = config return config } @@ -144,11 +144,11 @@ public actor NonSigningCoordinator: FROSTCoordinator { /// receives the signature share from a partipicant public func receive(signatureShare: SignatureShare) throws { // TODO: validate that the commitment belongs to a known identifier - guard self.signatureShares[signatureShare.identifier] == nil else { + guard signatureShares[signatureShare.identifier] == nil else { throw FROSTCoordinatorError.repeatedSignatureShareFromIdentifier(signatureShare.identifier) } - self.signatureShares[signatureShare.identifier] = signatureShare + signatureShares[signatureShare.identifier] = signatureShare } public func aggregate() throws -> Signature { @@ -161,20 +161,20 @@ public actor NonSigningCoordinator: FROSTCoordinator { let signature = try FrostSwiftFFI.aggregate( signingPackage: round2config.signingPackage.package, - signatureShares: self.signatureShares.values.map { $0.share }, - pubkeyPackage: self.publicKeyPackage.package, + signatureShares: signatureShares.values.map { $0.share }, + pubkeyPackage: publicKeyPackage.package, randomizer: randomizer ) return Signature(signature: signature) } - public func verify(signature: Signature) throws { + public func verify(signature _: Signature) throws { throw FrostError.invalidSignature } func round2ConfigPresent() throws -> Round2Configuration { - guard let config = self.round2Config else { + guard let config = round2Config else { throw FROSTCoordinatorError.signingPackageMissing } @@ -182,14 +182,14 @@ public actor NonSigningCoordinator: FROSTCoordinator { } func validateNumberOfCommitments() throws { - guard commitments.count >= configuration.minSigners && - commitments.count <= configuration.maxSigners + guard commitments.count >= configuration.minSigners && + commitments.count <= configuration.maxSigners else { throw FROSTCoordinatorError.incorrectNumberOfCommitments( - min: configuration.minSigners, - max: configuration.maxSigners, - found: UInt16(commitments.count) - ) + min: configuration.minSigners, + max: configuration.maxSigners, + found: UInt16(commitments.count) + ) } } } @@ -212,16 +212,15 @@ public actor SigningCoordinator: FROSTCoordinator { self.configuration = configuration self.publicKeyPackage = publicKeyPackage self.message = message - self.nonSigningCoordinator = try NonSigningCoordinator( + nonSigningCoordinator = try NonSigningCoordinator( configuration: configuration, publicKeyPackage: publicKeyPackage, message: message ) self.signingParticipant = signingParticipant - } - public init(configuration: Configuration, publicKeyPackage: PublicKeyPackage, keyPackage: KeyPackage, message: Message) throws { + public init(configuration: Configuration, publicKeyPackage: PublicKeyPackage, keyPackage: KeyPackage, message: Message) throws { let signingParticipant = SigningParticipant( keyPackage: keyPackage, publicKey: publicKeyPackage @@ -233,7 +232,6 @@ public actor SigningCoordinator: FROSTCoordinator { signingParticipant: signingParticipant, message: message ) - } public func receive(commitment: SigningCommitments) async throws { @@ -242,36 +240,35 @@ public actor SigningCoordinator: FROSTCoordinator { public func createSigningPackage() async throws -> Round2Configuration { // sends its own commitment before creating the signign package - let commitment = try self.signingParticipant.commit() - try await self.nonSigningCoordinator.receive(commitment: commitment) + let commitment = try signingParticipant.commit() + try await nonSigningCoordinator.receive(commitment: commitment) // create the signing package - let round2Config = try await self.nonSigningCoordinator.createSigningPackage() + let round2Config = try await nonSigningCoordinator.createSigningPackage() self.round2Config = round2Config return round2Config } public func receive(signatureShare: SignatureShare) async throws { - try await self.nonSigningCoordinator.receive(signatureShare: signatureShare) + try await nonSigningCoordinator.receive(signatureShare: signatureShare) } public func aggregate() async throws -> Signature { - let round2Config = try await self.nonSigningCoordinator.round2ConfigPresent() + let round2Config = try await nonSigningCoordinator.round2ConfigPresent() // create own signature share - self.signingParticipant.receive(round2Config: round2Config) - let signatureShare = try self.signingParticipant.sign() + signingParticipant.receive(round2Config: round2Config) + let signatureShare = try signingParticipant.sign() // sends its own share before creating the signature - try await self.nonSigningCoordinator.receive(signatureShare: signatureShare) + try await nonSigningCoordinator.receive(signatureShare: signatureShare) // produce signature by aggregating all shares. - let signature = try await self.nonSigningCoordinator.aggregate() + let signature = try await nonSigningCoordinator.aggregate() return signature - } public func verify(signature: Signature) async throws { - try await self.nonSigningCoordinator.verify(signature: signature) + try await nonSigningCoordinator.verify(signature: signature) } } diff --git a/FrostSwift/Sources/FrostSwift/Error.swift b/FrostSwift/Sources/FrostSwift/Error.swift index f05c01b..cdc32c4 100644 --- a/FrostSwift/Sources/FrostSwift/Error.swift +++ b/FrostSwift/Sources/FrostSwift/Error.swift @@ -3,13 +3,12 @@ // // // Created by Pacu in 2024. -// +// import Foundation import FrostSwiftFFI public enum FrostError: Error { - case invalidConfiguration case invalidSignature case malformedIdentifier diff --git a/FrostSwift/Sources/FrostSwift/FFIConversion.swift b/FrostSwift/Sources/FrostSwift/FFIConversion.swift index 52fb0e4..686ebf7 100644 --- a/FrostSwift/Sources/FrostSwift/FFIConversion.swift +++ b/FrostSwift/Sources/FrostSwift/FFIConversion.swift @@ -1,6 +1,6 @@ // // FFIConversion.swift -// +// // // Created by Pacu on 26-06-2024. // @@ -11,9 +11,9 @@ import FrostSwiftFFI extension Configuration { func intoFFIConfiguration() -> FrostSwiftFFI.Configuration { FrostSwiftFFI.Configuration( - minSigners: self.minSigners, - maxSigners: self.maxSigners, - secret: self.secret ?? Data() + minSigners: minSigners, + maxSigners: maxSigners, + secret: secret ?? Data() ) } } @@ -26,21 +26,20 @@ extension ParticipantIdentifier { extension Identifier { func toParticipantIdentifier() -> ParticipantIdentifier { - self.id + id } } extension TrustedKeyGeneration { func toKeyGeneration() -> TrustedDealerCoordinator.KeyGeneration { - var keys = [Identifier: SecretShare]() - self.secretShares.forEach { - keys[$0.key.toIdentifier()] = SecretShare(share: $0.value) + for secretShare in secretShares { + keys[secretShare.key.toIdentifier()] = SecretShare(share: secretShare.value) } return TrustedDealerCoordinator.KeyGeneration( - publicKeyPackage: PublicKeyPackage(package: self.publicKeyPackage), + publicKeyPackage: PublicKeyPackage(package: publicKeyPackage), secretShares: keys ) } diff --git a/FrostSwift/Sources/FrostSwift/FROST.swift b/FrostSwift/Sources/FrostSwift/FROST.swift index 963f629..f9eb8f7 100644 --- a/FrostSwift/Sources/FrostSwift/FROST.swift +++ b/FrostSwift/Sources/FrostSwift/FROST.swift @@ -1,5 +1,5 @@ -import FrostSwiftFFI import Foundation +import FrostSwiftFFI enum FrostSwift { static func frost() -> String { @@ -41,11 +41,12 @@ public struct PublicKeyPackage { randomizer: randomizer.randomizer, message: message, signature: signature.signature, - pubkey: self.package + pubkey: package ) } } } + /// Identifier of a signing participant of a FROST signature scheme. /// an identifier is unique within a signature scheme. /// - Note: a participant that is the same actor may (and most probably will) have @@ -54,7 +55,7 @@ public struct Identifier: Hashable { let id: ParticipantIdentifier init(participant: ParticipantIdentifier) { - self.id = participant + id = participant } public init?(with scalar: UInt16) { @@ -114,15 +115,15 @@ public struct RandomizedParams { } public init(publicKey: PublicKeyPackage, signingPackage: SigningPackage) throws { - self.params = try randomizedParamsFromPublicKeyAndSigningPackage( + params = try randomizedParamsFromPublicKeyAndSigningPackage( publicKey: publicKey.package, signingPackage: signingPackage.package ) } public func randomizer() throws -> Randomizer { - Randomizer( - randomizer: try FrostSwiftFFI.randomizerFromParams( + try Randomizer( + randomizer: FrostSwiftFFI.randomizerFromParams( randomizedParams: params ) ) @@ -141,7 +142,6 @@ public struct VerifyingKey { } public var asString: String { key } - } /// _Secret_ key package of a given signing participant. @@ -156,7 +156,7 @@ public struct KeyPackage { } public var identifier: Identifier { - self.package.identifier.toIdentifier() + package.identifier.toIdentifier() } } @@ -168,7 +168,7 @@ public struct SecretShare { /// Verifies the Secret share and creates a `KeyPackage` public func verifyAndGetKeyPackage() throws -> KeyPackage { - let package = try verifyAndGetKeyPackageFrom(secretShare: self.share) + let package = try verifyAndGetKeyPackageFrom(secretShare: share) return KeyPackage(package: package) } @@ -223,6 +223,7 @@ public struct SignatureShare: Equatable { public struct SigningPackage: Equatable { let package: FrostSigningPackage } + /// Signature produced by aggregating the `SignatureShare`s of the /// different _t_ participants of a threshold signature. /// - note: to validate a signature use the `PublicKeyPackage` method. diff --git a/FrostSwift/Sources/FrostSwift/SigningParticipant.swift b/FrostSwift/Sources/FrostSwift/SigningParticipant.swift index 2b0c2be..f1f3fc4 100644 --- a/FrostSwift/Sources/FrostSwift/SigningParticipant.swift +++ b/FrostSwift/Sources/FrostSwift/SigningParticipant.swift @@ -1,6 +1,6 @@ // -// Participant.swift -// +// SigningParticipant.swift +// // // Created by Pacu on 26-06-2024. // @@ -9,7 +9,6 @@ import Foundation import FrostSwiftFFI enum ParticipantError: Error { - /// participant attempted to produce a signature share but /// it was missing the corresponing nonces. generate a commitment first case missingSigningNonces @@ -49,7 +48,7 @@ public class SigningParticipant { let commitments = try generateNoncesAndCommitments(keyPackage: keyPackage.package) let nonces = SigningNonces(nonces: commitments.nonces) - self.signingNonces = nonces + signingNonces = nonces return SigningCommitments(commitment: commitments.commitments) } @@ -67,7 +66,7 @@ public class SigningParticipant { throw ParticipantError.missingSigningNonces } - guard let round2Config = self.round2Config else { + guard let round2Config = round2Config else { throw ParticipantError.missingRound2Config } @@ -78,11 +77,10 @@ public class SigningParticipant { let share = try FrostSwiftFFI.sign( signingPackage: round2Config.signingPackage.package, nonces: nonces.nonces, - keyPackage: self.keyPackage.package, + keyPackage: keyPackage.package, randomizer: randomizer.randomizer ) return SignatureShare(share: share) } - } diff --git a/FrostSwift/Sources/FrostSwift/TrustedDealer.swift b/FrostSwift/Sources/FrostSwift/TrustedDealer.swift index da517d5..46a9540 100644 --- a/FrostSwift/Sources/FrostSwift/TrustedDealer.swift +++ b/FrostSwift/Sources/FrostSwift/TrustedDealer.swift @@ -25,7 +25,6 @@ public struct Configuration { self.minSigners = minSigners self.secret = secret } - } /// Represents a Trusted Dealer Key Generation Coordinator. @@ -33,7 +32,6 @@ public struct Configuration { /// - Note: `SecretShare`s must be sent to participants through encrypted and /// authenticated communication channels!. public struct TrustedDealerCoordinator { - public struct KeyGeneration { public let publicKeyPackage: PublicKeyPackage public let secretShares: [Identifier: SecretShare] @@ -57,9 +55,9 @@ public struct TrustedDealerCoordinator { } let keys = try trustedDealerKeygenWithIdentifiers( - configuration: self.configuration.intoFFIConfiguration(), + configuration: configuration.intoFFIConfiguration(), participants: ParticipantList( - identifiers: identifiers.map { $0.toParticipantIdentifier()} + identifiers: identifiers.map { $0.toParticipantIdentifier() } ) ) diff --git a/FrostSwift/Sources/FrostSwiftFFI/frost_uniffi_sdk.swift b/FrostSwift/Sources/FrostSwiftFFI/frost_uniffi_sdk.swift index 7e8158d..4b8433b 100644 --- a/FrostSwift/Sources/FrostSwiftFFI/frost_uniffi_sdk.swift +++ b/FrostSwift/Sources/FrostSwiftFFI/frost_uniffi_sdk.swift @@ -6,10 +6,10 @@ import Foundation // might be in a separate module, or it might be compiled inline into // this module. This is a bit of light hackery to work with both. #if canImport(frost_uniffi_sdkFFI) -import frost_uniffi_sdkFFI + import frost_uniffi_sdkFFI #endif -fileprivate extension RustBuffer { +private extension RustBuffer { // Allocate a new buffer, copying the contents of a `UInt8` array. init(bytes: [UInt8]) { let rbuf = bytes.withUnsafeBufferPointer { ptr in @@ -29,7 +29,7 @@ fileprivate extension RustBuffer { } } -fileprivate extension ForeignBytes { +private extension ForeignBytes { init(bufferPointer: UnsafeBufferPointer) { self.init(len: Int32(bufferPointer.count), data: bufferPointer.baseAddress) } @@ -42,7 +42,7 @@ fileprivate extension ForeignBytes { // Helper classes/extensions that don't change. // Someday, this will be in a library of its own. -fileprivate extension Data { +private extension Data { init(rustBuffer: RustBuffer) { // TODO: This copies the buffer. Can we read directly from a // Rust buffer? @@ -72,7 +72,7 @@ private func createReader(data: Data) -> (data: Data, offset: Data.Index) { // the offset on success. Throws if reading the integer would move the // offset past the end of the buffer. private func readInt(_ reader: inout (data: Data, offset: Data.Index)) throws -> T { - let range = reader.offset...size + let range = reader.offset ..< reader.offset + MemoryLayout.size guard reader.data.count >= range.upperBound else { throw UniffiInternalError.bufferOverflow } @@ -82,7 +82,7 @@ private func readInt(_ reader: inout (data: Data, offset: return value as! T } var value: T = 0 - _ = withUnsafeMutableBytes(of: &value, { reader.data.copyBytes(to: $0, from: range)}) + _ = withUnsafeMutableBytes(of: &value) { reader.data.copyBytes(to: $0, from: range) } reader.offset = range.upperBound return value.bigEndian } @@ -90,26 +90,26 @@ private func readInt(_ reader: inout (data: Data, offset: // Reads an arbitrary number of bytes, to be used to read // raw bytes, this is useful when lifting strings private func readBytes(_ reader: inout (data: Data, offset: Data.Index), count: Int) throws -> [UInt8] { - let range = reader.offset..<(reader.offset+count) + let range = reader.offset ..< (reader.offset + count) guard reader.data.count >= range.upperBound else { throw UniffiInternalError.bufferOverflow } var value = [UInt8](repeating: 0, count: count) - value.withUnsafeMutableBufferPointer({ buffer in + value.withUnsafeMutableBufferPointer { buffer in reader.data.copyBytes(to: buffer, from: range) - }) + } reader.offset = range.upperBound return value } // Reads a float at the current offset. private func readFloat(_ reader: inout (data: Data, offset: Data.Index)) throws -> Float { - return Float(bitPattern: try readInt(&reader)) + return try Float(bitPattern: readInt(&reader)) } // Reads a float at the current offset. private func readDouble(_ reader: inout (data: Data, offset: Data.Index)) throws -> Double { - return Double(bitPattern: try readInt(&reader)) + return try Double(bitPattern: readInt(&reader)) } // Indicates if the offset has reached the end of the buffer. @@ -159,7 +159,7 @@ private protocol FfiConverter { } // Types conforming to `Primitive` pass themselves directly over the FFI. -private protocol FfiConverterPrimitive: FfiConverter where FfiType == SwiftType { } +private protocol FfiConverterPrimitive: FfiConverter where FfiType == SwiftType {} extension FfiConverterPrimitive { public static func lift(_ value: FfiType) throws -> SwiftType { @@ -187,11 +187,12 @@ extension FfiConverterRustBuffer { } public static func lower(_ value: SwiftType) -> RustBuffer { - var writer = createWriter() - write(value, into: &writer) - return RustBuffer(bytes: writer) + var writer = createWriter() + write(value, into: &writer) + return RustBuffer(bytes: writer) } } + // An error type for FFI errors. These errors occur at the UniFFI level, not // the library level. private enum UniffiInternalError: LocalizedError { @@ -225,11 +226,11 @@ private let CALL_ERROR: Int8 = 1 private let CALL_PANIC: Int8 = 2 private let CALL_CANCELLED: Int8 = 3 -fileprivate extension RustCallStatus { +private extension RustCallStatus { init() { self.init( code: CALL_SUCCESS, - errorBuf: RustBuffer.init( + errorBuf: RustBuffer( capacity: 0, len: 0, data: nil @@ -244,7 +245,8 @@ private func rustCall(_ callback: (UnsafeMutablePointer) -> T private func rustCallWithError( _ errorHandler: @escaping (RustBuffer) throws -> Error, - _ callback: (UnsafeMutablePointer) -> T) throws -> T { + _ callback: (UnsafeMutablePointer) -> T +) throws -> T { try makeRustCall(callback, errorHandler: errorHandler) } @@ -253,7 +255,7 @@ private func makeRustCall( errorHandler: ((RustBuffer) throws -> Error)? ) throws -> T { uniffiEnsureInitialized() - var callStatus = RustCallStatus.init() + var callStatus = RustCallStatus() let returnedVal = callback(&callStatus) try uniffiCheckCallStatus(callStatus: callStatus, errorHandler: errorHandler) return returnedVal @@ -264,33 +266,33 @@ private func uniffiCheckCallStatus( errorHandler: ((RustBuffer) throws -> Error)? ) throws { switch callStatus.code { - case CALL_SUCCESS: - return + case CALL_SUCCESS: + return - case CALL_ERROR: - if let errorHandler = errorHandler { - throw try errorHandler(callStatus.errorBuf) - } else { - callStatus.errorBuf.deallocate() - throw UniffiInternalError.unexpectedRustCallError - } + case CALL_ERROR: + if let errorHandler = errorHandler { + throw try errorHandler(callStatus.errorBuf) + } else { + callStatus.errorBuf.deallocate() + throw UniffiInternalError.unexpectedRustCallError + } - case CALL_PANIC: - // When the rust code sees a panic, it tries to construct a RustBuffer - // with the message. But if that code panics, then it just sends back - // an empty buffer. - if callStatus.errorBuf.len > 0 { - throw UniffiInternalError.rustPanic(try FfiConverterString.lift(callStatus.errorBuf)) - } else { - callStatus.errorBuf.deallocate() - throw UniffiInternalError.rustPanic("Rust panic") - } + case CALL_PANIC: + // When the rust code sees a panic, it tries to construct a RustBuffer + // with the message. But if that code panics, then it just sends back + // an empty buffer. + if callStatus.errorBuf.len > 0 { + throw try UniffiInternalError.rustPanic(FfiConverterString.lift(callStatus.errorBuf)) + } else { + callStatus.errorBuf.deallocate() + throw UniffiInternalError.rustPanic("Rust panic") + } - case CALL_CANCELLED: - throw CancellationError() + case CALL_CANCELLED: + throw CancellationError() - default: - throw UniffiInternalError.unexpectedRustCallStatusCode + default: + throw UniffiInternalError.unexpectedRustCallStatusCode } } @@ -337,7 +339,7 @@ private struct FfiConverterString: FfiConverter { public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> String { let len: Int32 = try readInt(&buf) - return String(bytes: try readBytes(&buf, count: Int(len)), encoding: String.Encoding.utf8)! + return try String(bytes: readBytes(&buf, count: Int(len)), encoding: String.Encoding.utf8)! } public static func write(_ value: String, into buf: inout [UInt8]) { @@ -352,7 +354,7 @@ private struct FfiConverterData: FfiConverterRustBuffer { public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> Data { let len: Int32 = try readInt(&buf) - return Data(try readBytes(&buf, count: Int(len))) + return try Data(readBytes(&buf, count: Int(len))) } public static func write(_ value: Data, into buf: inout [UInt8]) { @@ -362,9 +364,7 @@ private struct FfiConverterData: FfiConverterRustBuffer { } } -public protocol DKGPart1ResultProtocol { - -} +public protocol DKGPart1ResultProtocol {} public class DkgPart1Result: DKGPart1ResultProtocol { fileprivate let pointer: UnsafeMutableRawPointer @@ -379,7 +379,6 @@ public class DkgPart1Result: DKGPart1ResultProtocol { deinit { try! rustCall { uniffi_frost_uniffi_sdk_fn_free_dkgpart1result(pointer, $0) } } - } public struct FfiConverterTypeDKGPart1Result: FfiConverter { @@ -420,9 +419,7 @@ public func FfiConverterTypeDKGPart1Result_lower(_ value: DkgPart1Result) -> Uns return FfiConverterTypeDKGPart1Result.lower(value) } -public protocol DKGPart2ResultProtocol { - -} +public protocol DKGPart2ResultProtocol {} public class DkgPart2Result: DKGPart2ResultProtocol { fileprivate let pointer: UnsafeMutableRawPointer @@ -437,7 +434,6 @@ public class DkgPart2Result: DKGPart2ResultProtocol { deinit { try! rustCall { uniffi_frost_uniffi_sdk_fn_free_dkgpart2result(pointer, $0) } } - } public struct FfiConverterTypeDKGPart2Result: FfiConverter { @@ -478,9 +474,7 @@ public func FfiConverterTypeDKGPart2Result_lower(_ value: DkgPart2Result) -> Uns return FfiConverterTypeDKGPart2Result.lower(value) } -public protocol DKGRound1SecretPackageProtocol { - -} +public protocol DKGRound1SecretPackageProtocol {} public class DkgRound1SecretPackage: DKGRound1SecretPackageProtocol { fileprivate let pointer: UnsafeMutableRawPointer @@ -495,7 +489,6 @@ public class DkgRound1SecretPackage: DKGRound1SecretPackageProtocol { deinit { try! rustCall { uniffi_frost_uniffi_sdk_fn_free_dkground1secretpackage(pointer, $0) } } - } public struct FfiConverterTypeDKGRound1SecretPackage: FfiConverter { @@ -536,9 +529,7 @@ public func FfiConverterTypeDKGRound1SecretPackage_lower(_ value: DkgRound1Secre return FfiConverterTypeDKGRound1SecretPackage.lower(value) } -public protocol DKGRound2SecretPackageProtocol { - -} +public protocol DKGRound2SecretPackageProtocol {} public class DkgRound2SecretPackage: DKGRound2SecretPackageProtocol { fileprivate let pointer: UnsafeMutableRawPointer @@ -553,7 +544,6 @@ public class DkgRound2SecretPackage: DKGRound2SecretPackageProtocol { deinit { try! rustCall { uniffi_frost_uniffi_sdk_fn_free_dkground2secretpackage(pointer, $0) } } - } public struct FfiConverterTypeDKGRound2SecretPackage: FfiConverter { @@ -594,9 +584,7 @@ public func FfiConverterTypeDKGRound2SecretPackage_lower(_ value: DkgRound2Secre return FfiConverterTypeDKGRound2SecretPackage.lower(value) } -public protocol FrostRandomizedParamsProtocol { - -} +public protocol FrostRandomizedParamsProtocol {} public class FrostRandomizedParams: FrostRandomizedParamsProtocol { fileprivate let pointer: UnsafeMutableRawPointer @@ -611,7 +599,6 @@ public class FrostRandomizedParams: FrostRandomizedParamsProtocol { deinit { try! rustCall { uniffi_frost_uniffi_sdk_fn_free_frostrandomizedparams(pointer, $0) } } - } public struct FfiConverterTypeFrostRandomizedParams: FfiConverter { @@ -667,7 +654,7 @@ public struct Configuration { } extension Configuration: Equatable, Hashable { - public static func ==(lhs: Configuration, rhs: Configuration) -> Bool { + public static func == (lhs: Configuration, rhs: Configuration) -> Bool { if lhs.minSigners != rhs.minSigners { return false } @@ -724,7 +711,7 @@ public struct DkgPart3Result { } extension DkgPart3Result: Equatable, Hashable { - public static func ==(lhs: DkgPart3Result, rhs: DkgPart3Result) -> Bool { + public static func == (lhs: DkgPart3Result, rhs: DkgPart3Result) -> Bool { if lhs.publicKeyPackage != rhs.publicKeyPackage { return false } @@ -775,7 +762,7 @@ public struct DkgRound1Package { } extension DkgRound1Package: Equatable, Hashable { - public static func ==(lhs: DkgRound1Package, rhs: DkgRound1Package) -> Bool { + public static func == (lhs: DkgRound1Package, rhs: DkgRound1Package) -> Bool { if lhs.identifier != rhs.identifier { return false } @@ -826,7 +813,7 @@ public struct DkgRound2Package { } extension DkgRound2Package: Equatable, Hashable { - public static func ==(lhs: DkgRound2Package, rhs: DkgRound2Package) -> Bool { + public static func == (lhs: DkgRound2Package, rhs: DkgRound2Package) -> Bool { if lhs.identifier != rhs.identifier { return false } @@ -877,7 +864,7 @@ public struct FirstRoundCommitment { } extension FirstRoundCommitment: Equatable, Hashable { - public static func ==(lhs: FirstRoundCommitment, rhs: FirstRoundCommitment) -> Bool { + public static func == (lhs: FirstRoundCommitment, rhs: FirstRoundCommitment) -> Bool { if lhs.nonces != rhs.nonces { return false } @@ -928,7 +915,7 @@ public struct FrostKeyPackage { } extension FrostKeyPackage: Equatable, Hashable { - public static func ==(lhs: FrostKeyPackage, rhs: FrostKeyPackage) -> Bool { + public static func == (lhs: FrostKeyPackage, rhs: FrostKeyPackage) -> Bool { if lhs.identifier != rhs.identifier { return false } @@ -979,7 +966,7 @@ public struct FrostPublicKeyPackage { } extension FrostPublicKeyPackage: Equatable, Hashable { - public static func ==(lhs: FrostPublicKeyPackage, rhs: FrostPublicKeyPackage) -> Bool { + public static func == (lhs: FrostPublicKeyPackage, rhs: FrostPublicKeyPackage) -> Bool { if lhs.verifyingShares != rhs.verifyingShares { return false } @@ -1028,7 +1015,7 @@ public struct FrostRandomizer { } extension FrostRandomizer: Equatable, Hashable { - public static func ==(lhs: FrostRandomizer, rhs: FrostRandomizer) -> Bool { + public static func == (lhs: FrostRandomizer, rhs: FrostRandomizer) -> Bool { if lhs.data != rhs.data { return false } @@ -1073,7 +1060,7 @@ public struct FrostSecretKeyShare { } extension FrostSecretKeyShare: Equatable, Hashable { - public static func ==(lhs: FrostSecretKeyShare, rhs: FrostSecretKeyShare) -> Bool { + public static func == (lhs: FrostSecretKeyShare, rhs: FrostSecretKeyShare) -> Bool { if lhs.identifier != rhs.identifier { return false } @@ -1122,7 +1109,7 @@ public struct FrostSignature { } extension FrostSignature: Equatable, Hashable { - public static func ==(lhs: FrostSignature, rhs: FrostSignature) -> Bool { + public static func == (lhs: FrostSignature, rhs: FrostSignature) -> Bool { if lhs.data != rhs.data { return false } @@ -1167,7 +1154,7 @@ public struct FrostSignatureShare { } extension FrostSignatureShare: Equatable, Hashable { - public static func ==(lhs: FrostSignatureShare, rhs: FrostSignatureShare) -> Bool { + public static func == (lhs: FrostSignatureShare, rhs: FrostSignatureShare) -> Bool { if lhs.identifier != rhs.identifier { return false } @@ -1218,7 +1205,7 @@ public struct FrostSigningCommitments { } extension FrostSigningCommitments: Equatable, Hashable { - public static func ==(lhs: FrostSigningCommitments, rhs: FrostSigningCommitments) -> Bool { + public static func == (lhs: FrostSigningCommitments, rhs: FrostSigningCommitments) -> Bool { if lhs.identifier != rhs.identifier { return false } @@ -1267,7 +1254,7 @@ public struct FrostSigningNonces { } extension FrostSigningNonces: Equatable, Hashable { - public static func ==(lhs: FrostSigningNonces, rhs: FrostSigningNonces) -> Bool { + public static func == (lhs: FrostSigningNonces, rhs: FrostSigningNonces) -> Bool { if lhs.data != rhs.data { return false } @@ -1310,7 +1297,7 @@ public struct FrostSigningPackage { } extension FrostSigningPackage: Equatable, Hashable { - public static func ==(lhs: FrostSigningPackage, rhs: FrostSigningPackage) -> Bool { + public static func == (lhs: FrostSigningPackage, rhs: FrostSigningPackage) -> Bool { if lhs.data != rhs.data { return false } @@ -1353,7 +1340,7 @@ public struct Message { } extension Message: Equatable, Hashable { - public static func ==(lhs: Message, rhs: Message) -> Bool { + public static func == (lhs: Message, rhs: Message) -> Bool { if lhs.data != rhs.data { return false } @@ -1396,7 +1383,7 @@ public struct ParticipantIdentifier { } extension ParticipantIdentifier: Equatable, Hashable { - public static func ==(lhs: ParticipantIdentifier, rhs: ParticipantIdentifier) -> Bool { + public static func == (lhs: ParticipantIdentifier, rhs: ParticipantIdentifier) -> Bool { if lhs.data != rhs.data { return false } @@ -1439,7 +1426,7 @@ public struct ParticipantList { } extension ParticipantList: Equatable, Hashable { - public static func ==(lhs: ParticipantList, rhs: ParticipantList) -> Bool { + public static func == (lhs: ParticipantList, rhs: ParticipantList) -> Bool { if lhs.identifiers != rhs.identifiers { return false } @@ -1484,7 +1471,7 @@ public struct TrustedKeyGeneration { } extension TrustedKeyGeneration: Equatable, Hashable { - public static func ==(lhs: TrustedKeyGeneration, rhs: TrustedKeyGeneration) -> Bool { + public static func == (lhs: TrustedKeyGeneration, rhs: TrustedKeyGeneration) -> Bool { if lhs.secretShares != rhs.secretShares { return false } @@ -1523,7 +1510,6 @@ public func FfiConverterTypeTrustedKeyGeneration_lower(_ value: TrustedKeyGenera } public enum ConfigurationError { - case InvalidMaxSigners case InvalidMinSigners case InvalidIdentifier @@ -1540,19 +1526,16 @@ public struct FfiConverterTypeConfigurationError: FfiConverterRustBuffer { public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> ConfigurationError { let variant: Int32 = try readInt(&buf) switch variant { - case 1: return .InvalidMaxSigners case 2: return .InvalidMinSigners case 3: return .InvalidIdentifier case 4: return .UnknownError - - default: throw UniffiInternalError.unexpectedEnumCase + default: throw UniffiInternalError.unexpectedEnumCase } } public static func write(_ value: ConfigurationError, into buf: inout [UInt8]) { switch value { - case .InvalidMaxSigners: writeInt(&buf, Int32(1)) @@ -1564,17 +1547,15 @@ public struct FfiConverterTypeConfigurationError: FfiConverterRustBuffer { case .UnknownError: writeInt(&buf, Int32(4)) - } } } extension ConfigurationError: Equatable, Hashable {} -extension ConfigurationError: Error { } +extension ConfigurationError: Error {} public enum CoordinationError { - case FailedToCreateSigningPackage case InvalidSigningCommitment case IdentifierDeserializationError @@ -1595,25 +1576,22 @@ public struct FfiConverterTypeCoordinationError: FfiConverterRustBuffer { public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> CoordinationError { let variant: Int32 = try readInt(&buf) switch variant { - case 1: return .FailedToCreateSigningPackage case 2: return .InvalidSigningCommitment case 3: return .IdentifierDeserializationError case 4: return .SigningPackageSerializationError case 5: return .SignatureShareDeserializationError case 6: return .PublicKeyPackageDeserializationError - case 7: return .SignatureShareAggregationFailed( - message: try FfiConverterString.read(from: &buf) + case 7: return try .SignatureShareAggregationFailed( + message: FfiConverterString.read(from: &buf) ) case 8: return .InvalidRandomizer - - default: throw UniffiInternalError.unexpectedEnumCase + default: throw UniffiInternalError.unexpectedEnumCase } } public static func write(_ value: CoordinationError, into buf: inout [UInt8]) { switch value { - case .FailedToCreateSigningPackage: writeInt(&buf, Int32(1)) @@ -1638,17 +1616,15 @@ public struct FfiConverterTypeCoordinationError: FfiConverterRustBuffer { case .InvalidRandomizer: writeInt(&buf, Int32(8)) - } } } extension CoordinationError: Equatable, Hashable {} -extension CoordinationError: Error { } +extension CoordinationError: Error {} public enum FrostError { - case InvalidMinSigners case InvalidMaxSigners case InvalidCoefficients @@ -1700,7 +1676,6 @@ public struct FfiConverterTypeFrostError: FfiConverterRustBuffer { public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> FrostError { let variant: Int32 = try readInt(&buf) switch variant { - case 1: return .InvalidMinSigners case 2: return .InvalidMaxSigners case 3: return .InvalidCoefficients @@ -1718,22 +1693,22 @@ public struct FfiConverterTypeFrostError: FfiConverterRustBuffer { case 15: return .MissingCommitment case 16: return .IncorrectCommitment case 17: return .IncorrectNumberOfCommitments - case 18: return .InvalidSignatureShare( - culprit: try FfiConverterTypeParticipantIdentifier.read(from: &buf) + case 18: return try .InvalidSignatureShare( + culprit: FfiConverterTypeParticipantIdentifier.read(from: &buf) ) case 19: return .InvalidSecretShare case 20: return .PackageNotFound case 21: return .IncorrectNumberOfPackages case 22: return .IncorrectPackage case 23: return .DkgNotSupported - case 24: return .InvalidProofOfKnowledge( - culprit: try FfiConverterTypeParticipantIdentifier.read(from: &buf) + case 24: return try .InvalidProofOfKnowledge( + culprit: FfiConverterTypeParticipantIdentifier.read(from: &buf) ) - case 25: return .FieldError( - message: try FfiConverterString.read(from: &buf) + case 25: return try .FieldError( + message: FfiConverterString.read(from: &buf) ) - case 26: return .GroupError( - message: try FfiConverterString.read(from: &buf) + case 26: return try .GroupError( + message: FfiConverterString.read(from: &buf) ) case 27: return .InvalidCoefficient case 28: return .IdentifierDerivationNotSupported @@ -1748,14 +1723,12 @@ public struct FfiConverterTypeFrostError: FfiConverterRustBuffer { case 37: return .InvalidSecretKey case 38: return .InvalidConfiguration case 39: return .UnexpectedError - - default: throw UniffiInternalError.unexpectedEnumCase + default: throw UniffiInternalError.unexpectedEnumCase } } public static func write(_ value: FrostError, into buf: inout [UInt8]) { switch value { - case .InvalidMinSigners: writeInt(&buf, Int32(1)) @@ -1876,17 +1849,15 @@ public struct FfiConverterTypeFrostError: FfiConverterRustBuffer { case .UnexpectedError: writeInt(&buf, Int32(39)) - } } } extension FrostError: Equatable, Hashable {} -extension FrostError: Error { } +extension FrostError: Error {} public enum FrostSignatureVerificationError { - case InvalidPublicKeyPackage case ValidationFailed(reason: String) @@ -1901,36 +1872,33 @@ public struct FfiConverterTypeFrostSignatureVerificationError: FfiConverterRustB public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> FrostSignatureVerificationError { let variant: Int32 = try readInt(&buf) switch variant { - case 1: return .InvalidPublicKeyPackage - case 2: return .ValidationFailed( - reason: try FfiConverterString.read(from: &buf) + + case 2: return try .ValidationFailed( + reason: FfiConverterString.read(from: &buf) ) - default: throw UniffiInternalError.unexpectedEnumCase + default: throw UniffiInternalError.unexpectedEnumCase } } public static func write(_ value: FrostSignatureVerificationError, into buf: inout [UInt8]) { switch value { - case .InvalidPublicKeyPackage: writeInt(&buf, Int32(1)) case let .ValidationFailed(reason): writeInt(&buf, Int32(2)) FfiConverterString.write(reason, into: &buf) - } } } extension FrostSignatureVerificationError: Equatable, Hashable {} -extension FrostSignatureVerificationError: Error { } +extension FrostSignatureVerificationError: Error {} public enum Round1Error { - case InvalidKeyPackage case NonceSerializationError case CommitmentSerializationError @@ -1946,18 +1914,15 @@ public struct FfiConverterTypeRound1Error: FfiConverterRustBuffer { public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> Round1Error { let variant: Int32 = try readInt(&buf) switch variant { - case 1: return .InvalidKeyPackage case 2: return .NonceSerializationError case 3: return .CommitmentSerializationError - - default: throw UniffiInternalError.unexpectedEnumCase + default: throw UniffiInternalError.unexpectedEnumCase } } public static func write(_ value: Round1Error, into buf: inout [UInt8]) { switch value { - case .InvalidKeyPackage: writeInt(&buf, Int32(1)) @@ -1966,17 +1931,15 @@ public struct FfiConverterTypeRound1Error: FfiConverterRustBuffer { case .CommitmentSerializationError: writeInt(&buf, Int32(3)) - } } } extension Round1Error: Equatable, Hashable {} -extension Round1Error: Error { } +extension Round1Error: Error {} public enum Round2Error { - case InvalidKeyPackage case NonceSerializationError case CommitmentSerializationError @@ -1995,23 +1958,20 @@ public struct FfiConverterTypeRound2Error: FfiConverterRustBuffer { public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> Round2Error { let variant: Int32 = try readInt(&buf) switch variant { - case 1: return .InvalidKeyPackage case 2: return .NonceSerializationError case 3: return .CommitmentSerializationError case 4: return .SigningPackageDeserializationError - case 5: return .SigningFailed( - message: try FfiConverterString.read(from: &buf) + case 5: return try .SigningFailed( + message: FfiConverterString.read(from: &buf) ) case 6: return .InvalidRandomizer - - default: throw UniffiInternalError.unexpectedEnumCase + default: throw UniffiInternalError.unexpectedEnumCase } } public static func write(_ value: Round2Error, into buf: inout [UInt8]) { switch value { - case .InvalidKeyPackage: writeInt(&buf, Int32(1)) @@ -2030,14 +1990,13 @@ public struct FfiConverterTypeRound2Error: FfiConverterRustBuffer { case .InvalidRandomizer: writeInt(&buf, Int32(6)) - } } } extension Round2Error: Equatable, Hashable {} -extension Round2Error: Error { } +extension Round2Error: Error {} private struct FfiConverterOptionTypeParticipantIdentifier: FfiConverterRustBuffer { typealias SwiftType = ParticipantIdentifier? @@ -2076,7 +2035,7 @@ private struct FfiConverterSequenceTypeFrostSignatureShare: FfiConverterRustBuff var seq = [FrostSignatureShare]() seq.reserveCapacity(Int(len)) for _ in 0 ..< len { - seq.append(try FfiConverterTypeFrostSignatureShare.read(from: &buf)) + try seq.append(FfiConverterTypeFrostSignatureShare.read(from: &buf)) } return seq } @@ -2098,7 +2057,7 @@ private struct FfiConverterSequenceTypeFrostSigningCommitments: FfiConverterRust var seq = [FrostSigningCommitments]() seq.reserveCapacity(Int(len)) for _ in 0 ..< len { - seq.append(try FfiConverterTypeFrostSigningCommitments.read(from: &buf)) + try seq.append(FfiConverterTypeFrostSigningCommitments.read(from: &buf)) } return seq } @@ -2120,7 +2079,7 @@ private struct FfiConverterSequenceTypeParticipantIdentifier: FfiConverterRustBu var seq = [ParticipantIdentifier]() seq.reserveCapacity(Int(len)) for _ in 0 ..< len { - seq.append(try FfiConverterTypeParticipantIdentifier.read(from: &buf)) + try seq.append(FfiConverterTypeParticipantIdentifier.read(from: &buf)) } return seq } @@ -2140,7 +2099,7 @@ private struct FfiConverterDictionaryTypeParticipantIdentifierString: FfiConvert let len: Int32 = try readInt(&buf) var dict = [ParticipantIdentifier: String]() dict.reserveCapacity(Int(len)) - for _ in 0.. FrostSignature { - return try FfiConverterTypeFrostSignature.lift( - try rustCallWithError(FfiConverterTypeCoordinationError.lift) { - uniffi_frost_uniffi_sdk_fn_func_aggregate( - FfiConverterTypeFrostSigningPackage.lower(signingPackage), - FfiConverterSequenceTypeFrostSignatureShare.lower(signatureShares), - FfiConverterTypeFrostPublicKeyPackage.lower(pubkeyPackage), - FfiConverterTypeFrostRandomizer.lower(randomizer), $0) -} + return try FfiConverterTypeFrostSignature.lift( + rustCallWithError(FfiConverterTypeCoordinationError.lift) { + uniffi_frost_uniffi_sdk_fn_func_aggregate( + FfiConverterTypeFrostSigningPackage.lower(signingPackage), + FfiConverterSequenceTypeFrostSignatureShare.lower(signatureShares), + FfiConverterTypeFrostPublicKeyPackage.lower(pubkeyPackage), + FfiConverterTypeFrostRandomizer.lower(randomizer), $0 + ) + } ) } public func fromHexString(hexString: String) throws -> FrostRandomizer { - return try FfiConverterTypeFrostRandomizer.lift( - try rustCallWithError(FfiConverterTypeFrostError.lift) { - uniffi_frost_uniffi_sdk_fn_func_from_hex_string( - FfiConverterString.lower(hexString), $0) -} + return try FfiConverterTypeFrostRandomizer.lift( + rustCallWithError(FfiConverterTypeFrostError.lift) { + uniffi_frost_uniffi_sdk_fn_func_from_hex_string( + FfiConverterString.lower(hexString), $0 + ) + } ) } public func generateNoncesAndCommitments(keyPackage: FrostKeyPackage) throws -> FirstRoundCommitment { - return try FfiConverterTypeFirstRoundCommitment.lift( - try rustCallWithError(FfiConverterTypeRound1Error.lift) { - uniffi_frost_uniffi_sdk_fn_func_generate_nonces_and_commitments( - FfiConverterTypeFrostKeyPackage.lower(keyPackage), $0) -} + return try FfiConverterTypeFirstRoundCommitment.lift( + rustCallWithError(FfiConverterTypeRound1Error.lift) { + uniffi_frost_uniffi_sdk_fn_func_generate_nonces_and_commitments( + FfiConverterTypeFrostKeyPackage.lower(keyPackage), $0 + ) + } ) } public func identifierFromJsonString(string: String) -> ParticipantIdentifier? { - return try! FfiConverterOptionTypeParticipantIdentifier.lift( + return try! FfiConverterOptionTypeParticipantIdentifier.lift( try! rustCall { - uniffi_frost_uniffi_sdk_fn_func_identifier_from_json_string( - FfiConverterString.lower(string), $0) -} + uniffi_frost_uniffi_sdk_fn_func_identifier_from_json_string( + FfiConverterString.lower(string), $0 + ) + } ) } public func identifierFromString(string: String) throws -> ParticipantIdentifier { - return try FfiConverterTypeParticipantIdentifier.lift( - try rustCallWithError(FfiConverterTypeFrostError.lift) { - uniffi_frost_uniffi_sdk_fn_func_identifier_from_string( - FfiConverterString.lower(string), $0) -} + return try FfiConverterTypeParticipantIdentifier.lift( + rustCallWithError(FfiConverterTypeFrostError.lift) { + uniffi_frost_uniffi_sdk_fn_func_identifier_from_string( + FfiConverterString.lower(string), $0 + ) + } ) } public func identifierFromUint16(unsignedUint: UInt16) throws -> ParticipantIdentifier { - return try FfiConverterTypeParticipantIdentifier.lift( - try rustCallWithError(FfiConverterTypeFrostError.lift) { - uniffi_frost_uniffi_sdk_fn_func_identifier_from_uint16( - FfiConverterUInt16.lower(unsignedUint), $0) -} + return try FfiConverterTypeParticipantIdentifier.lift( + rustCallWithError(FfiConverterTypeFrostError.lift) { + uniffi_frost_uniffi_sdk_fn_func_identifier_from_uint16( + FfiConverterUInt16.lower(unsignedUint), $0 + ) + } ) } public func newSigningPackage(message: Message, commitments: [FrostSigningCommitments]) throws -> FrostSigningPackage { - return try FfiConverterTypeFrostSigningPackage.lift( - try rustCallWithError(FfiConverterTypeCoordinationError.lift) { - uniffi_frost_uniffi_sdk_fn_func_new_signing_package( - FfiConverterTypeMessage.lower(message), - FfiConverterSequenceTypeFrostSigningCommitments.lower(commitments), $0) -} + return try FfiConverterTypeFrostSigningPackage.lift( + rustCallWithError(FfiConverterTypeCoordinationError.lift) { + uniffi_frost_uniffi_sdk_fn_func_new_signing_package( + FfiConverterTypeMessage.lower(message), + FfiConverterSequenceTypeFrostSigningCommitments.lower(commitments), $0 + ) + } ) } public func part1(participantIdentifier: ParticipantIdentifier, maxSigners: UInt16, minSigners: UInt16) throws -> DkgPart1Result { - return try FfiConverterTypeDKGPart1Result.lift( - try rustCallWithError(FfiConverterTypeFrostError.lift) { - uniffi_frost_uniffi_sdk_fn_func_part_1( - FfiConverterTypeParticipantIdentifier.lower(participantIdentifier), - FfiConverterUInt16.lower(maxSigners), - FfiConverterUInt16.lower(minSigners), $0) -} + return try FfiConverterTypeDKGPart1Result.lift( + rustCallWithError(FfiConverterTypeFrostError.lift) { + uniffi_frost_uniffi_sdk_fn_func_part_1( + FfiConverterTypeParticipantIdentifier.lower(participantIdentifier), + FfiConverterUInt16.lower(maxSigners), + FfiConverterUInt16.lower(minSigners), $0 + ) + } ) } public func part2(secretPackage: DkgRound1SecretPackage, round1Packages: [ParticipantIdentifier: DkgRound1Package]) throws -> DkgPart2Result { - return try FfiConverterTypeDKGPart2Result.lift( - try rustCallWithError(FfiConverterTypeFrostError.lift) { - uniffi_frost_uniffi_sdk_fn_func_part_2( - FfiConverterTypeDKGRound1SecretPackage.lower(secretPackage), - FfiConverterDictionaryTypeParticipantIdentifierTypeDKGRound1Package.lower(round1Packages), $0) -} + return try FfiConverterTypeDKGPart2Result.lift( + rustCallWithError(FfiConverterTypeFrostError.lift) { + uniffi_frost_uniffi_sdk_fn_func_part_2( + FfiConverterTypeDKGRound1SecretPackage.lower(secretPackage), + FfiConverterDictionaryTypeParticipantIdentifierTypeDKGRound1Package.lower(round1Packages), $0 + ) + } ) } public func part3(secretPackage: DkgRound2SecretPackage, round1Packages: [ParticipantIdentifier: DkgRound1Package], round2Packages: [ParticipantIdentifier: DkgRound2Package]) throws -> DkgPart3Result { - return try FfiConverterTypeDKGPart3Result.lift( - try rustCallWithError(FfiConverterTypeFrostError.lift) { - uniffi_frost_uniffi_sdk_fn_func_part_3( - FfiConverterTypeDKGRound2SecretPackage.lower(secretPackage), - FfiConverterDictionaryTypeParticipantIdentifierTypeDKGRound1Package.lower(round1Packages), - FfiConverterDictionaryTypeParticipantIdentifierTypeDKGRound2Package.lower(round2Packages), $0) -} + return try FfiConverterTypeDKGPart3Result.lift( + rustCallWithError(FfiConverterTypeFrostError.lift) { + uniffi_frost_uniffi_sdk_fn_func_part_3( + FfiConverterTypeDKGRound2SecretPackage.lower(secretPackage), + FfiConverterDictionaryTypeParticipantIdentifierTypeDKGRound1Package.lower(round1Packages), + FfiConverterDictionaryTypeParticipantIdentifierTypeDKGRound2Package.lower(round2Packages), $0 + ) + } ) } public func randomizedParamsFromPublicKeyAndSigningPackage(publicKey: FrostPublicKeyPackage, signingPackage: FrostSigningPackage) throws -> FrostRandomizedParams { - return try FfiConverterTypeFrostRandomizedParams.lift( - try rustCallWithError(FfiConverterTypeFrostError.lift) { - uniffi_frost_uniffi_sdk_fn_func_randomized_params_from_public_key_and_signing_package( - FfiConverterTypeFrostPublicKeyPackage.lower(publicKey), - FfiConverterTypeFrostSigningPackage.lower(signingPackage), $0) -} + return try FfiConverterTypeFrostRandomizedParams.lift( + rustCallWithError(FfiConverterTypeFrostError.lift) { + uniffi_frost_uniffi_sdk_fn_func_randomized_params_from_public_key_and_signing_package( + FfiConverterTypeFrostPublicKeyPackage.lower(publicKey), + FfiConverterTypeFrostSigningPackage.lower(signingPackage), $0 + ) + } ) } public func randomizerFromParams(randomizedParams: FrostRandomizedParams) throws -> FrostRandomizer { - return try FfiConverterTypeFrostRandomizer.lift( - try rustCallWithError(FfiConverterTypeFrostError.lift) { - uniffi_frost_uniffi_sdk_fn_func_randomizer_from_params( - FfiConverterTypeFrostRandomizedParams.lower(randomizedParams), $0) -} + return try FfiConverterTypeFrostRandomizer.lift( + rustCallWithError(FfiConverterTypeFrostError.lift) { + uniffi_frost_uniffi_sdk_fn_func_randomizer_from_params( + FfiConverterTypeFrostRandomizedParams.lower(randomizedParams), $0 + ) + } ) } public func sign(signingPackage: FrostSigningPackage, nonces: FrostSigningNonces, keyPackage: FrostKeyPackage, randomizer: FrostRandomizer) throws -> FrostSignatureShare { - return try FfiConverterTypeFrostSignatureShare.lift( - try rustCallWithError(FfiConverterTypeRound2Error.lift) { - uniffi_frost_uniffi_sdk_fn_func_sign( - FfiConverterTypeFrostSigningPackage.lower(signingPackage), - FfiConverterTypeFrostSigningNonces.lower(nonces), - FfiConverterTypeFrostKeyPackage.lower(keyPackage), - FfiConverterTypeFrostRandomizer.lower(randomizer), $0) -} + return try FfiConverterTypeFrostSignatureShare.lift( + rustCallWithError(FfiConverterTypeRound2Error.lift) { + uniffi_frost_uniffi_sdk_fn_func_sign( + FfiConverterTypeFrostSigningPackage.lower(signingPackage), + FfiConverterTypeFrostSigningNonces.lower(nonces), + FfiConverterTypeFrostKeyPackage.lower(keyPackage), + FfiConverterTypeFrostRandomizer.lower(randomizer), $0 + ) + } ) } public func trustedDealerKeygenFrom(configuration: Configuration) throws -> TrustedKeyGeneration { - return try FfiConverterTypeTrustedKeyGeneration.lift( - try rustCallWithError(FfiConverterTypeFrostError.lift) { - uniffi_frost_uniffi_sdk_fn_func_trusted_dealer_keygen_from( - FfiConverterTypeConfiguration.lower(configuration), $0) -} + return try FfiConverterTypeTrustedKeyGeneration.lift( + rustCallWithError(FfiConverterTypeFrostError.lift) { + uniffi_frost_uniffi_sdk_fn_func_trusted_dealer_keygen_from( + FfiConverterTypeConfiguration.lower(configuration), $0 + ) + } ) } public func trustedDealerKeygenWithIdentifiers(configuration: Configuration, participants: ParticipantList) throws -> TrustedKeyGeneration { - return try FfiConverterTypeTrustedKeyGeneration.lift( - try rustCallWithError(FfiConverterTypeFrostError.lift) { - uniffi_frost_uniffi_sdk_fn_func_trusted_dealer_keygen_with_identifiers( - FfiConverterTypeConfiguration.lower(configuration), - FfiConverterTypeParticipantList.lower(participants), $0) -} + return try FfiConverterTypeTrustedKeyGeneration.lift( + rustCallWithError(FfiConverterTypeFrostError.lift) { + uniffi_frost_uniffi_sdk_fn_func_trusted_dealer_keygen_with_identifiers( + FfiConverterTypeConfiguration.lower(configuration), + FfiConverterTypeParticipantList.lower(participants), $0 + ) + } ) } public func validateConfig(config: Configuration) throws { try rustCallWithError(FfiConverterTypeConfigurationError.lift) { - uniffi_frost_uniffi_sdk_fn_func_validate_config( - FfiConverterTypeConfiguration.lower(config), $0) -} + uniffi_frost_uniffi_sdk_fn_func_validate_config( + FfiConverterTypeConfiguration.lower(config), $0 + ) + } } public func verifyAndGetKeyPackageFrom(secretShare: FrostSecretKeyShare) throws -> FrostKeyPackage { - return try FfiConverterTypeFrostKeyPackage.lift( - try rustCallWithError(FfiConverterTypeFrostError.lift) { - uniffi_frost_uniffi_sdk_fn_func_verify_and_get_key_package_from( - FfiConverterTypeFrostSecretKeyShare.lower(secretShare), $0) -} + return try FfiConverterTypeFrostKeyPackage.lift( + rustCallWithError(FfiConverterTypeFrostError.lift) { + uniffi_frost_uniffi_sdk_fn_func_verify_and_get_key_package_from( + FfiConverterTypeFrostSecretKeyShare.lower(secretShare), $0 + ) + } ) } public func verifyRandomizedSignature(randomizer: FrostRandomizer, message: Message, signature: FrostSignature, pubkey: FrostPublicKeyPackage) throws { try rustCallWithError(FfiConverterTypeFrostSignatureVerificationError.lift) { - uniffi_frost_uniffi_sdk_fn_func_verify_randomized_signature( - FfiConverterTypeFrostRandomizer.lower(randomizer), - FfiConverterTypeMessage.lower(message), - FfiConverterTypeFrostSignature.lower(signature), - FfiConverterTypeFrostPublicKeyPackage.lower(pubkey), $0) -} + uniffi_frost_uniffi_sdk_fn_func_verify_randomized_signature( + FfiConverterTypeFrostRandomizer.lower(randomizer), + FfiConverterTypeMessage.lower(message), + FfiConverterTypeFrostSignature.lower(signature), + FfiConverterTypeFrostPublicKeyPackage.lower(pubkey), $0 + ) + } } public func verifySignature(message: Message, signature: FrostSignature, pubkey: FrostPublicKeyPackage) throws { try rustCallWithError(FfiConverterTypeFrostSignatureVerificationError.lift) { - uniffi_frost_uniffi_sdk_fn_func_verify_signature( - FfiConverterTypeMessage.lower(message), - FfiConverterTypeFrostSignature.lower(signature), - FfiConverterTypeFrostPublicKeyPackage.lower(pubkey), $0) -} + uniffi_frost_uniffi_sdk_fn_func_verify_signature( + FfiConverterTypeMessage.lower(message), + FfiConverterTypeFrostSignature.lower(signature), + FfiConverterTypeFrostPublicKeyPackage.lower(pubkey), $0 + ) + } } private enum InitializationResult { @@ -2407,6 +2385,7 @@ private enum InitializationResult { case contractVersionMismatch case apiChecksumMismatch } + // Use a global variables to perform the versioning checks. Swift ensures that // the code inside is only computed once. private var initializationResult: InitializationResult { diff --git a/FrostSwift/Tests/FrostSwift/ModelTests.swift b/FrostSwift/Tests/FrostSwift/ModelTests.swift index f5ed83f..2f3e0f1 100644 --- a/FrostSwift/Tests/FrostSwift/ModelTests.swift +++ b/FrostSwift/Tests/FrostSwift/ModelTests.swift @@ -1,12 +1,13 @@ // // ModelTests.swift -// +// // // Created by Pacu 28-06-2024 // -import XCTest import FrostSwift +import XCTest + final class ModelTests: XCTestCase { func testConfigurationThrows() { XCTAssertThrowsError(try Configuration(maxSigners: 3, minSigners: 4, secret: nil)) @@ -18,7 +19,6 @@ final class ModelTests: XCTestCase { func testIdentifierSerializationFromString() { XCTAssertNotNil(Identifier(identifier: "0100000000000000000000000000000000000000000000000000000000000000")) - } func testIdentifierSerializationFromScalar() throws { @@ -29,6 +29,5 @@ final class ModelTests: XCTestCase { let stringIdentifier = try Identifier(with: 1)?.toString() XCTAssertEqual(expectedId, stringIdentifier) - } } diff --git a/FrostSwift/Tests/FrostSwift/TrustedDealerSignatureIntegrationTest.swift b/FrostSwift/Tests/FrostSwift/TrustedDealerSignatureIntegrationTest.swift index bc57de4..118cfe2 100644 --- a/FrostSwift/Tests/FrostSwift/TrustedDealerSignatureIntegrationTest.swift +++ b/FrostSwift/Tests/FrostSwift/TrustedDealerSignatureIntegrationTest.swift @@ -1,11 +1,12 @@ // // TrustedDealerSignatureIntegrationTest.swift -// +// // // Created by Pacu in 2024. // -import XCTest import FrostSwift +import XCTest + final class TrustedDealerSignatureIntegrationTest: XCTestCase { func initializeWithTrustedDealer() throws -> ( Configuration, @@ -49,7 +50,6 @@ final class TrustedDealerSignatureIntegrationTest: XCTestCase { } func testSignatureFromTrustedDealerWithNonSigningCoordinator() async throws { - let values = try initializeWithTrustedDealer() let configuration = values.0 @@ -83,7 +83,6 @@ final class TrustedDealerSignatureIntegrationTest: XCTestCase { // for every participan for identifier in signingParticipants { - // get participant let participant = participants[identifier]! @@ -145,7 +144,6 @@ final class TrustedDealerSignatureIntegrationTest: XCTestCase { // for every participant for identifier in signingParticipants { - // get participant let participant = participants[identifier]! diff --git a/FrostSwift/Tests/FrostSwift/TrustedDealerTests.swift b/FrostSwift/Tests/FrostSwift/TrustedDealerTests.swift index cd0f37b..01e7594 100644 --- a/FrostSwift/Tests/FrostSwift/TrustedDealerTests.swift +++ b/FrostSwift/Tests/FrostSwift/TrustedDealerTests.swift @@ -1,6 +1,7 @@ +@testable import FrostSwift + // swift-format-ignore-file import XCTest -@testable import FrostSwift public class TrustedDealerTests: XCTestCase { func testFrostTrustedDealerGeneratesKeys() throws { @@ -12,7 +13,6 @@ public class TrustedDealerTests: XCTestCase { let keys = try dealer.generateKeys() XCTAssertEqual(keys.secretShares.count, 3) - } func testFrostTrustedDealerFailsWhenLessIdentifiersAreProvided() throws { @@ -22,7 +22,7 @@ public class TrustedDealerTests: XCTestCase { let identifiers: [Identifier] = [ Identifier(identifier: "0100000000000000000000000000000000000000000000000000000000000000")!, - Identifier(identifier: "0200000000000000000000000000000000000000000000000000000000000000")! + Identifier(identifier: "0200000000000000000000000000000000000000000000000000000000000000")!, ] // generate keys with default identifiers @@ -39,7 +39,7 @@ public class TrustedDealerTests: XCTestCase { Identifier(identifier: "0100000000000000000000000000000000000000000000000000000000000000")!, Identifier(identifier: "0200000000000000000000000000000000000000000000000000000000000000")!, Identifier(identifier: "0300000000000000000000000000000000000000000000000000000000000000")!, - Identifier(identifier: "0400000000000000000000000000000000000000000000000000000000000000")! + Identifier(identifier: "0400000000000000000000000000000000000000000000000000000000000000")!, ] // generate keys with default identifiers @@ -54,7 +54,7 @@ public class TrustedDealerTests: XCTestCase { let identifiers: [Identifier] = [ Identifier(identifier: "0100000000000000000000000000000000000000000000000000000000000000")!, Identifier(identifier: "0200000000000000000000000000000000000000000000000000000000000000")!, - Identifier(identifier: "0300000000000000000000000000000000000000000000000000000000000000")! + Identifier(identifier: "0300000000000000000000000000000000000000000000000000000000000000")!, ] // generate keys with default identifiers diff --git a/FrostSwift/Tests/FrostSwiftFFI/NotRedPallasTests/NotRedPallasTests.swift b/FrostSwift/Tests/FrostSwiftFFI/NotRedPallasTests/NotRedPallasTests.swift index ee4556f..39e9000 100644 --- a/FrostSwift/Tests/FrostSwiftFFI/NotRedPallasTests/NotRedPallasTests.swift +++ b/FrostSwift/Tests/FrostSwiftFFI/NotRedPallasTests/NotRedPallasTests.swift @@ -7,14 +7,14 @@ // import Foundation -import XCTest @testable import FrostSwiftFFI +import XCTest class NotRedPallasTests: XCTestCase { func ignore_testTrustedDealerFromConfigWithSecret() throws { let secret: [UInt8] = [ 123, 28, 51, 211, 245, 41, 29, 133, 222, 102, 72, 51, 190, 177, 173, 70, 159, 127, 182, 2, - 90, 14, 199, 139, 58, 121, 12, 110, 19, 169, 131, 4 + 90, 14, 199, 139, 58, 121, 12, 110, 19, 169, 131, 4, ] let secretConfig = Configuration(minSigners: 2, maxSigners: 3, secret: Data(secret)) @@ -27,7 +27,7 @@ class NotRedPallasTests: XCTestCase { let shares = keygen.secretShares // get key packages for participants - let keyPackages = try shares.map { (identifier, value) in + let keyPackages = try shares.map { identifier, value in let keyPackage = try verifyAndGetKeyPackageFrom(secretShare: value) return (identifier, keyPackage) @@ -42,7 +42,6 @@ class NotRedPallasTests: XCTestCase { var commitments = [FrostSigningCommitments]() for (participant, secretShare) in shares { - let keyPackage = try verifyAndGetKeyPackageFrom(secretShare: secretShare) let firstRoundCommitment = try generateNoncesAndCommitments(keyPackage: keyPackage) diff --git a/FrostSwift/Tests/FrostSwiftFFI/NotRedPallasTests/RedPallasTests.swift b/FrostSwift/Tests/FrostSwiftFFI/NotRedPallasTests/RedPallasTests.swift index d9441ad..545ae07 100644 --- a/FrostSwift/Tests/FrostSwiftFFI/NotRedPallasTests/RedPallasTests.swift +++ b/FrostSwift/Tests/FrostSwiftFFI/NotRedPallasTests/RedPallasTests.swift @@ -1,14 +1,14 @@ // swift-format-ignore-file // -// NotRedPallasTests.swift +// RedPallasTests.swift // // // Created by Pacu on 23-05-2024. // import Foundation -import XCTest @testable import FrostSwiftFFI +import XCTest class RedPallasTests: XCTestCase { func testTrustedDealerRandomizedSignatureFromConfig() throws { @@ -24,7 +24,7 @@ class RedPallasTests: XCTestCase { let shares = keygen.secretShares // get key packages for participants - let keyPackages = try shares.map { (identifier, value) in + let keyPackages = try shares.map { identifier, value in let keyPackage = try verifyAndGetKeyPackageFrom(secretShare: value) return (identifier, keyPackage)