From 0997228dc360ac901e0c48ffdbe6b53d4ecdabce Mon Sep 17 00:00:00 2001 From: Ezerous Date: Mon, 25 Feb 2019 19:44:58 +0200 Subject: [PATCH] Up OrbitDB --- app/package.json | 4 +- app/src/containers/UsernameFormContainer.js | 11 ++-- app/src/orbit.js | 44 +++++++++---- app/src/redux/sagas/orbitSaga.js | 24 ++++--- contracts/Forum.sol | 73 +++++++++++++++------ 5 files changed, 109 insertions(+), 47 deletions(-) diff --git a/app/package.json b/app/package.json index 246d0ba..d7b7dfa 100644 --- a/app/package.json +++ b/app/package.json @@ -12,8 +12,8 @@ "connected-react-router": "^6.3.1", "drizzle": "^1.3.3", "history": "^4.7.2", - "ipfs": "~0.33.0", - "orbit-db": "^0.19.9", + "ipfs": "~0.34.4", + "orbit-db": "github:mistakia/orbit-db#proto/read-from-disk", "orbit-db-keystore": "^0.1.0", "prop-types": "^15.7.2", "react": "^16.8.3", diff --git a/app/src/containers/UsernameFormContainer.js b/app/src/containers/UsernameFormContainer.js index 8fae0cb..0b36a6a 100644 --- a/app/src/containers/UsernameFormContainer.js +++ b/app/src/containers/UsernameFormContainer.js @@ -66,11 +66,14 @@ class UsernameFormContainer extends Component { const orbitdbInfo = await createDatabases(); this.stackId = drizzle.contracts[contract].methods[signUpMethod] .cacheSend(...[this.state.usernameInput, - orbitdbInfo.id, + orbitdbInfo.identityId, + orbitdbInfo.identityPublicKey, + orbitdbInfo.identityPrivateKey, + orbitdbInfo.orbitId, + orbitdbInfo.orbitPublicKey, + orbitdbInfo.orbitPrivateKey, orbitdbInfo.topicsDB, - orbitdbInfo.postsDB, - orbitdbInfo.publicKey, - orbitdbInfo.privateKey + orbitdbInfo.postsDB ]); } /*this.setState({ usernameInput: '' });*/ diff --git a/app/src/orbit.js b/app/src/orbit.js index 8cb5090..aebe570 100644 --- a/app/src/orbit.js +++ b/app/src/orbit.js @@ -15,7 +15,7 @@ function initIPFS(){ } async function createDatabases() { - orbitdb = new OrbitDB(ipfs); + orbitdb = await OrbitDB.createInstance(ipfs); topicsDB = await orbitdb.keyvalue('topics'); postsDB = await orbitdb.keyvalue('posts'); store.dispatch({ @@ -25,20 +25,42 @@ async function createDatabases() { postsDB: postsDB, id: orbitdb.id }); - return {id: orbitdb.id, topicsDB: topicsDB.address.root, postsDB: postsDB.address.root, - publicKey: orbitdb.key.getPublic('hex'), privateKey:orbitdb.key.getPrivate('hex')}; + + const identityKey = orbitdb.keystore.getKey(orbitdb.identity.id); + const orbitKey = orbitdb.keystore.getKey(orbitdb.id); + + const returnValue = { + identityId: orbitdb.identity.id, + identityPublicKey: identityKey.getPublic('hex'), + identityPrivateKey: identityKey.getPrivate('hex'), + orbitId: orbitdb.id, + orbitPublicKey: orbitKey.getPublic('hex'), + orbitPrivateKey: orbitKey.getPrivate('hex'), + topicsDB: topicsDB.address.root, + postsDB: postsDB.address.root + }; + console.dir(returnValue); + + return returnValue; } -async function loadDatabases(id,mTopicsDB, mPostsDB,publicKey,privateKey) { +async function loadDatabases(identityId, identityPublicKey, identityPrivateKey, + orbitId, orbitPublicKey, orbitPrivateKey, topicsDB, postsDB) { let directory = "./orbitdb"; - let keystore = Keystore.create(path.join(directory, id, '/keystore')); - keystore._storage.setItem(id, JSON.stringify({ - publicKey: publicKey, - privateKey: privateKey + let keystore = Keystore.create(path.join(directory, identityId, '/keystore')); + keystore._storage.setItem(identityId, JSON.stringify({ + publicKey: identityPublicKey, + privateKey: identityPrivateKey })); - orbitdb = new OrbitDB(ipfs,directory,{peerId:id, keystore:keystore}); - topicsDB = await orbitdb.keyvalue('/orbitdb/' + mTopicsDB +'/topics'); - postsDB = await orbitdb.keyvalue('/orbitdb/' + mPostsDB +'/posts'); + + keystore._storage.setItem(orbitId, JSON.stringify({ + publicKey: orbitPublicKey, + privateKey: orbitPrivateKey + })); + + orbitdb = await OrbitDB.createInstance(ipfs, {directory: directory, peerId:identityId, keystore:keystore}); + topicsDB = await orbitdb.keyvalue('/orbitdb/' + topicsDB +'/topics'); + postsDB = await orbitdb.keyvalue('/orbitdb/' + postsDB +'/posts'); topicsDB.load(); postsDB.load(); diff --git a/app/src/redux/sagas/orbitSaga.js b/app/src/redux/sagas/orbitSaga.js index 5f5e1ef..8b20e98 100644 --- a/app/src/redux/sagas/orbitSaga.js +++ b/app/src/redux/sagas/orbitSaga.js @@ -1,4 +1,4 @@ -import { call, put, take, takeLatest } from 'redux-saga/effects' +import { all, call, put, take, takeLatest } from 'redux-saga/effects' import { contract, getCurrentAccount} from './drizzleUtilsSaga'; import { loadDatabases } from '../../orbit' @@ -8,16 +8,18 @@ function* getOrbitDBInfo() { yield put({type: 'ORRBIT_GETTING_INFO', ...[]}); const account = yield call(getCurrentAccount); if(account!==latestAccount) { - //console.log("Deleting local storage.."); - //localStorage.clear(); const txObj1 = yield call(contract.methods["hasUserSignedUp"], ...[account]); try { const callResult = yield call(txObj1.call, {address:account}); if(callResult) { - const txObj2 = yield call(contract.methods["getOrbitDBInfo"], ...[account]); - const info = yield call(txObj2.call, {address: account}); - //TODO: update localStorage OrbitDB stuff - yield call(loadDatabases, info[0], info[1], info[2],info[3], info[4]); + // console.log("Deleting local storage.."); + // localStorage.clear(); + const txObj2 = yield call(contract.methods["getOrbitIdentityInfo"], ...[account]); + const orbitIdentityInfo = yield call(txObj2.call, {address: account}); + const txObj3 = yield call(contract.methods["getOrbitDBInfo"], ...[account]); + const orbitDBInfo = yield call(txObj3.call, {address: account}); + yield call(loadDatabases, orbitIdentityInfo[0], orbitIdentityInfo[1], orbitIdentityInfo[2], + orbitDBInfo[0], orbitDBInfo[1], orbitDBInfo[2],orbitDBInfo[3], orbitDBInfo[4]); } else yield put({type: 'DATABASES_NOT_READY', ...[]}); @@ -33,9 +35,11 @@ function* getOrbitDBInfo() { } function* orbitSaga() { - yield take("DRIZZLE_UTILS_SAGA_INITIALIZED"); - yield take('IPFS_INITIALIZED'); - yield takeLatest("ACCOUNT_CHANGED", getOrbitDBInfo); //TODO: takeEvery (?) + yield all([ + take("DRIZZLE_UTILS_SAGA_INITIALIZED"), + take("IPFS_INITIALIZED") + ]); + yield takeLatest("ACCOUNT_CHANGED", getOrbitDBInfo); } export default orbitSaga; diff --git a/contracts/Forum.sol b/contracts/Forum.sol index c9e5136..c2eafac 100644 --- a/contracts/Forum.sol +++ b/contracts/Forum.sol @@ -18,11 +18,15 @@ contract Forum { event UserSignedUp(string username, address userAddress); event UsernameUpdated(string newName, string oldName,address userAddress); - function signUp(string memory username, string memory orbitDBId, string memory orbitTopicsDB, string memory orbitPostsDB, string memory orbitPublicKey, string memory orbitPrivateKey) public returns (bool) { + function signUp(string memory username, string memory orbitIdentityId, + string memory orbitIdentityPublicKey, string memory orbitIdentityPrivateKey, + string memory orbitId, string memory orbitPublicKey, string memory orbitPrivateKey, + string memory orbitTopicsDB, string memory orbitPostsDB) public returns (bool) { require (!hasUserSignedUp(msg.sender), "User has already signed up."); require(!isUserNameTaken(username), "Username is already taken."); users[msg.sender] = User(username, - OrbitDB(orbitDBId,orbitTopicsDB, orbitPostsDB, orbitPublicKey, orbitPrivateKey), + OrbitDB(orbitIdentityId, orbitIdentityPublicKey, orbitIdentityPrivateKey, + orbitId, orbitPublicKey, orbitPrivateKey, orbitTopicsDB, orbitPostsDB), new uint[](0), new uint[](0), block.timestamp, true); userAddresses[username] = msg.sender; emit UserSignedUp(username, msg.sender); @@ -75,52 +79,81 @@ contract Forum { } //----------------------------------------OrbitDB---------------------------------------- + // TODO: set upper bounds to strings (instead of being of arbitrary length) + // TODO: not sure if topicsDB//postsDB are actually needed struct OrbitDB { - string id; // TODO: set an upper bound instead of arbitrary string - string topicsDB; //TODO: not sure yet which of these are actually needed + string identityId; + string identityPublicKey; + string identityPrivateKey; + string orbitId; + string orbitPublicKey; + string orbitPrivateKey; + string topicsDB; string postsDB; - string publicKey; - string privateKey; } + function getOrbitIdentityId(address userAddress) public view returns (string memory) { + require (hasUserSignedUp(userAddress), "User hasn't signed up."); + return users[userAddress].orbitdb.identityId; + } - function getOrbitDBId(address userAddress) public view returns (string memory) { + function getOrbitIdentityPublicKey(address userAddress) public view returns (string memory) { require (hasUserSignedUp(userAddress), "User hasn't signed up."); - return users[userAddress].orbitdb.id; + return users[userAddress].orbitdb.identityPublicKey; } - function getOrbitTopicsDB(address userAddress) public view returns (string memory) { + function getOrbitIdentityPrivateKey(address userAddress) public view returns (string memory) { require (hasUserSignedUp(userAddress), "User hasn't signed up."); - return users[userAddress].orbitdb.topicsDB; + return users[userAddress].orbitdb.identityPrivateKey; } - function getOrbitPostsDB(address userAddress) public view returns (string memory) { + + function getOrbitDBId(address userAddress) public view returns (string memory) { require (hasUserSignedUp(userAddress), "User hasn't signed up."); - return users[userAddress].orbitdb.postsDB; + return users[userAddress].orbitdb.orbitId; } function getOrbitPublicKey(address userAddress) public view returns (string memory) { require (hasUserSignedUp(userAddress), "User hasn't signed up."); - return users[userAddress].orbitdb.publicKey; + return users[userAddress].orbitdb.orbitPublicKey; } //TODO: encrypt using Metamask in the future function getOrbitPrivateKey(address userAddress) public view returns (string memory) { require (hasUserSignedUp(userAddress), "User hasn't signed up."); - return users[userAddress].orbitdb.privateKey; + return users[userAddress].orbitdb.orbitPrivateKey; } - function getOrbitDBInfo(address userAddress) public view returns (string memory, string memory, string memory, string memory, string memory) { + function getOrbitTopicsDB(address userAddress) public view returns (string memory) { + require (hasUserSignedUp(userAddress), "User hasn't signed up."); + return users[userAddress].orbitdb.topicsDB; + } + + function getOrbitPostsDB(address userAddress) public view returns (string memory) { + require (hasUserSignedUp(userAddress), "User hasn't signed up."); + return users[userAddress].orbitdb.postsDB; + } + + function getOrbitIdentityInfo(address userAddress) public view returns (string memory, string memory, string memory) { require (hasUserSignedUp(userAddress), "User hasn't signed up."); return ( - users[userAddress].orbitdb.id, - users[userAddress].orbitdb.topicsDB, - users[userAddress].orbitdb.postsDB, - users[userAddress].orbitdb.publicKey, - users[userAddress].orbitdb.privateKey + users[userAddress].orbitdb.identityId, + users[userAddress].orbitdb.identityPublicKey, + users[userAddress].orbitdb.identityPrivateKey ); } + function getOrbitDBInfo(address userAddress) public view returns (string memory, string memory, + string memory, string memory, string memory) { + require (hasUserSignedUp(userAddress), "User hasn't signed up."); + return ( + users[userAddress].orbitdb.orbitId, + users[userAddress].orbitdb.orbitPublicKey, + users[userAddress].orbitdb.orbitPrivateKey, + users[userAddress].orbitdb.topicsDB, + users[userAddress].orbitdb.postsDB + ); + } //----------------------------------------POSTING---------------------------------------- struct Topic {