Skip to content

Commit

Permalink
✨ (core) [DSDK-251]: Add new Apdu Receiver Service (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
aussedatlo authored Mar 29, 2024
2 parents 4b89316 + 31d7aca commit 9d5b825
Show file tree
Hide file tree
Showing 12 changed files with 655 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const APDU_RESPONSE_STATUS_CODE_LENGTH = 2;
8 changes: 4 additions & 4 deletions packages/core/src/internal/device-session/data/FramerConst.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export const HEAD_TAG = 0x05;
export const HEAD_TAG_SIZE = 1;
export const CHANNEL_SIZE = 2;
export const INDEX_SIZE = 2;
export const APDU_DATA_SIZE = 2;
export const HEAD_TAG_LENGTH = 1;
export const CHANNEL_LENGTH = 2;
export const INDEX_LENGTH = 2;
export const APDU_DATA_LENGTH_LENGTH = 2;
22 changes: 22 additions & 0 deletions packages/core/src/internal/device-session/model/ApduResponse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
type ApduResponseConstructorArgs = {
statusCode: Uint8Array;
data: Uint8Array;
};

export class ApduResponse {
protected _statusCode: Uint8Array;
protected _data: Uint8Array;

constructor({ statusCode, data }: ApduResponseConstructorArgs) {
this._statusCode = statusCode;
this._data = data;
}

public getStatusCode() {
return this._statusCode;
}

public getData() {
return this._data;
}
}
9 changes: 9 additions & 0 deletions packages/core/src/internal/device-session/model/Errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ export class FramerApduError implements SdkError {
}
}

export class ReceiverApduError implements SdkError {
readonly _tag = "ReceiverApduError";
originalError: Error;

constructor(message?: string) {
this.originalError = new Error(message ?? "Unable to parse apdu");
}
}

export class DeviceSessionNotFound implements SdkError {
readonly _tag = "DeviceSessionNotFound";
originalError?: Error;
Expand Down
11 changes: 11 additions & 0 deletions packages/core/src/internal/device-session/model/Frame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ type FrameConstructorArgs = {
export class Frame {
protected _header: FrameHeader;
protected _data: Uint8Array;

constructor({ header, data }: FrameConstructorArgs) {
this._header = header;
this._data = data;
}

toString(): string {
return JSON.stringify(
{
Expand All @@ -22,6 +24,7 @@ export class Frame {
2,
);
}

getRawData(): Uint8Array {
const headerRaw = this._header.getRawData();
const raw = new Uint8Array(headerRaw.length + this._data.length);
Expand All @@ -30,4 +33,12 @@ export class Frame {
raw.set(this._data, headerRaw.length);
return raw;
}

getHeader(): FrameHeader {
return this._header;
}

getData(): Uint8Array {
return this._data;
}
}
15 changes: 10 additions & 5 deletions packages/core/src/internal/device-session/model/FrameHeader.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Maybe } from "purify-ts";

import { FramerUtils } from "@internal/device-session/utils/FramerUtils";

type FrameHeaderConstructorArgs = {
uuid: string;
channel: Maybe<Uint8Array>;
Expand All @@ -15,7 +17,7 @@ export class FrameHeader {
protected _headTag: Uint8Array;
protected _index: Uint8Array;
protected _length: number;
protected _dataSize: Maybe<Uint8Array>;
protected _dataLength: Maybe<Uint8Array>;
constructor({
uuid,
dataSize,
Expand All @@ -25,14 +27,17 @@ export class FrameHeader {
channel,
}: FrameHeaderConstructorArgs) {
this._uuid = uuid;
this._dataSize = dataSize;
this._dataLength = dataSize;
this._index = index;
this._headTag = headTag;
this._length = length;
this._channel = channel;
}
getDataLength(): Maybe<number> {
return this._dataLength.map((value) => FramerUtils.bytesToNumber(value));
}
setDataSize(dataSize: Maybe<Uint8Array>): FrameHeader {
this._dataSize = dataSize;
this._dataLength = dataSize;
return this;
}
getLength(): number {
Expand All @@ -41,7 +46,7 @@ export class FrameHeader {
toString(): string {
return JSON.stringify({
uuid: this._uuid.toString(),
dataSize: this._dataSize.extract()?.toString(),
dataSize: this._dataLength.extract()?.toString(),
index: this._index.toString(),
headTag: this._headTag.toString(),
length: this._length.toString(),
Expand All @@ -56,7 +61,7 @@ export class FrameHeader {
}),
...this._headTag,
...this._index,
...this._dataSize.caseOf({
...this._dataLength.caseOf({
Just: (dataSize) => [...dataSize],
Nothing: () => [],
}),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Either, Maybe } from "purify-ts";

import { ApduResponse } from "@internal/device-session/model/ApduResponse";
import { ReceiverApduError } from "@internal/device-session/model/Errors";

export interface ApduReceiverService {
handleFrame(apdu: Uint8Array): Either<ReceiverApduError, Maybe<ApduResponse>>;
}
Loading

0 comments on commit 9d5b825

Please sign in to comment.