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
22 changes: 22 additions & 0 deletions examples/add-key-auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
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');

/* Output public active WIF */
//const pubActiveWif = steem.auth.wifToPublic(privActiveWif);
//console.log(pubActiveWif);

/* Add posting key auth */
steem.broadcast.addKeyAuth(
privActiveWif,
username,
'STM88CPfhCmeEzCnvC1Cjc3DNd1DTjkMcmihih8SSxmm4LBqRq5Y9', // @fabien public posting WIF
'posting',
1,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i would love for these arguments to be named instead of positional

(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
130 changes: 109 additions & 21 deletions src/broadcast/helpers.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,32 @@
import api from "../api";

const defaultWeight = 1;
import api from '../api';

exports = module.exports = steemBroadcast => {
steemBroadcast.addAccountAuth = (
activeWif,
username,
authorizedUsername,
role = "posting",
role = 'posting',
weight = 1,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what if the account has a different threshold?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the account has a weight_threshold of 2 then the newly added key will not be able to broadcast alone if the weight is not set to 2. I can change the default weight to use the same value as weight_threshold.

cb
) => {
api.getAccountsAsync([username]).then(([userAccount]) => {
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;

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,
Expand All @@ -44,10 +45,13 @@ exports = module.exports = steemBroadcast => {
activeWif,
username,
authorizedUsername,
role = "posting",
role = 'posting',
cb
) => {
api.getAccountsAsync([username]).then(([userAccount]) => {
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,14 +61,98 @@ 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(
activeWif,
userAccount.name,
owner,
active,
posting,
userAccount.memo_key,
userAccount.json_metadata,
cb
);
});
};

steemBroadcast.addKeyAuth = (
activeWif,
username,
authorizedKey,
role = 'posting',
weight = 1,
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);
}

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(
activeWif,
userAccount.name,
owner,
active,
posting,
userAccount.memo_key,
userAccount.json_metadata,
cb
);
});
};

steemBroadcast.removeKeyAuth = (
activeWif,
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,
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