# Functions

{% hint style="danger" %}
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](https://docs.neopin.io/developers/walletconnect-2.0 "mention")
{% endhint %}

<table><thead><tr><th width="180">Type</th><th width="568">Descriptions</th></tr></thead><tbody><tr><td><a href="#connect">Connect</a></td><td><ul><li>setClient(wcSession: <a href="broken-reference">WCSession</a>, peerMeta: <a href="broken-reference">WCPeerMeta</a>): <br>Creates and receives <a href="broken-reference">WCSession</a>, <a href="broken-reference">WCPeerMeta</a>, connects the Bridge Server, and creates an Instance with the connected session.</li><li>requestSession(id: String): <br>Requests a session connection to the Wallet by calling requestSession (wc_sessionRequest) with the peerId received during connect.</li><li>wcConnect Listener: <br>Called when connecting to the Bridge Server.</li><li>wcSessionApprove Listener: <br>Called when a connection with the Wallet is complete.</li><li>wcSessionReject Listener: <br>Called when connection with the Wallet fails.</li></ul></td></tr><tr><td><a href="#disconnect">Disconnect</a></td><td><ul><li>disconnect(): <br>Calls KillSession(wc_sessionUpdate approved=false) if there is a connected session, and disconnects WebSocket if WebSocket is open.</li><li>wcDisconnect Listener: <br>Called when a Disconnect request (KillSession request) is received, or when a SocketException error occurs.</li></ul></td></tr><tr><td><a href="#getaccount">GetAccount</a></td><td><ul><li>getAccounts(): <br>When sessionApprove is reached, userAddress and network(ChainId) are set. If you want to check again while connected, you can check again by sending getAccounts (get_accounts) to your wallet.</li><li>wcResultResponse Listener: <br>Receives success or failure response values.</li></ul></td></tr><tr><td><a href="#sendtransaction">SendTransaction</a></td><td><ul><li>sendTransaction(transaction: <a href="broken-reference">WCEthereumTransaction</a>): <br>Creates and transmits <a href="broken-reference">WCEthereumTransaction</a> data to the Wallet to execute a transaction.</li><li>wcResultResponse Listener: <br>Receives success or failure response values. This value is the return hash value of the executed contract when the transaction is successful.</li></ul></td></tr><tr><td><a href="#signtransaction">SignTransaction</a></td><td>Signs a transaction that can be submitted to the network</td></tr><tr><td><a href="#personalsign">PersonalSign</a></td><td>The sign method calculates an Ethereum specific signature with:sign(keccack256("\x19Ethereum Signed Message:\n" + len(message) + message))).</td></tr></tbody></table>

## Connect

### **Connect using DeepLink**

Create a Session with DeepLink structure to open a Socket and connect to BridgeServer.

```kotlin
ConnectManager.setClient(
    wcSession = WCSession(
        bridge = "https://wc-bridge.neopin.io/",
    ),
    peerMeta = WCPeerMeta(
        appId = "NWC1004HS6VFTPPPLUGFQBQ5SFNWB79B",
        name = "Dapp Sample",
        url = "https://www.sample.com/",
        description = "Dapp Platform",
        icons = listOf(""),
    )
)
```

When the BridgeServer is connected, events are forwarded to the ConnectManager.wcConnect Listener. When the connection is confirmed, send requestSession(wc\_sessionRequest). Call WalletSample via DeepLink.

```kotlin
ConnectManager.wcConnect = { peerId, session ->
    ConnectManager.requestSession(peerId)
    startActivity(Intent(
        Intent.ACTION_VIEW,
        Uri.parse(session.toUri())
    ))
}
```

Touching the Connect button in WalletSample sends an event to the DAppSample's ConnectManager.wcSessionApprove Listener. When canceled, an event is forwarded to ConnectManager.wcSessionReject Listener.

```kotlin
ConnectManager.wcSessionApprove = { peerId, requestId, accounts ->
    val instance = ConnectManager.getInstance()
    // TODO
}
ConnectManager.wcSessionReject = { peerId, requestId, payload ->
    val instance = ConnectManager.getInstance()
    // TODO
}
```

### **Connect using QRCode**

Create a Session with DeepLink structure to open a Socket and connect to BridgeServer.

```kotlin
ConnectManager.setClient(
    wcSession = WCSession(
        bridge = "https://wc-bridge.neopin.io/",
    ),
    peerMeta = WCPeerMeta(
        appId = "NWC1004HS6VFTPPPLUGFQBQ5SFNWB79B",
        name = "Dapp Sample",
        url = "https://www.sample.com/",
        description = "Dapp Platform",
        icons = listOf(""),
    )
)
```

When the BridgeServer is connected, events are forwarded to the ConnectManager.wcConnect Listener. When the connection is confirmed, send a requestSession(wc\_sessionRequest). Use DeepLink to generate a QRCode image and display it on the screen. Run WalletSample and scan the QRCode.

```kotlin
ConnectManager.wcConnect = { peerId, session ->
    ConnectManager.requestSession(peerId)
    QrCodeDialog(session.toUri()).let { dialog ->
        qrCodeDialog = dialog
        dialog.show(supportFragmentManager, dialog.tag)
    }
}
```

Touching the Connect button in WalletSample sends an event to the DAppSample's ConnectManager.wcSessionApprove Listener. When canceled, an event is forwarded to ConnectManager.wcSessionReject Listener. When sessionApprove is reached, the QRCode image is removed from the screen.

```kotlin
ConnectManager.wcSessionApprove = { peerId, requestId, accounts ->
    qrCodeDialog?.dismiss()
    val instance = ConnectManager.getInstance()
    // TODO
}
ConnectManager.wcSessionReject = { peerId, requestId, payload ->
    val instance = ConnectManager.getInstance()
    // TODO
}

```

## Disconnect

Disconnect from DApp: Call ConnectManager.disconnect().

```kotlin
ConnectManager.disconnect()
```

Disconnect from Wallet: When disconnected from Wallet, an event is forwarded to ConnectManager.wcDisconnect Listener.

```kotlin
ConnectManager.wcDisconnect = { peerId ->
    // TODO
}
```

## GetAccount

Method to check the Address and ChainId of the currently connected Wallet.

Call ConnectManager.getAccounts().

```kotlin
ConnectManager.getAccounts()
```

The event is forwarded to ConnectManager.wcResultResponse Listener.

```kotlin
ConnectManager.wcResultResponse = { peerId, requestId, payload ->
    val instance = ConnectManager.getInstance()
    // TODO
}
```

## SendTransaction

Method to communicate with Wallet.

To use NEOPIN Connect eth\_sendTransaction in DApp, the following procedure is required.

<figure><img src="https://3207921449-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FqGie2GKzhU3olmN7yiWW%2Fuploads%2FDbNUTztPDw4B19BE00zf%2F%EC%8A%A4%ED%81%AC%EB%A6%B0%EC%83%B7%202022-08-23%20%EC%98%A4%ED%9B%84%204.54.40.png?alt=media&#x26;token=2ad84d3f-afec-4b24-a319-adfe92e57d4c" alt=""><figcaption></figcaption></figure>

Create and send a WCEthereumTransaction object to ConncetManager.sendTransaction(). You have to create and insert the data that goes into WCEthereumTransaction yourself.

```kotlin
val instance = ConnectManager.getInstance()
// to, nonce, gasPrice, gas, value, data are arbitrary data.
// Each should be modified according to its purpose.
ConnectManager.sendTransaction(
    WCEthereumTransaction(
        
chainId = "8217",
        from = instance?.userAddress ?: "0x0",
        to = "0x0", // Contract Address
        nonce = "0x0001",
        gasPrice = "0x9184e72a000",
        gas = "0x76c0",
        value = "0x0",
        data = "0xa9059cbb00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
    )
)
```

The success and failure results of SendTransaction are forwarded to ConnectManager.wcResultResponse Listener.

```kotlin
ConnectManager.wcResultResponse = { peerId, requestId, payload ->
    val instance = ConnectManager.getInstance()
    // TODO
}
```

* When creating a Client.Transaction object for SendTransaction, specify the ChainID of the target network. (\*Required)
* NEOPIN Connect supports a variety of networks, so specifying a chainID is required.

| Network | MainNet | TestNet |
| ------- | ------- | ------- |
| Klaytn  | 8217    | 1001    |
| Polygon | 137     | 80001   |

## 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

<pre class="language-kotlin"><code class="lang-kotlin">val instance = ConnectManager.getInstance()
// to, nonce, gasPrice, gas, value, data are arbitrary data.
// Each should be modified according to its purpose.
ConnectManager.signTransaction(
    WCEthereumTransaction(
        <a data-footnote-ref href="#user-content-fn-1">chainId = "8217"</a>,
        from = instance?.userAddress ?: "0x0",
        to = "0x0", // Contract Address
        nonce = "0x0001",
        gasPrice = "0x9184e72a000",
        gas = "0x76c0",
        value = "0x0",
        data = "0xa9059cbb00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
    )
)
</code></pre>

The success and failure results of SignTransaction are forwarded to ConnectManager.wcResultResponse Listener.

```kotlin
ConnectManager.wcResultResponse = { peerId, requestId, payload ->
    val instance = ConnectManager.getInstance()
    // TODO
}
```

## PersonalSign

* The sign method calculates an Ethereum specific signature with:<mark style="background-color:green;">`sign(keccack256("\\x19Ethereum Signed Message:\\n" + len(message) + message)))`</mark>.
* 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.

```kotlin
val instance = ConnectManager.getInstance()

ConnectManager.personalSign("message".encodeHex())
```

The success and failure results of PersonalSign are forwarded to ConnectManager.wcResultResponse Listener.

```kotlin
ConnectManager.wcResultResponse = { peerId, requestId, payload ->
    val instance = ConnectManager.getInstance()
    // TODO
}
```

[^1]: 수정됨
