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