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.7 KB · 185 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 private key.13public final class PrivateKey {1415    /// Determines if the given private key is valid or not.16    ///17    /// - Parameter data: block of data (private key bytes)18    /// - Parameter curve: Eliptic curve of the private key19    /// - Returns: true if the private key is valid, false otherwise20    public static func isValid(data: Data, curve: Curve) -> Bool {21        let dataData = TWDataCreateWithNSData(data)22        defer {23            TWDataDelete(dataData)24        }25        return TWPrivateKeyIsValid(dataData, TWCurve(rawValue: curve.rawValue))26    }2728    /// Convert the given private key to raw-bytes block of data29    ///30    /// - Parameter pk: Non-null pointer to the private key31    /// - Returns: Non-null block of data (raw bytes) of the given private key32    public var data: Data {33        return TWDataNSData(TWPrivateKeyData(rawValue))34    }3536    let rawValue: OpaquePointer3738    init(rawValue: OpaquePointer) {39        self.rawValue = rawValue40    }4142    public init() {43        rawValue = TWPrivateKeyCreate()44    }4546    public init?(data: Data) {47        let dataData = TWDataCreateWithNSData(data)48        defer {49            TWDataDelete(dataData)50        }51        guard let rawValue = TWPrivateKeyCreateWithData(dataData) else {52            return nil53        }54        self.rawValue = rawValue55    }5657    public init?(key: PrivateKey) {58        guard let rawValue = TWPrivateKeyCreateCopy(key.rawValue) else {59            return nil60        }61        self.rawValue = rawValue62    }6364    deinit {65        TWPrivateKeyDelete(rawValue)66    }6768    /// Returns the public key associated with the given coinType and privateKey69    ///70    /// - Parameter pk: Non-null pointer to the private key71    /// - Parameter coinType: coinType of the given private key72    /// - Returns: Non-null pointer to the corresponding public key73    public func getPublicKey(coinType: CoinType) -> PublicKey {74        return PublicKey(rawValue: TWPrivateKeyGetPublicKey(rawValue, TWCoinType(rawValue: coinType.rawValue)))75    }7677    /// Returns the public key associated with the given pubkeyType and privateKey78    ///79    /// - Parameter pk: Non-null pointer to the private key80    /// - Parameter pubkeyType: pubkeyType of the given private key81    /// - Returns: Non-null pointer to the corresponding public key82    public func getPublicKeyByType(pubkeyType: PublicKeyType) -> PublicKey {83        return PublicKey(rawValue: TWPrivateKeyGetPublicKeyByType(rawValue, TWPublicKeyType(rawValue: pubkeyType.rawValue)))84    }8586    /// Returns the Secp256k1 public key associated with the given private key87    ///88    /// - Parameter pk: Non-null pointer to the private key89    /// - Parameter compressed: if the given private key is compressed or not90    /// - Returns: Non-null pointer to the corresponding public key91    public func getPublicKeySecp256k1(compressed: Bool) -> PublicKey {92        return PublicKey(rawValue: TWPrivateKeyGetPublicKeySecp256k1(rawValue, compressed))93    }9495    /// Returns the Nist256p1 public key associated with the given private key96    ///97    /// - Parameter pk: Non-null pointer to the private key98    /// - Returns: Non-null pointer to the corresponding public key99    public func getPublicKeyNist256p1() -> PublicKey {100        return PublicKey(rawValue: TWPrivateKeyGetPublicKeyNist256p1(rawValue))101    }102103    /// Returns the Ed25519 public key associated with the given private key104    ///105    /// - Parameter pk: Non-null pointer to the private key106    /// - Returns: Non-null pointer to the corresponding public key107    public func getPublicKeyEd25519() -> PublicKey {108        return PublicKey(rawValue: TWPrivateKeyGetPublicKeyEd25519(rawValue))109    }110111    /// Returns the Ed25519Blake2b public key associated with the given private key112    ///113    /// - Parameter pk: Non-null pointer to the private key114    /// - Returns: Non-null pointer to the corresponding public key115    public func getPublicKeyEd25519Blake2b() -> PublicKey {116        return PublicKey(rawValue: TWPrivateKeyGetPublicKeyEd25519Blake2b(rawValue))117    }118119    /// Returns the Ed25519Cardano public key associated with the given private key120    ///121    /// - Parameter pk: Non-null pointer to the private key122    /// - Returns: Non-null pointer to the corresponding public key123    public func getPublicKeyEd25519Cardano() -> PublicKey {124        return PublicKey(rawValue: TWPrivateKeyGetPublicKeyEd25519Cardano(rawValue))125    }126127    /// Returns the Curve25519 public key associated with the given private key128    ///129    /// - Parameter pk: Non-null pointer to the private key130    /// - Returns: Non-null pointer to the corresponding public key131    public func getPublicKeyCurve25519() -> PublicKey {132        return PublicKey(rawValue: TWPrivateKeyGetPublicKeyCurve25519(rawValue))133    }134135    /// Signs a digest using ECDSA and given curve.136    ///137    /// - Parameter pk:  Non-null pointer to a Private key138    /// - Parameter digest: Non-null digest block of data139    /// - Parameter curve: Eliptic curve140    /// - Returns: Signature as a Non-null block of data141    public func sign(digest: Data, curve: Curve) -> Data? {142        let digestData = TWDataCreateWithNSData(digest)143        defer {144            TWDataDelete(digestData)145        }146        guard let result = TWPrivateKeySign(rawValue, digestData, TWCurve(rawValue: curve.rawValue)) else {147            return nil148        }149        return TWDataNSData(result)150    }151152    /// Signs a digest using ECDSA. The result is encoded with DER.153    ///154    /// - Parameter pk:  Non-null pointer to a Private key155    /// - Parameter digest: Non-null digest block of data156    /// - Returns: Signature as a Non-null block of data157    public func signAsDER(digest: Data) -> Data? {158        let digestData = TWDataCreateWithNSData(digest)159        defer {160            TWDataDelete(digestData)161        }162        guard let result = TWPrivateKeySignAsDER(rawValue, digestData) else {163            return nil164        }165        return TWDataNSData(result)166    }167168    /// Signs a digest using ECDSA and Zilliqa schnorr signature scheme.169    ///170    /// - Parameter pk: Non-null pointer to a Private key171    /// - Parameter message: Non-null message172    /// - Returns: Signature as a Non-null block of data173    public func signZilliqaSchnorr(message: Data) -> Data? {174        let messageData = TWDataCreateWithNSData(message)175        defer {176            TWDataDelete(messageData)177        }178        guard let result = TWPrivateKeySignZilliqaSchnorr(rawValue, messageData) else {179            return nil180        }181        return TWDataNSData(result)182    }183184}185