1 | var Datastore, bs, defer, ecc, _;
|
2 |
|
3 | _ = require('lodash');
|
4 |
|
5 | ecc = require('ecc-tools');
|
6 |
|
7 | bs = require('bs58check');
|
8 |
|
9 | defer = require('when');
|
10 |
|
11 | Datastore = (function() {
|
12 | function Datastore(_store) {
|
13 | this._store = _store;
|
14 | if (!(this instanceof Datastore)) {
|
15 | return new Datastore(this._store);
|
16 | }
|
17 | }
|
18 |
|
19 | Datastore.prototype.put = function(data) {
|
20 | return defer(data).then((function(_this) {
|
21 | return function(data) {
|
22 | var hash;
|
23 | hash = ecc.checksum(data);
|
24 | return defer(_this._store.put(hash, data)).then(function() {
|
25 | return hash;
|
26 | });
|
27 | };
|
28 | })(this));
|
29 | };
|
30 |
|
31 | Datastore.prototype.get = function(hash) {
|
32 | return defer(hash).then((function(_this) {
|
33 | return function(hash) {
|
34 | if (typeof hash === 'string') {
|
35 | hash = bs.decode(hash);
|
36 | }
|
37 | return _this._store.get(hash);
|
38 | };
|
39 | })(this)).then(function(data) {
|
40 | return data;
|
41 | });
|
42 | };
|
43 |
|
44 | return Datastore;
|
45 |
|
46 | })();
|
47 |
|
48 | module.exports = Datastore;
|