Skip to content

Instantly share code, notes, and snippets.

@matzipan
Last active January 4, 2016 16:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matzipan/346e76bba792848545bc to your computer and use it in GitHub Desktop.
Save matzipan/346e76bba792848545bc to your computer and use it in GitHub Desktop.
You have the auth api defined by the authservice in the following angular module. The parameters are roughly defined by the rouchApiSketch.md file (each socket.io api call is mapped to a REST call in the backend.. Write an html/js + test angular example that allows the user to register, login, and change password.
var address = "<%= api.protocol %>://<%= api.host %>:<%= api.port %>";
var resources = angular.module("bc.resources", ['ngCookies', "btford.socket-io"]);
resources.factory("BackendSocket", ["socketFactory", function (socketFactory) {
var socket = socketFactory({
ioSocket: io.connect(address),
prefix: "backend"
});
return socket;
}]);
resources.factory("AuthService", ["BackendSocket", "$cookieStore", function (BackendSocket, $cookieStore) {
//@TODO write a wrapper function for authorized functions
var authTokenKey = "bc-auth-token";
var setToken = function (token) {
$cookieStore.put(authTokenKey, token);
};
var getToken = function () {
return $cookieStore.get(authTokenKey);
};
var unsetToken = function () {
$cookieStore.remove(authTokenKey);
}
var api = {
isAuthenticated: function () {
return typeof getToken() !== "undefined";
},
signup: function (args, callback) {
BackendSocket.emit("player-server/signup", args, callback);
},
login: function (args, callback) {
BackendSocket.emit("player-server/login", args, function (status, data) {
if (status === "success") {
setToken(data.authToken);
//@TODO fetch and store data
}
callback(status, data);
});
},
logout: function (args, callback) {
args.authToken = getToken();
BackendSocket.emit("player-server/logout", args, function () {
if (status === "success") {
unsetToken();
//@TODO consider refreshing the page
}
callback(status, data);
});
},
getChallengeSignature: function (args, callback) {
args.authToken = getToken();
BackendSocket.emit("player-server/getChallengeSignature", args, callback);
},
getUserInfo: function (args, callback) {
args.authToken = getToken();
BackendSocket.emit("player-server/getUserInfo", args, callback);
},
setUserAlias: function (args, callback) {
args.authToken = getToken();
BackendSocket.emit("player-server/setUserAlias", args, callback);
},
setUserPassword: function (args, callback) {
args.authToken = getToken();
BackendSocket.emit("player-server/setUserPassword", args, callback);
},
setUserAddress: function (args, callback) {
args.authToken = getToken();
BackendSocket.emit("player-server/setUserAddress", args, callback);
},
withdraw: function (args, callback) {
args.authToken = getToken();
BackendSocket.emit("player-server/withdraw", args, callback);
},
checkFaucet

#Player Server

##API Endpoints

###GET /catpcha

Get a captcha challenge

###POST /signup

Sign up a user

POST values

  • password: password
  • alias: optional, will generate a unique alias if not provided

###POST /login

Log in a user

POST values

  • password
  • alias

Returns

  • key: a session key to use for all subsequent requests

###POST /logout

Logout

POST values

  • alias
  • key: the session key

###PUT /users/sig

Get a challenege string for signed messages

PUT values

  • key

Returns

  • sig: the challenge string to be signed by the user

###POST /users

List all users

POST values

  • key

Returns an array of all users

###POST /users/:alias

Get a single user's info

POST values

  • key

Returns a user data object for the provided user alias

###PUT /users/alias

Update the user alias

PUT values

  • key
  • alias
  • password
  • sig: a signed message from the user

###PUT /users/email

Update the user email

PUT values

  • key
  • email
  • password
  • sig: a signed message from the user

###PUT /users/password

Update the user password

PUT values

  • key
  • newPass
  • password
  • sig: a signed message from the user

###PUT /users/addr

add a withdraw address

PUT values

  • key
  • password
  • address: the bitcoin address to add
  • currency: currently only 'btc' is supported
  • sig: a signed message from the user

###PUT /users/request-affiliate/btc

Request an affiliate account for the user

PUT values

  • key

###PUT /users/faucet/btc

Receive BTC in the user account once per hour

PUT values

  • key

###PUT /users/withdraw

Withdraw user's balance to their withdraw address

PUT values

  • key
  • currency: currenly only 'btc'

###GET /txs/btc

Get the transaction history

Body Params

  • key

###GET /txs/:alias/btc

Get the transaction history for a player

Body Params

  • key

###POST /txs

Add a transaction to the transaction database

This enpoint is currently locked down to a separate server, which only listens on the 172.17.0.0/16 subnet (which is the default subnet for docker). If you need to listen on a different subnet, set the environment variable DOCKER_SUBNET before running the player server

#!sh
DOCKER_SUBNET=192.168 node app

POST values

  • userId: the userId (mongo ObjectId) of the user the transaction is for
  • refId: the "id" of the thing that caused this transaction (e.g. a game ID, faucet timestamp)
  • type: a string identifying the source of the transaction (e.g. 'deposit', 'withdraw', '')
  • amtIn: the amount (in Satoshi) to add to the account (can be ommited or set to 0 for a withdrawal)
  • amtOut: the amount (in Satoshi) to be taken from the account (can be omitted or set to 0 for a deposit)
  • meta: an object containing any other data that should be stored with the transaction, single level key:value only please
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment