Skip to content

Instantly share code, notes, and snippets.

@resilience-me
Last active August 29, 2015 14:24
Show Gist options
  • Save resilience-me/c1c315ba8d1f5abc6556 to your computer and use it in GitHub Desktop.
Save resilience-me/c1c315ba8d1f5abc6556 to your computer and use it in GitHub Desktop.
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 users = []
// generate a wallet. The smart-wallet manages this wallet, and the user can only control it via the smart-wallet.
// The wallet is bound to the rules of the 'resilience network'. It will automatically send dividends based on the rate that the user has set.
app.get('/new_user/', function (req, res) {
var Resilience = function(){}
Resilience.prototype.create_smart_contract = function () {
// generate a key for the owner of this smart-wallet
var keygen = require('keygen');
var user_key = keygen.url(keygen.small);
// generate bitcoin address
var bitcore = require('bitcore');
var privateKey = new bitcore.PrivateKey();
var address = privateKey.toAddress();
console.log(address)
// and subscribe to transactions for this wallet
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
};
var dividendRate = ""
var dividend_pathways = []
var donation_instructions = {}
// provide some control to the user who has the smart-wallet's key (not the key to the wallet - the wallet can only be known by the contract - the contract's wallet is controlled by another key)
app.post('/'+user_key+'/send_payment/', function (req, res) {
var simpleTx = new bitcore.Transaction();
simpleTx.to(req.body.destination, req.body.amount);
simpleTx.sign(privateKey);
});
app.post('/'+user_key+'/edit_dividend_rate/', function (req, res) {
dividendRate = req.body.dividendRate
res.send(dividendRate)
});
app.post('/'+user_key+'/update_donation_behavior/', function (req, res) {
donation_instructions = req.body
res.send(donation_instructions)
});
users.push(address)
res.send(user_key)
}
var ResilienceService = new Resilience
var new_user = ResilienceService.create_smart_contract()
});
app.get('/total_users/', function(req, res) {
res.send(users)
});
app.listen(process.env.PORT || 8080);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment