diff --git a/Examples/CreateAccount/main.swift b/Examples/CreateAccount/main.swift index daa6c1b5..40d0df7e 100644 --- a/Examples/CreateAccount/main.swift +++ b/Examples/CreateAccount/main.swift @@ -22,7 +22,7 @@ defer { let client = clientFromEnvironment(eventLoopGroup: eventLoopGroup) .setMaxTransactionFee(100_000_000) -let newAccountKey = Ed25519PrivateKey() +let newAccountKey = Ed25519PrivateKey.generate()! print("private key for new account: \(newAccountKey)") diff --git a/Sources/Hedera/crypto/KeyList.swift b/Sources/Hedera/crypto/KeyList.swift index cb676b4a..c11806ea 100644 --- a/Sources/Hedera/crypto/KeyList.swift +++ b/Sources/Hedera/crypto/KeyList.swift @@ -1,6 +1,12 @@ public final class KeyList: PublicKey { var keys: [PublicKey] + override public init() { + keys = [] + + super.init() + } + required init?(_ proto: Proto_Key) { guard proto.keyList.keys.count > 0 else { return nil } keys = proto.keyList.keys.compactMap(PublicKey.fromProto) @@ -33,4 +39,18 @@ public final class KeyList: PublicKey { return proto } + + @discardableResult + public func add(_ key: PublicKey) -> Self { + keys.append(key) + + return self + } + + @discardableResult + public func addAll(_ keys: [PublicKey]) -> Self { + self.keys.append(contentsOf: keys) + + return self + } } diff --git a/Sources/Hedera/crypto/ThresholdKey.swift b/Sources/Hedera/crypto/ThresholdKey.swift index 21e499c0..fb23cd5b 100644 --- a/Sources/Hedera/crypto/ThresholdKey.swift +++ b/Sources/Hedera/crypto/ThresholdKey.swift @@ -2,9 +2,9 @@ public final class ThresholdKey: PublicKey { let threshold: UInt32 var keys: [PublicKey] - init(threshold: UInt32, keys: [PublicKey] = []) { + init(threshold: UInt32) { self.threshold = threshold - self.keys = keys + self.keys = [] super.init() } @@ -35,13 +35,13 @@ public final class ThresholdKey: PublicKey { } @discardableResult - public func addKey(_ key: PublicKey) -> Self { + public func add(_ key: PublicKey) -> Self { keys.append(key) return self } @discardableResult - public func addKeys(_ keys: [PublicKey]) -> Self { + public func addAll(_ keys: [PublicKey]) -> Self { self.keys.append(contentsOf: keys) return self } diff --git a/Sources/Hedera/crypto/ed25519/Ed25519PrivateKey.swift b/Sources/Hedera/crypto/ed25519/Ed25519PrivateKey.swift index 73de238a..fac18b5c 100644 --- a/Sources/Hedera/crypto/ed25519/Ed25519PrivateKey.swift +++ b/Sources/Hedera/crypto/ed25519/Ed25519PrivateKey.swift @@ -8,8 +8,13 @@ public struct Ed25519PrivateKey { private let inner: Sign.KeyPair /// Generate a new private key - public init() { - inner = sodium.sign.keyPair()! + public static func generate() -> Self? { + Ed25519PrivateKey() + } + + private init?() { + guard let keypair = sodium.sign.keyPair() else { return nil } + inner = keypair } /// Initialize a private key from bytes