|
| 1 | +// |
| 2 | +// Recovery.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 | +import secp256k1_bindings |
| 13 | + |
| 14 | +// MARK: - secp256k1 + Recovery |
| 15 | + |
| 16 | +public extension secp256k1 { |
| 17 | + enum Recovery { |
| 18 | + public struct PublicKey { |
| 19 | + let baseKey: PublicKeyImplementation |
| 20 | + |
| 21 | + /// Initializes a secp256k1 public key using a data message and a recovery signature. |
| 22 | + /// - Parameters: |
| 23 | + /// - data: The data to be hash and assumed signed. |
| 24 | + /// - signature: A raw representation of the initialized signature that supports pubkey recovery. |
| 25 | + /// - format: the format of the public key object |
| 26 | + public init<D: DataProtocol>( |
| 27 | + _ data: D, |
| 28 | + signature: secp256k1.Recovery.ECDSASignature, |
| 29 | + format: secp256k1.Format = .compressed |
| 30 | + ) throws { |
| 31 | + self.baseKey = try PublicKeyImplementation( |
| 32 | + SHA256.hash(data: data), |
| 33 | + signature: signature, |
| 34 | + format: format |
| 35 | + ) |
| 36 | + } |
| 37 | + |
| 38 | + /// Initializes a secp256k1 public key using a hash digest and a recovery signature. |
| 39 | + /// - Parameters: |
| 40 | + /// - digest: The hash digest assumed to be signed. |
| 41 | + /// - signature: A raw representation of the initialized signature that supports pubkey recovery. |
| 42 | + /// - format: the format of the public key object |
| 43 | + public init<D: Digest>( |
| 44 | + _ digest: D, |
| 45 | + signature: secp256k1.Recovery.ECDSASignature, |
| 46 | + format: secp256k1.Format = .compressed |
| 47 | + ) throws { |
| 48 | + self.baseKey = try PublicKeyImplementation(digest, signature: signature, format: format) |
| 49 | + } |
| 50 | + |
| 51 | + /// Initializes a secp256k1 public key for recovery. |
| 52 | + /// - Parameter baseKey: generated secp256k1 public key. |
| 53 | + init(baseKey: PublicKeyImplementation) { |
| 54 | + self.baseKey = baseKey |
| 55 | + } |
| 56 | + |
| 57 | + /// A data representation of the public key |
| 58 | + public var rawRepresentation: Data { baseKey.rawRepresentation } |
| 59 | + |
| 60 | + /// Implementation public key object |
| 61 | + var bytes: [UInt8] { baseKey.bytes } |
| 62 | + } |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +/// An ECDSA (Elliptic Curve Digital Signature Algorithm) Recovery Signature |
| 67 | +public extension secp256k1.Recovery { |
| 68 | + |
| 69 | + /// Recovery Signature |
| 70 | + struct ECDSACompactSignature { |
| 71 | + let signature: Data |
| 72 | + let recoveryId: Int32 |
| 73 | + } |
| 74 | + |
| 75 | + struct ECDSASignature: ContiguousBytes, RawSignature { |
| 76 | + /// Returns the raw signature. |
| 77 | + public var rawRepresentation: Data |
| 78 | + |
| 79 | + /// Initializes ECDSASignature from the raw representation. |
| 80 | + /// - Parameters: |
| 81 | + /// - rawRepresentation: A raw representation of the key as a collection of contiguous bytes. |
| 82 | + /// - Throws: If there is a failure with the dataRepresentation count |
| 83 | + public init<D: DataProtocol>(rawRepresentation: D) throws { |
| 84 | + guard rawRepresentation.count == 4 * secp256k1.CurveDetails.coordinateByteCount + 1 else { |
| 85 | + throw secp256k1Error.incorrectParameterSize |
| 86 | + } |
| 87 | + |
| 88 | + self.rawRepresentation = Data(rawRepresentation) |
| 89 | + } |
| 90 | + |
| 91 | + /// Initializes ECDSASignature from the raw representation. |
| 92 | + /// - Parameters: |
| 93 | + /// - rawRepresentation: A raw representation of the key as a collection of contiguous bytes. |
| 94 | + /// - Throws: If there is a failure with the dataRepresentation count |
| 95 | + internal init(_ dataRepresentation: Data) throws { |
| 96 | + guard dataRepresentation.count == 4 * secp256k1.CurveDetails.coordinateByteCount + 1 else { |
| 97 | + throw secp256k1Error.incorrectParameterSize |
| 98 | + } |
| 99 | + |
| 100 | + self.rawRepresentation = dataRepresentation |
| 101 | + } |
| 102 | + |
| 103 | + /// Initializes ECDSASignature from the Compact representation. |
| 104 | + /// - Parameter compactRepresentation: A Compact representation of the key as a collection of contiguous bytes. |
| 105 | + /// - Throws: If there is a failure with parsing the derRepresentation |
| 106 | + public init<D: DataProtocol>(compactRepresentation: D, recoveryId: Int32) throws { |
| 107 | + var recoverableSignature = secp256k1_ecdsa_recoverable_signature() |
| 108 | + |
| 109 | + guard secp256k1_ecdsa_recoverable_signature_parse_compact( |
| 110 | + secp256k1.Context.raw, |
| 111 | + &recoverableSignature, |
| 112 | + Array(compactRepresentation), |
| 113 | + recoveryId |
| 114 | + ).boolValue else { |
| 115 | + throw secp256k1Error.underlyingCryptoError |
| 116 | + } |
| 117 | + |
| 118 | + self.rawRepresentation = recoverableSignature.dataValue |
| 119 | + } |
| 120 | + |
| 121 | + /// Invokes the given closure with a buffer pointer covering the raw bytes of the digest. |
| 122 | + /// - Parameter body: A closure that takes a raw buffer pointer to the bytes of the digest and returns the digest. |
| 123 | + /// - Throws: If there is a failure with underlying `withUnsafeBytes` |
| 124 | + /// - Returns: The signature as returned from the body closure. |
| 125 | + public func withUnsafeBytes<R>(_ body: (UnsafeRawBufferPointer) throws -> R) rethrows -> R { |
| 126 | + try rawRepresentation.withUnsafeBytes(body) |
| 127 | + } |
| 128 | + |
| 129 | + /// Serialize an ECDSA signature in compact (64 byte) format. |
| 130 | + /// - Throws: If there is a failure parsing signature |
| 131 | + /// - Returns: a 64-byte data representation of the compact serialization |
| 132 | + public var compactRepresentation: ECDSACompactSignature { |
| 133 | + get throws { |
| 134 | + let compactSignatureLength = 64 |
| 135 | + var recoveryId = Int32() |
| 136 | + var recoverableSignature = secp256k1_ecdsa_recoverable_signature() |
| 137 | + var compactSignature = [UInt8](repeating: 0, count: compactSignatureLength) |
| 138 | + |
| 139 | + rawRepresentation.copyToUnsafeMutableBytes(of: &recoverableSignature.data) |
| 140 | + |
| 141 | + guard secp256k1_ecdsa_recoverable_signature_serialize_compact( |
| 142 | + secp256k1.Context.raw, |
| 143 | + &compactSignature, |
| 144 | + &recoveryId, |
| 145 | + &recoverableSignature |
| 146 | + ).boolValue else { |
| 147 | + throw secp256k1Error.underlyingCryptoError |
| 148 | + } |
| 149 | + |
| 150 | + return secp256k1.Recovery.ECDSACompactSignature( |
| 151 | + signature: Data(bytes: &compactSignature, count: compactSignatureLength), |
| 152 | + recoveryId: recoveryId |
| 153 | + ) |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + /// Convert a recoverable signature into a normal signature. |
| 158 | + public var normalize: secp256k1.Signing.ECDSASignature { |
| 159 | + get throws { |
| 160 | + var normalizedSignature = secp256k1_ecdsa_signature() |
| 161 | + var recoverableSignature = secp256k1_ecdsa_recoverable_signature() |
| 162 | + |
| 163 | + rawRepresentation.copyToUnsafeMutableBytes(of: &recoverableSignature.data) |
| 164 | + |
| 165 | + guard secp256k1_ecdsa_recoverable_signature_convert( |
| 166 | + secp256k1.Context.raw, |
| 167 | + &normalizedSignature, |
| 168 | + &recoverableSignature |
| 169 | + ).boolValue else { |
| 170 | + throw secp256k1Error.underlyingCryptoError |
| 171 | + } |
| 172 | + |
| 173 | + return try secp256k1.Signing.ECDSASignature(normalizedSignature.dataValue) |
| 174 | + } |
| 175 | + } |
| 176 | + } |
| 177 | +} |
0 commit comments