Skip to content

Commit

Permalink
PharmaLedger-IMI/epi-workspace#888 implemented PathKeyMapping and Wal…
Browse files Browse the repository at this point in the history
…letDBEnclaveHandler
  • Loading branch information
skutner committed Oct 24, 2022
1 parent 55003ff commit f07c11d
Show file tree
Hide file tree
Showing 8 changed files with 145 additions and 55 deletions.
15 changes: 13 additions & 2 deletions enclave/impl/Enclave_Mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,19 @@ function Enclave_Mixin(target, did) {
}

target.getPrivateKeyForSlot = (forDID, slot, callback) => {
target.storageDB.getRecord(constants.TABLE_NAMES.PATH_KEY_SSI_PRIVATE_KEYS, slot, callback);
target.storageDB.getRecord(constants.TABLE_NAMES.PATH_KEY_SSI_PRIVATE_KEYS, slot, (err, privateKeyRecord)=>{
if (err) {
return callback(err);
}
let privateKey;
try{
privateKey = $$.Buffer.from(privateKeyRecord.privateKey);
}catch (e) {
return callback(e);
}

callback(undefined, privateKey);
});
};

target.addIndex = (forDID, table, field, forceReindex, callback) => {
Expand Down Expand Up @@ -180,7 +192,6 @@ function Enclave_Mixin(target, did) {
}

try {

derivedKeySSI.derive((err, _derivedKeySSI) => {
if (err) {
return callback(err);
Expand Down
13 changes: 8 additions & 5 deletions enclave/impl/PathKeyMapping.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,13 @@ function PathKeyMapping(enclaveHandler) {
if (err) {
return callback(err);
}
const derivedKeySSIs = await utils.getAllDerivedSSIsForKeySSI(pathKeySSI);
pathKeysMapping = {...pathKeysMapping, ...derivedKeySSIs};
console.log(pathKeysMapping);
callback();
try {
const derivedKeySSIs = await $$.promisify(utils.getAllDerivedSSIsForKeySSI)(pathKeySSI);
pathKeysMapping = {...pathKeysMapping, ...derivedKeySSIs};
callback();
} catch (e) {
callback(e);
}
});
}
storePathKeySSI();
Expand All @@ -47,7 +50,7 @@ function PathKeyMapping(enclaveHandler) {
}
}
keySSI = keySSI.getIdentifier();
callback(pathKeysMapping[keySSI]);
callback(undefined, pathKeysMapping[keySSI]);
};

utilsAPI.bindAutoPendingFunctions(this);
Expand Down
21 changes: 13 additions & 8 deletions enclave/impl/WalletDBEnclaveHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ function WalletDBEnclaveHandler(walletDBEnclaveKeySSI, config) {
try {
const files = await $$.promisify(enclaveDSU.listFiles)(constants.PATHS.SCATTERED_PATH_KEYS);
if (files.length === config.maxNoScatteredKeys) {
await compactPathKeys();
try {
await compactPathKeys();
} catch (e) {
return callback(e);
}
}
callback();
} catch (e) {
Expand All @@ -62,15 +66,16 @@ function WalletDBEnclaveHandler(walletDBEnclaveKeySSI, config) {
const compactPathKeys = async () => {
let compactedContent = "";
const crypto = require("opendsu").loadAPI("crypto");
const files = $$.promisify(enclaveDSU.listFiles)(constants.PATHS.SCATTERED_PATH_KEYS);
const files = await $$.promisify(enclaveDSU.listFiles)(constants.PATHS.SCATTERED_PATH_KEYS);

for (let i = 0; i < files.length; i++) {
const {key, value} = getKeyValueFromPath(files[i]);
compactedContent = `${compactedContent}${key} ${value}\n`;
}

const fileName = crypto.encodeBase58(crypto.generateRandom("16"));
await enclaveDSU.writeFile(pathModule.join(constants.PATHS.COMPACTED_PATH_KEYS, fileName), compactedContent);
compactedContent = compactedContent.slice(0, compactedContent.length - 1);
const fileName = crypto.encodeBase58(crypto.generateRandom(16));
await $$.promisify(enclaveDSU.writeFile)(pathModule.join(constants.PATHS.COMPACTED_PATH_KEYS, fileName), compactedContent);

for (let i = 0; i < files.length; i++) {
const filePath = pathModule.join(constants.PATHS.SCATTERED_PATH_KEYS, files[i]);
Expand All @@ -80,8 +85,8 @@ function WalletDBEnclaveHandler(walletDBEnclaveKeySSI, config) {

const getKeyValueFromPath = (pth) => {
const lastSegmentIndex = pth.lastIndexOf("/");
const key = lastSegmentIndex.slice(0, lastSegmentIndex);
const value = lastSegmentIndex.slice(lastSegmentIndex + 1);
const key = pth.slice(0, lastSegmentIndex);
const value = pth.slice(lastSegmentIndex + 1);
return {
key, value
}
Expand All @@ -100,7 +105,7 @@ function WalletDBEnclaveHandler(walletDBEnclaveKeySSI, config) {
}

try {
const keySSIsMap = await utils.deriveAllKeySSIsFromPathKeys({...compactedKeys, ...scatteredKeys});
const keySSIsMap = await $$.promisify(utils.deriveAllKeySSIsFromPathKeys)({...compactedKeys, ...scatteredKeys});
callback(undefined, keySSIsMap);
} catch (e) {
callback(e);
Expand Down Expand Up @@ -138,7 +143,7 @@ function WalletDBEnclaveHandler(walletDBEnclaveKeySSI, config) {
try {
for (let i = 0; i < files.length; i++) {
const filePath = pathModule.join(compactedValuesLocation, files[i]);
let compactedFileContent = enclaveDSU.readFile(filePath);
let compactedFileContent = await $$.promisify(enclaveDSU.readFile)(filePath);
compactedFileContent = compactedFileContent.toString();
const partialKeyMap = mapFileContent(compactedFileContent);
pathKeyMap = {...pathKeyMap, ...partialKeyMap};
Expand Down
59 changes: 43 additions & 16 deletions enclave/impl/utils.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,64 @@
const openDSU = require("opendsu");
const keySSISpace = openDSU.loadAPI("keyssi");
const deriveAllKeySSIsFromPathKeys = async (pathKeyMap) => {

const deriveAllKeySSIsFromPathKeys = (pathKeyMap, callback) => {
let keySSIMap = {};
const props = Object.keys(pathKeyMap);
const __deriveAllKeySSIsFromPathKeysRecursively = (index) => {
const pth = props[index];
if (typeof pth === "undefined") {
return callback(undefined, keySSIMap);
}

for (let pth in pathKeyMap) {
const pathSSIIdentifier = pathKeyMap[pth];
let keySSI = keySSISpace.parse(pathSSIIdentifier);
const derivedKeySSIs = await getAllDerivedSSIsForKeySSI(keySSI);
keySSIMap = {...keySSIMap, ...derivedKeySSIs};
let keySSI;
try {
keySSI = keySSISpace.parse(pathSSIIdentifier);
} catch (e) {
return callback(e);
}

getAllDerivedSSIsForKeySSI(keySSI, (err, derivedKeySSIs) => {
if (err) {
return callback(err);
}

keySSIMap = {...keySSIMap, ...derivedKeySSIs};
__deriveAllKeySSIsFromPathKeysRecursively(index + 1);
})

}

return keySSIMap;
__deriveAllKeySSIsFromPathKeysRecursively(0);
}

const getAllDerivedSSIsForKeySSI = async (keySSI) => {
const getAllDerivedSSIsForKeySSI = (keySSI, callback) => {
if (typeof keySSI === "string") {
keySSI = keySSISpace.parse(keySSI);
try {
keySSI = keySSISpace.parse(keySSI);
} catch (e) {
return callback(e);
}
}
const derivedKeySSIs = {};
const keySSIIdentifier = keySSI.getIdentifier();
const __getDerivedKeySSIRecursively = async (currentKeySSI) => {
derivedKeySSIs[keySSIIdentifier] = currentKeySSI.getIdentifier();
const __getDerivedKeySSIsRecursively = (currentKeySSI) => {
derivedKeySSIs[currentKeySSI.getIdentifier()] = keySSIIdentifier;
try {
currentKeySSI = await $$.promisify(currentKeySSI.derive)();
currentKeySSI = currentKeySSI.derive((err, derivedKeySSI) => {
if (err) {
return callback(err);
}

currentKeySSI = derivedKeySSI;
__getDerivedKeySSIsRecursively(currentKeySSI);
});
} catch (e) {
return;
return callback(undefined, derivedKeySSIs);
}

await __getDerivedKeySSIRecursively(currentKeySSI);
}

await __getDerivedKeySSIRecursively(keySSI);
return derivedKeySSIs;
__getDerivedKeySSIsRecursively(keySSI);
}

module.exports = {
Expand Down
12 changes: 7 additions & 5 deletions keyssi/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,16 @@ const createTemplateSeedSSI = (domain, specificString, control, vn, hint, callba
return createTemplateKeySSI(SSITypes.SEED_SSI, domain, specificString, control, vn, hint, callback);
};

const we_createPathKeySSI = (enclave, domain, path, vn, hint, callback) => {
const we_createPathKeySSI = (enclave, domain, path, vn, hint) => {
let pathKeySSI = keySSIFactory.createType(SSITypes.PATH_SSI, enclave);
pathKeySSI.initialize(domain, path, vn, hint, callback);
pathKeySSI.load(SSITypes.PATH_SSI, domain, path, '', vn, hint);
return pathKeySSI;
}

const createPathKeySSI = (domain, path, vn, hint, callback)=>{
return we_createPathKeySSI(openDSU.loadAPI("sc").getMainEnclave(), domain, path, vn, hint, callback);
}
const createPathKeySSI = (domain, path, vn, hint) => {
return we_createPathKeySSI(openDSU.loadAPI("sc").getMainEnclave(), domain, path, vn, hint);
};

const createHashLinkSSI = (domain, hash, vn, hint) => {
const hashLinkSSI = keySSIFactory.createType(SSITypes.HASH_LINK_SSI)
hashLinkSSI.initialize(domain, hash, vn, hint);
Expand Down
29 changes: 12 additions & 17 deletions tests/enclave/PathKeyMappingTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,8 @@ const assert = dc.assert;
const openDSU = require('../../index');
$$.__registerModule("opendsu", openDSU);
const enclaveAPI = openDSU.loadAPI("enclave");
const resolver = openDSU.loadAPI("resolver");
const keySSISpace = openDSU.loadAPI("keyssi");
const scAPI = openDSU.loadAPI("sc");
const w3cDID = openDSU.loadAPI("w3cdid");
const crypto = openDSU.loadAPI("crypto");

const EnclaveHandler = require("../../enclave/impl/WalletDBEnclaveHandler");
const PathKeyMapping = require("../../enclave/impl/PathKeyMapping");
Expand All @@ -26,22 +23,20 @@ assert.callback('WalletDBEnclave test', (testFinished) => {

const mainEnclave = enclaveAPI.initialiseWalletDBEnclave();
mainEnclave.on("initialised", async () => {

await $$.promisify(scAPI.setMainEnclave)(mainEnclave);
const sc = scAPI.refreshSecurityContext();
sc.on("initialised", async () => {
const NO_PATH_KEY_SSIS = 100;
const mainEnclaveKeySSI = await $$.promisify(mainEnclave.getKeySSI)();
const enclaveHandler = new EnclaveHandler(mainEnclaveKeySSI);
const pathKeySSIMapping = new PathKeyMapping(enclaveHandler);
for (let i = 0; i < NO_PATH_KEY_SSIS; i++) {
const path = crypto.generateRandom(16).toString("hex")
const pathKeySSI = await $$.promisify(keySSISpace.createPathKeySSI)("vault", `0/${path}`);
await $$.promisify(pathKeySSIMapping.storePathKeySSI)(pathKeySSI);
}
const mainEnclaveKeySSI = await $$.promisify(mainEnclave.getKeySSI)();
const enclaveHandler = new EnclaveHandler(mainEnclaveKeySSI);
const pathKeySSIMapping = new PathKeyMapping(enclaveHandler);
const pathKeySSI = keySSISpace.createPathKeySSI("vault", `0/path`);
await $$.promisify(pathKeySSIMapping.storePathKeySSI)(pathKeySSI);
const anchorId = await $$.promisify(pathKeySSI.getAnchorId)();
const capableOfSigningKeySSI = await $$.promisify(pathKeySSIMapping.getCapableOfSigningKeySSI)(anchorId);
assert.equal(capableOfSigningKeySSI, pathKeySSI.getIdentifier());
const newPathKeyMapping = new PathKeyMapping(enclaveHandler);
const newCapableOfSigningKeySSI = await $$.promisify(newPathKeyMapping.getCapableOfSigningKeySSI)(anchorId);
assert.equal(newCapableOfSigningKeySSI, capableOfSigningKeySSI);

testFinished();
});
testFinished();
});
});
}, 1000000);
47 changes: 47 additions & 0 deletions tests/enclave/WalletDBEnclaveHandlerTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
require("../../../../psknode/bundles/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 resolver = openDSU.loadAPI("resolver");
const keySSISpace = openDSU.loadAPI("keyssi");
const scAPI = openDSU.loadAPI("sc");
const w3cDID = openDSU.loadAPI("w3cdid");
const crypto = openDSU.loadAPI("crypto");
const utils = require("../../enclave/impl/utils");
const EnclaveHandler = require("../../enclave/impl/WalletDBEnclaveHandler");
assert.callback('WalletDBEnclave test', (testFinished) => {
dc.createTestFolder('createDSU', async (err, folder) => {
const vaultDomainConfig = {
"anchoring": {
"type": "FS",
"option": {}
}
}
await tir.launchConfigurableApiHubTestNodeAsync({domains: [{name: "vault", config: vaultDomainConfig}]});

const mainEnclave = enclaveAPI.initialiseWalletDBEnclave();
mainEnclave.on("initialised", async () => {
await $$.promisify(scAPI.setMainEnclave)(mainEnclave);
const NO_PATH_KEY_SSIS = 30;
const mainEnclaveKeySSI = await $$.promisify(mainEnclave.getKeySSI)();
const enclaveHandler = new EnclaveHandler(mainEnclaveKeySSI, {maxNoScatteredKeys: 10});
let expectedResult = {};
for (let i = 0; i < NO_PATH_KEY_SSIS; i++) {
const path = crypto.generateRandom(16).toString("hex")
const pathKeySSI = keySSISpace.createPathKeySSI("vault", `0/${path}`);
await $$.promisify(enclaveHandler.storePathKeySSI)(pathKeySSI);
const derivedKeySSIs = await $$.promisify(utils.getAllDerivedSSIsForKeySSI)(pathKeySSI);
expectedResult = {...expectedResult, ...derivedKeySSIs};
}

const loadedPaths = await $$.promisify(enclaveHandler.loadPaths)();
assert.objectsAreEqual(expectedResult, loadedPaths);

testFinished();
});
});
}, 1000000);
4 changes: 2 additions & 2 deletions tests/keyssi/PathKeySSITest.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ assert.callback('WalletDBEnclave test', (testFinished) => {
await $$.promisify(scAPI.setMainEnclave)(mainEnclave);
const sc = scAPI.refreshSecurityContext();
sc.on("initialised", async () => {

const pathKeySSI = await $$.promisify(keySSISpace.createPathKeySSI)("vault", "0/somePath")
console.log(pathKeySSI.getIdentifier());
const expectedIdentifier = "ssi:path:vault:0/somePath::v0"
assert.equal(pathKeySSI.getIdentifier(true), expectedIdentifier);
testFinished();
});
});
Expand Down

0 comments on commit f07c11d

Please sign in to comment.