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%
4.9 KB · 109 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/// Ethereum message signing and verification.13///14/// Ethereum and some other wallets support a message signing & verification format, to create a proof (a signature)15/// that someone has access to the private keys of a specific address.16public struct EthereumMessageSigner {1718    /// Sign a typed message EIP-712 V4.19    ///20    /// - Parameter privateKey:: the private key used for signing21    /// - Parameter messageJson:: A custom typed data message in json22    /// - Returns:s the signature, Hex-encoded. On invalid input empty string is returned. Returned object needs to be deleted after use.23    public static func signTypedMessage(privateKey: PrivateKey, messageJson: String) -> String {24        let messageJsonString = TWStringCreateWithNSString(messageJson)25        defer {26            TWStringDelete(messageJsonString)27        }28        return TWStringNSString(TWEthereumMessageSignerSignTypedMessage(privateKey.rawValue, messageJsonString))29    }3031    /// Sign a typed message EIP-712 V4 with EIP-155 replay attack protection.32    ///33    /// - Parameter privateKey:: the private key used for signing34    /// - Parameter messageJson:: A custom typed data message in json35    /// - Parameter chainId:: chainId for eip-155 protection36    /// - Returns:s the signature, Hex-encoded. On invalid input empty string is returned or invalid chainId error message. Returned object needs to be deleted after use.37    public static func signTypedMessageEip155(privateKey: PrivateKey, messageJson: String, chainId: Int32) -> String {38        let messageJsonString = TWStringCreateWithNSString(messageJson)39        defer {40            TWStringDelete(messageJsonString)41        }42        return TWStringNSString(TWEthereumMessageSignerSignTypedMessageEip155(privateKey.rawValue, messageJsonString, Int32(chainId)))43    }4445    /// Sign a message.46    ///47    /// - Parameter privateKey:: the private key used for signing48    /// - Parameter message:: A custom message which is input to the signing.49    /// - Returns:s the signature, Hex-encoded. On invalid input empty string is returned. Returned object needs to be deleted after use.50    public static func signMessage(privateKey: PrivateKey, message: String) -> String {51        let messageString = TWStringCreateWithNSString(message)52        defer {53            TWStringDelete(messageString)54        }55        return TWStringNSString(TWEthereumMessageSignerSignMessage(privateKey.rawValue, messageString))56    }5758    /// Sign a message with Immutable X msg type.59    ///60    /// - Parameter privateKey:: the private key used for signing61    /// - Parameter message:: A custom message which is input to the signing.62    /// - Returns:s the signature, Hex-encoded. On invalid input empty string is returned. Returned object needs to be deleted after use.63    public static func signMessageImmutableX(privateKey: PrivateKey, message: String) -> String {64        let messageString = TWStringCreateWithNSString(message)65        defer {66            TWStringDelete(messageString)67        }68        return TWStringNSString(TWEthereumMessageSignerSignMessageImmutableX(privateKey.rawValue, messageString))69    }7071    /// Sign a message with Eip-155 msg type.72    ///73    /// - Parameter privateKey:: the private key used for signing74    /// - Parameter message:: A custom message which is input to the signing.75    /// - Parameter chainId:: chainId for eip-155 protection76    /// - Returns:s the signature, Hex-encoded. On invalid input empty string is returned. Returned object needs to be deleted after use.77    public static func signMessageEip155(privateKey: PrivateKey, message: String, chainId: Int32) -> String {78        let messageString = TWStringCreateWithNSString(message)79        defer {80            TWStringDelete(messageString)81        }82        return TWStringNSString(TWEthereumMessageSignerSignMessageEip155(privateKey.rawValue, messageString, Int32(chainId)))83    }8485    /// Verify signature for a message.86    ///87    /// - Parameter pubKey:: pubKey that will verify and recover the message from the signature88    /// - Parameter message:: the message signed (without prefix)89    /// - Parameter signature:: in Hex-encoded form.90    /// - Returns:s false on any invalid input (does not throw), true if the message can be recovered from the signature91    public static func verifyMessage(pubKey: PublicKey, message: String, signature: String) -> Bool {92        let messageString = TWStringCreateWithNSString(message)93        defer {94            TWStringDelete(messageString)95        }96        let signatureString = TWStringCreateWithNSString(signature)97        defer {98            TWStringDelete(signatureString)99        }100        return TWEthereumMessageSignerVerifyMessage(pubKey.rawValue, messageString, signatureString)101    }102103104    init() {105    }106107108}109