From 414c184b148fc7039652ff2c8b9154602e174e80 Mon Sep 17 00:00:00 2001 From: Dariusz Rybicki <dariusz@elixxir.io> Date: Wed, 1 Jun 2022 11:48:52 +0200 Subject: [PATCH] Add PasswordGenerator --- Sources/ElixxirDAppsSDK/Errors.swift | 4 +++ .../ElixxirDAppsSDK/PasswordGenerator.swift | 27 +++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 Sources/ElixxirDAppsSDK/PasswordGenerator.swift diff --git a/Sources/ElixxirDAppsSDK/Errors.swift b/Sources/ElixxirDAppsSDK/Errors.swift index acb28692..ae4ae8ae 100644 --- a/Sources/ElixxirDAppsSDK/Errors.swift +++ b/Sources/ElixxirDAppsSDK/Errors.swift @@ -1,3 +1,7 @@ public struct BindingsDownloadAndVerifySignedNdfWithUrlUnknownError: Error, Equatable { public init() {} } + +public struct BindingsGenerateSecretUnknownError: Error, Equatable { + public init() {} +} diff --git a/Sources/ElixxirDAppsSDK/PasswordGenerator.swift b/Sources/ElixxirDAppsSDK/PasswordGenerator.swift new file mode 100644 index 00000000..251ea873 --- /dev/null +++ b/Sources/ElixxirDAppsSDK/PasswordGenerator.swift @@ -0,0 +1,27 @@ +import Bindings + +public struct PasswordGenerator { + public var run: () throws -> Data + + public func callAsFunction() throws -> Data { + try run() + } +} + +extension PasswordGenerator { + public static let live = PasswordGenerator { + guard let secret = BindingsGenerateSecret(32) else { + throw BindingsGenerateSecretUnknownError() + } + return secret + } +} + +#if DEBUG +extension PasswordGenerator { + public static let failing = PasswordGenerator { + struct NotImplemented: Error {} + throw NotImplemented() + } +} +#endif -- GitLab