Used to check for browser translation.
用于检测浏览器翻译。
ブラウザの翻訳を検出する

Request for signature

zhanghan
2024-09-29 02:52
· edited

In some cases, you may need to use your DID Wallet to sign some data, this signature can be verified by your DID Wallet, follow this article to learn how to generate a signature with DID Connect.

The basic process is consistent with the "Quick Start" chapter, change the code in the handler as follows:

/* eslint-disable no-console */
const { toTypeInfo } = require('@arcblock/did');
const { fromPublicKey } = require('@ocap/wallet');
const { fromBase58 } = require('@ocap/util');
const { getRandomBytes } = require('@ocap/mcrypto');

const getRandomMessage = (len = 16) => {
  const hex = getRandomBytes(len);
  return hex.replace(/^0x/, '').toUpperCase();
};

const action = 'request-text-signature';
module.exports = {
  action,
onConnect() {
  return {
    signature: () => {
      const data = getRandomMessage();

      return {
        description: 'Please sign the text',
        type: 'mime:text/plain',
        data,
      };
    },
  };
},

  // eslint-disable-next-line consistent-return
  onAuth: ({ userDid, userPk, claims, updateSession }) => {
    const type = toTypeInfo(userDid);
    const user = fromPublicKey(userPk, type);
    const claim = claims.find((x) => x.type === 'signature');

    logger.info(`${action}.onAuth`, { userPk, userDid, claim });

    if (claim.origin) {
      if (user.verify(claim.origin, claim.sig, claim.method !== 'none') === false) {
        throw new Error('Origin signature error');
      }
    }

    updateSession({
      result: {
        origin: fromBase58(claim.origin).toString(),
        sig: claim.sig,
      },
    });
  },
};



Change the front-end part calling the DID Connect code to the following:

connectApi.open({
  locale: 'en',
  action: 'request-text-signature',
  onSuccess({ result }) {
    // signatue data will be in result
  },
  messages: {
title: 'Request Text Signature',
    scan: 'Please sign some text to continue',
},
});


Sticker