函数签名Function Signature

功能签名

    函数签名由函数原型组成。它告诉你的是关于函数的一般信息,它的名称,参数,它的范围以及其他杂项信息。 C ++“mangle”的函数名称使它们很漂亮,尽管事实上它们可能非常难看。另一方面,不管是否存在错误,函数签名中存在大量信息(存储在大多数编译器内部),这些信息不易获得,但可以访问。考虑以下功能:

  static const int&Foo :: Run(World&W,PrioQ&pq)const;
---这是相同的(虽然这个函数的签名在上面......因为这是你必须用它来定义它。):
  class Foo {
    
     
      static const int&Run(World&W,PrioQ&pq)const;
    
  };

功能签名是上面的那个。如何知道这一点,因为为了定义函数,必须使用签名...虽然上述签名是什么意思?

  static const int&Foo :: Run(World&W,PrioQ&pq)const;

static  - 这个函数只能在这个文件中看到?不,这意味着可以在没有实例化对象的情况下调用此函数,因为通常必须使用类的实例化来调用成员函数(方法),但是使用此关键字,您不需要它。

const  - 此函数返回的int无法更改,因为此函数返回对int的引用,您通常可以更改它,但此特定函数不允许更改。

int& - 这是函数的返回类型。这个特殊的函数返回一个int,但不仅如此,它是对int的引用,因此它实际上不是一个int,而是表现为一个。

Foo ::  - 这个函数实际上是在范围Foo中声明的函数,它是一个类。

运行 - 函数的名称。

World& - 第一个参数的类型。这对于大型对象来说是典型的,因为如果它们是按值传递的,那么必须复制整个对象(可能很大)(如果对象很大,复制它可能需要很长时间)并且引用允许一个修改传入的对象。

W  - 这是函数用于引用它的第一个参数的名称。

PrioQ& - 这是第二个参数的类型。这对于大型对象来说是典型的,因为如果它们是按值传递的,那么必须复制整个对象(可能很大)(如果对象很大,复制它可能需要很长时间)并且引用允许一个修改传入的对象。

pq  - 这是第二个参数的名称,该函数将用于引用它。

const  - 虽然这看起来像这个函数前面的const,但它不一样。函数末尾的const与开头的const不同。此const阻止此函数更改它所属的类的成员变量(在本例中为Foo)。

    A function signature consists of the function prototype.  What it tells you is the general information about a function, its name, parameters, what scope it is in, and other miscellaneous information.  C++ "mangle"s function names so that they are pretty, though in all truth they can be very ugly.  On the other hand, mangling or not there is alot of information in a function signature (which is stored internally to most compilers) which is not readily available, but can be accessed.  Consider the following function:

  static const int & Foo::Run(World& W, PrioQ& pq) const; 
--- which is the same as (though this function's signature is above...as this is what you must use to define it.): 
  class Foo { 
    public: 
    -- majorly snipped of code-- 
      static const int & Run(World& W, PrioQ & pq) const; 
    -- more snipping... -- 
  };

The function signature is the one above.  How does one know this, because in order to define the function, one must use the signature...  Though what does the above signature mean?

  static const int & Foo::Run(World& W, PrioQ& pq) const;

static -- this function can only be seen in this file?  No, this means that this function can be called without an instantiated object, as normally member functions (methods) must be called using an instantiation of the class, though with this keyword, you don't need it.

const -- the int that this function returns cannot be changed, because this function returns a reference to an int, you could normally change it, but this particular function does not allow that.

int & -- This is the return type of the function.  This particular function returns an int, but not only that, but it is a reference to an int, so it is not really an int, but behaves as one.

Foo:: -- This function is really a function declared in the scope Foo, which is a class.

Run -- The name of the function.

World& -- The type of the first argument.  This is typical for large objects, as, if they are passed by value, then the entire object (which can be huge) must be copied (if the object is huge, copying it can take a long time) and the reference allows one to modify the object being passed in.

W -- This is the name of the first parameter that the function will use to refer to it.

PrioQ & -- This is the type of the second argument.  This is typical for large objects, as, if they are passed by value, then the entire object (which can be huge) must be copied (if the object is huge, copying it can take a long time) and the reference allows one to modify the object being passed in.

pq -- This is the name of the second parameter, that the function will use to refer to it.

const -- Though this looks like the const on the front of this function, it is not the same.  The const on the end of the function is not the same as the const at the beginning.  This const prevents this function from changing the member variables of the class to which it belongs (in this case Foo).

 

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
`permit`函数需要传入签名的v、r、s值,这些值可以通过以下步骤获取: 1. 使用代币持有者的私钥对授权信息进行签名,得到一个签名数据。 2. 使用签名数据提取出v、r、s值。 具体步骤如下: 1. 组装签名数据 签名数据需要包含以下字段: - `address owner`: 代币持有者地址。 - `address spender`: 授权的代币接收地址。 - `uint256 value`: 授权的代币数量。 - `uint256 nonce`: 代币持有者的nonce值,用于防止重放攻击。 - `uint256 deadline`: 授权的截止时间戳,用于提高安全性。 - `uint256 chainId`: 链ID,用于区分不同的链。 - `address tokenAddress`: 代币合约地址。 具体代码如下: ```solidity function getPermitHash( address owner, address spender, uint256 value, uint256 nonce, uint256 deadline, uint256 chainId, address tokenAddress ) public pure returns (bytes32) { bytes32 hash = keccak256( abi.encodePacked( bytes1(0x19), bytes1(0x01), chainId, tokenAddress, keccak256( abi.encode( "permit", owner, spender, value, nonce, deadline ) ) ) ); return hash; } ``` 其中,`chainId`是链ID,用于区分不同的链。`tokenAddress`是代币合约地址。`nonce`是代币持有者的nonce值,可以从`ERC20`合约的`nonce`函数获取。 2. 对签名数据进行签名 使用代币持有者的私钥对签名数据进行签名,得到一个签名数据。可以使用钱包或其他工具实现签名功能。 3. 提取v、r、s值 使用签名数据提取出v、r、s值,可以使用以下代码实现: ```solidity function splitSignature(bytes memory signature) public pure returns (uint8 v, bytes32 r, bytes32 s) { require(signature.length == 65, "invalid signature length"); assembly { r := mload(add(signature, 32)) s := mload(add(signature, 64)) v := byte(0, mload(add(signature, 96))) } if (v < 27) { v += 27; } require(v == 27 || v == 28, "invalid signature value"); return (v, r, s); } ``` 例如,以下是一个完整的获取签名数据的示例代码: ```solidity IERC20 token = IERC20(0x123456...); // 代币合约地址 address owner = 0xabcdef...; // 代币持有者地址 address spender = 0x789abc...; // 授权的代币接收地址 uint256 value = 100; // 授权的代币数量 uint256 deadline = block.timestamp + 3600; // 授权的截止时间戳 uint256 nonce = token.nonces(owner); // 代币持有者的nonce值 uint256 chainId = 1; // 链ID bytes32 hash = getPermitHash(owner, spender, value, nonce, deadline, chainId, address(token)); // 获取签名hash bytes memory signature = signMessage(hash, ownerPrivateKey); // 对签名hash进行签名 (uint8 v, bytes32 r, bytes32 s) = splitSignature(signature); // 提取v、r、s值 token.permit(owner, spender, value, deadline, v, r, s); // 调用permit函数 ``` 其中,`signMessage`函数是使用代币持有者的私钥对签名hash进行签名函数

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值