Skip to content

Commit 0af8c91

Browse files
authored
Removed nodejs polyfill and added info to readme (#414)
1 parent c140525 commit 0af8c91

File tree

3 files changed

+66
-33
lines changed

3 files changed

+66
-33
lines changed

README.md

Lines changed: 57 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,44 @@ This project is used as a dependency by several other projects.
1313

1414
Proposals and issues for the specification itself should be submitted as pull requests to the [spec repo](https://github.com/decentralized-identity/decentralized-web-node).
1515

16-
1716
## Installation
1817

19-
If you are interested in using DWNs and web5 in your web app, you probably want to look at web5-js, instead of this repository. Head on over here: https://github.com/TBD54566975/web5-js
18+
If you are interested in using DWNs and web5 in your web app, you probably want to look at web5-js, instead of this repository. Head on over here: https://github.com/TBD54566975/web5-js.
19+
20+
For advanced users wishing to use this repo directly:
2021

2122
```bash
2223
npm install @tbd54566975/dwn-sdk-js
2324
```
2425

26+
Additional steps needed for some environments.
27+
28+
Node.js <= 18
29+
30+
```js
31+
// node.js 18 and earlier, needs globalThis.crypto polyfill
32+
import { webcrypto } from "node:crypto";
33+
// @ts-ignore
34+
if (!globalThis.crypto) globalThis.crypto = webcrypto;
35+
```
36+
37+
React Native:
38+
39+
```js
40+
// If you're on react native. React Native needs crypto.getRandomValues polyfill and sha512
41+
import "react-native-get-random-values";
42+
import { hmac } from "@noble/hashes/hmac";
43+
import { sha256 } from "@noble/hashes/sha256";
44+
import { sha512 } from "@noble/hashes/sha512";
45+
ed.etc.sha512Sync = (...m) => sha512(ed.etc.concatBytes(...m));
46+
ed.etc.sha512Async = (...m) => Promise.resolve(ed.etc.sha512Sync(...m));
47+
48+
secp.etc.hmacSha256Sync = (k, ...m) =>
49+
hmac(sha256, k, secp.etc.concatBytes(...m));
50+
secp.etc.hmacSha256Async = (k, ...m) =>
51+
Promise.resolve(secp.etc.hmacSha256Sync(k, ...m));
52+
```
53+
2554
## Usage
2655

2756
[API docs](https://tbd54566975.github.io/dwn-sdk-js/)
@@ -54,31 +83,35 @@ const result = await dwn.processMessage(didKey.did, recordsWrite.message, dataSt
5483
```
5584

5685
With a web wallet installed:
57-
```javascript
5886

59-
const result = await window.web5.dwn.processMessage({
60-
method : 'RecordsQuery',
61-
message : {
62-
filter: {
63-
schema: 'http://some-schema-registry.org/todo'
64-
},
65-
dateSort: 'createdAscending'
66-
}
67-
});
68-
```
87+
```javascript
88+
const result = await window.web5.dwn.processMessage({
89+
method: "RecordsQuery",
90+
message: {
91+
filter: {
92+
schema: "http://some-schema-registry.org/todo",
93+
},
94+
dateSort: "createdAscending",
95+
},
96+
});
97+
```
6998

7099
## Release/Build Process
100+
71101
The DWN JS SDK releases builds to [npmjs.com](https://www.npmjs.com/package/@tbd54566975/dwn-sdk-js). There are two build types: stable build and unstable build.
72102

73103
### Stable Build
104+
74105
This is triggered manually by:
75-
1. Increment `version` in `package.json` in [Semantic Versioning (semver)](https://semver.org/) format.
76-
2. Merge the change into `main` branch
77-
3. Create a release from GitHub.
78-
79-
An official build with version matching the `package.json` will be published to [npmjs.com](https://www.npmjs.com/package/@tbd54566975/dwn-sdk-js).
106+
107+
1. Increment `version` in `package.json` in [Semantic Versioning (semver)](https://semver.org/) format.
108+
2. Merge the change into `main` branch
109+
3. Create a release from GitHub.
110+
111+
An official build with version matching the `package.json` will be published to [npmjs.com](https://www.npmjs.com/package/@tbd54566975/dwn-sdk-js).
80112

81113
### Unstable Build
114+
82115
Every push to the `main` branch will automatically trigger an unstable build to [npmjs.com](https://www.npmjs.com/package/@tbd54566975/dwn-sdk-js) for developers to experiment and test.
83116

84117
The version string contains the date as well as the commit hash of the last change.
@@ -91,17 +124,15 @@ An example version string:
91124
- `2023-03-16` indicates the date of March 16th 2023
92125
- `36ec2ce` is the commit hash of the last change
93126

94-
## Some projects that use this library:
95-
96-
* [Web5 JS SDK](https://github.com/TBD54566975/web5-js)
97-
* [Example CLI](https://github.com/TBD54566975/dwn-cli)
98-
* [Example with a web wallet](https://github.com/TBD54566975/incubating-web5-labs/)
99-
* [Server side aggregator](https://github.com/TBD54566975/dwn-server)
100-
101-
127+
## Some projects that use this library:
102128

129+
- [Web5 JS SDK](https://github.com/TBD54566975/web5-js)
130+
- [Example CLI](https://github.com/TBD54566975/dwn-cli)
131+
- [Example with a web wallet](https://github.com/TBD54566975/incubating-web5-labs/)
132+
- [Server side aggregator](https://github.com/TBD54566975/dwn-server)
103133

104134
## Architecture
135+
105136
<img src="./images/dwn-architecture.png" alt="Architecture of DWN SDN" width="700">
106137

107138
> NOTE: The diagram is a conceptual view of the architecture, the actual component abstraction and names in source file may differ.

src/utils/secp256k1.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,6 @@ import { Encoder } from '../utils/encoder.js';
77
import { sha256 } from 'multiformats/hashes/sha2';
88
import { DwnError, DwnErrorCode } from '../core/dwn-error.js';
99

10-
// NOTE: @noble/secp256k1 requires globalThis.crypto polyfill for node.js <=18: https://github.com/paulmillr/noble-secp256k1/blob/main/README.md#usage
11-
// Remove when we move off of node.js v18 to v20, earliest possible time would be Oct 2023: https://github.com/nodejs/release#release-schedule
12-
import { webcrypto } from 'node:crypto';
13-
// @ts-ignore
14-
if (!globalThis.crypto) {globalThis.crypto = webcrypto;}
15-
16-
1710
/**
1811
* Class containing SECP256K1 related utility methods.
1912
*/

tests/validation/json-schemas/jwk-verification-method.spec.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@ import { validateJsonSchema } from '../../../src/schema-validator.js';
55
const { secp256k1 } = signers;
66

77
describe('JwkVerificationMethod', async () => {
8+
// NOTE: @noble/secp256k1 requires globalThis.crypto polyfill for
9+
// node.js <=18: https://github.com/paulmillr/noble-secp256k1/blob/main/README.md#usage
10+
// Remove when we move off of node.js v18 to v20, earliest possible time would be Oct 2023: https://github.com/nodejs/release#release-schedule
11+
if (parseInt(process.versions.node) <= 18) {
12+
const myCrypto = await import('node:crypto');
13+
// @ts-expect-error
14+
if (!globalThis.crypto) { globalThis.crypto = myCrypto; }
15+
}
16+
817
const { publicJwk } = await secp256k1.generateKeyPair();
918
it('should not throw an exception if properly formatted verificationMethod', () => {
1019
expect(

0 commit comments

Comments
 (0)