Functions

Descriptions of NEOPIN Connect functions

The following information is deprecated and may limit its use. Please refer to the WalletConnect 2.0 tab for the latest version of NEOPIN wallet integration: WalletConnect 2.0

TypeDescriptions

Method for connecting with the NEOPIN Wallet, and the URL is returned when the connection is made.

ex) ConnectManager.shared.connect()

Method for disconnecting from the NEOPIN Wallet. Disconnect is recommended in the following cases.

  • When the app is terminated or forcibly terminated. (AppDelegate.applicationWillTerminate)

  • When the transaction with NEOPIN Wallet is finished.

  • When a certain time passes after connecting to NEOPIN Wallet.

ex) ConnectManager.shared.disconnect()

Method for obtaining the address of the NEOPIN Wallet.

Addresses can be imported only when Wallet is connected.

ex) ConnectManager.shared.getMyAccount()

Among the methods for communicating with the NEOPIN Wallet, this is the most important method. After sending a Transaction to the wallet, you must give a response value from the Wallet to receive it as a Response.

  • When creating a Transaction in this method, be sure to check the variable values (from, to, data, gas, gasPrice, value, nonce, type, accessList, chainId, maxPriorityFeePerGas, maxFeePerGas).

  • When creating a Client.Transaction object for SendTransaction, specify the ChainID of the target network. (*Required)

  • Data, one of the values of Transaction, must be entered as encodedData that complies with ERC-20 “Transfer”. Please refer to the example.

Signs a transaction that can be submitted to the network

The sign method calculates an Ethereum specific signature with:sign(keccack256("\x19Ethereum Signed Message:\n" + len(message) + message))).

Connect

Method for connecting with the NEOPIN Wallet, and the URL is returned when the connection is made.

The code block below is a code that requests connection to the NEOPIN Wallet through ConnectManager and opens the NEOPIN Wallet through the obtained URL.

You must request a connection and approve it in the NEOPIN Wallet to connect to the Wallet.

guard let connectionURL = ConnectManager.shared.connect() else {
        print("Connection Fail")
        return
}
        
let deepLinkURL = "examplewallet\(connectionURL)"
guard let url = URL(string: deepLinkURL) else { return }
UIApplication.shared.open(url, options: [:], completionHandler: nil)

Close

Method for disconnecting from the NEOPIN Wallet.

Disconnect is recommended in the following cases.

  • When the app is terminated (AppDelegate.applicationWillTerminate)

  • When the transaction with NEOPIN Wallet is finished

  • When connected to NEOPIN Wallet, but a certain amount of time has elapsed

func disconnect(){
    guard let session = self.session else { return }
    do {
        try self.client?.disconnect(from: session)
    } catch {
        print("Current Session is Nil: \(error)")
    }
}

ex) ConnectManager.shared.disconnect()

GetAccount

Method for obtaining the address of the NEOPIN Wallet.

Addresses can be imported only when Wallet is connected.

func getMyAccount(){
    guard let session = self.session,
      let from = session.walletInfo?.accounts.first else { return }
    
    print("getMyAccount: \(from)")
}

ex) ConnectManager.shared.getMyAccount()

SendTransaction

Method for communicating with the NEOPIN Wallet.

The following procedure is required to use NEOPIN Connect_sendTransaction in DApp.

  • Create Transaction encodedData compliant with ERC-20.

    • Data, one of the values of Transaction, must be entered as encodedData that complies with ERC-20 “Transfer”. Please refer to the example.

    struct ABIHelper {
        static func encode(
            param: [AnyObject]?,
            function: ABI.Element.Function?
        ) -> String? {
            guard let param = param else { return nil }
            guard let function = function else { return nil }
            let object = ABI.Element.function(function)
            guard let encodedData = object.encodeParameters(param) else { return nil }
            return "0x" + encodedData.toHexString()
        }

    • Create Transaction by filling in the remaining values.

      • When creating a Transaction in this method, be sure to check the variable values. In this project, an arbitrary value is set. (from, to, data, gas, gasPrice, value, nonce, type, accessList, chainId, maxPriorityFeePerGas, maxFeePerGas).

    static func transaction(
        from address: String,
        to: String
    ) -> Client.Transaction? {
        // MARK: - transferFucntion(address, value)
        guard let amount = Web3.Utils.parseToBigUInt("10000", units: .eth) else { return nil }
        guard let data = ABIHelper.encode(
            param: [
                to as AnyObject,
                amount as AnyObject
            ],
            function: ABIHelper.getTransferFunction()
        ) else { return nil }
        
        return PodsNeopinConnect.Client.Transaction(
            from: address,
            to: to,
            data: data,
            gas: "0x76c0",
            gasPrice: "0x",
            value: "0x",
            nonce: "0x",  //Update to the latest nonce from the NEOPIN Wallet.
            type: nil,
            accessList: nil,
            chainId: nil,
            maxPriorityFeePerGas: nil,
            maxFeePerGas: nil
        )
    }

    • Transfer to the NEOPIN Wallet.

      • Please create a Transaction object after checking Address(From, To).

      • After sending the Transaction to the Wallet, you can receive Response only when the wallet gives a response value.

    func requestSendTransaction(){
        guard let session = self.session,
              let from = session.walletInfo?.accounts.first else { return }
    
        guard let transaction = Stub.transaction(
            from: from,
            to: "0xb093add5a8ad3e997ccbde6d12dfb2e0c2befbb7"
        ) else { return }
            
        do {
            try self.client?.eth_sendTransaction(
                url: session.url,
                transaction: transaction,
                completion: { [weak self] response in
                    guard let self = self else { return }
                    do {
                        if let error = response.error {
                            let log = """
                            function: \(#function)
                            error: \(error)
                            """
                            self.appendLog(log: log)
                            return
                        }
                            
                        let result = try response.result(as: String.self)
                        print("requestSendTransaction: \(result)")
    
                        ABIHelper.decodedABIInfo(data: result)
                           let log = """
                           function: \(#function)
                           result: \(result)
                        """
                        self.appendLog(log: log)
                       } catch {
                        print("\(#function) error: \(error)")
                        let log = """
                           function: \(#function)
                           response.result error: \(response.error?.localizedDescription ?? "")
                           """
                           self.appendLog(log: log)
                       }
                   }
               )
       } catch {
            let log = """
            function: \(#function)
            eth_sendTransaction error: \(error)
            """
            self.appendLog(log: log)
        }
    }

SignTransaction

  • Transfer to the NEOPIN Wallet.

    • Please create a Transaction object after checking Address(From, To).

    • After sending the Transaction to the Wallet, you can receive Response only when the wallet gives a response value.

  • Signs a transaction that can be submitted to the network

// MARK: - requestSignTransaction
    func requestSignTransaction(chain: Chain) {
        guard let session = self.session,
              let from = session.walletInfo?.accounts.first else { return }
        
        guard let transaction = Stub.transaction(
            from: from,
            to: "0xb093add5a8ad3e997ccbde6d12dfb2e0c2befbb7",
            chain: chain
        ) else { return }
        
        do {
            try self.client?.eth_signTransaction(
                url: session.url,
                transaction: transaction,
                completion: { [weak self] response in
                    guard let self = self else { return }
                    do {
                        if let error = response.error {
                            let log = """
                            function: \(#function)
                            error: \(error)
                            """
                            self.appendLog(log: log)
                            return
                        }
                        
                        let result = try response.result(as: String.self)
                        print("requestSignTransaction: \(result)")

                        ABIHelper.decodedABIInfo(data: result)
                        let log = """
                        function: \(#function)
                        result: \(result)
                        """
                        self.appendLog(log: log)
                    } catch {
                        print("\(#function) error: \(error)")
                        let log = """
                        function: \(#function)
                        response.result error: \(response.error?.localizedDescription ?? "")
                        """
                        self.appendLog(log: log)
                    }
                }
            )
        } catch {
            let log = """
            function: \(#function)
            eth_signTransaction error: \(error)
            """
            self.appendLog(log: log)
        }
    }

PersonalSign

  • The sign method calculates an Ethereum specific signature with:sign(keccack256("\\x19Ethereum Signed Message:\\n" + len(message) + message))).

  • By adding a prefix to the message makes the calculated signature recognisable as an Ethereum specific signature. This prevents misuse where a malicious DApp can sign arbitrary data (e.g. transaction) and use the signature to impersonate the victim.

  • Note See ecRecover to verify the signature.

func requestPersonalSign() {
        guard let session = self.session,
              let from = session.walletInfo?.accounts.first else { return }
        guard let url = ConnectManager.shared.session?.url else { return }
        
        do {
            try self.client?.personal_sign(
                url: url,
                message: "Test",
                account: from,
                completion: { [weak self] response in
                    guard let self = self else { return }
                    do {
                        if let error = response.error {
                            let log = """
                            function: \(#function)
                            error: \(error)
                            """
                            self.appendLog(log: log)
                            return
                        }
                        
                        let result = try response.result(as: String.self)
                        print("requestSendTransaction: \(result)")

                        ABIHelper.decodedABIInfo(data: result)
                        let log = """
                        function: \(#function)
                        result: \(result)
                        """
                        self.appendLog(log: log)
                    } catch {
                        print("\(#function) error: \(error)")
                        let log = """
                        function: \(#function)
                        response.result error: \(response.error?.localizedDescription ?? "")
                        """
                        self.appendLog(log: log)
                    }
                }
            )
        } catch {
            print("\(#function) error: \(error)")
            let log = """
            function: \(#function)
            response.result error: \(error.localizedDescription )
            """
            self.appendLog(log: log)
        }
    }

Last updated