Skip to content

Commit

Permalink
working invite acceptance and initial sync
Browse files Browse the repository at this point in the history
  • Loading branch information
adrastaea committed Jan 10, 2025
1 parent 5a32f96 commit 20b0a8e
Show file tree
Hide file tree
Showing 12 changed files with 428 additions and 388 deletions.
38 changes: 38 additions & 0 deletions packages/backend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@
"blockstore-fs": "^2.0.2",
"blockstore-level": "^2.0.1",
"bs58": "^6.0.0",
"bufferutil": "^4.0.9",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.1",
"cli-table": "^0.3.6",
Expand Down
15 changes: 8 additions & 7 deletions packages/backend/src/nest/auth/sigchain.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,14 @@ export class SigChainService implements OnModuleInit {
* @param setActive Whether to set the chain as active
* @returns Whether the chain was set as active
*/
addChain(chain: SigChain, setActive: boolean): boolean {
if (this.chains.has(chain.team!.teamName)) {
throw new Error(`Chain for team ${chain.team!.teamName} already exists`)
addChain(chain: SigChain, setActive: boolean, teamName?: string): boolean {
teamName = teamName || chain.team!.teamName
if (this.chains.has(teamName)) {
throw new Error(`Chain for team ${teamName} already exists`)
}
this.chains.set(chain.team!.teamName, chain)
this.chains.set(teamName, chain)
if (setActive) {
this.setActiveChain(chain.team!.teamName)
this.setActiveChain(teamName)
return true
}
return false
Expand Down Expand Up @@ -103,9 +104,9 @@ export class SigChainService implements OnModuleInit {
return sigChain
}

async createChainFromInvite(username: string, seed: string, setActive: boolean): Promise<SigChain> {
async createChainFromInvite(username: string, teamName: string, seed: string, setActive: boolean): Promise<SigChain> {
const sigChain = SigChain.createFromInvite(username, seed)
this.addChain(sigChain, setActive)
this.addChain(sigChain, setActive, teamName)
return sigChain
}

Expand Down
9 changes: 7 additions & 2 deletions packages/backend/src/nest/auth/sigchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ class SigChain {
private _invites: InviteService | null = null
private _crypto: CryptoService | null = null

private constructor(context: auth.LocalUserContext, team?: auth.Team) {
this.localUserContext = context
private constructor(localUserContext: auth.LocalUserContext, team?: auth.Team) {
this.localUserContext = localUserContext
if (team) this.team = team
}

Expand All @@ -41,6 +41,11 @@ class SigChain {
const context = UserService.create(username)
const team: auth.Team = this.lfa.createTeam(teamName, context)
const sigChain = this.init(context, team)
sigChain.context = {
user: context.user,
device: context.device,
team: team,
} as auth.MemberContext

// sigChain.roles.createWithMembers(RoleName.ADMIN, [context.user.userId])
sigChain.roles.createWithMembers(RoleName.MEMBER, [context.user.userId])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,14 @@ export class ConnectionsManagerService extends EventEmitter implements OnModuleI
id: payload.id,
}
await this.storageService.setIdentity(identity)

if (!community.name) {
this.logger.error('Community name is required to create sigchain')
return community
}
this.logger.info(`Creating new LFA chain`)
await this.sigChainService.createChain(community.name, identity.nickname, true)

await this.launchCommunity(community)

const meta = await this.storageService.updateCommunityMetadata({
Expand All @@ -610,14 +618,6 @@ export class ConnectionsManagerService extends EventEmitter implements OnModuleI
await this.storageService.saveCSR({ csr: identity.userCsr.userCsr })
}

// create sigchain
if (!community.name) {
this.logger.error('Community name is required to create sigchain')
return community
}

this.logger.info(`Creating new LFA chain`)
await this.sigChainService.createChain(community.name, identity.nickname, true)
// this is the forever invite that all users get
this.logger.info(`Creating long lived LFA invite code`)
this.socketService.emit(SocketActionTypes.CREATE_LONG_LIVED_LFA_INVITE)
Expand Down Expand Up @@ -663,7 +663,12 @@ export class ConnectionsManagerService extends EventEmitter implements OnModuleI

const inviteData = payload.inviteData
if (inviteData && inviteData?.version == InvitationDataVersion.v2) {
this.sigChainService.createChainFromInvite(identity.nickname, inviteData.authData.seed, true)
this.sigChainService.createChainFromInvite(
identity.nickname,
inviteData.authData.communityName,
inviteData.authData.seed,
true
)
}

if (!metadata.peers || metadata.peers.length === 0) {
Expand Down Expand Up @@ -954,18 +959,18 @@ export class ConnectionsManagerService extends EventEmitter implements OnModuleI
SocketActionTypes.VALIDATE_OR_CREATE_LONG_LIVED_LFA_INVITE,
async (
inviteId: Base58,
callback: (response: { isValid: boolean; newInvite?: InviteResult } | undefined) => void
callback: (response: { valid: boolean; newInvite?: InviteResult } | undefined) => void
) => {
this.logger.info(`socketService - ${SocketActionTypes.VALIDATE_OR_CREATE_LONG_LIVED_LFA_INVITE}`)
if (this.sigChainService.activeChainTeamName != null) {
if (this.sigChainService.getActiveChain().invites.isValidLongLivedUserInvite(inviteId)) {
this.logger.info(`Invite is a valid long lived LFA invite code!`)
callback({ isValid: true })
callback({ valid: true })
} else {
this.logger.info(`Invite is an invalid long lived LFA invite code! Generating a new code!`)
const newInvite = this.sigChainService.getActiveChain().invites.createLongLivedUserInvite()
this.serverIoProvider.io.emit(SocketActionTypes.CREATED_LONG_LIVED_LFA_INVITE, newInvite)
callback({ isValid: false, newInvite })
callback({ valid: false, newInvite })
}
} else {
this.logger.warn(`No sigchain configured, skipping long lived LFA invite code validation/generation!`)
Expand Down
Loading

0 comments on commit 20b0a8e

Please sign in to comment.