forked from makevoid/blockchain-pen
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathkeychain.coffee
79 lines (57 loc) · 1.69 KB
/
keychain.coffee
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
env = if typeof window != "undefined" then "browser" else "node"
c = console
c.log "running in env: #{env}"
b = require 'bitcore'
if env == "node"
fs = require 'fs'
bc = require 'blockchain-api-basic'
BitcoreExt = require './bitcore_ext'
else
b = bitcore
bc = BchainApi
class KeyChain
key_path: "./.key"
constructor: ->
@privateKey = new b.PrivateKey @load_saved_key()
@address = @privateKey.toAddress()
@address_s = @address.toString()
@save_key()
balance: (cb) ->
bc.balance @address, cb
write: ->
#bc.write @privateKey, "test_message"
pen = new Pen
pen.write @privateKey, "test_message"
unspent: (callback) ->
# bpen posts to wite:
# EW #decentralized #ngrok #appidea #mkv # unrelated # TODO delete this comment
# EW
c.log "UNSPENT!"
c.log bc
bc.unspent(@address, callback)
.then (data) ->
c.log data.notice if data.notice
# logif data, "notice" # add styntactic sugar via libraries to write the line above
Promise.resolve data
load_saved_key: ->
if env == "node" then @load_saved_key_node() else @load_saved_key_browser()
load_saved_key_node: ->
path = @key_path
if fs.existsSync path
fs.readFileSync(path).toString()
load_saved_key_browser: ->
localStorage.bp_pvt_key if localStorage && localStorage.bp_pvt_key
save_key: ->
if env == "node" then @save_key_node() else @save_key_browser()
save_key_node: ->
fs.writeFileSync './.key', @privateKey
save_key_browser: ->
localStorage.bp_pvt_key = @privateKey
# accessors
privateKey: ->
@privateKey
address: ->
@address
address_s: ->
@address_s
module.exports = KeyChain if module?