mirror of
https://github.com/signalapp/libsignal.git
synced 2026-04-26 01:35:22 +02:00
These might be places where we've chosen not to expose an init, but
they might also be places where we forgot. (It's only structs because
classes don't synthesize initializers by default, other than
inheriting them.)
As a regex-based check, this isn't perfect; it's specifically looking
for "public" followed by "struct" followed by "{" followed by "}" with
no "init(" (or a few other variations) between the two braces. This
does not at all handle nesting, so if a struct has other, non-stored
members, they must come after at least one initializer.
96 lines
2.9 KiB
Swift
96 lines
2.9 KiB
Swift
//
|
|
// Copyright 2020-2022 Signal Messenger, LLC.
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
//
|
|
|
|
import Foundation
|
|
import SignalFfi
|
|
|
|
public struct IdentityKey: Equatable, Sendable {
|
|
public let publicKey: PublicKey
|
|
|
|
public init(publicKey: PublicKey) {
|
|
self.publicKey = publicKey
|
|
}
|
|
|
|
public init<Bytes: ContiguousBytes>(bytes: Bytes) throws {
|
|
self.publicKey = try PublicKey(bytes)
|
|
}
|
|
|
|
public func serialize() -> Data {
|
|
return self.publicKey.serialize()
|
|
}
|
|
|
|
public func verifyAlternateIdentity<Bytes: ContiguousBytes>(_ other: IdentityKey, signature: Bytes) throws -> Bool {
|
|
return try withAllBorrowed(publicKey, other.publicKey, .bytes(signature)) {
|
|
selfHandle,
|
|
otherHandle,
|
|
signatureBuffer in
|
|
try invokeFnReturningBool {
|
|
signal_identitykey_verify_alternate_identity(
|
|
$0,
|
|
selfHandle.const(),
|
|
otherHandle.const(),
|
|
signatureBuffer
|
|
)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public struct IdentityKeyPair: Sendable {
|
|
public let publicKey: PublicKey
|
|
public let privateKey: PrivateKey
|
|
|
|
public init(publicKey: PublicKey, privateKey: PrivateKey) {
|
|
self.publicKey = publicKey
|
|
self.privateKey = privateKey
|
|
}
|
|
|
|
public init<Bytes: ContiguousBytes>(bytes: Bytes) throws {
|
|
let out = try bytes.withUnsafeBorrowedBuffer { bytes in
|
|
try invokeFnReturningValueByPointer(.init()) {
|
|
signal_identitykeypair_deserialize($0, bytes)
|
|
}
|
|
}
|
|
|
|
self.publicKey = PublicKey(owned: NonNull(out.first)!)
|
|
self.privateKey = PrivateKey(owned: NonNull(out.second)!)
|
|
}
|
|
|
|
public static func generate() -> IdentityKeyPair {
|
|
let privateKey = PrivateKey.generate()
|
|
let publicKey = privateKey.publicKey
|
|
return IdentityKeyPair(publicKey: publicKey, privateKey: privateKey)
|
|
}
|
|
|
|
public func serialize() -> Data {
|
|
return failOnError {
|
|
try withAllBorrowed(self.publicKey, self.privateKey) { publicKey, privateKey in
|
|
try invokeFnReturningData {
|
|
signal_identitykeypair_serialize($0, publicKey.const(), privateKey.const())
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public var identityKey: IdentityKey {
|
|
return IdentityKey(publicKey: self.publicKey)
|
|
}
|
|
|
|
public func signAlternateIdentity(_ other: IdentityKey) -> Data {
|
|
return failOnError {
|
|
try withAllBorrowed(self.publicKey, self.privateKey, other.publicKey) { publicKey, privateKey, other in
|
|
try invokeFnReturningData {
|
|
signal_identitykeypair_sign_alternate_identity(
|
|
$0,
|
|
publicKey.const(),
|
|
privateKey.const(),
|
|
other.const()
|
|
)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|