MAINNET:
Loading...
TESTNET:
Loading...
/
onflow.org
Flow Playground

Signing and Verifying Arbitrary Data


Signing Arbitrary Data

Cryptographic signatures are a key part of the blockchain. They are used to prove ownership of an address without exposing its private key. While primarily used for signing transactions, cryptographic signatures can also be used to sign arbitrary messages.

FCL has a feature that lets you send arbitrary data to a configured wallet/service where the user may approve signing it with their private key/s.

Verifying User Signatures

What makes message signatures more interesting is that we can use Flow blockchain to verify the signatures. Cadence has a built-in function called verify that will recover the address of the private key that a message was signed with.

FCL includes a utility function, verifyUserSignatures, for verifying one or more signatures against an account's public key on the Flow blockchain.

You can use both in tandem to prove a user is in control of a private key or keys. This enables cryptographically-secure login flow using a message-signing-based authentication mechanism with a user’s public address as their identifier.


currentUser().signUserMessage()

A method to use allowing the user to personally sign data via FCL Compatible Wallets/Services.

:Note: Requires authentication/configuration with an authorized signing service.

Arguments

NameTypeDescription
messagestringA hexadecimal string to be signed

Returns

TypeDescription
ArrayAn Array of CompositeSignatures: {addr, keyId, signature}

Usage

import * as fcl from "@onflow/fcl"

export const signMessage = async () => {
  const MSG = Buffer.from("FOO").toString("hex")
  try {
    return await currentUser().signUserMessage(MSG)
  } catch (error) {
    console.log(error)
  }
}

verifyUserSignatures

A method allowing applications to cryptographically verify the ownership of a Flow account by verifying a message was signed by a user's private key/s. This is typically used with the response from currentUser().signUserMessage.

Arguments

NameTypeDescription
messagestring (required)A hexadecimal string
compositeSignaturesArray (required)An Array of CompositeSignatures

Returns

TypeDescription
Booleantrue if verifed or false

Usage

import * as fcl from "@onflow/fcl"

const verifySignatures = async (message, compositeSignatures) => {
  try {
    return await verifyUserSignature(message, compositeSignatures)
  } catch (error) {
    console.log(error)
  }
}

Examples

Use cases include cryptographic login, message validation, verifiable credentials, and others.