zoukankan      html  css  js  c++  java
  • 区块链地址生成算法

    比特币地址

    地址是为了人们交换方便而弄出来的一个方案,因为公钥太长了(130字符串或66字符串)。地址长度为25字节,转为base58编码后,为34或35个字符。base58是类似base64的编码,但去掉了易引起视觉混淆的字符,又在地址末尾添加了4个字节校验位,保障在人们交换个别字符错误时,也能够因地址校验失败而制止了误操作。
    由于存在公钥有两种形式,那么一个公钥便对应两个地址。这两个地址都可由同一私钥签署交易。
    公钥生成地址的算法:

    Version = 1 byte of 0 (zero); on the test network, this is 1 byte of 111
    Key hash = Version concatenated with RIPEMD-160(SHA-256(public key))
    Checksum = 1st 4 bytes of SHA-256(SHA-256(Key hash))
    Bitcoin Address = Base58Encode(Key hash concatenated with Checksum)

    下图是非压缩公钥生成地址的过程:

    对于压缩公钥生成地址时,则只取公钥的X部分即可。

    推导关系

    三者推导关系:私钥 >> 公钥 >> 两个地址。过程均不可逆。拥有私钥便拥有一切,但通常为了方便,会把对应的公钥、地址也存储起来。

    -----------------------以下来源于百度http://zhidao.baidu.com/link?url=-iAaP8yzPO7NI4ruKOtmowXhR4FONK5cdx7IqFWkHuCwA8XFt9I82LOlGAGoJko06gQxs5qI0lKtvr8ndC0Qp76eW0rg_lx7CTf9FVyaF2u ---------------
    比特币地址的生成过程

    (说明: 有些数字以"0x"开头,意思是此数字使用十六进制表示法。"0x"本身没有任何含义,它是C语言流传下来的,约定俗成的写法,比如0xA就是十进制的10。另外,1个字节 = 8位二进制 = 2位十六进制)。

    第一步,随机选取一个32字节的数、大小介于1 ~ 0xFFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFE BAAE DCE6 AF48 A03B BFD2 5E8C D036 4141之间,作为私钥。
    18E14A7B6A307F426A94F8114701E7C8E774E7F9A47E2C2035DB29A206321725

    第二步,使用椭圆曲线加密算法(ECDSA-secp256k1)计算私钥所对应的非压缩公钥。 (共65字节, 1字节 0x04, 32字节为x坐标,32字节为y坐标)关于公钥压缩、非压缩的问题另文说明。

    0450863AD64A87AE8A2FE83C1AF1A8403CB53F53E486D8511DAD8A04887E5B
    23522CD470243453A299FA9E77237716103ABC11A1DF38855ED6F2EE187E9C582BA6

    第三步,计算公钥的 SHA-256 哈希值
    600FFE422B4E00731A59557A5CCA46CC183944191006324A447BDB2D98D4B408

    第四步,取上一步结果,计算 RIPEMD-160 哈希值
    010966776006953D5567439E5E39F86A0D273BEE

    第五步,取上一步结果,前面加入地址版本号(比特币主网版本号“0x00”)
    00010966776006953D5567439E5E39F86A0D273BEE

    第六步,取上一步结果,计算 SHA-256 哈希值
    445C7A8007A93D8733188288BB320A8FE2DEBD2AE1B47F0F50BC10BAE845C094

    第七步,取上一步结果,再计算一下 SHA-256 哈希值(哈哈)
    D61967F63C7DD183914A4AE452C9F6AD5D462CE3D277798075B107615C1A8A30

    第八步,取上一步结果的前4个字节(8位十六进制)
    D61967F6

    第九步,把这4个字节加在第五步的结果后面,作为校验(这就是比特币地址的16进制形态)。
    00010966776006953D5567439E5E39F86A0D273BEED61967F6

    第十步,用base58表示法变换一下地址(这就是最常见的比特币地址形态)。

    16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM

    ------------------------------来自于以太坊黄皮书-------------------------------------------------------
    以太坊地址生成算法


    ------------------------------来自于以太坊白皮书------------------------------------------------------------------------
    https://github.com/ethereum/wiki/wiki/White-Paper#ethereum-accounts

    Ethereum Accounts

    In Ethereum, the state is made up of objects called "accounts", with each account having a 20-byte address and state transitions being direct transfers of value and information between accounts. 
    以太坊中,账户包含20字节的地址,以及账户之间价值和信息的传递的状态转换。

    An Ethereum account contains four fields:
    The nonce, a counter used to make sure each transaction can only be processed once
    The account's current ether balance
    The account's contract code, if present
    The account's storage (empty by default)
    以太坊账户包含4个部分:
    nonce 是一个计数器用于确保每笔交易只被处理一次
    账户当前以太币数量
    合约代码
    以太坊存储空间?

    "Ether" is the main internal crypto-fuel of Ethereum, and is used to pay transaction fees. 
    以太币是以太坊中的主要密码货币,用于支付交易费用

    In general, there are two types of accounts: externally owned accounts, controlled by private keys, and contract accounts, controlled by their contract code. An externally owned account has no code, and one can send messages from an externally owned account by creating and signing a transaction; in a contract account, every time the contract account receives a message its code activates, allowing it to read and write to internal storage and send other messages or create contracts in turn.
    以太坊中有两种账户:外部用户的账户,通过私钥控制;智能合约账户,通过合约代码控制。
    外部用户账户不包含代码,用户可以通过创建和签署交易来给外部用户账户发送信息。
    合约账户,每次接受到信息时,合约代码开始工作,允许读取、写入内部存储,已结发送信息,或者反过来创建合约。

    Note that "contracts" in Ethereum should not be seen as something that should be "fulfilled" or "complied with"; rather, they are more like "autonomous agents" that live inside of the Ethereum execution environment, always executing a specific piece of code when "poked" by a message or transaction, and having direct control over their own ether balance and their own key/value store to keep track of persistent variables.
    注意以太坊中的合约不应该被视为某种可以被完成,被遵守的恭喜。它们更像是以太坊环境的自治代理机构。能够直接控制它们的以太币和秘钥来追踪持续变量。

  • 相关阅读:
    函数之返回值
    函数之初识函数
    三元运算符
    枚举enumerate
    模块
    迭代器
    斐波那契
    leetcode155 最小栈
    leetcode94 二叉树的中序遍历
    leetcode20 有效的括号
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13313027.html
Copyright © 2011-2022 走看看