Skip to content

Commit

Permalink
feat: update to PubKey Protocol v1 sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
beeman committed Nov 13, 2024
1 parent 17ca1e5 commit 890681a
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 55 deletions.
Original file line number Diff line number Diff line change
@@ -1,26 +1,30 @@
import { AnchorProvider } from '@coral-xyz/anchor'
import { Injectable, Logger, OnModuleInit } from '@nestjs/common'
import {
PUBKEY_PROFILE_PROGRAM_ID,
PubKeyIdentityProvider,
AnchorKeypairWallet,
getKeypairFromByteArray,
IdentityProvider,
ProfileGetByProvider,
ProfileGetByUsername,
PUBKEY_PROTOCOL_PROGRAM_ID,
PubKeyCommunity,
PubKeyPointer,
PubKeyProfile,
} from '@pubkey-program-library/anchor'
import { AnchorKeypairWallet, GetProfileByUsername, PubKeyProfileSdk } from '@pubkey-program-library/sdk'
import { GetProfileByProvider } from '@pubkey-program-library/sdk/src/lib/pubkey-profile-sdk'
PubkeyProtocolSdk,
} from '@pubkey-protocol/sdk'
import { Connection, Keypair, LAMPORTS_PER_SOL } from '@solana/web3.js'
import { ApiCoreConfigService } from '../config/api-core-config.service'

function isValidProvider(provider: string): boolean {
return Object.values(PubKeyIdentityProvider).includes(provider as PubKeyIdentityProvider)
return Object.values(IdentityProvider).includes(provider as IdentityProvider)
}

@Injectable()
export class ApiCoreProtocolService implements OnModuleInit {
private readonly logger = new Logger(ApiCoreProtocolService.name)
private feePayer: Keypair | undefined
private connection: Connection | undefined
private sdk: PubKeyProfileSdk | undefined
private sdk: PubkeyProtocolSdk | undefined

constructor(private readonly config: ApiCoreConfigService) {}

Expand All @@ -34,16 +38,16 @@ export class ApiCoreProtocolService implements OnModuleInit {
return
}

this.feePayer = Keypair.fromSecretKey(Uint8Array.from(JSON.parse(this.config.pubkeyProtocolFeePayer)))
this.feePayer = getKeypairFromByteArray(JSON.parse(this.config.pubkeyProtocolFeePayer))
this.connection = new Connection(this.config.pubkeyProtocolEndpoint, 'confirmed')
this.logger.verbose(`PubKey Protocol: Endpoint: ${this.config.pubkeyProtocolEndpoint}`)
const balance = await this.connection.getBalance(this.feePayer.publicKey)
this.logger.verbose(
`PubKey Protocol: Fee payer: ${this.feePayer.publicKey}, balance: ${balance / LAMPORTS_PER_SOL}`,
)
this.sdk = new PubKeyProfileSdk({
this.sdk = new PubkeyProtocolSdk({
connection: this.connection,
programId: PUBKEY_PROFILE_PROGRAM_ID,
programId: PUBKEY_PROTOCOL_PROGRAM_ID,
provider: new AnchorProvider(this.connection, new AnchorKeypairWallet(this.feePayer), {
commitment: this.connection.commitment,
}),
Expand All @@ -59,26 +63,34 @@ export class ApiCoreProtocolService implements OnModuleInit {
return this.sdk
}

async getProfileByProvider(options: GetProfileByProvider): Promise<PubKeyProfile | null> {
async getCommunity(options: { community: string }): Promise<PubKeyCommunity> {
return this.ensureSdk().communityGet(options)
}

async getCommunities(): Promise<PubKeyCommunity[]> {
return this.ensureSdk().communityGetAll()
}

async getProfileByProvider(options: ProfileGetByProvider): Promise<PubKeyProfile | null> {
if (!isValidProvider(options.provider)) {
throw new Error(`Invalid provider: ${options.provider}`)
}
return this.ensureSdk().getProfileByProviderNullable(options)
return this.ensureSdk().profileGetByProviderNullable(options)
}

async getProfileByUsername(options: GetProfileByUsername): Promise<PubKeyProfile | null> {
return this.ensureSdk().getProfileByUsernameNullable(options)
async getProfileByUsername(options: ProfileGetByUsername): Promise<PubKeyProfile | null> {
return this.ensureSdk().profileGetByUsernameNullable(options)
}

async getProfiles(): Promise<PubKeyProfile[]> {
return this.ensureSdk().getProfiles()
return this.ensureSdk().profileGetAll()
}

getProviders() {
return Object.values(PubKeyIdentityProvider)
return Object.values(IdentityProvider)
}

async getPointers(): Promise<PubKeyPointer[]> {
return this.ensureSdk().getPointers()
return this.ensureSdk().pointerGetAll()
}
}
38 changes: 30 additions & 8 deletions libs/api/core/feature/src/lib/api-core-protocol.controller.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
import { Controller, Get, Param } from '@nestjs/common'
import { Controller, Get, NotFoundException, Param } from '@nestjs/common'
import { ApiCoreService } from '@pubkey-link/api-core-data-access'
import { PubKeyIdentityProvider } from '@pubkey-program-library/anchor'
import { IdentityProvider } from '@pubkey-protocol/sdk'

@Controller()
@Controller('ppl')
export class ApiCoreProtocolController {
constructor(private readonly service: ApiCoreService) {}

@Get('communities')
communities() {
return this.service.protocol.getCommunities()
}

@Get('community/:community')
async community(@Param('community') community: string) {
const found = await this.service.protocol.getCommunity({ community })
if (found) {
return found
}
throw new NotFoundException('Profile not found')
}

@Get('pointers')
pointers() {
return this.service.protocol.getPointers()
Expand All @@ -22,12 +36,20 @@ export class ApiCoreProtocolController {
}

@Get('provider/:provider/:providerId')
profileByProvider(@Param('provider') provider: PubKeyIdentityProvider, @Param('providerId') providerId: string) {
return this.service.protocol.getProfileByProvider({ providerId, provider })
async profileByProvider(@Param('provider') provider: IdentityProvider, @Param('providerId') providerId: string) {
const found = await this.service.protocol.getProfileByProvider({ providerId, provider })
if (found) {
return found
}
throw new NotFoundException('Profile not found')
}

@Get('username/:username')
profileByUsername(@Param('username') username: string) {
return this.service.protocol.getProfileByUsername({ username })
@Get('profile/:username')
async profileByUsername(@Param('username') username: string) {
const found = await this.service.protocol.getProfileByUsername({ username })
if (found) {
return found
}
throw new NotFoundException('Profile not found')
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Injectable, Logger } from '@nestjs/common'
import { OnEvent } from '@nestjs/event-emitter'
import { Network, NetworkCluster } from '@prisma/client'
import { ApiCoreService } from '@pubkey-link/api-core-data-access'
import { AnchorKeypairWallet } from '@pubkey-program-library/sdk'
import { AnchorKeypairWallet } from '@pubkey-protocol/sdk'
import { Connection, Keypair } from '@solana/web3.js'
import { ChainId, Client } from '@solflare-wallet/utl-sdk'
import {
Expand Down
4 changes: 2 additions & 2 deletions libs/api/user/data-access/src/lib/api-user-data.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Injectable } from '@nestjs/common'
import { Prisma } from '@prisma/client'
import { ApiCoreService, PagingInputFields } from '@pubkey-link/api-core-data-access'
import { PubKeyIdentityProvider } from '@pubkey-program-library/anchor'
import { IdentityProvider } from '@pubkey-protocol/sdk'
import { UserPaging } from './entity/user.entity'

@Injectable()
Expand Down Expand Up @@ -77,7 +77,7 @@ export class ApiUserDataService {
for (const identity of found.identities) {
console.log(`Checking ${identity.provider} ${identity.providerId}`)
foundPubkeyProfile = await this.core.protocol.getProfileByProvider({
provider: identity.provider as PubKeyIdentityProvider,
provider: identity.provider as IdentityProvider,
providerId: identity.providerId,
})
if (foundPubkeyProfile) {
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@
"@ogma/platform-express": "^5.0.1",
"@ogma/platform-graphql": "^5.0.1",
"@prisma/client": "5.18.0",
"@pubkey-program-library/anchor": "^1.7.1",
"@pubkey-program-library/sdk": "^1.7.1",
"@pubkey-protocol/sdk": "^1.0.0",
"@pubkey-ui/core": "^1.7.0",
"@pubkeyapp/wallet-adapter-mantine-ui": "^2.3.0",
"@solana-developers/helpers": "^2.3.0",
Expand Down
Loading

0 comments on commit 890681a

Please sign in to comment.