Skip to content

Commit

Permalink
feat: adjusted crypto module for import in React Native, some refacto…
Browse files Browse the repository at this point in the history
…rings
  • Loading branch information
ohager committed Jan 13, 2025
1 parent e26c997 commit 933efb2
Show file tree
Hide file tree
Showing 49 changed files with 506 additions and 136 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -256,12 +256,13 @@ This monorepo uses [changeset](https://github.com/changesets/changesets) to mana

1. Create a changeset: `npx changeset`
2. Bump version: `npx changeset version`
3. Publish `npx changeset publish --tag <VERSION> --otp <NPM_OTP>`
3. Create git tag: `git tag <VERSION>` (starting with `v`, e.g. `v2.0.2`)
3. Publish `npx changeset publish --no-git-tag --otp=<NPM_OTP>`

The latter can be run as

```bash
npm run publish v2.0.1 --otp 123456
npm run publish v2.0.1 --otp=123456
```

> Note: Only with a valid npm OTP token
27 changes: 14 additions & 13 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"compile": "turbo run compile",
"bundle": "turbo run bundle",
"doc": "typedoc",
"publish": "turbo run compile test bundle && changeset publish --tag",
"publish": "./publish.sh",
"test": "turbo run test",
"test:ci": "turbo run test:ci",
"posttest:ci": "./generate-coverage-report.sh",
Expand Down
35 changes: 35 additions & 0 deletions packages/contracts/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,40 @@
# Change Log

## 2.0.3

### Patch Changes

- Decoupled Crypto Provider. Breaking Change as this requires the developer to define the platform specific crypto provider before using the sdk

Breaking Change:

If you see the following error:

```ts
"No Crypto Provider provided - Use [Crypto.init()] first";
```

You need to initialize the crypto module with the platform specific CryptoProvider.

**NodeJS**

```ts
import { Crypto, NodeJSCryptoProvider } from "@signumjs/crypto";
Crypto.init(new NodeJSCryptoProvider());
```

**Web/Browser**

```ts
import { Crypto, WebCryptoProvider } from "@signumjs/crypto";
Crypto.init(new WebCryptoProvider());
```

> Further implementations will be provided as external modules/packages, i.e. React Native Expo
- Updated dependencies
- @signumjs/util@2.0.3

## 2.0.2

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/contracts/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@signumjs/contracts",
"version": "2.0.2",
"version": "2.0.3",
"description": "Smart Contracts package for Signum Network",
"keywords": [
"signum",
Expand Down
38 changes: 38 additions & 0 deletions packages/core/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,43 @@
# Change Log

## 2.0.3

### Patch Changes

- Decoupled Crypto Provider. Breaking Change as this requires the developer to define the platform specific crypto provider before using the sdk

Breaking Change:

If you see the following error:

```ts
"No Crypto Provider provided - Use [Crypto.init()] first";
```

You need to initialize the crypto module with the platform specific CryptoProvider.

**NodeJS**

```ts
import { Crypto, NodeJSCryptoProvider } from "@signumjs/crypto";
Crypto.init(new NodeJSCryptoProvider());
```

**Web/Browser**

```ts
import { Crypto, WebCryptoProvider } from "@signumjs/crypto";
Crypto.init(new WebCryptoProvider());
```

> Further implementations will be provided as external modules/packages, i.e. React Native Expo
- Updated dependencies
- @signumjs/crypto@2.0.3
- @signumjs/contracts@2.0.3
- @signumjs/http@2.0.3
- @signumjs/util@2.0.3

## 2.0.2

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@signumjs/core",
"version": "2.0.2",
"version": "2.0.3",
"description": "Principal package with functions and models for building Signum Network applications.",
"keywords": [
"signum",
Expand Down
3 changes: 2 additions & 1 deletion packages/core/vitest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import { defineConfig } from 'vitest/config'
export default defineConfig({
test: {
globals: true,
setupFiles: ['./vitest.setup.ts'],
coverage: {
reporter: [['json', {file : "core-coverage.json"}]],
include: ["src/**"],
}
}
})
})
3 changes: 3 additions & 0 deletions packages/core/vitest.setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import {Crypto} from '@signumjs/crypto';
import {NodeJSCryptoAdapter} from "@signumjs/crypto/adapters"
Crypto.init(new NodeJSCryptoAdapter())
32 changes: 32 additions & 0 deletions packages/crypto/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,37 @@
# Change Log

## 2.0.3

### Patch Changes

- Decoupled Crypto Provider. Breaking Change as this requires the developer to define the platform specific crypto provider before using the sdk

Breaking Change:

If you see the following error:

```ts
"No Crypto Provider provided - Use [Crypto.init()] first";
```

You need to initialize the crypto module with the platform specific CryptoProvider.

**NodeJS**

```ts
import { Crypto, NodeJSCryptoProvider } from "@signumjs/crypto";
Crypto.init(new NodeJSCryptoProvider());
```

**Web/Browser**

```ts
import { Crypto, WebCryptoProvider } from "@signumjs/crypto";
Crypto.init(new WebCryptoProvider());
```

> Further implementations will be provided as external modules/packages, i.e. React Native Expo
## 2.0.2

### Patch Changes
Expand Down
29 changes: 25 additions & 4 deletions packages/crypto/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ yarn add @signumjs/crypto
#### Example

```js
import {sha256AsHex} from '@signumjs/crypto'
import {sha256AsHex, Crypto, NodeJSCryptoProvider} from '@signumjs/crypto'

Crypto.init(new NodeJSCryptoProvider()); // or WebCryptoProvider
console.log(sha256AsHex('test'))
```

Expand All @@ -44,6 +46,7 @@ Just import the package using the HTML `<script>` tag.
#### Example

```js
sig$crypto.Crypto.init(new sig$crypto.WebCryptoProvider())
console.log(sig$crypto.sha256AsHex('test'))
```

Expand All @@ -53,11 +56,29 @@ See more here:

## Crossplatform Usage

The crypto package is built to be used out of the box in modern web browsers and NodeJS (and alike backends).
Depending on the runtime environment the correct `CryptoProvider`-implementation is being used for cryptographic routines.
As there are different crypto implementations for different platforms available the underlying crypto contexts need to be initialized.
The crypto package provides used out of the box implementations for modern web browsers and NodeJS (and alike backends, i.e. deno and bun).
Depending on the runtime environment the correct `CryptoAdapter`-implementation needs to be set for cryptographic routines.
In a web browser the [Crypto Web API](https://developer.mozilla.org/en-US/docs/Web/API/Crypto) is used, i.e. a secure (https) environment is required.
In NodeJS the [NodeJS Crypto API](https://nodejs.org/api/crypto.html) is used.

Run the following before any usage of crypto functions

__Web__

```ts
import {Crypto, WebCryptoAdapter} from "@signumjs/crypto"
Crypto.init(new WebCryptoAdapter());
```

__NodeJS__ (Deno, Bun)

```ts
import {Crypto, NodeJSCryptoAdapter} from "@signumjs/crypto"
Crypto.init(new NodeJSCryptoAdapter());
```


> For web `localhost` is considered a secure context

Expand Down Expand Up @@ -112,7 +133,7 @@ Then use the custom crypto provider like this:
```ts
import {Crypto, sha256AsHex} from '@signumjs/crypto'

Crypto.getInstance().setCustomProvider(new CustomCryptoProvider());
Crypto.init(new CustomCryptoProvider());

(async ()=> {
// internally uses the custom crypto provider
Expand Down
2 changes: 1 addition & 1 deletion packages/crypto/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const esbuild = require('esbuild');

function createBuildSettings(options) {
return {
entryPoints: ['./src/index.ts'],
entryPoints: ['./src/web-bundle.ts'],
outfile: './dist/signumjs.crypto.min.js',
globalName: 'sig$crypto',
external: ["crypto"],
Expand Down
6 changes: 5 additions & 1 deletion packages/crypto/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@signumjs/crypto",
"version": "2.0.2",
"version": "2.0.3",
"description": "Cryptographic functions for building Signum Network apps.",
"keywords": [
"signum",
Expand All @@ -25,6 +25,10 @@
"license": "Apache-2.0",
"main": "./out/index.js",
"typings": "./out/index.d.ts",
"exports": {
".": "./out/index.js",
"./adapters": "./out/adapters/index.js"
},
"private": false,
"devDependencies": {
"@types/node": "^20.16.0",
Expand Down
Loading

0 comments on commit 933efb2

Please sign in to comment.