Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions builder/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"build:hooks": "node dist/index.js -t build:hooks",
"build:factories": "node dist/index.js -t build:factories",
"build:types": "node dist/index.js -t build:types",
"build:crypto": "node dist/index.js -t build:crypto",
"build:utils": "node dist/index.js -t build:utils",
"clear": "rm -rf node_modules dist tsconfig.tsbuildinfo"
},
Expand Down
54 changes: 54 additions & 0 deletions builder/src/builders/crypto/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/* @license Copyright 2024 w3ux authors & contributors
SPDX-License-Identifier: GPL-3.0-only */

import { PACKAGE_OUTPUT } from "config";
import { prebuild } from "builders/common/prebuild";
import {
gePackageDirectory,
generatePackageJson,
removePackageOutput,
} from "builders/util";
import { promisify } from "util";
import { exec } from "child_process";

const execPromisify = promisify(exec);

export const build = async () => {
const folder = "crypto";
const libDirectory = gePackageDirectory(folder);

try {
// Prebuild integrity checks.
if (!(await prebuild(folder))) {
throw `Prebuild failed.`;
}

// Call tsup command to build dist folder.
try {
await execPromisify(`cd ../library/${folder} && yarn build`);
} catch (e) {
throw `Failed to generate dist. ${e}`;
}

// Generate package.json.
if (
!(await generatePackageJson(
libDirectory,
`${libDirectory}/${PACKAGE_OUTPUT}`,
"tsup"
))
) {
throw `Failed to generate package.json file.`;
}

console.log(`✅ Package successfully built.`);
} catch (err) {
// Handle on error.
console.error(`❌ Error occurred while building the package.`, err);

// Remove package output directory if it exists.
if (!(await removePackageOutput(libDirectory, false))) {
console.error(`❌ Failed to remove package output directory.`);
}
}
};
14 changes: 1 addition & 13 deletions builder/src/builders/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,7 @@ export const build = async () => {
!(await generatePackageJson(
libDirectory,
`${libDirectory}/${PACKAGE_OUTPUT}`,
"tsup",
// Defining the additional entry points as `polkadot/util` and `polkadot/util-crypto`
// packages are re-exported.
{
"./util": {
import: "./util/index.js",
require: "./util/index.cjs",
},
"./util-crypto": {
import: "./util-crypto/index.js",
require: "./util-crypto/index.cjs",
},
}
"tsup"
))
) {
throw `Failed to generate package.json file.`;
Expand Down
5 changes: 5 additions & 0 deletions builder/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import minimist from "minimist";
import * as extensionAssets from "./builders/extension-assets";
import * as validatorAssets from "./builders/validator-assets";
import * as types from "./builders/types";
import * as crypto from "./builders/crypto";
import * as utils from "./builders/utils";
import * as directory from "./builders/directory";
import * as reactOdometer from "./builders/react-odometer";
Expand Down Expand Up @@ -58,6 +59,10 @@ switch (task) {
utils.build();
break;

case "build:crypto":
crypto.build();
break;

default:
console.log("❌ No task provided.");
}
11 changes: 11 additions & 0 deletions library/crypto/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "@w3ux/crypto-source",
"license": "GPL-3.0-only",
"version": "1.0.0-beta.0",
"type": "module",
"scripts": {
"clear": "rm -rf node_modules dist tsconfig.tsbuildinfo",
"test": "vitest run",
"build": "tsup src/**/* --format esm,cjs --target es2022 --dts --no-splitting"
}
}
8 changes: 8 additions & 0 deletions library/crypto/packageInfo.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
directory:
- name: Crypto
description: Cryptographic utilities for Dapps.

npm:
title: Crypto
contents:
- item: Cryptographic utilities for Dapps.
28 changes: 28 additions & 0 deletions library/crypto/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* @license Copyright 2024 w3ux authors & contributors
SPDX-License-Identifier: GPL-3.0-only */

import { blake2b } from "blakejs";

/**
* Creates a BLAKE2b hash of the input data and returns it as a Uint8Array.
*
* @param data - The input data to hash, as a string, Uint8Array, or Buffer.
* @param bitLength - The bit length of the hash output (default 256).
* @returns The BLAKE2b hash output as a Uint8Array.
*/
export function blake2AsU8a(
data: Uint8Array | string | Buffer,
bitLength = 256
): Uint8Array {
// Convert input to Uint8Array if it's a string
const input =
typeof data === "string" ? new TextEncoder().encode(data) : data;

// Calculate byte length from bit length (256 bits => 32 bytes)
const byteLength = bitLength / 8;

// Generate the hash using blake2b with the specified output length
const hash = blake2b(input, undefined, byteLength);

return new Uint8Array(hash);
}
19 changes: 19 additions & 0 deletions library/crypto/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"baseUrl": ".",
"rootDir": ".",
"declarationDir": ".",
"module": "ESNext",
"moduleResolution": "Node",
"target": "es2020",
"lib": ["DOM", "DOM.Iterable", "ESNext"],
"sourceMap": false,
"jsx": "react-jsx",
"allowSyntheticDefaultImports": true,
"emitDeclarationOnly": true,
"resolveJsonModule": true,
"esModuleInterop": true,
},
"include": ["./**/*"],
}
13 changes: 13 additions & 0 deletions library/crypto/tsup.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/* @license Copyright 2024 w3ux authors & contributors
SPDX-License-Identifier: GPL-3.0-only */

import { defineConfig } from "tsup";

export default defineConfig({
entry: ["src/index.ts"],
splitting: false,
sourcemap: false,
clean: true,
dts: true,
format: "esm",
});
2 changes: 1 addition & 1 deletion library/factories/packageInfo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ directory:
description: A collection of general purpose TypeScript factories.

npm:
title: Hooks
title: Factories
contents:
- item: A collection of general purpose TypeScript factories.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
},
"workspaces": [
"builder",
"library/crypto",
"library/utils",
"library/types",
"library/factories",
Expand Down
1 change: 1 addition & 0 deletions release-please-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"library/react-polkicon": {},
"library/types": {},
"library/utils": {},
"library/crypto": {},
"library/validator-assets": {}
}
}