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%
3.6 KB · 97 lines swift
Raw Blame History
1import WalletCoreC2import Foundation3// Copyright © 2017-2018 Trust.4//5// This file is part of Trust. The full Trust copyright notice, including6// terms governing use, modification, and redistribution, is contained in the7// file LICENSE at the root of the source code distribution tree.89import Foundation10import SwiftProtobuf1112public typealias SigningInput = Message13public typealias SigningOutput = Message1415/// Represents a signer to sign transactions for any blockchain.16public final class AnySigner {1718    /// Signs a transaction by SigningInput message and coin type19    ///20    /// - Parameters:21    /// - input: The generic SigningInput SwiftProtobuf message22    /// - coin: CoinType23    /// - Returns: The generic SigningOutput SwiftProtobuf message24    public static func sign<SigningOutput: Message>(input: SigningInput, coin: CoinType) -> SigningOutput {25        do {26            let outputData = nativeSign(data: try input.serializedData(), coin: coin)27            return try SigningOutput(serializedBytes: outputData)28        } catch let error {29            fatalError(error.localizedDescription)30        }31    }3233    /// Signs a transaction by serialized data of a SigningInput and coin type34    ///35    /// - Parameters:36    /// - data: The serialized data of a SigningInput37    /// - coin: CoinType38    /// - Returns: The serialized data of a SigningOutput39    public static func nativeSign(data: Data, coin: CoinType) -> Data {40        let inputData = TWDataCreateWithNSData(data)41        defer {42            TWDataDelete(inputData)43        }44        return TWDataNSData(TWAnySignerSign(inputData, TWCoinType(rawValue: coin.rawValue)))45    }4647    /// Check if AnySigner supports signing JSON representation of SigningInput for a given coin.48    public static func supportsJSON(coin: CoinType) -> Bool {49        return TWAnySignerSupportsJSON(TWCoinType(rawValue: coin.rawValue))50    }5152    /// Signs a transaction specified by the JSON representation of a SigningInput, coin type and a private key53    ///54    /// - Parameters:55    /// - json: JSON representation of a SigningInput56    /// - key: The private key data57    /// - coin: CoinType58    /// - Returns: The JSON representation of a SigningOutput.59    public static func signJSON(_ json: String, key: Data, coin: CoinType) -> String {60        let jsonString = TWStringCreateWithNSString(json)61        let keyData = TWDataCreateWithNSData(key)62        defer {63            TWDataDelete(keyData)64        }65        return TWStringNSString(TWAnySignerSignJSON(jsonString, keyData, TWCoinType(rawValue: coin.rawValue)))66    }6768    /// Plans a transaction (for UTXO chains only).69    ///70    /// - Parameters:71    /// - input: The generic SigningInput SwiftProtobuf message72    /// - coin: CoinType73    /// - Returns: TransactionPlan SwiftProtobuf message74    public static func plan<TransactionPlan: Message>(input: SigningInput, coin: CoinType) -> TransactionPlan {75        do {76            let outputData = nativePlan(data: try input.serializedData(), coin: coin)77            return try TransactionPlan(serializedBytes: outputData)78        } catch let error {79            fatalError(error.localizedDescription)80        }81    }8283    /// Plans a transaction (for UTXO chains only).84    ///85    /// - Parameters:86    /// - input: The serialized data of a SigningInput87    /// - coin: CoinType88    /// - Returns: The serialized data of a TransactionPlan89    public static func nativePlan(data: Data, coin: CoinType) -> Data {90        let inputData = TWDataCreateWithNSData(data)91        defer {92            TWDataDelete(inputData)93        }94        return TWDataNSData(TWAnySignerPlan(inputData, TWCoinType(rawValue: coin.rawValue)))95    }96}97