Skip to content

Commit

Permalink
[SDK] Add dashboard wallet users API to SDK [C-3530] (#6971)
Browse files Browse the repository at this point in the history
Co-authored-by: Nikki Kang <kangaroo233@gmail.com>
  • Loading branch information
nicoback2 and nicoback authored Dec 22, 2023
1 parent 3c92e3d commit 179ba8a
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import type { AuthService, EntityManagerService } from '../../services'
import {
Action,
AdvancedOptions,
EntityType
} from '../../services/EntityManager/types'
import { parseParams } from '../../utils/parseParams'

import {
CreateDashboardWalletUser,
CreateDashboardWalletUserRequest,
DeleteDashboardWalletUserRequest,
DeleteDashboardWalletUserSchema
} from './types'

// TODO(nkang): Extend generated dashboard wallet users API
export class DashboardWalletUsersApi {
constructor(
// config: Configuration,
private readonly entityManager: EntityManagerService,
private readonly auth: AuthService
) {
// super(config)
}

/**
* Connect an Audius user to a wallet on the protocol dashboard
*/
async connectUserToDashboardWallet(
params: CreateDashboardWalletUserRequest,
advancedOptions?: AdvancedOptions
) {
const {
wallet,
userId,
walletSignature: { message, signature }
} = await parseParams(
'createDashboardWalletUser',
CreateDashboardWalletUser
)(params)

const response = await this.entityManager.manageEntity({
userId,
entityType: EntityType.DASHBOARD_WALLET_USER,
entityId: 0, // Unused
action: Action.CREATE,
metadata: JSON.stringify({
wallet,
wallet_signature: {
message,
signature
}
}),
auth: this.auth,
...advancedOptions
})

return {
...response
}
}

/**
* Disconnect an Audius user from a wallet on the protocol dashboard
*/
async disconnectUserFromDashboardWallet(
params: DeleteDashboardWalletUserRequest
) {
const { userId, wallet } = await parseParams(
'deleteDashboardWalletUser',
DeleteDashboardWalletUserSchema
)(params)

return await this.entityManager.manageEntity({
userId,
entityType: EntityType.DASHBOARD_WALLET_USER,
entityId: 0, // Unused
action: Action.DELETE,
metadata: JSON.stringify({
wallet
}),
auth: this.auth
})
}
}
31 changes: 31 additions & 0 deletions packages/libs/src/sdk/api/dashboard-wallet-users/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { z } from 'zod'

import { HashId } from '../../types/HashId'
import { isEthAddressValid } from '../../utils/ethAddress'

export const CreateDashboardWalletUser = z.object({
wallet: z.custom<string>((data: unknown) => {
return isEthAddressValid(data as string)
}),
userId: HashId,
walletSignature: z.object({
/** Message should be of the form: "Connecting Audius user id a93jl at 39823489" */
message: z.string(),
signature: z.string()
})
})

export type CreateDashboardWalletUserRequest = z.input<
typeof CreateDashboardWalletUser
>

export const DeleteDashboardWalletUserSchema = z.object({
userId: HashId,
wallet: z.custom<string>((data: unknown) => {
return isEthAddressValid(data as string)
})
})

export type DeleteDashboardWalletUserRequest = z.input<
typeof DeleteDashboardWalletUserSchema
>
9 changes: 8 additions & 1 deletion packages/libs/src/sdk/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { defaultEntityManagerConfig } from './services/EntityManager/constants'
import { Logger } from './services/Logger'
import { StorageNodeSelector } from './services/StorageNodeSelector'
import { SdkConfig, SdkConfigSchema, ServicesContainer } from './types'
import { DashboardWalletUsersApi } from './api/dashboard-wallet-users/DashboardWalletUsersApi'

/**
* The Audius SDK
Expand Down Expand Up @@ -181,6 +182,11 @@ const initializeApis = ({
services.auth
)

const dashboardWalletUsers = new DashboardWalletUsersApi(
services.entityManager,
services.auth
)

const generatedApiClientConfigFull = new ConfigurationFull({
fetchApi: fetch,
middleware
Expand All @@ -206,7 +212,8 @@ const initializeApis = ({
full,
chats,
grants,
developerApps
developerApps,
dashboardWalletUsers
}
}

Expand Down
3 changes: 2 additions & 1 deletion packages/libs/src/sdk/services/EntityManager/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ export enum EntityType {
USER_REPLICA_SET = 'UserReplicaSet',
NOTIFICATION = 'Notification',
DEVELOPER_APP = 'DeveloperApp',
GRANT = 'Grant'
GRANT = 'Grant',
DASHBOARD_WALLET_USER = 'DashboardWalletUser'
}

export type AdvancedOptions = {
Expand Down
11 changes: 11 additions & 0 deletions packages/libs/src/sdk/utils/ethAddress.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export const isEthAddressValid = (address: string) => {
try {
if (address.length !== 42 || !address.startsWith('0x')) {
return false
}
const hexadecimalRegex = /^[0-9a-fA-F]+$/
return hexadecimalRegex.test(address)
} catch (_e) {
return false
}
}

0 comments on commit 179ba8a

Please sign in to comment.