-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
wallet.clj
79 lines (61 loc) · 3.11 KB
/
wallet.clj
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
;; Freecoin - digital social currency toolkit
;; part of Decentralized Citizen Engagement Technologies (D-CENT)
;; R&D funded by the European Commission (FP7/CAPS 610349)
;; Copyright (C) 2015 Dyne.org foundation
;; Sourcecode designed, written and maintained by
;; Denis Roio <jaromil@dyne.org>
;; Aspasia Beneti <aspra@dyne.org>
;; With contributions by
;; Duncan Mortimer <dmortime@thoughtworks.com>
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU Affero General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU Affero General Public License for more details.
;; You should have received a copy of the GNU Affero General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
(ns freecoin-lib.db.wallet
(:require [freecoin-lib.core :as blockchain]
[clj-storage.core :as storage]))
(defn- empty-wallet [name email]
{:name name ;; identifier, case insensitive, space counts
:email email ;; verified email account
; :info nil ;; misc information text on the account
; :creation-date nil ;; date on which the wallet was created
; :last-login nil ;; last time this participant logged in succesfully
; :last-login-ip nil ;; connection ip address of the last succesful login
; :failed-logins nil ;; how many consecutive failed logins were attempted
:public-key nil ;; public asymmetric key for off-the-blockchain encryption
:private-key nil ;; private asymmetric key for off-the-blockchain encryption
:account-id nil ;; blockchain account id
})
(defn secret->apikey [secret]
(str (:cookie secret) "::" (:_id secret)))
(defn secret->participant-shares [secret]
(take 4 (:slices secret)))
(defn secret->organization-shares [secret]
(->> (:slices secret)
(drop 3) (take 4)))
(defn secret->auditor-shares [secret]
(take-last 3 (:slices secret)))
(defn new-empty-wallet! [wallet-store blockchain name email]
(let [{:keys [account-id account-secret]} (blockchain/create-account blockchain name)
wallet (-> (empty-wallet name email)
(assoc :account-id account-id))]
{:wallet (storage/store! wallet-store :email wallet)
:apikey (secret->apikey account-secret)
:participant (secret->participant-shares account-secret)
:organization (secret->organization-shares account-secret)
:auditor (secret->auditor-shares account-secret)}))
(defn fetch [wallet-store email]
(storage/fetch wallet-store email))
(defn fetch-by-name [wallet-store name]
(first (storage/query wallet-store {:name name})))
(defn fetch-by-account-id [wallet-store name]
(first (storage/query wallet-store {:account-id name})))
(defn query
([wallet-store] (query wallet-store {}))
([wallet-store query-m] (storage/query wallet-store query-m)))