1 个不稳定版本
0.1.0 | 2022 年 3 月 10 日 |
---|
#66 在 #solidity
用于 eip712-cli
83KB
2K SLoC
eip712
生成用于验证 EIP-712 风格签名的 Solidity 代码。
用法
首先,创建一个实现所需功能的抽象合约
// SPDX-License-Identifier: CC0-1.0
pragma solidity ^0.8.11;
abstract contract NameRegistry {
mapping (string => address) public nameToAddress;
mapping (address => string) public addressToName;
// This `claim` function implements the functionality you want to expose
// via an EIP-712 signature.
//
// Importantly, the first argument must be the sender's address, and
// the function must be internal.
function claim(address sender, string memory name) internal {
require(bytes(addressToName[sender]).length == 0, "address already registered");
require(nameToAddress[name] == address(0), "name already registered");
addressToName[sender] = name;
nameToAddress[name] = sender;
}
// You can optionally expose your functionality using `msg.sender`.
function claim(string memory name) external {
claim(msg.sender, name);
}
// Finally, you should declare an abstract function that accepts a signature.
//
// The implementation will be generated for you.
function claim(string memory name, uint8 v, bytes32 r, bytes32 s) external virtual;
}
在命令行上
安装 eip712-cli
$ cargo install eip712-cli
然后运行它
$ eip712 \
--base NameRegistry \
--signing-domain NameRegistry \
--version 1 \
--output /tmp/output.sol \
NameRegistry.json
作为库
对于高级使用,这个crate可以从Rust或JavaScript作为库使用。
Rust
use eip712::Eip712;
use std::io::Write;
fn main() {
// Read the ABI file (normally generated by `solc`.)
let abi = include_str!("NameRegistry.json");
let mut output = String::new();
// Configure and run the generator.
Eip712::<()>::new("NameRegistry") // Name of the base contract.
.signing_domain("NameRegistry") // Name for the EIP-712 domain.
.version("1") // Contract version.
.read_str(abi)
.unwrap()
.generate(&mut output)
.unwrap();
std::io::stdout().write_all(output.as_bytes()).unwrap();
}
JavaScript
import { default as eip712, Eip712 } from './eip712.js';
async function main() {
await eip712();
// Read the ABI file (normally generated by `solc`.)
const resp = await fetch("./NameRegistry.json");
const abi = await resp.text();
// Configure and run the generator.
const output = new Eip712("NameRegistry") // Name of the base contract.
.error(console.log) // Error handler.
.warning(console.log) // Warning handler.
.signing_domain("NameRegistry") // Name for the EIP-712 domain.
.version("1") // Contract version.
.read_str(abi)
.generate();
let elem = document.createElement('pre');
elem.innerText = output;
document.body.appendChild(elem);
}
main();
依赖关系
~1.7–3MB
~66K SLoC