-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from arobsn/refactor
Overall refactor
- Loading branch information
Showing
34 changed files
with
768 additions
and
765 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
); | ||
}); | ||
}); |
Oops, something went wrong.