Base58Check 编码


本文章翻译自 Base58Check encoding ,如有侵权,请联系 作者 进行删除


Background

Base58Check 是一种被用来编码比特币地址的协议

这原始比特币源码解释了为何使用base58而不是base64

  • 不想在某些字体中看起来相同的0OIl字符,可用于创建外观相同的帐号
  • 具有非字母数字字符的字符串不像帐号那样容易被接受
  • 如果没有标点符号中断,电子邮件通常不会换行
  • 如果全为字母数字,则双击将整个数字选择为一个单词

Features of Base58Check

Base58Check有如下特点:

  • 一个任意大小的payload
  • 一组58个字母数字符号,由容易区分的大写和小写字母组成
  • 比特币地址使用一个字节保存版本信息, 0x00
  • 基于SHA256的错误检查代码的四个字节(32位)。此代码可用于自动检测并可能纠正印刷错误
  • 一个额外的步骤用来保存数据中的先导零

Creating a Base58Check string

一个base58Check字符串从版本号和附加字段被创建:

    1. 带有版本字节和payload字节,级联在一起
    1. 得到sha256(result of step 1)的前四个字节
    1. 将step1和step2的结果级联在一起
    1. 将step3结果作为一个大端大整数转换为base58字符串
    1. 前导零被保存为字符1,
    1. 级联step5和step4的结果,This is the Base58Check

Encoding a Bitcoin address

比特币地址被实现为Base58Check编码

  • Pay-to-script-hash(p2sh): payload is:RIPEMD160(SHA256(redeemScript)) where redeemScript is a script the wallet knows how to spend; version 0x05(these addresses begin with the digit ‘3’)
  • Pay-to-pubkey-hash (p2pkh): payload is RIPEMD160(SHA256(ECDSA_publicKey)) where ECDSA_publicKey is a public key the wallet knows the private key for; version 0x00 (these addresses begin with the digit ‘1’)

这结果一直是20个字节

code_string = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
x = convert_bytes_to_big_integer(hash_result)

output_string = ""
while(x > 0) {
    x, remainder = divide(x, 58)
    output_string.append(code_string[remainder])
}

repeat(number_of_leading_zero_bytes_in_hash) {
    output_string.append(code_string[0])
}

output_string.reverse()

Version bytes

Reference