(
value: Uint8Array,
params?: { raw?: boolean; locked?: boolean },
)
| 18 | } |
| 19 | |
| 20 | export function createKeyring( |
| 21 | value: Uint8Array, |
| 22 | params?: { raw?: boolean; locked?: boolean }, |
| 23 | ) { |
| 24 | const raw = params?.raw ?? value.length <= 32; |
| 25 | |
| 26 | const _wrappedData = new WrappedData(value, { |
| 27 | raw, |
| 28 | }); |
| 29 | |
| 30 | return new (class Keyring implements Wrappable, IKeyring { |
| 31 | keys: Uint8Array[] = []; |
| 32 | |
| 33 | constructor() { |
| 34 | if (_wrappedData.topLayer !== DataLayer.Raw) { |
| 35 | return; |
| 36 | } |
| 37 | |
| 38 | this.keys = decodeKeys(_wrappedData.content); |
| 39 | |
| 40 | if (_wrappedData.metadata.keys == null) { |
| 41 | _wrappedData.metadata.keys = new Array(this.keys.length).fill({ |
| 42 | rotationDate: new Date(), |
| 43 | }); |
| 44 | } |
| 45 | |
| 46 | this.trimExpiredKeys(); |
| 47 | } |
| 48 | |
| 49 | trimExpiredKeys() { |
| 50 | if (_wrappedData.topLayer !== DataLayer.Raw) { |
| 51 | throw new Error('Cannot trim keys on non-raw keyring.'); |
| 52 | } |
| 53 | |
| 54 | const currentDate = new Date(); |
| 55 | |
| 56 | const unexpiredIndexes = this.keys |
| 57 | .map((_, index) => index) |
| 58 | .filter((index) => { |
| 59 | return ( |
| 60 | index === 0 || |
| 61 | addDays(_wrappedData.metadata.keys[index].rotationDate, 1) > |
| 62 | currentDate |
| 63 | ); |
| 64 | }); |
| 65 | |
| 66 | this.keys = this.keys.filter((_, index) => |
| 67 | unexpiredIndexes.includes(index), |
| 68 | ); |
| 69 | _wrappedData.content = encodeKeys(this.keys); |
| 70 | |
| 71 | _wrappedData.metadata.keys = _wrappedData.metadata.keys.filter( |
| 72 | (_, index) => unexpiredIndexes.includes(index), |
| 73 | ); |
| 74 | } |
| 75 | |
| 76 | get layers() { |
| 77 | return _wrappedData.layers; |
no outgoing calls
no test coverage detected