LogoLogo
  • Sign Protocol
    • Introduction
    • FAQ
  • For Hackers
    • Getting Started
  • For Builders
    • Getting Started
      • Fundamentals
      • Tutorials
        • Building a Simple Notary Platform
          • Schema Creation
          • Attestation Creation
          • Querying Attestations
          • Parsing Attestation Data
        • Delegate Attestation Creation
          • Express Backend
          • Solidity
      • Examples
        • Attestation Discord Bot
        • KYC-Gated Smart Contract Access Control
    • Supported Networks
    • Sign Developer Platform
    • Advanced Topics
      • Cross Chain Attestations
      • Hybrid Attestations
      • Schema Hooks
        • Tutorial: Checking Attestation Data
          • Schema Creation
          • Schema Hook
          • Creating an Attestation (Solidity)
      • ZK Attestations
        • Compile a Circuit
        • Create a Schema Hook
    • Querying Data
      • NPM SDK
        • Usage & Examples
      • REST APIs
        • Schemas
        • Attestations
      • GraphQL
        • Schemas
        • Attestations
    • Writing Data
      • NPM SDK
        • Installation
        • Usage
          • Schemas
          • Attestations
        • Examples
        • Changelog
      • Smart Contract
        • EVM
          • Interfaces
            • ISP
            • ISPHook
            • IVersionable
          • Data Models
            • Schema
            • Attestation
            • DataLocation
  • For Thinkers
    • A Future Of Verifiable Trust
    • Effective Attestations
    • Incentive-Aligned Public Good
    • Glossary
      • Attestations
      • Schema
      • Schema Registry
      • Attestation Repository
  • Case Study
    • EthSign
    • KYC-Gated Contract Calls
    • Proof of Audit
    • Developer Onchain Reputation
    • Onboarding Web2 Data
Powered by GitBook
LogoLogo

Copyright Sign 2021-2024

On this page
  • Initializing the Sign Protocol Client
  • Setting Up Your Controller
  • (Frontend) Generating a Delegation Signature
  • Recap

Was this helpful?

  1. For Builders
  2. Getting Started
  3. Tutorials
  4. Delegate Attestation Creation

Express Backend

We will start from a simple Express backend. You can use any backend infrastructure or library of your choosing but note that current library support is limited to JavaScript/TypeScript. Install express, @ethsign/sp-sdk, and viem.

import express from "express";
const app = express()
const port = 3000

app.get('/', (req, res) => {
  res.send('Hello World!')
})

app.listen(port, () => {
  console.log(`Example app listening on port ${port}`)
})

This boilerplate code is the simplest backend you can create. It creates a new server and listens for requests on port 3000.

Initializing the Sign Protocol Client

Since we are running this code from a backend server, Sign Protocol's SDK requires you to pass in a private key account when creating your SignProtocolClient. Typically, when no account is provided, the client will use window.ethereum, which does not exist in a backend environment. Your code should look something like the following, assuming you are building on Sepolia:

import {
  SignProtocolClient,
  SpMode,
  EvmChains
} from "@ethsign/sp-sdk";
import { privateKeyToAccount } from "viem/accounts";

const privateKey = "0x..."; // account responsible for paying gas fees

const client = new SignProtocolClient(SpMode.OnChain, {
  chain: EvmChains.sepolia,
  account: privateKeyToAccount(privateKey) // required in backend environments
});

Setting Up Your Controller

Your controller will be responsible for creating the attestation. Since this is a delegate attestation, it must provide the attestation object and the delegation signature. The backend is not responsible for generating the delegation signature because the user's wallet must sign a message to retrieve the signature. Your controller should look something like the following:

app.post(
  "/delegate",
  async (req: Request<{}, {}, { attestation: Attestation; delegationSignature: string }>, res: Response) => {
    try {
      const { attestation, delegationSignature } = req.body;

      const response = await client.createAttestation(attestation, {
        delegationSignature
      });
      res.status(200).json({ message: response });
    } catch (error: any) {
      res.status(500).json({
        error: {
          message: error.message
        }
      });
    }
  }
);

(Frontend) Generating a Delegation Signature

This section must be implemented on your frontend. Delegation signatures are a mechanism to allow a third party to upload an attestation on behalf of an attester. The attester must approve (sign) the attestation that is being created to prevent fraudulent attestations. In other words, you would not want someone making attestations using your wallet address without you approving the attestation content first - unapproved attestations could pose a major security threat. After attaining a signature from the user/attester, the attestation and signature can be sent to the backend we wrote above. Your frontend code should include the following:

import {
  EvmChains,
  delegateSignAttestation
} from "@ethsign/sp-sdk";

const schemaId = "0x...";
const attestationData = {
  ...
};
const indexingValue = "0x...";

const res = await delegateSignAttestation(
  {
    schemaId: schemaId,
    data: attestationData,
    indexingValue: indexingValue
  },
  { chain: EvmChains.sepolia }
);

const attResponse = await fetch(`${YOUR_BACKEND_URL}/delegate`, {
  method: "POST",
  headers: {
    "Content-Type": "application/json"
  },
  body: JSON.stringify({ attestation: res.attestation, delegationSignature: res.delegationSignature })
});

...

// You can now use `attResponse` for anything you need in your frontend code

Recap

The general flow to create a delegated attestation using a backend is the following:

  1. Generate the attestation content. This can be done on the frontend (as shown above) or on a dedicated backend (and then sent to the frontend for step 2).

  2. Request a delegation signature from the user in a browser.

  3. Forward the attestation and delegation signature to your dedicated backend.

  4. Create the attestation, providing the attestation and delegation signature.

PreviousDelegate Attestation CreationNextSolidity

Last updated 5 months ago

Was this helpful?