Bitcoin/address validation: Difference between revisions

m
(Added Seed7 example)
m (→‎{{header|Wren}}: Minor tidy)
 
(20 intermediate revisions by 8 users not shown)
Line 1:
[[Category:Checksums]]
{{alertbox|#ffff70|'''<big>Warning:</big>''' Many of these snippets are [[{{TALKPAGENAME}}#C-based_code_.28and_possibly_others.29_improperly_validates|incomplete]]. It is recommended that you use an established [https://en.bitcoin.it/wiki/Software#Libraries library] for any projects that are likely to see external use}}
{{task}}
[[Category:Checksums]]
{{omit from|Brlcad}}
{{omit from|GUISS}}
 
 
Line 38 ⟶ 36:
<br>You can change a few characters in this string and check that it'll fail the test.
<br><br>
 
=={{header|Ada}}==
<langsyntaxhighlight lang="ada">with Ada.Exceptions, Interfaces;
with Ada.Streams;
use Ada.Exceptions, Interfaces;
Line 168 ⟶ 165:
end;
end Bitcoin_Addr_Validate;
</syntaxhighlight>
</lang>
 
{{out}}
Line 177 ⟶ 174:
1A Na15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i validity: *** Error: Invalid BT address.
</pre>
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <string.h>
#include <openssl/sha.h>
Line 239 ⟶ 235:
 
return 0;
}</langsyntaxhighlight>
Compile with -lcrypto
{{out}}
Line 248 ⟶ 244:
1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62I: bad char
</pre>
=={{header|C sharp|C#}}==
 
=={{header|C#}}==
This requires [https://www.nuget.org/packages/NUnit/ NUnit package] to compile.
<langsyntaxhighlight lang="csharp">
using System;
using System.Linq;
Line 321 ⟶ 316:
}
}
</syntaxhighlight>
</lang>
 
=={{header|D}}==
This requires the D module from the SHA-256 Task.
{{trans|Go}}
<langsyntaxhighlight lang="d">import std.stdio, std.algorithm, std.array, std.string, sha_256_2;
 
struct A25 {
Line 410 ⟶ 404:
writefln(`"%s": %s`, test, err.empty ? "OK." : err);
}
}</langsyntaxhighlight>
{{out}}
<pre>"1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i": OK.
Line 417 ⟶ 411:
"1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62iz": not Bitcoin version 0.
"1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62izz": too long Bitcoin address.</pre>
=={{header|dart}}==
This requires [https://pub.dev/packages/crypto Crypto package] to compile.
{{trans|Java}}
<syntaxhighlight lang="dart">import 'package:crypto/crypto.dart';
 
class Bitcoin {
final String ALPHABET =
"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
 
List<int> bigIntToByteArray(BigInt data) {
String str;
bool neg = false;
if (data < BigInt.zero) {
str = (~data).toRadixString(16);
neg = true;
} else str = data.toRadixString(16);
int p = 0;
int len = str.length;
 
int blen = (len + 1) ~/ 2;
int boff = 0;
List bytes;
if (neg) {
if (len & 1 == 1) {
p = -1;
}
int byte0 = ~int.parse(str.substring(0, p + 2), radix: 16);
if (byte0 < -128) byte0 += 256;
if (byte0 >= 0) {
boff = 1;
bytes = new List<int>(blen + 1);
bytes[0] = -1;
bytes[1] = byte0;
} else {
bytes = new List<int>(blen);
bytes[0] = byte0;
}
for (int i = 1; i < blen; ++i) {
int byte = ~int.parse(str.substring(p + (i << 1), p + (i << 1) + 2),
radix: 16);
if (byte < -128) byte += 256;
bytes[i + boff] = byte;
}
} else {
if (len & 1 == 1) {
p = -1;
}
int byte0 = int.parse(str.substring(0, p + 2), radix: 16);
if (byte0 > 127) byte0 -= 256;
if (byte0 < 0) {
boff = 1;
bytes = new List<int>(blen + 1);
bytes[0] = 0;
bytes[1] = byte0;
} else {
bytes = new List<int>(blen);
bytes[0] = byte0;
}
for (int i = 1; i < blen; ++i) {
int byte =
int.parse(str.substring(p + (i << 1), p + (i << 1) + 2), radix: 16);
if (byte > 127) byte -= 256;
bytes[i + boff] = byte;
}
}
return bytes;
}
 
List<int> arrayCopy(bytes, srcOffset, result, destOffset, bytesLength) {
for (int i = srcOffset; i < bytesLength; i++) {
result[destOffset + i] = bytes[i];
}
return result;
}
 
List<int> decodeBase58To25Bytes(String input) {
BigInt number = BigInt.zero;
for (String t in input.split('')) {
int p = ALPHABET.indexOf(t);
if (p == (-1))
return null;
number = number * (BigInt.from(58)) + (BigInt.from(p));
}
List<int> result = new List<int>(24);
List<int> numBytes = bigIntToByteArray(number);
return arrayCopy(
numBytes, 0, result, result.length - numBytes.length, numBytes.length);
}
 
validateAddress(String address) {
List<int> decoded = new List.from(decodeBase58To25Bytes(address));
List<int> temp = new List<int>.from(decoded);
temp.insert(0, 0);
List<int> hash1 = sha256.convert(temp.sublist(0, 21)).bytes;
List<int> hash2 = sha256.convert(hash1).bytes;
if (hash2[0] != decoded[20] ||
hash2[1] != decoded[21] ||
hash2[2] != decoded[22] ||
hash2[3] != decoded[23]) return false;
return true;
}
}</syntaxhighlight>
{{out}}
<pre>"1BNGaR29FmfAqidXmD9HLwsGv9p5WVvvhq" true
"1BNGaR29FmfAqidXmD9HLws" false
</pre>
=={{header|Delphi}}==
This requires [http://www.cityinthesky.co.uk/opensource/DCPcrypt/ DCPcrypt library] to compile.
<langsyntaxhighlight lang="delphi">
uses
DCPsha256;
Line 485 ⟶ 584:
raise Exception.Create('Bad digest');
end;
</syntaxhighlight>
</lang>
 
=={{header|Erlang}}==
Using base58 module from http://github.com/titan098/erl-base58.git.
 
<syntaxhighlight lang="erlang">
<lang Erlang>
-module( bitcoin_address ).
 
Line 508 ⟶ 606:
{checksum, Checksum} = {checksum, Four_bytes},
ok.
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 520 ⟶ 618:
</pre>
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USING: byte-arrays checksums checksums.sha io.binary kernel math
math.parser sequences ;
IN: rosetta-code.bitcoin.validation
Line 536 ⟶ 634:
 
: btc-valid? ( str -- ? ) base58> [ btc-checksum ] [ 4 tail* ] bi = ;
</syntaxhighlight>
</lang>
 
{{out}}
Line 544 ⟶ 642:
"1ANNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i" btc-valid? . ! f, data changed, original checksum.
</pre>
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">' version 05-04-2017
' compile with: fbc -s console
 
Line 751 ⟶ 848:
Print : Print "hit any key to end program"
Sleep
End</langsyntaxhighlight>
{{out}}
<pre>Bitcoin address: 1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i is valid
Line 758 ⟶ 855:
Bitcoin address: 0AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i first character is not 1 or 3
Bitcoin address: 1AGNa15ZQXAZUgFlqJ2i7Z2DPU2J6hW62i bitcoin address contains illegal character</pre>
 
=={{header|Go}}==
{{trans|C}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 869 ⟶ 965:
os.Stderr.WriteString(m + "\n")
os.Exit(1)
}</langsyntaxhighlight>
{{out}}
Command line usage examples showing program exit status.
Line 895 ⟶ 991:
</pre>
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Control.Monad (when)
import Data.List (elemIndex)
import Data.Monoid ((<>))
Line 953 ⟶ 1,049:
validate "1ANa55215ZQXAZUgFiqJ2i7Z2DPU2J6hW62i" -- too long
validate "i55j" -- too short
</syntaxhighlight>
</lang>
{{out}}
<pre style="font-size:80%">"1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i" -> Valid
Line 962 ⟶ 1,058:
"1ANa55215ZQXAZUgFiqJ2i7Z2DPU2J6hW62i" -> "Address length exceeds 25 bytes"
"i55j" -> "Address length less than 4 bytes"</pre>
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<lang Mathematica>chars = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"; data =
IntegerDigits[
FromDigits[
StringPosition[chars, #][[1]] - 1 & /@ Characters[InputString[]],
58], 256, 25];
data[[-4 ;;]] ==
IntegerDigits[
Hash[FromCharacterCode[
IntegerDigits[Hash[FromCharacterCode[data[[;; -5]]], "SHA256"],
256, 32]], "SHA256"], 256, 32][[;; 4]]</lang>
{{in}}
<pre>1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i
2AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i</pre>
{{out}}
<pre>True
False</pre>
 
 
=={{header|Java}}==
<langsyntaxhighlight lang="java">import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
Line 1,051 ⟶ 1,127:
throw new AssertionError(String.format("Expected %s for %s, but got %s.", expected, address, actual));
}
}</langsyntaxhighlight>
 
=={{header|Julia}}==
{{works with|Julia|0.6}}
{{trans|Python}}
<langsyntaxhighlight lang="julia">using SHA
 
bytes(n::Integer, l::Int) = collect(UInt8, (n >> 8i) & 0xFF for i in l-1:-1:0)
Line 1,093 ⟶ 1,168:
for (addr, corr) in addresses
println("Address: $addr\nExpected: $corr\tChecked: ", checkbcaddress(addr))
end</langsyntaxhighlight>
 
{{out}}
Line 1,122 ⟶ 1,197:
Address: 1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62iz
Expected: false Checked: false</pre>
 
=={{header|Kotlin}}==
{{trans|Java}}
<langsyntaxhighlight lang="scala">import java.security.MessageDigest
 
object Bitcoin {
Line 1,184 ⟶ 1,258:
for (address in addresses)
println("${address.padEnd(36)} -> ${if (Bitcoin.validateAddress(address)) "valid" else "invalid"}")
}</langsyntaxhighlight>
 
{{out}}
Line 1,202 ⟶ 1,276:
1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62I -> invalid
</pre>
=={{header|Mathematica}} / {{header|Wolfram Language}}==
 
<syntaxhighlight lang="mathematica">chars = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"; data =
IntegerDigits[
FromDigits[
StringPosition[chars, #][[1]] - 1 & /@ Characters[InputString[]],
58], 256, 25];
data[[-4 ;;]] ==
IntegerDigits[
Hash[FromCharacterCode[
IntegerDigits[Hash[FromCharacterCode[data[[;; -5]]], "SHA256"],
256, 32]], "SHA256"], 256, 32][[;; 4]]</syntaxhighlight>
{{in}}
<pre>1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i
2AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i</pre>
{{out}}
<pre>True
False</pre>
=={{header|Nim}}==
Tests on first digit character and on address length have been added to detect wrong addresses such as "BZbvjr".
 
===Using “libssl”===
Requires libssl library to perform SHA256. Build with -d:ssl flag.
 
<langsyntaxhighlight lang="nim">import algorithm
 
const SHA256Len = 32
Line 1,269 ⟶ 1,360:
stdout.write(vector & " : ")
try:
if vector[0] notin {'1', '3'}:
raise newException(ValueError, "invalid starting character")
if vector.len < 26:
raise newException(ValueError, "address too short")
 
decodeBase58(vector, buf)
 
Line 1,282 ⟶ 1,378:
 
main()
</syntaxhighlight>
</lang>
 
{{out}}
<pre>3yQ : NG - address too short
<pre>
3yQ : NG - checksum invalid
1Q1pE5vPGEEMqRcVRMbtBK842Y6Pzo6nK9 : OK
1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i : OK
Line 1,297 ⟶ 1,392:
16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM : OK
1111111111111111111114oLvT2 : OK
BZbvjr : NG - invalid starting character</pre>
BZbvjr : OK
</pre>
 
===Using “nimcrypto”===
Note: The last two test addresses have valid checksums and conform to the specified address constraints.
{{libheader|nimcrypto}}
<syntaxhighlight lang="nim">import nimcrypto
import strformat
 
const
 
DecodedLength = 25 # Decoded address length.
CheckSumLength = 4 # Checksum length in address.
 
# Representation of a decoded address.
type Bytes25 = array[DecodedLength, byte]
 
 
#---------------------------------------------------------------------------------------------------
 
proc base58Decode(input: string): Bytes25 =
## Decode a base58 encoded bitcoin address.
 
const Base = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
 
for ch in input:
 
var n = Base.find(ch)
if n < 0:
raise newException(ValueError, "invalid character: " & ch)
 
for i in countdown(result.high, 0):
n += 58 * result[i].int
result[i] = byte(n and 255)
n = n div 256
 
if n != 0:
raise newException(ValueError, "decoded address is too long")
 
#---------------------------------------------------------------------------------------------------
 
proc verifyChecksum(data: Bytes25) =
## Verify that data has the expected checksum.
 
var digest = sha256.digest(data.toOpenArray(0, data.high - CheckSumLength))
digest = sha256.digest(digest.data)
if digest.data[0..<CheckSumLength] != data[^CheckSumLength..^1]:
raise newException(ValueError, "wrong checksum")
 
#---------------------------------------------------------------------------------------------------
 
proc checkValidity(address: string) =
## Check the validity of a bitcoin address.
 
try:
if address[0] notin {'1', '3'}:
raise newException(ValueError, "starting character is not 1 or 3")
if address.len < 26:
raise newException(ValueError, "address too short")
address.base58Decode().verifyChecksum()
echo fmt"Address “{address}” is valid."
 
except ValueError:
echo fmt"Address “{address}” is invalid ({getCurrentExceptionMsg()})."
 
 
#———————————————————————————————————————————————————————————————————————————————————————————————————
 
const testVectors : seq[string] = @[
"3yQ", # Invalid.
"1Q1pE5vPGEEMqRcVRMbtBK842Y6Pzo6nK9", # Valid.
"1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i", # Valid.
"1Q1pE5vPGEEMqRcVRMbtBK842Y6Pzo6nJ9", # Invalid.
"1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62I", # Invalid.
"1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62ix", # Invalid.
"1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62ixx", # Invalid.
"17NdbrSGoUotzeGCcMMCqnFkEvLymoou9j", # Valid.
"1badbadbadbadbadbadbadbadbadbadbad", # Invalid.
"16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM", # Valid.
"1111111111111111111114oLvT2", # Valid.
"BZbvjr"] # Invalid.
 
for vector in testVectors:
vector.checkValidity()</syntaxhighlight>
 
{{out}}
<pre>Address “3yQ” is invalid (address too short).
Address “1Q1pE5vPGEEMqRcVRMbtBK842Y6Pzo6nK9” is valid.
Address “1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i” is valid.
Address “1Q1pE5vPGEEMqRcVRMbtBK842Y6Pzo6nJ9” is invalid (wrong checksum).
Address “1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62I” is invalid (invalid character: I).
Address “1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62ix” is invalid (wrong checksum).
Address “1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62ixx” is invalid (decoded address is too long).
Address “17NdbrSGoUotzeGCcMMCqnFkEvLymoou9j” is valid.
Address “1badbadbadbadbadbadbadbadbadbadbad” is invalid (wrong checksum).
Address “16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM” is valid.
Address “1111111111111111111114oLvT2” is valid.
Address “BZbvjr” is invalid (starting character is not 1 or 3).</pre>
=={{header|Oberon-2}}==
{{works with|oo2c}}{{libheader|Crypto}}
<langsyntaxhighlight lang="oberon2">
MODULE BitcoinAddress;
IMPORT
Line 1,386 ⟶ 1,572:
Out.Bool(Valid("1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62I"));Out.Ln
END BitcoinAddress.
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,395 ⟶ 1,581:
FALSE
</pre>
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">my @b58 = qw{
1 2 3 4 5 6 7 8 9
A B C D E F G H J K L M N P Q R S T U V W X Y Z
Line 1,428 ⟶ 1,613:
(pack 'C*', @byte[21..24]) eq
substr sha256(sha256 pack 'C*', @byte[0..20]), 0, 4;
}</langsyntaxhighlight>
 
=={{header|Perl 6}}==
<lang perl6>my $bitcoin-address = rx/
<+alnum-[0IOl]> ** 26..* # an address is at least 26 characters long
<?{
use Digest::SHA;
.subbuf(21, 4) eqv sha256(sha256 .subbuf(0, 21)).subbuf(0, 4) given
Blob.new: <
1 2 3 4 5 6 7 8 9
A B C D E F G H J K L M N P Q R S T U V W X Y Z
a b c d e f g h i j k m n o p q r s t u v w x y z
>.pairs.invert.hash{$/.comb}
.reduce(* * 58 + *)
.polymod(256 xx 24)
.reverse;
}>
/;
say "Here is a bitcoin address: 1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i" ~~ $bitcoin-address;</lang>
{{out}}
<pre>「1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i」</pre>
 
=={{header|Phix}}==
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>--
<span style="color: #000080;font-style:italic;">--
-- demo\rosetta\bitcoin_address_validation.exw
-- demo\rosetta\bitcoin_address_validation.exw
--
-- ===========================================
string coin_err
--</span>
 
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
constant b58 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
<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>
string charmap = ""
 
function unbase58(string s)
string out = repeat('\0',25)
if length(charmap)=0 then
charmap = repeat('\0',256)
for i=1 to length(b58) do
charmap[b58[i]] = i
end for
end if
-- not at all sure about this:
-- if length(s)!=34 then
-- coin_err = "bad length"
-- return 0
-- end if
if s[1]!='1' and s[1]!='3' then
coin_err = "first character is not 1 or 3"
return 0
end if
for i=1 to length(s) do
integer c = charmap[s[i]]
if c=0 then
coin_err = "bad char"
return 0
end if
c -= 1
for j=25 to 1 by -1 do
c += 58 * out[j];
out[j] = and_bits(c,#FF)
c = floor(c/#100)
end for
if c!=0 then
coin_err = "address too long"
return 0
end if
end for
if out[1]!='\0' then
coin_err = "not version 0"
return 0
end if
return out
end function
 
include builtins\sha256.e
 
procedure valid(string s)
object dec
coin_err = "OK"
dec = unbase58(s)
if string(dec) then
if dec[22..$]!=sha256(sha256(dec[1..21]))[1..4] then
coin_err = "bad digest"
end if
end if
end procedure
<span style="color: #008080;">constant</span> <span style="color: #000000;">b58</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"</span>
constant tests = {
<span style="color: #004080;">string</span> <span style="color: #000000;">charmap</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">""</span>
"1Q1pE5vPGEEMqRcVRMbtBK842Y6Pzo6nK9", -- OK
"1Q1pE5vPGEEMqRcVRMbtBK842Y6Pzo6nJ9", -- bad digest
<span style="color: #008080;">function</span> <span style="color: #000000;">valid</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">bool</span> <span style="color: #000000;">expected</span><span style="color: #0000FF;">)</span>
"1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i", -- OK
<span style="color: #004080;">bool</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">:=</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">expected</span><span style="color: #0000FF;">==</span><span style="color: #004600;">false</span><span style="color: #0000FF;">)</span>
"1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62X", -- bad digest (checksum changed, original data.)
<span style="color: #008080;">if</span> <span style="color: #000000;">charmap</span><span style="color: #0000FF;">=</span><span style="color: #008000;">""</span> <span style="color: #008080;">then</span>
"1ANNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i", -- bad digest (data changed, original checksum.)
<span style="color: #000000;">charmap</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #008000;">'\0'</span><span style="color: #0000FF;">,</span><span style="color: #000000;">256</span><span style="color: #0000FF;">)</span>
"1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62iz", -- not version 0
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">b58</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
"1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62izz", -- address too long
<span style="color: #000000;">charmap</span><span style="color: #0000FF;">[</span><span style="color: #000000;">b58</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">i</span>
"1A Na15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i", -- bad char
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
"1111111111111111111114oLvT2", -- OK?
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
"17NdbrSGoUotzeGCcMMCqnFkEvLymoou9j", -- OK (*NB*: apparently differs to Python solution,
<span style="color: #000080;font-style:italic;">-- not at all sure about this:
-- but does agree with Racket and Rust entries)
-- if length(s)!=34 then
"1badbadbadbadbadbadbadbadbadbadbad", -- not version 0
-- return {expected==false,"bad length"}
"BZbvjr", -- first character is not 1 or 3 (checksum is fine, address too short)
-- end if</span>
"16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM", -- OK (from public_point_to_address)
<span style="color: #008080;">if</span> <span style="color: #008080;">not</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">],</span><span style="color: #008000;">"13"</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
$}
<span style="color: #008080;">return</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"first character is not 1 or 3"</span><span style="color: #0000FF;">}</span>
 
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
for i=1 to length(tests) do
<span style="color: #004080;">string</span> <span style="color: #000000;">out</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #008000;">'\0'</span><span style="color: #0000FF;">,</span><span style="color: #000000;">25</span><span style="color: #0000FF;">)</span>
valid(tests[i]);
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
printf(1,"%s: %s\n", {tests[i], coin_err})
<span style="color: #004080;">integer</span> <span style="color: #000000;">c</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">charmap</span><span style="color: #0000FF;">[</span><span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]]</span>
end for
<span style="color: #008080;">if</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
{} = wait_key()</lang>
<span style="color: #008080;">return</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"bad char"</span><span style="color: #0000FF;">}</span>
 
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000000;">c</span> <span style="color: #0000FF;">-=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">=</span><span style="color: #000000;">25</span> <span style="color: #008080;">to</span> <span style="color: #000000;">1</span> <span style="color: #008080;">by</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">1</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">c</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">58</span> <span style="color: #0000FF;">*</span> <span style="color: #000000;">out</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">];</span>
<span style="color: #000000;">out</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">and_bits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">c</span><span style="color: #0000FF;">,</span><span style="color: #000000;">#FF</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">c</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">c</span><span style="color: #0000FF;">/</span><span style="color: #000000;">#100</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">!=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
<span style="color: #008080;">return</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"address too long"</span><span style="color: #0000FF;">}</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">out</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]!=</span><span style="color: #008000;">'\0'</span> <span style="color: #008080;">then</span>
<span style="color: #008080;">return</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"not version 0"</span><span style="color: #0000FF;">}</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">out</span><span style="color: #0000FF;">[</span><span style="color: #000000;">22</span><span style="color: #0000FF;">..$]!=</span><span style="color: #7060A8;">sha256</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sha256</span><span style="color: #0000FF;">(</span><span style="color: #000000;">out</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">21</span><span style="color: #0000FF;">]))[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">4</span><span style="color: #0000FF;">]</span> <span style="color: #008080;">then</span>
<span style="color: #008080;">return</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"bad digest"</span><span style="color: #0000FF;">}</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000000;">res</span> <span style="color: #0000FF;">:=</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">expected</span><span style="color: #0000FF;">==</span><span style="color: #004600;">true</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"OK"</span><span style="color: #0000FF;">}</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">tests</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{{</span><span style="color: #008000;">"1Q1pE5vPGEEMqRcVRMbtBK842Y6Pzo6nK9"</span><span style="color: #0000FF;">,</span><span style="color: #004600;">true</span><span style="color: #0000FF;">},</span> <span style="color: #000080;font-style:italic;">-- OK</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"1Q1pE5vPGEEMqRcVRMbtBK842Y6Pzo6nJ9"</span><span style="color: #0000FF;">,</span><span style="color: #004600;">false</span><span style="color: #0000FF;">},</span> <span style="color: #000080;font-style:italic;">-- bad digest</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i"</span><span style="color: #0000FF;">,</span><span style="color: #004600;">true</span><span style="color: #0000FF;">},</span> <span style="color: #000080;font-style:italic;">-- OK</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62j"</span><span style="color: #0000FF;">,</span><span style="color: #004600;">false</span><span style="color: #0000FF;">},</span> <span style="color: #000080;font-style:italic;">-- bad disgest</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62X"</span><span style="color: #0000FF;">,</span><span style="color: #004600;">false</span><span style="color: #0000FF;">},</span> <span style="color: #000080;font-style:italic;">-- bad digest (checksum changed, original data.)</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"1ANNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i"</span><span style="color: #0000FF;">,</span><span style="color: #004600;">false</span><span style="color: #0000FF;">},</span> <span style="color: #000080;font-style:italic;">-- bad digest (data changed, original checksum.)</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62iz"</span><span style="color: #0000FF;">,</span><span style="color: #004600;">false</span><span style="color: #0000FF;">},</span> <span style="color: #000080;font-style:italic;">-- not version 0</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62izz"</span><span style="color: #0000FF;">,</span><span style="color: #004600;">false</span><span style="color: #0000FF;">},</span> <span style="color: #000080;font-style:italic;">-- address too long</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"1BGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i"</span><span style="color: #0000FF;">,</span><span style="color: #004600;">false</span><span style="color: #0000FF;">},</span> <span style="color: #000080;font-style:italic;">-- bad digest</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"1A Na15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i"</span><span style="color: #0000FF;">,</span><span style="color: #004600;">false</span><span style="color: #0000FF;">},</span> <span style="color: #000080;font-style:italic;">-- bad char</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62I"</span><span style="color: #0000FF;">,</span><span style="color: #004600;">false</span><span style="color: #0000FF;">},</span> <span style="color: #000080;font-style:italic;">-- bad char</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62!"</span><span style="color: #0000FF;">,</span><span style="color: #004600;">false</span><span style="color: #0000FF;">},</span> <span style="color: #000080;font-style:italic;">-- bad char</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"1AGNa15ZQXAZUgFiqJ3i7Z2DPU2J6hW62i"</span><span style="color: #0000FF;">,</span><span style="color: #004600;">false</span><span style="color: #0000FF;">},</span> <span style="color: #000080;font-style:italic;">-- bad digest</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"1111111111111111111114oLvT2"</span><span style="color: #0000FF;">,</span> <span style="color: #004600;">true</span><span style="color: #0000FF;">},</span> <span style="color: #000080;font-style:italic;">-- OK</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"17NdbrSGoUotzeGCcMMCqnFkEvLymoou9j"</span><span style="color: #0000FF;">,</span><span style="color: #004600;">true</span><span style="color: #0000FF;">},</span> <span style="color: #000080;font-style:italic;">-- OK</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"1badbadbadbadbadbadbadbadbadbadbad"</span><span style="color: #0000FF;">,</span><span style="color: #004600;">false</span><span style="color: #0000FF;">},</span> <span style="color: #000080;font-style:italic;">-- not version 0</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"BZbvjr"</span><span style="color: #0000FF;">,</span><span style="color: #004600;">false</span><span style="color: #0000FF;">},</span> <span style="color: #000080;font-style:italic;">-- first character is not 1 or 3 (checksum is fine, address too short)</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"i55j"</span><span style="color: #0000FF;">,</span><span style="color: #004600;">false</span><span style="color: #0000FF;">},</span> <span style="color: #000080;font-style:italic;">-- first character is not 1 or 3 (checksum is fine, address too short)</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM"</span><span style="color: #0000FF;">,</span> <span style="color: #004600;">true</span><span style="color: #0000FF;">},</span> <span style="color: #000080;font-style:italic;">-- OK (from public_point_to_address)</span>
<span style="color: #0000FF;">$}</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tests</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #0000FF;">{</span><span style="color: #004080;">string</span> <span style="color: #000000;">ti</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">bool</span> <span style="color: #000000;">expected</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">tests</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span>
<span style="color: #0000FF;">{</span><span style="color: #004080;">bool</span> <span style="color: #000000;">res</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">string</span> <span style="color: #000000;">coin_err</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">valid</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ti</span><span style="color: #0000FF;">,</span><span style="color: #000000;">expected</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #008080;">not</span> <span style="color: #000000;">res</span> <span style="color: #008080;">then</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%s: %s\n"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">ti</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">coin_err</span><span style="color: #0000FF;">})</span>
<span style="color: #0000FF;">{}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">wait_key</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #0000FF;">?</span><span style="color: #008000;">"done"</span>
<span style="color: #0000FF;">{}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">wait_key</span><span style="color: #0000FF;">()</span>
<!--</syntaxhighlight>-->
(No output other than "done" since all tests pass)
=={{header|PHP}}==
<langsyntaxhighlight lang="php">
function validate($address){
$decoded = decodeBase58($address);
Line 1,596 ⟶ 1,760:
main();
 
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,605 ⟶ 1,769:
1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62I: invalid character found
</pre>
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(load "sha256.l")
 
(setq *Alphabet
Line 1,636 ⟶ 1,799:
(valid "1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62!")
(valid "1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62iz")
(valid "1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62izz") ) )</langsyntaxhighlight>
 
=={{header|PureBasic}}==
<syntaxhighlight lang="purebasic">
<lang PureBasic>
; using PureBasic 5.50 (x64)
EnableExplicit
Line 1,717 ⟶ 1,879:
EndIf
 
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,725 ⟶ 1,887:
1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62I -> Invalid
</pre>
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">from hashlib import sha256
 
digits58 = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
Line 1,744 ⟶ 1,905:
 
print(check_bc('1AGNa15ZQXAZUgFiqJ3i7Z2DPU2J6hW62i'))
print(check_bc("17NdbrSGoUotzeGCcMMCqnFkEvLymoou9j"))</langsyntaxhighlight>
 
{{out}}
Line 1,754 ⟶ 1,915:
:Yuuki-chan edit: Delete this help if it's not needed anymore
:For those looking at examples here to try and work out what is required, the <code>n.to_bytes()</code> call is equivalent to this code which converts a (long) integer into individual bytes of a byte array in a particular order:
:<langsyntaxhighlight lang="python">>>> n = 2491969579123783355964723219455906992268673266682165637887
>>> length = 25
>>> list( reversed(range(length)) )
[24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
>>> assert n.to_bytes(length, 'big') == bytes( (n >> i*8) & 0xff for i in reversed(range(length)))
>>> </langsyntaxhighlight>
 
=={{header|Racket}}==
<langsyntaxhighlight lang="racket">
#lang racket/base
 
Line 1,807 ⟶ 1,967:
(validate-bitcoin-address "1Q1pE5vPGEEMqRcVRMbtBK842Y6Pzo6nK9") ; => #t
(validate-bitcoin-address "1badbadbadbadbadbadbadbadbadbadbad") ; => #f
</syntaxhighlight>
</lang>
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku" line>sub sha256(blob8 $b) returns blob8 {
given run <openssl dgst -sha256 -binary>, :in, :out, :bin {
.in.write: $b;
.in.close;
return .out.slurp;
}
}
 
my $bitcoin-address = rx/
<< <+alnum-[0IOl]> ** 26..* >> # an address is at least 26 characters long
<?{
.subbuf(21, 4) eq sha256(sha256 .subbuf(0, 21)).subbuf(0, 4) given
blob8.new: <
1 2 3 4 5 6 7 8 9
A B C D E F G H J K L M N P Q R S T U V W X Y Z
a b c d e f g h i j k m n o p q r s t u v w x y z
>.pairs.invert.hash{$/.comb}
.reduce(* * 58 + *)
.polymod(256 xx 24)
.reverse;
}>
/;
say "Here is a bitcoin address: 1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i" ~~ $bitcoin-address;</syntaxhighlight>
{{out}}
<pre>「1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i」</pre>
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">
# Validate Bitcoin address
#
Line 1,827 ⟶ 2,014:
(n.length...42).each{n.insert(0,'0')}
puts "I think the checksum should be #{g}\nI calculate that it is #{Digest::SHA256.hexdigest(Digest::SHA256.digest(convert(n)))[0,8]}"
</syntaxhighlight>
</lang>
{{out}}
With A = '1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i'
Line 1,839 ⟶ 2,026:
I calculate that it is c046b2ff
</pre>
 
=={{header|Rust}}==
This requires the [https://crates.io/crates/rust-crypto rust-crypto] crate for sha256.
<syntaxhighlight lang="rust">
<lang Rust>
extern crate crypto;
 
Line 1,898 ⟶ 2,084:
Ok(res)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,906 ⟶ 2,092:
false
</pre>
 
=={{header|Scala}}==
<langsyntaxhighlight Scalalang="scala">import java.security.MessageDigest
import java.util.Arrays.copyOfRange
 
Line 1,917 ⟶ 2,102:
 
private def bitcoinTestHarness(address: String, expected: Boolean): Unit =
assert(validateBitcoinAddress(address=1J26TeMg6uK9GkoCKkHNeDaKwtFWdsFnR8) == expected, s"Expected $expected for $address%s, but got ${!expected}.")
 
private def validateBitcoinAddress(addr: String1J26TeMg6uK9GkoCKkHNeDaKwtFWdsFnR8String): Boolean = {
def sha256(data: Array[Byte]) = {
val md: MessageDigest = MessageDigest.getInstance("SHA-256")
Line 1,974 ⟶ 2,159:
println(s"Successfully completed without errors. [total ${scala.compat.Platform.currentTime - executionStart}ms]")
 
}</langsyntaxhighlight>
 
=={{header|Seed7}}==
The Seed7 library [http://seed7.sourceforge.net/libraries/encoding.htm encoding.s7i] defines
the function [http://seed7.sourceforge.net/libraries/encoding.htm#fromBase58(in_string) fromBase58],
which decodes a Base58 encoded string.
The Seed7 library [http://seed7.sourceforge.net/libraries/msgdigest.htm msgdigest.s7i] defines
the function [http://seed7.sourceforge.net/libraries/msgdigest.htm#sha256(in_var_string) sha256].,
which computes a SHA-256 message digest.
No external library is needed.
 
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
include "msgdigest.s7i";
include "encoding.s7i";
 
const func stringboolean: unbase58validBitcoinAddress (in string: stri, inout string: outaddress) is func
result
var stringboolean: stateisValid is ""FALSE;
local
constvar string: digitsdecoded is "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
var char: ch is ' ';
var integer: digit is 0;
var integer: index is 0;
begin
if succeeds(decoded := fromBase58(address)) and
for ch range stri until state <> "" do
length(decoded) = 25 and decoded[1] = '\0;' and
digit := pred(pos(digits, ch));
if digit sha256(sha256(decoded[.. 21]))[.. 4] = -1decoded[22 ..] then
stateisValid := "bad char"TRUE;
end elseif;
for index range 25 downto 1 do
digit +:= 58 * ord(out[index]);
out @:= [index] char(digit rem 256);
digit := digit div 256;
end for;
if digit <> 0 then
state := "address too long";
end if;
end if;
end for;
end func;
 
const func stringproc: validcheckValidationFunction (in string: striaddress, in boolean: expected) is func
result
var string: state is "";
local
var stringboolean: decodedisValid is "\0;" mult 25FALSE;
begin
stateisValid := unbase58validBitcoinAddress(stri, decodedaddress);
writeln((address <& ": ") rpad 37 <& isValid);
if state = "" and sha256(sha256(decoded[.. 21]))[.. 4] <> decoded[22 ..] then
if isValid <> expected then
state := "bad digest";
writeln(" *** Expected " <& expected <& " for " <& address <& ", but got " <& isValid <& ".");
end if;
end func;
 
const proc: main is func
local
const array string: tests is [] ("1Q1pE5vPGEEMqRcVRMbtBK842Y6Pzo6nK9",
"1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i",
"1Q1pE5vPGEEMqRcVRMbtBK842Y6Pzo6nJ9",
"1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62I");
var string: test is "";
var string: state is "";
begin
checkValidationFunction("1Q1pE5vPGEEMqRcVRMbtBK842Y6Pzo6nK9", TRUE); # okay
for test range tests do
checkValidationFunction("1Q1pE5vPGEEMqRcVRMbtBK842Y6Pzo6nJ9", FALSE); # bad digest
state := valid(test);
checkValidationFunction("1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i", TRUE); # okay
if state = "" then
checkValidationFunction("1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62j", FALSE); # bad digest
writeln(test <& ": okay");
checkValidationFunction("1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62X", FALSE); # bad digest
else
checkValidationFunction("1ANNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i", FALSE); # bad digest
writeln(test <& ": " <& state);
checkValidationFunction("oMRDCDfyQhEerkaSmwCfSPqf3MLgBwNvs", FALSE); # not version 0
end if;
checkValidationFunction("1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62iz", FALSE); # wrong length
end for;
checkValidationFunction("1BGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i", FALSE); # bad digest
end func;</lang>
checkValidationFunction("1A Na15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i", FALSE); # bad char
checkValidationFunction("1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62I", FALSE); # bad char
checkValidationFunction("1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62!", FALSE); # bad char
checkValidationFunction("1AGNa15ZQXAZUgFiqJ3i7Z2DPU2J6hW62i", FALSE); # bad digest
checkValidationFunction("1111111111111111111114oLvT2", TRUE); # okay
checkValidationFunction("17NdbrSGoUotzeGCcMMCqnFkEvLymoou9j", TRUE); # okay
checkValidationFunction("1badbadbadbadbadbadbadbadbadbadbad", FALSE); # wrong length
checkValidationFunction("BZbvjr", FALSE); # wrong length
checkValidationFunction("i55j", FALSE); # wrong length
checkValidationFunction("16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM", TRUE); # okay
end func;</syntaxhighlight>
 
{{out}}
<pre>
1Q1pE5vPGEEMqRcVRMbtBK842Y6Pzo6nK9: okay TRUE
1Q1pE5vPGEEMqRcVRMbtBK842Y6Pzo6nJ9: FALSE
1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i: okay
1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i: TRUE
1Q1pE5vPGEEMqRcVRMbtBK842Y6Pzo6nJ9: bad digest
1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62j: FALSE
1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62I: bad char
1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62X: FALSE
1ANNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i: FALSE
oMRDCDfyQhEerkaSmwCfSPqf3MLgBwNvs: FALSE
1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62iz: FALSE
1BGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i: FALSE
1A Na15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i: FALSE
1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62I: FALSE
1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62!: FALSE
1AGNa15ZQXAZUgFiqJ3i7Z2DPU2J6hW62i: FALSE
1111111111111111111114oLvT2: TRUE
17NdbrSGoUotzeGCcMMCqnFkEvLymoou9j: TRUE
1badbadbadbadbadbadbadbadbadbadbad: FALSE
BZbvjr: FALSE
i55j: FALSE
16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM: TRUE
</pre>
 
=={{header|Tcl}}==
{{tcllib|sha256}}
<langsyntaxhighlight lang="tcl">package require sha256
 
# Generate a large and boring piece of code to do the decoding of
Line 2,083 ⟶ 2,277:
}
return "$address is ok"
}</langsyntaxhighlight>
Testing if it works
<langsyntaxhighlight lang="tcl">puts [bitcoin_addressValid 1Q1pE5vPGEEMqRcVRMbtBK842Y6Pzo6nK9]
puts [bitcoin_addressValid 1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i]</langsyntaxhighlight>
{{out}}
<pre>
Line 2,092 ⟶ 2,286:
1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i is ok
</pre>
 
=={{header|UNIX Shell}}==
{{works with|bash}}
<langsyntaxhighlight lang="bash">base58=({1..9} {A..H} {J..N} {P..Z} {a..k} {m..z})
bitcoinregex="^[$(printf "%s" "${base58[@]}")]{34}$"
 
Line 2,122 ⟶ 2,315:
else return 2
fi
}</langsyntaxhighlight>
=={{header|Wren}}==
{{trans|Kotlin}}
{{libheader|Wren-crypto}}
{{libheader|wren-str}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="wren">import "./crypto" for Sha256
import "./str" for Str
import "./fmt" for Conv, Fmt
 
class Bitcoin {
static alphabet_ { "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz" }
 
static contentEquals_(ba1, ba2) {
if (ba1.count != ba2.count) return false
return !(0...ba1.count).any { |i| ba1[i] != ba2[i] }
}
 
static decodeBase58_(input) {
var output = List.filled(25, 0)
for (c in input) {
var p = alphabet_.indexOf(c)
if (p == -1) return null
for (j in 24..1) {
p = p + 58 * output[j]
output[j] = p % 256
p = p >> 8
}
if (p != 0) return null
}
return output
}
 
static sha256_(data, start, len, recursion) {
if (recursion == 0) return data
var md = Sha256.digest(data[start...start+len])
md = Str.chunks(md, 2).map { |x| Conv.atoi(x, 16) }.toList
return sha256_(md, 0, 32, recursion-1)
}
 
static validateAddress(address) {
var len = address.count
if (len < 26 || len > 35) return false
var decoded = decodeBase58_(address)
if (!decoded) return false
var hash = sha256_(decoded, 0, 21, 2)
return contentEquals_(hash[0..3], decoded[21..24])
}
}
 
var addresses = [
"1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i",
"1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62j",
"1Q1pE5vPGEEMqRcVRMbtBK842Y6Pzo6nK9",
"1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62X",
"1ANNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i",
"1A Na15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i",
"BZbvjr",
"i55j",
"1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62!",
"1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62iz",
"1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62izz",
"1Q1pE5vPGEEMqRcVRMbtBK842Y6Pzo6nJ9",
"1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62I"
]
for (address in addresses) {
Fmt.print("$-36s -> $s", address, Bitcoin.validateAddress(address) ? "valid" : "invalid")
}</syntaxhighlight>
 
{{out}}
<pre>
1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i -> valid
1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62j -> invalid
1Q1pE5vPGEEMqRcVRMbtBK842Y6Pzo6nK9 -> valid
1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62X -> invalid
1ANNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i -> invalid
1A Na15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i -> invalid
BZbvjr -> invalid
i55j -> invalid
1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62! -> invalid
1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62iz -> invalid
1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62izz -> invalid
1Q1pE5vPGEEMqRcVRMbtBK842Y6Pzo6nJ9 -> invalid
1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62I -> invalid
</pre>
 
=={{header|zkl}}==
Uses shared library zklMsgHash.
<langsyntaxhighlight lang="zkl">var [const] MsgHash=Import("zklMsgHash"); // SHA-256, etc
const symbols="123456789" // 58 characters: no cap i,o; ell, zero
"ABCDEFGHJKLMNPQRSTUVWXYZ"
Line 2,150 ⟶ 2,427:
(2).reduce(MsgHash.SHA256.fp1(1,dec),dec); // dec is i/o buffer
dec[0,4]==chkSum;
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">T("1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i","1Q1pE5vPGEEMqRcVRMbtBK842Y6Pzo6nK9",
"1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62X", // checksum changed, original data.
"1ANNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i", // data changed, original checksum.
"1A Na15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i", // invalid chars
"1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62izz", // too long
).apply(coinValide).println();</langsyntaxhighlight>
{{out}}
<pre>L(True,True,False,False,False,False)</pre>
{{omit from|Brlcad}}
{{omit from|GUISS}}
9,476

edits