bucashcoins.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. package bucash
  2. import (
  3. "encoding/json"
  4. //"errors"
  5. "fmt"
  6. "strconv"
  7. . "template"
  8. "time"
  9. simplejson "github.com/go-simplejson"
  10. tml "github.com/toml-master"
  11. )
  12. var (
  13. Config config
  14. CoinApi BuCashApi
  15. )
  16. type Bucash struct {
  17. walletstatus bool
  18. }
  19. const (
  20. NameCoin = "BCC"
  21. )
  22. func (m Bucash) InitDriver(parm interface{}) bool {
  23. if m.initconfig() == false {
  24. return false
  25. }
  26. go m.getwalletinfo()
  27. return true
  28. }
  29. //get the server's total available balance
  30. func (m Bucash) CoinBalance(parm interface{}) ([]byte, error) {
  31. confirmedBalance, err := CoinApi.WalleGetBalance("*")
  32. if err != nil {
  33. LOG("ERROR", " GetBalance err:%v", err.Error())
  34. return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, GETBALANCE_ERR, getErrMsg(GETBALANCE_ERR, nil))
  35. }
  36. UnconfirmedBalance, err := CoinApi.WalleGetUnconfirmedBalance("")
  37. if err != nil {
  38. LOG("ERROR", " GetUnconfirmedBalance err:%s", err.Error())
  39. return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, GETUNCONFIRMEDBALANCE_ERR, getErrMsg(GETUNCONFIRMEDBALANCE_ERR, nil))
  40. }
  41. var jbuf []byte
  42. resp := make(map[string]interface{})
  43. resp["confirmedbalance"] = confirmedBalance
  44. resp["unconfirmed"] = UnconfirmedBalance
  45. resp["errcode"] = 0
  46. resp["exact"] = 0
  47. if jbuf, err = json.Marshal(resp); err != nil {
  48. LOG("ERROR", "lite inner json error:%v", err.Error())
  49. return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, INNER_ERR, getErrMsg(INNER_ERR, nil))
  50. }
  51. return jbuf, nil
  52. }
  53. //Returns up to 'count' most recent transactions skipping the first 'from' transactions for account 'account'.
  54. func (m Bucash) TransInfo(parm interface{}) ([]byte, error) {
  55. pjs := parm.(*simplejson.Json)
  56. startH := pjs.Get("starth").MustUint64()
  57. endH := pjs.Get("endh").MustUint64()
  58. if startH < 1 {
  59. LOG("DEBUG", " input para err:%v", startH)
  60. startH = 1
  61. }
  62. //get longest block count
  63. blockcount, err := CoinApi.WalleGetBlockCount()
  64. if err != nil {
  65. LOG("ERROR", " WalleGetBlockCount err:%v", err.Error())
  66. return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, GETCLOCKCOUNT_ERR, getErrMsg(GETCLOCKCOUNT_ERR, nil))
  67. }
  68. //get startblock hash by height
  69. if startH > blockcount {
  70. LOG("DEBUG", " startheight:%v > blockcount:%v", startH, blockcount)
  71. startH = blockcount
  72. }
  73. startblockhash, err := CoinApi.WalletGetBlockHash(uint64(startH - 1))
  74. if err != nil {
  75. LOG("ERROR", " WalletGetBlockHash startH:%v err:%v", startH, err.Error())
  76. return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, GETBLOCKINFO_ERR, getErrMsg(GETBLOCKINFO_ERR, nil))
  77. }
  78. //get endblock time by height
  79. if endH > blockcount {
  80. LOG("DEBUG", " endheight:%v > blockcount:%v", endH, blockcount)
  81. endH = blockcount
  82. }
  83. endblockhash, err := CoinApi.WalletGetBlockHash(uint64(endH))
  84. if err != nil {
  85. LOG("ERROR", " WalletGetBlockHash startH:%v err:%v", startH, err.Error())
  86. return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, GETBLOCKINFO_ERR, getErrMsg(GETBLOCKINFO_ERR, nil))
  87. }
  88. endblocktime, err := CoinApi.WalletGetBlockTime(endblockhash)
  89. if err != nil {
  90. LOG("ERROR", " WalletGetBlockTime endH:%v err:%v", endH, err.Error())
  91. return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, GETBLOCKINFO_ERR, getErrMsg(GETBLOCKINFO_ERR, nil))
  92. }
  93. //Get all transactions in blocks since block [blockhash]
  94. resp, err := CoinApi.WalleListSinceBlock(startblockhash)
  95. if err != nil {
  96. LOG("ERROR", " WalleListSinceBlock err:%v", err.Error())
  97. return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, TRANSINFO_ERR, getErrMsg(TRANSINFO_ERR, nil))
  98. }
  99. var litelisttransactionsinfo []Listtransactions
  100. if err = json.Unmarshal([]byte(resp), &litelisttransactionsinfo); err != nil {
  101. LOG("ERROR", " inner json error:%v", err.Error())
  102. return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, INNER_ERR, getErrMsg(INNER_ERR, nil))
  103. }
  104. outputsArr := make([]map[string]interface{}, 0)
  105. for _, listtransaction := range litelisttransactionsinfo {
  106. // get all transactions from block startHright to endHeight
  107. if listtransaction.Blocktime <= endblocktime {
  108. requestData := make(map[string]interface{})
  109. confirmationheight, err := CoinApi.WalletGetBlockHeight(listtransaction.Blockhash)
  110. if err != nil {
  111. LOG("ERROR", "WalletGetBlockHeight error:%v", err.Error())
  112. continue
  113. }
  114. requestData["confirmationheight"] = confirmationheight
  115. requestData["address"] = listtransaction.Address
  116. requestData["confirmationtimestamp"] = listtransaction.Timereceived
  117. requestData["value"] = listtransaction.Amount
  118. requestData["transactionid"] = listtransaction.Txid
  119. requestData["confirmations"] = listtransaction.Confirmations
  120. requestData["walletaddress"] = true
  121. requestData["vout"] = listtransaction.Vout
  122. outputsArr = append(outputsArr, requestData)
  123. }
  124. }
  125. var jbuf []byte
  126. resptmp := make(map[string]interface{})
  127. resptmp["errcode"] = 0
  128. resptmp["cointype"] = "BCC"
  129. resptmp["outputs"] = outputsArr
  130. if jbuf, err = json.Marshal(resptmp); err != nil {
  131. LOG("ERROR", " inner json error:%v", err.Error())
  132. return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, INNER_ERR, getErrMsg(INNER_ERR, nil))
  133. }
  134. return jbuf, nil
  135. }
  136. //Sent an amount from an account to a lite address.
  137. func (m Bucash) TransferAccounts(parm interface{}) ([]byte, error) {
  138. pjs := parm.(*simplejson.Json)
  139. amountTmp := pjs.Get("amount").MustString()
  140. destination := pjs.Get("address").MustString()
  141. if len(amountTmp) == 0 {
  142. LOG("ERROR", " amount illeagal")
  143. return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, AMOUNT_ERR, getErrMsg(AMOUNT_ERR, nil))
  144. }
  145. f, err := strconv.ParseFloat(amountTmp, 64)
  146. if err != nil {
  147. LOG("ERROR", " inner json error:%v", err.Error())
  148. return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, INNER_ERR, getErrMsg(INNER_ERR, nil))
  149. }
  150. amount := float64(f)
  151. transactionid, err := CoinApi.WalleSendFrom("", destination, amount, 1, "", "")
  152. if err != nil {
  153. LOG("ERROR", " Send coin error:%v", err.Error())
  154. return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, SENDCOINS_ERR, getErrMsg(SENDCOINS_ERR, nil))
  155. }
  156. if len(transactionid) != 0 {
  157. var jbuf []byte
  158. resp := make(map[string]interface{})
  159. resp["transcationids"] = []string{transactionid}
  160. resp["errcode"] = 0
  161. resp["msg"] = "ok"
  162. if jbuf, err = json.Marshal(resp); err != nil {
  163. LOG("ERROR", " inner json error:%v", err.Error())
  164. return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, INNER_ERR, getErrMsg(INNER_ERR, nil))
  165. }
  166. return jbuf, nil
  167. }
  168. LOG("ERROR", " transactionid is null error:%v", err.Error())
  169. return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, INNER_ERR, getErrMsg(INNER_ERR, nil))
  170. }
  171. //create a new address&key for receiving payments
  172. func (m Bucash) CreateAddress(parm interface{}) ([]byte, error) {
  173. addr, err := CoinApi.WalletNewAddr()
  174. if err != nil {
  175. LOG("ERROR", " get new address error:%v", err.Error())
  176. return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, GETNEWADDRESS_ERR, getErrMsg(GETNEWADDRESS_ERR, nil))
  177. }
  178. if len(addr) != 0 {
  179. //get key by the address
  180. key, err := CoinApi.WalletDumpPrivKey(addr)
  181. if err != nil {
  182. LOG("ERROR", " dump private key error:%v", err.Error())
  183. return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, DUMPPRIVKEY_ERR, getErrMsg(DUMPPRIVKEY_ERR, nil))
  184. }
  185. if len(key) != 0 {
  186. var jbuf []byte
  187. resp := make(map[string]interface{})
  188. resp["address"] = addr
  189. resp["privkey"] = key
  190. resp["errcode"] = 0
  191. if jbuf, err = json.Marshal(resp); err != nil {
  192. LOG("ERROR", " inner json error:%v", err.Error())
  193. return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, INNER_ERR, getErrMsg(INNER_ERR, nil))
  194. }
  195. return jbuf, nil
  196. }
  197. LOG("ERROR", " privkey is null error:%v")
  198. return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, INNER_ERR, getErrMsg(INNER_ERR, nil))
  199. }
  200. LOG("ERROR", " address is null error:%v")
  201. return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, INNER_ERR, getErrMsg(INNER_ERR, nil))
  202. }
  203. func (m Bucash) getwalletinfo() bool {
  204. fmt.Println("WalletInfo:")
  205. m.walletstatus = false
  206. for {
  207. if m.walletstatus == false {
  208. resp, err := CoinApi.WalletInfo()
  209. if err != nil {
  210. LOG("ERROR", "WalletInfo error:%v", err.Error())
  211. time.Sleep(time.Second * 1)
  212. continue
  213. }
  214. LOG("DEBUG", "WalletInfo resp:%v", string(resp))
  215. m.walletstatus = true
  216. }
  217. time.Sleep(time.Second)
  218. }
  219. }
  220. //config rpc from lite.toml
  221. func (m Bucash) initconfig() bool {
  222. if _, err := tml.DecodeFile("./src/bucash/bucash.toml", &Config); err != nil {
  223. fmt.Println(err)
  224. return false
  225. }
  226. fmt.Printf("%+v\n", Config)
  227. User := Config.Rpc.Rpcuser
  228. Password := Config.Rpc.Rpcpassword
  229. Host := Config.Rpc.Rpchost
  230. Port := Config.Rpc.Rpcport
  231. CoinApi.InitRpcConf(User, Password, Host, Port)
  232. initLiteErr()
  233. f, err := strconv.ParseFloat(Config.Limit.Fee, 64)
  234. if err != nil {
  235. LOG("ERROR", "inner json error:%v", err.Error())
  236. return false
  237. }
  238. fee := float64(f)
  239. CoinApi.WalletSetTxFee(fee)
  240. return true
  241. }
  242. func (m Bucash) ChainHeight(parm interface{}) ([]byte, error) {
  243. blockcount, err := CoinApi.WalleGetBlockCount()
  244. if err != nil {
  245. LOG("ERROR", " get current block count error:%v", err.Error())
  246. return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, GETCLOCKCOUNT_ERR, getErrMsg(GETCLOCKCOUNT_ERR, nil))
  247. }
  248. sendstr := fmt.Sprintf(`{"errcode":%v,"height":%v}`, OK, blockcount)
  249. return []byte(sendstr), nil
  250. }
  251. func (m Bucash) ListUnspent(parm interface{}) ([]byte, error) {
  252. pjs := parm.(*simplejson.Json)
  253. address, err := pjs.Get("address").StringArray()
  254. if err != nil {
  255. LOG("ERROR", " input parm error:%v", err.Error())
  256. return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, LISTUNSPENT_ERR, getErrMsg(LISTUNSPENT_ERR, nil))
  257. }
  258. unspent, err := CoinApi.Listunspent(address)
  259. if err != nil {
  260. LOG("ERROR", " get listunspent:%v", err.Error())
  261. return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, LISTUNSPENT_ERR, getErrMsg(LISTUNSPENT_ERR, nil))
  262. }
  263. var jbuf []byte
  264. resptmp := make(map[string]interface{})
  265. resptmp["errcode"] = 0
  266. resptmp["cointype"] = "BCC"
  267. resptmp["unspents"] = unspent
  268. if jbuf, err = json.Marshal(resptmp); err != nil {
  269. LOG("ERROR", " inner json error:%v", err.Error())
  270. return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, INNER_ERR, getErrMsg(INNER_ERR, nil))
  271. }
  272. return jbuf, nil
  273. }
  274. func (m Bucash) SendRawTransaction(parm interface{}) ([]byte, error) {
  275. pjs := parm.(*simplejson.Json)
  276. transdata := pjs.Get("data").MustString()
  277. if len(transdata) == 0 {
  278. return nil, fmt.Errorf(`{"cointype":"BCC","errcode":%v,"msg":"%v"}`, PARM_ERR, getErrMsg(PARM_ERR, []byte("hexstring is null")))
  279. }
  280. transactionhex, err := CoinApi.SendRawTransaction(transdata)
  281. if err != nil {
  282. LOG("ERROR", " SendRawTransaction err:%s", err.Error())
  283. return nil, fmt.Errorf(`{"cointype":"BCC","errcode":%v,"msg":"%s"}`, SEND_RAW_TRANSACTION_ERR, getErrMsg(SEND_RAW_TRANSACTION_ERR, nil))
  284. }
  285. var jbuf []byte
  286. resp := make(map[string]interface{})
  287. resp["cointype"] = "BCC"
  288. resp["transactionids"] = transactionhex
  289. resp["errcode"] = 0
  290. resp["msg"] = "OK"
  291. if jbuf, err = json.Marshal(resp); err != nil {
  292. LOG("ERROR", "inner json error:%v", err.Error())
  293. return nil, fmt.Errorf(`{"cointype":"BCC","errcode":%v,"msg":"%s"}`, INNER_ERR, getErrMsg(INNER_ERR, nil))
  294. }
  295. return jbuf, nil
  296. }
  297. func (m Bucash) SumcoinBalance(parm interface{}) ([]byte, error) {
  298. return nil, nil
  299. }
  300. func init() {
  301. Register(NameCoin, &Bucash{})
  302. }