Skip to content

Commit

Permalink
use byteStream to avoid allocation
Browse files Browse the repository at this point in the history
  • Loading branch information
chcmedeiros committed Jun 19, 2024
1 parent 4fa63dd commit 09f11ba
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import BaseApp, {
processErrorResponse,
processResponse,
} from "@zondax/ledger-js";
import { ByteStream } from "@zondax/ledger-js/dist/byteStream";

const crypto = require("crypto");
const bech32 = require("bech32");
Expand Down Expand Up @@ -61,10 +62,12 @@ export default class CosmosApp extends BaseApp {
if (hrp == null || hrp.length < 3 || hrp.length > 83) {
throw new Error("Invalid HRP");
}
const buf = Buffer.alloc(1 + hrp.length);
buf.writeUInt8(hrp.length, 0);
buf.write(hrp, 1);
return buf;

const buf = new ByteStream();
buf.appendUint8(hrp.length);
buf.appendBytes(Buffer.from(hrp));

return buf.getCompleteBuffer();
}

async publicKey(path: string): Promise<ResponsePubkey> {
Expand Down Expand Up @@ -98,8 +101,8 @@ export default class CosmosApp extends BaseApp {
);

const response = processResponse(responseBuffer);
const compressed_pk = Buffer.from(response.readBytes(PKLEN));
const bech32_address = Buffer.from(response.readBytes(response.length())).toString();
const compressed_pk = response.readBytes(PKLEN);
const bech32_address = response.readBytes(response.length()).toString();

return {
compressed_pk,
Expand All @@ -125,8 +128,8 @@ export default class CosmosApp extends BaseApp {

const response = processResponse(responseBuffer);

const compressed_pk = Buffer.from(response.readBytes(PKLEN));
const bech32_address = Buffer.from(response.readBytes(response.length())).toString();
const compressed_pk = response.readBytes(PKLEN);
const bech32_address = response.readBytes(response.length()).toString();

return {
compressed_pk,
Expand Down

0 comments on commit 09f11ba

Please sign in to comment.