forked from Pauli-Group/LamportWalletManager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMultiSourceKeyTracker.ts
65 lines (56 loc) · 2 KB
/
MultiSourceKeyTracker.ts
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
import BaseKeyTracker from "./BaseKeyTracker";
import KeyTrackerA from "./KeyTrackerA";
import KeyTrackerB from "./KeyTrackerB";
import KeyTrackerC from "./KeyTrackerC";
/**
* @name MultiSourceKeyTracker
* @description A class that keeps track of keys and allows you to get them
* @date March 28th 2023
* @author William Doyle
*/
export default class MultiSourceKeyTracker extends BaseKeyTracker {
keyTrackers: BaseKeyTracker[] = []
get count() {
return this.keyTrackers.reduce((a, b) => a + b.count, 0)
}
get exhausted() {
return this.count === 0
}
more(amount: number = 2) {
// Default to most secure key tracker
return this.moreTypeA(amount)
}
getOne() {
// get the first kley tracker that is not exhausted
const keyTracker = this.keyTrackers.find(k => !k.exhausted)
if (keyTracker === undefined)
throw new Error('No keys left')
return keyTracker.getOne()
}
getN (amount: number) {
// get the first kley tracker that is not exhausted
const keyTracker = this.keyTrackers.find(k => !k.exhausted)
if (keyTracker === undefined)
throw new Error('No keys left')
return keyTracker.getN(amount)
// TODO: handle the case where the selected key tracker does not have enough keys --> move on to the next key tracker
}
moreTypeA(amount: number = 2) {
const newKeyTracker : KeyTrackerA = new KeyTrackerA()
const rval = newKeyTracker.more(amount)
this.keyTrackers.push(newKeyTracker)
return rval
}
moreTypeB(amount: number = 2) {
const newKeyTracker : KeyTrackerB = new KeyTrackerB()
const rval = newKeyTracker.more(amount)
this.keyTrackers.push(newKeyTracker)
return rval
}
moreTypeC(amount: number = 2) {
const newKeyTracker : KeyTrackerC = new KeyTrackerC()
const rval = newKeyTracker.more(amount)
this.keyTrackers.push(newKeyTracker)
return rval
}
}