forked from NxtChg/pieces
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbtc_validate.min.js
1 lines (1 loc) · 2.2 KB
/
btc_validate.min.js
1
function SHA256(n){function r(n,r){return n>>>r|n<<32-r}function e(n,r,e){return n&r^~n&e}function t(n,r,e){return n&r^n&e^r&e}function u(n){return r(n,2)^r(n,13)^r(n,22)}function f(n){return r(n,6)^r(n,11)^r(n,25)}function o(n){return r(n,7)^r(n,18)^n>>>3}function i(n){return r(n,17)^r(n,19)^n>>>10}function c(r){var e=r<<2,t=n[e]<<24|n[e+1]<<16|n[e+2]<<8|n[e+3];return r==a>>5&&(t|=128<<24-(31&a)),(r==h?a:t)>>>0}for(var a=8*n.length,h=15+(a+64>>9<<4),l=function(n,r){var c,a,h,l,g,s,v,d,A,b,p,w=new Array(64),y=new Array(1116352408,1899447441,3049323471,3921009573,961987163,1508970993,2453635748,2870763221,3624381080,310598401,607225278,1426881987,1925078388,2162078206,2614888103,3248222580,3835390401,4022224774,264347078,604807628,770255983,1249150122,1555081692,1996064986,2554220882,2821834349,2952996808,3210313671,3336571891,3584528711,113926993,338241895,666307205,773529912,1294757372,1396182291,1695183700,1986661051,2177026350,2456956037,2730485921,2820302411,3259730800,3345764771,3516065817,3600352804,4094571909,275423344,430227734,506948616,659060556,883997877,958139571,1322822218,1537002063,1747873779,1955562222,2024104815,2227730452,2361852424,2428436474,2756734187,3204031479,3329325298),H=new Array(1779033703,3144134277,1013904242,2773480762,1359893119,2600822924,528734635,1541459225);for(A=0;A<r;A+=16){for(c=H[0],a=H[1],h=H[2],l=H[3],g=H[4],s=H[5],v=H[6],d=H[7],b=0;b<64;b++)w[b]=b<16?n(b+A):i(w[b-2])+w[b-7]+o(w[b-15])+w[b-16],p=d+f(g)+e(g,s,v)+y[b]+w[b],d=v,v=s,s=g,g=l+p>>>0,l=h,h=a,a=c,c=p+u(a)+t(a,h,l)>>>0;H[0]+=c,H[1]+=a,H[2]+=h,H[3]+=l,H[4]+=g,H[5]+=s,H[6]+=v,H[7]+=d}return H}(c,h+1),g=[],s=0;s<32;s++)g.push(l[s>>2]>>>24-((3&s)<<3)&255);return g}function base58_decode(n){var r,e,t,u,f=[0];if(!n.length)return!1;for(r=0;r<n.length;r++){if((t="123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz".indexOf(n[r]))<0)return!1;for(e=0;e<f.length;e++)f[e]*=58;for(f[0]+=t,u=0,e=0;e<f.length;e++)f[e]+=u,u=f[e]>>8,f[e]&=255;for(;u;)f.push(255&u),u>>=8}for(r=0;"1"===n[r]&&r<n.length-1;r++)f.push(0);return f.reverse()}function btc_validate(n){var r=base58_decode(n);if(!1!==r&&25==r.length){if(SHA256(SHA256(r.slice(0,r.length-4))).slice(0,4)+""==r.slice(r.length-4)+""&&(0==r[0]||5==r[0]))return!0}return!1}