Skip to content

Instantly share code, notes, and snippets.

@resilience-me
Last active August 29, 2015 14:24
Show Gist options
  • Save resilience-me/ed196ce279b083fb5182 to your computer and use it in GitHub Desktop.
Save resilience-me/ed196ce279b083fb5182 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())
/* Loading ripple-lib with Node.js */
var ripple = require('ripple-lib')
var Remote = ripple.Remote;
/* Loading ripple-lib in a webpage */
// var Remote = ripple.Remote;
var remote = new Remote({
// see the API Reference for available options
servers: [ 'wss://s1.ripple.com:443' ]
});
remote.connect(function() {
/* remote connected */
});
// 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.
var keygen = require('keygen');
var users = []
app.get('/new_user/', function (req, res) {
var Resilience = function(){}
Resilience.prototype.create_smart_contract = function (dividendRate) {
var wallet = ripple.Wallet.generate();
// generate a key for the owner of this smart-wallet
var user_key = keygen.url(keygen.small);
var dividendRate = dividendRate
var dividend_pathways = []
var donation_instructions = {}
// subscribe to transactions for this wallet
var req = remote.request_subscribe();
req.message.accounts = [wallet.address]
req.request();
remote.on('transaction', function(data){
console.log(data)
})
// 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) {
remote.setSecret(wallet.address, wallet.secret);
var transaction = remote.createTransaction('Payment', {
account: wallet.address,
destination: req.body.destination,
amount: {currency: req.body.currency, value: String(req.body.amount), issuer: wallet.address}
});
transaction.submit(function(err, res) {
/* handle submission errors / success */
console.log(err)
console.log(res)
});
});
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(wallet.address)
res.send(user_key)
}
var ResilienceService = new Resilience
var new_user = ResilienceService.create_smart_contract(4)
});
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