Skip to content

Commit

Permalink
Add Key DID package (#190)
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulLeCam authored Jul 4, 2024
1 parent afe5a2a commit c8d1de7
Show file tree
Hide file tree
Showing 34 changed files with 12,661 additions and 9,512 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build-docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
- name: Install pnpm
uses: pnpm/action-setup@v2
with:
version: 8
version: 9

- name: Setup node
uses: actions/setup-node@v3
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/build-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
- name: Install pnpm
uses: pnpm/action-setup@v2
with:
version: 8
version: 9

- name: Use Node ${{ matrix.node }}
uses: actions/setup-node@v3
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/deploy-docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
- name: Install pnpm
uses: pnpm/action-setup@v2
with:
version: 8
version: 9

- name: Setup node
uses: actions/setup-node@v3
Expand Down
2 changes: 1 addition & 1 deletion packages/did-session/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
*
* Additional helper functions are available to help you manage a session lifecycle and the user experience.
*
* ```ss
* ```js
* // Check if authorized or created from existing session string
* didsession.hasSession
*
Expand Down
13 changes: 13 additions & 0 deletions packages/key-did/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"extends": ["3box", "3box/jest", "3box/typescript"],
"parserOptions": {
"project": ["tsconfig.lint.json"]
},
"rules": {
"@typescript-eslint/ban-ts-comment": "off",
"@typescript-eslint/no-namespace": "off",
"@typescript-eslint/no-unsafe-assignment": "off",
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/ban-ts-ignore": "off"
}
}
6 changes: 6 additions & 0 deletions packages/key-did/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
*.log
.DS_Store
node_modules/
coverage/
lib/
dist/
5 changes: 5 additions & 0 deletions packages/key-did/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Change Log

## 1.0.0

Initial release
5 changes: 5 additions & 0 deletions packages/key-did/LICENSE-APACHE
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
19 changes: 19 additions & 0 deletions packages/key-did/LICENSE-MIT
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
The MIT License (MIT)

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.
42 changes: 42 additions & 0 deletions packages/key-did/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Key DID

Utility functions for `did:key` DIDs.

## Installation

```sh
npm install @didtools/key-did
```

## Usage

### Create a DID with an optional seed

```js
import { createDID } from '@didtools/key-did'

const seed = // 32 bytes of entropy, Uint8Array
const did = createDID(seed)

// Authenticate with the provider
await did.authenticate()
```

### Generate a random private key and create an authenticated DID

```js
import { generatePrivateKey, getAuthenticatedDID } from '@didtools/key-did'

const randomKey = generatePrivateKey()
const did = await getAuthenticatedDID(randomKey)
```

## Additional Usage Notes

See the [dids developer site](https://did.js.org/) for more details about how to use this package.

## Contributing
We are happy to accept small and large contributions.

## License
Apache-2.0 OR MIT
22 changes: 22 additions & 0 deletions packages/key-did/jest.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"moduleFileExtensions": ["js", "json", "ts"],
"testRegex": ".(spec|test).ts$",
"testEnvironment": "node",
"extensionsToTreatAsEsm": [".ts"],
"globals": {
"ts-jest": {
"useESM": true
}
},
"moduleNameMapper": {
"^(\\.{1,2}/.*)\\.js$": "$1"
},
"transform": {
"^.+\\.(t|j)s$": [
"@swc/jest",
{
"root": "../.."
}
]
}
}
46 changes: 46 additions & 0 deletions packages/key-did/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"name": "@didtools/key-did",
"version": "1.0.0",
"description": "Ceramic did:key utilities",
"keywords": [
"ceramic",
"did:key",
"did",
"key"
],
"repository": {
"type": "git",
"url": "git+https://github.com/ceramicnetwork/js-did.git"
},
"author": "3Box Labs",
"license": "(Apache-2.0 OR MIT)",
"type": "module",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"exports": {
".": "./dist/index.js"
},
"files": [
"dist"
],
"engines": {
"node": ">=14.14"
},
"sideEffects": false,
"scripts": {
"build:clean": "del dist",
"build:js": "swc src -d ./dist --config-file ../../.swcrc",
"build:types": "tsc --emitDeclarationOnly --skipLibCheck",
"build": "pnpm run build:clean && pnpm run build:types && pnpm run build:js",
"lint": "eslint src --fix",
"test": "node --experimental-vm-modules ../../node_modules/jest/bin/jest.js",
"test:ci": "pnpm run test --ci --coverage",
"prepare": "pnpm run build",
"prepublishOnly": "package-check"
},
"dependencies": {
"dids": "workspace:^",
"key-did-provider-ed25519": "workspace:^",
"key-did-resolver": "workspace:^"
}
}
58 changes: 58 additions & 0 deletions packages/key-did/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
* # Key DID
*
* Utility functions for `did:key` DIDs.
*
* ## Installation
*
* ```sh
* npm install @didtools/key-did
* ```
*
* ## Usage
*
* ### Create a DID with an optional seed
*
* ```js
* import { createDID } from '@didtools/key-did'
*
* const seed = // 32 bytes of entropy, Uint8Array
* const did = createDID(seed)
*
* // Authenticate with the provider
* await did.authenticate()
* ```
*
* ### Generate a random private key and create an authenticated DID
*
* ```js
* import { generatePrivateKey, getAuthenticatedDID } from '@didtools/key-did'
*
* const randomKey = generatePrivateKey()
* const did = await getAuthenticatedDID(randomKey)
* ```
*
* @module key-did
*/

import { DID } from 'dids'
import { Ed25519Provider } from 'key-did-provider-ed25519'
import { getResolver } from 'key-did-resolver'

export function createDID(seed?: Uint8Array): DID {
const did = new DID({ resolver: getResolver() })
if (seed != null) {
did.setProvider(new Ed25519Provider(seed))
}
return did
}

export function generatePrivateKey(): Uint8Array {
return globalThis.crypto.getRandomValues(new Uint8Array(32))
}

export async function getAuthenticatedDID(seed: Uint8Array): Promise<DID> {
const did = createDID(seed)
await did.authenticate()
return did
}
42 changes: 42 additions & 0 deletions packages/key-did/test/lib.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { webcrypto } from 'node:crypto'

import {
createDID,
generatePrivateKey,
getAuthenticatedDID,
} from '../src'

globalThis.crypto = webcrypto

describe('createDID()', () => {
test('creates a DID with no provider by default', async () => {
const did = createDID()
await expect(async () => {
await did.authenticate()
}).rejects.toThrow('No provider available')
})

test('creates a DID with a provider when a seed is provided', async () => {
const did = createDID(new Uint8Array(32))
expect(did.authenticated).toBe(false)
await did.authenticate()
expect(did.authenticated).toBe(true)
})
})

test('generatePrivateKey() generates a random 32-bytes key', () => {
const key1 = generatePrivateKey()
const key2 = generatePrivateKey()
expect(key1.toString()).not.toBe(key2.toString())
})

test('getAuthenticatedDID() returns an authenticated DID instance for the given seed', async () => {
const [did1, did2, did3] = await Promise.all([
getAuthenticatedDID(new Uint8Array(32)),
getAuthenticatedDID(new Uint8Array(32)),
getAuthenticatedDID(generatePrivateKey()),
])
expect(did1.authenticated).toBe(true)
expect(did2.id).toBe(did1.id)
expect(did3.id).not.toBe(did1.id)
})
8 changes: 8 additions & 0 deletions packages/key-did/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "../../tsconfig.build.json",
"compilerOptions": {
"outDir": "./dist"
},
"include": ["src"]
}

4 changes: 4 additions & 0 deletions packages/key-did/tsconfig.lint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "./tsconfig.json",
"include": ["src", "test"]
}
Loading

0 comments on commit c8d1de7

Please sign in to comment.