Reputation: 21
In the chain code for Hyperledger Fabric, is it possible to check if an identity exists with a given name? that is, something like this:
func (c *SmartContract) UserExists(ctx contractapi.TransactionContextInterface, username string) bool {
// if an identity exists with the username, then return true, otherwise return false.
}
Upvotes: 1
Views: 187
Reputation: 938
You can do it with the IdentityService
provided contractapi.TransactionContextInterface
. However, in fact identities in Hyperledger Fabric are typically managed by the MSP and not directly accessible within the chaincode. If you use identity-based access control within your Fabric network, then you can try the following approach.
//assuming that you've imported necessary packages
func (c *SmartContract) UserExists(ctx contractapi.TransactionContextInterface,
username string) (bool, error) {
// Get the identity service from the transaction context
identityService, err :=
ctx.GetClientIdentity().GetSigningIdentity().CreateAccessControl()
if err != nil {
return false, err
}
// Check if the identity exists with the given username
exists, err := identityService.GetState(username)
if err != nil {
return false, err
}
return exists != nil, nil
}
Upvotes: 0