Skip to content

Commit 6171381

Browse files
committed
Made FFI init non throwing -> many methods in K1 are no longer throwing. Mapping returned value of many secp256k1 functions to ResultRaw enum using apinotes. Verify signature functions return VerifySignatureOutcomeRaw using apinotes. Normalize signature function return NormalizeSignatureOutcomeRaw using apinotes.
1 parent 63bedc2 commit 6171381

29 files changed

+424
-287
lines changed

Sources/K1/K1/ECDSA/ECDSASignatureRecoverable.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,10 @@ extension K1.ECDSAWithKeyRecovery.Signature {
6464
/// Compact aka `IEEE P1363` aka `R||S` and `V` (`RecoveryID`).
6565
public func compact() throws -> Compact {
6666
// swiftlint:disable:next identifier_name
67-
let (rs, recid) = try FFI.ECDSAWithKeyRecovery.serializeCompact(
67+
let (rs, recid) = FFI.ECDSAWithKeyRecovery.serializeCompact(
6868
wrapped
6969
)
70-
return try .init(
70+
return try Compact(
7171
compact: Data(rs),
7272
recoveryID: .init(recid: recid)
7373
)
@@ -199,8 +199,8 @@ extension K1.ECDSAWithKeyRecovery.Signature {
199199
// MARK: Conversion
200200
extension K1.ECDSAWithKeyRecovery.Signature {
201201
/// Converts this recoverable ECDSA signature to a non-recoverable version.
202-
public func nonRecoverable() throws -> K1.ECDSA.Signature {
203-
try K1.ECDSA.Signature(
202+
public func nonRecoverable() -> K1.ECDSA.Signature {
203+
K1.ECDSA.Signature(
204204
wrapped: FFI.ECDSAWithKeyRecovery.nonRecoverable(self.wrapped)
205205
)
206206
}

Sources/K1/K1/Keys/Keys.generated.swift

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,16 @@ extension K1.KeyAgreement {
1717
let publicKeyImpl: K1._PublicKeyImplementation
1818

1919
/// The corresponding public key.
20-
public var publicKey: PublicKey {
21-
try! .init(rawRepresentation: publicKeyImpl.rawRepresentation)
22-
}
20+
public let publicKey: PublicKey
2321

2422
init(impl: Impl) {
2523
self.impl = impl
2624
self.publicKeyImpl = impl.publicKey
25+
do {
26+
self.publicKey = try PublicKey(rawRepresentation: impl.publicKey.rawRepresentation)
27+
} catch {
28+
fatalError("Should never fail to instantiate a PublicKey from a PrivateKey, error: \(error)")
29+
}
2730
}
2831

2932
/// Creates a random `secp256k1` private key for key agreement.
@@ -239,13 +242,16 @@ extension K1.Schnorr {
239242
let publicKeyImpl: K1._PublicKeyImplementation
240243

241244
/// The corresponding public key.
242-
public var publicKey: PublicKey {
243-
try! .init(rawRepresentation: publicKeyImpl.rawRepresentation)
244-
}
245+
public let publicKey: PublicKey
245246

246247
init(impl: Impl) {
247248
self.impl = impl
248249
self.publicKeyImpl = impl.publicKey
250+
do {
251+
self.publicKey = try PublicKey(rawRepresentation: impl.publicKey.rawRepresentation)
252+
} catch {
253+
fatalError("Should never fail to instantiate a PublicKey from a PrivateKey, error: \(error)")
254+
}
249255
}
250256

251257
/// Creates a random `secp256k1` private key for signing.
@@ -462,13 +468,16 @@ extension K1.ECDSA {
462468
let publicKeyImpl: K1._PublicKeyImplementation
463469

464470
/// The corresponding public key.
465-
public var publicKey: PublicKey {
466-
try! .init(rawRepresentation: publicKeyImpl.rawRepresentation)
467-
}
471+
public let publicKey: PublicKey
468472

469473
init(impl: Impl) {
470474
self.impl = impl
471475
self.publicKeyImpl = impl.publicKey
476+
do {
477+
self.publicKey = try PublicKey(rawRepresentation: impl.publicKey.rawRepresentation)
478+
} catch {
479+
fatalError("Should never fail to instantiate a PublicKey from a PrivateKey, error: \(error)")
480+
}
472481
}
473482

474483
/// Creates a random `secp256k1` private key for signing.
@@ -685,13 +694,16 @@ extension K1.ECDSAWithKeyRecovery {
685694
let publicKeyImpl: K1._PublicKeyImplementation
686695

687696
/// The corresponding public key.
688-
public var publicKey: PublicKey {
689-
try! .init(rawRepresentation: publicKeyImpl.rawRepresentation)
690-
}
697+
public let publicKey: PublicKey
691698

692699
init(impl: Impl) {
693700
self.impl = impl
694701
self.publicKeyImpl = impl.publicKey
702+
do {
703+
self.publicKey = try PublicKey(rawRepresentation: impl.publicKey.rawRepresentation)
704+
} catch {
705+
fatalError("Should never fail to instantiate a PublicKey from a PrivateKey, error: \(error)")
706+
}
695707
}
696708

697709
/// Creates a random `secp256k1` private key for signing.

Sources/K1/K1/Keys/Keys.swift.gyb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,17 @@ extension K1.${FEATURE} {
5858
let publicKeyImpl: K1._PublicKeyImplementation
5959

6060
/// The corresponding public key.
61-
public var publicKey: PublicKey {
62-
try! .init(rawRepresentation: publicKeyImpl.rawRepresentation)
63-
}
61+
public let publicKey: PublicKey
62+
6463

6564
init(impl: Impl) {
6665
self.impl = impl
6766
self.publicKeyImpl = impl.publicKey
67+
do {
68+
self.publicKey = try PublicKey(rawRepresentation: impl.publicKey.rawRepresentation)
69+
} catch {
70+
fatalError("Should never fail to instantiate a PublicKey from a PrivateKey, error: \(error)")
71+
}
6872
}
6973

7074
/// Creates a random `secp256k1` private key for ${PURPOSE_PRIVATEKEY}.

Sources/K1/K1/Keys/PublicKeyImplementation.swift

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,12 @@ extension K1._PublicKeyImplementation {
8383

8484
/// `04 || X || Y` (65 bytes)
8585
var x963Representation: Data {
86-
// swiftlint:disable:next force_try
87-
try! FFI.PublicKey.serialize(wrapped, format: .uncompressed)
86+
FFI.PublicKey.serialize(wrapped, format: .uncompressed)
8887
}
8988

9089
/// `02|03 || X` (33 bytes)
9190
var compressedRepresentation: Data {
92-
// swiftlint:disable:next force_try
93-
try! FFI.PublicKey.serialize(wrapped, format: .compressed)
91+
FFI.PublicKey.serialize(wrapped, format: .compressed)
9492
}
9593

9694
/// `DER`
@@ -119,15 +117,7 @@ extension K1._PublicKeyImplementation {
119117
static func == (lhsSelf: Self, rhsSelf: Self) -> Bool {
120118
let lhs = lhsSelf.wrapped
121119
let rhs = rhsSelf.wrapped
122-
do {
123-
return try lhs.compare(to: rhs)
124-
} catch {
125-
return lhs.withUnsafeBytes { lhsBytes in
126-
rhs.withUnsafeBytes { rhsBytes in
127-
safeCompare(lhsBytes, rhsBytes)
128-
}
129-
}
130-
}
120+
return lhs.isEqual(to: rhs)
131121
}
132122
}
133123

Sources/K1/K1/Validation/Validation.generated.swift

Lines changed: 29 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,12 @@ extension K1.ECDSA.PublicKey {
2121
hashed: some DataProtocol,
2222
options: K1.ECDSA.ValidationOptions = .default
2323
) -> Bool {
24-
do {
25-
return try FFI.ECDSA.isValid(
26-
signature: signature.wrapped,
27-
publicKey: self.impl.wrapped,
28-
message: [UInt8](hashed),
29-
options: options
30-
)
31-
} catch {
32-
return false
33-
}
24+
FFI.ECDSA.isValid(
25+
signature: signature.wrapped,
26+
publicKey: self.impl.wrapped,
27+
message: [UInt8](hashed),
28+
options: options
29+
)
3430
}
3531

3632
/// Verifies an Elliptic Curve Digital Signature Algorithm (ECDSA) non recoverable signature on a digest over the `secp256k1` elliptic curve.
@@ -87,16 +83,12 @@ extension K1.ECDSAWithKeyRecovery.PublicKey {
8783
hashed: some DataProtocol,
8884
options: K1.ECDSA.ValidationOptions = .default
8985
) -> Bool {
90-
do {
91-
return try FFI.ECDSAWithKeyRecovery.isValid(
92-
signature: signature.wrapped,
93-
publicKey: self.impl.wrapped,
94-
message: [UInt8](hashed),
95-
options: options
96-
)
97-
} catch {
98-
return false
99-
}
86+
FFI.ECDSAWithKeyRecovery.isValid(
87+
signature: signature.wrapped,
88+
publicKey: self.impl.wrapped,
89+
message: [UInt8](hashed),
90+
options: options
91+
)
10092
}
10193

10294
/// Verifies an Elliptic Curve Digital Signature Algorithm (ECDSA) recoverable signature on a digest over the `secp256k1` elliptic curve.
@@ -151,15 +143,11 @@ extension K1.Schnorr.PublicKey {
151143
_ signature: K1.Schnorr.Signature,
152144
hashed: Span<UInt8>
153145
) -> Bool {
154-
do {
155-
return try FFI.Schnorr.isValid(
156-
signature: signature.wrapped,
157-
publicKey: self.impl.wrapped,
158-
message: hashed
159-
)
160-
} catch {
161-
return false
162-
}
146+
FFI.Schnorr.isValid(
147+
signature: signature.wrapped,
148+
publicKey: self.impl.wrapped,
149+
message: hashed
150+
)
163151
}
164152

165153
/// Verifies Schnorr signature on some _hash_ over the `secp256k1` elliptic curve.
@@ -171,16 +159,12 @@ extension K1.Schnorr.PublicKey {
171159
_ signature: K1.Schnorr.Signature,
172160
hashed bytes: [UInt8]
173161
) -> Bool {
174-
do {
175-
return try withSpanFromArray(bytes) { span in
176-
try FFI.Schnorr.isValid(
177-
signature: signature.wrapped,
178-
publicKey: self.impl.wrapped,
179-
message: span
180-
)
181-
}
182-
} catch {
183-
return false
162+
withSpanFromArray(bytes) { span in
163+
FFI.Schnorr.isValid(
164+
signature: signature.wrapped,
165+
publicKey: self.impl.wrapped,
166+
message: span
167+
)
184168
}
185169
}
186170

@@ -193,16 +177,12 @@ extension K1.Schnorr.PublicKey {
193177
_ signature: K1.Schnorr.Signature,
194178
hashed data: some DataProtocol
195179
) -> Bool {
196-
do {
197-
return try withSpanFromData(data) { span in
198-
try FFI.Schnorr.isValid(
199-
signature: signature.wrapped,
200-
publicKey: self.impl.wrapped,
201-
message: span
202-
)
203-
}
204-
} catch {
205-
return false
180+
withSpanFromData(data) { span in
181+
FFI.Schnorr.isValid(
182+
signature: signature.wrapped,
183+
publicKey: self.impl.wrapped,
184+
message: span
185+
)
206186
}
207187
}
208188

Sources/K1/K1/Validation/Validation.swift.gyb

Lines changed: 22 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,11 @@ extension K1.${FEATURE}.PublicKey {
5858
_ signature: K1.${FEATURE}.Signature,
5959
hashed: Span<UInt8>${VALIDATION_OPTIONS_ARG}
6060
) -> Bool {
61-
do {
62-
return try FFI.${FEATURE}.isValid(
63-
signature: signature.wrapped,
64-
publicKey: self.impl.wrapped,
65-
message: hashed${VALIDATION_OPTIONS_FWD}
66-
)
67-
} catch {
68-
return false
69-
}
61+
FFI.${FEATURE}.isValid(
62+
signature: signature.wrapped,
63+
publicKey: self.impl.wrapped,
64+
message: hashed${VALIDATION_OPTIONS_FWD}
65+
)
7066
}
7167

7268
/// Verifies ${VARIANT} signature on some _hash_ over the `secp256k1` elliptic curve.
@@ -78,16 +74,12 @@ extension K1.${FEATURE}.PublicKey {
7874
_ signature: K1.${FEATURE}.Signature,
7975
hashed bytes: [UInt8]${VALIDATION_OPTIONS_ARG}
8076
) -> Bool {
81-
do {
82-
return try withSpanFromArray(bytes) { span in
83-
try FFI.${FEATURE}.isValid(
84-
signature: signature.wrapped,
85-
publicKey: self.impl.wrapped,
86-
message: span${VALIDATION_OPTIONS_FWD}
87-
)
88-
}
89-
} catch {
90-
return false
77+
withSpanFromArray(bytes) { span in
78+
FFI.${FEATURE}.isValid(
79+
signature: signature.wrapped,
80+
publicKey: self.impl.wrapped,
81+
message: span${VALIDATION_OPTIONS_FWD}
82+
)
9183
}
9284
}
9385

@@ -100,16 +92,12 @@ extension K1.${FEATURE}.PublicKey {
10092
_ signature: K1.${FEATURE}.Signature,
10193
hashed data: some DataProtocol${VALIDATION_OPTIONS_ARG}
10294
) -> Bool {
103-
do {
104-
return try withSpanFromData(data) { span in
105-
try FFI.${FEATURE}.isValid(
106-
signature: signature.wrapped,
107-
publicKey: self.impl.wrapped,
108-
message: span${VALIDATION_OPTIONS_FWD}
109-
)
110-
}
111-
} catch {
112-
return false
95+
withSpanFromData(data) { span in
96+
FFI.${FEATURE}.isValid(
97+
signature: signature.wrapped,
98+
publicKey: self.impl.wrapped,
99+
message: span${VALIDATION_OPTIONS_FWD}
100+
)
113101
}
114102
}
115103
% else:
@@ -120,15 +108,11 @@ extension K1.${FEATURE}.PublicKey {
120108
_ signature: K1.${FEATURE}.Signature,
121109
hashed: some DataProtocol${VALIDATION_OPTIONS_ARG}
122110
) -> Bool {
123-
do {
124-
return try FFI.${FEATURE}.isValid(
125-
signature: signature.wrapped,
126-
publicKey: self.impl.wrapped,
127-
message: [UInt8](hashed)${VALIDATION_OPTIONS_FWD}
128-
)
129-
} catch {
130-
return false
131-
}
111+
FFI.${FEATURE}.isValid(
112+
signature: signature.wrapped,
113+
publicKey: self.impl.wrapped,
114+
message: [UInt8](hashed)${VALIDATION_OPTIONS_FWD}
115+
)
132116
}
133117
% end
134118

Sources/K1/Support/Extensions/Data+Extensions.swift renamed to Sources/K1/Support/Extensions/Foundation/Data+Extensions.swift

File renamed without changes.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import Secp256k1
2+
3+
/// Outcome of comparing two values with each other
4+
enum ComparisonOutcome {
5+
/// The compared values are equal.
6+
case equal
7+
8+
/// The RHS of the compared values is greater than the LHS.
9+
case lhsIsGreater
10+
11+
/// The RHS of the compared values is greater than the LHS.
12+
case rhsIsGreater
13+
}
14+
15+
extension ComparisonOutcome {
16+
init(raw: ComparisonOutcomeRaw) {
17+
switch raw {
18+
case .SECP256K1_PUBKEY_CMP_EQUAL: self = .equal
19+
case .SECP256K1_PUBKEY_CMP_LHS_IS_GREATER: self = .lhsIsGreater
20+
case .SECP256K1_PUBKEY_CMP_RHS_IS_GREATER: self = .rhsIsGreater
21+
}
22+
}
23+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import Secp256k1
2+
3+
extension NormalizeSignatureOutcomeRaw {
4+
/// The checked signature was not normalized.
5+
static let wasntNormalized: Self = .SECP256K1_NORMALIZE_SIG_WASNT_NORMALIZED
6+
}

0 commit comments

Comments
 (0)