Bitcoin/public point to address: Difference between revisions

Crate ripemd160 is deprecated - Update
m (syntax highlighting fixup automation)
(Crate ripemd160 is deprecated - Update)
 
(2 intermediate revisions by 2 users not shown)
Line 22:
 
''Extra credit:'' add a verification procedure about the public point, making sure it belongs to the secp256k1 elliptic curve
 
=={{header|C}}==
<syntaxhighlight lang="c">#include <stdio.h>
#include <string.h>
#include <ctype.h>
Line 99 ⟶ 98:
return 0;
}</syntaxhighlight>
 
=={{header|Common Lisp}}==
{{libheader|Quicklisp}}
{{libheader|Ironclad}}
<syntaxhighlight 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 210 ⟶ 208:
Here's an example of how to feed a point into the functions defined above:
 
<syntaxhighlight lang="text">
 
;; ? (pubkey-to-p2pkh
Line 219 ⟶ 217:
 
</syntaxhighlight>
 
=={{header|D}}==
Requires the second D module from the SHA-256 task.
{{trans|C}}
{{trans|Go}}
<syntaxhighlight lang="d">import std.stdio, std.algorithm, std.digest.ripemd, sha_256_2;
 
// A Bitcoin public point.
Line 290 ⟶ 287:
{{libheader| DCPripemd160}}
{{Trans|Go}}
<syntaxhighlight lang=Delphi"delphi">
program Public_point_to_address;
 
Line 454 ⟶ 451:
readln;
end.</syntaxhighlight>
 
=={{header|Factor}}==
<syntaxhighlight lang="factor">USING: checksums checksums.ripemd checksums.sha io.binary kernel
math sequences ;
IN: rosetta-code.bitcoin.point-address
Line 485 ⟶ 481:
point>address . ! "16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM"
</pre>
 
=={{header|Go}}==
{{libheader|Go sub-repositories}}
<syntaxhighlight lang="go">package main
 
import (
Line 593 ⟶ 588:
16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM
</pre>
 
=={{header|Haskell}}==
<syntaxhighlight lang="haskell">import Numeric (showIntAtBase)
import Data.List (unfoldr)
import Data.Binary (Word8)
Line 619 ⟶ 613:
<pre>"6UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM"
</pre>
 
=={{header|Julia}}==
{{works with|Julia|0.6}}
Line 625 ⟶ 618:
 
'''Main functions''':
<syntaxhighlight lang="julia">const digits = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
function encodebase58(b::Vector{<:Integer})
out = Vector{Char}(34)
Line 655 ⟶ 648:
 
'''Main''':
<syntaxhighlight lang="julia">x = "50863AD64A87AE8A2FE83C1AF1A8403CB53F53E486D8511DAD8A04887E5B2352"
y = "2CD470243453A299FA9E77237716103ABC11A1DF38855ED6F2EE187E9C582BA6"
println(pubpoint2address(x, y)))</syntaxhighlight>
Line 661 ⟶ 654:
{{out}}
<pre>6UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM</pre>
 
=={{header|Wolfram Language}}/{{header|Mathematica}}==
<syntaxhighlight lang=Mathematica>BlockchainKeyEncode[
PublicKey[
<|
"Type"->"EllipticCurve",
"PublicCurvePoint"-> {
16^^50863AD64A87AE8A2FE83C1AF1A8403CB53F53E486D8511DAD8A04887E5B2352,
16^^2CD470243453A299FA9E77237716103ABC11A1DF38855ED6F2EE187E9C582BA6
}
|>
],
"Address",
BlockchainBase-> "Bitcoin"
]</syntaxhighlight>
{{output}}
<pre>16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM</pre>
 
=={{header|Nim}}==
 
Line 689 ⟶ 664:
The “bignum” library is used to check if the public point belongs to the “secp256k1” elliptic curve.
 
<syntaxhighlight lang="nim">import parseutils
import nimcrypto
import bignum
Line 761 ⟶ 736:
<pre>Coordinates are valid.
Address is: 16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM</pre>
 
=={{header|Perl}}==
Here we'll use the standard Digest::SHA module, and the CPAN-available Crypt::RIPEMD160 and Encode::Base58::GMP.
<syntaxhighlight lang="perl">
use Crypt::RIPEMD160;
use Digest::SHA qw(sha256);
Line 788 ⟶ 762:
{{out}}
<pre>16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM</pre>
 
=={{header|Phix}}==
<!--<syntaxhighlight lang=Phix"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 842 ⟶ 815:
"16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM"
</pre>
 
=={{header|PicoLisp}}==
<syntaxhighlight lang=PicoLisp"picolisp">(load "ripemd160.l")
(load "sha256.l")
 
Line 885 ⟶ 857:
{{out}}
<pre>"16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM"</pre>
 
=={{header|Python}}==
<syntaxhighlight lang="python">#!/usr/bin/env python3
 
import binascii
Line 914 ⟶ 885:
b'6UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM'
</pre>
 
=={{header|Racket}}==
 
Uses code from [[SHA-256#Racket]] (which is isolated in a submodule).
 
<syntaxhighlight lang="racket">#lang racket/base
(module sha256 racket/base
;; define a quick SH256 FFI interface, similar to the Racket's default
Line 1,044 ⟶ 1,014:
1 test passed
8 tests passed</pre>
 
=={{header|Raku}}==
(formerly Perl 6)
 
<syntaxhighlight lang="raku" line>sub dgst(blob8 $b, Str :$dgst) returns blob8 {
given run «openssl dgst "-$dgst" -binary», :in, :out, :bin {
.in.write: $b;
Line 1,076 ⟶ 1,045:
{{out}}
<pre>6UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM</pre>
 
=={{header|Ruby}}==
<syntaxhighlight lang="ruby">
# Translate public point to Bitcoin address
#
Line 1,105 ⟶ 1,073:
6UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM
</pre>
 
=={{header|Rust}}==
<syntaxhighlight lang="rust">
use ring::digest::{digest, SHA256};
use ripemd160ripemd::{Digest, Ripemd160};
 
use hex::FromHex;
Line 1,188 ⟶ 1,155:
which encodes a string with the Base58 encoding used by Bitcoin. No external library is needed.
 
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i";
include "bytedata.s7i";
include "msgdigest.s7i";
Line 1,213 ⟶ 1,180:
16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM
</pre>
 
=={{header|Tcl}}==
{{tcllib|ripemd160}}
{{tcllib|sha256}}
<syntaxhighlight lang="tcl">package require ripemd160
package require sha256
 
Line 1,247 ⟶ 1,213:
}</syntaxhighlight>
Demonstrating
<syntaxhighlight lang="tcl">puts [bitcoin_mkaddr \
0x50863AD64A87AE8A2FE83C1AF1A8403CB53F53E486D8511DAD8A04887E5B2352 \
0x2CD470243453A299FA9E77237716103ABC11A1DF38855ED6F2EE187E9C582BA6]</syntaxhighlight>
Line 1,254 ⟶ 1,220:
16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM
</pre>
=={{header|Wolfram Language}}/{{header|Mathematica}}==
 
<syntaxhighlight lang=Mathematica"mathematica">BlockchainKeyEncode[
PublicKey[
<|
"Type"->"EllipticCurve",
"PublicCurvePoint"-> {
16^^50863AD64A87AE8A2FE83C1AF1A8403CB53F53E486D8511DAD8A04887E5B2352,
16^^2CD470243453A299FA9E77237716103ABC11A1DF38855ED6F2EE187E9C582BA6
}
|>
],
"Address",
BlockchainBase-> "Bitcoin"
]</syntaxhighlight>
{{output}}
<pre>16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM</pre>
=={{header|Wren}}==
{{trans|Go}}
Line 1,260 ⟶ 1,241:
{{libheader|Wren-str}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang=ecmascript"wren">import "./crypto" for Sha256, Ripemd160
import "./str" for Str
import "./fmt" for Conv
 
// converts an hexadecimal string to a byte list.
Line 1,363 ⟶ 1,344:
=={{header|zkl}}==
Uses shared library zklMsgHash.
<syntaxhighlight lang="zkl">var [const] MsgHash=Import.lib("zklMsgHash"); // SHA-256, etc
 
const symbols = "123456789" // 58 characters: no cap i,o; ell, zero
Line 1,390 ⟶ 1,371:
base58Encode(bytes.append(chkSum.del(4,*))); // first 4 bytes of hashed hash
}</syntaxhighlight>
<syntaxhighlight lang="zkl">e:=coinEncode(
"50863AD64A87AE8A2FE83C1AF1A8403CB53F53E486D8511DAD8A04887E5B2352",
"2CD470243453A299FA9E77237716103ABC11A1DF38855ED6F2EE187E9C582BA6");
Line 1,396 ⟶ 1,377:
{{out}}
<pre>True</pre>
 
 
{{omit from|Brlcad}}
{{omit from|GUISS}}
117

edits