Wow, I was trying to make a simple code snippet and practice cleaning up my promises. That didn’t work out but, anyway, this is what I did to create a private MD for encrypting entries, relying on public signing key:
let appInfo = {
id: 'net.maidsafe.api_playground.webclient.10',
name: 'SAFE web API playground',
vendor: 'MaidSafe Ltd.',
scope: null
};
let encryptedEntries = {};
function decryptQueue(i) {
let entries = Object.keys(encryptedEntries);
if ( i === entries.length ) {
console.log('Decrypted entry: ', encryptedEntries);
return;
}
window.safeMutableData.decrypt(mdHandle, encryptedEntries[entries[i]].buf).then(decryptedValue => {
encryptedEntries[entries[i]] = String.fromCharCode.apply(null, new Uint8Array(decryptedValue));
decryptQueue(i + 1);
})
}
window.safeApp.initialise(appInfo).then(appHandle => window.safeApp.authorise(appHandle, {
_public: ['Read', 'Insert', 'Update', 'Delete'],
_publicNames: ['Read', 'Insert', 'Update', 'Delete']
}, { own_container: true })
.then(authUri => window.safeApp.connectAuthorised(appHandle, authUri)))
.then(appHandle => window.safeCrypto.sha3Hash(appHandle, 'explicitly_named_md')
.then(hashedString => ({hashedString, appHandle})))
.then(({hashedString, appHandle}) => window.safeCrypto.getAppPubSignKey(appHandle)
.then(signKeyHandle => window.safeCryptoSignKey.getRaw(signKeyHandle).then(rawSignKey => ({rawSignKey, hashedString, appHandle}))))
.then(({rawSignKey, hashedString, appHandle}) => window.safeCrypto.generateNonce(appHandle).then(nonce => ({rawSignKey, hashedString, appHandle, nonce})))
.then(({rawSignKey, hashedString, appHandle, nonce}) => window.safeMutableData.newPrivate(appHandle, hashedString, 15001, rawSignKey.buffer, nonce.buffer)
.then(mdHandle => window.safeMutableData.encryptKey(mdHandle, 'key1').then(encryptedKey => ({encryptedKey}))
.then(({encryptedKey}) => window.safeMutableData.encryptValue(mdHandle, 'encrypted value').then(encryptedValue => ({encryptedKey, encryptedValue}))
.then(({encryptedKey, encryptedValue}) => window.safeMutableData.quickSetup(mdHandle)
.then(() => window.safeMutableData.newMutation(appHandle)
.then(mutationHandle => window.safeMutableDataMutation.insert(mutationHandle, encryptedKey, encryptedValue)
.then(() => window.safeMutableData.applyEntriesMutation(mdHandle, mutationHandle).then(() => ({mdHandle, appHandle, hashedString})))
)
)
)
)
)
)
.then(({mdHandle, appHandle, hashedString}) => window.safeMutableData.getEntries(mdHandle)
.then(entriesHandle => window.safeMutableDataEntries.forEach(entriesHandle, (k, v) => {
let key = String.fromCharCode.apply(null, k);
let value = String.fromCharCode.apply(null, new Uint8Array(v.buf));
console.log('Key: ', key, ',', 'Value: ', value);
}).then(() => ({appHandle, hashedString}))
))
.then(({appHandle, hashedString}) => window.safeCrypto.getAppPubSignKey(appHandle)
.then(signKeyHandle => window.safeCryptoSignKey.getRaw(signKeyHandle).then(rawSignKey => ({rawSignKey, hashedString, appHandle})))
.then(({rawSignKey, hashedString, appHandle}) => window.safeCrypto.generateNonce(appHandle).then(nonce => ({rawSignKey, hashedString, appHandle, nonce})))
.then(({rawSignKey, hashedString, appHandle, nonce}) => window.safeMutableData.newPrivate(appHandle, hashedString, 15001, rawSignKey.buffer, nonce.buffer))
.then(mdHandle => window.safeMutableData.getEntries(mdHandle)
.then(entriesHandle => window.safeMutableDataEntries.forEach(entriesHandle, (k, v) => {
Object.defineProperty(encryptedEntries, k, {value: v, writable: true, enumerable: true, configurable: true});
}))
)
)
.then(() => {
decryptQueue(0);
})
UPDATE: see my latest post for updated code