Commit cfd8338c authored by / /\ / /\/'s avatar / /\ / /\/
Browse files

wip, debug dsteem send ops call

parent e90e1b1b
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import shouldComponentUpdate from 'app/utils/shouldComponentUpdate';
import { APP_NAME } from 'app/client_config';
import { connect } from 'react-redux';
import * as communityActions from 'app/redux/CommunityReducer';
import tt from 'counterpart';
import {
PrivateKey,
PublicKey,
key_utils,
} from '@steemit/steem-js/lib/auth/ecc';
const CreateCommunity = ({
accountName,
communityCreateError,
communityCreatePending,
communityCreateSuccess,
createCommunity,
communityDescription,
communityHivemindOperationError,
communityHivemindOperationPending,
communityNSFW,
communityOwnerMasterPassword,
communityOwnerWifPassword,
communityOwnerName,
communityTitle,
isMyAccount,
updateCommunityTitle,
updateCommunityDescription,
updateCommunityNSFW,
updateCommunityOwnerAccountName,
updateCommunityOwnerWifPassword,
}) => {
debugger;
const handleCommunityTitleInput = e => {};
const handleCommunityTitleInput = e => {
if (e.target.value.length > 32) {
return;
}
updateCommunityTitle(e.target.value);
};
const handleCommunityDescriptionInput = e => {
if (e.target.value.length > 120) {
return;
}
updateCommunityDescription(e.target.value);
};
const handleCommunityNSFWInput = e => {
updateCommunityNSFW(e.target.checked);
};
const handleCommunitySubmit = e => {
debugger;
e.preventDefault();
const createCommunitypayload = {
accountName,
communityTitle,
communityDescription,
communityNSFW,
communityOwnerName,
communityOwnerWifPassword,
};
debugger;
createCommunity(createCommunitypayload);
};
const generateCommunityOwnerName = () => {
return `hive-${Math.floor(Math.random() * 90000) + 10000}`;
};
const generateCreatorWifPassword = () => {
return 'P' + key_utils.get_random_key().toWif();
};
const generateWif = () => {
const wif = generateCreatorWifPassword();
updateCommunityOwnerWifPassword(wif);
};
const generateUsername = () => {
const ownerUsername = generateCommunityOwnerName();
updateCommunityOwnerAccountName(ownerUsername);
};
const generateCommunityCredentials = () => {
generateWif();
generateUsername();
};
const generateCommunityCredentialsButton = (
<button
type="button"
className="button hollow"
onClick={generateCommunityCredentials}
>
{tt('g.click_to_generate_password')}
</button>
);
const rememberCredentialsPrompt = (
<div>
<div>{`Your community owner name is: ${communityOwnerName}`}</div>
<div>{`Your password is: ${communityOwnerWifPassword}`}</div>
</div>
);
const rememberCredentialsCheckbox1 = (
<label htmlFor="box1">
<input type="checkbox" name="box1" required />
{tt('g.understand_that_APP_NAME_cannot_recover_password', {
APP_NAME,
})}.
</label>
);
const rememberCredentialsCheckbox2 = (
<label htmlFor="box2">
<input type="checkbox" name="box2" required />
{tt('g.i_saved_password')}.
</label>
);
const submitCreateCommunityFormButton = (
<input type="submit" value="Submit" />
);
return (
<div className="row">
<div className="column large-6 small-12">
<div>CREATE A COMMUNITY</div>
<label htmlFor="community_title">Title</label>
<input
id="community_title"
name="community_title"
minLength="4"
maxLength="30"
onChange={handleCommunityTitleInput}
/>
<label htmlFor="community_description">Description</label>
<input
id="community_description"
name="community_description"
minLength="10"
maxLength="140"
onChange={handleCommunityDescriptionInput}
required
/>
<label id="is_nsfw" htmlFor="is_nsfw">
Is NSFW.
</label>
<input
type="checkbox"
name="is_nsfw"
onChange={handleCommunityNSFWInput}
/>
<form onSubmit={handleCommunitySubmit}>
<div>CREATE A COMMUNITY</div>
<label htmlFor="community_title">
Title
<input
id="community_title"
name="community_title"
type="text"
minLength="4"
maxLength="30"
onChange={handleCommunityTitleInput}
value={communityTitle}
required
/>
</label>
<label htmlFor="community_description">
Description
<input
id="community_description"
name="community_description"
type="text"
minLength="10"
maxLength="140"
onChange={handleCommunityDescriptionInput}
value={communityDescription}
required
/>
</label>
<label id="is_nsfw" htmlFor="is_nsfw">
Is NSFW.
<input
type="checkbox"
name="is_nsfw"
checked={communityNSFW}
onChange={handleCommunityNSFWInput}
/>
</label>
{communityOwnerWifPassword.length <= 0 &&
generateCommunityCredentialsButton}
{communityOwnerWifPassword.length > 0 &&
rememberCredentialsPrompt}
{communityOwnerWifPassword.length > 0 &&
rememberCredentialsCheckbox1}
{communityOwnerWifPassword.length > 0 &&
rememberCredentialsCheckbox2}
{communityOwnerWifPassword.length > 0 &&
submitCreateCommunityFormButton}
</form>
</div>
</div>
);
......@@ -83,6 +203,21 @@ export default connect(
updateCommunityNSFW: isNSFW => {
dispatch(communityActions.setCommunityNSFW(isNSFW));
},
updateCommunityOwnerAccountName: accountName => {
dispatch(
communityActions.setCommunityOwnerAccountName(accountName)
);
},
updateCommunityOwnerWifPassword: password => {
dispatch(
communityActions.setCommunityOwnerWifPassword(password)
);
},
createCommunity: createCommunityPayload => {
dispatch(
communityActions.createCommunity(createCommunityPayload)
);
},
};
}
)(CreateCommunity);
import { fromJS, Map } from 'immutable';
import { fromJS } from 'immutable';
// Action constants
const SET_COMMUNITY_TITLE = 'community/SET_COMMUNITY_TITLE';
......@@ -7,7 +7,7 @@ const SET_COMMUNITY_NSFW = 'community/SET_COMMUNITY_NSFW';
const SET_COMMUNITY_OWNER_ACCOUNT_NAME =
'community/SET_COMMUNITY_OWNER_ACCOUNT_NAME';
const SET_COMMUNITY_OWNER_MASTER_PASSWORD =
const SET_COMMUNITY_OWNER_WIF_PASSWORD =
'community/SET_COMMUNITY_OWNER_MASTER_PASSWORD';
export const CREATE_COMMUNITY_ACCOUNT = 'community/CREATE_COMMUNITY_ACCOUNT'; // Has saga.
......@@ -15,6 +15,7 @@ const CREATE_COMMUNITY_ACCOUNT_PENDING =
'community/CREATE_COMMUNITY_ACCOUNT_PENDING';
const CREATE_COMMUNITY_ACCOUNT_ERROR =
'community/CREATE_COMMUNITY_ACCOUNT_ERROR';
const CREATE_COMMUNITY_SUCCESS = 'community/CREATE_COMMUNITY_SUCCESS';
const COMMUNITY_HIVEMIND_OPERATION = 'community/COMMUNITY_HIVEMIND_OPERATION'; // Has saga.
const COMMUNITY_HIVEMIND_OPERATION_PENDING =
......@@ -28,11 +29,12 @@ const defaultState = fromJS({
communityDescription: '',
communityNSFW: false,
communityOwnerName: '',
communityOwnerMasterPassword: '',
communityOwnerWifPassword: '',
communityCreatePending: false,
communityCreateError: false,
communityHivemindOperationPending: false,
communityHivemindOperationError: false,
communityCreateSuccess: false,
});
export default function reducer(state = defaultState, action) {
......@@ -54,9 +56,9 @@ export default function reducer(state = defaultState, action) {
const name = fromJS(payload);
return state.merge({ communityOwnerName: name });
}
case SET_COMMUNITY_OWNER_MASTER_PASSWORD: {
case SET_COMMUNITY_OWNER_WIF_PASSWORD: {
const password = fromJS(payload);
return state.merge({ communityOwnerMasterPassword: password });
return state.merge({ communityOwnerWifPassword: password });
}
// Has a saga watcher.
case CREATE_COMMUNITY_ACCOUNT: {
......@@ -71,13 +73,19 @@ export default function reducer(state = defaultState, action) {
const err = fromJS(payload);
return state.merge({ communityCreateError: err });
}
case CREATE_COMMUNITY_SUCCESS: {
const success = fromJS(payload);
return state.merge({ communityCreateSuccess: success });
}
// Has a saga watcher.
case COMMUNITY_HIVEMIND_OPERATION: {
return state;
}
case COMMUNITY_HIVEMIND_OPERATION_PENDING: {
const pending = fromJS(payload);
return state.merge({ communityHivemindOperationPending: pending });
return state.merge({
communityHivemindOperationPending: pending,
});
}
case COMMUNITY_HIVEMIND_OPERATION_ERROR: {
const err = fromJS(payload);
......@@ -107,11 +115,12 @@ export const setCommunityOwnerAccountName = payload => ({
type: SET_COMMUNITY_OWNER_ACCOUNT_NAME,
payload,
});
export const setCommunityOwnerMasterPassword = payload => ({
type: SET_COMMUNITY_OWNER_MASTER_PASSWORD,
export const setCommunityOwnerWifPassword = payload => ({
type: SET_COMMUNITY_OWNER_WIF_PASSWORD,
payload,
});
export const createCommunityAccount = payload => ({
// Has a saga.
export const createCommunity = payload => ({
type: CREATE_COMMUNITY_ACCOUNT,
payload,
});
......@@ -124,6 +133,10 @@ export const createCommunityAccountError = payload => ({
type: CREATE_COMMUNITY_ACCOUNT_ERROR,
payload,
});
export const createCommunityAccountSuccess = payload => ({
type: CREATE_COMMUNITY_SUCCESS,
payload,
});
export const communityHivemindOperation = payload => ({
type: COMMUNITY_HIVEMIND_OPERATION,
payload,
......
import { call, put, takeLatest, select } from 'redux-saga/effects';
import { Client, PrivateKey } from 'dsteem';
import * as dsteem from 'dsteem';
import * as communityActions from './CommunityReducer';
import { wait } from './MarketSaga';
// TODO: use steem endpoint from env var.
const dSteemClient = new Client('https://api.steemit.com');
const usernameSelector = state => state.user.current.username;
const activeKeySelector = state => state.user.current.pub_keys_used.active;
const activeKeySelector = state => {
return state.user.getIn(['pub_keys_used']).active;
};
const communityTitleSelector = state => state.community.communityTitle;
const generateAuth = (user, pass, type) => {
const key = PrivateKey.fromLogin(user, pass, type).createPublic();
const key = dsteem.PrivateKey.fromLogin(user, pass, type).createPublic();
if (type == 'memo') return key;
return { weight_threshold: 1, account_auths: [], key_auths: [[key, 1]] };
};
......@@ -41,59 +42,95 @@ export const communityWatches = [
];
export function* createCommunityAccount(createCommunityAction) {
const dSteemClient = new dsteem.Client('https://api.steemit.com');
debugger;
yield put({
type: communityActions.createCommunityAccountPending,
payload: true,
});
const { community, password } = createCommunityAction.payload;
const {
accountName,
communityTitle,
communityDescription,
communityNSFW,
communityOwnerName,
communityOwnerWifPassword,
} = createCommunityAction.payload;
debugger;
try {
// Get the currently logged in user.
const creatorName = yield select(usernameSelector);
// Get the currently logged in user active key.
const creatorActiveKey = yield select(activeKeySelector);
const op = [
'account_create',
{
fee: '3.00 STEEM',
creator: creatorName,
owner: generateAuth(community, password, 'owner'),
active: generateAuth(community, password, 'active'),
posting: generateAuth(community, password, 'posting'),
memo_key: generateAuth(community, password, 'memo'),
creator: accountName,
owner: generateAuth(
communityOwnerName,
communityOwnerWifPassword,
'owner'
),
active: generateAuth(
communityOwnerName,
communityOwnerWifPassword,
'active'
),
posting: generateAuth(
communityOwnerName,
communityOwnerWifPassword,
'posting'
),
memo_key: generateAuth(
communityOwnerPosting,
communityOwnerWifPassword,
'memo'
),
json_metadata: '',
},
];
debugger;
yield call(
dSteemClient.broadcast.sendOperations,
[dSteemClient, dSteemClient.broadcast.sendOperations],
[op],
creatorActiveKey
);
// The client cannot submit custom_json and account_create in the same block. The easiest way around this, for now, is to pause for 3 seconds after the account is created before submitting the ops.
yield call(wait, 3000);
// Call the custom ops sagas.
const ownerPosting = PrivateKey.fromLogin(
community,
password,
debugger;
const communityOwnerPosting = dsteem.PrivateKey.fromLogin(
communityOwnerName,
communityOwnerWifPassword,
'posting'
);
const communityTitle = yield select(communityTitleSelector);
const setRoleOperation = generateHivemindOperation(
'setRole',
{ community, account: creatorName, role: 'admin' },
community,
ownerPosting
{ communityOwnerName, account: accountName, role: 'admin' },
communityOwnerName,
communityOwnerPosting
);
// TODO: Should this op update the community description and NSFW prop?
const updatePropsOperation = generateHivemindOperation(
'updateProps',
{ community, props: { title: communityTitle } },
community,
ownerPosting
{ communityOwnerName, props: { title: communityTitle } },
communityOwnerName,
communityOwnerPosting
);
yield call(dSteemClient.broadcast.sendOperations, [
setRoleOperation,
updatePropsOperation,
]);
yield call(
[dSteemClient, dSteemClient.broadcast.sendOperations],
[setRoleOperation, updatePropsOperation]
);
debugger;
yield put({
type: communityActions.createCommunitySuccess,
payload: true,
});
} catch (error) {
debugger;
console.log(error);
yield put({
type: communityActions.createCommunityAccountError,
payload: true,
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment