-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathsigner.min.js
1 lines (1 loc) · 26.6 KB
/
signer.min.js
1
function exceptionToString(b){var a="";for(var c in b){a+="property: "+c+" value: ["+b[c]+"]\n"}return"toString(): value: ["+b.toString()+"]"}try{if(typeof window=="undefined"||!window){importScripts("/Resources/wallet/bitcoinjs.min.js");self.addEventListener("message",function(d){var c=d.data;switch(c.cmd){case"sign_input":try{var a=new Bitcoin.Transaction(c.tx);var b=new Bitcoin.Script(c.connected_script);var f=signInput(a,c.outputN,c.priv_to_use,b);if(f){self.postMessage({cmd:"on_sign",script:f,outputN:c.outputN})}else{throw"Unknown Error Signing Script "+c.outputN}}catch(d){self.postMessage({cmd:"on_error",e:exceptionToString(d)})}break;default:self.postMessage({cmd:"on_error",e:"Unknown Command"})}},false)}}catch(e){}Bitcoin.Transaction.prototype.addOutputScript=function(a,b){if(arguments[0] instanceof Bitcoin.TransactionOut){this.outs.push(arguments[0])}else{if(b instanceof BigInteger){b=b.toByteArrayUnsigned().reverse();while(b.length<8){b.push(0)}}else{if(Bitcoin.Util.isArray(b)){}}this.outs.push(new Bitcoin.TransactionOut({value:b,script:a}))}};function showPrivateKeyModal(f,a,g){var d=$("#private-key-modal");d.modal({keyboard:true,backdrop:"static",show:true});d.find(".address").text(g);var c=null;var b=null;loadScript(resource+"wallet/llqrcode.js",function(){loadScript(resource+"wallet/qr.code.reader.js",function(){var h=initQRCodeReader("qr-code-reader",function(i){try{c=privateKeyStringToKey(i,detectPrivateKeyFormat(i));if(c==null){throw"Error decoding private key"}clearInterval(h)}catch(j){b="Error decoding private key "+j}d.modal("hide")},resource+"wallet/");d.on("hidden",function(){clearInterval(h)})})});d.find(".btn.btn-primary").unbind().click(function(){var h=d.find('input[name="key"]').val();try{if(h.length==0){throw"You must enter a private key to import"}c=privateKeyStringToKey(h,detectPrivateKeyFormat(h));if(c==null){throw"Could not decode private key"}}catch(i){b="Error importing private key "+i}d.modal("hide")});d.on("hide",function(){if(c){f(c)}else{a(b)}});d.find(".btn.btn-secondary").unbind().click(function(){d.modal("hide")})}function resolveAddress(a){a=$.trim(a);try{return new Bitcoin.Address(a).toString()}catch(d){}a=a.toLowerCase();for(var b in address_book){var c=address_book[b];if(c.toLowerCase()==a){return $.trim(b)}}for(var b in addresses){var c=addresses[b].label;if(c&&c.toLowerCase()==a){return b}}return null}function apiResolveFirstbits(c,b,a){setLoadingText("Getting Firstbits");$.get(root+"q/resolvefirstbits/"+c+"?format=plain").success(function(d){if(d==null||d.length==0){a()}else{b(d)}}).error(function(d){a()})}function startTxUI(a,i,d,c){try{var f=0;a.find('input[name="send-value"]').each(function(){f+=parseFloat($(this).val())});if(f>10){if(i=="email"||i=="facebook"||i=="sms"){throw"Cannot Send More Than 10 BTC via email or facebook"}else{if(i=="quick"){i="custom"}}}else{if(i=="anonymous"&&f<0.5){throw"The Minimum Amount You Can Send Anonymously is 0.5 BTC"}}if(mixer_fee&&mixer_fee<0&&(i=="custom"||i=="quick")&&f>=5&&getCookie("anonymous-never-ask")!="true"&&!c){var h=getCookie("anonymous-accepted-time");if(!h||parseInt(h)<new Date().getTime()-43200000){var k=$("#ask-for-anonymous");k.find(".bonus-percent").text(-mixer_fee);k.find(".bonus-value").text((f/100)*-mixer_fee);var j=k.find(".delay");if(f<=10){j.text("20 Seconds")}else{if(f<=25){j.text("10 Minutes")}else{if(f<=250){j.text("20 Minutes")}else{j.text("1 hour")}}}k.modal({keyboard:false,backdrop:"static",show:true});k.find(".btn.btn-primary").unbind().click(function(){var l=startTxUI(a,"anonymous",d);l.addListener({on_success:function(){SetCookie("anonymous-accepted-time",new Date().getTime())}});k.modal("hide")});k.find(".btn.btn-secondary").unbind().click(function(){startTxUI(a,i,d,true);k.modal("hide")});return}}var b={};if(i=="custom"||i=="anonymous"){var b={on_error:function(l){if(this.modal){this.modal.modal("hide")}},on_success:function(){buildVisibleView(true)},on_start:function(){var l=this;this.modal=$("#new-transaction-modal");this.modal.modal({keyboard:false,backdrop:"static",show:true});this.modal.find(".offline-transaction").hide();this.modal.find("#missing-private-key").hide();this.modal.find("#review-tx").hide();this.modal.find(".modal-header h3").html("Creating transaction");this.modal.find("#tx-sign-progress").hide();this.modal.find(".btn.btn-primary").attr("disabled",true);this.modal.find(".btn.btn-primary").text("Send Transaction");this.modal.find(".btn.btn-secondary").unbind().click(function(){l.cancel();l.modal.modal("hide")})},on_begin_signing:function(){$("#tx-sign-progress").show(200).find(".t").text(this.tx.ins.length)},on_sign_progress:function(l){$("#tx-sign-progress").find(".n").text(l)},on_finish_signing:function(){$("#tx-sign-progress").hide()}};d.addListener(b);d.ask_for_fee=function(o,n){var l=this;l.modal.modal("hide");var m=$("#ask-for-fee");m.modal({keyboard:false,backdrop:"static",show:true});m.find(".btn.btn-primary").unbind().click(function(){o();m.modal("hide")});m.find(".btn.btn-secondary").unbind().click(function(){n();m.modal("hide")});m.on("hidden",function(){l.modal.modal("show")})};d.ask_to_send=function(){var l=this;try{if(privateKeyToSweep==null){l.modal.find(".modal-header h3").html("Transaction Ready to Send.")}else{l.modal.find(".modal-header h3").html("Bitcoins Ready to Claim.")}l.modal.find("#missing-private-key").hide();l.modal.find("#review-tx").show();setReviewTransactionContent(l.modal,l.tx,l.type);l.modal.center();var m=l.modal.find(".btn.btn-primary");setLoadingText("Checking Connectivity");$.get(root+"ping?"+new Date().getTime()).success(function(o){m.attr("disabled",false);m.text("Send Transaction");m.unbind().click(function(){m.attr("disabled",true);l.modal.modal("hide");l.send()})}).error(function(o){l.modal.find(".modal-header h3").html("Created Offline Transaction.");m.attr("disabled",false);m.text("Show Offline Instructions");m.unbind().click(function(){m.attr("disabled",true);l.modal.find("#missing-private-key").hide();l.modal.find("#review-tx").hide();l.modal.find(".offline-transaction").show();var p=l.tx.serialize();var q=Crypto.util.bytesToHex(p);l.modal.find('.offline-transaction textarea[name="data"]').val(q)});l.modal.center()})}catch(n){l.error(n)}}}else{if(i=="quick"||i=="email"||i=="dice"||i=="facebook"||i=="sms"){var b={on_error:function(l){a.find(".send").show(200);if(this.p){this.p.hide()}},on_success:function(){try{a.find(".send").show(200);if(i!="dice"){buildVisibleView(true)}if(this.p){this.p.hide()}}catch(l){console.log(l)}},on_start:function(){this.p=a.find(".progress");a.find(".send").hide();this.p.show();this.p.children().css("width","10%")},on_begin_signing:function(){this.p.children().css("width","25%")},on_sign_progress:function(l){this.p.children().css("width",25+((100/this.tx.ins.length)*l)+"%")},on_finish_signing:function(){this.p.children().css("width","100%")}};d.addListener(b);if(i=="email"){d.ask_to_send=function(){var l=this;var m=$("#send-email-modal");try{$.post("/wallet",{guid:guid,sharedKey:sharedKey,method:"get-info",format:"plain"},function(o){try{m.modal({keyboard:true,backdrop:"static",show:true});var q=o.alias;if(q==null){q=o.email}if(q==null){q="Anonymous"}m.find(".amount").text(formatBTC(l.email_data.amount.toString()));m.find(".email").text(l.email_data.email);m.find(".frame").html('<iframe frameBorder="0" style="box-sizing:border-box;width:100%;height:100%" src="'+root+"email-template?from_name="+q+"&amount="+l.email_data.amount+'&priv=Preview&type=send-bitcoins-get"></iframe>');m.find(".btn.btn-secondary").unbind().click(function(){l.cancel();m.modal("hide")});m.find(".btn.btn-primary").unbind().click(function(){try{$.get(root+"send-via?type=email&format=plain&to="+l.email_data.email+"&guid="+guid+"&priv="+decryptPK(l.email_data.addr.priv)+"&sharedKey="+sharedKey+"&hash="+Crypto.util.bytesToHex(l.tx.getHash().reverse())).success(function(s){l.send()}).error(function(s){l.error(s?s.responseText:null)})}catch(r){l.error(r)}m.modal("hide")})}catch(p){m.modal("hide");l.error(p)}}).error(function(o){m.modal("hide");l.error("Error Getting Account Data")})}catch(n){m.modal("hide");l.error(n)}}}}}d.ask_for_private_key=function(n,m,o){var l=this;if(l.modal){l.modal.modal("hide")}showPrivateKeyModal(function(p){if(l.modal){l.modal.modal("show")}n(p)},function(p){if(l.modal){l.modal.modal("show")}m(p)},o)};d.type=i;getSecondPassword(function(){try{var t=false;var m=a.find('select[name="from"]');var q=m.val();if(q==null||q=="any"){d.from_addresses=getActiveAddresses()}else{if(m.attr("multiple")=="multiple"){d.from_addresses=q}else{d.from_addresses=[q]}}var p=a.find('select[name="change"]').val();if(p!=null){if(p=="new"){var u=generateNewAddressAndKey();d.change_address=u;d.generated_addresses.push(u.toString())}else{if(p!="any"){try{d.change_address=new Bitcoin.Address(p)}catch(s){throw"Invalid change address: "+s}}}}var l=a.find('input[name="fees"]').val();if(l!=null){d.fee=Bitcoin.Util.parseValue(l);if(d.fee.compareTo(BigInteger.ZERO)<0){throw"Fees cannot be negative"}}var n=a.find(".recipient");if(n.length==0){throw"A transaction must have at least one recipient"}var r=n.length;var o=function(){if(r==0){d.error("Nothing to send.");return}if(d.to_addresses.length<r){return}if(d.to_addresses.length>r){d.error("We seem to have more recipients than required. Unknown error");return}d.start()};n.each(function(){try{var C=$(this);var G=C.find('input[name="send-value"]');var J=C.find('input[name="send-to-address"]');var E=C.find('input[name="send-to-email"]');var I=C.find('input[name="send-to-facebook"]');var A=C.find('input[name="send-to-sms"]');var F=0;try{F=Bitcoin.Util.parseValue(G.val());if(F==null||F.compareTo(BigInteger.ZERO)<=0){throw"You must enter a value greater than zero"}}catch(K){if(G.data("optional")==true){--r;return true}else{throw"Invalid send amount"}}if(J.length>0){var v=$.trim(J.val()).replace(/[\u200B-\u200D\uFEFF]/g,"");if(v==null||v.length==0){throw"You must enter a bitcoin address for each recipient"}else{var z=resolveAddress(v);if(i=="anonymous"){var H=a.find('input[name="disclaimer"]');if(H.length!=0&&!H.is(":checked")){throw"You Must Agree To The Disclaimer."}if(!z){throw"Invalid Bitcoin Address"}setLoadingText("Creating Forwarding Address");$.post(root+"forwarder",{action:"create-mix",address:z,format:"plain"},function(O){try{console.log("Generated Forwarding Address "+O.input_address);if(O.destination!=z){throw"Mismatch between requested and returned destination address"}if(O.fee_percent!=mixer_fee){queryAPIMultiAddress();throw"The mixer fee may have changed"}d.to_addresses.push({address:new Bitcoin.Address(O.input_address),value:F});o()}catch(N){d.error(N)}}).error(function(N){d.error(N?N.responseText:null)})}else{if(z){d.to_addresses.push({address:new Bitcoin.Address(z),value:F})}else{apiResolveFirstbits(v,function(N){try{d.to_addresses.push({address:new Bitcoin.Address(N),value:F});o()}catch(O){d.error(O)}},function(){d.error("Invalid to address: "+v)});return false}}}}else{if(I.length>0){var L=$.trim(I.val());var w=generateNewAddressAndKey();var D=addresses[w.toString()];D.tag=2;D.label=L+" (Sent Via Facebook)";d.generated_addresses.push(D.addr);d.to_addresses.push({address:w,value:F});if(d.facebook){throw"Cannot send to more than one facebook recipient at a time"}var y=I.data("fb-id");if(y==null){y=L}d.facebook={to:y,addr:D,amount:F}}else{if(A.length>0){var x=$.trim(A.val());if(x.charAt(0)=="0"){x=x.substring(1)}if(x.charAt(0)!="+"){x="+"+C.find('select[name="sms-country-code"]').val()+x}var B=generateNewMiniPrivateKey();var D=addresses[B.addr.toString()];D.tag=2;D.label=x+" (Sent Via SMS)";d.generated_addresses.push(D.addr);d.to_addresses.push({address:B.addr,value:F});if(d.sms){throw"Cannot send to more than one SMS recipient at a time"}d.sms={number:x,miniKey:B.miniKey,addr:D,amount:F};d.ask_to_send=function(){try{var N=this;$.get(root+"send-via?type=sms&format=plain&to="+encodeURIComponent(N.sms.number)+"&guid="+guid+"&priv="+N.sms.miniKey+"&sharedKey="+sharedKey+"&hash="+Crypto.util.bytesToHex(N.tx.getHash().reverse())).success(function(P){N.send()}).error(function(P){N.error(P?P.responseText:null)})}catch(O){N.error(O)}}}else{if(E.length>0){var M=$.trim(E.val());if(validateEmail(M)){var w=generateNewAddressAndKey();var D=addresses[w.toString()];D.tag=2;D.label=M+" (Sent Via Email)";d.generated_addresses.push(D.addr);d.to_addresses.push({address:w,value:F});if(d.email){throw"Cannot send to more than one email recipient at a time"}d.email_data={email:M,addr:D,amount:F}}else{throw"Invalid Email Address"}}}}}}catch(K){console.log(K);d.error(K)}});o()}catch(s){d.error(s)}})}catch(g){d.error(g)}return d}function apiGetPubKey(c,b,a){setLoadingText("Getting Pub Key");$.get(root+"q/pubkeyaddr/"+c+"?format=plain").success(function(d){if(d==null||d.length==0){a()}else{b(Crypto.util.hexToBytes(d))}}).error(function(d){a()})}function apiGetRejectionReason(c,b,a){$.get(root+"q/rejected/"+c+"?format=plain").success(function(d){if(d==null||d.length==0){a()}else{b(d)}}).error(function(d){if(a){a()}})}function readVarInt(c){var a,b;a=c.splice(0,1)[0];if(a<253){b=[a]}else{if(a==253){b=c.splice(0,2)}else{if(a==254){b=c.splice(0,4)}else{b=c.splice(0,8)}}}return new BigInteger(b)}function readUInt32(a){return new BigInteger(a.splice(0,4).reverse()).intValue()}Bitcoin.Transaction.deserialize=function(k){var m=new Bitcoin.Transaction();m.version=readUInt32(k);var f=readVarInt(k).intValue();for(var l=0;l<f;l++){var g=k.splice(0,32);var c=Crypto.util.bytesToBase64(g);var a=readUInt32(k);var j=readVarInt(k).intValue();var n=new Bitcoin.Script(k.splice(0,j));var d=readUInt32(k);var o=new Bitcoin.TransactionIn({outpoint:{hash:c,index:a},script:n,sequence:d});m.ins.push(o)}var p=readVarInt(k).intValue();for(var l=0;l<p;l++){var b=k.splice(0,8);var j=readVarInt(k).intValue();var n=new Bitcoin.Script(k.splice(0,j));var h=new Bitcoin.TransactionOut({script:n,value:b});m.outs.push(h)}m.lock_time=readUInt32(k);return m};function getUnspentOutputs(b,c,a){setLoadingText("Getting Unspent Outputs");$.ajax({type:"POST",url:root+"unspent",data:{"addr[]":b,format:"json"},converters:{"* text":window.String,"text html":true,"text json":window.String,"text xml":$.parseXML},success:function(d){try{var g=$.parseJSON(d);if(g==null){throw"Unspent returned null object"}if(g.error!=null){throw g.error}if(g.notice!=null){makeNotice("notice","misc-notice",g.notice)}try{localStorage.setItem("unspent",d)}catch(f){}c(g)}catch(f){a(f)}},error:function(f){try{try{var d=localStorage.getItem("unspent");if(d!=null){var h=$.parseJSON(d);c(h);return}}catch(g){console.log(g)}if(f.responseText){throw f.responseText}else{throw"Error Contacting Server. No unspent outputs available in cache."}}catch(g){a(g)}}})}function signInput(j,c,b,m){var a=m.simpleOutPubKeyHash();var d=new Bitcoin.Address(a).toString();var n=new Bitcoin.ECKey(b);var k;if(n.getBitcoinAddress().toString()==d.toString()){k=false}else{if(n.getBitcoinAddressCompressed().toString()==d.toString()){k=true}else{throw"Private key does not match bitcoin address "+d.toString()+" = "+n.getBitcoinAddress().toString()+" | "+n.getBitcoinAddressCompressed().toString()}}var h=parseInt(1);var i=j.hashTransactionForSignature(m,c,h);var g=n.sign(i);var f=Bitcoin.ECDSA.serializeSig(g.r,g.s);f.push(h);var l;if(k){l=Bitcoin.Script.createInputScript(f,n.getPubCompressed())}else{l=Bitcoin.Script.createInputScript(f,n.getPub())}if(l==null){throw"Error creating input script"}return l}function formatAddresses(b,f,a){var g="";if(f.length==1){var c=f[0].toString();if(a&&addresses[c]!=null&&addresses[c].label!=null){g=addresses[c].label}else{if(a&&address_book[c]!=null){g=address_book[c]}else{g=c}}}else{g="Escrow (<i>";for(var d=0;d<f.length;++d){g+=f[d].toString()+", "}g=g.substring(0,g.length-2);g+="</i> - "+b+" Required)"}return g}function setReviewTransactionContent(r,u,a){$("#rtc-hash").html(Crypto.util.bytesToHex(u.getHash()));$("#rtc-version").html(u.version);$("#rtc-from").html("");$("#rtc-to").html("");var v=BigInteger.ZERO;var t=BigInteger.ZERO;var n=BigInteger.ZERO;var g="send ";var q=true;var c=BigInteger.ZERO;for(var o=0;o<u.ins.length;++o){var k=u.ins[o];t=t.add(k.outpoint.value);n=n.add(k.outpoint.value);var f=null;try{f=new Bitcoin.Address(k.script.simpleInPubKeyHash())}catch(s){f="Unable To Decode Address"}$("#rtc-from").append(f+' <font color="green">'+formatBTC(k.outpoint.value.toString())+" BTC <br />")}var j=true;for(var o=0;o<u.outs.length;++o){var p=u.outs[o];var d=p.value.slice();d.reverse();var w=new BigInteger(d);var h=[];var l=p.script.extractAddresses(h);$("#rtc-to").append(formatAddresses(l,h)+' <font color="green">'+formatBTC(w.toString())+" BTC </font><br />");v=v.add(w);t=t.subtract(w);if(h.length>1){if(!j){g+=" and "}g+="<b>"+formatBTC(w.toString())+" BTC</b> to "+formatAddresses(l,h,true);q=false;n=n.subtract(w)}else{if(h.length>0){var b=h[0].toString();if(addresses[b]==null||addresses[b].tag==2){if(b!=our_address){if(!j){g+=" and "}if(a&&a=="anonymous"){g+="<b>"+formatBTC(w.toString())+" BTC</b> Anonymously"}else{g+="<b>"+formatBTC(w.toString())+" BTC</b> to "+formatAddresses(1,[b],true)}q=false}}else{n=n.subtract(w);if(b!=our_address){c=c.add(w)}}}}j=false}if(t.compareTo(BigInteger.valueOf(1).multiply(BigInteger.valueOf(satoshi)))>=0){alert("Warning fees are very high for this transaction. Please double check each output!")}if(q==true){if(privateKeyToSweep==null){g="move <b>"+formatBTC(v.toString())+" BTC</b> between your own bitcoin addresses"}else{g="claim <b>"+formatBTC(c.toString())+" BTC</b> into your bitcoin wallet"}}$("#rtc-basic-summary").html(g);$("#rtc-effect").html("-"+formatBTC(n.toString())+" BTC");$("#rtc-fees").html(formatBTC(t.toString())+" BTC");$("#rtc-value").html(formatBTC(v.toString())+" BTC")}function initNewTx(){var b={generated_addresses:[],to_addresses:[],fee:BigInteger.ZERO,extra_private_keys:[],listeners:[],is_cancelled:false,ask_to_send_anonymously:false,addListener:function(c){this.listeners.push(c)},invoke:function(c,f,g){for(var d in this.listeners){if(this.listeners[d][c]){this.listeners[d][c].call(this,f,g)}}},start:function(){var c=this;try{c.invoke("on_start");getUnspentOutputs(c.from_addresses,function(k){try{if(c.is_cancelled){throw"Transaction Cancelled"}if(k.unspent_outputs==null||k.unspent_outputs.length==0){throw"No Free Outputs To Spend"}c.unspent=[];for(var h=0;h<k.unspent_outputs.length;++h){var f;try{f=new Bitcoin.Script(Crypto.util.hexToBytes(k.unspent_outputs[h].script))}catch(j){makeNotice("error","misc-error","Error decoding script: "+j);continue}var g={script:f,value:BigInteger.fromByteArrayUnsigned(Crypto.util.hexToBytes(k.unspent_outputs[h].value_hex)),tx_output_n:k.unspent_outputs[h].tx_output_n,tx_hash:k.unspent_outputs[h].tx_hash,confirmations:k.unspent_outputs[h].confirmations};c.unspent.push(g)}c.makeTransaction()}catch(j){c.error(j)}},function(f){c.error(f)})}catch(d){c.error(d)}},makeTransaction:function(){var t=this;try{if(t.is_cancelled){throw"Transaction Cancelled"}this.selected_outputs=[];var l=BigInteger.ZERO;for(var w=0;w<this.to_addresses.length;++w){l=l.add(this.to_addresses[w].value)}var u=(this.to_addresses.length==0);var y=false;for(var w=0;w<this.to_addresses.length;++w){var q=this.to_addresses[w];if(q.m!=null){y=true;break}}var f=BigInteger.ZERO;if(this.fee!=null){l=l.add(this.fee)}var B=0;var d=[];var g=[];var s=this.unspent;for(var w=0;w<s.length;++w){var x=s[w];try{var p=new Bitcoin.Address(x.script.simpleOutPubKeyHash()).toString();if(p==null){throw"Unable to decode output address from transaction hash "+x.tx_hash}if(s!=g){if(w==s.length-1){s=g;w=-1}else{if(addresses[p]!=null&&addresses[p].priv==null){g.push(x);continue}}}if(this.from_addresses!=null&&this.from_addresses.length>0&&$.inArray(p,this.from_addresses)==-1){continue}var m=Crypto.util.hexToBytes(x.tx_hash);var k=Crypto.util.bytesToBase64(Crypto.util.hexToBytes(x.tx_hash));var r=new Bitcoin.TransactionIn({outpoint:{hash:k,hexhash:m,index:x.tx_output_n,value:x.value},script:x.script,sequence:4294967295});if(!u&&x.value.compareTo(l)>=0){this.selected_outputs=[r];d=[p];B=x.value*x.confirmations;f=x.value;break}else{this.selected_outputs.push(r);d.push(p);B+=x.value*x.confirmations;f=f.add(x.value);if(!u&&f.compareTo(l)>=0){break}}}catch(A){makeNotice("info","tx-error",A)}}if(f.compareTo(l)<0){this.error("Insufficient funds. Value Needed "+formatBTC(l.toString())+" BTC. Available amount "+formatBTC(f.toString())+" BTC");return}if(this.selected_outputs.length==0){this.error("No Available Outputs To Spend.");return}var j=new Bitcoin.Transaction();for(var w=0;w<this.selected_outputs.length;w++){j.addInput(this.selected_outputs[w])}var v=false;for(var w=0;w<this.to_addresses.length;++w){var q=this.to_addresses[w];if(q.m!=null){j.addOutputScript(Bitcoin.Script.createMultiSigOutputScript(q.m,q.pubkeys),q.value)}else{j.addOutput(q.address,q.value)}}var z=f.subtract(l);if(z.compareTo(BigInteger.ZERO)>0){if(this.change_address!=null){j.addOutput(this.change_address,z)}else{if(!u&&d.length>0){j.addOutput(new Bitcoin.Address(d[Math.floor(Math.random()*d.length)]),z)}else{j.addOutput(new Bitcoin.Address(getPreferredAddress()),z)}}}var o=false;for(var w=0;w<j.outs.length;++w){var x=j.outs[w];var n=x.value.slice();n.reverse();var D=new BigInteger(n);if(D.compareTo(BigInteger.valueOf(100000))<0){o=true}else{if(D.compareTo(BigInteger.valueOf(1000000))<0){v=true}}}var c=j.serialize(j).length+(114*j.ins.length);B/=c;var h=parseInt(c/1024);var C=!t.fee||t.fee.compareTo(BigInteger.ZERO)==0;if(C&&o){t.fee=BigInteger.valueOf(50000);t.makeTransaction()}else{if(C&&(B<57600000||h>1||y||v)){t.ask_for_fee(function(){var i=BigInteger.valueOf(h);if(i&&i.compareTo(BigInteger.ZERO)>0){t.fee=BigInteger.valueOf(100000).multiply(i)}else{t.fee=BigInteger.valueOf(50000)}t.makeTransaction()},function(){t.tx=j;t.determinePrivateKeys(function(){t.signInputs()})})}else{t.tx=j;t.determinePrivateKeys(function(){t.signInputs()})}}}catch(A){this.error(A)}},ask_for_fee:function(d,c){d()},determinePrivateKeys:function(l){var c=this;try{if(c.is_cancelled){throw"Transaction Cancelled"}var j={};for(var f in c.selected_outputs){var d=c.selected_outputs[f].script;if(d.priv_to_use==null){var h=d.simpleOutPubKeyHash();var g=new Bitcoin.Address(h).toString();if(j[g]){d.priv_to_use=j[g]}else{if(c.extra_private_keys[g]){d.priv_to_use=Bitcoin.Base58.decode(c.extra_private_keys[g])}else{if(addresses[g]&&addresses[g].priv){try{d.priv_to_use=decodePK(addresses[g].priv)}catch(k){console.log(k)}}}}if(d.priv_to_use==null){c.ask_for_private_key(function(i){try{if(g==i.getBitcoinAddress().toString()||g==i.getBitcoinAddressCompressed().toString()){c.extra_private_keys[g]=Bitcoin.Base58.encode(i.priv);c.determinePrivateKeys(l)}else{throw"The private key you entered does not match the bitcoin address"}}catch(m){c.error(m)}},function(i){c.from_addresses=$.grep(c.from_addresses,function(m){return m!=g});c.makeTransaction()},g);return false}else{j[g]=d.priv_to_use}}}l()}catch(k){c.error(k)}},signWebWorker:function(k,h){try{var m=this;var d=0;var l=Math.min(3,m.tx.ins.length);m.worker=[];for(var c=0;c<l;++c){m.worker[c]=new Worker("/Resources/wallet/signer.min.js");m.worker[c].addEventListener("message",function(n){var i=n.data;try{switch(i.cmd){case"on_sign":m.invoke("on_sign_progress",parseInt(i.outputN)+1);m.tx.ins[i.outputN].script=new Bitcoin.Script(i.script);++d;if(d==m.tx.ins.length){m.terminateWorkers();k()}break;case"on_error":throw i.e}}catch(n){m.terminateWorkers();m.error(n)}},false);m.worker[c].addEventListener("error",function(i){h(i)})}for(var f in m.selected_outputs){var j=m.selected_outputs[f].script;m.worker[f%l].postMessage({cmd:"sign_input",tx:m.tx,outputN:f,priv_to_use:j.priv_to_use,connected_script:j})}}catch(g){h(g)}},signNormal:function(g,f){var d=this;var c=0;signOne=function(){setTimeout(function(){if(d.is_cancelled){f();return}try{d.invoke("on_sign_progress",c+1);var h=d.selected_outputs[c].script;var j=signInput(d.tx,c,h.priv_to_use,h);if(j){d.tx.ins[c].script=j;++c;if(c==d.tx.ins.length){g()}else{signOne()}}else{throw"Unknown error signing transaction"}}catch(i){f(i)}},1)};signOne()},signInputs:function(){var c=this;try{c.invoke("on_begin_signing");var f=function(){c.invoke("on_finish_signing");c.is_ready=true;c.ask_to_send()};c.signWebWorker(f,function(g){console.log(g);c.signNormal(f,function(h){c.error(h)})})}catch(d){c.error(d)}},terminateWorkers:function(){if(this.worker){for(var c in this.worker){try{this.worker[c].terminate()}catch(d){}}}},cancel:function(){if(!this.has_pushed){this.terminateWorkers();this.error("Transaction Cancelled")}},send:function(){var c=this;if(c.is_cancelled){c.error("This transaction has already been cancelled");return}if(!c.is_ready){c.error("Transaction is not ready to send yet");return}c.invoke("on_before_send");if(c.generated_addresses.length>0){c.has_saved_addresses=true;backupWallet("update",function(){c.pushTx()},function(){c.error("Error Backing Up Wallet. Cannot Save Newly Generated Keys.")})}else{c.pushTx()}},pushTx:function(){if(this.is_cancelled){return}var c=this;try{var d=this.tx.serialize();var f=Crypto.util.bytesToHex(d);if(f.length>=32768){this.error("My wallet cannot handle transactions over 32KB in size. Please try splitting your transaction,")}setLoadingText("Sending Transaction");if(transactions.length>0){var h=transactions[0]}c.has_pushed=true;$.post("/pushtx",{format:"plain",tx:f,format:"plain"},function(i){try{setTimeout(function(){if(transactions.length==0||transactions[0]==h){queryAPIMultiAddress(function(){if(transactions.length==0||transactions[0]==h){apiGetRejectionReason(Crypto.util.bytesToHex(c.tx.getHash().reverse()),function(k){c.error(k)},function(){c.error("Unknown Error Pushing Transaction")})}})}},3000);c.success()}catch(j){c.error(j)}}).error(function(i){c.error(i?i.responseText:null)})}catch(g){c.error(g)}},ask_for_private_key:function(d,c){c("Cannot ask for private key without user interaction disabled")},ask_to_send:function(){var n=this;for(var h=0;h<n.tx.ins.length;++h){var l=n.tx.ins[h];console.log("From : "+new Bitcoin.Address(l.script.simpleInPubKeyHash())+" => "+l.outpoint.value.toString())}var g=true;for(var h=0;h<n.tx.outs.length;++h){var f=n.tx.outs[h];var k=[];var d=f.script.extractAddresses(k);var j=f.value.slice();j.reverse();var c=new BigInteger(j);console.log("To: "+formatAddresses(d,k)+" => "+c.toString())}n.send()},error:function(c){if(this.is_cancelled){return}this.is_cancelled=true;if(!this.has_pushed&&this.generated_addresses.length>0){for(var d in this.generated_addresses){internalDeleteAddress(this.generated_addresses[d])}if(this.has_saved_addresses){backupWallet()}}this.invoke("on_error",c)},success:function(){this.invoke("on_success")}};var a={on_error:function(c){if(c){makeNotice("error","tx-error",c)}$(".send").attr("disabled",false)},on_success:function(c){try{$(".send").attr("disabled",false)}catch(c){console.log(c)}},on_start:function(c){$(".send").attr("disabled",true)},on_begin_signing:function(){this.start=new Date().getTime()},on_finish_signing:function(){console.log("Took "+(new Date().getTime()-this.start)+"ms")}};b.addListener(a);return b};