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

请求签名

zhanghan
2024-09-29 02:55
· edited

基本的流程与 快速开始 章节中一致,将 handler 中代码改为以下内容:

/* 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,
      },
    });
  },
};

将前端部分调用 DID Connect 代码改为以下内容:

connectApi.open({
  locale: 'zh',
  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