https://solidity.readthedocs.io/en/latest/units-and-global-variables.html#mathematical-and-cryptographic-functions

ecrecover(bytes32 hash, uint8 v, bytes32 r, bytes32 s) returns (address):
recover the address associated with the public key from elliptic curve signature or return zero on error (example usage)

It might be that you run into Out-of-Gas for sha256ripemd160 or ecrecover on a private blockchain. The reason for this is that those are implemented as so-called precompiled contracts and these contracts only really exist after they received the first message (although their contract code is hardcoded). Messages to non-existing contracts are more expensive and thus the execution runs into an Out-of-Gas error. A workaround for this problem is to first send e.g. 1 Wei to each of the contracts before you use them in your actual contracts. This is not an issue on the official or test net.

ecrecover的思想是,可以计算对应于用于创建ECDSA签名的私钥的公钥,这两个额外的字节通常是由签名提供的。签名本身是椭圆曲线点R和S的两个(编码),而V是恢复公钥所需的两个附加位。

这也解释了为什么返回类型是地址:它返回对应于恢复的公钥(即其sha3/keccak的哈希)的地址。这意味着要实际验证签名,检查返回的地址是否等于相应的私钥应该已经签署哈希的那个地址。

找到了这个接口的代码:

// https://github.com/ethereum/go-ethereum/wiki/Management-APIs#personal_ecRecover
func (s *PrivateAccountAPI) EcRecover(ctx context.Context, data, sig hexutil.Bytes) (common.Address, error) {
if len(sig) != {
return common.Address{}, fmt.Errorf("signature must be 65 bytes long")
}
if sig[] != && sig[] != {
return common.Address{}, fmt.Errorf("invalid Ethereum signature (V is not 27 or 28)")
}
sig[] -= // Transform yellow paper V from 27/28 to 0/1 rpk, err := crypto.Ecrecover(signHash(data), sig)
if err != nil {
return common.Address{}, err
}
pubKey := crypto.ToECDSAPub(rpk)
recoveredAddr := crypto.PubkeyToAddress(*pubKey)
return recoveredAddr, nil
}

从这里我们可以看见,如果你使用web3来调用geth,你的v只有27,28两种选择,不会有分叉中的35,36

https://github.com/ethereum/go-ethereum/blob/55599ee95d4151a2502465e0afc7c47bd1acba77/internal/ethapi/api.go#L452-L459

这是所有api接口的内部实现代码,go-ethereum/internal/ethapi/api.go

solidity ecrecover的更多相关文章

  1. 智能合约语言 Solidity 教程系列8 - Solidity API

    这是Solidity教程系列文章第8篇介绍Solidity API,它们主要表现为内置的特殊的变量及函数,存在于全局命名空间里. 写在前面 Solidity 是以太坊智能合约编程语言,阅读本文前,你应 ...

  2. solidity learning (1)

    学习文档笔记:http://solidity-cn.readthedocs.io/zh/develop/layout-of-source-files.html 1.pragma solidity ^0 ...

  3. solidity 汇编语言问题——内存数据mload时为什么从第32位开始

    问题:内存数据mload时为什么从第32位开始 代码出处:https://gist.github.com/axic/5b33912c6f61ae6fd96d6c4a47afde6d pragma so ...

  4. 以太坊系列之十四: solidity特殊函数

    solidity中的特殊函数 括号里面有类型和名字的是参数,只有类型是返回值. block.blockhash(uint blockNumber) returns (bytes32): hash of ...

  5. 安装solidity遇见的问题——unused variable 'returned'

    在编译安装solidity的过程中遇见了一个很奇怪的问题 webthree-umbrella/libethereum/libethereum/Executive.cpp: In member func ...

  6. Solidity教程系列1 - 类型介绍

    现在的Solidity中文文档,要么翻译的太烂,要么太旧,决定重新翻译下,再加上代码事例讲解. 写在前面 Solidity是以太坊智能合约编程语言,阅读本文前,你应该对以太坊.智能合约有所了解, 如果 ...

  7. 智能合约语言Solidity教程系列2 - 地址类型介绍

    智能合约语言Solidity教程系列第二篇 - Solidity地址类型介绍. 写在前面 Solidity是以太坊智能合约编程语言,阅读本文前,你应该对以太坊.智能合约有所了解,如果你还不了解,建议你 ...

  8. 智能合约语言 Solidity 教程系列3 - 函数类型

    Solidity 教程系列第三篇 - Solidity 函数类型介绍. 写在前面 Solidity 是以太坊智能合约编程语言,阅读本文前,你应该对以太坊.智能合约有所了解,如果你还不了解,建议你先看以 ...

  9. 智能合约语言 Solidity 教程系列4 - 数据存储位置分析

    写在前面 Solidity 是以太坊智能合约编程语言,阅读本文前,你应该对以太坊.智能合约有所了解, 如果你还不了解,建议你先看以太坊是什么 这部分的内容官方英文文档讲的不是很透,因此我在参考Soli ...

随机推荐

  1. ajax+ashx:实现文件的批量导出

    背景: 最近公司有一个需求,就是实现excle的批量导出(一次性导出多个excle). 实现方式: 想到的实现方式: 1.发起一个导出请求,然后批量生产需要导出的excle文件,最后将文件生成一个压缩 ...

  2. 【读书笔记】iOS-微定位技术

    在大型商场,医院或是大楼里,你是否曾经有过找不到想去的地方的经历呢?这种情况下采用传统的定位方法就有些力不从心了.首先这些地方不能采用GPS定们,而Wifi和蜂窝式移动电话基站定位误差比较大.这种情况 ...

  3. 如今领占主导地位的19种AI技术!

    如今领占主导地位的19种AI技术! http://blog.itpub.net/31542119/viewspace-2212797/ 深度学习的突破将人工智能带进全新阶段. 2006 年-2015 ...

  4. Flutter 布局(六)- SizedOverflowBox、Transform、CustomSingleChildLayout详解

    本文主要介绍Flutter布局中的SizedOverflowBox.Transform.CustomSingleChildLayout三种控件,详细介绍了其布局行为以及使用场景,并对源码进行了分析. ...

  5. wap2app(六)-- wap2app的原生标题头无法隐藏

    大概有一个星期没有用HBuilder打包,今天更新了HBuilder版本再继续打包,就出现了一系列问题.其中有一个就是打包之后,除首页外,其他页面多出了原生标题,如下图: 查阅资料,打开 sitema ...

  6. python第一百零二天-----第十七周作业

    由于内容众多 直接使用 git 链接 : https://github.com/uge3/hosts_masg 主机管理WEB页面 使用 SQLALchemy 主机管理(8列) ip 用户表: 用户名 ...

  7. python第六十一天,第六十二天 redis

    redis 缓存系统 redis是业界主流的key-value nosql 数据库之一.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).se ...

  8. linux下安装mysql简单步骤

    linux下使用yum安装mysql 1.安装 查看有没有安装过: yum list installed mysql* rpm -qa | grep mysql* 查看有没有安装包: yum list ...

  9. Python基础知识:列表

    1.pop(i)删除列表任意位置元素,并继续使用它,如果括号为空,默认删除末尾 #pop()函数 guests=['Liming','Liuhan','Hanjieming'] for guest i ...

  10. 【Excel】SUMIF函数的兼容性

    兼容性非常强的两个函数 SUMIF() 说兼容性,当然得说SUMIF了. 来,我们先举个例子. 现有一个表格,算起来只有"科目划分"."发生额"两列内容,但是折 ...