Skip to content

Instantly share code, notes, and snippets.

@matthieu
Created March 7, 2015 22:43
Show Gist options
  • Save matthieu/d78bd39a6709c5be3dcd to your computer and use it in GitHub Desktop.
Save matthieu/d78bd39a6709c5be3dcd to your computer and use it in GitHub Desktop.
Sending op_return transactions with BlockCypher
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Block Cypher</title>
<!-- Bootstrap -->
<link href="css/bootstrap.css" rel="stylesheet">
<link href="css/font-awesome.css" rel="stylesheet">
<link href="css/style.css" rel="stylesheet">
<link href="css/custom.css" rel="stylesheet">
<link href="css/prism.css" rel="stylesheet">
<!--link href="css/animation.css" rel="stylesheet"-->
<link href="css/allinone_carousel.css" rel="stylesheet" type="text/css">
<link href='http://fonts.googleapis.com/css?family=Montserrat' rel='stylesheet' type='text/css'>
<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<!-- nav st-->
<nav class="navbar navbar-default navbar-fixed-top" role="navigation">
<div class="container">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed"> <span class="sr-only">Toggle navigation</span> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button>
<a class="navbar-brand" href="#">
<img src="img/logo_50px.png" alt="">
<div class="navbar-name">B<span class="smaller">LOCK</span><span class="alt-color">C<span class="smaller">YPHER</span></span></div>
</a>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<div class="single-page-nav">
<ul class="nav navbar-nav navbar-right">
<li><a href="../reference.html">Reference</a></li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li><a href="../index.html">Documentation</a></li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li><a href="http://www.blockcypher.com">Home</a></li>
</ul>
</div>
</div>
<!-- /.navbar-collapse -->
</div>
<!-- /.container-fluid -->
</nav>
<!-- nav nd-->
<!-- bg-primary st-->
<section id="home" class="bg-primary">
<div class="container">
<div class="row">
<div class="col-md-12 text-center">
<h1>BlockCypher &#10084; BitHack</h1>
<h2>@blockcypher — founders@blockcypher.com — &#9743; 415-868-5675</h2>
</div>
</div>
</div>
</div>
</section>
<section id="main" class="bg-danger">
<div class="container">
<div class="row">
<div class="col-md-12">
<form role="form" class="form-inline">
<div class="form-group">
<input type="text" class="box form-control input-lg" maxlength="40" width="40" style="width: 300px;" placeholder="Type something good and short..."/>
<button type="button" class="send btn btn-primary btn-lg" id="browser-websocket">Send it over!</button>
</div>
</form>
<div class="log" style="font-size: 24px; padding-top: 30px; min-height: 300px;"></div>
<script src="js/prism.js"></script>
<script src="js/bitcoinjs-min.js"></script>
<script src="http://code.jquery.com/jquery-2.1.0.min.js"></script>
</div>
</div>
</div>
</section>
<!--Footer st -->
<footer>
<div class="container">
<div class="row">
<div class="col-md-12 col-sm-6 col-md-6">
<p>We are social <a href="https://twitter.com/blockcypher"><i class="fa fa-twitter"></i></a> <a href="https://www.facebook.com/blockcypher"><i class="fa fa-facebook"></i></a> <!--a href="#"><i class="fa fa-linkedin"></i></a--> </p>
</div>
<div class="col-md-12 col-sm-6 col-md-6">
<p class="text-right">© 2014 - BlockCypher</p>
</div>
</div>
</div>
</footer>
<!--Footer nd -->
<script>
var bytesToHex = Bitcoin.convert.bytesToHex;
var hexToBytes = Bitcoin.convert.hexToBytes;
var mula = {
private : "1af97b1f428ac89b7d35323ea7a68aba8cad178a04eddbbf591f65671bae48a2",
public : "03bb318b00de944086fad67ab78a832eb1bf26916053ecd3b14a3f48f9fbe0821f",
address : "mtWg6ccLiZWw2Et7E5UqmHsYgrAi5wqiov"
}
function hexToString(hexstr) { return Bitcoin.convert.bytesToString(Bitcoin.convert.hexToBytes(hexstr)); };
function sign(data, address) {
var key = Bitcoin.ECKey.fromHex(address.private);
data.pubkeys = [];
data.signatures = data.tosign.map(function(tosign) {
data.pubkeys.push(address.public);
return bytesToHex(key.sign(hexToBytes(tosign)));
});
return data;
}
function sendNullData(str) {
var newtx = {
"inputs": [{"addresses": [mula.address]}],
"outputs": [{addresses: [str], value: 1, script_type: "null-data"}]
};
$.post("http://api.blockcypher.com/v1/btc/test3/txs/new", JSON.stringify(newtx), function(tx) {
var signedtx = sign(tx, mula);
$.post("http://api.blockcypher.com/v1/btc/test3/txs/send", JSON.stringify(signedtx), function(senttx) {
console.log("Sent", senttx.tx.hash);
});
});
}
$(function() {
$(".send").click(function() { sendNullData($(".box").val()); $(".box").val(''); })
var ws = new WebSocket("ws://socket.blockcypher.com/v1/btc/test3");
setInterval(function() {ws.send(JSON.stringify({event: 'ping'}))}, 5000)
ws.onmessage = function(evt) {
var tx = JSON.parse(evt.data);
if (tx.hash) {
$('.log').prepend("<div>"+tx.received.substring(11, 19)+" | "+tx.hash.substring(0, 8)+
"... &gt; " + hexToString(tx.outputs[0].script.substring(2)) + "</div>");
}
}
ws.onopen = function(event) {
ws.send(JSON.stringify({event: "unconfirmed-tx", script: "null-data"}));
}
});
</script>
</body>
</html>
@tanmay-joshi-tfs
Copy link

Here's the payload:
{"inputs":[{"addresses":["mnXp7HZ7aH4PjT21Q2BvWzeCdigXo1CQGX"]}], "outputs":[{"addresses":["Mystring"],"value":1,"script_type":"null-data"}]}:

@gregnomis
Copy link

Bump
I keep getting this error when attaching a 32 byte hash to a OP_RETURN:
Error creating transaction skeleton: {
errors: [ { error: 'Error no data to embed for OP_RETURN output 1.' } ],

my txskeleton:
Creating transaction skeleton with requestBody: {
"inputs": [
{
"addresses": [
"bc1qu05e8nvpy5f9pyyxs9c76drvksfqsy6gztf70u"
]
}
],
"outputs": [
{
"addresses": [
"bc1quul9kxpd63mghcgj6h5gc7g5c2zqmnmzex94ml"
],
"value": 700
},
{
"script_type": "null-data",
"data": "84e4111fc915e6465cc024512b590a93e7fd99bdc4b50f15e45b7cae3c7588ea",
"value": 0
}
],
"token": "8a2fe74g171c45379ad7bc3b655510ef"
}

any ideas?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment