From 9aa73fb4d6ed63d9796de1e5510e66b6daff4839 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roberto=20Garc=C3=ADa?= Date: Tue, 8 Nov 2022 11:20:22 +0100 Subject: [PATCH 1/3] added new example of how to build a multivalued credential --- .../6.addEntityMultivaluedCredential.js | 177 ++++++++++++++++++ 1 file changed, 177 insertions(+) create mode 100644 exampleCredentials/6.addEntityMultivaluedCredential.js diff --git a/exampleCredentials/6.addEntityMultivaluedCredential.js b/exampleCredentials/6.addEntityMultivaluedCredential.js new file mode 100644 index 0000000..d7d35a9 --- /dev/null +++ b/exampleCredentials/6.addEntityMultivaluedCredential.js @@ -0,0 +1,177 @@ +const { + transactionFactory, + UserIdentity, + tokensFactory + } = require('alastria-identity-lib') + const Web3 = require('web3') + const fs = require('fs') + const keythereum = require('keythereum') + + const rawdata = fs.readFileSync('../configuration.json') + const configData = JSON.parse(rawdata) + + const keyDataEntity1 = fs.readFileSync( + //'../keystores/entity1-a9728125c573924b2b1ad6a8a8cd9bf6858ced49.json' + '../keystores/eu-issuer.json' + ) + const keystoreDataEntity1 = JSON.parse(keyDataEntity1) + + // Init your blockchain provider + const myBlockchainServiceIp = configData.nodeURL + const web3 = new Web3(new Web3.providers.HttpProvider(myBlockchainServiceIp)) + + // ------------------------------------------------------------------------------ + console.log('\n ------ Preparing Entity1 identity ------ \n') + + // Some fake data to test + + const entity1Keystore = keystoreDataEntity1 + + let entity1PrivateKey + try { + entity1PrivateKey = keythereum.recover( + configData.addressPassword, + entity1Keystore + ) + } catch (error) { + console.error('ERROR: ', error) + } + + const entity1Identity = new UserIdentity( + web3, + `0x${entity1Keystore.address}`, + entity1PrivateKey + ) + + // Creating Multivalued Credential + console.log('\n ------ Creating multivalued credential ------ \n') + + const jti = configData.jti + const kidCredential = configData.kidCredential + //const subjectAlastriaID = configData.subjectAlastriaID + const subjectAlastriaID = configData.didEuSubject + //const didEntity1 = configData.didEntity1 + const didEntity1 = configData.didEuIssuer + const context = configData.context + const tokenExpTime = configData.tokenExpTime + const tokenActivationDate = configData.tokenActivationDate + + // Multivalued Credential Map (key-->value) + const credentialSubject = {} + const credentialKey = configData.credentialKeyFather + const credentialValue = configData.credentialValueFather + const credentialSubKey1 = configData.credentialSubKey1 + const credentialSubKey2 = configData.credentialSubKey2 + const credentialSubKey3 = configData.credentialSubKey3 + const credentialSubKey4 = configData.credentialSubKey4 + const credentialSubValue1 = configData.credentialSubValue1 + const credentialSubValue2 = configData.credentialSubValue2 + const credentialSubValue3 = configData.credentialSubValue3 + const credentialSubValue4 = configData.credentialSubValue4 + credentialSubject[credentialKey] = credentialValue + credentialSubject[credentialKey][credentialSubKey1] = credentialSubValue1 + credentialSubject[credentialKey][credentialSubKey2] = credentialSubValue2 + credentialSubject[credentialKey][credentialSubKey3] = credentialSubValue3 + credentialSubject[credentialKey][credentialSubKey4] = credentialSubValue4 + credentialSubject.levelOfAssurance = 'basic' + console.log("Multivalued Credential", credentialSubject) + + // End multivalued credential data + + // Build multivalued credential with tokensFactory library + const credential = tokensFactory.tokens.createCredential( + didEntity1, + context, + credentialSubject, + kidCredential, + subjectAlastriaID, + tokenExpTime, + tokenActivationDate, + jti + ) + console.log('The credential1 is: ', credential) + + // Sign multivalued credential with tokensFactory library + const signedJWTCredential = tokensFactory.tokens.signJWT( + credential, + entity1PrivateKey + ) + console.log('The signed token is: ', signedJWTCredential) + + // Create Issuer PSMHash of multivalued credential with tokensFactory library + const credentialHash = tokensFactory.tokens.PSMHash( + web3, + signedJWTCredential, + didEntity1 + ) + console.log('The Entity1 PSMHash is:', credentialHash) + fs.writeFileSync( + `./PSMHashEntity1.json`, + JSON.stringify({ psmhash: credentialHash, jwt: signedJWTCredential }) + ) + + // Build addIssuerCredential transaction with transactionFactory library + function addIssuerCredential() { + const issuerCredential = + transactionFactory.credentialRegistry.addIssuerCredential( + web3, + credentialHash + ) + console.log('(addIssuerCredential)The transaction is: ', issuerCredential) + return issuerCredential + } + + function sendSigned(issuerCredentialSigned) { + return new Promise((resolve, reject) => { + web3.eth + .sendSignedTransaction(issuerCredentialSigned) + .on('transactionHash', function (hash) { + console.log('HASH: ', hash) + }) + .on('receipt', (receipt) => { + resolve(receipt) + }) + .on('error', (error) => { + console.error('Error------>', error) + reject(error) + process.exit(1) + }) + }) + } + + async function main() { + const resultIssuerCredential = await addIssuerCredential() + + const issuerCredentialSigned = await entity1Identity.getKnownTransaction( + resultIssuerCredential + ) + console.log( + '(addIssuerCredential)The transaction bytes data is: ', + issuerCredentialSigned + ) + sendSigned(issuerCredentialSigned).then((receipt) => { + console.log('RECEIPT:', receipt) + // Build getIssuerCredentialStatus transaction with transactionFactory library + const issuerCredentialTransaction = + transactionFactory.credentialRegistry.getIssuerCredentialStatus( + web3, + //configData.didEntity1, + configData.didEuIssuer, + credentialHash + ) + web3.eth + .call(issuerCredentialTransaction) + .then((IssuerCredentialStatus) => { + const result = web3.eth.abi.decodeParameters( + ['bool', 'uint8'], + IssuerCredentialStatus + ) + const credentialStatus = { + exists: result[0], + status: result[1] + } + console.log('(IssuerCredentialStatus) -----> ', credentialStatus) + }) + }) + } + main() \ No newline at end of file From c66f09403289dcca5381364b3f6db34ce22fdb30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roberto=20Garc=C3=ADa?= Date: Tue, 8 Nov 2022 11:23:00 +0100 Subject: [PATCH 2/3] fix the numbering of the credential examples --- ...ltivaluedCredential.js => 9.addEntityMultivaluedCredential.js} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename exampleCredentials/{6.addEntityMultivaluedCredential.js => 9.addEntityMultivaluedCredential.js} (100%) diff --git a/exampleCredentials/6.addEntityMultivaluedCredential.js b/exampleCredentials/9.addEntityMultivaluedCredential.js similarity index 100% rename from exampleCredentials/6.addEntityMultivaluedCredential.js rename to exampleCredentials/9.addEntityMultivaluedCredential.js From ab22b79e7f5987cd826fff0a6a5199a27ea85d89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roberto=20Garc=C3=ADa?= Date: Tue, 8 Nov 2022 11:27:58 +0100 Subject: [PATCH 3/3] added neccesary information to configuration.json to build multivalued credential and cleaned new example --- configuration.json | 10 ++++++++++ .../9.addEntityMultivaluedCredential.js | 12 ++++-------- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/configuration.json b/configuration.json index 7e3680b..fa20531 100644 --- a/configuration.json +++ b/configuration.json @@ -49,6 +49,16 @@ "subjectAlastriaID": "did:ala:quor:redt:0x12eeaCCA9eEbB78eB97d7cac6b", "credentialKey": "StudentID", "credentialValue": "11235813", + "credentialKeyFather":"Driving licence", + "credentialValueFather": {}, + "credentialSubKey1": "Driver`s Name", + "credentialSubKey2": "Driver`s Surname", + "credentialSubKey3": "Type of licence", + "credentialSubKey4": "Years with the licence", + "credentialSubValue1": "PersonName", + "credentialSubValue2": "PersonSurname", + "credentialSubValue3": "B", + "credentialSubValue4": "20 years", "uri": "www.google.com", "procUrl": "https://www.empresa.com/alastria/businessprocess/4583", "procHash": "H398sjHd...kldjUYn475n", diff --git a/exampleCredentials/9.addEntityMultivaluedCredential.js b/exampleCredentials/9.addEntityMultivaluedCredential.js index d7d35a9..2d8e125 100644 --- a/exampleCredentials/9.addEntityMultivaluedCredential.js +++ b/exampleCredentials/9.addEntityMultivaluedCredential.js @@ -11,8 +11,7 @@ const { const configData = JSON.parse(rawdata) const keyDataEntity1 = fs.readFileSync( - //'../keystores/entity1-a9728125c573924b2b1ad6a8a8cd9bf6858ced49.json' - '../keystores/eu-issuer.json' + '../keystores/entity1-a9728125c573924b2b1ad6a8a8cd9bf6858ced49.json' ) const keystoreDataEntity1 = JSON.parse(keyDataEntity1) @@ -48,10 +47,8 @@ const { const jti = configData.jti const kidCredential = configData.kidCredential - //const subjectAlastriaID = configData.subjectAlastriaID - const subjectAlastriaID = configData.didEuSubject - //const didEntity1 = configData.didEntity1 - const didEntity1 = configData.didEuIssuer + const subjectAlastriaID = configData.subjectAlastriaID + const didEntity1 = configData.didEntity1 const context = configData.context const tokenExpTime = configData.tokenExpTime const tokenActivationDate = configData.tokenActivationDate @@ -155,8 +152,7 @@ const { const issuerCredentialTransaction = transactionFactory.credentialRegistry.getIssuerCredentialStatus( web3, - //configData.didEntity1, - configData.didEuIssuer, + configData.didEntity1, credentialHash ) web3.eth