Skip to content

Commit

Permalink
Refactor Ed25519PrivateKey to store the whole keypair instead of just…
Browse files Browse the repository at this point in the history
… the private key bytes
  • Loading branch information
qtbeee committed Sep 26, 2019
1 parent 0f386e1 commit 7564cfa
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
27 changes: 15 additions & 12 deletions Sources/Hedera/crypto/ed25519/Ed25519PrivateKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,39 @@ let ed25519PrivateKeyPrefix = "302e020100300506032b657004220420"
let ed25519PrivateKeyLength = 32
let combinedEd25519KeyLength = 64

// TODO: init using a pem file

public struct Ed25519PrivateKey {
private let inner: Bytes
private let inner: Sign.KeyPair

/// Generate a new private key
public init() {
inner = sodium.sign.keyPair()!
}

public init?(bytes: Bytes) {
if bytes.count == ed25519PrivateKeyLength {
inner = bytes
inner = sodium.sign.keyPair(seed: bytes)!
} else if bytes.count == combinedEd25519KeyLength {
inner = Bytes(bytes.prefix(ed25519PrivateKeyLength))
inner = sodium.sign.keyPair(seed: Bytes(bytes.prefix(ed25519PrivateKeyLength)))!
} else {
// Invalid key length
return nil
}
}

public var bytes: Bytes {
inner
}

public static func generate() -> Ed25519PrivateKey {
Ed25519PrivateKey(bytes: sodium.sign.keyPair()!.secretKey)!
inner.secretKey
}

public var publicKey: Ed25519PublicKey {
Ed25519PublicKey(bytes: sodium.sign.keyPair(seed: inner)!.publicKey)!
Ed25519PublicKey(bytes: inner.publicKey)!
}
}

extension Ed25519PrivateKey: CustomStringConvertible, CustomDebugStringConvertible {
public var description: String {
hexEncode(bytes: inner, prefixed: ed25519PrivateKeyPrefix)
hexEncode(bytes: inner.secretKey, prefixed: ed25519PrivateKeyPrefix)
}

public var debugDescription: String {
Expand All @@ -47,14 +50,14 @@ extension Ed25519PrivateKey: LosslessStringConvertible {
switch description.count {
case ed25519PrivateKeyLength * 2, combinedEd25519KeyLength * 2: // lone key, or combined key
guard let decoded = try? hexDecode(description) else { return nil }
inner = decoded
self = Ed25519PrivateKey(bytes: decoded)!

case ed25519PrivateKeyLength * 2 + ed25519PrivateKeyPrefix.count: // DER encoded key
guard description.hasPrefix(ed25519PrivateKeyPrefix) else { return nil }

let range = description.index(description.startIndex, offsetBy: ed25519PrivateKeyPrefix.count)...
guard let decoded = try? hexDecode(description[range]) else { return nil }
inner = decoded
self = Ed25519PrivateKey(bytes: decoded)!

default:
return nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ let combinedKeyBytes = Array<UInt8>(arrayLiteral: 219, 72, 75, 130, 142, 100, 17

final class Ed25519PrivateKeyTests: XCTestCase {
func testGenerate() {
XCTAssertNoThrow(Ed25519PrivateKey.generate())
XCTAssertNoThrow(Ed25519PrivateKey())
}

func testFromBytes() {
Expand Down

0 comments on commit 7564cfa

Please sign in to comment.