import { ConnectedIdentityMetadata, ISemaphoreFullProof, ISemaphoreProofRequiredArgs, IRLNProofRequiredArgs, IRLNFullProof, IJoinGroupMemberArgs, IGenerateGroupMerkleProofArgs, IVerifiablePresentationRequest, } from "@cryptkeeperzk/types"; import { RPCExternalAction } from "../constants"; import { Handler } from "../services"; import { ICryptKeeperInjectedProvider } from "./interface"; /** * Represents the CryptKeeper provider that is injected into the application. * This class is responsible for handling interactions with the CryptKeeper extension. * * @class */ export class CryptKeeperInjectedProvider extends Handler implements ICryptKeeperInjectedProvider { /** * Indicates whether the provider is CryptKeeper. */ readonly isCryptKeeper = true; /** * Creates an instance of CryptKeeperInjectedProvider. * * @constructor */ // eslint-disable-next-line @typescript-eslint/no-useless-constructor constructor(connectedOrigin?: string) { super(connectedOrigin); } async getConnectedIdentity(): Promise { return this.post({ method: RPCExternalAction.GET_CONNECTED_IDENTITY_DATA, }) as Promise; } async connect(isChangeIdentity = false): Promise { await this.post({ method: RPCExternalAction.CONNECT, payload: { isChangeIdentity, urlOrigin: this.connectedOrigin, }, }); } async generateSemaphoreProof({ externalNullifier, signal, merkleProofSource, }: ISemaphoreProofRequiredArgs): Promise { return this.post({ method: RPCExternalAction.GENERATE_SEMAPHORE_PROOF, payload: { externalNullifier, signal, merkleProofSource, }, }) as Promise; } async generateRlnProof({ rlnIdentifier, message, messageLimit, messageId, epoch, merkleProofSource, }: IRLNProofRequiredArgs): Promise { return this.post({ method: RPCExternalAction.GENERATE_RLN_PROOF, payload: { rlnIdentifier, message, messageId, messageLimit, epoch, merkleProofSource, }, }) as Promise; } async joinGroup({ groupId, apiKey, inviteCode }: IJoinGroupMemberArgs): Promise { await this.post({ method: RPCExternalAction.JOIN_GROUP_REQUEST, payload: { groupId, apiKey, inviteCode, }, }); } async generateGroupMerkleProof({ groupId }: IGenerateGroupMerkleProofArgs): Promise { await this.post({ method: RPCExternalAction.GENERATE_GROUP_MERKLE_PROOF_REQUEST, payload: { groupId, }, }); } /** * Requests user to reveal a connected identity commitment. * * @returns {Promise} */ async revealConnectedIdentityRequest(): Promise { await this.post({ method: RPCExternalAction.REVEAL_CONNECTED_IDENTITY_COMMITMENT_REQUEST, }); } /** * Requests user to provide a verifiable presentation. * NOTE: THIS FUNCTION IS UNDER DEVELOPMENT AND NOT READY FOR PRODUCTION USE * * @param {IVerifiablePresentationRequest} verifiablePresentationRequest - The information provided to the user when requesting a verifiable presentation. * @returns {void} */ async DEV_generateVerifiablePresentationRequest( verifiablePresentationRequest: IVerifiablePresentationRequest, ): Promise { await this.post({ method: RPCExternalAction.GENERATE_VERIFIABLE_PRESENTATION_REQUEST, payload: verifiablePresentationRequest, }); } /** * Requests user to reveal a connected identity commitment. * NOTE: THIS FUNCTION IS UNDER DEVELOPMENT AND NOT READY FOR PRODUCTION USE * * @param {string} serializedVerifiableCredential - The json string representation of the verifiable credential to add. * @returns {void} */ async DEV_addVerifiableCredentialRequest(serializedVerifiableCredential: string): Promise { await this.post({ method: RPCExternalAction.ADD_VERIFIABLE_CREDENTIAL_REQUEST, payload: serializedVerifiableCredential, }); } }