Skip to content

Commit

Permalink
message: pass message timestamp to getLeafKey
Browse files Browse the repository at this point in the history
  • Loading branch information
indutny committed Feb 1, 2020
1 parent f3ead1c commit d4f6f62
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 5 deletions.
8 changes: 4 additions & 4 deletions lib/protocol/identity.js
Expand Up @@ -105,12 +105,12 @@ export default class Identity {
});
}

addChain(channel, chain) {
if (!chain.verify(channel)) {
addChain(channel, chain, timestamp = now()) {
if (!chain.verify(channel, timestamp)) {
throw new Error('Invalid chain');
}

const leafKey = chain.getLeafKey(channel);
const leafKey = chain.getLeafKey(channel, timestamp);
if (!leafKey.equals(this.publicKey)) {
throw new Error('Invalid leaf key in the chain');
}
Expand Down Expand Up @@ -172,7 +172,7 @@ export default class Identity {
parents,
} = options;

const chain = this.getChain(channel, options.timestamp);
const chain = this.getChain(channel, timestamp);
if (!chain) {
throw new Error(
`No chain available for a channel ${channel.id.toString('hex')}`);
Expand Down
2 changes: 1 addition & 1 deletion lib/protocol/message.js
Expand Up @@ -75,7 +75,7 @@ export default class Message {

verify(channel) {
const sodium = this.sodium;
const leafKey = this.chain.getLeafKey(channel);
const leafKey = this.chain.getLeafKey(channel, this.timestamp);

return sodium.crypto_sign_verify_detached(
this.signature,
Expand Down
41 changes: 41 additions & 0 deletions test/message-test.js
Expand Up @@ -3,6 +3,7 @@ import * as assert from 'assert';
import * as sodium from 'sodium-universal';

import { Chain, Channel, Identity, Message } from '../';
import { now } from '../lib/utils';

describe('Message', () => {
let id = null;
Expand Down Expand Up @@ -53,6 +54,46 @@ describe('Message', () => {
second.publicKey.toString('hex'));
});

it('should be verified after expiration of the link', () => {
// Expired a day ago
const validTo = now() - 24 * 3600;

// Posted an hour before expiration
const postTime = validTo - 3600;

const second = new Identity('second', { sodium });
const chain = new Chain([
id.issueLink(channel, {
validFrom: 0,
validTo,

trusteePubKey: second.publicKey,
trusteeDisplayName: 'second',
}),
]);

second.addChain(channel, chain, postTime);

const content = second.signMessageBody(
Message.json('okay'),
channel,
{
height: 0,
parents: [],
timestamp: postTime,
});

const message = new Message({
...content,
sodium,
});
assert.ok(message.verify(channel));

assert.strictEqual(
message.chain.getLeafKey(channel, postTime).toString('hex'),
second.publicKey.toString('hex'));
});

it('should be serialized/deserialized', () => {
const content = id.signMessageBody(
Message.json('okay'),
Expand Down

0 comments on commit d4f6f62

Please sign in to comment.