Browse Source

Up OrbitDB

develop
Ezerous 6 years ago
parent
commit
0997228dc3
  1. 4
      app/package.json
  2. 11
      app/src/containers/UsernameFormContainer.js
  3. 44
      app/src/orbit.js
  4. 24
      app/src/redux/sagas/orbitSaga.js
  5. 73
      contracts/Forum.sol

4
app/package.json

@ -12,8 +12,8 @@
"connected-react-router": "^6.3.1", "connected-react-router": "^6.3.1",
"drizzle": "^1.3.3", "drizzle": "^1.3.3",
"history": "^4.7.2", "history": "^4.7.2",
"ipfs": "~0.33.0", "ipfs": "~0.34.4",
"orbit-db": "^0.19.9", "orbit-db": "github:mistakia/orbit-db#proto/read-from-disk",
"orbit-db-keystore": "^0.1.0", "orbit-db-keystore": "^0.1.0",
"prop-types": "^15.7.2", "prop-types": "^15.7.2",
"react": "^16.8.3", "react": "^16.8.3",

11
app/src/containers/UsernameFormContainer.js

@ -66,11 +66,14 @@ class UsernameFormContainer extends Component {
const orbitdbInfo = await createDatabases(); const orbitdbInfo = await createDatabases();
this.stackId = drizzle.contracts[contract].methods[signUpMethod] this.stackId = drizzle.contracts[contract].methods[signUpMethod]
.cacheSend(...[this.state.usernameInput, .cacheSend(...[this.state.usernameInput,
orbitdbInfo.id, orbitdbInfo.identityId,
orbitdbInfo.identityPublicKey,
orbitdbInfo.identityPrivateKey,
orbitdbInfo.orbitId,
orbitdbInfo.orbitPublicKey,
orbitdbInfo.orbitPrivateKey,
orbitdbInfo.topicsDB, orbitdbInfo.topicsDB,
orbitdbInfo.postsDB, orbitdbInfo.postsDB
orbitdbInfo.publicKey,
orbitdbInfo.privateKey
]); ]);
} }
/*this.setState({ usernameInput: '' });*/ /*this.setState({ usernameInput: '' });*/

44
app/src/orbit.js

@ -15,7 +15,7 @@ function initIPFS(){
} }
async function createDatabases() { async function createDatabases() {
orbitdb = new OrbitDB(ipfs); orbitdb = await OrbitDB.createInstance(ipfs);
topicsDB = await orbitdb.keyvalue('topics'); topicsDB = await orbitdb.keyvalue('topics');
postsDB = await orbitdb.keyvalue('posts'); postsDB = await orbitdb.keyvalue('posts');
store.dispatch({ store.dispatch({
@ -25,20 +25,42 @@ async function createDatabases() {
postsDB: postsDB, postsDB: postsDB,
id: orbitdb.id 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 directory = "./orbitdb";
let keystore = Keystore.create(path.join(directory, id, '/keystore')); let keystore = Keystore.create(path.join(directory, identityId, '/keystore'));
keystore._storage.setItem(id, JSON.stringify({ keystore._storage.setItem(identityId, JSON.stringify({
publicKey: publicKey, publicKey: identityPublicKey,
privateKey: privateKey privateKey: identityPrivateKey
})); }));
orbitdb = new OrbitDB(ipfs,directory,{peerId:id, keystore:keystore});
topicsDB = await orbitdb.keyvalue('/orbitdb/' + mTopicsDB +'/topics'); keystore._storage.setItem(orbitId, JSON.stringify({
postsDB = await orbitdb.keyvalue('/orbitdb/' + mPostsDB +'/posts'); 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(); topicsDB.load();
postsDB.load(); postsDB.load();

24
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 { contract, getCurrentAccount} from './drizzleUtilsSaga';
import { loadDatabases } from '../../orbit' import { loadDatabases } from '../../orbit'
@ -8,16 +8,18 @@ function* getOrbitDBInfo() {
yield put({type: 'ORRBIT_GETTING_INFO', ...[]}); yield put({type: 'ORRBIT_GETTING_INFO', ...[]});
const account = yield call(getCurrentAccount); const account = yield call(getCurrentAccount);
if(account!==latestAccount) { if(account!==latestAccount) {
//console.log("Deleting local storage..");
//localStorage.clear();
const txObj1 = yield call(contract.methods["hasUserSignedUp"], ...[account]); const txObj1 = yield call(contract.methods["hasUserSignedUp"], ...[account]);
try { try {
const callResult = yield call(txObj1.call, {address:account}); const callResult = yield call(txObj1.call, {address:account});
if(callResult) { if(callResult) {
const txObj2 = yield call(contract.methods["getOrbitDBInfo"], ...[account]); // console.log("Deleting local storage..");
const info = yield call(txObj2.call, {address: account}); // localStorage.clear();
//TODO: update localStorage OrbitDB stuff const txObj2 = yield call(contract.methods["getOrbitIdentityInfo"], ...[account]);
yield call(loadDatabases, info[0], info[1], info[2],info[3], info[4]); 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 else
yield put({type: 'DATABASES_NOT_READY', ...[]}); yield put({type: 'DATABASES_NOT_READY', ...[]});
@ -33,9 +35,11 @@ function* getOrbitDBInfo() {
} }
function* orbitSaga() { function* orbitSaga() {
yield take("DRIZZLE_UTILS_SAGA_INITIALIZED"); yield all([
yield take('IPFS_INITIALIZED'); take("DRIZZLE_UTILS_SAGA_INITIALIZED"),
yield takeLatest("ACCOUNT_CHANGED", getOrbitDBInfo); //TODO: takeEvery (?) take("IPFS_INITIALIZED")
]);
yield takeLatest("ACCOUNT_CHANGED", getOrbitDBInfo);
} }
export default orbitSaga; export default orbitSaga;

73
contracts/Forum.sol

@ -18,11 +18,15 @@ contract Forum {
event UserSignedUp(string username, address userAddress); event UserSignedUp(string username, address userAddress);
event UsernameUpdated(string newName, string oldName,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 (!hasUserSignedUp(msg.sender), "User has already signed up.");
require(!isUserNameTaken(username), "Username is already taken."); require(!isUserNameTaken(username), "Username is already taken.");
users[msg.sender] = User(username, 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); new uint[](0), new uint[](0), block.timestamp, true);
userAddresses[username] = msg.sender; userAddresses[username] = msg.sender;
emit UserSignedUp(username, msg.sender); emit UserSignedUp(username, msg.sender);
@ -75,52 +79,81 @@ contract Forum {
} }
//----------------------------------------OrbitDB---------------------------------------- //----------------------------------------OrbitDB----------------------------------------
// TODO: set upper bounds to strings (instead of being of arbitrary length)
// TODO: not sure if topicsDB//postsDB are actually needed
struct OrbitDB { struct OrbitDB {
string id; // TODO: set an upper bound instead of arbitrary string string identityId;
string topicsDB; //TODO: not sure yet which of these are actually needed string identityPublicKey;
string identityPrivateKey;
string orbitId;
string orbitPublicKey;
string orbitPrivateKey;
string topicsDB;
string postsDB; 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."); 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."); 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."); 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) { function getOrbitPublicKey(address userAddress) public view returns (string memory) {
require (hasUserSignedUp(userAddress), "User hasn't signed up."); 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 //TODO: encrypt using Metamask in the future
function getOrbitPrivateKey(address userAddress) public view returns (string memory) { function getOrbitPrivateKey(address userAddress) public view returns (string memory) {
require (hasUserSignedUp(userAddress), "User hasn't signed up."); 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."); require (hasUserSignedUp(userAddress), "User hasn't signed up.");
return ( return (
users[userAddress].orbitdb.id, users[userAddress].orbitdb.identityId,
users[userAddress].orbitdb.topicsDB, users[userAddress].orbitdb.identityPublicKey,
users[userAddress].orbitdb.postsDB, users[userAddress].orbitdb.identityPrivateKey
users[userAddress].orbitdb.publicKey,
users[userAddress].orbitdb.privateKey
); );
} }
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---------------------------------------- //----------------------------------------POSTING----------------------------------------
struct Topic { struct Topic {

Loading…
Cancel
Save