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%
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/// Bitcoin script manipulating functions13public final class BitcoinScript {1415 /// Determines whether 2 scripts have the same content16 ///17 /// - Parameter lhs: Non-null pointer to the first script18 /// - Parameter rhs: Non-null pointer to the second script19 /// - Returns: true if both script have the same content20 public static func == (lhs: BitcoinScript, rhs: BitcoinScript) -> Bool {21 return TWBitcoinScriptEqual(lhs.rawValue, rhs.rawValue)22 }2324 /// Builds a standard 'pay to public key' script.25 ///26 /// - Parameter pubkey: Non-null pointer to a pubkey27 /// - Note: Must be deleted with \TWBitcoinScriptDelete28 /// - Returns: A pointer to the built script29 public static func buildPayToPublicKey(pubkey: Data) -> BitcoinScript {30 let pubkeyData = TWDataCreateWithNSData(pubkey)31 defer {32 TWDataDelete(pubkeyData)33 }34 return BitcoinScript(rawValue: TWBitcoinScriptBuildPayToPublicKey(pubkeyData))35 }3637 /// Builds a standard 'pay to public key hash' script.38 ///39 /// - Parameter hash: Non-null pointer to a PublicKey hash40 /// - Note: Must be deleted with \TWBitcoinScriptDelete41 /// - Returns: A pointer to the built script42 public static func buildPayToPublicKeyHash(hash: Data) -> BitcoinScript {43 let hashData = TWDataCreateWithNSData(hash)44 defer {45 TWDataDelete(hashData)46 }47 return BitcoinScript(rawValue: TWBitcoinScriptBuildPayToPublicKeyHash(hashData))48 }4950 /// Builds a standard 'pay to script hash' script.51 ///52 /// - Parameter scriptHash: Non-null pointer to a script hash53 /// - Note: Must be deleted with \TWBitcoinScriptDelete54 /// - Returns: A pointer to the built script55 public static func buildPayToScriptHash(scriptHash: Data) -> BitcoinScript {56 let scriptHashData = TWDataCreateWithNSData(scriptHash)57 defer {58 TWDataDelete(scriptHashData)59 }60 return BitcoinScript(rawValue: TWBitcoinScriptBuildPayToScriptHash(scriptHashData))61 }6263 /// Builds a pay-to-witness-public-key-hash (P2WPKH) script..64 ///65 /// - Parameter hash: Non-null pointer to a witness public key hash66 /// - Note: Must be deleted with \TWBitcoinScriptDelete67 /// - Returns: A pointer to the built script68 public static func buildPayToWitnessPubkeyHash(hash: Data) -> BitcoinScript {69 let hashData = TWDataCreateWithNSData(hash)70 defer {71 TWDataDelete(hashData)72 }73 return BitcoinScript(rawValue: TWBitcoinScriptBuildPayToWitnessPubkeyHash(hashData))74 }7576 /// Builds a pay-to-witness-script-hash (P2WSH) script.77 ///78 /// - Parameter scriptHash: Non-null pointer to a script hash79 /// - Note: Must be deleted with \TWBitcoinScriptDelete80 /// - Returns: A pointer to the built script81 public static func buildPayToWitnessScriptHash(scriptHash: Data) -> BitcoinScript {82 let scriptHashData = TWDataCreateWithNSData(scriptHash)83 defer {84 TWDataDelete(scriptHashData)85 }86 return BitcoinScript(rawValue: TWBitcoinScriptBuildPayToWitnessScriptHash(scriptHashData))87 }8889 /// Builds a appropriate lock script for the given address..90 ///91 /// - Parameter address: Non-null pointer to an address92 /// - Parameter coin: coin type93 /// - Note: Must be deleted with \TWBitcoinScriptDelete94 /// - Returns: A pointer to the built script95 public static func lockScriptForAddress(address: String, coin: CoinType) -> BitcoinScript {96 let addressString = TWStringCreateWithNSString(address)97 defer {98 TWStringDelete(addressString)99 }100 return BitcoinScript(rawValue: TWBitcoinScriptLockScriptForAddress(addressString, TWCoinType(rawValue: coin.rawValue)))101 }102103 /// Builds a appropriate lock script for the given address with replay.104 public static func lockScriptForAddressReplay(address: String, coin: CoinType, blockHash: Data, blockHeight: Int64) -> BitcoinScript {105 let addressString = TWStringCreateWithNSString(address)106 defer {107 TWStringDelete(addressString)108 }109 let blockHashData = TWDataCreateWithNSData(blockHash)110 defer {111 TWDataDelete(blockHashData)112 }113 return BitcoinScript(rawValue: TWBitcoinScriptLockScriptForAddressReplay(addressString, TWCoinType(rawValue: coin.rawValue), blockHashData, blockHeight))114 }115116 /// Return the default HashType for the given coin, such as TWBitcoinSigHashTypeAll.117 ///118 /// - Parameter coinType: coin type119 /// - Returns: default HashType for the given coin120 public static func hashTypeForCoin(coinType: CoinType) -> UInt32 {121 return TWBitcoinScriptHashTypeForCoin(TWCoinType(rawValue: coinType.rawValue))122 }123124 /// Get size of a script125 ///126 /// - Parameter script: Non-null pointer to a script127 /// - Returns: size of the script128 public var size: Int {129 return TWBitcoinScriptSize(rawValue)130 }131132 /// Get data of a script133 ///134 /// - Parameter script: Non-null pointer to a script135 /// - Returns: data of the given script136 public var data: Data {137 return TWDataNSData(TWBitcoinScriptData(rawValue))138 }139140 /// Return script hash of a script141 ///142 /// - Parameter script: Non-null pointer to a script143 /// - Returns: script hash of the given script144 public var scriptHash: Data {145 return TWDataNSData(TWBitcoinScriptScriptHash(rawValue))146 }147148 /// Determines whether this is a pay-to-script-hash (P2SH) script.149 ///150 /// - Parameter script: Non-null pointer to a script151 /// - Returns: true if this is a pay-to-script-hash (P2SH) script, false otherwise152 public var isPayToScriptHash: Bool {153 return TWBitcoinScriptIsPayToScriptHash(rawValue)154 }155156 /// Determines whether this is a pay-to-witness-script-hash (P2WSH) script.157 ///158 /// - Parameter script: Non-null pointer to a script159 /// - Returns: true if this is a pay-to-witness-script-hash (P2WSH) script, false otherwise160 public var isPayToWitnessScriptHash: Bool {161 return TWBitcoinScriptIsPayToWitnessScriptHash(rawValue)162 }163164 /// Determines whether this is a pay-to-witness-public-key-hash (P2WPKH) script.165 ///166 /// - Parameter script: Non-null pointer to a script167 /// - Returns: true if this is a pay-to-witness-public-key-hash (P2WPKH) script, false otherwise168 public var isPayToWitnessPublicKeyHash: Bool {169 return TWBitcoinScriptIsPayToWitnessPublicKeyHash(rawValue)170 }171172 /// Determines whether this is a witness program script.173 ///174 /// - Parameter script: Non-null pointer to a script175 /// - Returns: true if this is a witness program script, false otherwise176 public var isWitnessProgram: Bool {177 return TWBitcoinScriptIsWitnessProgram(rawValue)178 }179180 let rawValue: OpaquePointer181182 init(rawValue: OpaquePointer) {183 self.rawValue = rawValue184 }185186 public init() {187 rawValue = TWBitcoinScriptCreate()188 }189190 public init(data: Data) {191 let dataData = TWDataCreateWithNSData(data)192 defer {193 TWDataDelete(dataData)194 }195 rawValue = TWBitcoinScriptCreateWithData(dataData)196 }197198 public init(script: BitcoinScript) {199 rawValue = TWBitcoinScriptCreateCopy(script.rawValue)200 }201202 deinit {203 TWBitcoinScriptDelete(rawValue)204 }205206 /// Matches the script to a pay-to-public-key (P2PK) script.207 ///208 /// - Parameter script: Non-null pointer to a script209 /// - Returns: The public key.210 public func matchPayToPubkey() -> Data? {211 guard let result = TWBitcoinScriptMatchPayToPubkey(rawValue) else {212 return nil213 }214 return TWDataNSData(result)215 }216217 /// Matches the script to a pay-to-public-key-hash (P2PKH).218 ///219 /// - Parameter script: Non-null pointer to a script220 /// - Returns: the key hash.221 public func matchPayToPubkeyHash() -> Data? {222 guard let result = TWBitcoinScriptMatchPayToPubkeyHash(rawValue) else {223 return nil224 }225 return TWDataNSData(result)226 }227228 /// Matches the script to a pay-to-script-hash (P2SH).229 ///230 /// - Parameter script: Non-null pointer to a script231 /// - Returns: the script hash.232 public func matchPayToScriptHash() -> Data? {233 guard let result = TWBitcoinScriptMatchPayToScriptHash(rawValue) else {234 return nil235 }236 return TWDataNSData(result)237 }238239 /// Matches the script to a pay-to-witness-public-key-hash (P2WPKH).240 ///241 /// - Parameter script: Non-null pointer to a script242 /// - Returns: the key hash.243 public func matchPayToWitnessPublicKeyHash() -> Data? {244 guard let result = TWBitcoinScriptMatchPayToWitnessPublicKeyHash(rawValue) else {245 return nil246 }247 return TWDataNSData(result)248 }249250 /// Matches the script to a pay-to-witness-script-hash (P2WSH).251 ///252 /// - Parameter script: Non-null pointer to a script253 /// - Returns: the script hash, a SHA256 of the witness script..254 public func matchPayToWitnessScriptHash() -> Data? {255 guard let result = TWBitcoinScriptMatchPayToWitnessScriptHash(rawValue) else {256 return nil257 }258 return TWDataNSData(result)259 }260261 /// Encodes the script.262 ///263 /// - Parameter script: Non-null pointer to a script264 /// - Returns: The encoded script265 public func encode() -> Data {266 return TWDataNSData(TWBitcoinScriptEncode(rawValue))267 }268269}270