Skip to content

Commit

Permalink
Merge pull request #9 from arobsn/refactor
Browse files Browse the repository at this point in the history
Overall refactor
  • Loading branch information
arobsn authored Jul 2, 2024
2 parents 66896e7 + 83043b3 commit def24ca
Show file tree
Hide file tree
Showing 34 changed files with 768 additions and 765 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/build-and-test-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,6 @@ jobs:
- run: pnpm install --frozen-lockfile
- run: pnpm run build
- run: pnpm test:unit
- run: pnpm test:format
- run: pnpm test:lint
- run: pnpm cov:check
3 changes: 2 additions & 1 deletion biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"enabled": true
},
"formatter": {
"indentStyle": "space"
"indentStyle": "space",
"lineWidth": 90
},
"javascript": {
"formatter": {
Expand Down
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,20 +57,21 @@
"access": "public"
},
"dependencies": {
"@fleet-sdk/core": "0.1.0-alpha.12",
"base-x": "^4.0.0",
"@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": {
"@biomejs/biome": "^1.8.3",
"@ledgerhq/hw-transport": "^6.31.0",
"@ledgerhq/hw-transport-mocker": "^6.29.0",
"@types/node": "^20.14.9",
"@types/typescript": "^2.0.0",
"@vitest/coverage-v8": "^1.6.0",
"open-cli": "^8.0.0",
"tsup": "^8.1.0",
"typescript": "^5.5.2",
"typescript": "^5.5.3",
"vitest": "^1.6.0"
}
}
81 changes: 51 additions & 30 deletions pnpm-lock.yaml

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

62 changes: 0 additions & 62 deletions rollup.config.ts

This file was deleted.

20 changes: 20 additions & 0 deletions src/assertions.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { describe, it, expect } from "vitest";
import { isErgoPath, isUint64String } from "./assertions";
import { pathToArray } from "./serialization/serialize";

describe("assertions", () => {
it("Ergo path", () => {
expect(isErgoPath(pathToArray("m/44'/429'"))).to.be.true;
expect(isErgoPath(pathToArray("m/44'/2'"))).to.be.false;
expect(isErgoPath(pathToArray("m/44'"))).to.be.false;
});

it("UInt64", () => {
expect(isUint64String("0")).to.be.true;
expect(isUint64String("18446744073709551615")).to.be.true;

expect(isUint64String("18446744073709551616")).to.be.false;
expect(isUint64String("1.2")).to.be.false;
expect(isUint64String("11a")).to.be.false;
});
});
34 changes: 34 additions & 0 deletions src/assertions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import bip32Path from "bip32-path";

const MIN_UINT_64 = 0n;
const MAX_UINT_64 = 18446744073709551615n;
const MIN_UINT_VALUE = 0;
const MAX_UINT32_VALUE = 4294967295;
const MAX_UINT16_VALUE = 65535;
const MAX_UNIT8_VALUE = 255;

const [ERGO_PURPOSE, ERGO_COIN_TYPE] = bip32Path.fromString("m/44'/429'").toPathArray();

export function isErgoPath(path: number[]): boolean {
if (path.length < 2) return false;
const [pathPurpose, pathCoinType] = path;
return pathPurpose === ERGO_PURPOSE && pathCoinType === ERGO_COIN_TYPE;
}

export function isUint32(data: number): boolean {
return Number.isInteger(data) && data >= MIN_UINT_VALUE && data <= MAX_UINT32_VALUE;
}

export function isUint16(data: number): boolean {
return Number.isInteger(data) && data >= MIN_UINT_VALUE && data <= MAX_UINT16_VALUE;
}

export function isUint8(data: number): boolean {
return Number.isInteger(data) && data >= MIN_UINT_VALUE && data <= MAX_UNIT8_VALUE;
}

export function isUint64String(value: string): boolean {
if (!/^[0-9]*$/.test(value)) return false;
const parsed = BigInt(value);
return parsed >= MIN_UINT_64 && parsed <= MAX_UINT_64;
}
51 changes: 51 additions & 0 deletions src/device.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
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", () => {
it("should create a new DeviceError instance from a know RETURN_CODE", () => {
const error = new DeviceError(RETURN_CODE.TOO_MUCH_DATA);
expect(error.code).toBe(RETURN_CODE.TOO_MUCH_DATA);
expect(error.message).toBe("Too much data");
expect(error.name).to.be.equal("DeviceError");
});

it("should create a new DeviceError instance from a unknown RETURN_CODE", () => {
const error = new DeviceError(0 as RETURN_CODE);
expect(error.code).toBe(0);
expect(error.message).toBe("Unknown error");
});
});

describe("Negative tests", () => {
it("Should throw an error if the response length is less than the minimum", async () => {
const transport = await openTransportReplayer(
RecordStore.fromString(`
=> e00102030104
<= 69
`)
);

const device = new Device(transport, CLA);

await expect(() => device.send(0x1, 0x2, 0x3, Buffer.from([0x4]))).rejects.toThrow(
"Wrong response length"
);
});

it("Should throw if data is too long", async () => {
const transport = await openTransportReplayer(
RecordStore.fromString(`
=> e00102030104
<= 9000
`)
);

const device = new Device(transport, CLA);

await expect(() => device.send(0x1, 0x2, 0x3, Buffer.alloc(260))).rejects.toThrow(
"Too much data"
);
});
});
Loading

0 comments on commit def24ca

Please sign in to comment.