Files
libsignal/swift/Sources/LibSignalClient/DeviceTransfer.swift
Jordan Rose f8f821e9ea swiftlint: Check for public structs without explicit inits
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.
2026-01-29 15:14:53 -08:00

51 lines
1.4 KiB
Swift

//
// Copyright 2021-2022 Signal Messenger, LLC.
// SPDX-License-Identifier: AGPL-3.0-only
//
import Foundation
import SignalFfi
public enum KeyFormat: UInt8, CaseIterable, Sendable {
// PKCS#8 is the default for backward compatibility
case pkcs8 = 0
case keySpecific = 1
}
public struct DeviceTransferKey: Sendable {
public let privateKey: Data
private init(privateKey: Data) {
self.privateKey = privateKey
}
public static func generate(formattedAs keyFormat: KeyFormat = .pkcs8) -> Self {
let privateKey = failOnError {
try invokeFnReturningData {
signal_device_transfer_generate_private_key_with_format($0, keyFormat.rawValue)
}
}
return Self(privateKey: privateKey)
}
public func privateKeyMaterial() -> Data {
return self.privateKey
}
public func generateCertificate(_ name: String, _ daysTilExpire: Int) -> Data {
return self.privateKey.withUnsafeBorrowedBuffer { privateKeyBuffer in
failOnError {
try invokeFnReturningData {
signal_device_transfer_generate_certificate(
$0,
privateKeyBuffer,
name,
UInt32(daysTilExpire)
)
}
}
}
}
}