/
ClaimsSaga.ts
150 lines (130 loc) · 4.34 KB
/
ClaimsSaga.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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import { delay, SagaIterator } from 'redux-saga'
import { call, put, select, spawn, take, takeEvery } from 'redux-saga/effects'
import Actions from '../Reducers/Actions'
import * as Selectors from '../Selectors'
import * as Enums from '../Enums'
import { map, forEach, isArray } from 'lodash'
import Config from '../Config'
import * as Services from '../Services'
import { decodeJWT } from 'did-jwt'
export function* signAnonymousClaim(action: any): SagaIterator {
yield put(Actions.Process.start({type: Enums.ProcessType.SignClaim}))
try {
const jwt = yield call(Services.uPort.signAnonymousClaim, action.payload)
const decodedClaim = yield call(decodeJWT, jwt)
const signedClaim: VerifiableClaim = {
...decodedClaim.payload,
jwt,
source: {
type: 'local',
},
}
yield put(Actions.Contacts.addClaim(signedClaim))
} catch (e) {
yield put(Actions.App.handleError(e))
}
yield put(Actions.Process.end({type: Enums.ProcessType.SignClaim}))
}
export function* watchSignAnonymousClaim(): SagaIterator {
yield takeEvery(Actions.Contacts.signAnonymousClaim, signAnonymousClaim)
}
export function* watchSignAnonymousClaimAndShareInRoom(): SagaIterator {
while (true) {
const action = yield take(Actions.Contacts.signAnonymousClaimAndShareInRoom)
yield put(Actions.Process.start({type: Enums.ProcessType.SignClaim}))
try {
const jwt = yield call(Services.uPort.signAnonymousClaim, action.payload.unsigned)
const decodedClaim = yield call(decodeJWT, jwt)
const signedClaim: VerifiableClaim = {
...decodedClaim.payload,
jwt,
source: {
type: 'local',
},
}
yield put(Actions.Contacts.addClaim(signedClaim))
const claims: any[] = []
claims.push(jwt)
const file = {
fileName: 'claims.json',
fileContent: JSON.stringify({claims}),
}
yield put(Actions.Matrix.sendFile(file))
} catch (e) {
yield put(Actions.App.handleError(e))
}
yield put(Actions.Process.end({type: Enums.ProcessType.SignClaim}))
}
}
export function* loadAndAppendMatrixClaims(action: any): SagaIterator {
yield put(Actions.Process.start({type: Enums.ProcessType.LoadMatrixClaims}))
try {
const roomId = action.payload.roomId
const url = action.payload.url
console.log({roomId, url})
const claims = yield call(loadClaim, url, roomId)
yield put(Actions.Contacts.appendMatrixClaims(claims))
} catch (e) {
yield put(Actions.App.handleError(e))
}
yield put(Actions.Process.end({type: Enums.ProcessType.LoadMatrixClaims}))
}
export function* watchLoadAndAppendMatrixClaims(): SagaIterator {
yield takeEvery(Actions.Contacts.loadAndAppendMatrixClaims, loadAndAppendMatrixClaims)
}
export function* watchLoadMatrixClaims(): SagaIterator {
while (true) {
const action = yield take(Actions.Contacts.loadMatrixClaims)
yield put(Actions.Process.start({type: Enums.ProcessType.LoadMatrixClaims}))
try {
const claims = yield call(loadClaims, action.payload)
yield put(Actions.Contacts.setMatrixClaims(claims))
} catch (e) {
yield put(Actions.App.handleError(e))
}
yield put(Actions.Process.end({type: Enums.ProcessType.LoadMatrixClaims}))
}
}
const loadClaims = (roomsAndUrls: any) => {
const promises: any = []
forEach(roomsAndUrls, (room: any) => {
forEach(room.urls, (url: string) => {
promises.push(loadClaim(url, room.roomId))
})
})
return Promise.all(promises).then((arr: any) => {
let result: VerifiableClaim[] = []
forEach(arr, (item: any) => {
result = result.concat(item)
})
return result
})
}
const loadClaim = (url: string, roomId: string) => {
return fetch(url)
.then((response: any) => response.json())
.then((json: any) => {
const result: VerifiableClaim[] = []
if (json.claims && isArray(json.claims)) {
forEach(json.claims, (jwt: string) => {
try {
const decodedClaim = decodeJWT(jwt)
if (decodedClaim) {
const signedClaim: VerifiableClaim = {
...decodedClaim.payload,
jwt,
source: {
type: 'matrix',
id: roomId,
},
}
result.push(signedClaim)
}
} catch (e) {
// invalid jwt
}
})
}
return result
})
}