Skip to content

Commit e6855a9

Browse files
committed
refactor: store token in profile record
1 parent f0a3923 commit e6855a9

File tree

6 files changed

+28
-17
lines changed

6 files changed

+28
-17
lines changed

packages/mask/background/database/persona/helper.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -180,31 +180,32 @@ export async function createPersonaByJsonWebKey(options: {
180180

181181
export async function createProfileWithPersona(
182182
profileID: ProfileIdentifier,
183-
data: LinkedProfileDetails,
184-
keys: {
183+
linkMeta: LinkedProfileDetails,
184+
persona: {
185185
nickname?: string
186186
publicKey: EC_Public_JsonWebKey
187187
privateKey?: EC_Private_JsonWebKey
188188
localKey?: AESJsonWebKey
189189
mnemonic?: PersonaRecord['mnemonic']
190190
},
191+
token?: string | null,
191192
): Promise<void> {
192-
const ec_id = (await ECKeyIdentifier.fromJsonWebKey(keys.publicKey)).unwrap()
193+
const ec_id = (await ECKeyIdentifier.fromJsonWebKey(persona.publicKey)).unwrap()
193194
const rec: PersonaRecord = {
194195
createdAt: new Date(),
195196
updatedAt: new Date(),
196197
identifier: ec_id,
197198
linkedProfiles: new Map(),
198-
nickname: keys.nickname,
199-
publicKey: keys.publicKey,
200-
privateKey: keys.privateKey,
201-
localKey: keys.localKey,
202-
mnemonic: keys.mnemonic,
199+
nickname: persona.nickname,
200+
publicKey: persona.publicKey,
201+
privateKey: persona.privateKey,
202+
localKey: persona.localKey,
203+
mnemonic: persona.mnemonic,
203204
hasLogout: false,
204205
}
205206
await consistentPersonaDBWriteAccess(async (t) => {
206207
await createOrUpdatePersonaDB(rec, { explicitUndefinedField: 'ignore', linkedProfiles: 'merge' }, t)
207-
await attachProfileDB(profileID, ec_id, data, t)
208+
await attachProfileDB(profileID, ec_id, linkMeta, { token }, t)
208209
})
209210
}
210211
// #endregion

packages/mask/background/database/persona/type.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ export interface ProfileRecord {
112112
nickname?: string
113113
localKey?: AESJsonWebKey
114114
linkedPersona?: PersonaIdentifier
115+
token?: string
115116
createdAt: Date
116117
updatedAt: Date
117118
}

packages/mask/background/database/persona/web.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,7 @@ export async function attachProfileDB(
522522
identifier: ProfileIdentifier,
523523
attachTo: PersonaIdentifier,
524524
data: LinkedProfileDetails,
525+
profileExtra?: { token?: string | null },
525526
t?: FullPersonaDBTransaction<'readwrite'>,
526527
): Promise<void> {
527528
t = t || createTransaction(await db(), 'readwrite')('personas', 'profiles', 'relations')
@@ -536,6 +537,12 @@ export async function attachProfileDB(
536537
await detachProfileDB(identifier, t)
537538
}
538539

540+
if (profileExtra?.token) {
541+
profile.token = profileExtra.token
542+
} else if (profileExtra && 'token' in profileExtra && !profileExtra.token) {
543+
delete profile.token
544+
}
545+
539546
profile.linkedPersona = attachTo
540547
persona.linkedProfiles.set(identifier, data)
541548

packages/mask/background/services/identity/profile/update.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,22 +86,23 @@ export async function resolveUnknownLegacyIdentity(identifier: ProfileIdentifier
8686
export async function attachProfile(
8787
source: ProfileIdentifier,
8888
target: ProfileIdentifier | PersonaIdentifier,
89-
data: LinkedProfileDetails,
89+
linkMeta: LinkedProfileDetails,
90+
profileExtra?: { token?: string | null },
9091
): Promise<void> {
9192
if (target instanceof ProfileIdentifier) {
9293
const profile = await queryProfileDB(target)
9394
if (!profile?.linkedPersona) throw new Error('target not found')
9495
target = profile.linkedPersona
9596
}
96-
return attachProfileDB(source, target, data)
97+
return attachProfileDB(source, target, linkMeta, profileExtra)
9798
}
9899
export function detachProfile(identifier: ProfileIdentifier): Promise<void> {
99100
return detachProfileDB(identifier)
100101
}
101102

102103
/**
103104
* Set NextID profile to profileDB
104-
* */
105+
*/
105106

106107
export async function attachNextIDPersonaToProfile(item: ProfileInformationFromNextID, whoAmI: ECKeyIdentifier) {
107108
if (!item.linkedPersona) throw new Error('LinkedPersona Not Found')
@@ -137,6 +138,7 @@ export async function attachNextIDPersonaToProfile(item: ProfileInformationFromN
137138
profileRecord.identifier,
138139
item.linkedPersona!,
139140
{ connectionConfirmState: 'confirmed' },
141+
undefined,
140142
t,
141143
)
142144
await createOrUpdateRelationDB(

packages/mask/popups/pages/Personas/ConnectFirefly/index.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { Trans, useLingui } from '@lingui/react/macro'
2-
import { attachProfile } from '@masknet/plugin-infra/dom/context'
32
import { PersonaContext, PopupHomeTabType } from '@masknet/shared'
43
import {
54
AbortError,
@@ -22,6 +21,7 @@ import { useMount } from 'react-use'
2221
import urlcat from 'urlcat'
2322
import { useTitle } from '../../../hooks/index.js'
2423
import { createAccountByRelayService } from './createAccountByRelayService.js'
24+
import Services from '#services'
2525

2626
const useStyles = makeStyles()({
2727
container: {
@@ -98,11 +98,12 @@ export const Component = memo(function ConnectFireflyPage() {
9898
setUrl(url)
9999
})
100100
console.log('account', account)
101-
if (attachProfile && currentPersona) {
102-
await attachProfile(
101+
if (currentPersona) {
102+
await Services.Identity.attachProfile(
103103
ProfileIdentifier.of(EnhanceableSite.Farcaster, account.session.profileId).unwrap(),
104104
currentPersona.identifier,
105-
{ connectionConfirmState: 'pending', token: account.session.token },
105+
{ connectionConfirmState: 'pending' },
106+
{ token: account.session.token },
106107
)
107108
}
108109
console.log('account', account)

packages/public-api/src/types/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ export interface JsonRpcResponse {
1414

1515
export interface LinkedProfileDetails {
1616
connectionConfirmState: 'confirmed' | 'pending'
17-
token?: string
1817
}
1918

2019
// These type MUST be sync with packages/shared-base/src/crypto/JWKType

0 commit comments

Comments
 (0)