-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
34 changed files
with
12,661 additions
and
9,512 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
*.log | ||
.DS_Store | ||
node_modules/ | ||
coverage/ | ||
lib/ | ||
dist/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Change Log | ||
|
||
## 1.0.0 | ||
|
||
Initial release |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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": "../.." | ||
} | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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:^" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"extends": "../../tsconfig.build.json", | ||
"compilerOptions": { | ||
"outDir": "./dist" | ||
}, | ||
"include": ["src"] | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"extends": "./tsconfig.json", | ||
"include": ["src", "test"] | ||
} |
Oops, something went wrong.