Skip to content

Instantly share code, notes, and snippets.

@ZeroDragon
Created February 28, 2016 21:23
Show Gist options
  • Save ZeroDragon/54cc4ca7dc51158885f5 to your computer and use it in GitHub Desktop.
Save ZeroDragon/54cc4ca7dc51158885f5 to your computer and use it in GitHub Desktop.
request = require 'request'
crypto = require 'crypto'
querystring = require 'querystring'
class Freezer
constructor: (data) ->
@coinWallet = data.coinWallet
@wallets = {
BTC : data.BTC
NBT : data.NBT
}
doRequest : (form,cb)->
form.nonce = new Date().getTime()
query = querystring.stringify form
hash = crypto.createHmac('sha512', @coinWallet.private).update(query).digest('hex')
options = {
method : "POST"
url : 'https://www.coinwallet.co/api/'
headers : {
Key : @coinWallet.public
Sign : hash
}
form : form
}
request options, (err,data,body)->
if err
cb err,null
else
cb null, JSON.parse body
###
# Get coinwallet coin Data from code
###
getCoin : (code,cb)->
form = {
method : 'getcurrencies'
}
@doRequest form, (err,data)->
throw err if err
cb data.response.filter((e)-> e.code is code)[0]
###
# Your wallet balance
###
getBalance : (code,cb)-> @getCoin code,(coin)=>
form = {
method : 'getbalance'
currency_id : coin.id
}
@doRequest form, (err,data)->
throw err if err
cb data.response
###
# Send Coins to other wallet
###
send : (code,address,value,cb)-> @getCoin code,(coin)=>
console.log coin
form = {
method : 'sendpayment'
currency_id : coin.id
target_address : address
send_value : value
}
@doRequest form, (err,data)->
cb data
###
# Request shapeshift transaction
###
getPassport : (payload,cb)->
returnAddress = @wallets[payload.from]
withdrawal = @wallets[payload.to]
pair = "#{payload.from.toLowerCase()}_#{payload.to.toLowerCase()}"
request.post 'https://shapeshift.io/shift', { form:{
returnAddress : returnAddress
withdrawal : withdrawal
pair : pair
}}, (err,data,body)->
throw err if err
cb JSON.parse body
###
# Freeze BTC into NBT
###
freeze : (ammount)->
_getAmmount = (cb) =>
if !ammount?
@getBalance 'BTC', (balance)->
cb balance.balance
else
cb ammount
_getAmmount (ammount)=>
@getPassport {from:'BTC',to:'NBT'}, (passport)=>
@send 'BTC',passport.deposit,ammount,(data)->
console.log data
###
# Thaw
###
thaw : (ammount)->
_getAmmount = (cb) =>
if !ammount?
@getBalance 'NBT', (balance)->
cb balance.balance
else
cb ammount
_getAmmount (ammount)=>
@getPassport {from:'NBT',to:'BTC'}, (passport)=>
@send 'NBT',passport.deposit,ammount,(data)->
console.log data
cons =
coinWallet : {
"public" : "CoinWallet Public",
"private" : "CoinWallet Private"
}
BTC : "BITCOIN WALLET"
NBT : "NBT WALLET"
freezer = new Freezer cons
freezer.freeze 0.0002
freezer.thaw 0.0002
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment