How to resolve the algorithm Bitcoin/public point to address step by step in the Ruby programming language
How to resolve the algorithm Bitcoin/public point to address step by step in the Ruby programming language
Table of Contents
Problem Statement
Bitcoin uses a specific encoding format to encode the digest of an elliptic curve public point into a short ASCII string. The purpose of this task is to perform such a conversion. The encoding steps are: The base-58 encoding is based on an alphabet of alphanumeric characters (numbers, upper case and lower case, in that order) but without the four characters 0, O, l and I. Here is an example public point: The corresponding address should be: 16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM Nb. The leading '1' is not significant as 1 is zero in base-58. It is however often added to the bitcoin address for various reasons. There can actually be several of them. You can ignore this and output an address without the leading 1. Extra credit: add a verification procedure about the public point, making sure it belongs to the secp256k1 elliptic curve
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Bitcoin/public point to address step by step in the Ruby programming language
The provided Ruby code is a function that translates a public point on the Bitcoin elliptic curve to a Bitcoin address. Here's a step-by-step explanation of the code:
-
Constants and Input:
- The code defines two constants,
X
andY
, which represent the coordinates of a public point on the Bitcoin elliptic curve. n
is calculated by concatenating '00' with the hexadecimal digest of the double SHA-256 hash of the converted public point.
- The code defines two constants,
-
Base58 Encoding:
- The code uses the
convert
function to convert the public point coordinates into a string of hexadecimal digits. This string is then packed into a binary format according to the specification provided by thei
string. - The resulting binary string is encoded using Base58, which is a modified version of Base64 that doesn't use the ambiguous characters '0' (zero), 'O' (uppercase o), 'I' (uppercase i), and '+' (plus). This encoded string is stored in the
res
variable.
- The code uses the
-
Loop and Reverse:
- The code enters a loop that repeatedly divides the
n
value by 58 and takes the remainder. - The character corresponding to the remainder is appended to the
res
string. - This loop continues until
n
becomes 0. - Finally, the
res
string is reversed, and the Bitcoin address is printed.
- The code enters a loop that repeatedly divides the
In summary, this code converts a public point on the Bitcoin elliptic curve to a Bitcoin address using double SHA-256 hashing and Base58 encoding. The resulting address is a string that represents the cryptocurrency wallet where bitcoins can be sent or received.
Source code in the ruby programming language
# Translate public point to Bitcoin address
#
# Nigel_Galloway
# October 12th., 2014
require 'digest/sha2'
def convert g
i,e = '',[]
(0...g.length/2).each{|n| e[n] = g[n+=n]+g[n+1]; i+='H2'}
e.pack(i)
end
X = '50863AD64A87AE8A2FE83C1AF1A8403CB53F53E486D8511DAD8A04887E5B2352'
Y = '2CD470243453A299FA9E77237716103ABC11A1DF38855ED6F2EE187E9C582BA6'
n = '00'+Digest::RMD160.hexdigest(Digest::SHA256.digest(convert('04'+X+Y)))
n+= Digest::SHA256.hexdigest(Digest::SHA256.digest(convert(n)))[0,8]
G = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
n,res = n.hex,''
while n > 0 do
n,ng = n.divmod(58)
res << G[ng]
end
puts res.reverse
You may also check:How to resolve the algorithm Hilbert curve step by step in the Racket programming language
You may also check:How to resolve the algorithm Multiplication tables step by step in the Lambdatalk programming language
You may also check:How to resolve the algorithm Visualize a tree step by step in the Java programming language
You may also check:How to resolve the algorithm Variables step by step in the uBasic/4tH programming language
You may also check:How to resolve the algorithm Roman numerals/Encode step by step in the Zoea programming language