Bitcoin blog

The essence of Bitcoin is innovation that be related to remittance, asset holding rights, liberation, and freedom.

Bitcoin address

Overview
Bitcoin address represent receiver of funds,it look like bank account.
The address produced from public key.
Show below example of a bitcoin address:

f:id:adrenaline2017:20170329212453p:plain

Bitcoin address example
15oYSxBcM5deLicKoqwNL2iuAJXpdoc3GL
※27-32 digit alphanumeric characters starting from 1 or 3

Flow
①Public key→SHA256→RIPEMD160→add prefix「0x00」→Let be called A

②A→SHA256→SHA256→prefix 4bytes(checksum)→Let be called B

③Add B behind A→Base58 encode→Bitcoin address

Hash function
The output of fixed bit width is calculated from input.
Always certain output calculated from certain input.
It is also called one-way function,The input can't restoration from output.
f:id:adrenaline2017:20170323093412j:plain

Theoretically, there is the possibility that the same output can be acquired from different inputs.
Bitcoin used SHA256 and RIPEMD160 hash functions that are designed so that 「collision」 unlikely.

Hash160
It's also call「Double hash」,Calculate hash value bySHA256andRIPEMD160.

def bin_hash160(string):
    intermed = hashlib.sha256(string).digest()
    digest = hashlib.new('ripemd160', intermed).digest()
    return digest

intermed = hashlib.sha256(string).digest():
→Hash by SHA256
digest = hashlib.new('ripemd160', intermed).digest():
→Hash by RIPEMD160

Checksum
Checksum is used error check of data transmission, record, replication.
Most simple error detection function.

The decoding software calculates checksum of encoded data,And compare with the included checksum.
If the two do not match, this indicates that an error has been mixed or that ths invalid.

def checksum(inp):
        inp = bytes([0]) + inp
        checksum_prefix = hashlib.sha256(hashlib.sha256(inp).digest()).digest()
        checksum = checksum_prefix[:4]
return checksum

def checksum(inp):
→Arguments use the hashed public key with HASH 160
inp = bytes([0]) + inp
→Add「0x00」to prefix
checksum_prefix = hashlib.sha256(hashlib.sha256(inp).digest()).digest()
→Twice hash to A
checksum = checksum_prefix[:4]
→The prefix 4 bytes is checksum

Decode
Encoded data restore to original form.

def decode(string, base):   
        base = int(base)
        result = 0
        while len(string) > 0:
            result *= base
            result += string[0]
            string = string[1:]
        return result

For example,when binary data is converted(decode) from hexadecimal to decimal.
you look at a character string unit by one character, it can be regarded as Base256 notation.
So each data multiply by 256, add to next data ,Repeat this.

Encode
The orignal fomat convert to different fomat.

def encode(val, base):
        base = int(base)
        code_string = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
        result_bytes = bytes()        
        while val > 0:
            curcode = code_string[val % base]
            result_bytes = bytes([ord(curcode)]) + result_bytes
            val //= base
        result_string = ''.join([chr(y) for y in result_bytes])
        return result_string

curcode = code_string[val % base]
→From remainder of argument "val" specify element of argument "code_string".
result_bytes = bytes([ord(curcode)]) + result_bytes
→It stores bytecode of Base 58 to "result_bytes" in order until "val > 0".
result_string = ''.join([chr(y) for y in result_bytes])
→Convert numbers to strings and coupling them.

Base58check encoding
Base58check encoding specially made for Bitcoin.
It use when necessary to convert binary string into format that can be read and written by a person.
Bitcoin's Base58 alphabet as shown below.

'123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'

Only added one line to the "checksum" code of above.

def bin_to_b58check(inp):
        inp = bytes([0]) + inp
        checksum_prefix = hashlib.sha256(hashlib.sha256(inp).digest()).digest()
        leadingzbytes = 0
        for x in inp:
            if x != 0:
                break
            leadingzbytes += 1
        checksum = checksum_prefix[:4]
        return '1' * leadingzbytes + encode(decode(inp+checksum, 256), 58)

encode(decode(inp+checksum, 256),58)
→ The binary data(Add B behind A) replace from hexadecimal to 58-ary.
leadingzbytes += 1
→It is necessary to record empty byte of leading.
→So put "1" as many as number of empty bytes in head.

Remove all ads