Skip to content

Commit

Permalink
Merge pull request #10 from arobsn/remove-buffer
Browse files Browse the repository at this point in the history
Abstract Buffer dependency
  • Loading branch information
arobsn authored Jul 3, 2024
2 parents def24ca + 7cec338 commit 651d10e
Show file tree
Hide file tree
Showing 26 changed files with 342 additions and 468 deletions.
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

0 comments on commit 651d10e

Please sign in to comment.