Bitcoin/address validation: Difference between revisions
Content added Content deleted
(→{{header|Ruby}}: Add Rust implementation) |
(→{{header|zkl}}: update) |
||
Line 1,316: | Line 1,316: | ||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
Uses shared library zklMsgHash. |
Uses shared library zklMsgHash. |
||
<lang zkl>var MsgHash=Import("zklMsgHash"); // SHA-256, etc |
<lang zkl>var [const] MsgHash=Import("zklMsgHash"); // SHA-256, etc |
||
const symbols="123456789" // 58 characters: no cap i,o; ell, zero |
const symbols="123456789" // 58 characters: no cap i,o; ell, zero |
||
"ABCDEFGHJKLMNPQRSTUVWXYZ" |
"ABCDEFGHJKLMNPQRSTUVWXYZ" |
||
Line 1,322: | Line 1,322: | ||
fcn unbase58(str){ // --> Data (byte bucket) |
fcn unbase58(str){ // --> Data (byte bucket) |
||
out:=( |
out:=Data().fill(0,25); |
||
str.pump(Void,symbols.index,'wrap(n){ // |
str.pump(Void,symbols.index,'wrap(n){ // throws on out of range |
||
[24..0,-1].reduce('wrap(c,idx){ |
[24..0,-1].reduce('wrap(c,idx){ |
||
c+=58*out[idx]; |
c+=58*out[idx]; // throws if not enough data |
||
out[idx]=c; |
out[idx]=c; |
||
c/256; |
c/256; // should be zero when done |
||
},n) : if(_) throw(Exception.ValueError("address too long")); |
},n) : if(_) throw(Exception.ValueError("address too long")); |
||
}); |
}); |
||
Line 1,334: | Line 1,334: | ||
fcn coinValide(addr){ |
fcn coinValide(addr){ |
||
reg dec,chkSum; |
|||
try{ dec=unbase58(addr) }catch{ return(False) } |
|||
⚫ | |||
chkSum=dec[-4,*]; dec.del(21,*); |
|||
d1:=MsgHash.SHA256(MsgHash.SHA256(dec[0,21],1,False),1,False); |
|||
⚫ | |||
d1[0,4]==dec[-4,*]; // two byte blobs |
|||
(2).reduce(MsgHash.SHA256.fp1(1,dec),dec); // dec is i/o buffer |
|||
dec[0,4]==chkSum; |
|||
}</lang> |
}</lang> |
||
<lang zkl>T("1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i","1Q1pE5vPGEEMqRcVRMbtBK842Y6Pzo6nK9", |
<lang zkl>T("1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i","1Q1pE5vPGEEMqRcVRMbtBK842Y6Pzo6nK9", |