白名单认证 solidity 代码

视频参考:https://www.youtube.com/watch?v=vYwYe-Gv_XI

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.10;

// 0. message to sign

// 1. hash(message)

// 2.sign(hash(message),private key) | offchain

// 3. ecrecover(hass(message),signature) == signer

contract verifyWhiteList{

function verify(

address _signer,

string memory _message,

bytes memory signature

) public pure returns (bool) {

bytes32 messageHash = getMessageHash( _message);

bytes32 ethSignedMessageHash = getEthSignedMessageHash(messageHash);

return recoverSigner(ethSignedMessageHash, signature) == _signer;

}

function getMessageHash(

string memory _message

) public pure returns (bytes32) {

return keccak256(abi.encodePacked(_message));

}

function getEthSignedMessageHash(bytes32 _messageHash)

public

pure

returns (bytes32)

{

/*

Signature is produced by signing a keccak256 hash with the following format:

"\x19Ethereum Signed Message\n" + len(msg) + msg

*/

return

keccak256(

abi.encodePacked("\x19Ethereum Signed Message:\n32", _messageHash)

);

}

function recoverSigner(bytes32 _ethSignedMessageHash, bytes memory _signature)

public

pure

returns (address)

{

(bytes32 r, bytes32 s, uint8 v) = splitSignature(_signature);

return ecrecover(_ethSignedMessageHash, v, r, s);

}

function splitSignature(bytes memory sig)

internal pure returns (

bytes32 r,

bytes32 s,

uint8 v

)

{

require(sig.length == 65, "invalid signature length");

assembly {

/*

First 32 bytes stores the length of the signature

add(sig, 32) = pointer of sig + 32

effectively, skips first 32 bytes of signature

mload(p) loads next 32 bytes starting at the memory address p into memory

*/

// first 32 bytes, after the length prefix

r := mload(add(sig, 32))

// second 32 bytes

s := mload(add(sig, 64))

// final byte (first byte of the next 32 bytes)

v := byte(0, mload(add(sig, 96)))

}

// implicitly return (r, s, v)

}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值