Skip to content

Commit

Permalink
setCredentials no longer requires address to be set for OHLEDGER
Browse files Browse the repository at this point in the history
  • Loading branch information
JakubNer committed May 9, 2019
1 parent 647385c commit e1c09ff
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 40 deletions.
51 changes: 24 additions & 27 deletions dist/ledgers.js
Original file line number Diff line number Diff line change
Expand Up @@ -723,63 +723,60 @@ var oh$ = function () {
switch (_context4.prev = _context4.next) {
case 0:
_context4.t0 = imparterTag;
_context4.next = _context4.t0 === OHLEDGER_IMPARTER_TAG ? 3 : 19;
_context4.next = _context4.t0 === OHLEDGER_IMPARTER_TAG ? 3 : 17;
break;

case 3:
if ('address' in credentials) {
if ('secret' in credentials) {
_context4.next = 5;
break;
}

throw new Error("'address' must be passed in");
throw new Error("'secret' must be passed in");

case 5:
if ('secret' in credentials) {
_context4.next = 7;
break;
if ('address' in credentials && credentials.address) {
data.OHLEDGER_IMPARTER_TAG.address = credentials.address.toLowerCase();
} else {
data.OHLEDGER_IMPARTER_TAG.address = eth_accounts.privateKeyToAccount(credentials.secret).address.toLowerCase();
}

throw new Error("'secret' must be passed in");

case 7:
data.OHLEDGER_IMPARTER_TAG.address = credentials.address;
data.OHLEDGER_IMPARTER_TAG.secret = credentials.secret;
_context4.prev = 9;
_context4.prev = 7;

if (eth_accounts.recover(eth_accounts.sign('test message', credentials.secret)).toLowerCase() == credentials.address.toLowerCase()) {
_context4.next = 12;
if (eth_accounts.recover(eth_accounts.sign('test message', data.OHLEDGER_IMPARTER_TAG.secret)).toLowerCase() == data.OHLEDGER_IMPARTER_TAG.address) {
_context4.next = 10;
break;
}

throw new Error("'secret' not valid for 'address");

case 12:
_context4.next = 17;
case 10:
_context4.next = 15;
break;

case 14:
_context4.prev = 14;
_context4.t1 = _context4["catch"](9);
case 12:
_context4.prev = 12;
_context4.t1 = _context4["catch"](7);
throw new Error("'secret' not valid for 'address");

case 17:
case 15:
fire('onCredentialsUpdate', {
imparterTag: OHLEDGER_IMPARTER_TAG,
address: credentials.address,
secret: credentials.secret
address: data.OHLEDGER_IMPARTER_TAG.address,
secret: data.OHLEDGER_IMPARTER_TAG.secret
});
return _context4.abrupt("return", true);

case 19:
case 17:
return _context4.abrupt("return", false);

case 20:
case 18:
case "end":
return _context4.stop();
}
}
}, _callee4, null, [[9, 14]]);
}, _callee4, null, [[7, 12]]);
}));
return _setCredentials.apply(this, arguments);
}
Expand Down Expand Up @@ -826,12 +823,12 @@ var oh$ = function () {

case 3:
res = eth_accounts.create();
data.OHLEDGER_IMPARTER_TAG.address = res.address;
data.OHLEDGER_IMPARTER_TAG.address = res.address.toLowerCase();
data.OHLEDGER_IMPARTER_TAG.secret = res.privateKey;
fire('onCredentialsUpdate', {
imparterTag: OHLEDGER_IMPARTER_TAG,
address: res.address,
secret: res.privateKey
address: data.OHLEDGER_IMPARTER_TAG.address,
secret: data.OHLEDGER_IMPARTER_TAG.secret
});
return _context5.abrupt("return", true);

Expand Down
2 changes: 1 addition & 1 deletion dist/ledgers.js.map

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions docs/ledgers.js-rendered-docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -1145,20 +1145,24 @@ <h3 class='fl m0' id='setcredentials'>
<tr>
<th>imparter tag</th>
<th>credentials object</th>
<th>comments</th>
</tr>
</thead>
<tbody>
<tr>
<td>eth-web3</td>
<td>N/A</td>
<td></td>
</tr>
<tr>
<td>ohledger</td>
<td><code>{address:..,secret:..}</code></td>
<td><code>address</code> is optional, if not set will be extracted from <code>secret</code></td>
</tr>
<tr>
<td>ohledger-web3</td>
<td>N/A</td>
<td></td>
</tr>
</tbody>
</table>
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ledgers.js",
"version": "2.0.1",
"version": "2.1.0",
"description": "JavaScript library for ledger-based authorizations :: abstracting different ledgers to be used in an application's authentication and authorization workflows.",
"engines": {
"node": "10.13.0"
Expand Down
25 changes: 14 additions & 11 deletions src/ledgers.js
Original file line number Diff line number Diff line change
Expand Up @@ -322,11 +322,11 @@ const oh$ = (function() {
*
* The credentials objects are as follows:
*
* | imparter tag | credentials object |
* | --- | --- |
* | eth-web3 | N/A |
* | ohledger | `{address:..,secret:..}` |
* | ohledger-web3 | N/A |
* | imparter tag | credentials object | comments |
* | --- | --- | --- |
* | eth-web3 | N/A | |
* | ohledger | `{address:..,secret:..}` | `address` is optional, if not set will be extracted from `secret` |
* | ohledger-web3 | N/A | |
*
* @returns {Promise} representing a 'true' if success else 'false'; also fires [onCredentialsUpdate](#eventoncredentialsupdate) event against `oh$`
*/
Expand Down Expand Up @@ -638,18 +638,21 @@ const oh$ = (function() {
async function setCredentials(imparterTag, credentials) {
switch (imparterTag) {
case OHLEDGER_IMPARTER_TAG:
if (!('address' in credentials)) throw new Error("'address' must be passed in");
if (!('secret' in credentials)) throw new Error("'secret' must be passed in");
data.OHLEDGER_IMPARTER_TAG.address = credentials.address;
if ('address' in credentials && credentials.address) {
data.OHLEDGER_IMPARTER_TAG.address = credentials.address.toLowerCase();
} else {
data.OHLEDGER_IMPARTER_TAG.address = eth_accounts.privateKeyToAccount(credentials.secret).address.toLowerCase();
}
data.OHLEDGER_IMPARTER_TAG.secret = credentials.secret;
try {
if (!(eth_accounts.recover(eth_accounts.sign('test message', credentials.secret)).toLowerCase() == credentials.address.toLowerCase())) {
if (!(eth_accounts.recover(eth_accounts.sign('test message', data.OHLEDGER_IMPARTER_TAG.secret)).toLowerCase() == data.OHLEDGER_IMPARTER_TAG.address)) {
throw new Error("'secret' not valid for 'address");
}
} catch (err) {
throw new Error("'secret' not valid for 'address");
}
fire('onCredentialsUpdate', { imparterTag: OHLEDGER_IMPARTER_TAG, address: credentials.address, secret: credentials.secret});
fire('onCredentialsUpdate', { imparterTag: OHLEDGER_IMPARTER_TAG, address: data.OHLEDGER_IMPARTER_TAG.address, secret: data.OHLEDGER_IMPARTER_TAG.secret});
return true;
default:
return false;
Expand All @@ -673,9 +676,9 @@ const oh$ = (function() {
switch (imparterTag) {
case OHLEDGER_IMPARTER_TAG:
let res = eth_accounts.create();
data.OHLEDGER_IMPARTER_TAG.address = res.address;
data.OHLEDGER_IMPARTER_TAG.address = res.address.toLowerCase();
data.OHLEDGER_IMPARTER_TAG.secret = res.privateKey;
fire('onCredentialsUpdate', { imparterTag: OHLEDGER_IMPARTER_TAG, address: res.address, secret: res.privateKey});
fire('onCredentialsUpdate', { imparterTag: OHLEDGER_IMPARTER_TAG, address: data.OHLEDGER_IMPARTER_TAG.address, secret: data.OHLEDGER_IMPARTER_TAG.secret});
return true;
default:
return false;
Expand Down
19 changes: 19 additions & 0 deletions test/js/smoke.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,25 @@ describe('ledgers.js smoke', function() {
});
});

it('can setCredentials() on ohledger from secret only', async () => {
await go(async () => {
var done = false;
oh$.addEventListener('onCredentialsUpdate', (event) => {
try {
if (done) return;
chai.assert(event.imparterTag == 'ohledger');
chai.assert(event.address == '0x968A1386f3ce3623a32908cC7Ec3dd6F72E74c36'.toLowerCase());
chai.assert(event.secret == '0x1b16186f7cf0aa09f561c7547d0e8ec88fb81fcf573cb8887a7d2aa9b9c284ff');
chai.assert(event.address == oh$.getCredentials('ohledger').address);
chai.assert(event.secret == oh$.getCredentials('ohledger').secret);
} finally {
done = true;
}
});
oh$.setCredentials('ohledger', { secret: '0x1b16186f7cf0aa09f561c7547d0e8ec88fb81fcf573cb8887a7d2aa9b9c284ff' });
});
});

it('can setNetwork() on ohledger and getOverhideRemunerationAPIUri', async () => {
await go(async () => {
var done = false;
Expand Down

0 comments on commit e1c09ff

Please sign in to comment.