Bitcoin/public point to address: Difference between revisions

m
syntax highlighting fixup automation
(→‎{{header|Raku}}: use openssl for digests and simplify code slightly)
m (syntax highlighting fixup automation)
Line 24:
 
=={{header|C}}==
<langsyntaxhighlight lang=c>#include <stdio.h>
#include <string.h>
#include <ctype.h>
Line 98:
0));
return 0;
}</langsyntaxhighlight>
 
=={{header|Common Lisp}}==
{{libheader|Quicklisp}}
{{libheader|Ironclad}}
<langsyntaxhighlight lang=lisp>
;;;; This is a revised version, inspired by a throwaway script originally
;;;; published at http://deedbot.org/bundle-381528.txt by the same Adlai.
Line 206:
(base58enc (checksum (sovcat '(0) (digest '(ripemd-160 sha256) point)))))))
 
</syntaxhighlight>
</lang>
 
Here's an example of how to feed a point into the functions defined above:
 
<syntaxhighlight lang=text>
 
;; ? (pubkey-to-p2pkh
Line 218:
;; "16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM"
 
</syntaxhighlight>
</lang>
 
=={{header|D}}==
Line 224:
{{trans|C}}
{{trans|Go}}
<langsyntaxhighlight lang=d>import std.stdio, std.algorithm, std.digest.ripemd, sha_256_2;
 
// A Bitcoin public point.
Line 281:
 
p.bitcoinEncode.writeln;
}</langsyntaxhighlight>
{{out}}
<pre>16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM</pre>
Line 290:
{{libheader| DCPripemd160}}
{{Trans|Go}}
<langsyntaxhighlight lang=Delphi>
program Public_point_to_address;
 
Line 453:
writeln(a.ToBase58);
readln;
end.</langsyntaxhighlight>
 
=={{header|Factor}}==
<langsyntaxhighlight lang=factor>USING: checksums checksums.ripemd checksums.sha io.binary kernel
math sequences ;
IN: rosetta-code.bitcoin.point-address
Line 478:
dup 0 prefix btc-checksum
append 0 prefix >base58 ;
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 488:
=={{header|Go}}==
{{libheader|Go sub-repositories}}
<langsyntaxhighlight lang=go>package main
 
import (
Line 588:
// show base58 representation
fmt.Println(string(a.A58()))
}</langsyntaxhighlight>
{{out}}
<pre>
Line 595:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang=haskell>import Numeric (showIntAtBase)
import Data.List (unfoldr)
import Data.Binary (Word8)
Line 615:
0x50863AD64A87AE8A2FE83C1AF1A8403CB53F53E486D8511DAD8A04887E5B2352
0x2CD470243453A299FA9E77237716103ABC11A1DF38855ED6F2EE187E9C582BA6
</syntaxhighlight>
</lang>
{{out}}
<pre>"6UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM"
Line 625:
 
'''Main functions''':
<langsyntaxhighlight lang=julia>const digits = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
function encodebase58(b::Vector{<:Integer})
out = Vector{Char}(34)
Line 652:
end
pubpoint2address(x::AbstractString, y::AbstractString) =
pubpoint2address(hex2bytes(x), hex2bytes(y))</langsyntaxhighlight>
 
'''Main''':
<langsyntaxhighlight lang=julia>x = "50863AD64A87AE8A2FE83C1AF1A8403CB53F53E486D8511DAD8A04887E5B2352"
y = "2CD470243453A299FA9E77237716103ABC11A1DF38855ED6F2EE187E9C582BA6"
println(pubpoint2address(x, y)))</langsyntaxhighlight>
 
{{out}}
Line 663:
 
=={{header|Wolfram Language}}/{{header|Mathematica}}==
<langsyntaxhighlight lang=Mathematica>BlockchainKeyEncode[
PublicKey[
<|
Line 675:
"Address",
BlockchainBase-> "Bitcoin"
]</langsyntaxhighlight>
{{output}}
<pre>16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM</pre>
Line 689:
The “bignum” library is used to check if the public point belongs to the “secp256k1” elliptic curve.
 
<langsyntaxhighlight lang=nim>import parseutils
import nimcrypto
import bignum
Line 756:
"2CD470243453A299FA9E77237716103ABC11A1DF38855ED6F2EE187E9C582BA6")
echo "Coordinates are valid."
echo "Address is: ", address</langsyntaxhighlight>
 
{{out}}
Line 764:
=={{header|Perl}}==
Here we'll use the standard Digest::SHA module, and the CPAN-available Crypt::RIPEMD160 and Encode::Base58::GMP.
<langsyntaxhighlight lang=perl>
use Crypt::RIPEMD160;
use Digest::SHA qw(sha256);
Line 785:
'2CD470243453A299FA9E77237716103ABC11A1DF38855ED6F2EE187E9C582BA6'
;
</syntaxhighlight>
</lang>
{{out}}
<pre>16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM</pre>
 
=={{header|Phix}}==
<!--<langsyntaxhighlight lang=Phix>(notonline)-->
<span style="color: #008080;">without</span> <span style="color: #008080;">javascript_semantics</span> <span style="color: #000080;font-style:italic;">-- no ripemd160.js as yet</span>
<span style="color: #008080;">include</span> <span style="color: #000000;">builtins</span><span style="color: #0000FF;">\</span><span style="color: #7060A8;">sha256</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
Line 834:
<span style="color: #0000FF;">?</span><span style="color: #000000;">coin_encode</span><span style="color: #0000FF;">(</span>x"<span style="color: #0000FF;">50863AD64A87AE8A2FE83C1AF1A8403CB53F53E486D8511DAD8A04887E5B2352</span>"<span style="color: #0000FF;">,</span>
x"<span style="color: #0000FF;">2CD470243453A299FA9E77237716103ABC11A1DF38855ED6F2EE187E9C582BA6</span>"<span style="color: #0000FF;">)</span>
<!--</langsyntaxhighlight>-->
There is actually an sha256.js included, not that I recommend it.<br>
You could probably get this to work in a browser if you provide a suitable ripemd160.js and tweak p2js to use it.
Line 844:
 
=={{header|PicoLisp}}==
<langsyntaxhighlight lang=PicoLisp>(load "ripemd160.l")
(load "sha256.l")
 
Line 882:
(point2address
"50863AD64A87AE8A2FE83C1AF1A8403CB53F53E486D8511DAD8A04887E5B2352"
"2CD470243453A299FA9E77237716103ABC11A1DF38855ED6F2EE187E9C582BA6" ) )</langsyntaxhighlight>
{{out}}
<pre>"16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM"</pre>
 
=={{header|Python}}==
<langsyntaxhighlight lang=python>#!/usr/bin/env python3
 
import binascii
Line 909:
print(public_point_to_address(
b'50863AD64A87AE8A2FE83C1AF1A8403CB53F53E486D8511DAD8A04887E5B2352',
b'2CD470243453A299FA9E77237716103ABC11A1DF38855ED6F2EE187E9C582BA6'))</langsyntaxhighlight>
{{out}}
<pre>
Line 919:
Uses code from [[SHA-256#Racket]] (which is isolated in a submodule).
 
<langsyntaxhighlight lang=racket>#lang racket/base
(module sha256 racket/base
;; define a quick SH256 FFI interface, similar to the Racket's default
Line 1,038:
(bytes->HEX-STRING s7) => "D61967F6"
(bytes->HEX-STRING s8) => "00010966776006953D5567439E5E39F86A0D273BEED61967F6"
s9 => #"16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM"))</langsyntaxhighlight>
 
{{out}}
Line 1,048:
(formerly Perl 6)
 
<syntaxhighlight lang=raku perl6line>sub dgst(blob8 $b, Str :$dgst) returns blob8 {
given run «openssl dgst "-$dgst" -binary», :in, :out, :bin {
.in.write: $b;
Line 1,073:
0x50863AD64A87AE8A2FE83C1AF1A8403CB53F53E486D8511DAD8A04887E5B2352,
0x2CD470243453A299FA9E77237716103ABC11A1DF38855ED6F2EE187E9C582BA6;
</syntaxhighlight>
</lang>
{{out}}
<pre>6UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM</pre>
 
=={{header|Ruby}}==
<langsyntaxhighlight lang=ruby>
# Translate public point to Bitcoin address
#
Line 1,100:
end
puts res.reverse
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,107:
 
=={{header|Rust}}==
<langsyntaxhighlight lang=rust>
use ring::digest::{digest, SHA256};
use ripemd160::{Digest, Ripemd160};
Line 1,174:
}
 
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,188:
which encodes a string with the Base58 encoding used by Bitcoin. No external library is needed.
 
<langsyntaxhighlight lang=seed7>$ include "seed7_05.s7i";
include "bytedata.s7i";
include "msgdigest.s7i";
Line 1,207:
writeln(publicPointToAddress("50863AD64A87AE8A2FE83C1AF1A8403CB53F53E486D8511DAD8A04887E5B2352",
"2CD470243453A299FA9E77237716103ABC11A1DF38855ED6F2EE187E9C582BA6"));
end func;</langsyntaxhighlight>
 
{{out}}
Line 1,217:
{{tcllib|ripemd160}}
{{tcllib|sha256}}
<langsyntaxhighlight lang=tcl>package require ripemd160
package require sha256
 
Line 1,245:
append addr [binary format "a4" [string range $hash 0 3]]
return [base58encode $addr]
}</langsyntaxhighlight>
Demonstrating
<langsyntaxhighlight lang=tcl>puts [bitcoin_mkaddr \
0x50863AD64A87AE8A2FE83C1AF1A8403CB53F53E486D8511DAD8A04887E5B2352 \
0x2CD470243453A299FA9E77237716103ABC11A1DF38855ED6F2EE187E9C582BA6]</langsyntaxhighlight>
{{out}}
<pre>
Line 1,260:
{{libheader|Wren-str}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight lang=ecmascript>import "/crypto" for Sha256, Ripemd160
import "/str" for Str
import "/fmt" for Conv
Line 1,354:
a.setPoint(p)
// show base58 representation
System.print(a.a58.map { |b| String.fromByte(b) }.join())</langsyntaxhighlight>
 
{{out}}
Line 1,363:
=={{header|zkl}}==
Uses shared library zklMsgHash.
<langsyntaxhighlight lang=zkl>var [const] MsgHash=Import.lib("zklMsgHash"); // SHA-256, etc
 
const symbols = "123456789" // 58 characters: no cap i,o; ell, zero
Line 1,389:
d,chkSum := Data(), MsgHash.SHA256(bytes,1,d) : MsgHash.SHA256(_,1,d);
base58Encode(bytes.append(chkSum.del(4,*))); // first 4 bytes of hashed hash
}</langsyntaxhighlight>
<langsyntaxhighlight lang=zkl>e:=coinEncode(
"50863AD64A87AE8A2FE83C1AF1A8403CB53F53E486D8511DAD8A04887E5B2352",
"2CD470243453A299FA9E77237716103ABC11A1DF38855ED6F2EE187E9C582BA6");
(e=="16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM").println();</langsyntaxhighlight>
{{out}}
<pre>True</pre>
10,333

edits