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 @@ - - +
+ +
+ From 4a661676b4b6681c50a3f15fec683075d8de013c Mon Sep 17 00:00:00 2001 From: Nugetzrul3 <52128634+Nugetzrul3@users.noreply.github.com> Date: Fri, 26 Jun 2020 19:53:42 +1000 Subject: [PATCH 2/5] Minor fix --- js/send.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/js/send.js b/js/send.js index 67e650a..a789a31 100644 --- a/js/send.js +++ b/js/send.js @@ -44,7 +44,7 @@ $("#sendTx").click(function () { var amount = $("#amountSUGAR").val() var receiver = $("#sendInput").val() Promise.resolve($.ajax({ - url: api + "/unspent/" + address,// + "?amount=" + amount, + url: api + "/unspent/" + address + "?amount=" + amount, dataType: 'json', type: 'GET' })).then(function(data) { From 5051acf7683c90a58e68f03f4ac1aa912e42aaff Mon Sep 17 00:00:00 2001 From: Nugetzrul3 <52128634+Nugetzrul3@users.noreply.github.com> Date: Sat, 27 Jun 2020 11:31:19 +1000 Subject: [PATCH 3/5] Add ask for confirmation --- js/send.js | 164 +++++++++++++++++++++++++++-------------------------- 1 file changed, 85 insertions(+), 79 deletions(-) diff --git a/js/send.js b/js/send.js index a789a31..04f121a 100644 --- a/js/send.js +++ b/js/send.js @@ -26,102 +26,108 @@ window.onload = function() { } $("#sendTx").click(function () { - var showErrororSuccess = $("#showErrororSuccess") - showErrororSuccess.text("Sending Transaction...") - var netconfig = { - 'network': { - 'messagePrefix': '\x19Sugarchain Signed Message:\n', - 'bip32': { - 'public': 0x0488b21e, - 'private': 0x0488ade4 - }, - 'bech32': 'sugar', - 'pubKeyHash': 0x3F, - 'scriptHash': 0x7D, - 'wif': 0x80} - } - var amount = $("#amountSUGAR").val() var receiver = $("#sendInput").val() - Promise.resolve($.ajax({ - url: api + "/unspent/" + address + "?amount=" + amount, - dataType: 'json', - type: 'GET' - })).then(function(data) { - txbuilder = bitcoin.TransactionBuilder(netconfig['network']) - txbuilder.setVersion(2) - - txbuilder.addOutput(receiver, amount) + var ask = confirm("Confirm Transaction. You are about to send " + amount + " SUGAR to " + receiver) + if (ask == true){ + var showErrororSuccess = $("#showErrororSuccess") + showErrororSuccess.text("Sending Transaction...") + var netconfig = { + 'network': { + 'messagePrefix': '\x19Sugarchain Signed Message:\n', + 'bip32': { + 'public': 0x0488b21e, + 'private': 0x0488ade4 + }, + 'bech32': 'sugar', + 'pubKeyHash': 0x3F, + 'scriptHash': 0x7D, + 'wif': 0x80} + } - var prevtxid = data.result[0].txid - var txindex = data.result[0].index - var txvalue = data.result[0].value + Promise.resolve($.ajax({ + url: api + "/unspent/" + address + "?amount=" + amount, + dataType: 'json', + type: 'GET' + })).then(function(data) { + txbuilder = bitcoin.TransactionBuilder(netconfig['network']) + txbuilder.setVersion(2) - var script = bitcoin.Buffer(data.result.script, 'hex') - var typeofaddress = scriptType(script) + txbuilder.addOutput(receiver, amount) - if (typeofaddress == 'bech32') { - var bech32script = bitcoin.payments.p2wpkh({'pubkey': wif.publicKey, 'network': netconfig['network']}) + var prevtxid = data.result[0].txid + var txindex = data.result[0].index + var txvalue = data.result[0].value - txbuilder.addInput(prevtxid, txindex, null, bech32script.output) - } + var script = bitcoin.Buffer(data.result.script, 'hex') + var typeofaddress = scriptType(script) - else { - txbuilder.addInput(txid, txindex) - } + if (typeofaddress == 'bech32') { + var bech32script = bitcoin.payments.p2wpkh({'pubkey': wif.publicKey, 'network': netconfig['network']}) - if (txvalue >= amount) { - var txchange = value - amount - if (change > 0) { - txbuilder.addOutput(receiver, txchange) + txbuilder.addInput(prevtxid, txindex, null, bech32script.output) } - 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") + else { + txbuilder.addInput(txid, txindex) } - 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) + if (txvalue >= amount) { + var txchange = value - amount + if (change > 0) { + txbuilder.addOutput(receiver, txchange) } - else { - showErrororSuccess.text("Broadcast Failed! Error: " + data.error) + 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") } - resetForm() - }) - - } + 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") - } - }) + else { + showErrororSuccess.text("Error: Not enough funds") + } + }) + } + else { + showErrororSuccess.text("You cancled the transaction") + } }) function scriptType(script) { From b028bbf04cbe0fb5dce0a96e68a3b6e8b5818cc9 Mon Sep 17 00:00:00 2001 From: Nugetzrul3 <52128634+Nugetzrul3@users.noreply.github.com> Date: Sun, 28 Jun 2020 08:08:10 +1000 Subject: [PATCH 4/5] Fix small typo --- js/send.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/js/send.js b/js/send.js index 04f121a..dd218cd 100644 --- a/js/send.js +++ b/js/send.js @@ -28,7 +28,7 @@ window.onload = function() { $("#sendTx").click(function () { var amount = $("#amountSUGAR").val() var receiver = $("#sendInput").val() - var ask = confirm("Confirm Transaction. You are about to send " + amount + " SUGAR to " + receiver) + confirm("Confirm Transaction. You are about to send " + amount + " SUGAR to " + receiver) if (ask == true){ var showErrororSuccess = $("#showErrororSuccess") showErrororSuccess.text("Sending Transaction...") @@ -69,12 +69,12 @@ $("#sendTx").click(function () { } else { - txbuilder.addInput(txid, txindex) + txbuilder.addInput(prevtxid, txindex) } if (txvalue >= amount) { - var txchange = value - amount - if (change > 0) { + var txchange = txvalue - amount + if (txchange > 0) { txbuilder.addOutput(receiver, txchange) } From f1d64a13027d005d5e4408dfc2bd82839fd16895 Mon Sep 17 00:00:00 2001 From: Nugetzrul3 <52128634+Nugetzrul3@users.noreply.github.com> Date: Sun, 28 Jun 2020 08:10:31 +1000 Subject: [PATCH 5/5] Minor mistake --- js/send.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/js/send.js b/js/send.js index dd218cd..0b50d9e 100644 --- a/js/send.js +++ b/js/send.js @@ -28,7 +28,7 @@ window.onload = function() { $("#sendTx").click(function () { var amount = $("#amountSUGAR").val() var receiver = $("#sendInput").val() - confirm("Confirm Transaction. You are about to send " + amount + " SUGAR to " + receiver) + ask = confirm("Confirm Transaction. You are about to send " + amount + " SUGAR to " + receiver) if (ask == true){ var showErrororSuccess = $("#showErrororSuccess") showErrororSuccess.text("Sending Transaction...")