erc20的签名值为629680c82f82ad9ea02704a988d70565741def2d93ec95efe07d8bd6976340e15b87a837d96406516436eef6d88491598e9c4f1c15aaf7260db8ef25af5b5851c,使用js求erc20的permit的r,s,v的值

时间: 2024-03-03 12:48:26 浏览: 19
以下是使用JavaScript计算ERC20 permit的r,s,v值的示例代码: ```javascript const ethers = require('ethers'); const abi = require('ethereumjs-abi'); const signature = '629680c82f82ad9ea02704a988d70565741def2d93ec95efe07d8bd6976340e15b87a837d96406516436eef6d88491598e9c4f1c15aaf7260db8ef25af5b5851c'; const owner = '0x1234567890123456789012345678901234567890'; const spender = '0x0987654321098765432109876543210987654321'; const value = 100; const nonce = 1; const deadline = 1634028589; // Define the EIP712 domain separator const domainType = [ { name: 'name', type: 'string' }, { name: 'version', type: 'string' }, { name: 'chainId', type: 'uint256' }, { name: 'verifyingContract', type: 'address' } ]; const domainData = { name: 'My Token', version: '1.0', chainId: 1, // Change to the correct chain ID verifyingContract: '0x1234567890123456789012345678901234567890' // Change to the correct contract address }; const domainSeparator = '0x' + ethers.utils.keccak256( ethers.utils.defaultAbiCoder.encode( ['bytes32', 'bytes32', 'bytes32', 'uint256', 'address'], [ ethers.utils.keccak256(ethers.utils.defaultAbiCoder.encode(domainType, [domainData])), ethers.utils.keccak256(Buffer.from('1901', 'hex')), ethers.utils.keccak256(Buffer.from('This is my typed data message!')), 1, domainData.verifyingContract ] ) ).substring(2); // Define the permit type const permitType = [ { name: 'owner', type: 'address' }, { name: 'spender', type: 'address' }, { name: 'value', type: 'uint256' }, { name: 'nonce', type: 'uint256' }, { name: 'deadline', type: 'uint256' } ]; const permitData = { owner, spender, value, nonce, deadline }; const permitStructHash = '0x' + abi.soliditySHA3( ['bytes32', ...permitType.map(({ type }) => type)], [ethers.utils.keccak256(Buffer.from('Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)')), ...Object.values(permitData)] ).toString('hex'); // Recover the signer from the signature const { v, r, s } = ethers.utils.splitSignature(Buffer.from(signature, 'hex')); const digest = ethers.utils.keccak256( ethers.utils.defaultAbiCoder.encode( ['bytes1', 'bytes1', 'bytes32', 'bytes32'], [ '0x19', '0x01', domainSeparator, permitStructHash ] ) ); const recoveredAddress = ethers.utils.recoverAddress(digest, { v, r, s }); // Verify that the recovered signer matches the owner if (recoveredAddress.toLowerCase() !== owner.toLowerCase()) { throw new Error('Invalid signature'); } // Print the r, s, v values console.log('r:', r.toString()); console.log('s:', s.toString()); console.log('v:', v.toString()); ``` 在上面的示例代码中,我们使用ethers.js和ethereumjs-abi库来计算ERC20 permit的r,s,v值。具体来说,我们首先定义了EIP712 domain separator和permit type,并将其编码为字节数组。然后,我们将这些字节数组哈希为domain separator和permit struct hash,使用签名值来恢复签名者的地址,并验证签名者是否与owner相匹配。最后,我们将r,s,v值打印到控制台上。 需要注意的是,上面的示例代码中的domainData.verifyingContract和domainData.chainId需要根据具体的ERC20代币合约进行修改。

相关推荐

最新推荐

recommend-type

埃斯顿机器人ER系列机器人操作手册.pdf

ESTUN ER系列机器人操作手册。包含安装部分、操作部分、系统部分。该操作手册对机器人的示教、再现、程序、文件编辑操作、作业管理等内容进行了全面的说明。
recommend-type

Altium Designer ERC错误中英对照

Altium Designer ERC错误中英对照表,方便各位朋友检查硬件设计错误
recommend-type

基于Python的蓝桥杯竞赛平台的设计与实现

【作品名称】:基于Python的蓝桥杯竞赛平台的设计与实现 【适用人群】:适用于希望学习不同技术领域的小白或进阶学习者。可作为毕设项目、课程设计、大作业、工程实训或初期项目立项。 【项目介绍】:基于Python的蓝桥杯竞赛平台的设计与实现
recommend-type

python实现基于深度学习TensorFlow框架的花朵识别项目源码.zip

python实现基于深度学习TensorFlow框架的花朵识别项目源码.zip
recommend-type

3-9.py

3-9
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

2. 通过python绘制y=e-xsin(2πx)图像

可以使用matplotlib库来绘制这个函数的图像。以下是一段示例代码: ```python import numpy as np import matplotlib.pyplot as plt def func(x): return np.exp(-x) * np.sin(2 * np.pi * x) x = np.linspace(0, 5, 500) y = func(x) plt.plot(x, y) plt.xlabel('x') plt.ylabel('y') plt.title('y = e^{-x} sin(2πx)') plt.show() ``` 运行这段
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。