以太坊数据签署(二)

写在前面

在上一篇文章中,我们简单介绍了以太坊数据签署,在这边文章我们将直接讲一些使用案例,处理一些业务逻辑

简单签署消息
 

var sign =  require('@metamask/eth-sig-util');

const helloWorldSignature =
    '0x90a938f7457df6e8f741264c32697fc52f9a8f867c52dd70713d9d2d472f2e415d9c94148991bbe1f4a1818d1dff09165782749c877f5cf1eff4ef126e55714d1c';

const helloWorldMessage = `0x${Buffer.from('Hello, world!').toString('hex')}`;
const privateKey = Buffer.from('你的私钥','hex');

var a = sign.personalSign({ privateKey, data: Buffer.from(helloWorldMessage) });


console.log(a);
const s = fromRpcSig(a);
console.log("0x"+s.r.toString("hex"));
console.log("0x"+s.s.toString("hex"));
console.log(s.v);


签署类型的数据

这里我们使用 uniswap 中智能合约 https://docs.uniswap.org/protocol/V2/reference/smart-contracts/router-02#removeliquidityethwithpermitsupportingfeeontransfertokens

 移除流动性方法 removeLiquidityETHWithPermitSupportingFeeOnTransferTokens 结合开始讲起,因为这个方法涉及到了以太坊的数据签名授权转账

因为这里涉及到多个合约,还有dapp开发,智能合约开发,uniswap去中心化交易所,以太坊数据签署,授权,智能合约相互调用,等众多领域和相关知识关联,不了解这些的,建议先去深度学习下。

接下来我们要开始讲如何签署数据授权调用智能合约的免授权操作过程,并调用智能合约接口

 

const eth_sig_util = require('@metamask/eth-sig-util');

    // UniswapV2Pair constructor 
    // EIP712
    // 
    // 
    // constructor() public {
    //     uint chainId;
    //     assembly {
    //         chainId := chainid
    //     }
    //     DOMAIN_SEPARATOR = keccak256(
    //         abi.encode(
    //             keccak256('EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)'),
    //             keccak256(bytes(name)),
    //             keccak256(bytes('1')),
    //             chainId,
    //             address(this)
    //         )
    //     );
    // }




var chainId = await web3.eth.net.getId();

    console.log("chainId", chainId);
    // 
    const EIP712Domain = [
        { name: 'name', type: 'string' },
        { name: 'version', type: 'string' },
        { name: 'chainId', type: 'uint256' },
        { name: 'verifyingContract', type: 'address' }
    ]

    const domain = {
        name: 'Uniswap V2', //参看UniswapV2Pair合约中构造参数
        version: '1',       //参看UniswapV2Pair合约中构造参数
        chainId: chainId,    
        verifyingContract: "这里是对应的UniswapV2Pair智能合约地址"
    };



    const Permit = [
        { name: 'owner', type: 'address' },
        { name: 'spender', type: 'address' },
        { name: 'value', type: 'uint256' },
        { name: 'nonce', type: 'uint256' },
        { name: 'deadline', type: 'uint256' }
    ]


    //lppairContract 是UniswapV2Pair合约实例对象
    var nonce = await lppairContract.methods.nonces(“用户地址”).call();
    console.log("nonce", nonce);
    const message = {
        owner: "调用者的用户地址",
        spender: "UniswapV2Router02地址",
        value: web3.utils.toHex("授权lp的金额"), //授权lp的金额
        nonce: web3.utils.toHex(nonce),//UniswapV2Pair合约中调用者的nonce
        deadline: web3.utils.toHex(_deadline)

    }

    const privateKey = Buffer.from(
        “你的私钥”,
        'hex',
    );
    var signature = eth_sig_util.signTypedData({
        privateKey,
        data: {
            types: {
                EIP712Domain,
                Permit
            },
            domain,
            primaryType: 'Permit',
            message
        },
        version: eth_sig_util.SignTypedDataVersion.V3,
    });

到此我们就获得了签名类型数据,免去用户approve再移除流动的复杂操作,让用户调用一次方法就可以移除流动性,从而简化操作

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

北纬32.6

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值