From da3703598139ea7acd765d7d7d1fcd12c5046787 Mon Sep 17 00:00:00 2001 From: Nugetzrul3 <52128634+Nugetzrul3@users.noreply.github.com> Date: Fri, 26 Jun 2020 19:42:00 +1000 Subject: [PATCH 1/5] Add send function --- js/send.js | 98 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- send.html | 6 ++-- 2 files changed, 99 insertions(+), 5 deletions(-) diff --git a/js/send.js b/js/send.js index c39fbac..67e650a 100644 --- a/js/send.js +++ b/js/send.js @@ -18,7 +18,7 @@ window.onload = function() { apiget = localStorage.getItem("api") if (apiget == null) { - api = "https://sugarchain.org" + api = "https://api.sugarchain.org" } else { api = apiget @@ -26,6 +26,8 @@ window.onload = function() { } $("#sendTx").click(function () { + var showErrororSuccess = $("#showErrororSuccess") + showErrororSuccess.text("Sending Transaction...") var netconfig = { 'network': { 'messagePrefix': '\x19Sugarchain Signed Message:\n', @@ -49,10 +51,100 @@ $("#sendTx").click(function () { txbuilder = bitcoin.TransactionBuilder(netconfig['network']) txbuilder.setVersion(2) + txbuilder.addOutput(receiver, amount) + var prevtxid = data.result[0].txid var txindex = data.result[0].index var txvalue = data.result[0].value - txbuilder.addOutput(receiver, amount) + var script = bitcoin.Buffer(data.result.script, 'hex') + var typeofaddress = scriptType(script) + + if (typeofaddress == 'bech32') { + var bech32script = bitcoin.payments.p2wpkh({'pubkey': wif.publicKey, 'network': netconfig['network']}) + + txbuilder.addInput(prevtxid, txindex, null, bech32script.output) + } + + else { + txbuilder.addInput(txid, txindex) + } + + if (txvalue >= amount) { + var txchange = value - amount + if (change > 0) { + txbuilder.addOutput(receiver, txchange) + } + + switch (typeofaddress) { + case 'bech32': + txbuilder.sign(0, wif, null, null, txvalue, null) + break + + case 'segwit': + var redeem = bitcoin.payments.p2wpkh({'pubkey': wif.publicKey, 'network': netconfig['network']}) + var segwitscript = bitcoin.payments.p2sh({'redeem': redeem, 'network': netconfig['network']}) + + txbuilder.sign(0, wif, segwitscript.output, null, txvalue, null) + break + + case 'legacy': + txbuilder.sign(0, wif) + break + + default: + showErrororSuccess.text("Bad UTXO") + } + + var txfinal = txbuilder.build() + + Promise.resolve($.ajax({ + 'url': api + "/broadcast/" + tx, + 'method': 'POST', + 'data': { + 'raw': txfinal.toHex() + } + })).then(function(data) { + if (data.error == null) { + showErrororSuccess.text("Success! Transaction ID: " + data.result) + } + + else { + showErrororSuccess.text("Broadcast Failed! Error: " + data.error) + } + + resetForm() + }) + + } + + else { + showErrororSuccess.text("Error: Not enough funds") + } }) -}) \ No newline at end of file +}) + +function scriptType(script) { + var type + + if (script[0] == bitcoin.opcodes.OP_0 && script[1] == 20) { + type = 'bech32' + } + + else if (script[0] == bitcoin.opcodes.OP_HASH160 && script[1] == 20) { + type = 'segwit' + } + + else if (script[0] == bitcoin.opcodes.OP_DUP && script[1] == bitcoin.opcodes.OP_HASH160 && script[2] == 20) { + type = 'legacy' + } + + return type + +} + +function resetForm() { + amount.val('') + receiver.val('') + showErrororSuccess.text('') +} \ No newline at end of file diff --git a/send.html b/send.html index 6d0ace9..b63b89e 100644 --- a/send.html +++ b/send.html @@ -49,8 +49,10 @@ - - +