import { Address, Hex, keccak256 } from 'viem'; import { PrexClient } from '../prex-client'; import { getProfileV2 } from '../evm-api/get-profile'; import { ProfileActionInterface } from '../interfaces/prex-client-interface'; import { PROFILE_REGISTRY_V2, ProfileRegistryV2Abi } from '@prex0/prex-structs'; export class ProfileActionV2 implements ProfileActionInterface { constructor(private client: PrexClient) {} async updateProfile({ domain = 0, name, avatar, metadata, from, }: { domain?: number; name: string; avatar: File; metadata: Hex; from?: Address; }) { const arrayBuffer = await avatar.arrayBuffer(); const pictureHash = keccak256(new Uint8Array(arrayBuffer)); await this.client.executeOperation( { address: PROFILE_REGISTRY_V2, abi: ProfileRegistryV2Abi, functionName: 'updateProfile', args: [BigInt(domain), name, pictureHash, metadata], }, from ); } async updateName({ domain = 0, name, from, }: { domain?: number; name: string; from?: Address; }) { await this.client.executeOperation( { address: PROFILE_REGISTRY_V2, abi: ProfileRegistryV2Abi, functionName: 'updateName', args: [BigInt(domain), name], }, from ); } async updateAvatar({ avatar, from }: { avatar: File; from?: Address }) { const arrayBuffer = await avatar.arrayBuffer(); const pictureHash = keccak256(new Uint8Array(arrayBuffer)); await this.client.executeOperation( { address: PROFILE_REGISTRY_V2, abi: ProfileRegistryV2Abi, functionName: 'updateAvatar', args: [pictureHash], }, from ); } async getProfile(address: Address) { return getProfileV2(this.client.evmChainClient, address); } async updateNickNameWithSharedWallet({ nickName, owners, nonce, sharedWalletAddress, }: { nickName: string; owners: Address[]; nonce: number; sharedWalletAddress: Address; }) { await this.client.executeWithCreateSharedWallet( { address: PROFILE_REGISTRY_V2, abi: ProfileRegistryV2Abi, functionName: 'updateName', args: [0n, nickName], }, owners, nonce, sharedWalletAddress ); } async uploadAvatar({ image, from }: { image: File; from?: Address }) { const ethAddress = from || this.client.user?.address; if (!ethAddress) { throw new Error('No address provided'); } if (!this.client.signer) { throw new Error('No signer found'); } return await this.client.apiService.uploadAvatarV2({ file: image, eth_address: ethAddress, }); } async copyAvatar({ pictureUrl, from, }: { pictureUrl: string; from?: Address; }) { const ethAddress = from || this.client.user?.address; if (!ethAddress) { throw new Error('No address provided'); } return await this.client.apiService.copyAvatarV2({ picture_url: pictureUrl, eth_address: ethAddress, }); } }