forked from blockchain/unused-My-Wallet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblockchainapi.min.js
1 lines (1 loc) · 5.29 KB
/
blockchainapi.min.js
1
var BlockchainAPI=new function(){var b=this;var c=120000;var a=2;(function(d){d.retryAjax=function(g){var f;g.tryCount=(!g.tryCount)?0:g.tryCount;g.retryLimit=(!g.retryLimit)?2:g.retryLimit;g.suppressErrors=true;if(g.error){f=g.error;delete g.error}else{f=function(){}}g.complete=function(h,i){if(d.inArray(i,["timeout","abort","error"])>-1){this.tryCount++;if(this.tryCount<=this.retryLimit){if(this.tryCount===this.retryLimit){this.error=f;delete this.suppressErrors}d.ajax(this);return true}return true}};d.ajax(g)}}(jQuery));this.get_history=function(i,d,f,h,k){MyWallet.setLoadingText("Loading transactions");var j=(new Date()).getTime();if(!f){f=0}if(!h){h=0}if(!k){k=0}var g={active:MyWallet.getActiveAddresses().join("|"),format:"json",filter:f,offset:h,no_compact:true,ct:j,n:k,language:MyWallet.getLanguage(),symbol_btc:symbol_btc.code,symbol_local:symbol_local.code};$.retryAjax({type:"POST",dataType:"json",url:root+"multiaddr",data:g,timeout:c,retryLimit:a,success:function(m){if(m.error!=null){MyWallet.makeNotice("error","misc-error",m.error)}MyWallet.handleNTPResponse(m,j);try{if(h==0&&f==0){MyStore.put("multiaddr",JSON.stringify(m))}i(m)}catch(l){MyWallet.makeNotice("error","misc-error",l);d()}},error:function(l){if(l.responseText){MyWallet.makeNotice("error","misc-error",l.responseText)}else{MyWallet.makeNotice("error","misc-error","Error Downloading Wallet Balance")}d()}})};this.get_balances=function(g,f,d){MyWallet.setLoadingText("Getting Balances");$.ajax({type:"POST",url:root+"multiaddr",dataType:"json",data:{active:g.join("|"),simple:true,format:"json"},success:function(i){for(var h in i){if(MyWallet.addressExists(h)){MyWallet.setAddressBalance(h,i[h].final_balance)}}f(i)},error:function(h){d(h.responseText)}})};this.get_balance=function(g,f,d){MyWallet.setLoadingText("Getting Balance");this.get_balances(g,function(j){var i=0;for(var h in j){i+=j[h].final_balance}f(i)},d)};this.get_ticker=function(){MyWallet.setLoadingText("Getting Ticker Data");$.ajax({type:"GET",dataType:"json",url:root+"ticker",data:{format:"json"},success:function(g){var d=$("#send-ticker ul").empty();for(var f in g){d.append('<li><div style="width:35px;padding-left:10px;font-weight:bold;display:inline-block">'+f+'</div> <i class="icon-user" style="background-image:url('+resource+((g[f]["15m"]>=g[f]["24h"])?"up_green.png":"down_red.png")+');width:14px;background-position:0px"></i>'+g[f]["15m"]+"</li>")}},error:function(d){console.log(d)}})};this.resolve_firstbits=function(g,f,d){MyWallet.setLoadingText("Querying Firstbits");$.ajax({type:"GET",url:root+"q/resolvefirstbits/"+g,data:{format:"plain"},success:function(h){if(h==null||h.length==0){d()}else{f(h)}},error:function(h){d(h.responseText)}})};this.get_rejection_reason=function(h,d,f,g){MyWallet.setLoadingText("Querying Rejection Reason");$.ajax({type:"GET",url:root+"q/rejected/"+h,data:{format:"plain"},success:function(i){if(i==null||i.length==0){g()}else{d(i)}},error:function(i){if(i.status==404){f()}else{g(i.responseText)}}})};this.push_tx=function(i,l,o,m){try{MyWallet.setLoadingText("Pushing Transaction");var q=MyWallet.getTransactions();if(q.length>0){var j=q[0].txIndex}var t=i.serialize();var p=Crypto.util.bytesToHex(Crypto.SHA256(Crypto.SHA256(t,{asBytes:true}),{asBytes:true}).reverse());function r(){o();function s(){MyWallet.get_history(function(){if(q.length==0||q[0].txIndex==j){b.get_rejection_reason(p,function(u){MyWallet.makeNotice("error","tx-error",u)},function(){if(q.length==0||q[0].txIndex==j){MyWallet.get_history()}},function(){if(q.length==0||q[0].txIndex==j){MyWallet.makeNotice("error","tx-error","Unknown Error Pushing Transaction")}})}else{playSound("beep")}},function(){MyWallet.makeNotice("error","tx-error","Unable to determine if transaction was submitted. Please re-login.")})}setTimeout(function(){if(q.length==0||q[0].txIndex==j){s()}},3000)}function n(){var s=Crypto.util.bytesToHex(t);var u={format:"plain",tx:s,hash:p};if(l){u.note=l}$.ajax({type:"POST",url:root+"pushtx",data:u,success:function(){r()},error:function(v){m(v?v.responseText:null)}})}try{var h=new ArrayBuffer(t.length);var f=new Int8Array(h);f.set(t);var d=new Blob([h],{type:"application/octet-stream"});if(d.size!=t.length){throw"Inconsistent Data Sizes (blob : "+d.size+" s : "+t.length+" buffer : "+h.byteLength+")"}var g=new FormData();g.append("txbytes",d);if(l){g.append("note",l)}g.append("format","plain");g.append("hash",p);$.ajax({url:root+"pushtx",data:g,processData:false,contentType:false,type:"POST",success:function(){r()},error:function(s){if(!s.responseText||s.responseText.indexOf("Parse:")==0){setTimeout(function(){n()},2000)}else{m(s?s.responseText:null)}}})}catch(k){console.log(k);n()}}catch(k){console.log(k);m(k)}};this.get_unspent=function(g,i,f,h,d){MyWallet.setLoadingText("Getting Unspent Outputs");$.retryAjax({type:"POST",dataType:"json",url:root+"unspent",timeout:c,retryLimit:a,data:{active:g.join("|"),format:"json",confirmations:h?h:0},success:function(k){try{if(k.error!=null){throw k.error}if(k.notice!=null){MyWallet.makeNotice("notice","misc-notice",k.notice)}MyStore.put("unspent",JSON.stringify(k));i(k)}catch(j){f(j)}},error:function(j){if(d){f(e)}else{MyStore.get("unspent",function(k){try{if(k!=null){var m=$.parseJSON(k);i(m)}else{if(j.responseText){throw j.responseText}else{throw"Error Contacting Server. No unspent outputs available in cache."}}}catch(l){f(l)}})}}})}};