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.8 KB · 175 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/// Wrapper class for Ethereum ABI encoding & decoding.13public struct EthereumAbi {1415    /// Decode a contract call (function input) according to an ABI json.16    ///17    /// - Parameter coin: EVM-compatible coin type.18    /// - Parameter input: The serialized data of `TW.EthereumAbi.Proto.ContractCallDecodingInput`.19    /// - Returns: The serialized data of a `TW.EthereumAbi.Proto.ContractCallDecodingOutput` proto object.20    public static func decodeContractCall(coin: CoinType, input: Data) -> Data {21        let inputData = TWDataCreateWithNSData(input)22        defer {23            TWDataDelete(inputData)24        }25        return TWDataNSData(TWEthereumAbiDecodeContractCall(TWCoinType(rawValue: coin.rawValue), inputData))26    }2728    /// Decode a function input or output data according to a given ABI.29    ///30    /// - Parameter coin: EVM-compatible coin type.31    /// - Parameter input: The serialized data of `TW.EthereumAbi.Proto.ParamsDecodingInput`.32    /// - Returns: The serialized data of a `TW.EthereumAbi.Proto.ParamsDecodingOutput` proto object.33    public static func decodeParams(coin: CoinType, input: Data) -> Data {34        let inputData = TWDataCreateWithNSData(input)35        defer {36            TWDataDelete(inputData)37        }38        return TWDataNSData(TWEthereumAbiDecodeParams(TWCoinType(rawValue: coin.rawValue), inputData))39    }4041    ///     /// Decodes an Eth ABI value according to a given type.42    ///43    /// - Parameter coin: EVM-compatible coin type.44    /// - Parameter input: The serialized data of `TW.EthereumAbi.Proto.ValueDecodingInput`.45    /// - Returns: The serialized data of a `TW.EthereumAbi.Proto.ValueDecodingOutput` proto object.46    public static func decodeValue(coin: CoinType, input: Data) -> Data {47        let inputData = TWDataCreateWithNSData(input)48        defer {49            TWDataDelete(inputData)50        }51        return TWDataNSData(TWEthereumAbiDecodeValue(TWCoinType(rawValue: coin.rawValue), inputData))52    }5354    /// Encode function to Eth ABI binary.55    ///56    /// - Parameter coin: EVM-compatible coin type.57    /// - Parameter input: The serialized data of `TW.EthereumAbi.Proto.FunctionEncodingInput`.58    /// - Returns: The serialized data of a `TW.EthereumAbi.Proto.FunctionEncodingOutput` proto object.59    public static func encodeFunction(coin: CoinType, input: Data) -> Data {60        let inputData = TWDataCreateWithNSData(input)61        defer {62            TWDataDelete(inputData)63        }64        return TWDataNSData(TWEthereumAbiEncodeFunction(TWCoinType(rawValue: coin.rawValue), inputData))65    }6667    /// Encode function to Eth ABI binary68    ///69    /// - Parameter fn: Non-null Eth abi function70    /// - Returns: Non-null encoded block of data71    public static func encode(fn: EthereumAbiFunction) -> Data {72        return TWDataNSData(TWEthereumAbiEncode(fn.rawValue))73    }7475    /// Decode function output from Eth ABI binary, fill output parameters76    ///77    /// \param[in] fn Non-null Eth abi function78    /// \param[out] encoded Non-null block of data79    /// - Returns: true if encoded have been filled correctly, false otherwise80    public static func decodeOutput(fn: EthereumAbiFunction, encoded: Data) -> Bool {81        let encodedData = TWDataCreateWithNSData(encoded)82        defer {83            TWDataDelete(encodedData)84        }85        return TWEthereumAbiDecodeOutput(fn.rawValue, encodedData)86    }8788    /// Decode function call data to human readable json format, according to input abi json89    ///90    /// - Parameter data: Non-null block of data91    /// - Parameter abi: Non-null string92    /// - Returns: Non-null json string function call data93    public static func decodeCall(data: Data, abi: String) -> String? {94        let dataData = TWDataCreateWithNSData(data)95        defer {96            TWDataDelete(dataData)97        }98        let abiString = TWStringCreateWithNSString(abi)99        defer {100            TWStringDelete(abiString)101        }102        guard let result = TWEthereumAbiDecodeCall(dataData, abiString) else {103            return nil104        }105        return TWStringNSString(result)106    }107108    /// Compute the hash of a struct, used for signing, according to EIP712 ("v4").109    /// Input is a Json object (as string), with following fields:110    /// - types: map of used struct types (see makeTypes())111    /// - primaryType: the type of the message (string)112    /// - domain: EIP712 domain specifier values113    /// - message: the message (object).114    /// Throws on error.115    /// Example input:116    ///  R"({117    ///     "types": {118    ///         "EIP712Domain": [119    ///             {"name": "name", "type": "string"},120    ///             {"name": "version", "type": "string"},121    ///             {"name": "chainId", "type": "uint256"},122    ///             {"name": "verifyingContract", "type": "address"}123    ///         ],124    ///         "Person": [125    ///             {"name": "name", "type": "string"},126    ///             {"name": "wallet", "type": "address"}127    ///         ]128    ///     },129    ///     "primaryType": "Person",130    ///     "domain": {131    ///         "name": "Ether Person",132    ///         "version": "1",133    ///         "chainId": 1,134    ///         "verifyingContract": "0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC"135    ///     },136    ///     "message": {137    ///         "name": "Cow",138    ///         "wallet": "CD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826"139    ///     }140    ///  })");141    /// On error, empty Data is returned.142    /// Returned data must be deleted (hint: use WRAPD() macro).143    ///144    /// - Parameter messageJson: Non-null json abi input145    /// - Returns: Non-null block of data, encoded abi input146    public static func encodeTyped(messageJson: String) -> Data {147        let messageJsonString = TWStringCreateWithNSString(messageJson)148        defer {149            TWStringDelete(messageJsonString)150        }151        return TWDataNSData(TWEthereumAbiEncodeTyped(messageJsonString))152    }153154    /// Get function signature from Ethereum ABI json155    ///156    /// - Parameter abi: The function ABI json string, for example: {"inputs":[{"internalType":"bool","name":"arg1","type":"bool"}],"name":"fun1","outputs":[],"stateMutability":"nonpayable","type":"function"}157    /// - Returns: the function type signature, of the form "baz(int32,uint256)", null if the abi is invalid.158    public static func getFunctionSignature(abi: String) -> String? {159        let abiString = TWStringCreateWithNSString(abi)160        defer {161            TWStringDelete(abiString)162        }163        guard let result = TWEthereumAbiGetFunctionSignature(abiString) else {164            return nil165        }166        return TWStringNSString(result)167    }168169170    init() {171    }172173174}175