import { Address, encodeAbiParameters, keccak256 } from 'viem'; import { PrexClient } from '../prex-client'; import { ProfileRegistryAbi } from '../abis'; import { DEFAULT_NAME_GROUP, PROFILE_REGISTRY } from '../constants'; export class NicknameAction { constructor(private client: PrexClient) {} async updateNickName({ nickName, from, }: { nickName: string; from?: Address; }) { await this.client.executeOperation( { address: PROFILE_REGISTRY, abi: ProfileRegistryAbi, functionName: 'setProfileName', args: [DEFAULT_NAME_GROUP, nickName], }, from ); } async updateNickNameWithSharedWallet({ nickName, owners, nonce, sharedWalletAddress, }: { nickName: string; owners: Address[]; nonce: number; sharedWalletAddress: Address; }) { await this.client.executeWithCreateSharedWallet( { address: PROFILE_REGISTRY, abi: ProfileRegistryAbi, functionName: 'setProfileName', args: [DEFAULT_NAME_GROUP, nickName], }, owners, nonce, sharedWalletAddress ); } async saveProfilePicture({ 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'); } const timestamp = Math.floor(Date.now() / 1000) + 60 * 60; const hash = createGrantHash(ethAddress, timestamp, 'avatar'); const signature = await this.client.signer.signReplaySafeHash( hash, ethAddress ); return await this.client.apiService.uploadAvatar({ file: image, eth_address: ethAddress, signature, timestamp, }); } } function createGrantHash( ethAddress: Address, timestamp: number, permission: 'avatar' ) { const hash = keccak256( encodeAbiParameters( [ { type: 'string' }, { type: 'uint256' }, { type: 'address' }, { type: 'bytes32' }, ], [ 'grant', BigInt(timestamp), ethAddress, keccak256(encodeAbiParameters([{ type: 'string' }], [permission])), ] ) ); return hash; }