RosettaCodeData/Task/Bitcoin-address-validation/Zkl/bitcoin-address-validation-...

26 lines
873 B
Plaintext

var [const] MsgHash=Import("zklMsgHash"); // SHA-256, etc
const symbols="123456789" // 58 characters: no cap i,o; ell, zero
"ABCDEFGHJKLMNPQRSTUVWXYZ"
"abcdefghijkmnopqrstuvwxyz";
fcn unbase58(str){ // --> Data (byte bucket)
out:=Data().fill(0,25);
str.pump(Void,symbols.index,'wrap(n){ // throws on out of range
[24..0,-1].reduce('wrap(c,idx){
c+=58*out[idx]; // throws if not enough data
out[idx]=c;
c/256; // should be zero when done
},n) : if(_) throw(Exception.ValueError("address too long"));
});
out;
}
fcn coinValide(addr){
reg dec,chkSum;
try{ dec=unbase58(addr) }catch{ return(False) }
chkSum=dec[-4,*]; dec.del(21,*);
// hash then hash the hash --> binary hash (instead of hex string)
(2).reduce(MsgHash.SHA256.fp1(1,dec),dec); // dec is i/o buffer
dec[0,4]==chkSum;
}