From 5f5b08a163c503924ffc28359abb4454eb1747d7 Mon Sep 17 00:00:00 2001 From: alupoaiemircea Date: Tue, 8 Aug 2023 17:17:19 +0300 Subject: [PATCH 1/9] #67 in progress --- .../AccountingEnclave/AccountSVD.js | 79 ++++++++++++++++++ .../AccountingEnclave/UserProfileSVD.js | 51 ++++++++++++ .../AccountingEnclave/lambdas/index.js | 81 +++++++++++++++++++ tests/enclave/accounting/RemoteEnclaveTest.js | 76 +++++++++++++++++ 4 files changed, 287 insertions(+) create mode 100644 tests/enclave/accounting/AccountingEnclave/AccountSVD.js create mode 100644 tests/enclave/accounting/AccountingEnclave/UserProfileSVD.js create mode 100644 tests/enclave/accounting/AccountingEnclave/lambdas/index.js create mode 100644 tests/enclave/accounting/RemoteEnclaveTest.js diff --git a/tests/enclave/accounting/AccountingEnclave/AccountSVD.js b/tests/enclave/accounting/AccountingEnclave/AccountSVD.js new file mode 100644 index 0000000..1a3d5a3 --- /dev/null +++ b/tests/enclave/accounting/AccountingEnclave/AccountSVD.js @@ -0,0 +1,79 @@ +/* The concept of a modeled account allows for the depositing of funds into the account, +the withdrawal of money from the account, as well as the structuring of loans between accounts with a fixed daily interest rate. + However, it is set up in such a way that withdrawals are not possible as long as the account has outstanding debts. */ +module.exports = { + ctor: function(usedID){ + this.usedID = usedID; + this.balance = 0; + this.loans = []; + + }, + actions:{ + deposit: function(amount){ + this.balance += amount; + }, + withdraw: function(amount){ + if(this.balance - this.loanedAmount >= amount){ + this.balance -= amount; + } else { + throw new Error("Assert failed: balance should be greater than or equal to amount"); + } + }, + loan: function(amount, dailyInterestRate, loanerId){ + if(this.balance >= amount){ + let loaner = this.session.lookup(loanerId); + if(loaner.loanedAmount == undefined) { + loaner.loanedAmount = amount; + loaner.dailyInterestRate = dailyInterestRate; + loaner.lenderId = this.usedID; + loaner.lendedTimestamp = this.session.now(); + this.loans.push(loanerId); + this.balance -= amount; + loaner.balance += amount; + } else { + throw new Error("Assert failed: loaner should not have an outstanding loan"); + } + } else { + throw new Error("Assert failed: balance should be greater than or equal to amount"); + } + }, + payLoan: function(amount){ + let lender = this.session.lookup(this.lenderId); + /* compute interest using the number of days and then add interest for each passed day for the amount that is returned*/ + let now = this.session.now(); + let days = Math.floor((now - this.lendedTimestamp) / (1000 * 60 * 60 * 24)); + let interest = days * this.dailyInterestRate * this.loanedAmount; + + if(lender != undefined){ + if(amount < this.balance){ + this.balance -= amount; + lender.balance += amount; + this.loanedAmount -= amount; + this.loanedAmount += interest; + } + if(this.loanedAmount < 0){ + lender.balance += this.loanedAmount; /* get back money that were wrongfully returned in the previous step*/ + this.balance -= this.loanedAmount; + this.loanedAmount = 0; + } + /* if the loaner has no more money to give, it gets removed from the list of loans of the lender*/ + if(this.loanedAmount == 0){ + this.lenderId = undefined; + this.dailyInterestRate = undefined; + this.lendedTimestamp = undefined; + let index = lender.loans.indexOf(this.usedID); + if(index > -1){ + lender.loans.splice(index, 1); + } + } + } + } + }, + getTotalDebt: function(){ + let interest = 0; + let now = this.session.now(); + let days = Math.floor((now - this.lendedTimestamp) / (1000 * 60 * 60 * 24)); + interest = days * this.dailyInterestRate * this.loanedAmount; + return this.loanedAmount + interest; + } +} \ No newline at end of file diff --git a/tests/enclave/accounting/AccountingEnclave/UserProfileSVD.js b/tests/enclave/accounting/AccountingEnclave/UserProfileSVD.js new file mode 100644 index 0000000..8ab0a07 --- /dev/null +++ b/tests/enclave/accounting/AccountingEnclave/UserProfileSVD.js @@ -0,0 +1,51 @@ +module.exports = { + ctor: function (userId) { + this.userId = userId; + this.availableInvitesCounter = 0; + this.acceptedInvites = []; + this.followedBrands= []; + /* the email and phone are private by default */ + this.isPrivate = true; + this.friends = []; + }, + read: function (){ + return this.getState(); + }, + actions: { + update: function (name, email, phone, publicDescription, isPrivate) { + this.name = name; + this.email = email; + this.phone = phone; + this.publicDescription=publicDescription; + this.isPrivate = isPrivate; + }, + registeredAsValidatedUser: function () { + this.availableInvitesCounter += 10; + }, + addInvites: function (invites) { + this.availableInvitesCounter += invites; + }, + inviteAccepted: function (inviteId) { + this.availableInvitesCounter--; + this.acceptedInvites.push(inviteId); + this.friends.push(inviteId); + }, + addFriend: function (friendId) { + this.friends.push(friendId); + }, + addBrandToFollowed(brandId) + { + this.followedBrands.push(brandId); + }, + removeBrandFromFollowed(brandId){ + const index = this.followedBrands.indexOf(brandId); + if(index>-1) + { + this.followedBrands.splice(index, 1); + } + else { + console.error(`Error: not found; Failed to remove brandId ${brandId} from user's followedBrands`); + } + } + } +} \ No newline at end of file diff --git a/tests/enclave/accounting/AccountingEnclave/lambdas/index.js b/tests/enclave/accounting/AccountingEnclave/lambdas/index.js new file mode 100644 index 0000000..767d18b --- /dev/null +++ b/tests/enclave/accounting/AccountingEnclave/lambdas/index.js @@ -0,0 +1,81 @@ +const UserProfileSVD = require("../UserProfileSVD"); +let fsSVDStorage; + +function initSVD() { + const fastSVD = require("opendsu").loadApi("svd"); + fsSVDStorage = fastSVD.createFsSVDStorage("./SVDS/"); + fsSVDStorage.registerType("user", UserProfileSVD); +} +function helloWorld (...args) { + const callback = args.pop(); + callback(undefined, args); +} + +function helloWorldWithAudit (...args) { + const callback = args.pop(); + this.audit(...args); + callback(undefined, args); +} +function addShadowUser(callback){ + const userSvdUid = "svd:user:user" + Math.floor(Math.random() * 100000); + const self = this; + fsSVDStorage.createTransaction(function (err, transaction){ + let user = transaction.create(userSvdUid, userSvdUid); + self.insertRecord("", "users", userSvdUid, { user: user.read() }, (err, res) => { + if (!err) { + transaction.commit((commitError) => { + console.log("@@Commit error", commitError); + callback(commitError, res); + }) + } + else { + callback(err); + } + }); + }); +} + +function addBrandToFollowed(brandId, userId, callback) { + const self = this; + const user=self.getRecord("users", userId, callback); + if(user){ + user.addBrandToFollowed(brandId); + } +} + +function removeBrandFromFollowed(brandId, userId, callback){ + const self = this; + const user=self.getRecord("users", userId, callback); + if(user){ + user.removeBrandFromFollowed(brandId); + } +} +function addNewUser(userId, name, email, phone, publicDescription, callback) { + const self = this; + try{ + const shadowUser=self.getRecord("","users", userId, (err) => { + if(err){ + console.log(err); + } + }); + if(shadowUser){ + const isPrivate=false; + shadowUser.update(name, email, phone, publicDescription, isPrivate); + } + } + catch (err) + { + console.log(err) + } +} +module.exports = { + registerLambdas: async (remoteEnclaveServer) => { + initSVD(); + remoteEnclaveServer.addEnclaveMethod("helloWorld", helloWorld, "read"); + remoteEnclaveServer.addEnclaveMethod("helloWorldWithAudit", helloWorldWithAudit, "read"); + remoteEnclaveServer.addEnclaveMethod("addNewUser", addNewUser, "write"); + remoteEnclaveServer.addEnclaveMethod("addShadowUser", addShadowUser, "read"); + remoteEnclaveServer.addEnclaveMethod("addBrandToFollowed", addBrandToFollowed, "write"); + remoteEnclaveServer.addEnclaveMethod("removeBrandFromFollowed", removeBrandFromFollowed, "write"); + } +} \ No newline at end of file diff --git a/tests/enclave/accounting/RemoteEnclaveTest.js b/tests/enclave/accounting/RemoteEnclaveTest.js new file mode 100644 index 0000000..74fa83d --- /dev/null +++ b/tests/enclave/accounting/RemoteEnclaveTest.js @@ -0,0 +1,76 @@ +require("../../../../../builds/output/testsRuntime"); +const tir = require("../../../../../psknode/tests/util/tir"); + +const dc = require("double-check"); +const assert = dc.assert; +const openDSU = require('../../../index'); +$$.__registerModule("opendsu", openDSU); +const enclaveAPI = openDSU.loadAPI("enclave"); +const scAPI = openDSU.loadAPI("sc"); +const w3cDID = openDSU.loadAPI("w3cdid"); + + +assert.callback('Remote enclave test', (testFinished) => { + dc.createTestFolder('createDSU', async (err, folder) => { + const vaultDomainConfig = { + "anchoring": { + "type": "FS", + "option": {} + }, + "enable": ["enclave", "mq"] + } + + const domain = "mqtestdomain"; + process.env.CLOUD_ENCLAVE_SECRET = "some secret"; + await tir.launchConfigurableApiHubTestNodeAsync({domains: [{name: domain, config: vaultDomainConfig}]}); + const serverDID = await tir.launchConfigurableCloudEnclaveTestNodeAsync({ + rootFolder: folder, + domain, + secret: process.env.CLOUD_ENCLAVE_SECRET, + lambdas: "./AccountingEnclave/lambdas", + name: "cloud-enclave" + }); + + const runAssertions = async () => { + try { + const clientDIDDocument = await $$.promisify(w3cDID.createIdentity)("ssi:name", domain, "client"); + const cloudEnclave = enclaveAPI.initialiseRemoteEnclave(clientDIDDocument.getIdentifier(), serverDID); + const TABLE = "users"; + cloudEnclave.on("initialised", async () => { + try { + const result = await $$.promisify(cloudEnclave.callLambda)("addShadowUser"); + assert.true(JSON.parse(result),('pk' in JSON.parse(result)),"pk missing from object"); + const shadowUserId= JSON.parse(result).pk; + await $$.promisify(cloudEnclave.callLambda)("addNewUser", shadowUserId, "name1", "email1", "phone1", "publicDescription1",(err) => { + if(err) + { + console.log(err); + } + }); + const user = await $$.promisify(cloudEnclave.getRecord)(TABLE, shadowUserId,(err) => { + if (err) { + logger.error("Error at initialising remote client" + err); + }}); + console.log(user) + assert.objectsAreEqual(record, addedRecord, "Records do not match"); + const allRecords = await $$.promisify(cloudEnclave.getAllRecords)("some_did", TABLE); + + assert.equal(allRecords.length, 2, "Not all inserted records have been retrieved") + testFinished(); + } catch (e) { + return console.log(e); + } + + }); + + } catch (e) { + return console.log(e); + } + } + const sc = scAPI.getSecurityContext(); + if (sc.isInitialised()) { + return runAssertions(); + } + sc.on("initialised", runAssertions); + }); +}, 20000); From 4cb6da38d7e9501fc527a40d28ed8b26f0c94536 Mon Sep 17 00:00:00 2001 From: alupoaiemircea Date: Wed, 9 Aug 2023 17:24:50 +0300 Subject: [PATCH 2/9] #67 in progress --- .../AccountingEnclave/UserProfileSVD.js | 10 ++- .../AccountingEnclave/lambdas/index.js | 66 +++++++++---------- tests/enclave/accounting/RemoteEnclaveTest.js | 28 ++++---- 3 files changed, 52 insertions(+), 52 deletions(-) diff --git a/tests/enclave/accounting/AccountingEnclave/UserProfileSVD.js b/tests/enclave/accounting/AccountingEnclave/UserProfileSVD.js index 8ab0a07..4db6b29 100644 --- a/tests/enclave/accounting/AccountingEnclave/UserProfileSVD.js +++ b/tests/enclave/accounting/AccountingEnclave/UserProfileSVD.js @@ -1,6 +1,11 @@ module.exports = { - ctor: function (userId) { + ctor: function (userId, name, email, phone, publicDescription, isPrivate) { this.userId = userId; + this.name = name; + this.email = email; + this.phone = phone; + this.publicDescription=publicDescription; + this.isPrivate = isPrivate; this.availableInvitesCounter = 0; this.acceptedInvites = []; this.followedBrands= []; @@ -12,9 +17,10 @@ module.exports = { return this.getState(); }, actions: { - update: function (name, email, phone, publicDescription, isPrivate) { + update: function (userId, name, email, phone, publicDescription, isPrivate) { this.name = name; this.email = email; + this.userId = userId; this.phone = phone; this.publicDescription=publicDescription; this.isPrivate = isPrivate; diff --git a/tests/enclave/accounting/AccountingEnclave/lambdas/index.js b/tests/enclave/accounting/AccountingEnclave/lambdas/index.js index 767d18b..4470740 100644 --- a/tests/enclave/accounting/AccountingEnclave/lambdas/index.js +++ b/tests/enclave/accounting/AccountingEnclave/lambdas/index.js @@ -1,10 +1,12 @@ const UserProfileSVD = require("../UserProfileSVD"); let fsSVDStorage; - +let SVDTransaction; function initSVD() { const fastSVD = require("opendsu").loadApi("svd"); fsSVDStorage = fastSVD.createFsSVDStorage("./SVDS/"); fsSVDStorage.registerType("user", UserProfileSVD); + + } function helloWorld (...args) { const callback = args.pop(); @@ -16,23 +18,39 @@ function helloWorldWithAudit (...args) { this.audit(...args); callback(undefined, args); } -function addShadowUser(callback){ - const userSvdUid = "svd:user:user" + Math.floor(Math.random() * 100000); +function addNewUser(shadowUserId, name, email, phone, publicDescription, isPrivate, callback){ const self = this; - fsSVDStorage.createTransaction(function (err, transaction){ - let user = transaction.create(userSvdUid, userSvdUid); - self.insertRecord("", "users", userSvdUid, { user: user.read() }, (err, res) => { - if (!err) { + if(email){ + fsSVDStorage.createTransaction(function (err, transaction){ + transaction.lookup(shadowUserId, (err, shadowUser) => { + if(err){ + return callback(err); + } + shadowUser.update(email, name, email, phone, publicDescription, isPrivate); transaction.commit((commitError) => { console.log("@@Commit error", commitError); - callback(commitError, res); - }) - } - else { - callback(err); - } + return callback(commitError); + }); + }); + + }); + }else { + const userSvdUid = "svd:user:user" + Math.floor(Math.random() * 100000); + fsSVDStorage.createTransaction(function (err, transaction){ + let user = transaction.create(userSvdUid, userSvdUid, "", "", "", "", ""); + self.insertRecord("", "users", userSvdUid, { user: user.read() }, (err, res) => { + if (!err) { + transaction.commit((commitError) => { + console.log("@@Commit error", commitError); + callback(commitError, res); + }) + } + else { + callback(err); + } + }); }); - }); + } } function addBrandToFollowed(brandId, userId, callback) { @@ -50,31 +68,13 @@ function removeBrandFromFollowed(brandId, userId, callback){ user.removeBrandFromFollowed(brandId); } } -function addNewUser(userId, name, email, phone, publicDescription, callback) { - const self = this; - try{ - const shadowUser=self.getRecord("","users", userId, (err) => { - if(err){ - console.log(err); - } - }); - if(shadowUser){ - const isPrivate=false; - shadowUser.update(name, email, phone, publicDescription, isPrivate); - } - } - catch (err) - { - console.log(err) - } -} + module.exports = { registerLambdas: async (remoteEnclaveServer) => { initSVD(); remoteEnclaveServer.addEnclaveMethod("helloWorld", helloWorld, "read"); remoteEnclaveServer.addEnclaveMethod("helloWorldWithAudit", helloWorldWithAudit, "read"); remoteEnclaveServer.addEnclaveMethod("addNewUser", addNewUser, "write"); - remoteEnclaveServer.addEnclaveMethod("addShadowUser", addShadowUser, "read"); remoteEnclaveServer.addEnclaveMethod("addBrandToFollowed", addBrandToFollowed, "write"); remoteEnclaveServer.addEnclaveMethod("removeBrandFromFollowed", removeBrandFromFollowed, "write"); } diff --git a/tests/enclave/accounting/RemoteEnclaveTest.js b/tests/enclave/accounting/RemoteEnclaveTest.js index 74fa83d..7202c3a 100644 --- a/tests/enclave/accounting/RemoteEnclaveTest.js +++ b/tests/enclave/accounting/RemoteEnclaveTest.js @@ -38,24 +38,18 @@ assert.callback('Remote enclave test', (testFinished) => { const TABLE = "users"; cloudEnclave.on("initialised", async () => { try { - const result = await $$.promisify(cloudEnclave.callLambda)("addShadowUser"); - assert.true(JSON.parse(result),('pk' in JSON.parse(result)),"pk missing from object"); - const shadowUserId= JSON.parse(result).pk; - await $$.promisify(cloudEnclave.callLambda)("addNewUser", shadowUserId, "name1", "email1", "phone1", "publicDescription1",(err) => { - if(err) - { - console.log(err); - } - }); - const user = await $$.promisify(cloudEnclave.getRecord)(TABLE, shadowUserId,(err) => { - if (err) { - logger.error("Error at initialising remote client" + err); - }}); - console.log(user) - assert.objectsAreEqual(record, addedRecord, "Records do not match"); - const allRecords = await $$.promisify(cloudEnclave.getAllRecords)("some_did", TABLE); + const result = await $$.promisify(cloudEnclave.callLambda)("addNewUser", "","", "", "", ""); + const user2 = await $$.promisify(cloudEnclave.getRecord)("",TABLE, JSON.parse(result).pk); + assert.equal(result,JSON.stringify(user2),"user input values differ from user output values"); - assert.equal(allRecords.length, 2, "Not all inserted records have been retrieved") + const result2= await $$.promisify(cloudEnclave.callLambda)("addNewUser", JSON.parse(result).pk, "name1", "email1", "phone1", "publicDescription1", ""); + const user = await $$.promisify(cloudEnclave.getRecord)("",TABLE, JSON.parse(result2).pk); + assert.equal(result2,JSON.stringify(user),"user input values differ from user output values"); + + //assert.objectsAreEqual(record, addedRecord, "Records do not match"); + //const allRecords = await $$.promisify(cloudEnclave.getAllRecords)("some_did", TABLE); + + //assert.equal(allRecords.length, 2, "Not all inserted records have been retrieved") testFinished(); } catch (e) { return console.log(e); From afadfce8e72166a6042e30c0b200e46e5d5b8259 Mon Sep 17 00:00:00 2001 From: alupoaiemircea Date: Thu, 10 Aug 2023 09:43:15 +0300 Subject: [PATCH 3/9] #67 in progress --- .../enclave/accounting/AccountingEnclave/lambdas/index.js | 1 - tests/enclave/accounting/RemoteEnclaveTest.js | 8 ++++---- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/tests/enclave/accounting/AccountingEnclave/lambdas/index.js b/tests/enclave/accounting/AccountingEnclave/lambdas/index.js index 4470740..8cfe0ff 100644 --- a/tests/enclave/accounting/AccountingEnclave/lambdas/index.js +++ b/tests/enclave/accounting/AccountingEnclave/lambdas/index.js @@ -1,6 +1,5 @@ const UserProfileSVD = require("../UserProfileSVD"); let fsSVDStorage; -let SVDTransaction; function initSVD() { const fastSVD = require("opendsu").loadApi("svd"); fsSVDStorage = fastSVD.createFsSVDStorage("./SVDS/"); diff --git a/tests/enclave/accounting/RemoteEnclaveTest.js b/tests/enclave/accounting/RemoteEnclaveTest.js index 7202c3a..cda3770 100644 --- a/tests/enclave/accounting/RemoteEnclaveTest.js +++ b/tests/enclave/accounting/RemoteEnclaveTest.js @@ -39,12 +39,12 @@ assert.callback('Remote enclave test', (testFinished) => { cloudEnclave.on("initialised", async () => { try { const result = await $$.promisify(cloudEnclave.callLambda)("addNewUser", "","", "", "", ""); - const user2 = await $$.promisify(cloudEnclave.getRecord)("",TABLE, JSON.parse(result).pk); - assert.equal(result,JSON.stringify(user2),"user input values differ from user output values"); + const user = await $$.promisify(cloudEnclave.getRecord)("",TABLE, JSON.parse(result).pk); + assert.equal(result,JSON.stringify(user),"user input values differ from user output values"); const result2= await $$.promisify(cloudEnclave.callLambda)("addNewUser", JSON.parse(result).pk, "name1", "email1", "phone1", "publicDescription1", ""); - const user = await $$.promisify(cloudEnclave.getRecord)("",TABLE, JSON.parse(result2).pk); - assert.equal(result2,JSON.stringify(user),"user input values differ from user output values"); + const user2 = await $$.promisify(cloudEnclave.getRecord)("",TABLE, JSON.parse(result2).pk); + assert.equal(result2,JSON.stringify(user2),"user input values differ from user output values"); //assert.objectsAreEqual(record, addedRecord, "Records do not match"); //const allRecords = await $$.promisify(cloudEnclave.getAllRecords)("some_did", TABLE); From 839a9902fd3ebd4694fd9f2bcf5a5dec960dd9af Mon Sep 17 00:00:00 2001 From: alupoaiemircea Date: Thu, 10 Aug 2023 17:20:37 +0300 Subject: [PATCH 4/9] #67 in progress --- .../AccountingEnclave/UserProfileSVD.js | 4 +- .../AccountingEnclave/lambdas/index.js | 92 ++++++++++++------- tests/enclave/accounting/RemoteEnclaveTest.js | 18 ++-- 3 files changed, 66 insertions(+), 48 deletions(-) diff --git a/tests/enclave/accounting/AccountingEnclave/UserProfileSVD.js b/tests/enclave/accounting/AccountingEnclave/UserProfileSVD.js index 4db6b29..d796f5a 100644 --- a/tests/enclave/accounting/AccountingEnclave/UserProfileSVD.js +++ b/tests/enclave/accounting/AccountingEnclave/UserProfileSVD.js @@ -11,11 +11,9 @@ module.exports = { this.followedBrands= []; /* the email and phone are private by default */ this.isPrivate = true; + //this.secretToken=secretToken; this.friends = []; }, - read: function (){ - return this.getState(); - }, actions: { update: function (userId, name, email, phone, publicDescription, isPrivate) { this.name = name; diff --git a/tests/enclave/accounting/AccountingEnclave/lambdas/index.js b/tests/enclave/accounting/AccountingEnclave/lambdas/index.js index 8cfe0ff..231cef8 100644 --- a/tests/enclave/accounting/AccountingEnclave/lambdas/index.js +++ b/tests/enclave/accounting/AccountingEnclave/lambdas/index.js @@ -1,11 +1,10 @@ const UserProfileSVD = require("../UserProfileSVD"); +const {userId} = require("../UserProfileSVD"); let fsSVDStorage; -function initSVD() { +function initSVD(remoteEnclaveServer) { const fastSVD = require("opendsu").loadApi("svd"); - fsSVDStorage = fastSVD.createFsSVDStorage("./SVDS/"); + fsSVDStorage = fastSVD.createFsSVDStorage("./SVDS"); fsSVDStorage.registerType("user", UserProfileSVD); - - } function helloWorld (...args) { const callback = args.pop(); @@ -17,39 +16,63 @@ function helloWorldWithAudit (...args) { this.audit(...args); callback(undefined, args); } -function addNewUser(shadowUserId, name, email, phone, publicDescription, isPrivate, callback){ +function updateUser(userId, name, email, phone, publicDescription, isPrivate, callback){ const self = this; - if(email){ - fsSVDStorage.createTransaction(function (err, transaction){ - transaction.lookup(shadowUserId, (err, shadowUser) => { - if(err){ - return callback(err); - } - shadowUser.update(email, name, email, phone, publicDescription, isPrivate); - transaction.commit((commitError) => { - console.log("@@Commit error", commitError); - return callback(commitError); - }); - }); + fsSVDStorage.createTransaction(function (err, transaction){ + transaction.lookup(userId, (err, userSVD) => { + if(err){ + return callback(err); + } + //const user = $$.promisify(self.getRecord)("", "users", userId); + + if(err){ + return callback(err); + } + userSVD.update(email, name, email, phone, publicDescription, isPrivate); + if(userSVD.email!==email){ + self.deleteRecord("", "users", userId, (err) => { + if (err) { + return callback(err); + } + const userSvdUid = "svd:user:" + crypto.generateRandom(32); + self.insertRecord("", "users", userSvdUid, { user: email }, (err) => { + if (err) { + return callback(err); + } + transaction.commit((commitError) => { + console.log("@@Commit error", commitError); + callback(commitError, userSvdUid); + }); + }); + }); + }else { + transaction.commit((commitError) => { + console.log("@@Commit error", commitError); + callback(commitError, {userId:userId}); + }); + } }); - }else { - const userSvdUid = "svd:user:user" + Math.floor(Math.random() * 100000); - fsSVDStorage.createTransaction(function (err, transaction){ - let user = transaction.create(userSvdUid, userSvdUid, "", "", "", "", ""); - self.insertRecord("", "users", userSvdUid, { user: user.read() }, (err, res) => { - if (!err) { - transaction.commit((commitError) => { - console.log("@@Commit error", commitError); - callback(commitError, res); - }) - } - else { - callback(err); - } - }); + }); +} +function addNewUser(name, email, phone, publicDescription, isPrivate, callback){ + const self = this; + const crypto = require("opendsu").loadApi("crypto"); + const userSvdUid = "svd:user:" + crypto.generateRandom(32).toString("hex"); + fsSVDStorage.createTransaction(function (err, transaction){ + let user = transaction.create(userSvdUid, userSvdUid, name, email, phone, publicDescription, isPrivate); + self.insertRecord("", "users", userSvdUid, { userId: userSvdUid }, (err) => { + if (!err) { + transaction.commit((commitError) => { + console.log("@@Commit error", commitError); + callback(commitError, {userId:userSvdUid}); + }) + } + else { + callback(err); + } }); - } + }); } function addBrandToFollowed(brandId, userId, callback) { @@ -70,10 +93,11 @@ function removeBrandFromFollowed(brandId, userId, callback){ module.exports = { registerLambdas: async (remoteEnclaveServer) => { - initSVD(); + initSVD(remoteEnclaveServer); remoteEnclaveServer.addEnclaveMethod("helloWorld", helloWorld, "read"); remoteEnclaveServer.addEnclaveMethod("helloWorldWithAudit", helloWorldWithAudit, "read"); remoteEnclaveServer.addEnclaveMethod("addNewUser", addNewUser, "write"); + remoteEnclaveServer.addEnclaveMethod("updateUser", updateUser, "write"); remoteEnclaveServer.addEnclaveMethod("addBrandToFollowed", addBrandToFollowed, "write"); remoteEnclaveServer.addEnclaveMethod("removeBrandFromFollowed", removeBrandFromFollowed, "write"); } diff --git a/tests/enclave/accounting/RemoteEnclaveTest.js b/tests/enclave/accounting/RemoteEnclaveTest.js index cda3770..9535068 100644 --- a/tests/enclave/accounting/RemoteEnclaveTest.js +++ b/tests/enclave/accounting/RemoteEnclaveTest.js @@ -38,18 +38,14 @@ assert.callback('Remote enclave test', (testFinished) => { const TABLE = "users"; cloudEnclave.on("initialised", async () => { try { - const result = await $$.promisify(cloudEnclave.callLambda)("addNewUser", "","", "", "", ""); - const user = await $$.promisify(cloudEnclave.getRecord)("",TABLE, JSON.parse(result).pk); - assert.equal(result,JSON.stringify(user),"user input values differ from user output values"); + const userId = await $$.promisify(cloudEnclave.callLambda)("addNewUser", "", "", "", "", ""); + const user = await $$.promisify(cloudEnclave.getRecord)("", TABLE, JSON.parse(userId).userId); + assert.equal(JSON.parse(userId).userId,user.userId,"user input values differ from user output values"); - const result2= await $$.promisify(cloudEnclave.callLambda)("addNewUser", JSON.parse(result).pk, "name1", "email1", "phone1", "publicDescription1", ""); - const user2 = await $$.promisify(cloudEnclave.getRecord)("",TABLE, JSON.parse(result2).pk); - assert.equal(result2,JSON.stringify(user2),"user input values differ from user output values"); - - //assert.objectsAreEqual(record, addedRecord, "Records do not match"); - //const allRecords = await $$.promisify(cloudEnclave.getAllRecords)("some_did", TABLE); - - //assert.equal(allRecords.length, 2, "Not all inserted records have been retrieved") + const userId2 = await $$.promisify(cloudEnclave.callLambda)("updateUser", JSON.parse(userId).userId, "name1", "email1", "phone1", "publicDescription1", ""); + const user2 = await $$.promisify(cloudEnclave.getRecord)("", TABLE, userId2); + assert.equal(JSON.parse(userId2).userId,user2.userId,"user input values differ from user output values"); + console.log(user2); testFinished(); } catch (e) { return console.log(e); From 7c6008e69281b2be4137053871374c3c53a3acc0 Mon Sep 17 00:00:00 2001 From: alupoaiemircea Date: Fri, 11 Aug 2023 13:55:23 +0300 Subject: [PATCH 5/9] #67 in progress --- tests/enclave/accounting/RemoteEnclaveTest.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/enclave/accounting/RemoteEnclaveTest.js b/tests/enclave/accounting/RemoteEnclaveTest.js index 9535068..ee59df6 100644 --- a/tests/enclave/accounting/RemoteEnclaveTest.js +++ b/tests/enclave/accounting/RemoteEnclaveTest.js @@ -43,7 +43,7 @@ assert.callback('Remote enclave test', (testFinished) => { assert.equal(JSON.parse(userId).userId,user.userId,"user input values differ from user output values"); const userId2 = await $$.promisify(cloudEnclave.callLambda)("updateUser", JSON.parse(userId).userId, "name1", "email1", "phone1", "publicDescription1", ""); - const user2 = await $$.promisify(cloudEnclave.getRecord)("", TABLE, userId2); + const user2 = await $$.promisify(cloudEnclave.getRecord)("", TABLE, JSON.parse(userId2).userId); assert.equal(JSON.parse(userId2).userId,user2.userId,"user input values differ from user output values"); console.log(user2); testFinished(); From e4294265dd1346f04a0850952273f3e47cd895ab Mon Sep 17 00:00:00 2001 From: alupoaiemircea Date: Fri, 18 Aug 2023 15:48:51 +0300 Subject: [PATCH 6/9] error at generate modifier ? addNewUser: bind undefined --- .../AccountingEnclave/UserProfileSVD.js | 11 +++++++-- .../AccountingEnclave/lambdas/index.js | 16 ++++++------- tests/enclave/accounting/RemoteEnclaveTest.js | 24 +++++++++++++------ 3 files changed, 34 insertions(+), 17 deletions(-) diff --git a/tests/enclave/accounting/AccountingEnclave/UserProfileSVD.js b/tests/enclave/accounting/AccountingEnclave/UserProfileSVD.js index d796f5a..788090f 100644 --- a/tests/enclave/accounting/AccountingEnclave/UserProfileSVD.js +++ b/tests/enclave/accounting/AccountingEnclave/UserProfileSVD.js @@ -1,5 +1,5 @@ module.exports = { - ctor: function (userId, name, email, phone, publicDescription, isPrivate) { + ctor: function (userId, name, email, phone, publicDescription, secretToken, userDID, isPrivate) { this.userId = userId; this.name = name; this.email = email; @@ -9,20 +9,27 @@ module.exports = { this.availableInvitesCounter = 0; this.acceptedInvites = []; this.followedBrands= []; + this.secretToken=secretToken; + this.userDID = userDID; /* the email and phone are private by default */ this.isPrivate = true; //this.secretToken=secretToken; this.friends = []; }, actions: { - update: function (userId, name, email, phone, publicDescription, isPrivate) { + update: function (userId, name, email, phone, publicDescription, secretToken, userDID, isPrivate) { this.name = name; this.email = email; this.userId = userId; this.phone = phone; this.publicDescription=publicDescription; + this.secretToken=secretToken; + this.userDID = userDID; this.isPrivate = isPrivate; }, + set temporarySecretToken(temporarySecretToken){ + this.temporarySecretToken=temporarySecretToken; + }, registeredAsValidatedUser: function () { this.availableInvitesCounter += 10; }, diff --git a/tests/enclave/accounting/AccountingEnclave/lambdas/index.js b/tests/enclave/accounting/AccountingEnclave/lambdas/index.js index 231cef8..fa2000b 100644 --- a/tests/enclave/accounting/AccountingEnclave/lambdas/index.js +++ b/tests/enclave/accounting/AccountingEnclave/lambdas/index.js @@ -16,7 +16,7 @@ function helloWorldWithAudit (...args) { this.audit(...args); callback(undefined, args); } -function updateUser(userId, name, email, phone, publicDescription, isPrivate, callback){ +function updateUser(userId, name, email, phone, publicDescription, secretToken, userDID, isPrivate, callback){ const self = this; fsSVDStorage.createTransaction(function (err, transaction){ @@ -29,43 +29,43 @@ function updateUser(userId, name, email, phone, publicDescription, isPrivate, ca if(err){ return callback(err); } - userSVD.update(email, name, email, phone, publicDescription, isPrivate); + userSVD.update(email, name, email, phone, publicDescription, secretToken, userDID, isPrivate); if(userSVD.email!==email){ self.deleteRecord("", "users", userId, (err) => { if (err) { return callback(err); } - const userSvdUid = "svd:user:" + crypto.generateRandom(32); + const userSvdUid = "svd:user:" + crypto.generateRandom(32).toString("hex"); self.insertRecord("", "users", userSvdUid, { user: email }, (err) => { if (err) { return callback(err); } transaction.commit((commitError) => { console.log("@@Commit error", commitError); - callback(commitError, userSvdUid); + callback(commitError, {userId:userSvdUid, secretToken:secretToken}); }); }); }); }else { transaction.commit((commitError) => { console.log("@@Commit error", commitError); - callback(commitError, {userId:userId}); + callback(commitError, {userId:userId, secretToken:secretToken}); }); } }); }); } -function addNewUser(name, email, phone, publicDescription, isPrivate, callback){ +function addNewUser(name, email, phone, publicDescription, secretToken, userDID, isPrivate, callback){ const self = this; const crypto = require("opendsu").loadApi("crypto"); const userSvdUid = "svd:user:" + crypto.generateRandom(32).toString("hex"); fsSVDStorage.createTransaction(function (err, transaction){ - let user = transaction.create(userSvdUid, userSvdUid, name, email, phone, publicDescription, isPrivate); + let user = transaction.create(userSvdUid, userSvdUid, name, email, phone, publicDescription, userDID, isPrivate, secretToken); self.insertRecord("", "users", userSvdUid, { userId: userSvdUid }, (err) => { if (!err) { transaction.commit((commitError) => { console.log("@@Commit error", commitError); - callback(commitError, {userId:userSvdUid}); + callback(commitError, {userId:userSvdUid, secretToken:secretToken}); }) } else { diff --git a/tests/enclave/accounting/RemoteEnclaveTest.js b/tests/enclave/accounting/RemoteEnclaveTest.js index ee59df6..e990418 100644 --- a/tests/enclave/accounting/RemoteEnclaveTest.js +++ b/tests/enclave/accounting/RemoteEnclaveTest.js @@ -22,7 +22,7 @@ assert.callback('Remote enclave test', (testFinished) => { const domain = "mqtestdomain"; process.env.CLOUD_ENCLAVE_SECRET = "some secret"; - await tir.launchConfigurableApiHubTestNodeAsync({domains: [{name: domain, config: vaultDomainConfig}]}); + await tir.launchConfigurableApiHubTestNodeAsync({domains: [{name: domain, config: vaultDomainConfig}], rootFolder: folder}); const serverDID = await tir.launchConfigurableCloudEnclaveTestNodeAsync({ rootFolder: folder, domain, @@ -38,13 +38,23 @@ assert.callback('Remote enclave test', (testFinished) => { const TABLE = "users"; cloudEnclave.on("initialised", async () => { try { - const userId = await $$.promisify(cloudEnclave.callLambda)("addNewUser", "", "", "", "", ""); - const user = await $$.promisify(cloudEnclave.getRecord)("", TABLE, JSON.parse(userId).userId); - assert.equal(JSON.parse(userId).userId,user.userId,"user input values differ from user output values"); + const userId = await $$.promisify(cloudEnclave.callLambda)("addNewUser", "", "", "", "", "", "", ''); + const user = await $$.promisify(cloudEnclave.getRecord)("", TABLE, userId.userId); + assert.equal(userId.userId,user.userId,"user input values differ from user output values"); - const userId2 = await $$.promisify(cloudEnclave.callLambda)("updateUser", JSON.parse(userId).userId, "name1", "email1", "phone1", "publicDescription1", ""); - const user2 = await $$.promisify(cloudEnclave.getRecord)("", TABLE, JSON.parse(userId2).userId); - assert.equal(JSON.parse(userId2).userId,user2.userId,"user input values differ from user output values"); + const password = "12345678"; + const crypto = require("opendsu").loadApi("crypto"); + const randomNr = crypto.generateRandom(32); + const secretToken = crypto.encrypt(randomNr,crypto.deriveEncryptionKey(password)); + + // const userDIDPublicKey = crypto.getPublicKeyFromPrivateKey(randomNr); + // const userDID = crypto.generateRandom(32).toString("hex"); + // const userDIDEncrypted = crypto.encrypt(userDID,userDIDPublicKey); + // assert.equal(crypto.decrypt(userDIDEncrypted,randomNr),userDID); + + const userId2 = await $$.promisify(cloudEnclave.callLambda)("updateUser", userId.userId, "name1", "email1", "phone1", "publicDescription1", secretToken, "userDID", ""); + const user2 = await $$.promisify(cloudEnclave.getRecord)("", TABLE, userId2.userId); + assert.equal(userId2.userId,user2.userId,"user input values differ from user output values"); console.log(user2); testFinished(); } catch (e) { From 0a0ed01a309fdb7a571c116ceeff362f6e5576e7 Mon Sep 17 00:00:00 2001 From: alupoaiemircea Date: Tue, 22 Aug 2023 15:16:12 +0300 Subject: [PATCH 7/9] Refactoring --- .../enclave/accounting/AccountingEnclave/UserProfileSVD.js | 2 +- tests/enclave/accounting/AccountingEnclave/lambdas/index.js | 4 ++-- tests/enclave/accounting/RemoteEnclaveTest.js | 6 +++--- .../history | 2 ++ .../state | 1 + .../history | 1 + .../state | 1 + 7 files changed, 11 insertions(+), 6 deletions(-) create mode 100644 tests/enclave/accounting/SVDS/8XHoZwVL9hW9v6tFmVsArc62S6i3oNLZM8f36yTBbWdsUfkw2xDywYvRySD36Vrfq5dshV14LgTf5gssMdjJNwAcgzqJVQrPYJBd/history create mode 100644 tests/enclave/accounting/SVDS/8XHoZwVL9hW9v6tFmVsArc62S6i3oNLZM8f36yTBbWdsUfkw2xDywYvRySD36Vrfq5dshV14LgTf5gssMdjJNwAcgzqJVQrPYJBd/state create mode 100644 tests/enclave/accounting/SVDS/8XHoZwVL9hW9v9Cmqziqh8qNdP9jECV23F2q4bX5PHx9PdCpYJcvDNPfq9pEFCB1kNqvpM2evSuaqas6SCFdpCr4AtMxgMr3HuwQ/history create mode 100644 tests/enclave/accounting/SVDS/8XHoZwVL9hW9v9Cmqziqh8qNdP9jECV23F2q4bX5PHx9PdCpYJcvDNPfq9pEFCB1kNqvpM2evSuaqas6SCFdpCr4AtMxgMr3HuwQ/state diff --git a/tests/enclave/accounting/AccountingEnclave/UserProfileSVD.js b/tests/enclave/accounting/AccountingEnclave/UserProfileSVD.js index 788090f..20ce12d 100644 --- a/tests/enclave/accounting/AccountingEnclave/UserProfileSVD.js +++ b/tests/enclave/accounting/AccountingEnclave/UserProfileSVD.js @@ -27,7 +27,7 @@ module.exports = { this.userDID = userDID; this.isPrivate = isPrivate; }, - set temporarySecretToken(temporarySecretToken){ + setTemporarySecretToken(temporarySecretToken){ this.temporarySecretToken=temporarySecretToken; }, registeredAsValidatedUser: function () { diff --git a/tests/enclave/accounting/AccountingEnclave/lambdas/index.js b/tests/enclave/accounting/AccountingEnclave/lambdas/index.js index fa2000b..a981834 100644 --- a/tests/enclave/accounting/AccountingEnclave/lambdas/index.js +++ b/tests/enclave/accounting/AccountingEnclave/lambdas/index.js @@ -35,7 +35,7 @@ function updateUser(userId, name, email, phone, publicDescription, secretToken, if (err) { return callback(err); } - const userSvdUid = "svd:user:" + crypto.generateRandom(32).toString("hex"); + const userSvdUid = "svd:user:" + crypto.getRandomSecret(32); self.insertRecord("", "users", userSvdUid, { user: email }, (err) => { if (err) { return callback(err); @@ -58,7 +58,7 @@ function updateUser(userId, name, email, phone, publicDescription, secretToken, function addNewUser(name, email, phone, publicDescription, secretToken, userDID, isPrivate, callback){ const self = this; const crypto = require("opendsu").loadApi("crypto"); - const userSvdUid = "svd:user:" + crypto.generateRandom(32).toString("hex"); + const userSvdUid = "svd:user:" + crypto.getRandomSecret(32); fsSVDStorage.createTransaction(function (err, transaction){ let user = transaction.create(userSvdUid, userSvdUid, name, email, phone, publicDescription, userDID, isPrivate, secretToken); self.insertRecord("", "users", userSvdUid, { userId: userSvdUid }, (err) => { diff --git a/tests/enclave/accounting/RemoteEnclaveTest.js b/tests/enclave/accounting/RemoteEnclaveTest.js index e990418..c70e5ac 100644 --- a/tests/enclave/accounting/RemoteEnclaveTest.js +++ b/tests/enclave/accounting/RemoteEnclaveTest.js @@ -47,8 +47,8 @@ assert.callback('Remote enclave test', (testFinished) => { const randomNr = crypto.generateRandom(32); const secretToken = crypto.encrypt(randomNr,crypto.deriveEncryptionKey(password)); - // const userDIDPublicKey = crypto.getPublicKeyFromPrivateKey(randomNr); - // const userDID = crypto.generateRandom(32).toString("hex"); + const userDIDPublicKey = crypto.getPublicKeyFromPrivateKey(randomNr); + const userDID = "someDID" // const userDIDEncrypted = crypto.encrypt(userDID,userDIDPublicKey); // assert.equal(crypto.decrypt(userDIDEncrypted,randomNr),userDID); @@ -73,4 +73,4 @@ assert.callback('Remote enclave test', (testFinished) => { } sc.on("initialised", runAssertions); }); -}, 20000); +}, 200000); diff --git a/tests/enclave/accounting/SVDS/8XHoZwVL9hW9v6tFmVsArc62S6i3oNLZM8f36yTBbWdsUfkw2xDywYvRySD36Vrfq5dshV14LgTf5gssMdjJNwAcgzqJVQrPYJBd/history b/tests/enclave/accounting/SVDS/8XHoZwVL9hW9v6tFmVsArc62S6i3oNLZM8f36yTBbWdsUfkw2xDywYvRySD36Vrfq5dshV14LgTf5gssMdjJNwAcgzqJVQrPYJBd/history new file mode 100644 index 0000000..0e43a40 --- /dev/null +++ b/tests/enclave/accounting/SVDS/8XHoZwVL9hW9v6tFmVsArc62S6i3oNLZM8f36yTBbWdsUfkw2xDywYvRySD36Vrfq5dshV14LgTf5gssMdjJNwAcgzqJVQrPYJBd/history @@ -0,0 +1,2 @@ +{"changes":[{"fn":"ctor","args":["svd:user:aa53ae8fdb9f096a1478e0a50375781a261a788ca6ea2491590ca8e5713ee4bd","","","","","","",""]}],"signature":"<>"} +{"changes":[{"fn":"update","args":["email1","name1","email1","phone1","publicDescription1",{"type":"Buffer","data":[99,228,144,142,177,176,219,228,130,8,106,90,19,37,11,193,211,134,198,226,94,245,52,185,243,70,219,99,122,45,43,158,72,26,213,220,228,57,71,140,131,148,197,18,52,27,138,88,170,106,98,165,221,35,202,139,177,196,169,150,57,216,75,186,24,252,105,191,252,156,115,50,169,170,70,53,84,112,33,27,86,228,85,191,225,165,166,198,66,238,150,186,222,143,151,17]},"userDID",""],"now":1692700863777}],"signature":"<>"} diff --git a/tests/enclave/accounting/SVDS/8XHoZwVL9hW9v6tFmVsArc62S6i3oNLZM8f36yTBbWdsUfkw2xDywYvRySD36Vrfq5dshV14LgTf5gssMdjJNwAcgzqJVQrPYJBd/state b/tests/enclave/accounting/SVDS/8XHoZwVL9hW9v6tFmVsArc62S6i3oNLZM8f36yTBbWdsUfkw2xDywYvRySD36Vrfq5dshV14LgTf5gssMdjJNwAcgzqJVQrPYJBd/state new file mode 100644 index 0000000..d755b2d --- /dev/null +++ b/tests/enclave/accounting/SVDS/8XHoZwVL9hW9v6tFmVsArc62S6i3oNLZM8f36yTBbWdsUfkw2xDywYvRySD36Vrfq5dshV14LgTf5gssMdjJNwAcgzqJVQrPYJBd/state @@ -0,0 +1 @@ +{"userId":"email1","name":"name1","email":"email1","phone":"phone1","publicDescription":"publicDescription1","isPrivate":"","availableInvitesCounter":0,"acceptedInvites":[],"followedBrands":[],"secretToken":{"type":"Buffer","data":[99,228,144,142,177,176,219,228,130,8,106,90,19,37,11,193,211,134,198,226,94,245,52,185,243,70,219,99,122,45,43,158,72,26,213,220,228,57,71,140,131,148,197,18,52,27,138,88,170,106,98,165,221,35,202,139,177,196,169,150,57,216,75,186,24,252,105,191,252,156,115,50,169,170,70,53,84,112,33,27,86,228,85,191,225,165,166,198,66,238,150,186,222,143,151,17]},"userDID":"userDID","friends":[],"__version":2,"__timeOfLastChange":1692700863777} \ No newline at end of file diff --git a/tests/enclave/accounting/SVDS/8XHoZwVL9hW9v9Cmqziqh8qNdP9jECV23F2q4bX5PHx9PdCpYJcvDNPfq9pEFCB1kNqvpM2evSuaqas6SCFdpCr4AtMxgMr3HuwQ/history b/tests/enclave/accounting/SVDS/8XHoZwVL9hW9v9Cmqziqh8qNdP9jECV23F2q4bX5PHx9PdCpYJcvDNPfq9pEFCB1kNqvpM2evSuaqas6SCFdpCr4AtMxgMr3HuwQ/history new file mode 100644 index 0000000..abe17d5 --- /dev/null +++ b/tests/enclave/accounting/SVDS/8XHoZwVL9hW9v9Cmqziqh8qNdP9jECV23F2q4bX5PHx9PdCpYJcvDNPfq9pEFCB1kNqvpM2evSuaqas6SCFdpCr4AtMxgMr3HuwQ/history @@ -0,0 +1 @@ +{"changes":[{"fn":"ctor","args":["svd:user:ca9f73e9a2339094f64b3466ab4489075c0d87cc57f581ec40033fee9bbf6e1c","","","","","","",""]}],"signature":"<>"} diff --git a/tests/enclave/accounting/SVDS/8XHoZwVL9hW9v9Cmqziqh8qNdP9jECV23F2q4bX5PHx9PdCpYJcvDNPfq9pEFCB1kNqvpM2evSuaqas6SCFdpCr4AtMxgMr3HuwQ/state b/tests/enclave/accounting/SVDS/8XHoZwVL9hW9v9Cmqziqh8qNdP9jECV23F2q4bX5PHx9PdCpYJcvDNPfq9pEFCB1kNqvpM2evSuaqas6SCFdpCr4AtMxgMr3HuwQ/state new file mode 100644 index 0000000..f6751dc --- /dev/null +++ b/tests/enclave/accounting/SVDS/8XHoZwVL9hW9v9Cmqziqh8qNdP9jECV23F2q4bX5PHx9PdCpYJcvDNPfq9pEFCB1kNqvpM2evSuaqas6SCFdpCr4AtMxgMr3HuwQ/state @@ -0,0 +1 @@ +{"userId":"svd:user:ca9f73e9a2339094f64b3466ab4489075c0d87cc57f581ec40033fee9bbf6e1c","name":"","email":"","phone":"","publicDescription":"","isPrivate":true,"availableInvitesCounter":0,"acceptedInvites":[],"followedBrands":[],"secretToken":"","userDID":"","friends":[],"__version":1} \ No newline at end of file From 522f3f9ae78ceef0289ffd8d61e87b8c8c9d2303 Mon Sep 17 00:00:00 2001 From: alupoaiemircea Date: Fri, 25 Aug 2023 17:19:22 +0300 Subject: [PATCH 8/9] begin did integration in test case --- .../AccountingEnclave/UserProfileSVD.js | 20 +-- .../AccountingEnclave/lambdas/index.js | 130 +++++++++++------- tests/enclave/accounting/RemoteEnclaveTest.js | 21 +-- .../history | 2 - .../state | 1 - .../history | 1 - .../state | 1 - 7 files changed, 106 insertions(+), 70 deletions(-) delete mode 100644 tests/enclave/accounting/SVDS/8XHoZwVL9hW9v6tFmVsArc62S6i3oNLZM8f36yTBbWdsUfkw2xDywYvRySD36Vrfq5dshV14LgTf5gssMdjJNwAcgzqJVQrPYJBd/history delete mode 100644 tests/enclave/accounting/SVDS/8XHoZwVL9hW9v6tFmVsArc62S6i3oNLZM8f36yTBbWdsUfkw2xDywYvRySD36Vrfq5dshV14LgTf5gssMdjJNwAcgzqJVQrPYJBd/state delete mode 100644 tests/enclave/accounting/SVDS/8XHoZwVL9hW9v9Cmqziqh8qNdP9jECV23F2q4bX5PHx9PdCpYJcvDNPfq9pEFCB1kNqvpM2evSuaqas6SCFdpCr4AtMxgMr3HuwQ/history delete mode 100644 tests/enclave/accounting/SVDS/8XHoZwVL9hW9v9Cmqziqh8qNdP9jECV23F2q4bX5PHx9PdCpYJcvDNPfq9pEFCB1kNqvpM2evSuaqas6SCFdpCr4AtMxgMr3HuwQ/state diff --git a/tests/enclave/accounting/AccountingEnclave/UserProfileSVD.js b/tests/enclave/accounting/AccountingEnclave/UserProfileSVD.js index 20ce12d..bce51a5 100644 --- a/tests/enclave/accounting/AccountingEnclave/UserProfileSVD.js +++ b/tests/enclave/accounting/AccountingEnclave/UserProfileSVD.js @@ -28,7 +28,7 @@ module.exports = { this.isPrivate = isPrivate; }, setTemporarySecretToken(temporarySecretToken){ - this.temporarySecretToken=temporarySecretToken; + this.temporarySecretToken=temporarySecretToken; }, registeredAsValidatedUser: function () { this.availableInvitesCounter += 10; @@ -46,17 +46,17 @@ module.exports = { }, addBrandToFollowed(brandId) { - this.followedBrands.push(brandId); + this.followedBrands.push(brandId); }, removeBrandFromFollowed(brandId){ - const index = this.followedBrands.indexOf(brandId); - if(index>-1) - { - this.followedBrands.splice(index, 1); - } - else { - console.error(`Error: not found; Failed to remove brandId ${brandId} from user's followedBrands`); - } + const index = this.followedBrands.indexOf(brandId); + if(index>-1) + { + this.followedBrands.splice(index, 1); + } + else { + console.error(`Error: not found; Failed to remove brandId ${brandId} from user's followedBrands`); + } } } } \ No newline at end of file diff --git a/tests/enclave/accounting/AccountingEnclave/lambdas/index.js b/tests/enclave/accounting/AccountingEnclave/lambdas/index.js index a981834..8bf3458 100644 --- a/tests/enclave/accounting/AccountingEnclave/lambdas/index.js +++ b/tests/enclave/accounting/AccountingEnclave/lambdas/index.js @@ -17,41 +17,20 @@ function helloWorldWithAudit (...args) { callback(undefined, args); } function updateUser(userId, name, email, phone, publicDescription, secretToken, userDID, isPrivate, callback){ - const self = this; fsSVDStorage.createTransaction(function (err, transaction){ transaction.lookup(userId, (err, userSVD) => { if(err){ return callback(err); } - //const user = $$.promisify(self.getRecord)("", "users", userId); + userSVD.update(email, name, email, phone, publicDescription, secretToken, userDID, isPrivate); + transaction.commit((commitError) => { + if(commitError){ + return callback(commitError); + } + callback({userId:userId, secretToken:secretToken}); + }); - if(err){ - return callback(err); - } - userSVD.update(email, name, email, phone, publicDescription, secretToken, userDID, isPrivate); - if(userSVD.email!==email){ - self.deleteRecord("", "users", userId, (err) => { - if (err) { - return callback(err); - } - const userSvdUid = "svd:user:" + crypto.getRandomSecret(32); - self.insertRecord("", "users", userSvdUid, { user: email }, (err) => { - if (err) { - return callback(err); - } - transaction.commit((commitError) => { - console.log("@@Commit error", commitError); - callback(commitError, {userId:userSvdUid, secretToken:secretToken}); - }); - }); - }); - }else { - transaction.commit((commitError) => { - console.log("@@Commit error", commitError); - callback(commitError, {userId:userId, secretToken:secretToken}); - }); - } }); }); } @@ -60,35 +39,90 @@ function addNewUser(name, email, phone, publicDescription, secretToken, userDID, const crypto = require("opendsu").loadApi("crypto"); const userSvdUid = "svd:user:" + crypto.getRandomSecret(32); fsSVDStorage.createTransaction(function (err, transaction){ - let user = transaction.create(userSvdUid, userSvdUid, name, email, phone, publicDescription, userDID, isPrivate, secretToken); - self.insertRecord("", "users", userSvdUid, { userId: userSvdUid }, (err) => { - if (!err) { - transaction.commit((commitError) => { - console.log("@@Commit error", commitError); - callback(commitError, {userId:userSvdUid, secretToken:secretToken}); - }) + let user = transaction.create(userSvdUid, userSvdUid, name, email, phone, publicDescription, secretToken, userDID, isPrivate); + self.insertRecord("", "users", userSvdUid, { email: email }, (err) => { + if (err) { + return callback(err); } - else { - callback(err); + transaction.commit((commitError) => { + if(commitError){ + return callback(commitError); + } + callback({userId:userSvdUid, secretToken:secretToken}); + }) + }); + }); +} +function getUserSVD(userId,callback){ + + fsSVDStorage.createTransaction(function (err, transaction){ + transaction.lookup(userId, (err, userSVD) => { + if(err){ + return callback(err); } + transaction.commit((commitError) => { + if(commitError){ + return callback(commitError); + } + + + }); + callback(userSVD.getState()); }); + }); } +function getUserIdByEmail(email, callback){ + const self = this; + self.getAllRecords("", "users", (err, users)=>{ + if(err){ + return callback(err); + } + users.forEach((user)=>{ + if(user.email===email){ + return callback({userId:user.pk}); + } + }); + return callback(`No userId found for email: ${email}`); + }); +} function addBrandToFollowed(brandId, userId, callback) { - const self = this; - const user=self.getRecord("users", userId, callback); - if(user){ - user.addBrandToFollowed(brandId); - } + + fsSVDStorage.createTransaction(function (err, transaction){ + transaction.lookup(userId, (err, userSVD) => { + if(err){ + return callback(err); + } + userSVD.addBrandToFollowed(brandId); + transaction.commit((commitError) => { + if(commitError){ + return callback(commitError); + } + callback({brandId:brandId}); + }); + + }); + }); } function removeBrandFromFollowed(brandId, userId, callback){ - const self = this; - const user=self.getRecord("users", userId, callback); - if(user){ - user.removeBrandFromFollowed(brandId); - } + + fsSVDStorage.createTransaction(function (err, transaction){ + transaction.lookup(userId, (err, userSVD) => { + if(err){ + return callback(err); + } + userSVD.removeBrandFromFollowed(brandId); + transaction.commit((commitError) => { + if(commitError){ + return callback(commitError); + } + callback({brandId:brandId}); + }); + + }); + }); } module.exports = { @@ -98,7 +132,9 @@ module.exports = { remoteEnclaveServer.addEnclaveMethod("helloWorldWithAudit", helloWorldWithAudit, "read"); remoteEnclaveServer.addEnclaveMethod("addNewUser", addNewUser, "write"); remoteEnclaveServer.addEnclaveMethod("updateUser", updateUser, "write"); + remoteEnclaveServer.addEnclaveMethod("getUserIdByEmail", getUserIdByEmail, "read"); remoteEnclaveServer.addEnclaveMethod("addBrandToFollowed", addBrandToFollowed, "write"); remoteEnclaveServer.addEnclaveMethod("removeBrandFromFollowed", removeBrandFromFollowed, "write"); + remoteEnclaveServer.addEnclaveMethod("getUserSVD", getUserSVD, "read"); } } \ No newline at end of file diff --git a/tests/enclave/accounting/RemoteEnclaveTest.js b/tests/enclave/accounting/RemoteEnclaveTest.js index c70e5ac..ba2725e 100644 --- a/tests/enclave/accounting/RemoteEnclaveTest.js +++ b/tests/enclave/accounting/RemoteEnclaveTest.js @@ -8,7 +8,7 @@ $$.__registerModule("opendsu", openDSU); const enclaveAPI = openDSU.loadAPI("enclave"); const scAPI = openDSU.loadAPI("sc"); const w3cDID = openDSU.loadAPI("w3cdid"); - +const keySSI = openDSU.loadAPI("keyssi"); assert.callback('Remote enclave test', (testFinished) => { dc.createTestFolder('createDSU', async (err, folder) => { @@ -38,23 +38,28 @@ assert.callback('Remote enclave test', (testFinished) => { const TABLE = "users"; cloudEnclave.on("initialised", async () => { try { - const userId = await $$.promisify(cloudEnclave.callLambda)("addNewUser", "", "", "", "", "", "", ''); + const userId = await $$.promisify(cloudEnclave.callLambda)("addNewUser", "", "", "", "", "", "", ""); const user = await $$.promisify(cloudEnclave.getRecord)("", TABLE, userId.userId); - assert.equal(userId.userId,user.userId,"user input values differ from user output values"); + assert.equal(userId.userId,user.pk,"user input values differ from user output values"); const password = "12345678"; const crypto = require("opendsu").loadApi("crypto"); const randomNr = crypto.generateRandom(32); const secretToken = crypto.encrypt(randomNr,crypto.deriveEncryptionKey(password)); - const userDIDPublicKey = crypto.getPublicKeyFromPrivateKey(randomNr); - const userDID = "someDID" - // const userDIDEncrypted = crypto.encrypt(userDID,userDIDPublicKey); - // assert.equal(crypto.decrypt(userDIDEncrypted,randomNr),userDID); + const domain = "default"; + const seedSSI = await $$.promisify(keySSI.createSeedSSI)(domain); + const didDocument = await $$.promisify(w3cDID.createIdentity)("ssi:key", seedSSI); + + const dataToSign = "someData"; + const signature = await $$.promisify(didDocument.sign)(dataToSign); + const resolvedDIDDocument = await $$.promisify(w3cDID.resolveDID)(didDocument.getIdentifier()); + const verificationResult = await $$.promisify(resolvedDIDDocument.verify)(dataToSign, signature); + assert.true(verificationResult, "Failed to verify signature"); const userId2 = await $$.promisify(cloudEnclave.callLambda)("updateUser", userId.userId, "name1", "email1", "phone1", "publicDescription1", secretToken, "userDID", ""); const user2 = await $$.promisify(cloudEnclave.getRecord)("", TABLE, userId2.userId); - assert.equal(userId2.userId,user2.userId,"user input values differ from user output values"); + assert.equal(userId2.userId,user2.pk,"user input values differ from user output values"); console.log(user2); testFinished(); } catch (e) { diff --git a/tests/enclave/accounting/SVDS/8XHoZwVL9hW9v6tFmVsArc62S6i3oNLZM8f36yTBbWdsUfkw2xDywYvRySD36Vrfq5dshV14LgTf5gssMdjJNwAcgzqJVQrPYJBd/history b/tests/enclave/accounting/SVDS/8XHoZwVL9hW9v6tFmVsArc62S6i3oNLZM8f36yTBbWdsUfkw2xDywYvRySD36Vrfq5dshV14LgTf5gssMdjJNwAcgzqJVQrPYJBd/history deleted file mode 100644 index 0e43a40..0000000 --- a/tests/enclave/accounting/SVDS/8XHoZwVL9hW9v6tFmVsArc62S6i3oNLZM8f36yTBbWdsUfkw2xDywYvRySD36Vrfq5dshV14LgTf5gssMdjJNwAcgzqJVQrPYJBd/history +++ /dev/null @@ -1,2 +0,0 @@ -{"changes":[{"fn":"ctor","args":["svd:user:aa53ae8fdb9f096a1478e0a50375781a261a788ca6ea2491590ca8e5713ee4bd","","","","","","",""]}],"signature":"<>"} -{"changes":[{"fn":"update","args":["email1","name1","email1","phone1","publicDescription1",{"type":"Buffer","data":[99,228,144,142,177,176,219,228,130,8,106,90,19,37,11,193,211,134,198,226,94,245,52,185,243,70,219,99,122,45,43,158,72,26,213,220,228,57,71,140,131,148,197,18,52,27,138,88,170,106,98,165,221,35,202,139,177,196,169,150,57,216,75,186,24,252,105,191,252,156,115,50,169,170,70,53,84,112,33,27,86,228,85,191,225,165,166,198,66,238,150,186,222,143,151,17]},"userDID",""],"now":1692700863777}],"signature":"<>"} diff --git a/tests/enclave/accounting/SVDS/8XHoZwVL9hW9v6tFmVsArc62S6i3oNLZM8f36yTBbWdsUfkw2xDywYvRySD36Vrfq5dshV14LgTf5gssMdjJNwAcgzqJVQrPYJBd/state b/tests/enclave/accounting/SVDS/8XHoZwVL9hW9v6tFmVsArc62S6i3oNLZM8f36yTBbWdsUfkw2xDywYvRySD36Vrfq5dshV14LgTf5gssMdjJNwAcgzqJVQrPYJBd/state deleted file mode 100644 index d755b2d..0000000 --- a/tests/enclave/accounting/SVDS/8XHoZwVL9hW9v6tFmVsArc62S6i3oNLZM8f36yTBbWdsUfkw2xDywYvRySD36Vrfq5dshV14LgTf5gssMdjJNwAcgzqJVQrPYJBd/state +++ /dev/null @@ -1 +0,0 @@ -{"userId":"email1","name":"name1","email":"email1","phone":"phone1","publicDescription":"publicDescription1","isPrivate":"","availableInvitesCounter":0,"acceptedInvites":[],"followedBrands":[],"secretToken":{"type":"Buffer","data":[99,228,144,142,177,176,219,228,130,8,106,90,19,37,11,193,211,134,198,226,94,245,52,185,243,70,219,99,122,45,43,158,72,26,213,220,228,57,71,140,131,148,197,18,52,27,138,88,170,106,98,165,221,35,202,139,177,196,169,150,57,216,75,186,24,252,105,191,252,156,115,50,169,170,70,53,84,112,33,27,86,228,85,191,225,165,166,198,66,238,150,186,222,143,151,17]},"userDID":"userDID","friends":[],"__version":2,"__timeOfLastChange":1692700863777} \ No newline at end of file diff --git a/tests/enclave/accounting/SVDS/8XHoZwVL9hW9v9Cmqziqh8qNdP9jECV23F2q4bX5PHx9PdCpYJcvDNPfq9pEFCB1kNqvpM2evSuaqas6SCFdpCr4AtMxgMr3HuwQ/history b/tests/enclave/accounting/SVDS/8XHoZwVL9hW9v9Cmqziqh8qNdP9jECV23F2q4bX5PHx9PdCpYJcvDNPfq9pEFCB1kNqvpM2evSuaqas6SCFdpCr4AtMxgMr3HuwQ/history deleted file mode 100644 index abe17d5..0000000 --- a/tests/enclave/accounting/SVDS/8XHoZwVL9hW9v9Cmqziqh8qNdP9jECV23F2q4bX5PHx9PdCpYJcvDNPfq9pEFCB1kNqvpM2evSuaqas6SCFdpCr4AtMxgMr3HuwQ/history +++ /dev/null @@ -1 +0,0 @@ -{"changes":[{"fn":"ctor","args":["svd:user:ca9f73e9a2339094f64b3466ab4489075c0d87cc57f581ec40033fee9bbf6e1c","","","","","","",""]}],"signature":"<>"} diff --git a/tests/enclave/accounting/SVDS/8XHoZwVL9hW9v9Cmqziqh8qNdP9jECV23F2q4bX5PHx9PdCpYJcvDNPfq9pEFCB1kNqvpM2evSuaqas6SCFdpCr4AtMxgMr3HuwQ/state b/tests/enclave/accounting/SVDS/8XHoZwVL9hW9v9Cmqziqh8qNdP9jECV23F2q4bX5PHx9PdCpYJcvDNPfq9pEFCB1kNqvpM2evSuaqas6SCFdpCr4AtMxgMr3HuwQ/state deleted file mode 100644 index f6751dc..0000000 --- a/tests/enclave/accounting/SVDS/8XHoZwVL9hW9v9Cmqziqh8qNdP9jECV23F2q4bX5PHx9PdCpYJcvDNPfq9pEFCB1kNqvpM2evSuaqas6SCFdpCr4AtMxgMr3HuwQ/state +++ /dev/null @@ -1 +0,0 @@ -{"userId":"svd:user:ca9f73e9a2339094f64b3466ab4489075c0d87cc57f581ec40033fee9bbf6e1c","name":"","email":"","phone":"","publicDescription":"","isPrivate":true,"availableInvitesCounter":0,"acceptedInvites":[],"followedBrands":[],"secretToken":"","userDID":"","friends":[],"__version":1} \ No newline at end of file From 38b04033fb5ff6e156c6442e011bb12e3e356e8d Mon Sep 17 00:00:00 2001 From: alupoaiemircea Date: Mon, 4 Sep 2023 15:33:06 +0300 Subject: [PATCH 9/9] #58 in progress: added did signature to registration + form validation --- tests/enclave/accounting/RemoteEnclaveTest.js | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/tests/enclave/accounting/RemoteEnclaveTest.js b/tests/enclave/accounting/RemoteEnclaveTest.js index ba2725e..6758b76 100644 --- a/tests/enclave/accounting/RemoteEnclaveTest.js +++ b/tests/enclave/accounting/RemoteEnclaveTest.js @@ -47,16 +47,14 @@ assert.callback('Remote enclave test', (testFinished) => { const randomNr = crypto.generateRandom(32); const secretToken = crypto.encrypt(randomNr,crypto.deriveEncryptionKey(password)); - const domain = "default"; - const seedSSI = await $$.promisify(keySSI.createSeedSSI)(domain); - const didDocument = await $$.promisify(w3cDID.createIdentity)("ssi:key", seedSSI); - + const didDocument = await $$.promisify(w3cDID.createIdentity)("key", undefined, randomNr); const dataToSign = "someData"; const signature = await $$.promisify(didDocument.sign)(dataToSign); - const resolvedDIDDocument = await $$.promisify(w3cDID.resolveDID)(didDocument.getIdentifier()); - const verificationResult = await $$.promisify(resolvedDIDDocument.verify)(dataToSign, signature); + const verificationResult = await $$.promisify(didDocument.verify)(dataToSign, signature); assert.true(verificationResult, "Failed to verify signature"); + + const userId2 = await $$.promisify(cloudEnclave.callLambda)("updateUser", userId.userId, "name1", "email1", "phone1", "publicDescription1", secretToken, "userDID", ""); const user2 = await $$.promisify(cloudEnclave.getRecord)("", TABLE, userId2.userId); assert.equal(userId2.userId,user2.pk,"user input values differ from user output values");