Skip to content

Commit

Permalink
fix ownerData selector (#2127)
Browse files Browse the repository at this point in the history
* fix ownerData selector

* initialize empty arr for getOldestParsedCertificate
  • Loading branch information
vinkabuki authored Dec 1, 2023
1 parent 5ddbc6a commit f78fb94
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { getOldestParsedCerificate } from '../users/users.selectors'
// Workaround for "The inferred type of 'communitiesSelectors' cannot be named without a reference to
// 'packages/identity/node_modules/pkijs/build'. This is likely not portable. A type annotation is necessary."
// https://github.com/microsoft/TypeScript/issues/47663#issuecomment-1270716220
import type {} from 'pkijs'
import type { } from 'pkijs'

const communitiesSlice: CreatedSelectors[StoreKeys.Communities] = (state: StoreState) => state[StoreKeys.Communities]

Expand Down Expand Up @@ -83,7 +83,7 @@ export const ownerNickname = createSelector(
const parsedCert = parseCertificate(certificate)
nickname = getCertFieldValue(parsedCert, CertFieldsTypes.nickName)
} else {
nickname = getCertFieldValue(oldestParsedCerificate, CertFieldsTypes.nickName)
nickname = getCertFieldValue(oldestParsedCerificate.certificate, CertFieldsTypes.nickName)
}

if (!nickname) {
Expand Down
33 changes: 19 additions & 14 deletions packages/state-manager/src/sagas/users/users.selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,26 +124,31 @@ export const getUserByPubKey = (pubKey: string) => createSelector(allUsers, user

export const getOldestParsedCerificate = createSelector(certificates, certs => {
const getTimestamp = (cert: Certificate) => new Date(cert.notBefore.value).getTime()
let certificates: Certificate[] = []
Object.keys(certs).map(pubKey => {
certificates = [...certificates, certs[pubKey]]
})
certificates.sort((a, b) => {
const aTimestamp = getTimestamp(a)
const bTimestamp = getTimestamp(b)
return aTimestamp - bTimestamp
})

let certificates: { pubkey: string, certificate: Certificate }[] = []

certificates =
Array.from(Object.entries(certs))
.sort((a, b) => {
const aTimestamp = getTimestamp(a[1])
const bTimestamp = getTimestamp(b[1])
return aTimestamp - bTimestamp
}).map(cert => {
return {
pubkey: cert[0],
certificate: cert[1]
}
})
return certificates[0]
})

export const ownerData = createSelector(getOldestParsedCerificate, ownerCert => {
if (!ownerCert) return null
const username = getCertFieldValue(ownerCert, CertFieldsTypes.nickName)
const onionAddress = getCertFieldValue(ownerCert, CertFieldsTypes.commonName)
const peerId = getCertFieldValue(ownerCert, CertFieldsTypes.peerId)
const dmPublicKey = getCertFieldValue(ownerCert, CertFieldsTypes.dmPublicKey)
const pubKey = keyFromCertificate(ownerCert)
const username = getCertFieldValue(ownerCert.certificate, CertFieldsTypes.nickName)
const onionAddress = getCertFieldValue(ownerCert.certificate, CertFieldsTypes.commonName)
const peerId = getCertFieldValue(ownerCert.certificate, CertFieldsTypes.peerId)
const dmPublicKey = getCertFieldValue(ownerCert.certificate, CertFieldsTypes.dmPublicKey)
const pubKey = ownerCert.pubkey

return {
username,
Expand Down

0 comments on commit f78fb94

Please sign in to comment.