http://www.7klian.com

凭据EIP-712类型签名完成委托和投票

      { name: ‘chainId’, type: ‘uint256’ },
const sig = signature.value;
    address delegatee;
sign.onclick = async () => {
按照EIP-712实现委托的完整代码[21]可以在compound管理样例代码客栈[22]中查察。

Compound[4]的管理体系是由发放给用户的COMP代币[5]来驱动的。COMP代币持有者拥有与持有量1:1的投票权。投票权利可以委托给任意一个地点,让其去给提案投票。
    console.log(‘msgParams’, JSON.parse(msgParams));
这种方法的利用场景大概是,一个委托者但愿连系其他COMP持有者将他们的投票委托给被委托人,并但愿以很是低的成原来完成这项事情。

      { name: ‘delegatee’, type: ‘address’ },
    uint expiry;
    EIP712Domain: [
Gas利用量: 306046
    EIP712Domain: [
      return;
[14]MetaMask: https://metamask.io/
  const domain = { name: ‘Compound’, chainId, verifyingContract: compAddress };
    params: [ myAccount, msgParams ],
    }
[25]完整的Node.js代码: https://github.com/compound-developers/compound-governance-examples/tree/master/signature-examples/batch-publish-examples
用户可以通过两种方法委托投票或对提案举办投票:可以直接挪用函数(delegate, castVote)或通过签名成果函数(delegateBySig, castVotebySig)。
signatures.forEach((signature) => {
};
[22]compound管理样例代码客栈: https://github.com/compound-developers/compound-governance-examples
var batch = new web3.BatchRequest();
      return;
[21]完整代码: https://github.com/compound-developers/compound-governance-examples/tree/master/signature-examples
  const _delegatee = delegateTo.value;
    uint nonce,
  const msgParams = createDelegateBySigMessage(compAddress, _delegatee, _expiry, _chainId, _nonce);
  });
利用Solidity智能合约也可以达到同样的结果,颠末比拟,这种方法耗损的gas更少。
别的用户还要传入分成3部门的
签名,凡是写作为v, r和s
      gasPrice: web3.utils.toHex(25000000000),
      Sig memory sig = sigs[i];

当一个用户会见这个页面时,他们可以看到本身选中的钱包[15]地点和默认的Compound管理地点。他们可以将被委托人的地点填到这个地点中。在实际应用中,这个地点可以牢靠写成你要委托的方针地点。
  return JSON.stringify({ types, primaryType, domain, message });
    uint8 v;
    ]
await batch.execute();
利用签名实现委托
[2]DIFENG: https://learnblockchain.cn/people/1234
一旦用户收集完Compound管理的已签名的生意业务后,他们需要将这些生意业务宣布到以太坊上。实现批量发送生意业务的代码对付对付打点多个用户的私钥生意业务所和钱包很有辅佐。
  );
  }

利用此代码[13],任何人可以建设一个让用户利用签名来委托投票权利的的网页,我们假定会见此页面的所有用户都利用MetaMask[14]来挪用Web3函数。

[23]完整页面代码: https://github.com/compound-developers/compound-governance-examples/tree/master/signature-examples
[1]登链翻译打算: https://github.com/lbc-team/Pioneer
  }, async (err, result) => {
      comp.delegateBySig(sig.delegatee, sig.nonce, sig.expiry, sig.v, sig.r, sig.s);
  ) external;
  }
    },
被委托者可以建设一个网页,让用户通过Metamask和私钥完成delegateBySig 生意业务,这样被委托者就能收集到签名信息。之后,被委托者可以将签名信息打包,批量一次写入到以太坊中,再执行delegateBySig函数就可以正式的收集到用户的投票权利。
pragma solidity ^0.5.16;
    ],
[15]钱包: https://learnblockchain.cn/2019/04/11/wallet-dev-guide
    ],
    Delegation: [
    )
  function delegateBySigs(Sig[] memory sigs) public {
1. 被委托人以太坊地点
      console.error(‘ERROR’, result.error.message);
[29]compound管理样例代码客栈: https://github.com/compound-developers/compound-governance-examples
};
[26]compound管理样例码客栈: https://github.com/compound-developers/compound-governance-examples
凭据EIP-712类型投票完整页面代码[23]可以在compound管理样例代码客栈[24]查察。
pragma experimental ABIEncoderV2;
任何故太坊地点都可以利用被委托人地点,nonce[20],逾期时间和签名来宣布委托生意业务。这些是挪用COMP合约的delegateBySig函数需要的参数。得到的签名需要分成3个参数,定名为 v, r 和 s。

  struct Sig {
2. 布尔值来暗示赞成照旧阻挡提案
const v = ‘0x’ + sig.substring(2).substring(128, 130);
[6]签名: https://learnblockchain.cn/article/786
  const primaryType = ‘Ballot’;
const s = ‘0x’ + sig.substring(2).substring(64, 128);
  const types = {
[18]函数: https://docs.metamask.io/guide/signing-data.html#sign-typed-data-v4
  function delegateBySig(
同delegateBySig一样,用户也可以委托第三方给 Compound管理提案[11]投票。任何用户只要有已签名的委投票生意业务,都可以挪用智能合约中castVoteBySig 函数[12]
[8]EIP-712: https://github.com/ethereum/EIPs/blob/master/EIPS/eip-712.md
和委托一样,投票也可以建设一个网页来完成。用户在委托投票权利的时候,“附和”和“阻挡”的委托需要分隔发送生意业务,第三方可以选择最终宣布哪一个委托。
[17]Metamask: https://learnblockchain.cn/article/1217
    method: ‘eth_signTypedData_v4’,
[12]函数: https://compound.finance/docs/governance#cast-vote-by-signature
在Web3网页中通过签名投票
  };
    {
    nonce.value = _nonce;
  const _nonce = await comp.methods.nonces(myAccount).call();
[31]Discord: https://compound.finance/discord
  };
});
web3.BatchRequest工具将delegateBySig 签名打包,然后宣布到区块链上。完整的Node.js代码[25] 可以在compound管理样例码客栈[26]查察。更多关于Web.js打包可以查察文档[27].
接下来,用户会点击“Create Delegation Signature(建设委托签名)“按钮,这个会触发Metamask执行数据签名。MetaMask的文档里有关于数据签名[16]的具体先容。

[11]Compound管理提案: https://compound.finance/governance/proposals
interface Comp {
    uint expiry,
通过签名投票
通过JSON RPC可能智能合约可以一次性将收集好的签名生意业务宣布到区块链上。以下代码仅利用了Web3.js,代码实现建设然后批量宣布由每一个私钥生成的委托签名的成果。
contract BatchDelegate {
      alert(result.error.message);
代码中利用了Metamask[17]自带eth_signTypedData_v4函数[18]。这个函数可以
凭据EIP-712类型的完成布局化数据签名[19]。完成一次有效的签名,需要3个参数
  const primaryType = ‘Delegation’;
  const message = { proposalId, support };
2. 利用签名账户从COMP智能合约中得到的的nonce
[9]COMP代币: https://etherscan.io/address/0xc00e94cb662c3520282e6f5717214004a7f26888
      { name: ‘expiry’, type: ‘uint256’ }
    from: myAccount
  batch.add(comp.methods.delegateBySig(delegatee, nonce, expiry, v, r, s).send.request(
完整的Solidity代码[28] 可以在compound管理样例代码客栈[29]找到,内里尚有JSON RPC的代码和智能合约陈设代码。
      alert(err);
      { name: ‘verifyingContract’, type: ‘address’ },
[13]此代码: https://github.com/compound-developers/compound-governance-examples/tree/master/signature-examples
点击“Create Vote Signatures(建设投票签名)”之后,会弹出让用户选择签名“赞成”和“阻挡”生意业务。与delegateBySig函数一致,通过通报2个参数来挪用castVoteBySig函数。

在用户点击按钮“SIGN(签名)”时,会执行如下触发事件:
参考资料
    bytes32 r;
    Comp comp = Comp(0xc00e94Cb662C3520282E6f5717214004A7f26888);
  const domain = { name: ‘Compound Governor Alpha’, chainId, verifyingContract: govAddress };
[7]gas: https://learnblockchain.cn/2019/06/11/gas-mean
      from: myWalletAddress,
  const message = { delegatee, nonce, expiry };
[27]文档: https://web3js.readthedocs.io/en/v1.2.9/web3.html#batchrequest
const r = ‘0x’ + sig.substring(2).substring(0, 64);
  const types = {
[19]布局化数据签名: https://github.com/ethereum/EIPs/blob/master/EIPS/eip-712.md#definition-of-typed-structured-data-%F0%9D%95%8A
    uint8 v,
Gas利用量: 306046
1. Compound管理提案独一编号(自增的整型)
[24]compound管理样例代码客栈: https://github.com/compound-developers/compound-governance-examples
      { name: ‘nonce’, type: ‘uint256’ },
}
const createVoteBySigMessage = (govAddress, proposalId, support, chainId = 1) => {
[30]Compound 新闻: https://compound.substack.com/?utm_source=guide_signatures
    if (err) {
在Web3页面中利用签名实现委托

第三方提交用户签名生意业务和delegateBySig的环境是一样的,可是投票权利仅限于一个提案,并非无限制的提案。在第三方正式将投票生意业务发送到以太坊之前,原有的用户依然保存自主投票的权利。
    uint nonce;
[5]COMP代币: https://etherscan.io/address/0xc00e94cb662c3520282e6f5717214004a7f26888
[4]Compound: https://learnblockchain.cn/article/1015
    ]
const createDelegateBySigMessage = (compAddress, delegatee, expiry = 10e9, chainId = 1, nonce = 0) => {
    signature.value = sig;
      { name: ‘support’, type: ‘bool’ }
const myWalletAddress = web3.eth.accounts.wallet[0].address;
[3]Tiny熊: https://learnblockchain.cn/people/15
[20]nonce: https://learnblockchain.cn/2019/07/09/4b493e1fb352
      { name: ‘name’, type: ‘string’ },
    } else if (result.error) {
      { name: ‘chainId’, type: ‘uint256’ },
这个布局化数据签名函数可以接管签名地点加上JSON名目标字符串。EIP-712类型界说了需要签名的数据的types, struct和domain。这个实此刻一个简朴的函数里,,在按钮触发事件产生时会被挪用。
    bytes32 s;
  const _expiry = 10e9; // expiration of signature, in seconds since unix epoch 以时间戳样式的签名逾期时间
[16]数据签名: https://docs.metamask.io/guide/signing-data.html
      gasLimit: web3.utils.toHex(1000000),
[10]函数: https://compound.finance/docs/governance#delegate-by-signature
    for (uint i = 0; i < sigs.length; i++) {
}
};
凭据EIP-712[8] 类型界说的布局化数据签名方法,COMP代币[9]持有者可以委托给任何一个地点。任何用户只要有已签名的委托生意业务,都可以挪用COMP智能合约中delegateBySig 函数[10]
  return JSON.stringify({ types, primaryType, domain, message });
  web3.currentProvider.sendAsync({
    bytes32 r,
      { name: ‘verifyingContract’, type: ‘address’ },
    expiry.value = _expiry;
[28]Solidity代码: https://github.com/compound-developers/compound-governance-examples/tree/master/signature-examples/batch-publish-examples
通过剧本可能智能合约打包和宣布签名
3. 以时间戳样式的生意业务逾期时间
  support = !!support;
  const _chainId = web3.currentProvider.networkVersion;
      { name: ‘proposalId’, type: ‘uint256’ },
    }
      console.log
  const { delegatee, nonce, expiry, v, r, s } = signature;
通过签名[6]成果函数的长处是用户可以免费完成委托或投票生意业务,同时会有可信的第三方耗费gas[7]用度将投票功效写到中。在本次教程中,我们重点展示这类函数的例子。
      console.error(‘ERROR’, err);
    Ballot: [
    const sig = result.result;
[32]Adam Bavosa: /@adam.bavosa?source=post_page—–a636c9dfec5e———————-

      { name: ‘name’, type: ‘string’ },
    console.log(‘signature’, sig);
    address delegatee,
    delegatee.value = _delegatee;
    bytes32 s

郑重声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们修改或删除,多谢。