mirror of
https://github.com/HackPlan/RootPanel.git
synced 2026-01-13 07:01:20 +08:00
bitcoin plugin
This commit is contained in:
@@ -6,10 +6,6 @@ moment = require 'moment'
|
||||
redis = require 'redis'
|
||||
|
||||
global.app = express()
|
||||
global.config = require './config'
|
||||
global.i18n = require './core/i18n'
|
||||
global.utils = require './core/router/utils'
|
||||
global.pluggable = require './core/pluggable'
|
||||
|
||||
if fs.existsSync config.web.listen
|
||||
fs.unlinkSync config.web.listen
|
||||
@@ -39,7 +35,11 @@ exports.run = ->
|
||||
app.redis = redis.createClient 6379, '127.0.0.1',
|
||||
auth_pass: config.redis_password
|
||||
|
||||
app.i18n = require './core/i18n'
|
||||
app.utils = require './core/utils'
|
||||
app.config = require '../config'
|
||||
app.package = require './package.json'
|
||||
app.pluggable = require './core/pluggable'
|
||||
|
||||
app.use connect.json()
|
||||
app.use connect.urlencoded()
|
||||
|
||||
@@ -14,7 +14,7 @@ module.exports =
|
||||
default_timezone: 'Asia/Shanghai'
|
||||
|
||||
plugin:
|
||||
available_extensions: ['rpvhost']
|
||||
available_extensions: ['rpvhost', 'wiki', 'bitcoin']
|
||||
available_services: ['shadowsocks']
|
||||
|
||||
billing:
|
||||
@@ -41,6 +41,12 @@ module.exports =
|
||||
host: 'localhost'
|
||||
name: 'RootPanel'
|
||||
|
||||
redis:
|
||||
host: '127.0.0.1'
|
||||
port: 6379
|
||||
password: 'password'
|
||||
prefix: 'RP'
|
||||
|
||||
redis_password: 'password'
|
||||
|
||||
email:
|
||||
|
||||
@@ -1,25 +0,0 @@
|
||||
request = require 'request'
|
||||
|
||||
config = require './../config'
|
||||
|
||||
exports.genAddress = (bitcoin_secret, callback) ->
|
||||
request 'https://coinbase.com/api/v1/account/generate_receive_address',
|
||||
method: 'POST'
|
||||
json:
|
||||
api_key: config.bitcoin.coinbase_api_key
|
||||
address:
|
||||
callback_url: "#{config.web.url}/bitcoin/coinbase_callback?secret=#{bitcoin_secret}"
|
||||
, (err, res, body) ->
|
||||
callback body.address
|
||||
|
||||
exports.getExchangeRate = (callback) ->
|
||||
app.redis.get 'rp:exchange_rate:cnybtc', (err, rate) ->
|
||||
if rate
|
||||
callback rate
|
||||
else
|
||||
request 'https://blockchain.info/ticker', (err, res, body) ->
|
||||
body = JSON.parse body
|
||||
rate = 1 / parseFloat(body['CNY']['15m'])
|
||||
|
||||
app.redis.setex 'rp:exchange_rate:cnybtc', 60, rate, ->
|
||||
callback parseFloat rate
|
||||
@@ -1,8 +1,6 @@
|
||||
|
||||
nodemailer = require 'nodemailer'
|
||||
|
||||
config = require '../../config'
|
||||
bitcoin = require '../bitcoin'
|
||||
{config, pluggable} = app
|
||||
|
||||
mBalance = require './balance'
|
||||
|
||||
@@ -59,38 +57,46 @@ sample =
|
||||
ua: 'Mozilla/5.0 (Intel Mac OS X) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.102'
|
||||
]
|
||||
|
||||
exports.hashPassword = (password, password_salt) ->
|
||||
return exports.sha256(password_salt + exports.sha256(password))
|
||||
|
||||
exports.register = (username, email, password, callback) ->
|
||||
# @param account: username, email, password
|
||||
# @param callback(account)
|
||||
exports.register = (account, callback) ->
|
||||
password_salt = exports.randomSalt()
|
||||
bitcoin_secret = exports.randomSalt()
|
||||
|
||||
bitcoin.genAddress bitcoin_secret, (address) ->
|
||||
exports.insert
|
||||
_id: new ObjectID()
|
||||
username: username
|
||||
password: exports.hashPassword(password, password_salt)
|
||||
password_salt: password_salt
|
||||
email: email
|
||||
signup_at: new Date()
|
||||
group: []
|
||||
setting:
|
||||
avatar_url: "//ruby-china.org/avatar/#{crypto.createHash('md5').update(email).digest('hex')}?s=58"
|
||||
attribute:
|
||||
bitcoin_deposit_address: address
|
||||
bitcoin_secret: bitcoin_secret
|
||||
services: []
|
||||
plans: []
|
||||
balance: 0
|
||||
last_billing_at: new Date()
|
||||
arrears_at: null
|
||||
resources_limit: {}
|
||||
{username, email, password} = account
|
||||
|
||||
plugin: {}
|
||||
tokens: []
|
||||
, (err, result) ->
|
||||
callback err, result?[0]
|
||||
account =
|
||||
username: username
|
||||
password: exports.hashPassword(password, password_salt)
|
||||
password_salt: password_salt
|
||||
email: email
|
||||
created_at: new Date()
|
||||
|
||||
group: []
|
||||
|
||||
settings:
|
||||
avatar_url: "//ruby-china.org/avatar/#{app.utils.md5(email)}?s=58"
|
||||
language: config.i18n.default_language
|
||||
timezone: config.i18n.default_timezone
|
||||
|
||||
billing:
|
||||
services: []
|
||||
plans: []
|
||||
|
||||
balance: 0
|
||||
last_billing_at: Date()
|
||||
arrears_at: null
|
||||
|
||||
pluggable: {}
|
||||
|
||||
resources_limit: {}
|
||||
|
||||
tokens: []
|
||||
|
||||
async.each pluggable.hooks.account.before_register, (hook_callback, callback) ->
|
||||
hook_callback account, callback
|
||||
, ->
|
||||
exports.insert account, (err, result) ->
|
||||
callback _.first result
|
||||
|
||||
exports.updatePassword = (account, password, callback) ->
|
||||
password_salt = exports.randomSalt()
|
||||
|
||||
@@ -10,6 +10,11 @@ i18n = require './i18n'
|
||||
exports.plugins = {}
|
||||
|
||||
exports.hooks =
|
||||
account:
|
||||
username_filter: []
|
||||
# function(account, callback)
|
||||
before_register: []
|
||||
|
||||
view:
|
||||
layout:
|
||||
styles: []
|
||||
@@ -38,7 +43,7 @@ exports.initializePlugins = (callback) ->
|
||||
|
||||
async.parallel [
|
||||
(callback) ->
|
||||
async.each config.plugin.available_services, (name, callback) ->
|
||||
async.each config.plugin.available_extensions, (name, callback) ->
|
||||
initializePlugin name, (plugin) ->
|
||||
initializeExtension plugin, callback
|
||||
, callback
|
||||
|
||||
@@ -27,7 +27,7 @@ exports.createToken = (account, type, payload, callback) ->
|
||||
, ->
|
||||
callback token
|
||||
|
||||
# @param payload Must be flat
|
||||
# @param payload must be flat
|
||||
# @param callback(is_found)
|
||||
exports.revokeToken = (token, payload, callback) ->
|
||||
modifier =
|
||||
|
||||
@@ -6,6 +6,12 @@ exports.sha256 = (data) ->
|
||||
else
|
||||
return null
|
||||
|
||||
exports.md5 = (data) ->
|
||||
if data
|
||||
return crypto.createHash('md5').update(data).digest('hex')
|
||||
else
|
||||
return null
|
||||
|
||||
exports.randomSalt = ->
|
||||
return exports.sha256 crypto.randomBytes 256
|
||||
|
||||
@@ -16,3 +22,6 @@ exports.randomString = (length) ->
|
||||
return char_map.charAt Math.floor(Math.random() * char_map.length)
|
||||
|
||||
return result.join ''
|
||||
|
||||
exports.hashPassword = (password, password_salt) ->
|
||||
return exports.sha256(password_salt + exports.sha256(password))
|
||||
|
||||
47
plugin/bitcoin/index.coffee
Normal file
47
plugin/bitcoin/index.coffee
Normal file
@@ -0,0 +1,47 @@
|
||||
request = require 'request'
|
||||
|
||||
{pluggable, redis, config} = app
|
||||
|
||||
module.exports =
|
||||
name: 'bitcoin'
|
||||
type: 'extension'
|
||||
|
||||
# @param callback(address)
|
||||
genAddress = (bitcoin_secret, callback) ->
|
||||
request 'https://coinbase.com/api/v1/account/generate_receive_address',
|
||||
method: 'POST'
|
||||
json:
|
||||
api_key: config.bitcoin.coinbase_api_key
|
||||
address:
|
||||
callback_url: "#{config.web.url}/bitcoin/coinbase_callback?secret=#{bitcoin_secret}"
|
||||
, (err, res, body) ->
|
||||
throw err if err
|
||||
callback body.address
|
||||
|
||||
# @param currency: CNY, USD, JPY
|
||||
# @param callback(rate)
|
||||
getExchangeRate = (currency, callback) ->
|
||||
REDIS_KEY = "#{config.redis.prefix}:[bitcoin.getExchangeRate]:#{currency}"
|
||||
|
||||
redis.get REDIS_KEY, (err, rate) ->
|
||||
if rate
|
||||
callback rate
|
||||
else
|
||||
request 'https://blockchain.info/ticker', (err, res, body) ->
|
||||
throw err if err
|
||||
|
||||
body = JSON.parse body
|
||||
rate = 1 / parseFloat(body[currency]['15m'])
|
||||
|
||||
app.redis.setex REDIS_KEY, 60, rate, ->
|
||||
callback parseFloat rate
|
||||
|
||||
pluggable.hooks.account.before_register.psuh (account, callback) ->
|
||||
bitcoin_secret = exports.randomSalt()
|
||||
|
||||
genAddress bitcoin_secret, (address) ->
|
||||
account.pluggable.bitcoin =
|
||||
bitcoin_deposit_address: address
|
||||
bitcoin_secret: bitcoin_secret
|
||||
|
||||
callback()
|
||||
Reference in New Issue
Block a user