Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save resilience-me/bc3988198575dc77bfec to your computer and use it in GitHub Desktop.
Save resilience-me/bc3988198575dc77bfec to your computer and use it in GitHub Desktop.
basic resilience-smart-contract framework. these will be launched on Ethereum and on NodeJS servers (think Codiu§) that log their data to the Horizon blockchain.
/** Bitcoin Smart-Contract with Resilience protocol
*
*/
var express = require('express');
var fs = require('fs');
var app = express();
var bodyParser = require('body-parser')
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))
// parse application/json
app.use(bodyParser.json())
var bitcore = require('bitcore')
var request = require('request')
/** The contract manages its own bitcoin address
*
*/
var privateKey = new bitcore.PrivateKey();
var address = privateKey.toAddress();
/** Now create keys to communicate with the smart-contract
*
*/
var smartContractKey = new bitcore.PrivateKey();
var smartContractAddress = smartContractKey.toAddress();
var dividendRate = ""
/** Subscribe to notifications for the contracts bitcoin address
*
*/
var WebSocket = require('ws');
var conn = new WebSocket("wss://ws.chain.com/v2/notifications");
conn.onopen = function (ev) {
var req = {type: "address", address:address, block_chain: "bitcoin"};
conn.send(JSON.stringify(req));
};
conn.onmessage = function (ev) {
var x = JSON.parse(ev.data);
console.log(x);
// add swarm-redistribution algorithm here
// https://github.com/p2p-safety-net-co-op-dividend-scheme/server/blob/master/connect_transaction.js
};
/** Let the issuer of the contract use the contracts funds
*
*/
app.post('/'+smartContractKey+'/send_payment/', function (req, res) {
request("https://blockchain.info/unspent?address="+address, function (error, response, body) {
if (!error && response.statusCode == 200) {
var body = JSON.parse(body)
var utxo = {
"txId" : body.unspent_outputs[0].tx_hash,
"outputIndex" : body.unspent_outputs[0].tx_output_n,
"address" : address,
"script" : body.unspent_outputs[0].script,
"satoshis" : body.unspent_outputs[0].value
};
var transaction = new bitcore.Transaction()
.from(utxo)
.to(req.body.destination, req.body.amount)
.sign(privateKey);
// send this to the Bitcoin network
console.log(transaction.serialize())
}
})
})
/** Let the issuer of the contract edit the contracts dividendRate
*
*/
app.post('/'+smartContractKey+'/edit_dividend_rate/', function (req, res) {
dividendRate = req.body.dividendRate
res.send(dividendRate)
});
app.listen(process.env.PORT || 8080);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment