Commit 03af7510 authored by Fabien's avatar Fabien Committed by GitHub

Merge pull request #213 from steemit/add-key-auth

Add & remove a key in key_auth
parents 2454f5e4 53325993
const steem = require('../lib');
/* Generate private active WIF */
const username = process.env.STEEM_USERNAME;
const password = process.env.STEEM_PASSWORD;
const privActiveWif = steem.auth.toWif(username, password, 'active');
/** Add posting key auth */
steem.broadcast.addKeyAuth({
signingKey: privActiveWif,
username,
authorizedKey: 'STM88CPfhCmeEzCnvC1Cjc3DNd1DTjkMcmihih8SSxmm4LBqRq5Y9',
role: 'posting',
},
(err, result) => {
console.log(err, result);
}
);
{
"name": "steem",
"version": "0.6.1",
"version": "0.6.2",
"description": "Steem.js the JavaScript API for Steem blockchain",
"main": "lib/index.js",
"scripts": {
......
......@@ -894,8 +894,8 @@ Types.public_key = {
return object.toString()
},
compare(a, b) {
// sort decending
return -1 * strCmp(a.toString(), b.toString())
// sort ascending
return 1 * strCmp(a.toString(), b.toString())
}
};
......
import api from "../api";
const defaultWeight = 1;
import api from '../api';
exports = module.exports = steemBroadcast => {
steemBroadcast.addAccountAuth = (
activeWif,
username,
authorizedUsername,
role = "posting",
cb
) => {
api.getAccountsAsync([username]).then(([userAccount]) => {
steemBroadcast.addAccountAuth = ({ signingKey, username, authorizedUsername, role = 'posting', weight }, cb) => {
api.getAccounts([username], (err, [userAccount]) => {
if (err) { return cb(new Error(err), null); }
if (!userAccount) { return cb(new Error('Invalid account name'), null); }
const updatedAuthority = userAccount[role];
const authorizedAccounts = updatedAuthority.account_auths.map(
auth => auth[0]
);
const hasAuthority =
authorizedAccounts.indexOf(authorizedUsername) !== -1;
/** Release callback if the account already exist in the account_auths array */
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;
/** Use weight_thresold as default weight */
weight = weight || userAccount[role].weight_threshold;
updatedAuthority.account_auths.push([authorizedUsername, weight]);
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,
signingKey,
userAccount.name,
owner,
active,
......@@ -40,14 +36,11 @@ exports = module.exports = steemBroadcast => {
});
};
steemBroadcast.removeAccountAuth = (
activeWif,
username,
authorizedUsername,
role = "posting",
cb
) => {
api.getAccountsAsync([username]).then(([userAccount]) => {
steemBroadcast.removeAccountAuth = ({ signingKey, username, authorizedUsername, role = 'posting' }, cb) => {
api.getAccounts([username], (err, [userAccount]) => {
if (err) { return cb(new Error(err), null); }
if (!userAccount) { return cb(new Error('Invalid account name'), null); }
const updatedAuthority = userAccount[role];
const totalAuthorizedUser = updatedAuthority.account_auths.length;
for (let i = 0; i < totalAuthorizedUser; i++) {
......@@ -57,17 +50,90 @@ exports = module.exports = steemBroadcast => {
break;
}
}
// user does not exist in authorized list
/** Release callback if the account does not exist in the account_auths array */
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;
const owner = role === 'owner' ? updatedAuthority : undefined;
const active = role === 'active' ? updatedAuthority : undefined;
const posting = role === 'posting' ? updatedAuthority : undefined;
steemBroadcast.accountUpdate(
signingKey,
userAccount.name,
owner,
active,
posting,
userAccount.memo_key,
userAccount.json_metadata,
cb
);
});
};
steemBroadcast.addKeyAuth = ({ signingKey, username, authorizedKey, role = 'posting', weight }, cb) => {
api.getAccounts([username], (err, [userAccount]) => {
if (err) { return cb(new Error(err), null); }
if (!userAccount) { return cb(new Error('Invalid account name'), null); }
const updatedAuthority = userAccount[role];
/** Release callback if the key already exist in the key_auths array */
const authorizedKeys = updatedAuthority.key_auths.map(auth => auth[0]);
const hasAuthority = authorizedKeys.indexOf(authorizedKey) !== -1;
if (hasAuthority) {
return cb(null, null);
}
/** Use weight_thresold as default weight */
weight = weight || userAccount[role].weight_threshold;
updatedAuthority.key_auths.push([authorizedKey, weight]);
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(
signingKey,
userAccount.name,
owner,
active,
posting,
userAccount.memo_key,
userAccount.json_metadata,
cb
);
});
};
steemBroadcast.removeKeyAuth = ({ signingKey, username, authorizedKey, role = 'posting' }, cb) => {
api.getAccounts([username], (err, [userAccount]) => {
if (err) { return cb(new Error(err), null); }
if (!userAccount) { return cb(new Error('Invalid account name'), null); }
const updatedAuthority = userAccount[role];
const totalAuthorizedKey = updatedAuthority.key_auths.length;
for (let i = 0; i < totalAuthorizedKey; i++) {
const user = updatedAuthority.key_auths[i];
if (user[0] === authorizedKey) {
updatedAuthority.key_auths.splice(i, 1);
break;
}
}
/** Release callback if the key does not exist in the key_auths array */
if (totalAuthorizedKey === updatedAuthority.key_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,
signingKey,
userAccount.name,
owner,
active,
......
......@@ -53,13 +53,13 @@ describe("steem.auth: types", function() {
it("public_key sort", function() {
let mapType = type.map(type.public_key, type.uint16)
let map = mapType.fromObject([//not sorted
["STM56ankGHKf6qUsQe7vPsXTSEqST6Dt1ff73aV3YQbedzRua8NLQ",0],
["STM8me6d9PqzTgcoHxx6b4rnvWVTqz11kafidRAZwfacJkcJtfd75",0],
["STM8me6d9PqzTgcoHxx6b4rnvWVTqz11kafidRAZwfacJkcJtfd75",0],
["STM56ankGHKf6qUsQe7vPsXTSEqST6Dt1ff73aV3YQbedzRua8NLQ",0],
])
let mapObject = mapType.toObject(map)
assert.deepEqual(mapObject, [ // sorted (uppercase comes first)
["STM8me6d9PqzTgcoHxx6b4rnvWVTqz11kafidRAZwfacJkcJtfd75",0],
["STM56ankGHKf6qUsQe7vPsXTSEqST6Dt1ff73aV3YQbedzRua8NLQ",0],
["STM8me6d9PqzTgcoHxx6b4rnvWVTqz11kafidRAZwfacJkcJtfd75",0],
])
})
......
Markdown is supported
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