Signing Data

Signing arbitrary data enables you to verify the user's possession of an account. This guide will walk you through the signing process and verification.

Obtain a chain provider

As covered in Provider Activation, obtain a chain provider for the networks you want to interact with. In the following examples, we will use both Ethereum and Constellation providers.

const dagProvider = window.stargazer.getProvider("constellation");
const ethProvider = window.stargazer.getProvider("ethereum");

Constellation Message Signingarrow-up-right

Build a signature requestarrow-up-right

Constellation signatures for messages are done through a signature request object. The signature request object is sent for the user to accept. Uppon approval, a signature of the whole object is returned.

// Build the signature request
const signatureRequest: SignatureRequest = {
  content: "Sign this message to confirm your address",
  metadata: {
    user: "3feb69d6-d3f0-4812-9c93-384bee08afe8",
  },
};

Encode the signature requestarrow-up-right

Requests need to be a Base64 < JSON encoded string to sign. The wallet will then generate the signature from the same characters that compose this encoded request.

Send the signature requestarrow-up-right

Once built and encoded, you can send the encoded signature request using the dag_signMessage RPC method.

Important

When the signature request is sent, the wallet will verify compliance with the schema of the signature request object. If it does not comply, the wallet will throw an error.

The returned signature corresponds to the SHA512 hash of the encoded signature request and the private key of the user. ECDSA.sign(privateKey, sha512(signatureRequestEnconded)).

Read more about Constellation signature verification

Get the account public keyarrow-up-right

After you generate a signature from your encoded request, you need to retrieve the public key from the signer's account for future verification. This is due to the fact that Constellation signatures are not recoverable (i.e. do not contain the v parameter like in Ethereum).

Ethereum Message Signingarrow-up-right

The Stargazer Ethereum RPC API implements both EIP-191arrow-up-right (personal_sign) and EIP-712arrow-up-right (eth_signTypedData) as arbitrary message signing methods.

personal_sign methodarrow-up-right

The RPC API provided reveals the personal_sign RPC method for message signing. In this case, the message signed is an arbitrary hex string prefixed by the "\x19Ethereum Signed Message:\n" string and the length of the message in bytes from EIP-191arrow-up-right.

The returned signature corresponds to the keccak256 hash of the prefix + message string and the private key of the user. ECDSA.sign(privateKey, keccak256("\x19Ethereum Signed Message:\n" + len(message) + message)).

Read more about Ethereum signature verification

eth_signTypedData methodarrow-up-right

The RPC API provided reveals the eth_signTypedData RPC method for typed message signing. In this case, the message signed is the hash of the typed data according to EIP-712arrow-up-right prefixed by the "\x19\x01" string according to EIP-191arrow-up-right.

The returned signature corresponds to the keccak256 hash of the domainSeparator + hashStruct(message) and the private key of the user. ECDSA.sign(privateKey, keccak256("\x19\x01" + domainSeparator + hashStruct(message)).

Read more about Ethereum signature verificationarrow-up-right

Constellation Signature Verificationarrow-up-right

For signature verification, we will be using the @stardust-collective/dag4arrow-up-right package. The following snippet illustrates how you can verify an encoded request signature.

Ethereum Signature Verificationarrow-up-right

For signature verification, we will be using the ethersarrow-up-right package. The following snippets illustrate how you can verify different message signatures.

eth_personalSign

eth_signTypedData

Last updated

Was this helpful?