';
$("#inputs").append(clone);
$("#inputs .txidClear:last").remove();
$("#inputs .glyphicon-plus:last").removeClass('glyphicon-plus').addClass('glyphicon-minus');
$("#inputs .glyphicon-minus:last").parent().removeClass('txidAdd').addClass('txidRemove');
$("#inputs .txidRemove").unbind("");
$("#inputs .txidRemove").click(function(){
$(this).parent().parent().fadeOut().remove();
totalInputAmount();
});
$("#inputs .row:last input").attr('disabled',false);
$("#inputs .txIdAmount").unbind("").change(function(){
totalInputAmount();
}).keyup(function(){
totalInputAmount();
});
});
$("#transactionBtn").click(function(){
var tx = coinjs.transaction();
if(($("#nLockTime").val()).match(/^[0-9]+$/g)){
tx.lock_time = $("#nLockTime").val()*1;
}
$.each($("#inputs .row"), function(i,o){
if($(".txId",o).val()!="" && $(".txIdN",o).val()!=""){
tx.addinput($(".txId",o).val(), $(".txIdN",o).val(), $(".txIdScript",o).val());
}
});
$("#recipients .row").removeClass('has-error');
$.each($("#recipients .row"), function(i,o){
var a = ($(".address",o).val()).substr(0,80);
if(((a!="") && coinjs.addressDecode(a)) && $(".amount",o).val()!=""){ // address
tx.addoutput(a, $(".amount",o).val());
} else if (((($("#opReturn").is(":checked")) && a.match(/^[a-f0-9]+$/ig)) && a.length<80) && (a.length%2)==0) { // data
tx.adddata(a);
} else { // neither address nor data
$(o).addClass('has-error');
}
});
$("#transactionCreate textarea").val(tx.serialize());
$("#transactionCreate .txSize").html(tx.size());
$("#transactionCreate").removeClass("hidden");
});
$(".txidClear").click(function(){
$("#inputs .row:first input").attr('disabled',false);
$("#inputs .row:first input").val("");
totalInputAmount();
});
$("#inputs .txIdAmount").unbind("").change(function(){
totalInputAmount();
}).keyup(function(){
totalInputAmount();
});
$("#redeemFromBtn").click(function(){
var thisbtn = this;
var addr = '';
var isMultiSig = false;
var s = $("#redeemFrom").val();
$("#redeemFromStatus, #redeemFromAddress").addClass('hidden');
$(thisbtn).html("Please wait, loading...").attr('disabled',true);
var decode = coinjs.addressDecode(s);
if(decode.version == coinjs.pub){
addr = s;
} else if (decode.version == coinjs.priv){
var a = coinjs.wif2address(s);
addr = a['address'];
} else if (decode.version == coinjs.multisig){
$("#redeemFromStatus").removeClass('hidden').html(' You should use the redeem script, not the multisig address!');
} else {
var script = coinjs.script();
var decodeRs = script.decodeRedeemScript(s);
if(decodeRs){
addr = decodeRs['address'];
isMultiSig = true;
} else {
// input is neither a regular address or redeem script
$("#redeemFromStatus").removeClass('hidden').html(' The address or multisig redeem script you have entered is invalid');
}
}
var tx = coinjs.transaction();
tx.listUnspent(addr, function(data){
if(addr) {
if($("#clearInputsOnLoad").is(":checked")){
$("#inputs .txidRemove, #inputs .txidClear").click();
}
$("#redeemFromAddress").removeClass('hidden').html(' Retrieved unspent inputs from address '+addr+'');
$.each($(data).find("unspent").children(), function(i,o){
if($("#inputs .txId:last").val()!=""){
$("#inputs .txidAdd").click();
}
$("#inputs .row:last input").attr('disabled',true);
var val = (($(o).find("value").text()*1)/100000000);
var txid = (($(o).find("tx_hash").text()).match(/.{1,2}/g).reverse()).join("")+'';
$("#inputs .txId:last").val(txid);
$("#inputs .txIdN:last").val($(o).find("tx_output_n").text());
$("#inputs .txIdAmount:last").val(val.toFixed(8));
if(isMultiSig==true){
$("#inputs .txIdScript:last").val(s);
} else {
$("#inputs .txIdScript:last").val($(o).find("script").text());
}
});
}
$(thisbtn).html("Load").attr('disabled',false);
totalInputAmount();
});
});
function totalInputAmount(){
$("#totalInput").html('0.00');
$.each($("#inputs .txIdAmount"), function(i,o){
if(isNaN($(o).val())){
$(o).parent().addClass('has-error');
} else {
$(o).parent().removeClass('has-error');
var f = 0;
if(!isNaN($(o).val())){
f += $(o).val()*1;
}
$("#totalInput").html((($("#totalInput").html()*1) + (f*1)).toFixed(8));
}
});
totalFee();
}
function validateOutputAmount(){
$("#recipients .amount").unbind('');
$("#recipients .amount").keyup(function(){
if(isNaN($(this).val())){
$(this).parent().addClass('has-error');
} else {
$(this).parent().removeClass('has-error');
var f = 0;
$.each($("#recipients .amount"),function(i,o){
if(!isNaN($(o).val())){
f += $(o).val()*1;
}
});
$("#totalOutput").html((f).toFixed(8));
}
totalFee();
}).keyup();
}
function totalFee(){
var fee = (($("#totalInput").html()*1) - ($("#totalOutput").html()*1)).toFixed(8);
$("#transactionFee").val((fee>0)?fee:'0.00');
}
$("#optionsCollapse").click(function(){
if($("#optionsAdvanced").hasClass('hidden')){
$("#glyphcollapse").removeClass('glyphicon-collapse-down').addClass('glyphicon-collapse-up');
$("#optionsAdvanced").removeClass("hidden");
} else {
$("#glyphcollapse").removeClass('glyphicon-collapse-up').addClass('glyphicon-collapse-down');
$("#optionsAdvanced").addClass("hidden");
}
});
/* broadcast a transaction */
$("#rawSubmitBtn").click(function(){
var thisbtn = this;
var tx = coinjs.transaction();
$(thisbtn).val('Please wait, loading...').attr('disabled',true);
tx.broadcast(function(data){
$("#rawTransactionStatus").html(unescape($(data).find("response").text()).replace(/\+/g,' ')).removeClass('hidden');
if($(data).find("result").text()==1){
$("#rawTransactionStatus").addClass('alert-success').removeClass('alert-danger');
$("#rawTransactionStatus").html('txid: '+$(data).find("txid").text());
} else {
$("#rawTransactionStatus").addClass('alert-danger').removeClass('alert-success').prepend(' ');
}
$("#rawTransactionStatus").fadeOut().fadeIn();
$(thisbtn).val('Submit').attr('disabled',false);
}, $("#rawTransaction").val());
});
/* verify script code */
$("#verifyBtn").click(function(){
$(".verifyData").addClass("hidden");
$("#verifyStatus").hide();
if(!decodeRedeemScript()){
if(!decodeTransactionScript()){
if(!decodePrivKey()){
if(!decodePubKey()){
$("#verifyStatus").removeClass('hidden').fadeOut().fadeIn();
}
}
}
}
});
function decodeRedeemScript(){
var script = coinjs.script();
var decode = script.decodeRedeemScript($("#verifyScript").val());
if(decode){
$("#verifyRsData .multisigAddress").val(decode['address']);
$("#verifyRsData .signaturesRequired").html(decode['signaturesRequired']);
$("#verifyRsData table tbody").html("");
for(var i=0;i
').appendTo("#verifyRsData table tbody");
}
$("#verifyRsData").removeClass("hidden");
return true;
} else {
return false;
}
}
function decodeTransactionScript(){
var tx = coinjs.transaction();
try {
var decode = tx.deserialize($("#verifyScript").val());
// console.log(decode);
$("#verifyTransactionData .transactionVersion").html(decode['version']);
$("#verifyTransactionData .transactionSize").html(decode.size()+' bytes');
$("#verifyTransactionData .transactionLockTime").html(decode['lock_time']);
$("#verifyTransactionData").removeClass("hidden");
$("#verifyTransactionData tbody").html("");
var h = '';
$.each(decode.ins, function(i,o){
var s = decode.extractScriptKey(i);
h += '
';
h += '
';
h += '
'+o.outpoint.index+'
';
h += '
';
h += '
';
if(s['type']=='multisig' && s['signatures']>=1){
h += ' '+s['signatures'];
}
h += '
';
h += '
';
if(s['type']=='multisig'){
var script = coinjs.script();
var rs = script.decodeRedeemScript(s.script);
h += rs['signaturesRequired']+' of '+rs['pubkeys'].length;
} else {
h += '';
}
h += '
';
h += '
';
});
$(h).appendTo("#verifyTransactionData .ins tbody");
h = '';
$.each(decode.outs, function(i,o){
if(o.script.chunks.length==2 && o.script.chunks[0]==106){ // OP_RETURN
var data = Crypto.util.bytesToHex(o.script.chunks[1]);
var dataascii = hex2ascii(data);
if(dataascii.match(/^[\s\d\w]+$/ig)){
data = dataascii;
}
h += '