Bitcoin/public point to address: Difference between revisions

Content deleted Content added
Thundergnat (talk | contribs)
m Automated syntax highlighting fixup (second round - minor fixes)
PSNOW123 (talk | contribs)
m Realigning text comments.
 
(6 intermediate revisions by 3 users not shown)
Line 98:
return 0;
}</syntaxhighlight>
 
=={{header|C++}}==
This example uses the C++ code from the SHA-256 and RIPEMD160 tasks. This slightly complicates the code because
the previous tasks were designed to hash a string of ASCII characters rather than a byte array. However, it
enables the task to be completed without the use of any external libraries.
<syntaxhighlight lang="c++">
#include <cstdint>
#include <iostream>
#include <map>
#include <string>
#include <vector>
 
#include "SHA256.cpp"
#include "RIPEMD160.cpp"
SHA256 sha256{ };
RIPEMD160 ripemd160{ };
 
const std::string BITCOIN_SPECIAL_VALUE = "04";
const std::string BITCOIN_VERSION_NUMBER = "00";
 
std::map<char, uint32_t> base_map =
{ { '0', 0 }, { '1', 1 }, { '2', 2 }, { '3', 3 }, { '4', 4 }, { '5', 5 }, { '6', 6 }, { '7', 7 },
{ '8', 8 }, { '9', 9 }, { 'a', 10 }, { 'b', 11 }, { 'c', 12 }, { 'd', 13 }, { 'e', 14 }, { 'f', 15 },
{ 'A', 10 }, { 'B', 11 }, { 'C', 12 }, { 'D', 13 }, { 'E', 14 }, { 'F', 15 } };
 
std::vector<uint32_t> hex_to_bytes(const std::string& text) {
std::vector<uint32_t> bytes(text.size() / 2, 0);
for ( uint64_t i = 0; i < text.size(); i += 2 ) {
const uint32_t first_digit = base_map[text[i]];
const uint32_t second_digit = base_map[text[i + 1]];
bytes[i / 2] = ( first_digit << 4 ) + second_digit;
}
return bytes;
}
 
std::string vector_to_ascii_string(const std::vector<uint32_t>& bytes) {
std::string result = "";
for ( uint64_t i = 0; i < bytes.size(); ++i ) {
result += static_cast<char>(bytes[i]);
}
return result;
}
 
std::vector<uint32_t> compute_message_bytes(const std::string& text) {
// Convert the hexadecimal string 'text' into a suitable ASCII string for the SHA256 hash
std::vector<uint32_t> bytes_1 = hex_to_bytes(text);
std::string ascii_1 = vector_to_ascii_string(bytes_1);
std::string hexSHA256 = sha256.message_digest(ascii_1);
// Convert the hexadecimal string 'hexSHA256' into a suitable ASCII string for the RIPEMD160 hash
std::vector<uint32_t> bytes_2 = hex_to_bytes(hexSHA256);
std::string ascii_2 = vector_to_ascii_string(bytes_2);
std::string hexRIPEMD160 = BITCOIN_VERSION_NUMBER + ripemd160.message_digest(ascii_2);
return hex_to_bytes(hexRIPEMD160);
}
 
std::vector<uint32_t> compute_checksum(const std::vector<uint32_t>& bytes) {
// Convert the given byte array into a suitable ASCII string for the first SHA256 hash
std::string ascii_1 = vector_to_ascii_string(bytes);
std::string hex_1 = sha256.message_digest(ascii_1);
// Convert the hexadecimal string 'hex1' into a suitable ASCII string for the second SHA256 hash
std::vector<uint32_t> bytes_1 = hex_to_bytes(hex_1);
std::string ascii_2 = vector_to_ascii_string(bytes_1);
std::string hex_2 = sha256.message_digest(ascii_2);
std::vector<uint32_t> bytes_2 = hex_to_bytes(hex_2);
std::vector<uint32_t> result(bytes_2.begin(), bytes_2.begin() + 4);
return result;
}
 
// Return the given byte array encoded into a base58 starting with most one '1'
std::string encode_base_58(std::vector<uint32_t> bytes) {
const std::string ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
const uint32_t ALPHABET_SIZE = ALPHABET.size();
 
std::string result(34, ' ');
for ( int64_t n = result.size() - 1; n >= 0; --n ) {
uint32_t c = 0;
for ( uint64_t i = 0; i < bytes.size(); ++i ) {
c = c * 256 + bytes[i];
bytes[i] = c / ALPHABET_SIZE;
c %= ALPHABET_SIZE;
}
result[n] = ALPHABET[c];
}
 
while ( result.starts_with("11") ) {
result = result.substr(1);
}
return result;
}
 
// Return the encoded address of the given coordinates.
std::string encode_address(const std::string& x, const std::string& y) {
std::string public_point = BITCOIN_SPECIAL_VALUE + x + y;
if ( public_point.size() != 130 ) {
throw std::invalid_argument("Invalid public point string");
}
 
std::vector<uint32_t> message_bytes = compute_message_bytes(public_point);
std::vector<uint32_t> checksum = compute_checksum(message_bytes);
message_bytes.insert(message_bytes.end(), checksum.begin(), checksum.end());
return encode_base_58(message_bytes);
}
 
int main() {
std::string x = "50863AD64A87AE8A2FE83C1AF1A8403CB53F53E486D8511DAD8A04887E5B2352";
std::string y = "2CD470243453A299FA9E77237716103ABC11A1DF38855ED6F2EE187E9C582BA6";
 
std::cout << encode_address(x, y) << std::endl;
}
</syntaxhighlight>
{{ out }}
<pre>
16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM
</pre>
 
=={{header|Common Lisp}}==
{{libheader|Quicklisp}}
Line 613 ⟶ 728:
<pre>"6UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM"
</pre>
 
=={{header|Java}}==
This example uses the Java code from the SHA-256 and RIPEMD-160 tasks. This slightly complicates the code because
the two previous tasks were designed to hash a string of ASCII characters rather than a byte array. However, it
enables the task to be completed without the use of any external libraries.
<syntaxhighlight lang="java">
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.stream.Collectors;
 
public final class BitcoinPublicPointToAddess {
 
public static void main(String[] args) {
String x = "50863AD64A87AE8A2FE83C1AF1A8403CB53F53E486D8511DAD8A04887E5B2352";
String y = "2CD470243453A299FA9E77237716103ABC11A1DF38855ED6F2EE187E9C582BA6";
if ( areValidCoordinates(x, y) ) {
System.out.println(encodeAddress(x, y));
} else {
System.out.println("Invalid Bitcoin public point coordinates");
}
}
// Return the encoded address of the given coordinates.
private static String encodeAddress(String x, String y) {
String publicPoint = BITCOIN_SPECIAL_VALUE + x + y;
if ( publicPoint.length() != 130 ) {
throw new AssertionError("Invalid public point string: " + publicPoint);
}
 
byte[] messageBytes = computeMessageBytes(publicPoint);
byte[] checksum = computeChecksum(messageBytes);
messageBytes = Arrays.copyOf(messageBytes, messageBytes.length + 4);
System.arraycopy(checksum, 0, messageBytes, 21, checksum.length);
return encodeBase58(messageBytes);
}
// Return the given byte array encoded into a base58 starting with most one '1'
private static String encodeBase58(byte[] bytes) {
final String ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
final int ALPHABET_SIZE = ALPHABET.length();
String[] temp = new String[34];
for ( int n = temp.length - 1; n >= 0; n-- ) {
int c = 0;
for ( int i = 0; i < bytes.length; i++ ) {
c = c * 256 + (int) ( bytes[i] & 0xFF );
bytes[i] = (byte) ( c / ALPHABET_SIZE );
c %= ALPHABET_SIZE;
}
temp[n] = ALPHABET.substring(c, c + 1);
}
String result = Arrays.stream(temp).collect(Collectors.joining(""));
while ( result.startsWith("11") ) {
result = result.substring(1);
}
return result;
}
// Return whether the given coordinates are those of a point on the secp256k1 elliptic curve
private static boolean areValidCoordinates(String x, String y) {
BigInteger modulus = new BigInteger("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F", 16);
BigInteger X = new BigInteger(x, 16);
BigInteger Y = new BigInteger(y, 16);
return Y.multiply(Y).mod(modulus).equals(X.multiply(X).multiply(X).add(BigInteger.valueOf(7)).mod(modulus));
}
private static byte[] computeMessageBytes(String text) {
// Convert the hexadecimal string 'text' into a suitable ASCII string for the SHA256 hash
byte[] bytesOne = hexToBytes(text);
String asciiOne = new String(bytesOne, StandardCharsets.ISO_8859_1);
String hexSHA256 = SHA256.messageDigest(asciiOne);
// Convert the hexadecimal string 'hexSHA256' into a suitable ASCII string for the RIPEMD160 hash
byte[] bytesTwo = hexToBytes(hexSHA256);
String asciiTwo = new String(bytesTwo, StandardCharsets.ISO_8859_1);
String hexRIPEMD160 = BITCOIN_VERSION_NUMBER + RIPEMD160.messageDigest(asciiTwo);
return hexToBytes(hexRIPEMD160);
}
private static byte[] computeChecksum(byte[] bytes) {
// Convert the given byte array into a suitable ASCII string for the first SHA256 hash
String asciiOne = new String(bytes, StandardCharsets.ISO_8859_1);
String hexOne = SHA256.messageDigest(asciiOne);
// Convert the hexadecimal string 'hex1' into a suitable ASCII string for the second SHA256 hash
byte[] bytesOne = hexToBytes(hexOne);
String asciiTwo = new String(bytesOne, StandardCharsets.ISO_8859_1);
String hexTwo = SHA256.messageDigest(asciiTwo);
return Arrays.copyOfRange(hexToBytes(hexTwo), 0, 4);
}
private static byte[] hexToBytes(String text) {
byte[] bytes = new byte[text.length() / 2];
for ( int i = 0; i < text.length(); i += 2 ) {
final int firstDigit = Character.digit(text.charAt(i), 16);
final int secondDigit = Character.digit(text.charAt(i + 1), 16);
bytes[i / 2] = (byte) ( ( firstDigit << 4 ) + secondDigit );;
}
return bytes;
}
private static final String BITCOIN_SPECIAL_VALUE = "04";
private static final String BITCOIN_VERSION_NUMBER = "00";
}
</syntaxhighlight>
{{ out }}
<pre>
16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM
</pre>
 
=={{header|Julia}}==
{{works with|Julia|0.6}}
Line 1,076 ⟶ 1,302:
<syntaxhighlight lang="rust">
use ring::digest::{digest, SHA256};
use ripemd160ripemd::{Digest, Ripemd160};
 
use hex::FromHex;
Line 1,146 ⟶ 1,372:
16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM
</pre>
 
=={{header|Seed7}}==
The Seed7 library [http://seed7.sourceforge.net/libraries/msgdigest.htm msgdigest.s7i] defines
Line 1,240 ⟶ 1,467:
{{libheader|Wren-str}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./crypto" for Sha256, Ripemd160
import "./str" for Str
import "./fmt" for Conv
 
// converts an hexadecimal string to a byte list.
Line 1,340 ⟶ 1,567:
16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM
</pre>
 
=={{header|zkl}}==
Uses shared library zklMsgHash.