-2

Other than the 3 attributes hf.EnrollmentId, hf.type and hf.Affiliation, I've created a custom attribute named email and added it as attrs:[{name: 'email', value: rahul18@gmail.com, ecert: true}] and it was successfully added to the attribute list.

In my chaincode, i'm able to get the enrollmentId by using the following command : cid.GetAttributeValue(ctx.GetStub(), "hf.EnrollmentID") but i'm not able to get the email using the same method cid.GetAttributeValue(ctx.GetStub(), "email")

Any help would be appreciated regarding why the first one is working and the second isn't Does getAttributeValue not support custom made attributes?

Volker
  • 40,468
  • 7
  • 81
  • 87

2 Answers2

0

Here is an example that may be helpful. A previous stackoverflow contribution helped me with a similar situation. I don't have the link for it right now, but thanks anyway.

First of all, you state that you have added attributes successfully. Here is some code as an example which I had placed in the code file for registering users.

//create user attr array
let registerAttrs = [];
let registerAttribute = {
  name: "recycler",
  value: config.recycler,
  ecert: true,
};
registerAttrs.push(registerAttribute);

const secret = await ca.register({
    affiliation: config.affiliation,
    enrollmentID: config.recycler,
    role: "client",
    attrs: registerAttrs,
  },
  adminUser
);

The contract code is able to find the value of "recycler" using the following code. Of particular importance is the getCurrentUserId() function.

async getCurrentUserId(ctx) {
let id = [];
id.push(ctx.clientIdentity.getID());
var begin = id[0].indexOf("/CN=");
var end = id[0].lastIndexOf("::/C=");
let userid = id[0].substring(begin + 4, end);
return userid;}

async getCurrentUserType(ctx) {
let userid = await this.getCurrentUserId(ctx);

//  check user id;  if admin, return type = admin;
//  else return value set for attribute "type" in certificate;
if (userid == "admin") {
  return userid;
}
return ctx.clientIdentity.getAttributeValue(userid);}

The user type returned from the getCurrentUserType function is subsequently examined further up in the contract code, as shown in the following example.

async readTheAsset(ctx, id) {
let userType = await this.getCurrentUserType(ctx);

const buffer = await ctx.stub.getState(id);
const asset = JSON.parse(buffer.toString());

asset.userType = userType;
asset.userID = ctx.clientIdentity.getID();

if (asset.userType === "recycler") {
  throw new Error(`The record cannot be read by ${asset.userType} `);
}

return asset;}

I feel sure that this code should solve your issue, as there is a lot of similarity.

Jerome O'Mahony
  • 167
  • 1
  • 5
  • Hey Jerome, Thank you for the code snippet , i was able to get the custom attribute values by reenrolling the user. The problem with my initial code was that i changed the attributes but since i didn't reenroll it , it didn't reflect in the caClient so i had to call the reenroll function so that the custom attributes got added to the certificate as well i'll attach the code snippet below in case you might want to go through it and use in the future – Rahul Giridharan Mar 30 '22 at 06:25
  • Sometimes it's just the little things that can catch us out. Thanks for the reply. – Jerome O'Mahony Mar 30 '22 at 09:43
0

const updateObj = { enrollmentID : userName, type:'client', affiliation:'' , attrs: [{name: 'email', value: email, ecert: true}, {name: 'orgId', value: orgId, ecert: true}, {name: 'userId', value: userName, ecert: true}] , }

const response = await identityService.update(userName, updateObj ,adminUser) const clientUser = await provider.getUserContext(userIdentity, userName);

            const reenrollment = await caClient.reenroll(clientUser,
                [{
                    name: 'email',
                    optional: false
                },
                {
                    name: 'orgId',
                    optional: false
                },
                {
                    name: 'userId',
                    optional: false
                }
                ]);
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 30 '22 at 10:21