-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Co-authored-by: Nikki Kang <kangaroo233@gmail.com>
- Loading branch information
Showing
5 changed files
with
137 additions
and
2 deletions.
There are no files selected for viewing
85 changes: 85 additions & 0 deletions
85
packages/libs/src/sdk/api/dashboard-wallet-users/DashboardWalletUsersApi.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |