|
| 1 | +// |
| 2 | +// Asymmetric.swift |
| 3 | +// GigaBitcoin/secp256k1.swift |
| 4 | +// |
| 5 | +// Copyright (c) 2022 GigaBitcoin LLC |
| 6 | +// Distributed under the MIT software license |
| 7 | +// |
| 8 | +// See the accompanying file LICENSE for information |
| 9 | +// |
| 10 | + |
| 11 | +import Foundation |
| 12 | + |
| 13 | +/// The secp256k1 Elliptic Curve. |
| 14 | +public extension secp256k1 { |
| 15 | + /// Signing operations on secp256k1 |
| 16 | + enum Signing { |
| 17 | + /// A Private Key for signing. |
| 18 | + public struct PrivateKey: Equatable { |
| 19 | + /// Generated secp256k1 Signing Key. |
| 20 | + private let baseKey: PrivateKeyImplementation |
| 21 | + |
| 22 | + /// The secp256k1 private key object |
| 23 | + var key: SecureBytes { |
| 24 | + baseKey.key |
| 25 | + } |
| 26 | + |
| 27 | + /// ECDSA Signing object. |
| 28 | + public var ecdsa: secp256k1.Signing.ECDSASigner { |
| 29 | + ECDSASigner(signingKey: baseKey) |
| 30 | + } |
| 31 | + |
| 32 | + /// Schnorr Signing object. |
| 33 | + public var schnorr: secp256k1.Signing.SchnorrSigner { |
| 34 | + SchnorrSigner(signingKey: baseKey) |
| 35 | + } |
| 36 | + |
| 37 | + /// The associated public key for verifying signatures done with this private key. |
| 38 | + /// |
| 39 | + /// - Returns: The associated public key |
| 40 | + public var publicKey: PublicKey { |
| 41 | + PublicKey(baseKey: baseKey.publicKey) |
| 42 | + } |
| 43 | + |
| 44 | + /// A data representation of the private key |
| 45 | + public var rawRepresentation: Data { |
| 46 | + baseKey.rawRepresentation |
| 47 | + } |
| 48 | + |
| 49 | + /// Creates a random secp256k1 private key for signing |
| 50 | + public init(format: secp256k1.Format = .compressed) throws { |
| 51 | + self.baseKey = try PrivateKeyImplementation(format: format) |
| 52 | + } |
| 53 | + |
| 54 | + /// Creates a secp256k1 private key for signing from a data representation. |
| 55 | + /// - Parameter data: A raw representation of the key. |
| 56 | + /// - Throws: An error is thrown when the raw representation does not create a private key for signing. |
| 57 | + public init<D: ContiguousBytes>(rawRepresentation data: D, format: secp256k1.Format = .compressed) throws { |
| 58 | + self.baseKey = try PrivateKeyImplementation(rawRepresentation: data, format: format) |
| 59 | + } |
| 60 | + |
| 61 | + public static func == (lhs: Self, rhs: Self) -> Bool { |
| 62 | + lhs.key == rhs.key |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + /// The corresponding public key. |
| 67 | + public struct PublicKey { |
| 68 | + /// Generated secp256k1 public key. |
| 69 | + private let baseKey: PublicKeyImplementation |
| 70 | + |
| 71 | + /// The secp256k1 public key object |
| 72 | + var keyBytes: [UInt8] { |
| 73 | + baseKey.bytes |
| 74 | + } |
| 75 | + |
| 76 | + /// A data representation of the public key |
| 77 | + public var rawRepresentation: Data { |
| 78 | + baseKey.rawRepresentation |
| 79 | + } |
| 80 | + |
| 81 | + /// ECDSA Validating object. |
| 82 | + public var ecdsa: secp256k1.Signing.ECDSAValidator { |
| 83 | + ECDSAValidator(validatingKey: baseKey) |
| 84 | + } |
| 85 | + |
| 86 | + /// Schnorr Validating object. |
| 87 | + public var schnorr: secp256k1.Signing.SchnorrValidator { |
| 88 | + SchnorrValidator(validatingKey: baseKey) |
| 89 | + } |
| 90 | + |
| 91 | + /// The associated x-only public key for verifying Schnorr signatures. |
| 92 | + /// |
| 93 | + /// - Returns: The associated x-only public key |
| 94 | + public var xonly: XonlyKey { |
| 95 | + XonlyKey(baseKey: baseKey.xonly) |
| 96 | + } |
| 97 | + |
| 98 | + /// A key format representation of the public key |
| 99 | + public var format: secp256k1.Format { |
| 100 | + baseKey.format |
| 101 | + } |
| 102 | + |
| 103 | + /// Generates a secp256k1 public key. |
| 104 | + /// - Parameter baseKey: generated secp256k1 public key. |
| 105 | + fileprivate init(baseKey: PublicKeyImplementation) { |
| 106 | + self.baseKey = baseKey |
| 107 | + } |
| 108 | + |
| 109 | + /// Generates a secp256k1 public key from a raw representation. |
| 110 | + /// - Parameter data: A raw representation of the key. |
| 111 | + /// - Throws: An error is thrown when the raw representation does not create a public key. |
| 112 | + public init<D: ContiguousBytes>(rawRepresentation data: D, xonly: D, format: secp256k1.Format) { |
| 113 | + self.baseKey = PublicKeyImplementation(rawRepresentation: data, xonly: xonly, format: format) |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + /// The corresponding x-only public key. |
| 118 | + public struct XonlyKey { |
| 119 | + /// Generated secp256k1 x-only public key. |
| 120 | + private let baseKey: XonlyKeyImplementation |
| 121 | + |
| 122 | + /// The secp256k1 x-only public key object |
| 123 | + public var bytes: [UInt8] { |
| 124 | + baseKey.bytes |
| 125 | + } |
| 126 | + |
| 127 | + fileprivate init(baseKey: XonlyKeyImplementation) { |
| 128 | + self.baseKey = baseKey |
| 129 | + } |
| 130 | + |
| 131 | + public init<D: ContiguousBytes>(rawRepresentation data: D) { |
| 132 | + self.baseKey = XonlyKeyImplementation(rawRepresentation: data) |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | +} |
0 commit comments