SPB Git

spb/os-vault Public

Self-custody, multi-chain crypto wallet for macOS. One recovery phrase, six chain families, zero API keys — nothing leaves your Mac.

Swift 96% Shell 3.4% Makefile 0.6%
6.6 KB · 173 lines swift
Raw Blame History
1import WalletCoreC2import Foundation3// SPDX-License-Identifier: Apache-2.04//5// Copyright © 2017 Trust Wallet.6//7// This is a GENERATED FILE, changes made here WILL BE LOST.8//910import Foundation1112/// Represents a public key.13public final class PublicKey {1415    /// Determines if the given public key is valid or not16    ///17    /// - Parameter data: Non-null block of data representing the public key18    /// - Parameter type: type of the public key19    /// - Returns: true if the block of data is a valid public key, false otherwise20    public static func isValid(data: Data, type: PublicKeyType) -> Bool {21        let dataData = TWDataCreateWithNSData(data)22        defer {23            TWDataDelete(dataData)24        }25        return TWPublicKeyIsValid(dataData, TWPublicKeyType(rawValue: type.rawValue))26    }2728    /// Try to get a public key from a given signature and a message29    ///30    /// - Parameter signature: Non-null pointer to a block of data corresponding to the signature31    /// - Parameter message: Non-null pointer to a block of data corresponding to the message32    /// - Returns: Null pointer if the public key can't be recover from the given signature and message,33    /// pointer to the public key otherwise34    public static func recover(signature: Data, message: Data) -> PublicKey? {35        let signatureData = TWDataCreateWithNSData(signature)36        defer {37            TWDataDelete(signatureData)38        }39        let messageData = TWDataCreateWithNSData(message)40        defer {41            TWDataDelete(messageData)42        }43        guard let value = TWPublicKeyRecover(signatureData, messageData) else {44            return nil45        }46        return PublicKey(rawValue: value)47    }4849    /// Determines if the given public key is compressed or not50    ///51    /// - Parameter pk: Non-null pointer to a public key52    /// - Returns: true if the public key is compressed, false otherwise53    public var isCompressed: Bool {54        return TWPublicKeyIsCompressed(rawValue)55    }5657    /// Give the compressed public key of the given non-compressed public key58    ///59    /// - Parameter from: Non-null pointer to a non-compressed public key60    /// - Returns: Non-null pointer to the corresponding compressed public-key61    public var compressed: PublicKey {62        return PublicKey(rawValue: TWPublicKeyCompressed(rawValue))63    }6465    /// Give the non-compressed public key of a corresponding compressed public key66    ///67    /// - Parameter from: Non-null pointer to the corresponding compressed public key68    /// - Returns: Non-null pointer to the corresponding non-compressed public key69    public var uncompressed: PublicKey {70        return PublicKey(rawValue: TWPublicKeyUncompressed(rawValue))71    }7273    /// Gives the raw data of a given public-key74    ///75    /// - Parameter pk: Non-null pointer to a public key76    /// - Returns: Non-null pointer to the raw block of data of the given public key77    public var data: Data {78        return TWDataNSData(TWPublicKeyData(rawValue))79    }8081    /// Give the public key type (eliptic) of a given public key82    ///83    /// - Parameter publicKey: Non-null pointer to a public key84    /// - Returns: The public key type of the given public key (eliptic)85    public var keyType: PublicKeyType {86        return PublicKeyType(rawValue: TWPublicKeyKeyType(rawValue).rawValue)!87    }8889    /// Get the public key description from a given public key90    ///91    /// - Parameter publicKey: Non-null pointer to a public key92    /// - Returns: Non-null pointer to a string representing the description of the public key93    public var description: String {94        return TWStringNSString(TWPublicKeyDescription(rawValue))95    }9697    let rawValue: OpaquePointer9899    init(rawValue: OpaquePointer) {100        self.rawValue = rawValue101    }102103    public init?(data: Data, type: PublicKeyType) {104        let dataData = TWDataCreateWithNSData(data)105        defer {106            TWDataDelete(dataData)107        }108        guard let rawValue = TWPublicKeyCreateWithData(dataData, TWPublicKeyType(rawValue: type.rawValue)) else {109            return nil110        }111        self.rawValue = rawValue112    }113114    deinit {115        TWPublicKeyDelete(rawValue)116    }117118    /// Verify the validity of a signature and a message using the given public key119    ///120    /// - Parameter pk: Non-null pointer to a public key121    /// - Parameter signature: Non-null pointer to a block of data corresponding to the signature122    /// - Parameter message: Non-null pointer to a block of data corresponding to the message123    /// - Returns: true if the signature and the message belongs to the given public key, false otherwise124    public func verify(signature: Data, message: Data) -> Bool {125        let signatureData = TWDataCreateWithNSData(signature)126        defer {127            TWDataDelete(signatureData)128        }129        let messageData = TWDataCreateWithNSData(message)130        defer {131            TWDataDelete(messageData)132        }133        return TWPublicKeyVerify(rawValue, signatureData, messageData)134    }135136    /// Verify the validity as DER of a signature and a message using the given public key137    ///138    /// - Parameter pk: Non-null pointer to a public key139    /// - Parameter signature: Non-null pointer to a block of data corresponding to the signature140    /// - Parameter message: Non-null pointer to a block of data corresponding to the message141    /// - Returns: true if the signature and the message belongs to the given public key, false otherwise142    public func verifyAsDER(signature: Data, message: Data) -> Bool {143        let signatureData = TWDataCreateWithNSData(signature)144        defer {145            TWDataDelete(signatureData)146        }147        let messageData = TWDataCreateWithNSData(message)148        defer {149            TWDataDelete(messageData)150        }151        return TWPublicKeyVerifyAsDER(rawValue, signatureData, messageData)152    }153154    /// Verify a Zilliqa schnorr signature with a signature and message.155    ///156    /// - Parameter pk: Non-null pointer to a public key157    /// - Parameter signature: Non-null pointer to a block of data corresponding to the signature158    /// - Parameter message: Non-null pointer to a block of data corresponding to the message159    /// - Returns: true if the signature and the message belongs to the given public key, false otherwise160    public func verifyZilliqaSchnorr(signature: Data, message: Data) -> Bool {161        let signatureData = TWDataCreateWithNSData(signature)162        defer {163            TWDataDelete(signatureData)164        }165        let messageData = TWDataCreateWithNSData(message)166        defer {167            TWDataDelete(messageData)168        }169        return TWPublicKeyVerifyZilliqaSchnorr(rawValue, signatureData, messageData)170    }171172}173