Skip to content

Commit

Permalink
Merge pull request #174 from steemit/add-broadcast-addAccountAuth
Browse files Browse the repository at this point in the history
Add broadcast add account auth
  • Loading branch information
Fabien committed Jun 22, 2017
2 parents ef4db1d + 3618ef3 commit c6bb445
Show file tree
Hide file tree
Showing 7 changed files with 3,347 additions and 1 deletion.
6 changes: 5 additions & 1 deletion .babelrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,9 @@
"presets": [
"es2015",
"es2017"
],
"plugins": [
"transform-async-to-generator",
"transform-regenerator"
]
}
}
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require("babel-polyfill");

const api = require("./lib/api");
const auth = require("./lib/auth");
const broadcast = require("./lib/broadcast");
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@
"babel-cli": "^6.16.0",
"babel-eslint": "^7.1.1",
"babel-loader": "^6.2.5",
"babel-plugin-transform-async-to-generator": "^6.24.1",
"babel-plugin-transform-regenerator": "^6.24.1",
"babel-polyfill": "^6.16.0",
"babel-preset-es2015": "^6.16.0",
"babel-preset-es2017": "^6.16.0",
Expand Down
79 changes: 79 additions & 0 deletions src/broadcast/helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import api from "../api";

const defaultWeight = 1;

exports = module.exports = steemBroadcast => {
steemBroadcast.addAccountAuth = async (
activeWif,
username,
authorizedUsername,
role = "posting",
cb
) => {
const [userAccount] = await api.getAccountsAsync([username]);

const updatedAuthority = userAccount[role];
const authorizedAccounts = updatedAuthority.account_auths.map(
auth => auth[0]
);
const hasAuthority = authorizedAccounts.indexOf(authorizedUsername) !== -1;

if (hasAuthority) {
// user does already exist in authorized list
return cb(null, null);
}
updatedAuthority.account_auths.push([authorizedUsername, defaultWeight]);
const owner = role === "owner" ? updatedAuthority : undefined;
const active = role === "active" ? updatedAuthority : undefined;
const posting = role === "posting" ? updatedAuthority : undefined;
/** Add authority on user account */
steemBroadcast.accountUpdate(
activeWif,
userAccount.name,
owner,
active,
posting,
userAccount.memo_key,
userAccount.json_metadata,
cb
);
};

steemBroadcast.removeAccountAuth = async (
activeWif,
username,
authorizedUsername,
role = "posting",
cb
) => {
const [userAccount] = await api.getAccountsAsync([username]);
const updatedAuthority = userAccount[role];
const totalAuthorizedUser = updatedAuthority.account_auths.length;
for (let i = 0; i < totalAuthorizedUser; i++) {
const user = updatedAuthority.account_auths[i];
if (user[0] === authorizedUsername) {
updatedAuthority.account_auths.splice(i, 1);
break;
}
}
// user does not exist in authorized list
if (totalAuthorizedUser === updatedAuthority.account_auths.length) {
return cb(null, null);
}

const owner = role === "owner" ? updatedAuthority : undefined;
const active = role === "active" ? updatedAuthority : undefined;
const posting = role === "posting" ? updatedAuthority : undefined;

steemBroadcast.accountUpdate(
activeWif,
userAccount.name,
owner,
active,
posting,
userAccount.memo_key,
userAccount.json_metadata,
cb
);
};
};
2 changes: 2 additions & 0 deletions src/broadcast/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Promise from 'bluebird';
import newDebug from 'debug';
import noop from 'lodash/noop';

import broadcastHelpers from './helpers';
import formatterFactory from '../formatter';
import operations from './operations.json';
import steemApi from '../api';
Expand Down Expand Up @@ -116,6 +117,7 @@ operations.forEach((operation) => {
});

const toString = obj => typeof obj === 'object' ? JSON.stringify(obj) : obj;
broadcastHelpers(steemBroadcast);

Promise.promisifyAll(steemBroadcast);

Expand Down
2 changes: 2 additions & 0 deletions src/browser.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require("babel-polyfill");

const api = require("./api");
const auth = require("./auth");
const broadcast = require("./broadcast");
Expand Down
Loading

0 comments on commit c6bb445

Please sign in to comment.