Bitcoin/address validation: Difference between revisions
Content deleted Content added
mNo edit summary |
Added zkl |
||
Line 756:
fi
}</lang>
=={{header|zkl}}==
Uses shared library zklMsgHash.
<lang zkl>var MsgHash=Import("zklMsgHash");
const symbols = "123456789" // 58 characters: no cap i,o; ell, zero
"ABCDEFGHJKLMNPQRSTUVWXYZ"
"abcdefghijkmnopqrstuvwxyz";
fcn unbase58(str){ // --> Data (byte bucket)
out:=(0).pump(25,Data,0);
str.pump(Void,symbols.index,'wrap(n){
[24..0,-1].reduce('wrap(c,idx){
c+=58*out[idx];
out[idx]=c;
c/256;
},n) : if(_) throw(Exception.ValueError("address too long"));
});
out;
}
fcn coinValide(addr){
reg dec; try{ dec=unbase58(addr) }catch{ return(False) }
// hash twice, once each time --> binary hash (instead of hex string)
d1:=MsgHash.SHA256(MsgHash.SHA256(dec[0,21],1,False),1,False);
d1[0,4]==dec[-4,*]; // two byte blobs
}</lang>
<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();</lang>
{{out}}
<pre>L(True,True,False,False,False,False)</pre>
{{omit from|Brlcad}}
|