Is there any way to remove particular user indentity from Hyperledger Fabric CA by HLF Java SDK?

24 views Asked by At

I am wondering if it is possible to remove user identity from Hyperledger Fabric CA in the local network.

This is my repo: Link

If you have any clue or advise please let me know. I realise project of an Spring boot app that communicates with Hyperledger Fabric local network with 2 simple orgs.

Currently, I have only registerUser method that allows creating Identities for new users. I need to provide removing users process. That's my method registerUser:

    public CustomIdentity registerUser(String username, String hospName, CustomIdentity adminId) throws Exception {
        HospInfo hospInfo = networkProps.getHospInfoByName().get(hospName);
        // Create a CA client for interacting with the CA.
        Properties props = new Properties();
        props.put("pemFile", hospInfo.getCertPath());
        props.put("allowAllHostNames", "true");

        HFCAClient caClient = HFCAClient.createNewInstance(hospInfo.getCaUrl(), props);
        CryptoSuite cryptoSuite = CryptoSuiteFactory.getDefault().getCryptoSuite();
        caClient.setCryptoSuite(cryptoSuite);

        // Create a wallet for managing identities
        Wallet wallet = Wallets.newFileSystemWallet(Paths.get("wallet"));

        // Check to see if we've already enrolled the user.
        if (wallet.get(username) != null) {
            throw new RuntimeException("An identity for the user: '" + username + "' already exists in the wallet");
        }

        X509Identity adminIdentity = (X509Identity) wallet.get(hospInfo.getUsername());

        if (!isIdentitySameAsCustomIdentity(adminIdentity, adminId)) {
//                System.out.println(hospInfo.getUsername() + " needs to be the same as value passed in request body");
            throw new RuntimeException(hospInfo.getUsername() + " identity in wallet needs to be the same as value passed in request body");
        }

        // Register the user, enroll the user, and import the new identity into the wallet.
        RegistrationRequest registrationRequest = new RegistrationRequest(username);
        //registrationRequest.setAffiliation("org1.department1");
        registrationRequest.setEnrollmentID(username);
//            String enrollmentSecret = caClient.register(registrationRequest, buildAdminUser(hospName, adminIdentity) );
//            X509Enrollment enrollment = (X509Enrollment) caClient.enroll(username, enrollmentSecret);
        X509Identity user = Identities.newX509Identity(hospInfo.getMspName(), adminIdentity.getCertificate(), adminIdentity.getPrivateKey());
        wallet.put(username, user);
        log.info("Successfully enrolled user" + username +" and imported it into the wallet");
        return CustomIdentity.builder().mspId(user.getMspId()).version(1).type("X.509").credentials(
                Credentials.builder()
                        .certificate(Identities.toPemString(user.getCertificate()))
                        .privateKey(Identities.toPemString(user.getPrivateKey())).build())
                .build();
    }

    /**
     * Compare identity and customIdentity in order to check wether data given by user are correct and related to already stored Identity.
     * @param identity
     * @param customIdentity
     * @return
     */
    private boolean isIdentitySameAsCustomIdentity(X509Identity identity, CustomIdentity customIdentity) {
        return Identities.toPemString(identity.getCertificate()).equals(customIdentity.getCredentials().getCertificate())
                && Identities.toPemString(identity.getPrivateKey()).equals(customIdentity.getCredentials().getPrivateKey());
    }
0

There are 0 answers