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

SC-7 Account login key check improvement (#47) #51

Draft
wants to merge 6 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.

### [0.6.6-alpha](https://github.com/peerplays-network/peerplaysjs-lib/compare/v0.6.5-alpha...v0.6.6-alpha) (2020-01-29)


### Bug Fixes

* Provide support for prefix assignment in checkKeys without requiring to instantiate the entire chainStore and websocket connection. Document all function within the AccountLogin class ([#47](https://github.com/peerplays-network/peerplaysjs-lib/issues/45))
* Documentation added to the AccountLogin.js class ([125e6c8](https://github.com/peerplays-network/peerplaysjs-lib/commit/125e6c88235c467ed629ff63d7a931411058f7cf))

### [0.6.5](https://github.com/peerplays-network/peerplaysjs-lib/compare/v0.6.0...v0.6.5) (2019-12-24)


Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ The auths object should contain the auth arrays from the account object. An exam
```json
{
active: [
["GPH5Abm5dCdy3hJ1C5ckXkqUH2Me7dXqi9Y7yjn9ACaiSJ9h8r8mL", 1]
["PPY∂5Abm5dCdy3hJ1C5ckXkqUH2Me7dXqi9Y7yjn9ACaiSJ9h8r8mL", 1]
]
}
```
Expand Down
71 changes: 66 additions & 5 deletions build/peerplaysjs-lib.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion build/peerplaysjs-lib.min.js

Large diffs are not rendered by default.

65 changes: 63 additions & 2 deletions dist/chain/src/AccountLogin.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,49 @@ var AccountLogin = function () {
function AccountLogin() {
_classCallCheck(this, AccountLogin);

var state = { loggedIn: false, roles: ['active', 'owner', 'memo'] };
var state = { loggedIn: false, roles: ['owner', 'active', 'memo'] };
this.get = (0, _state.get)(state);
this.set = (0, _state.set)(state);

this.subs = {};
}

/**
* Subscribe to provided item.
*
* @param {*} cb
* @memberof AccountLogin
*/


AccountLogin.prototype.addSubscription = function addSubscription(cb) {
this.subs[cb] = cb;
};

/**
* Set the roles. Used for key generation.
*
* @param {Array} roles - ['owner', 'active', 'memo']
* @memberof AccountLogin
*/


AccountLogin.prototype.setRoles = function setRoles(roles) {
this.set('roles', roles);
};

/**
* Call this function to generate Peerplays user account keys.
*
* @param {String} accountName - The users' account name (username).
* @param {String} password - The users' password.
* @param {Array} roles - ['owner', 'active', 'memo']
* @param {String} prefix - Optional. The core token symbol (1.3.0 = 'PPY')
* @returns {Object} - Keys object: `{privKeys, pubKeys}`
* @memberof AccountLogin
*/


AccountLogin.prototype.generateKeys = function generateKeys(accountName, password, roles, prefix) {
if (!accountName || !password) {
throw new Error('Account name or password required');
Expand All @@ -64,12 +92,32 @@ var AccountLogin = function () {
return { privKeys: privKeys, pubKeys: pubKeys };
};

/**
* Accepts an account name {string}, password {string}, and an auths object. We loop over the
* provided auths whichcontains the keys associated with the account we are working with.
*
* We verify that the keys provided (which are pulled from the blockchain prior) match up with
* keys we generate with the provided account name and password.
*
* This function is dependant upon the roles array being specified in the correct order:
* 1. owner
* 2. active
* 3. memo
*
* @param {Object} {accountName, password, auths} - string, string, array
* @param {String} prefix - Optional. The core token symbol (1.3.0 = 'PPY')
* @returns {Boolean}
* @memberof AccountLogin
*/


AccountLogin.prototype.checkKeys = function checkKeys(_ref) {
var _this = this;

var accountName = _ref.accountName,
password = _ref.password,
auths = _ref.auths;
var prefix = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'PPY';

if (!accountName || !password || !auths) {
throw new Error('checkKeys: Missing inputs');
Expand All @@ -81,11 +129,12 @@ var AccountLogin = function () {
var _loop = function _loop(i, len) {
var role = roles[i];

var _generateKeys = _this.generateKeys(accountName, password, [role]),
var _generateKeys = _this.generateKeys(accountName, password, [role], prefix),
privKeys = _generateKeys.privKeys,
pubKeys = _generateKeys.pubKeys;

auths[role].forEach(function (roleKey) {
// Check if the active key matches
if (roleKey[0] === pubKeys[role]) {
hasKey = true;
_this.set(role, { priv: privKeys[role], pub: pubKeys[role] });
Expand All @@ -106,6 +155,18 @@ var AccountLogin = function () {
return hasKey;
};

/**
* Call this function and provide a valid transaction object to sign it with
* the users' Active key.
* Pre-requisite is that AccountLogin.js (`Login`) has had its roles set and
* the users' keys were generated with AccountLogin.js (`Login`)
*
* @param {Object} tr - Transaction object built via TransactionBuilder.js
* @returns {Object} tr - Transaction object that was passed in but signed.
* @memberof AccountLogin
*/


AccountLogin.prototype.signTransaction = function signTransaction(tr) {
var _this2 = this;

Expand Down
4 changes: 2 additions & 2 deletions dist/ecc/src/PublicKey.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ var PublicKey = function () {
};

/**
@arg {string} public_key - like GPHXyz...
@arg {string} address_prefix - like GPH
@arg {string} public_key - like PPYXyz...
@arg {string} address_prefix - like PPY
@throws {Error} if public key is invalid
@return PublicKey
*/
Expand Down
66 changes: 61 additions & 5 deletions lib/chain/src/AccountLogin.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,43 @@ let _keyCachePub = {};

class AccountLogin {
constructor() {
let state = {loggedIn: false, roles: ['active', 'owner', 'memo']};
let state = {loggedIn: false, roles: ['owner', 'active', 'memo']};
this.get = get(state);
this.set = set(state);

this.subs = {};
}

/**
* Subscribe to provided item.
*
* @param {*} cb
* @memberof AccountLogin
*/
addSubscription(cb) {
this.subs[cb] = cb;
}

/**
* Set the roles. Used for key generation.
*
* @param {Array} roles - ['owner', 'active', 'memo']
* @memberof AccountLogin
*/
setRoles(roles) {
this.set('roles', roles);
}

/**
* Call this function to generate Peerplays user account keys.
*
* @param {String} accountName - The users' account name (username).
* @param {String} password - The users' password.
* @param {Array} roles - ['owner', 'active', 'memo']
* @param {String} prefix - Optional. The core token symbol (1.3.0 = 'PPY')
* @returns {Object} - Keys object: `{privKeys, pubKeys}`
* @memberof AccountLogin
*/
generateKeys(accountName, password, roles, prefix) {
if (!accountName || !password) {
throw new Error('Account name or password required');
Expand All @@ -43,15 +65,34 @@ class AccountLogin {
_keyCachePriv[seed] = pkey;

privKeys[role] = pkey;
pubKeys[role] = _keyCachePub[seed] ? _keyCachePub[seed] : pkey.toPublicKey().toString(prefix);
pubKeys[role] = _keyCachePub[seed]
? _keyCachePub[seed]
: pkey.toPublicKey().toString(prefix);

_keyCachePub[seed] = pubKeys[role];
});

return {privKeys, pubKeys};
}

checkKeys({accountName, password, auths}) {
/**
* Accepts an account name {string}, password {string}, and an auths object. We loop over the
* provided auths whichcontains the keys associated with the account we are working with.
*
* We verify that the keys provided (which are pulled from the blockchain prior) match up with
* keys we generate with the provided account name and password.
*
* This function is dependant upon the roles array being specified in the correct order:
* 1. owner
* 2. active
* 3. memo
*
* @param {Object} {accountName, password, auths} - string, string, array
* @param {String} prefix - Optional. The core token symbol (1.3.0 = 'PPY')
* @returns {Boolean}
* @memberof AccountLogin
*/
checkKeys({accountName, password, auths}, prefix = 'PPY') {
if (!accountName || !password || !auths) {
throw new Error('checkKeys: Missing inputs');
}
Expand All @@ -61,8 +102,11 @@ class AccountLogin {

for (let i = 0, len = roles.length; i < len; i++) {
let role = roles[i];
let {privKeys, pubKeys} = this.generateKeys(accountName, password, [role]);
const {privKeys, pubKeys} = this.generateKeys(accountName, password, [
role
], prefix);
auths[role].forEach((roleKey) => {
// Check if the active key matches
if (roleKey[0] === pubKeys[role]) {
hasKey = true;
this.set(role, {priv: privKeys[role], pub: pubKeys[role]});
Expand All @@ -79,6 +123,16 @@ class AccountLogin {
return hasKey;
}

/**
* Call this function and provide a valid transaction object to sign it with
* the users' Active key.
* Pre-requisite is that AccountLogin.js (`Login`) has had its roles set and
* the users' keys were generated with AccountLogin.js (`Login`)
*
* @param {Object} tr - Transaction object built via TransactionBuilder.js
* @returns {Object} tr - Transaction object that was passed in but signed.
* @memberof AccountLogin
*/
signTransaction(tr) {
let hasKey = false;

Expand All @@ -93,7 +147,9 @@ class AccountLogin {
});

if (!hasKey) {
throw new Error('You do not have any private keys to sign this transaction');
throw new Error(
'You do not have any private keys to sign this transaction'
);
}
}
}
Expand Down
12 changes: 9 additions & 3 deletions lib/ecc/README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
# Elliptic curve cryptography functions (ECC)
# ECC

## Elliptic curve cryptography functions (ECC)

Private Key, Public Key, Cryptographic Signature, AES, Encryption / Decryption

```js
import {Address, Aes, PrivateKey, PublicKey, Signature} from "@graphene/ecc"
```

# Configure
## Configure

Update `./.npmrc` if you need to change something:

```bash
@graphene/ecc:default_address_prefix = GPH
```

# See Also
## See Also

* [Address](./src/address.js)
* [Aes](./src/aes.js)
* [PrivateKey](./src/PrivateKey.js)
Expand Down
4 changes: 2 additions & 2 deletions lib/ecc/src/PublicKey.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ class PublicKey {
}

/**
@arg {string} public_key - like GPHXyz...
@arg {string} address_prefix - like GPH
@arg {string} public_key - like PPYXyz...
@arg {string} address_prefix - like PPY
@throws {Error} if public key is invalid
@return PublicKey
*/
Expand Down
Loading