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

Abstract Buffer dependency #10

Merged
merged 8 commits into from
Jul 3, 2024
Merged
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
11 changes: 3 additions & 8 deletions biome.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
{
"$schema": "https://biomejs.dev/schemas/1.8.2/schema.json",
"files": {
"ignore": [
"**/coverage/*",
"**/dist/*",
"**/node_modules/*",
"**/_test-vectors/mockedGraphQLBoxResponses.json",
"**/package.json"
]
"ignore": ["**/coverage/*", "**/dist/*", "**/node_modules/*", "**/package.json"]
},
"organizeImports": {
"enabled": true
Expand All @@ -26,7 +20,8 @@
"rules": {
"recommended": true,
"style": {
"noParameterAssign": { "level": "off" }
"noParameterAssign": { "level": "off" },
"useNodejsImportProtocol": { "level": "off" }
},
"suspicious": {
"noConsoleLog": { "level": "error" },
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@
"@fleet-sdk/common": "^0.4.1",
"@fleet-sdk/core": "0.5.0",
"@fleet-sdk/crypto": "^0.5.0",
"base-x": "^5.0.0",
"bip32-path": "^0.4.2"
},
"devDependencies": {
Expand Down
8 changes: 0 additions & 8 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 0 additions & 20 deletions src/assertions.spec.ts

This file was deleted.

34 changes: 0 additions & 34 deletions src/assertions.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/device.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { RecordStore, openTransportReplayer } from "@ledgerhq/hw-transport-mocker";
import { describe, expect, it } from "vitest";
import { Device, DeviceError, RETURN_CODE } from "./device";
import { RecordStore, openTransportReplayer } from "@ledgerhq/hw-transport-mocker";
import { CLA } from "./erg";

describe("DeviceError construction", () => {
Expand Down
32 changes: 17 additions & 15 deletions src/device.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Buffer } from "buffer";
import type Transport from "@ledgerhq/hw-transport";
import { ByteWriter } from "./serialization/byteWriter";
import type { DeviceResponse } from "./types/internal";
import { serialize } from "./serialization/serialize";

export const enum COMMAND {
GET_APP_VERSION = 0x01,
Expand All @@ -12,8 +13,9 @@ export const enum COMMAND {
SIGN_TX = 0x21
}

const MAX_DATA_LENGTH = 255;
export const MAX_DATA_LENGTH = 255;
const MIN_RESPONSE_LENGTH = 2;
const MIN_APDU_LENGTH = 5;

export class Device {
#transport: Transport;
Expand All @@ -32,7 +34,7 @@ export class Device {
ins: COMMAND,
p1: number,
p2: number,
data: Buffer
data: Uint8Array
): Promise<DeviceResponse[]> {
const responses: DeviceResponse[] = [];
for (let i = 0; i < Math.ceil(data.length / MAX_DATA_LENGTH); i++) {
Expand All @@ -51,14 +53,14 @@ export class Device {
ins: COMMAND,
p1: number,
p2: number,
data: Buffer
data: Uint8Array
): Promise<DeviceResponse> {
if (data.length > MAX_DATA_LENGTH) {
throw new DeviceError(RETURN_CODE.TOO_MUCH_DATA);
}

const adpu = mountApdu(this.#cla, ins, p1, p2, data);
const response = await this.transport.exchange(adpu);
const apdu = mountApdu(this.#cla, ins, p1, p2, data);
const response = await this.transport.exchange(apdu);

if (response.length < MIN_RESPONSE_LENGTH) {
throw new DeviceError(RETURN_CODE.WRONG_RESPONSE_LENGTH);
Expand All @@ -76,16 +78,16 @@ function mountApdu(
ins: COMMAND,
p1: number,
p2: number,
data: Buffer
data: Uint8Array
): Buffer {
return Buffer.concat([
serialize.uint8(cla),
serialize.uint8(ins),
serialize.uint8(p1),
serialize.uint8(p2),
serialize.uint8(data.length),
data
]);
return new ByteWriter(MIN_APDU_LENGTH + data.length)
.write(cla)
.write(ins)
.write(p1)
.write(p2)
.write(data.length)
.writeBytes(data)
.toBuffer();
}

export class DeviceError extends Error {
Expand Down
18 changes: 9 additions & 9 deletions src/erg.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, it, expect, test, vi } from "vitest";
import { RecordStore, openTransportReplayer } from "@ledgerhq/hw-transport-mocker";
import { describe, expect, it, test, vi } from "vitest";
import { ErgoLedgerApp } from "./erg";
import { openTransportReplayer, RecordStore } from "@ledgerhq/hw-transport-mocker";

describe("construction", () => {
it("should construct app with transport", async () => {
Expand Down Expand Up @@ -220,8 +220,8 @@ describe("public key management without auth token", () => {
describe("transaction signing", () => {
test.each(txTestVectors)(
"should sign $name",
async ({ adpuQueue, authToken, proofs, tx }) => {
const transport = await openTransportReplayer(RecordStore.fromString(adpuQueue));
async ({ apduQueue, authToken, proofs, tx }) => {
const transport = await openTransportReplayer(RecordStore.fromString(apduQueue));

const app = new ErgoLedgerApp(transport, authToken).useAuthToken(!!authToken);

Expand All @@ -231,8 +231,8 @@ describe("transaction signing", () => {
);

it("Should throw with empty inputs", async () => {
const { adpuQueue, tx } = txTestVectors[0];
const transport = await openTransportReplayer(RecordStore.fromString(adpuQueue));
const { apduQueue, tx } = txTestVectors[0];
const transport = await openTransportReplayer(RecordStore.fromString(apduQueue));

const app = new ErgoLedgerApp(transport);
await expect(() => app.signTx({ ...tx, inputs: [] })).rejects.toThrow(
Expand Down Expand Up @@ -301,7 +301,7 @@ const txTestVectors = [
{
name: "simple erg-only p2p transaction",
authToken: 0,
adpuQueue: `
apduQueue: `
=> e0200101378e73d69748e76867f9e71351481137bc6d5e671979d050f61eabfdccee28a513000100000000067f2af0000000240013d0a10000000001
<= 469000

Expand Down Expand Up @@ -428,7 +428,7 @@ const txTestVectors = [
{
name: "simple p2p transaction with tokens",
authToken: Number("0x68771637"),
adpuQueue: `
apduQueue: `
=> e02001023b27c4d6a5a5e883282a5a1246975a1b42df78aa270638c8d843d63c14fae7a31f000b0000000000009ee8000000240013d08c010000000168771637
<= aa9000

Expand Down Expand Up @@ -615,7 +615,7 @@ const txTestVectors = [
{
name: "transaction with input extension and data inputs",
authToken: 0,
adpuQueue: `
apduQueue: `
=> e0200101375f083bdf25ef9b915205e569c3c0623c5b7ae743a6d26112704cfd8ae1261555000100000001c235c8c0000000c200128d98010000004a
<= f09000

Expand Down
Loading
Loading