From 72ee6b17d055ef929f0ac8e261f02bc03ac8001b Mon Sep 17 00:00:00 2001 From: OutCast3k Date: Mon, 29 Dec 2014 22:55:28 +0000 Subject: [PATCH] custom data can now be added to the block chain using OP_RETURN when building a transaction --- index.html | 10 +++++++- js/coin.js | 16 +++++++++++++ js/coinbin.js | 64 ++++++++++++++++++++++++++++++++++++++------------- 3 files changed, 73 insertions(+), 17 deletions(-) diff --git a/index.html b/index.html index fb785bb..61aa67a 100644 --- a/index.html +++ b/index.html @@ -283,7 +283,7 @@

Transaction Create a new transaction

-

Use this page to create to create a raw transaction

+

Use this page to create a raw transaction

Address, WIF key or Multisig Redeem Script:
@@ -311,6 +311,14 @@
+ (40 byte limit) +

+ +

When using this option you may enter a hex string or address into the address field on the output tab.
+

+ +
+

The locktime indicates the earliest time a transaction can be added to the block chain.

diff --git a/js/coin.js b/js/coin.js index a1a2c1a..a303f31 100644 --- a/js/coin.js +++ b/js/coin.js @@ -383,9 +383,25 @@ o.value = new BigInteger('' + Math.round((value*1) * 1e8), 10); var s = coinjs.script(); o.script = s.spendToScript(address); + return this.outs.push(o); } + /* add data to a transaction */ + r.adddata = function(data){ + var r = false; + if(((data.match(/^[a-f0-9]+$/gi)) && data.length<80) && (data.length%2)==0) { + var s = coinjs.script(); + s.writeOp(106); // OP_RETURN + s.writeBytes(Crypto.util.hexToBytes(data)); + o = {}; + o.value = 0; + o.script = s; + return this.outs.push(o); + } + return r; + } + /* list unspent transactions */ r.listUnspent = function(address, callback) { coinjs.ajax(coinjs.host+'?uid='+coinjs.uid+'&key='+coinjs.key+'&setmodule=addresses&request=unspent&address='+address+'&r='+Math.random(), callback, "GET"); diff --git a/js/coinbin.js b/js/coinbin.js index 6016fa3..a411b67 100644 --- a/js/coinbin.js +++ b/js/coinbin.js @@ -361,9 +361,16 @@ $(document).ready(function() { } }); + $("#recipients .row").removeClass('has-error'); + $.each($("#recipients .row"), function(i,o){ - if($(".address",o).val()!="" && $(".amount",o).val()!=""){ - tx.addoutput($(".address",o).val(), $(".amount",o).val()); + 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'); } }); @@ -589,26 +596,44 @@ $(document).ready(function() { h += ''; h += ''; }); + $(h).appendTo("#verifyTransactionData .ins tbody"); h = ''; $.each(decode.outs, function(i,o){ - var addr = ''; - if(o.script.chunks.length==5){ - addr = coinjs.scripthash2address(Crypto.util.bytesToHex(o.script.chunks[2])); - } else { - var pub = coinjs.pub; - coinjs.pub = coinjs.multisig; - addr = coinjs.scripthash2address(Crypto.util.bytesToHex(o.script.chunks[1])); - coinjs.pub = pub; - } + if(o.script.chunks.length==2 && o.script.chunks[0]==106){ // OP_RETURN - h += ''; - h += ''; - h += ''+(o.value/100000000).toFixed(8)+''; - h += ''; - h += ''; + var data = Crypto.util.bytesToHex(o.script.chunks[1]); + var dataascii = hex2ascii(data); + + if(dataascii.match(/^[\s\d\w]+$/ig)){ + data = dataascii; + } + + h += ''; + h += ''; + h += ''+(o.value/100000000).toFixed(8)+''; + h += ''; + h += ''; + } else { + + var addr = ''; + if(o.script.chunks.length==5){ + addr = coinjs.scripthash2address(Crypto.util.bytesToHex(o.script.chunks[2])); + } else { + var pub = coinjs.pub; + coinjs.pub = coinjs.multisig; + addr = coinjs.scripthash2address(Crypto.util.bytesToHex(o.script.chunks[1])); + coinjs.pub = pub; + } + + h += ''; + h += ''; + h += ''+(o.value/100000000).toFixed(8)+''; + h += ''; + h += ''; + } }); $(h).appendTo("#verifyTransactionData .outs tbody"); @@ -618,6 +643,13 @@ $(document).ready(function() { } } + function hex2ascii(hex) { + var str = ''; + for (var i = 0; i < hex.length; i += 2) + str += String.fromCharCode(parseInt(hex.substr(i, 2), 16)); + return str; + } + function decodePrivKey(){ var wif = $("#verifyScript").val(); if(wif.length==51 || wif.length==52){