forked from cryptobuks1/JsWallet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall-plugin.ls
More file actions
173 lines (169 loc) · 6.53 KB
/
install-plugin.ls
File metadata and controls
173 lines (169 loc) · 6.53 KB
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
161
162
163
164
165
166
167
168
169
170
171
172
173
require! {
\localStorage
\prelude-ls : { keys, any, map, filter, obj-to-pairs }
#react controls
\./modal.ls : { install, replace }
\../web3t/providers/superagent.ls : { get }
\./json-parse.ls
\./providers.ls
\../web3t/plugins/eth-coin.ls : eth
\../web3t/plugins/eth-legacy-coin.ls : eth_legacy
\../web3t/plugins/symblox.ls : syx
\../web3t/plugins/symblox-v2.ls : syx2
\../web3t/plugins/ltc-coin.ls : ltc
\../web3t/plugins/usdt-coin.ls : usdt
\../web3t/plugins/usdt_erc20.json : usdt_erc20
#\../web3t/plugins/vlx-coin.ls : vlx_evm
\../web3t/plugins/vlx_erc20-coin.ls : vlx_erc20
\../web3t/plugins/bnb-coin.ls : bnb
\../web3t/plugins/vlx_busd-coin.ls : vlx_busd
\../web3t/plugins/busd-coin.ls : busd
\../web3t/plugins/huobi-coin.ls : huobi
\../web3t/plugins/vlx-huobi-coin.ls : vlx_huobi
\../web3t/plugins/vlx-usdt-coin.ls : vlx_usdt
\../web3t/plugins/vlx-eth-coin.ls : vlx_eth
\../web3t/plugins/usdc-coin.ls : usdc
\../web3t/plugins/vlx_usdc-coin.ls : vlx_usdc
\../web3t/plugins/usdt_erc20_legacy-coin.json : usdt_erc20_legacy
\../web3t/plugins/bsc-vlx-coin.ls : bsc_vlx
\../web3t/plugins/vlx-evm-legacy-coin.ls : vlx_evm_legacy
}
current-configs = { vlx_eth, eth_legacy, syx, syx2, usdt, usdt_erc20, ltc, vlx_erc20, bnb, vlx_busd, busd, huobi, vlx_huobi, vlx_usdt, usdt_erc20_legacy, usdc, vlx_usdc, bsc_vlx, vlx_evm_legacy }
plugin-pairs = {
vlx_huobi: \huobi
busd: \bnb
vlx_erc20: \eth
#vlx_usdc: \usdc
bsc_vlx: \bnb
#usdc: <[ vlx_usdc ]>
}
required-fields = <[ type token enabled ]>
not-in = (arr, arr2)->
arr |> any -> arr2.index-of(it) is -1
verify-plugin = (plugin, cb)->
return cb "Expected Object" if typeof! plugin isnt \Object
return cb "Required fields: #{required-fields.join(', ')}" if required-fields `not-in` Object.keys(plugin)
cb null
get-registry = (cb)->
registry-string = local-storage.get-item(\plugin-registry) ? "[]"
json-parse registry-string, cb
get-plugin = (name, cb)->
coin-name = name.substr("plugin-".length)
if current-configs[coin-name]?
item = JSON.stringify current-configs[coin-name]
else
/* try to search in local storage for custom token */
item = local-storage.get-item(name)
if not item? or (item ? "").trim!.length is 0
console.error "[get-plugin] plugin #{name} not found"
return cb null
return cb null if typeof! item isnt \String
json-parse item, cb
get-plugin-one-by-one = ([item, ...rest], cb)->
return cb null, [] if not item?
err, plugin <- get-plugin item
console.error "[get-plugin-one-by-one] error:" err if err?
return cb err if err?
err, other <- get-plugin-one-by-one rest
return cb err if err?
all = if plugin? then (other ++ [plugin] ) else other
cb null, all
export get-install-list = (cb)->
err, data <- get-registry
return cb err if err?
err, res <- get-plugin-one-by-one data
return cb err if err?
cb null, res
save-registry = (registry)->
resulted-string = JSON.stringify registry
local-storage.set-item \plugin-registry, resulted-string
add-to-registry = (name, cb)->
err, registry <- get-registry
return cb err if err?
return cb "#{name} already installed" if registry.index-of(name) > -1
registry.push name
save-registry registry
cb null
remove-from-registry = (name, cb)->
err, registry <- get-registry
return cb err if err?
index = registry.index-of(name)
return cb null if index is -1
registry.splice index, 1
save-registry registry
cb null
build-name = (token)-> "plugin-#{token}"
install-plugins = (plugin, cb)->
result-plugins =
| plugin-pairs[plugin.token]? and typeof! plugin-pairs[plugin.token] is \Array =>
rest =
current-configs
|> obj-to-pairs
|> filter (it)->
it?1?token in plugin-pairs[plugin.token]
|> map (-> it?1)
[plugin] ++ rest
| plugin-pairs[plugin.token]? => [plugin, current-configs[plugin-pairs[plugin.token]]]
| _ => [plugin]
err <- install-all-plugins result-plugins
return cb err if err?
cb null
install-all-plugins = ([plugin, ...rest], cb)->
return cb null if not plugin?
err <- install-plugin(plugin)
return cb err if err?
err <- install-all-plugins(rest)
return cb err if err?
cb null
export install-plugin = (plugin, cb)->
err <- verify-plugin plugin
return cb err if err?
plugin.can-uninstall = yes
name = build-name plugin.token
body = JSON.stringify plugin
local-storage.set-item name, body
err <- add-to-registry name
console.error err if err?
cb null
uninstall-plugin = (cweb3, token, cb)->
return cb "expected string argument" if typeof! token isnt \String
name = build-name token
err <- remove-from-registry name
return cb err if err?
local-storage.set-item name, ""
cweb3.refresh cb
ask-user = (cweb3, store, plugin, cb)->
err, registry <- get-registry
return cb err if err?
return cb "pluing is required" if typeof! plugin isnt \Object
return cb "callback is required" if typeof! cb isnt \Function
return install store, plugin, cb if registry.index-of(plugin.token) is -1
replace store, plugin, cb
export build-install = (cweb3, store)-> (plugin, cb)->
return cb "Please unlock the wallet" if store.current.page is \locked
err <- verify-plugin plugin
return cb err if err?
err <- ask-user cweb3, store, plugin
return cb err if err?
err <- install-plugins plugin
return cb err if err?
cweb3.refresh cb
export build-quick-install = (cweb3, store)-> (plugin, cb)->
return cb "Please unlock the wallet" if store.current.page is \locked
err <- verify-plugin plugin
return cb err if err?
err <- install-plugins plugin
return cb err if err?
cweb3.refresh cb
export build-uninstall = (cweb3, store)-> (name, cb)->
uninstall-plugin cweb3, name, cb
export build-install-by-name = (cweb3, store)-> (name, cb)->
err, resp <- get "https://raw.githubusercontent.com/web3space/plugin-registry/master/plugins/#{name}.json" .end
return cb err if err?
err, plugin <- json-parse resp.text
return cb err if err?
return cb "type is required" if not plugin.type?
return cb "enabled is true" if plugin.enabled isnt yes
err <- install-plugin plugin
return cb err if err?
cweb3.refresh cb