Skip to content

Commit

Permalink
add crypto package
Browse files Browse the repository at this point in the history
  • Loading branch information
arobsn committed Jul 30, 2023
1 parent 7b94d72 commit 8a13a29
Show file tree
Hide file tree
Showing 15 changed files with 172 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .changeset/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"changelog": "@changesets/cli/changelog",
"commit": false,
"fixed": [],
"linked": [["*/core", "*/common", "*/wallet", "*/mock-chain", "*/compiler"]],
"linked": [["*/core", "*/common", "*/wallet", "*/mock-chain", "*/compiler", "*/crypto"]],
"access": "public",
"baseBranch": "master",
"updateInternalDependencies": "patch"
Expand Down
5 changes: 5 additions & 0 deletions .changeset/lucky-mangos-sell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@fleet-sdk/crypto": minor
---

Introduce `crypto` package with Ergo crypto primitives.
2 changes: 1 addition & 1 deletion packages/common/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# @fleet-sdk/core [![License](https://badgen.net/github/license/fleet-sdk/fleet/)](https://github.com/fleet-sdk/fleet/blob/master/LICENSE) [![npm](https://badgen.net/npm/v/@fleet-sdk/common)](https://www.npmjs.com/package/@fleet-sdk/common)
# @fleet-sdk/common [![License](https://badgen.net/github/license/fleet-sdk/fleet/)](https://github.com/fleet-sdk/fleet/blob/master/LICENSE) [![npm](https://badgen.net/npm/v/@fleet-sdk/common)](https://www.npmjs.com/package/@fleet-sdk/common)

Internal utility functions, constants and types shared across @fleet-sdk packages.
21 changes: 21 additions & 0 deletions packages/crypto/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2022 Nautilus Team

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
3 changes: 3 additions & 0 deletions packages/crypto/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# @fleet-sdk/crypto [![License](https://badgen.net/github/license/fleet-sdk/fleet/)](https://github.com/fleet-sdk/fleet/blob/master/LICENSE) [![npm](https://badgen.net/npm/v/@fleet-sdk/crypto)](https://www.npmjs.com/package/@fleet-sdk/crypto)

Ergo blockchain crypto primitives.
47 changes: 47 additions & 0 deletions packages/crypto/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
"name": "@fleet-sdk/crypto",
"version": "0.0.0",
"description": "Ergo blockchain crypto primitives.",
"main": "dist/cjs/index.js",
"module": "dist/esm/index.js",
"typings": "dist/esm/index.d.ts",
"sideEffects": true,
"repository": "fleet-sdk/fleet",
"license": "MIT",
"publishConfig": {
"access": "public",
"provenance": true
},
"keywords": [
"ergo",
"blockchain",
"crypto"
],
"scripts": {
"build": "run-p build:*",
"build:cjs": "tsc -p tsconfig.json",
"build:esm": "tsc -p tsconfig.esm.json",
"watch:build": "tsc -p tsconfig.json -w",
"watch:unit": "vitest"
},
"engines": {
"node": ">=14"
},
"dependencies": {
"@noble/hashes": "^1.3.0",
"@scure/base": "^1.1.1"
},
"devDependencies": {
"@fleet-sdk/common": "workspace:^"
},
"files": [
"src",
"dist",
"!**/*.spec.*",
"!**/*.json",
"!tests",
"CHANGELOG.md",
"LICENSE",
"README.md"
]
}
16 changes: 16 additions & 0 deletions packages/crypto/src/coders.bench.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { hexToBytes } from "@fleet-sdk/common";
import { hex } from "@scure/base";
import { bench, describe } from "vitest";

describe("HEX <> Bytes decoding", () => {
const validHex = "0008cd026dc059d64a50d0dbf07755c2c4a4e557e3df8afa7141868b3ab200643d437ee7";
// const invalidHex = "0008cd026dc059d64a50d0dbf07755c2c4a4e557e3df8afa7141868b3ab200643d437ee7tt";

bench("Using scure", () => {
hex.decode(validHex);
});

bench("Using own impl", () => {
hexToBytes(validHex);
});
});
21 changes: 21 additions & 0 deletions packages/crypto/src/coders.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import {
base58 as base58Coder,
base64 as base64Coder,
hex as hexCoder,
utf8 as utf8Coder
} from "@scure/base";

export interface Coder<F, T> {
encode(from: F): T;
decode(to: T): F;
}

export interface BytesCoder extends Coder<Uint8Array, string> {
encode: (data: Uint8Array) => string;
decode: (str: string) => Uint8Array;
}

export const base58 = base58Coder as BytesCoder;
export const hex = hexCoder as BytesCoder;
export const utf8 = utf8Coder as BytesCoder;
export const base64 = base64Coder as BytesCoder;
17 changes: 17 additions & 0 deletions packages/crypto/src/hashes.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { hexToBytes, utf8ToBytes } from "@fleet-sdk/common";
import { describe, expect, it } from "vitest";
import { blake2b256, sha256, tests } from "./hashes";

describe("Hashes", () => {
it("Should hash message using BLAKE2b256", () => {
expect(blake2b256(utf8ToBytes("blake2b256"))).to.be.deep.equal(
hexToBytes("eb95e6932cedac15db722fcdb0cfd21437f94690339a716251fad2f89842ea8b")
);
});

it("Should hash message using sha256", () => {
expect(sha256(utf8ToBytes("sha256"))).to.be.deep.equal(
hexToBytes("5d5b09f6dcb2d53a5fffc60c4ac0d55fabdf556069d6631545f42aa6e3500f2e")
);
});
});
7 changes: 7 additions & 0 deletions packages/crypto/src/hashes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { blake2b } from "@noble/hashes/blake2b";
import { sha256 as nobleSha256 } from "@noble/hashes/sha256";
import { BytesInput } from "./types";

export const blake2b256 = (message: BytesInput) => blake2b(message, { dkLen: 32 });

export const sha256 = nobleSha256 as (message: BytesInput) => Uint8Array;
1 change: 1 addition & 0 deletions packages/crypto/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./hashes";
1 change: 1 addition & 0 deletions packages/crypto/src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type BytesInput = Uint8Array | string;
8 changes: 8 additions & 0 deletions packages/crypto/tsconfig.esm.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"target": "ESNext",
"module": "ESNext",
"outDir": "dist/esm"
}
}
8 changes: 8 additions & 0 deletions packages/crypto/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"rootDir": "./src",
"outDir": "dist/cjs"
},
"include": ["src/**/*.ts"]
}
17 changes: 15 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8a13a29

Please sign in to comment.