diff --git a/scripts/sdk.js b/scripts/sdk.js index 62828e83910150f43c99ceb651277f4923dc5bf0..203a03bc8eb1ea81118459154ed7df77359ea58e 100644 --- a/scripts/sdk.js +++ b/scripts/sdk.js @@ -37,6 +37,14 @@ const createAttrs = (organization, role, affiliation, email, lang = "es") => [ const toValidHfAffiliation = affiliation => affiliation ? "org1.department1." + affiliation : "org1.department1" +const extractAffiliationFromAttrs = attrs => { + const affiliationAttr = attrs.find(attr => attr.name === "affiliation") + if (!!affiliationAttr) { + return toValidHfAffiliation(affiliationAttr.value) + } + // return undefined +} + class SDKHelper { constructor(connectionProfilePath) { this.connectionProfile = loadProfile(connectionProfilePath) @@ -128,6 +136,8 @@ class SDKHelper { await wallet.put(userId, x509Identity) } + // Register the user, enroll the user, and import the new identity into the wallet. + // if affiliation is specified by client, the affiliation value must be configured in CA async createUser( userId, password, @@ -145,7 +155,7 @@ class SDKHelper { enrollmentID: userId, enrollmentSecret: password, role: "client", // "hf.Type" in the certificate - affiliation: toValidHfAffiliation(affiliation), + affiliation: extractAffiliationFromAttrs(extraAttrs), maxEnrollments: -1, // unlimited attrs: updateAttrs( createAttrs(organization, role, email, language), @@ -155,10 +165,7 @@ class SDKHelper { adminUser ) - // Register the user, enroll the user, and import the new identity into the wallet. - // if affiliation is specified by client, the affiliation value must be configured in CA - const secret = await this._register(organization) - await this._enrollUser(userId, secret, organization) + await this._enrollUser(userId, password, organization) } /** @@ -190,19 +197,12 @@ class SDKHelper { const adminUser = await provider.getUserContext(adminIdentity, "admin") const identityService = await this.caClient.newIdentityService() - const affiliationAttr = oldCert.attrs.find( - attr => attr.name === "affiliation" - ) - const affiliation = affiliationAttr - ? toValidHfAffiliation(affiliationAttr.value) - : undefined - return identityService.update( userId, { enrollmentID: userId, secret: password, - affiliation, + affiliation: extractAffiliationFromAttrs(oldCert.attrs), role: "client", // "hf.Type" in the certificate attrs: updateAttrs( createAttrs(organization, role, email),