@metamask/eth-sig-util 上的 recoverTypedSignature 函数不起作用

recoverTypedSignature function on @metamask/eth-sig-util is not working

@metamask/eth-sig-util 上更新的 recoverTypedSignature 函数似乎都无法正常工作。一旦我将它添加到项目中,它就会报错。

如有任何帮助,我们将不胜感激。

错误是:

bundle.js:6306 Uncaught ReferenceError: Buffer is not defined at Object../node_modules/@metamask/eth-sig-util/node_modules/ethereumjs-util/dist/secp256k1v3 -lib/der.js (bundle.js:6306:40) 在 Object.options.factory (bundle.js:84170:31) 在 webpack_require (bundle.js:83608:33) 在 fn (bundle.js:83841:21) 在对象../node_modules/@metamask/eth-sig-util/node_modules/ ethereumjs-util/dist/secp256k1v3-adapter.js (bundle.js:5932:11) 在 Object.options.factory (bundle.js:84170:31) 在 webpack_require (bundle.js:83608:33) 在 fn (bundle.js:83841:21) 在对象../node_modules/@metamask/eth-sig-util/node_modules/ethereumjs-util/dist/index.js (bundle.js:5724:17) 在 Object.options.factory (bundle.js:84170:31)

密码是:


import { SignTypedDataVersion, recoverTypedSignature } from '@metamask/eth-sig-util';


const Request_Signature = (props: any) => {
    // Step 2:  Once user has authorized the use of its crypto wallet a signature can
    //          be requested

    async function sign_TypedDataV4() {
        const msgParamsOg = {
            domain: {
                // Defining the chain: 1 - Ethereum Main Net
                chainId: 1,
                // Friendly name
                name: "Initial Example Contract",
                // Additional way of verifying contract to make sure you are establishing contracts with the proper entity
                verifyingContract: "this",
                // Just let's you know the latest version. Definitely make sure the field name is correct.
                version: "1",
            },

            // Defining the message signing data content.
            message: {
                Request: "Please complete your authentication by signing this",
                username: "test_user",
            },
            // Refers to the keys of the *types* object below.
            primaryType: "LogIn",
            types: {
                EIP712Domain: [
                    {
                        name: "name",
                        type: "string",
                    },
                    {
                        name: "version",
                        type: "string",
                    },
                    {
                        name: "chainId",
                        type: "uint256",
                    },
                    {
                        name: "verifyingContract",
                        type: "address",
                    },
                ],
                // Refer to PrimaryType
                LogIn: [
                    {
                        name: "username",
                        type: "string",
                    },
                ],
            },
        };
        let msgParams = JSON.stringify(msgParamsOg);

        let account = props.account;
        var params = [account, msgParams];
        var method = "eth_signTypedData_v4";
        console.log('User Address:' + account);

        (window as any).ethereum.sendAsync(
            {
                method,
                params,
                account,
            },
            async function (err: Error, result: any) {
                if (err) return console.dir(err);
                if (result.error) {
                    alert(result.error.message);
                    return console.error("ERROR", result);
                }
                //console.log('TYPED SIGNED:' + JSON.stringify(result.result));

                let signature = result.result;

                const restored = recoverTypedSignature({
                    data: msgParamsOg as any,
                    signature,
                    version: SignTypedDataVersion.V4,
                  });

                console.log(restored);

            }
        );
    }


    return (
        <div>
            <button
                className='btn_main'
                type="button"
                onClick={async (e) => {
                    e.preventDefault();
                    sign_TypedDataV4();
                }}
            >
                Sign Now
            </button>
        </div>
    )
};

export default Request_Signature;

如果您使用的是 React,我不知道确切的解决方法,但这是一个解决方法: 将 react-scripts 降级到版本 4.0.3

就我而言,我还需要执行以下操作:

  1. 在package.json中使用react-script 4.0.3
  2. 移除package-lock.json
  3. 删除 node_modules 文件夹
  4. 运行 npm 安装

经过这一切似乎一切正常。

我不得不使用 webpack (https://webpack.js.org/) with Babel (https://babeljs.io/docs/en/babel-preset-react) to solve this. I as well had to install Buffer (https://www.npmjs.com/package/buffer) and stream-browserify (https://www.npmjs.com/package/stream-browserify)

这解决了问题