This repository has been archived by the owner on Aug 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 310
/
InMemorySignalProtocolStore.js
119 lines (109 loc) · 3.8 KB
/
InMemorySignalProtocolStore.js
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
function SignalProtocolStore() {
this.store = {};
}
SignalProtocolStore.prototype = {
Direction: {
SENDING: 1,
RECEIVING: 2,
},
getIdentityKeyPair: function() {
return Promise.resolve(this.get('identityKey'));
},
getLocalRegistrationId: function() {
return Promise.resolve(this.get('registrationId'));
},
put: function(key, value) {
if (key === undefined || value === undefined || key === null || value === null)
throw new Error("Tried to store undefined/null");
this.store[key] = value;
},
get: function(key, defaultValue) {
if (key === null || key === undefined)
throw new Error("Tried to get value for undefined/null key");
if (key in this.store) {
return this.store[key];
} else {
return defaultValue;
}
},
remove: function(key) {
if (key === null || key === undefined)
throw new Error("Tried to remove value for undefined/null key");
delete this.store[key];
},
isTrustedIdentity: function(identifier, identityKey, direction) {
if (identifier === null || identifier === undefined) {
throw new Error("tried to check identity key for undefined/null key");
}
if (!(identityKey instanceof ArrayBuffer)) {
throw new Error("Expected identityKey to be an ArrayBuffer");
}
var trusted = this.get('identityKey' + identifier);
if (trusted === undefined) {
return Promise.resolve(true);
}
return Promise.resolve(util.toString(identityKey) === util.toString(trusted));
},
loadIdentityKey: function(identifier) {
if (identifier === null || identifier === undefined)
throw new Error("Tried to get identity key for undefined/null key");
return Promise.resolve(this.get('identityKey' + identifier));
},
saveIdentity: function(identifier, identityKey) {
if (identifier === null || identifier === undefined)
throw new Error("Tried to put identity key for undefined/null key");
var address = new libsignal.SignalProtocolAddress.fromString(identifier);
var existing = this.get('identityKey' + address.getName());
this.put('identityKey' + address.getName(), identityKey)
if (existing && util.toString(identityKey) !== util.toString(existing)) {
return Promise.resolve(true);
} else {
return Promise.resolve(false);
}
},
/* Returns a prekeypair object or undefined */
loadPreKey: function(keyId) {
var res = this.get('25519KeypreKey' + keyId);
if (res !== undefined) {
res = { pubKey: res.pubKey, privKey: res.privKey };
}
return Promise.resolve(res);
},
storePreKey: function(keyId, keyPair) {
return Promise.resolve(this.put('25519KeypreKey' + keyId, keyPair));
},
removePreKey: function(keyId) {
return Promise.resolve(this.remove('25519KeypreKey' + keyId));
},
/* Returns a signed keypair object or undefined */
loadSignedPreKey: function(keyId) {
var res = this.get('25519KeysignedKey' + keyId);
if (res !== undefined) {
res = { pubKey: res.pubKey, privKey: res.privKey };
}
return Promise.resolve(res);
},
storeSignedPreKey: function(keyId, keyPair) {
return Promise.resolve(this.put('25519KeysignedKey' + keyId, keyPair));
},
removeSignedPreKey: function(keyId) {
return Promise.resolve(this.remove('25519KeysignedKey' + keyId));
},
loadSession: function(identifier) {
return Promise.resolve(this.get('session' + identifier));
},
storeSession: function(identifier, record) {
return Promise.resolve(this.put('session' + identifier, record));
},
removeSession: function(identifier) {
return Promise.resolve(this.remove('session' + identifier));
},
removeAllSessions: function(identifier) {
for (var id in this.store) {
if (id.startsWith('session' + identifier)) {
delete this.store[id];
}
}
return Promise.resolve();
}
};