Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add & remove a key in key_auth #213

Merged
merged 14 commits into from
Aug 3, 2017
Merged
18 changes: 18 additions & 0 deletions examples/add-key-auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
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);
}
);
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "steem",
"version": "0.6.1",
"version": "0.6.2",
"description": "Steem.js the JavaScript API for Steem blockchain",
"main": "lib/index.js",
"scripts": {
Expand Down
4 changes: 2 additions & 2 deletions src/auth/serializer/src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
};

Expand Down
136 changes: 101 additions & 35 deletions src/broadcast/helpers.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,30 @@
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,
Expand All @@ -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++) {
Expand All @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions test/types_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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],
])
})

Expand Down