Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WalletConnect/BNB]: Add support for WalletConnect signing requests #3632

Merged
merged 18 commits into from
Jan 10, 2024

Conversation

satoshiotomakan
Copy link
Collaborator

@satoshiotomakan satoshiotomakan commented Dec 29, 2023

Description

Add an interface to parse signing requests received through WalletConnect protocol. For now, BNB chain is only supported.

How to use

  1. Parse the received signing request by using WalletConnectRequest.parse method.
  2. Adjust fee, add a privateKey to the result Proto::SigningInput message.
  3. Generate and sign a transaction by using AnySigner.sign method.
  4. Generate a WalletConnect JSON response with the result signature.

Example

Request received by WalletConnect framework (BNB Beacon SendOrder)

{
    "signerAddress": "bnb1grpf0955h0ykzq3ar5nmum7y6gdfl6lxfn46h2",
    "signDoc": {
        "account_number": "12",
        "chain_id": "chain-bnb",
        "memo": "",
        "data": null,
        "msgs": [
            {
                "inputs": [
                    {
                        "address": "bnb1grpf0955h0ykzq3ar5nmum7y6gdfl6lxfn46h2",
                        "coins": [ {"amount": 1001000000, "denom": "BNB"} ]
                    }
                ],
                "outputs": [
                    {
                        "address": "bnb1hlly02l6ahjsgxw9wlcswnlwdhg4xhx38yxpd5",
                        "coins": [ {"amount": 1001000000, "denom": "BNB"} ]
                    }
                ]
            }
        ],
        "sequence": "35",
        "source": "1"
    }
}

How to parse and handle the signing request

import wallet.core.jni.AnySigner
import wallet.core.jni.CoinType
import wallet.core.jni.proto.WalletConnect

// Step 1: Parse a signing request received through WalletConnect.

val parsingInput = WalletConnect.ParseRequestInput.newBuilder().apply {
    method = WalletConnect.Method.CosmosSignAmino
    payload = "{\"signerAddress\":\"bnb1grpf0955h0ykzq3ar5nmum7y6gdfl6lxfn46h2\",\"signDoc\":{\"account_number\":\"19\",\"chain_id\":\"chain-bnb\",\"memo\":\"\",\"data\":null,\"msgs\":[{\"inputs\":[{\"address\":\"bnb1grpf0955h0ykzq3ar5nmum7y6gdfl6lxfn46h2\",\"coins\":[{\"amount\":1001000000,\"denom\":\"BNB\"}]}],\"outputs\":[{\"address\":\"bnb13zeh6hs97d5eu2s5qerguhv8ewwue6u4ywa6yf\",\"coins\":[{\"amount\":1001000000,\"denom\":\"BNB\"}]}]}],\"sequence\":\"23\",\"source\":\"1\"}}"
}.build()

val parsingOutputBytes = WalletConnectRequest.parse(BINANCE, parsingInput.toByteArray())
val parsingOutput = WalletConnect.ParseRequestOutput.parseFrom(parsingOutputBytes)

assertEquals(parsingOutput.error, Common.SigningError.OK)

// Step 2: Set missing fields.

val signingInput = parsingOutput.binance.toBuilder().apply {
    privateKey = ByteString.copyFrom("95949f757db1f57ca94a5dff23314accbe7abee89597bf6a3c7382c84d7eb832".toHexBytes())
}.build()

// Step 3: Sign the transaction.

val output = AnySigner.sign(signingInput, BINANCE, SigningOutput.parser())

assertEquals(output.error, Common.SigningError.OK)

Prepare a WalletConnect JSON response (note this is blockchain specific).

import org.json.JSONObject

@Serializable 
data class CosmosResponse(val signed: JSONObject, val signature: JSONObject)

// Step 4: Construct a WalletConnect JSON response.

// Use the result `signatureJson`.
val signature = JSONObject(output.signatureJson)
// Use the original `signDoc` as a `signed` value.
val response = CosmosResponse(request.params.signDoc, signature)

Response to be sent via WalletConnect framework

{
    "signed": {
        "account_number": "12",
        "chain_id": "chain-bnb",
        "memo": "",
        "data": null,
        "msgs": [
            {
                "inputs": [
                    {
                        "address": "bnb1grpf0955h0ykzq3ar5nmum7y6gdfl6lxfn46h2",
                        "coins": [ {"amount": 1001000000, "denom": "BNB"} ]
                    }
                ],
                "outputs": [
                    {
                        "address": "bnb1hlly02l6ahjsgxw9wlcswnlwdhg4xhx38yxpd5",
                        "coins": [ {"amount": 1001000000, "denom": "BNB"} ]
                    }
                ]
            }
        ],
        "sequence": "35",
        "source": "1"
    },
    "signature": {
        "pub_key": {
						"type": "tendermint/PubKeySecp256k1",
						"value": "Amo1kgCI2Yw4iMpoxT38k/RWRgJgbLuH8P5e5TPbOOUC"
				},
        "signature": "1b1181faec30b60a2ddaa2804c253cf264c69180ec31814929b5de62088c0c5a45e8a816d1208fc5366bb8b041781a6771248550d04094c3d7a504f9e8310679"
    }
}

How to test

Run Rust, C++, Kotlin, iOS tests

Types of changes

Checklist

  • Create pull request as draft initially, unless its complete.
  • Add tests to cover changes as needed.
  • Update documentation as needed.
  • If there is a related Issue, mention it in the description.

If you're adding a new blockchain

  • I have read the guidelines for adding a new blockchain.

@satoshiotomakan satoshiotomakan marked this pull request as ready for review January 9, 2024 05:37
impl TWBinanceProto for BinanceMessageEnum {
type Proto<'a> = BinanceMessageProto<'a>;

fn from_tw_proto(coin: &dyn CoinContext, msg: &Self::Proto<'_>) -> SigningResult<Self> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh boy, what a list 😅

@satoshiotomakan satoshiotomakan merged commit 8f8a266 into master Jan 10, 2024
12 checks passed
@satoshiotomakan satoshiotomakan deleted the s/wallet-connect-v2 branch January 10, 2024 06:31
@satoshiotomakan satoshiotomakan linked an issue Jan 10, 2024 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Invalid signature for BNB through Walletconnect 2.0
3 participants