Skip to content

Commit

Permalink
add blake2b function
Browse files Browse the repository at this point in the history
  • Loading branch information
arobsn committed Dec 25, 2024
1 parent 6d96b6b commit 9a244b5
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .changeset/few-chefs-sleep.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@fleet-sdk/crypto": patch
---

Add `blake2b` function
21 changes: 19 additions & 2 deletions packages/crypto/src/hashes.spec.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,31 @@
import { describe, expect, it } from "vitest";
import { hex, utf8 } from "./coders";
import { blake2b256, sha256 } from "./hashes";
import { blake2b, blake2b256, sha256 } from "./hashes";

describe("Hashes smoke tests", () => {
it("Should hash message using BLAKE2b256", () => {
expect(blake2b256(utf8.decode("blake2b256"))).to.be.deep.equal(
const msg = utf8.decode("blake2b256");
expect(blake2b256(msg)).to.be.deep.equal(
hex.decode("eb95e6932cedac15db722fcdb0cfd21437f94690339a716251fad2f89842ea8b")
);
});

it("Should hash message using BLAKE2b with parameters", () => {
const xpk =
"0488b21e000000000000000000b345a673afdeb85091c35d02083035f6e0ca284b1846223b23b566c4070a0cec02a3ad1969b60e85426791b75eccf038e6105c3afab8167af7eb6b73e709b81882";

expect(
hex.encode(
blake2b(utf8.decode(xpk), {
dkLen: 64,
personalization: "wallets checksum"
})
)
).to.be.equal(
"5d33031ea3bbba9d3332559b1dafd8612683092f535273a4c15ffa103ffa3fc11f7b6992f5a034b3c8dd30f6f103b24e500c44ba4cff2e5c7f6e3e2eb124cd32"
);
});

it("Should have the same result regardless input format", () => {
const byte = Uint8Array.from([0xde, 0xad, 0xbe, 0xef]);
const hex = "deadbeef";
Expand Down
20 changes: 17 additions & 3 deletions packages/crypto/src/hashes.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,28 @@
import { blake2b } from "@noble/hashes/blake2b";
import { blake2b as _blake2b } from "@noble/hashes/blake2b";
import { sha256 as _sha256 } from "@noble/hashes/sha256";
import { hex } from "./coders";
import type { ByteInput } from "./types";

export type Blake2b256Options = {
key?: ByteInput;
salt?: ByteInput;
personalization?: ByteInput;
};

export type Blake2bOptions = Blake2b256Options & {
dkLen?: number;
};

export function ensureBytes(input: ByteInput): Uint8Array {
return typeof input === "string" ? hex.decode(input) : input;
}

export function blake2b256(message: ByteInput): Uint8Array {
return blake2b(ensureBytes(message), { dkLen: 32 });
export function blake2b(message: ByteInput, options?: Blake2bOptions): Uint8Array {
return _blake2b(ensureBytes(message), options);
}

export function blake2b256(message: ByteInput, options?: Blake2b256Options): Uint8Array {
return blake2b(ensureBytes(message), { dkLen: 32, ...options });
}

export function sha256(message: ByteInput): Uint8Array {
Expand Down
17 changes: 8 additions & 9 deletions packages/serializer/src/serializers/transactionSerializer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {
type Amount,
type BoxCandidate,
type ContextExtension,
type DataInput,
isDefined,
type UnsignedInput
Expand Down Expand Up @@ -46,24 +45,24 @@ function writeInput(writer: SigmaByteWriter, input: UnsignedInput): void {
writeExtension(writer, input.extension);
}

function writeExtension(writer: SigmaByteWriter, extension: ContextExtension): void {
function writeExtension(
writer: SigmaByteWriter,
extension: Record<string, string | undefined>
): void {
const keys = Object.keys(extension);
let length = 0;

for (const key of keys) {
const ext = extension[key as unknown as keyof ContextExtension];
if (isDefined(ext)) {
length++;
}
if (isDefined(extension[key])) length++;
}

writer.writeVLQ(length);
if (length === 0) return;

for (const key of keys) {
const ext = extension[key as unknown as keyof ContextExtension];
if (isDefined(ext)) {
writer.writeVLQ(Number(key)).writeHex(ext);
const val = extension[key];
if (isDefined(val)) {
writer.writeVLQ(Number(key)).writeHex(val);
}
}
}
Expand Down

0 comments on commit 9a244b5

Please sign in to comment.