Skip to content

Commit

Permalink
feat(Channel): Make getOutboundRAMFRecipient() concrete (#505)
Browse files Browse the repository at this point in the history
  • Loading branch information
gnarea authored Aug 22, 2022
1 parent bef264d commit dcf413b
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 25 deletions.
20 changes: 15 additions & 5 deletions src/lib/nodes/channels/Channel.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,18 @@ describe('wrapMessagePayload', () => {
});
});

class StubChannel extends Channel {
async getOutboundRAMFRecipient(): Promise<Recipient> {
throw new Error('not implemented');
}
}
describe('getOutboundRAMFRecipient', () => {
test('Id should be output', () => {
const channel = new StubChannel(
nodePrivateKey,
nodeCertificate,
peerId,
peerPublicKey,
KEY_STORES,
);

expect(channel.getOutboundRAMFRecipient()).toEqual<Recipient>({ id: peerId });
});
});

class StubChannel extends Channel {}
4 changes: 3 additions & 1 deletion src/lib/nodes/channels/Channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ export abstract class Channel {
return envelopedData.serialize();
}

public abstract getOutboundRAMFRecipient(): Promise<Recipient>;
public getOutboundRAMFRecipient(): Recipient {
return { id: this.peerId };
}

protected async getNodeId(): Promise<string> {
return getIdFromIdentityKey(this.nodePrivateKey);
Expand Down
9 changes: 1 addition & 8 deletions src/lib/nodes/channels/GatewayChannel.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import Cargo from '../../messages/Cargo';
import Parcel from '../../messages/Parcel';
import CargoMessageSet from '../../messages/payloads/CargoMessageSet';
import ServiceMessage from '../../messages/payloads/ServiceMessage';
import { Recipient } from '../../messages/Recipient';
import { issueGatewayCertificate } from '../../pki/issuance';
import { RAMF_MAX_TTL } from '../../ramf/serialization';
import { SessionKey } from '../../SessionKey';
Expand Down Expand Up @@ -92,7 +91,7 @@ describe('generateCargoes', () => {
);

const cargo = await Cargo.deserialize(bufferToArray(cargoesSerialized[0]));
expect(cargo.recipient.id).toEqual(StubGatewayChannel.OUTBOUND_RAMF_RECIPIENT_ID);
expect(cargo.recipient.id).toEqual(peerId);
});

test('Payload should be encrypted with session key', async () => {
Expand Down Expand Up @@ -326,13 +325,7 @@ async function generateDummyParcel(
}

class StubGatewayChannel extends GatewayChannel {
public static readonly OUTBOUND_RAMF_RECIPIENT_ID = '0deadbeef';

constructor(cryptoOptions: Partial<NodeCryptoOptions> = {}) {
super(nodePrivateKey, nodeCertificate, peerId, peerPublicKey, KEY_STORES, cryptoOptions);
}

async getOutboundRAMFRecipient(): Promise<Recipient> {
return { id: StubGatewayChannel.OUTBOUND_RAMF_RECIPIENT_ID };
}
}
2 changes: 1 addition & 1 deletion src/lib/nodes/channels/GatewayChannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export abstract class GatewayChannel extends Channel {
public async *generateCargoes(messages: CargoMessageStream): AsyncIterable<Buffer> {
const messagesAsArrayBuffers = convertBufferMessagesToArrayBuffer(messages);
const cargoMessageSets = CargoMessageSet.batchMessagesSerialized(messagesAsArrayBuffers);
const recipient = await this.getOutboundRAMFRecipient();
const recipient = this.getOutboundRAMFRecipient();
for await (const { messageSerialized, expiryDate } of cargoMessageSets) {
const creationDate = getCargoCreationTime();
const ttl = getSecondsBetweenDates(creationDate, expiryDate);
Expand Down
5 changes: 0 additions & 5 deletions src/lib/nodes/channels/PrivateGatewayChannel.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
} from '../../crypto_wrappers/keys';
import Certificate from '../../crypto_wrappers/x509/Certificate';
import { MockKeyStoreSet } from '../../keyStores/testMocks';
import { Recipient } from '../../messages/Recipient';
import { CertificationPath } from '../../pki/CertificationPath';
import { issueGatewayCertificate } from '../../pki/issuance';
import { NodeCryptoOptions } from '../NodeCryptoOptions';
Expand Down Expand Up @@ -198,8 +197,4 @@ class StubPrivateGatewayChannel extends PrivateGatewayChannel {
cryptoOptions,
);
}

async getOutboundRAMFRecipient(): Promise<Recipient> {
throw new Error('not implemented');
}
}
9 changes: 6 additions & 3 deletions src/lib/nodes/channels/PrivateInternetGatewayChannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,11 @@ export class PrivateInternetGatewayChannel extends PrivateGatewayChannel {
);
}

async getOutboundRAMFRecipient(): Promise<Recipient> {
return { id: this.peerId, internetAddress: this.internetGatewayInternetAddress };
override getOutboundRAMFRecipient(): Recipient {
return {
...super.getOutboundRAMFRecipient(),
internetAddress: this.internetGatewayInternetAddress,
};
}

//region Private endpoint registration
Expand Down Expand Up @@ -116,7 +119,7 @@ export class PrivateInternetGatewayChannel extends PrivateGatewayChannel {
const ccr = new CargoCollectionRequest(cargoDeliveryAuthorization);
const ccaPayload = await this.wrapMessagePayload(ccr);
const cca = new CargoCollectionAuthorization(
await this.getOutboundRAMFRecipient(),
this.getOutboundRAMFRecipient(),
this.nodeDeliveryAuth,
Buffer.from(ccaPayload),
{ creationDate: startDate, ttl: differenceInSeconds(endDate, startDate) },
Expand Down
4 changes: 2 additions & 2 deletions src/lib/nodes/channels/PrivatePublicGatewayChannel.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ beforeEach(() => {
);
});

test('getOutboundRAMFAddress should return Internet address of Internet gateway', async () => {
await expect(channel.getOutboundRAMFRecipient()).resolves.toEqual<Recipient>({
test('getOutboundRAMFRecipient should return Internet address of Internet gateway', async () => {
expect(channel.getOutboundRAMFRecipient()).toEqual<Recipient>({
id: internetGatewayId,
internetAddress: INTERNET_GATEWAY_INTERNET_ADDRESS,
});
Expand Down

0 comments on commit dcf413b

Please sign in to comment.