Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ensure valid random PrivateKey if no secret is given #392

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 8 additions & 10 deletions Sources/zkp/secp256k1.swift
Original file line number Diff line number Diff line change
Expand Up @@ -151,16 +151,14 @@ extension secp256k1 {

/// Backing initialization that creates a random secp256k1 private key for signing
@usableFromInline init(format: secp256k1.Format = .compressed) throws {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We will want to update the function signature so a try catch statement isn't needed.

Suggested change
@usableFromInline init(format: secp256k1.Format = .compressed) throws {
@usableFromInline init(format: secp256k1.Format = .compressed) {

let privateKey = SecureBytes(count: secp256k1.ByteDetails.count)
self.keyParity = 0
self.format = format
self.privateBytes = privateKey
self.publicBytes = try PublicKeyImplementation.generate(bytes: &privateBytes, format: format)
self.xonlyBytes = try XonlyKeyImplementation.generate(
bytes: publicBytes,
keyParity: &keyParity,
format: format
)
for _ in 0 ..< 10 {
let randomBytes = SecureBytes(count: secp256k1.ByteDetails.count)
if let privateKey = try? PrivateKeyImplementation(dataRepresentation: Data(randomBytes), format: format) {
self = privateKey
return
}
}
fatalError("Looped more than 10 times trying to generate a key")
}

/// Backing initialization that creates a secp256k1 private key for signing from a data representation.
Expand Down