Skip to content

Commit

Permalink
Bring several crypto things closer to parity
Browse files Browse the repository at this point in the history
  • Loading branch information
qtbeee committed Jan 7, 2020
1 parent 1852c16 commit 371a135
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Examples/CreateAccount/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)")

Expand Down
20 changes: 20 additions & 0 deletions Sources/Hedera/crypto/KeyList.swift
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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
}
}
8 changes: 4 additions & 4 deletions Sources/Hedera/crypto/ThresholdKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down Expand Up @@ -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
}
Expand Down
9 changes: 7 additions & 2 deletions Sources/Hedera/crypto/ed25519/Ed25519PrivateKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 371a135

Please sign in to comment.