Skip to content

Commit

Permalink
chore: update and properly constrain secp256k1
Browse files Browse the repository at this point in the history
Signed-off-by: Skyler Ross <[email protected]>
  • Loading branch information
izik1 committed Jun 28, 2023
1 parent 71e0246 commit d06dba8
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 31 deletions.
15 changes: 3 additions & 12 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/GigaBitcoin/secp256k1.swift.git",
"state" : {
"revision" : "48fb20fce4ca3aad89180448a127d5bc16f0e44c",
"version" : "0.10.0"
"revision" : "1a14e189def5eaa92f839afdd2faad8e43b61a6e",
"version" : "0.12.2"
}
},
{
Expand Down Expand Up @@ -68,16 +68,7 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-docc-plugin",
"state" : {
"revision" : "9b1258905c21fc1b97bf03d1b4ca12c4ec4e5fda",
"version" : "1.2.0"
}
},
{
"identity" : "swift-docc-symbolkit",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-docc-symbolkit",
"state" : {
"revision" : "b45d1f2ed151d057b54504d653e0da5552844e34",
"revision" : "3303b164430d9a7055ba484c8ead67a52f7b74f6",
"version" : "1.0.0"
}
},
Expand Down
4 changes: 2 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ let package = Package(
.package(url: "https://github.com/vsanthanam/AnyAsyncSequence.git", from: "1.0.0"),
.package(url: "https://github.com/apple/swift-atomics.git", from: "1.1.0"),
// swift-asn1 wants swift 5.7+ past 0.4
.package(url: "https://github.com/apple/swift-asn1.git", "0.3.0"..<"0.4.0"),
.package(url: "https://github.com/GigaBitcoin/secp256k1.swift.git", .upToNextMajor(from: "0.10.0")),
.package(url: "https://github.com/apple/swift-asn1.git", .upToNextMinor(from: "0.3.0")),
.package(url: "https://github.com/GigaBitcoin/secp256k1.swift.git", .upToNextMinor(from: "0.12.0")),
// we use this entirely for sha3-keccak256, yes, I'm serious.
.package(url: "https://github.com/krzyzanowskim/CryptoSwift.git", from: "1.0.0"),
.package(url: "https://github.com/apple/swift-docc-plugin", from: "1.0.0"),
Expand Down
10 changes: 5 additions & 5 deletions Sources/Hedera/PrivateKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,15 @@ public struct PrivateKey: LosslessStringConvertible, ExpressibleByStringLiteral,

fileprivate init(kind: PrivateKey.Kind) {
switch kind {
case .ecdsa(let key): self = .ecdsa(key.rawRepresentation)
case .ecdsa(let key): self = .ecdsa(key.dataRepresentation)
case .ed25519(let key): self = .ed25519(key.rawRepresentation)
}
}

fileprivate var kind: PrivateKey.Kind {
// swiftlint:disable force_try
switch self {
case .ecdsa(let key): return .ecdsa(try! .init(rawRepresentation: key))
case .ecdsa(let key): return .ecdsa(try! .init(dataRepresentation: key))
case .ed25519(let key): return .ed25519(try! .init(rawRepresentation: key))
}

Expand Down Expand Up @@ -144,7 +144,7 @@ public struct PrivateKey: LosslessStringConvertible, ExpressibleByStringLiteral,
}

do {
self.init(kind: .ecdsa(try .init(rawRepresentation: bytes.safeSubdata(in: 0..<32)!)))
self.init(kind: .ecdsa(try .init(dataRepresentation: bytes.safeSubdata(in: 0..<32)!)))
return
} catch {
throw HError.keyParse(String(describing: error))
Expand Down Expand Up @@ -335,7 +335,7 @@ public struct PrivateKey: LosslessStringConvertible, ExpressibleByStringLiteral,

public func toBytesRaw() -> Data {
switch kind {
case .ecdsa(let ecdsa): return ecdsa.rawRepresentation
case .ecdsa(let ecdsa): return ecdsa.dataRepresentation
case .ed25519(let ed25519): return ed25519.rawRepresentation
}
}
Expand Down Expand Up @@ -380,7 +380,7 @@ public struct PrivateKey: LosslessStringConvertible, ExpressibleByStringLiteral,
public func sign(_ message: Data) -> Data {
switch kind {
case .ecdsa(let key):
return try! key.ecdsa.signature(for: Keccak256Digest(Crypto.Sha3.keccak256(message))!).compactRepresentation
return try! key.signature(for: Keccak256Digest(Crypto.Sha3.keccak256(message))!).compactRepresentation
case .ed25519(let key):
return try! key.signature(for: message)
}
Expand Down
26 changes: 16 additions & 10 deletions Sources/Hedera/PublicKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ public struct PublicKey: LosslessStringConvertible, ExpressibleByStringLiteral,

fileprivate init(kind: PublicKey.Kind) {
switch kind {
case .ecdsa(let key): self = .ecdsa(key.rawRepresentation, compressed: key.format == .compressed)
case .ecdsa(let key): self = .ecdsa(key.dataRepresentation, compressed: key.format == .compressed)
case .ed25519(let key): self = .ed25519(key.rawRepresentation)
}
}

fileprivate var kind: PublicKey.Kind {
switch self {
case .ecdsa(let key, let compressed):
return .ecdsa(try! .init(rawRepresentation: key, format: compressed ? .compressed : .uncompressed))
return .ecdsa(try! .init(dataRepresentation: key, format: compressed ? .compressed : .uncompressed))
case .ed25519(let key): return .ed25519(try! .init(rawRepresentation: key))
}
}
Expand Down Expand Up @@ -129,7 +129,7 @@ public struct PublicKey: LosslessStringConvertible, ExpressibleByStringLiteral,
}

do {
self.init(.ecdsa(try .init(rawRepresentation: bytes, format: .compressed)))
self.init(.ecdsa(try .init(dataRepresentation: bytes, format: .compressed)))
} catch {
throw HError.keyParse(String(describing: error))
}
Expand Down Expand Up @@ -239,7 +239,7 @@ public struct PublicKey: LosslessStringConvertible, ExpressibleByStringLiteral,

public func toBytesRaw() -> Data {
switch kind {
case .ecdsa(let key): return key.rawRepresentation
case .ecdsa(let key): return key.dataRepresentation
case .ed25519(let key): return key.rawRepresentation
}
}
Expand Down Expand Up @@ -274,7 +274,7 @@ public struct PublicKey: LosslessStringConvertible, ExpressibleByStringLiteral,
case .ecdsa(let key):
let isValid: Bool
do {
isValid = try key.ecdsa.isValidSignature(
isValid = try key.isValidSignature(
.init(compactRepresentation: signature), for: Keccak256Digest(Crypto.Sha3.keccak256(message))!)
} catch {
throw HError(kind: .signatureVerify, description: "invalid signature")
Expand Down Expand Up @@ -319,12 +319,15 @@ public struct PublicKey: LosslessStringConvertible, ExpressibleByStringLiteral,
return nil
}

let context = secp256k1.Context.rawRepresentation

// when the bindings aren't enough :/
// and to be clear, using `key.rawRepresentation` which gives a `secp256k1_pubkey` _fails_ to work.
var pubkey = secp256k1_pubkey()

key.rawRepresentation.withUnsafeTypedBytes { bytes in
key.dataRepresentation.withUnsafeTypedBytes { bytes in
let result = secp256k1_bindings.secp256k1_ec_pubkey_parse(
secp256k1.Context.raw,
context,
&pubkey,
bytes.baseAddress!,
bytes.count
Expand All @@ -333,16 +336,19 @@ public struct PublicKey: LosslessStringConvertible, ExpressibleByStringLiteral,
precondition(result == 1)
}

var output = Data(repeating: 0, count: 65)
let format = secp256k1.Format.uncompressed

var output = Data(repeating: 0, count: format.length)

output.withUnsafeMutableTypedBytes { output in
var outputLen = output.count

let result = secp256k1_ec_pubkey_serialize(
secp256k1.Context.raw, output.baseAddress!,
context,
output.baseAddress!,
&outputLen,
&pubkey,
secp256k1.Format.uncompressed.rawValue
format.rawValue
)

precondition(result == 1)
Expand Down
4 changes: 2 additions & 2 deletions Tests/HederaTests/PublicKeyTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ internal final class PublicKeyTests: XCTestCase {
"debae3ca62ab3157110dba79c8de26540dc320ee9be73a77d70ba175643a3500"
).publicKey

let evmAddress = publicKey.toEvmAddress()
let evmAddress = try XCTUnwrap(publicKey.toEvmAddress())

XCTAssertEqual(evmAddress, "0xd8eb8db03c699faa3f47adcdcd2ae91773b10f8b")
}
Expand All @@ -61,7 +61,7 @@ internal final class PublicKeyTests: XCTestCase {
let publicKey = try PublicKey.fromStringEcdsa(
"029469a657510f3bf199a0e29b21e11e7039d8883f3547d59c3568f9c89f704cbc")

let evmAddress = publicKey.toEvmAddress()
let evmAddress = try XCTUnwrap(publicKey.toEvmAddress())

XCTAssertEqual(evmAddress, "0xbbaa6bdfe888ae1fc8e7c8cee82081fa79ba8834")
}
Expand Down

0 comments on commit d06dba8

Please sign in to comment.