-
Notifications
You must be signed in to change notification settings - Fork 225
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
Changes from 8 commits
c112fe8
d81a9a3
86147a6
8e483e0
aa137e3
78b7f8f
a60ee71
1812b09
bb9adf6
954cb85
e30f535
2c84442
9908052
5332599
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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, | ||
(err, result) => { | ||
console.log(err, result); | ||
} | ||
); |
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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what if the account has a different threshold? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the account has a |
||
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, | ||
|
@@ -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++) { | ||
|
@@ -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, | ||
|
There was a problem hiding this comment.
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