File size: 3,674 Bytes
0936134
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
var bitcoin = require('bitcoin')
  , log = require('./log')

module.exports = window.bitcoin = Bitcoin

function Bitcoin(opts) {
  if (!(this instanceof Bitcoin))
    return new Bitcoin(opts)

  this.opts = {
    host: opts.rpchost || '127.0.0.1'
  , port: opts.rpcport || 8999
  , user: opts.rpcuser
  , pass: opts.rpcpassword
  , timeout: 1000 * 60
  }
  log('opts ', this.opts)
  this.client = new bitcoin.Client(this.opts)
}

Bitcoin.prototype.getInfo = function getInfo(cb) {
  var self = this
  this.client.getInfo(function(err, out) {
    if (err) return cb(err)
    self.getBalance(function(err, balances) {
      if (err) return cb(err)
      out.balance = balances.balance
      out.unconfirmed = balances.unconfirmed
      cb(null, out)
    })
  })
}

Bitcoin.prototype.setAccount = function setAccount(addr, label, cb) {
  this.client.setAccount(addr, label, cb)
}

Bitcoin.prototype.getMinting = function getMinting(cb) {
  this.client.cmd('listminting', function(err, out) {
    if (err) return cb(err)
    out = out.sort(function(a, b) {
      return a.time < b.time
        ? -1
        : a.time > b.time
        ? 1
        : 0
    })

    cb(null, out)
  })
}

Bitcoin.prototype.getBalance = function getBalance(cb) {
  var self = this
  self.client.getBalance('*', 0, function(err, unconf) {
    if (err) return cb(err)
    self.client.getBalance('*', 6, function(err, conf) {
      if (err) return cb(err)
      unconf = (unconf - conf).toFixed(6)
      cb(null, {
        balance: conf
      , unconfirmed: unconf
      })
    })
  })
}

Bitcoin.prototype.getTransactions = function getTransactions(cb) {
  this.client.listTransactions('*', 1000, function(err, out) {
    if (err) return cb(err)
    out = out.map(function(item) {
      item.amount = item.amount.toFixed(6)
      return item
    })
    cb(null, out)
  })
}

Bitcoin.prototype.sendFunds = function sendFunds(data, cb) {
  this.client.sendToAddress(data.payee, +data.amount, data.label, cb)
}

Bitcoin.prototype.getAccounts = function getAccounts(cb) {
  this.client.listAddressGroupings(function(err, out) {
    if (err) return cb(err)
    return cb(null, parseAccounts(out).sort(sortAccounts))
  })
}

Bitcoin.prototype.listAccounts = function listAccounts(cb) {
  var self = this
  this.client.cmd('listaddressbook', function(err, results) {
    if (err) return cb(err)
    cb(null, results.sort(sortAccountList))
  })
}

Bitcoin.prototype.getAccountAddress = function getAccountAddress(acc, cb) {
  this.client.getAccountAddress(acc, cb)
}

Bitcoin.prototype.getBlockInfo = function getBlockInfo(cb) {
  var self = this
  self.client.getPeerInfo(function(err, pi) {
    if (err) return cb(err)
    if (!pi || !pi.length) return cb(null, 0)
    return cb(null, pi.reduce(function(set, item) {
      if (set < item.height) set = item.height
      return set
    }, 0))
  })
}

Bitcoin.prototype.createAccount = function createAccount(label, cb) {
  this.client.getNewAddress(label, cb)
}

function parseAccounts(data) {
  var len = data.length
  var out = []
  for (var i=0; i<len; i++) [
    out = out.concat(parseAccount(data[i]))
  ]

  return out
}

function parseAccount(data) {
  return data.map(function(item) {
    return {
      address: item[0]
    , balance: item[1]
    , label: item[2] || ''
    }
  })
}

function sortAccounts(a, b) {
  if (a.balance === b.balance) {
    return a.address < b.address
      ? -1
      : a.address > b.address
      ? 1
      : 0
  }
  return a.balance < b.balance
    ? 1
    : a.balance > b.balance
    ? -1
    : 0
}

function sortAccountList(a, b) {
  return a.account < b.account
    ? -1
    : a.account > b.account
    ? 1
    : 0
}