Bitcoin/address validation: Difference between revisions

Content added Content deleted
m (correction)
(corrections, reformulation)
Line 1: Line 1:
{{draft task}}
{{draft task}}


Write a program that takes a [[wp:bitcoin|bitcoin address]] as argument, and checks whether or not this address is valid. The program can either return a boolean value or throw an exception when not valid.
Write a program that takes a [[wp:bitcoin|bitcoin address]] as argument, and checks whether or not this address is valid.


A bitcoin address uses a base58 encoding, which uses an alphabet of the characters 0 .. 9, A ..Z, a .. z, but without the four characters 0, O, I and l.
A bitcoin address uses a base58 encoding, which uses an alphabet of the characters 0 .. 9, A ..Z, a .. z, but without the four characters 0, O, I and l.


With this encoding, a bitcoin address encodes a string 25 bytes:
With this encoding, a bitcoin address encodes 25 bytes:
* the first byte is the version number, which will be zero for this task ;
* the first byte is the version number, which will be zero for this task ;
* the next twenty bytes are a [[RIPEMD-160]] digest, but you don't have to know that for this task: you can consider them a pure arbitrary data ;
* the next twenty bytes are a [[RIPEMD-160]] digest, but you don't have to know that for this task: you can consider them a pure arbitrary data ;
* the last four bytes are a checksum check. They are the first four bytes of a double [[SHA-256]] digest of the previous 21 bytes.
* the last four bytes are a checksum check. They are the first four bytes of a double [[SHA-256]] digest of the previous 21 bytes.


To check the bitcoin address, you must read the first bytes, compute the checksum, and check that it corresponds to the last four bytes.
To check the bitcoin address, you must read the first twenty-one bytes, compute the checksum, and check that it corresponds to the last four bytes.

The program can either return a boolean value or throw an exception when not valid.


You can use a digest library for [[SHA-256]].
You can use a digest library for [[SHA-256]].