Skip to content

Instantly share code, notes, and snippets.

@flavioespinoza
Created June 12, 2018 20:34
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 flavioespinoza/4f17076a4f6ec6bf1449dc6b0f174e3c to your computer and use it in GitHub Desktop.
Save flavioespinoza/4f17076a4f6ec6bf1449dc6b0f174e3c to your computer and use it in GitHub Desktop.
bitgo-node-server
const express = require('express')
const app = express()
const port = process.env.PORT || 9001
const http = require('http')
const index = require('./routes/index')
const server = http.createServer(app)
const socketIo = require('socket.io')
const io = socketIo(server)
const BitGoJS = require('bitgo')
const log = require('ololog').configure({locate: false})
const BASE58 = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
const bs58 = require('base-x')(BASE58)
const CryptoJS = require("crypto-js");
const sha256 = require('crypto-js/sha256')
app.use(index)
let keys
if (process.env.NODE_ENV === 'production') {
keys = require('./production')
} else {
keys = require('./development')
}
const wallet_params = {
name: 'bitgo',
apiKey: keys.bitgo_api_key,
secret: keys.bitgo_secret_key,
secret_passphrase: keys.secret_passphrase,
nonce: function () {
return this.milliseconds()
}
}
const bitgo = new BitGoJS.BitGo({
env: 'test', accessToken: wallet_params.secret
})
const divisor = 100000000
let __wallets = []
async function get_wallet_list (__coin, __socket) {
try {
return bitgo.coin(__coin).wallets().list({}).then(async function (wallets) {
let wallet_list = []
let all_wallets = wallets.wallets
for (let i = 0; i < all_wallets.length; i++) {
let id = all_wallets[i]._wallet.id
let single_wallet = await get_wallet(__coin, id, __socket)
if (!single_wallet.address) {
wallet_list.push({success: false, message: single_wallet, wallet: {}})
} else {
wallet_list.push({success: true, message: 'success', wallet: single_wallet})
}
}
__wallets = wallet_list
let list_name = __coin + '_wallet_list'
__socket.emit(list_name, wallet_list)
}).catch(function (__err) {
log.red('error_wallet', __err.message)
log.bright.red('get_wallet_list().then()')
__socket.emit('error_wallet', {
message: __err.message,
in_function: 'get_wallet_list().then()',
log_color: 'red'
})
})
} catch (__err) {
log.cyan('error_wallet', __err.message)
log.bright.cyan('get_wallet_list() try {} catch() {}')
__socket.emit('error_wallet', {
message: __err.message,
in_function: 'get_wallet_list() try {} catch() {}',
log_color: 'cyan'
})
}
}
async function get_wallet (__coin, __wallet_id, __socket) {
try {
return bitgo.coin(__coin).wallets().get({id: __wallet_id}).then(function (wallet) {
let wallet_info = {}
let __wallet = wallet._wallet
wallet_info.id = __wallet.id
wallet_info.label = __wallet.label
wallet_info.coin = __wallet.coin
wallet_info.address = __wallet.receiveAddress.address
wallet_info.satoshi = __wallet.balance
wallet_info.balance = wallet_info.satoshi / divisor
return wallet_info
}).catch(function (__err) {
log.red('error_wallet', __err.message)
log.bright.red('get_wallet().then()')
__socket.emit('error_wallet', {
message: __err.message,
in_function: 'get_wallet().then()',
log_color: 'red'
})
return __err.message
})
} catch (__err) {
log.cyan('error_wallet ', __err.message)
log.bright.cyan('get_wallet() try {} catch() {}')
__socket.emit('error_wallet', {
message: __err.message,
in_function: 'get_wallet() try {} catch() {}',
log_color: 'cyan'
})
}
}
/** Websocket */
io.on('connection', function (socket) {
console.log('connected...')
socket.on('get_tbtc_wallet_list', async function () {
await get_wallet_list('tbtc', socket)
})
socket.on('send_transaction', async function (__params) {
await send_transaction(__params)
})
})
async function send_transaction (__params) {
console.log(JSON.stringify(__params, null, 2))
log.red('wallet_id: ', __params.wallet_id)
if (__params.instruction === 'send to') {
let key = __params.label
let amount = __params.amount * 100000
bitgo.coin(__params.coin).wallets().get({ id: __params.wallet_id })
.then(function(wallet) {
let my_wallet = wallet
let address = wallet._wallet.receiveAddress.address
log.blue('address: ', address)
let params = {
recipients: [{
amount: amount,
address: address
}]
}
my_wallet.prebuildTransaction(params)
.then(function (transaction) {
console.log()
let params = {
txPrebuild: transaction,
prv: keys.bitgo_secret_key
}
log.lightRed(typeof params === 'object')
my_wallet.signTransaction(params)
.then(function (signature) {
log.cyan(signature)
})
.catch(function (error) {
log.lightYellow(error)
})
})
.catch(function (error) {
log.yellow(error)
})
})
}
}
const unhandledRejections = new Map()
process.on('unhandledRejection', (reason, p) => {
unhandledRejections.set(p, reason)
log.magenta('Unhandled Rejection at:', p, 'reason:', reason)
})
process.on('rejectionHandled', (p) => {
unhandledRejections.delete(p)
log.magenta('rejectionHandled', p)
})
process.on('uncaughtException', (err) => {
log.magenta(`uncaughtException: ${err}\n`)
})
server.listen(port, () => log.lightYellow(`Listening on port ${port}`))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment