🔴stx_signStructuredMessage

You can request your user to sign a message with structured data, using their wallet's Stacks address, by invoking the stx_signStructuredMessage method.

The structured data implementation makes the message easier to read and allows you facilitates the interaction with ClarityValue in your application. It follows the SIP-018 Signed Structured Data specification, a standard way to represent information in human-readable format in applications and smart contracts in order to produce signatures that are straightforward and inexpensive to verify. For developers familiar with Ethereum, this design is similar to EIP-712.

Request parameters
Description

message

a string representing the message, as a ClarityValue serialized in hexadecimal format.

domain

a string representing the domain, as a ClarityValue serialized in hexadecimal format.

To convert Clarity values to their hex representation, you can use the { cvToHex, stringAsciiCV, tupleCV, uintCV } helpers from @stacks/transactions package.

   import { cvToHex, stringAsciiCV, tupleCV, uintCV } from "@stacks/transactions";
   import { request } from "sats-connect";
   
   const response = await request("stx_signStructuredMessage", {
      message: cvToHex(tupleCV({ hello: stringAsciiCV("world") })).slice(2), // remove 0x,
      domain: cvToHex(
        tupleCV({
          name: stringAsciiCV("sats-connect-example"),
          version: stringAsciiCV("1.2.3"),
          "chain-id": uintCV(chainIds[network]),
        })
      ).slice(2),
    });
    if (response.status === "success") {
      alert("Success! Check the console for the response.");
      console.log(response.result);
    } else {
      console.error(
        "Something went wrong. Check the console for the response."
      );
      console.error(response);
    }

The user will see a Stacks structured message signing request prompt in the wallet.

The stx_signStructuredMessage method returns a Promise that resolves if the user approves the request. The message is then hashed using sha256 before being signed with secp256k1, and the method returns the SignStxStructuredMessageResult object:

Property
Description

signature

a string representing the signed message

publicKey

a string representing the public key of the address used for signing, as a hex-encoded string

Last updated