Jump to content

Bitcoin/address validation: Difference between revisions

Added Seed7 example
(Added nim implementation.)
(Added Seed7 example)
Line 1,975:
 
}</lang>
 
=={{header|Seed7}}==
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].
 
<lang seed7>$ include "seed7_05.s7i";
include "msgdigest.s7i";
 
const func string: unbase58 (in string: stri, inout string: out) is func
result
var string: state is "";
local
const string: digits is "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
var char: ch is ' ';
var integer: digit is 0;
var integer: index is 0;
begin
for ch range stri until state <> "" do
digit := pred(pos(digits, ch));
if digit = -1 then
state := "bad char";
else
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 string: valid (in string: stri) is func
result
var string: state is "";
local
var string: decoded is "\0;" mult 25;
begin
state := unbase58(stri, decoded);
if state = "" and sha256(sha256(decoded[.. 21]))[.. 4] <> decoded[22 ..] then
state := "bad digest";
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
for test range tests do
state := valid(test);
if state = "" then
writeln(test <& ": okay");
else
writeln(test <& ": " <& state);
end if;
end for;
end func;</lang>
 
{{out}}
<pre>
1Q1pE5vPGEEMqRcVRMbtBK842Y6Pzo6nK9: okay
1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i: okay
1Q1pE5vPGEEMqRcVRMbtBK842Y6Pzo6nJ9: bad digest
1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62I: bad char
</pre>
 
=={{header|Tcl}}==
{{tcllib|sha256}}
Cookies help us deliver our services. By using our services, you agree to our use of cookies.