Skip to content

Commit

Permalink
feat: encryption (#11)
Browse files Browse the repository at this point in the history
* feat: add support for encryption and decryption

* chore: add auto generated docs

* chore: re-add exports
  • Loading branch information
oed authored Sep 22, 2020
1 parent 2933711 commit 4fc79ba
Show file tree
Hide file tree
Showing 35 changed files with 6,716 additions and 1,033 deletions.
191 changes: 24 additions & 167 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ A simple library to interact with DIDs that conform to the DID-provider interfac
npm install dids
```

## API
[API documentation](https://ceramicnetwork.github.io/js-did/classes/_src_index_.did.html)

## Examples

### Authentication with the provider
Expand Down Expand Up @@ -73,186 +76,40 @@ console.log((await ipfs.dag.get(jwsCid)).value)

As can be observed above the `createDagJWS` method takes the payload, encodes it using `dag-cbor` and computes it's CID. It then uses this CID as the payload of the JWS that is then signed. The JWS that was just created can be put into ipfs using the `dag-jose` codec. Returned is also the encoded block of the payload. This can be put into ipfs using `ipfs.block.put`. Alternatively `ipfs.dag.put(payload)` would have the same effect.

### Resolving DIDs

```js
import { DID } from 'dids'

// See https://github.com/decentralized-identity/did-resolver
const registry = { test: myTestResolver }
const did = new DID({ resolver: { registry } })

// Resolve a DID document
await did.resolve('did:test:...')
```

## Interfaces and types

### CID

A `CID` instance, as exported by the [CID library](https://github.com/multiformats/js-cid).
### Use DagJWE with IPFS

### DIDDocument
The DagJWE functionality allows us to encrypt IPLD data to one or multiple DIDs. The resulting JWE object can then be put into ipfs using the [dag-jose](https://github.com/ceramicnetwork/js-dag-jose) codec. A user that is authenticated can at a later point decrypt this object.

The DID document interface, as defined in the [DID resolver library](https://github.com/decentralized-identity/did-resolver).

### DIDProvider

The DID provider interface, an alias for [`RPCConnection`](https://github.com/ceramicnetwork/js-rpc-utils#rpcconnection).

### JWSSignature

```ts
interface JWSSignature {
protected: string
signature: string
}
```

### DagJWS

```ts
interface DagJWS {
payload: string
signatures: Array<JWSSignature>
link: CID
}
```

### AuthenticateOptions

```ts
interface AuthenticateOptions {
provider?: DIDProvider
}
```

### CreateJWSOptions

```ts
interface CreateJWSOptions {
did?: string
protected?: Record<string, any>
}
```
```js
const cleartext = { some: 'data', coolLink: new CID('bafyqacnbmrqxgzdgdeaui') }

### DagJWSResult
// encrypt the cleartext object
const jwe = await did.createDagJWE(cleartext, ['did:3:bafy89h4f9...', 'did:key:za234...'])

```ts
interface DagJWSResult {
jws: string // base64-encoded
linkedBlock: string // base64-encoded
}
```
// put the JWE into the ipfs dag
const jweCid = await ipfs.dag.put(jwe, { format: 'dag-jose', hashAlg: 'sha2-256' })

### ResolverRegistry

A record of DID methods to resolvers, as defined in the [DID resolver library](https://github.com/decentralized-identity/did-resolver).

```ts
export type ResolverRegistry = Record<string, DIDResolver>
// get the jwe from the dag and decrypt it
const dagJWE = await ipfs.dag.get(jweCid)
console.log(await did.decryptDagJWE(dagJWE))
// output:
// > { some: 'data' }
```

### ResolverOptions
Options used to create a `Resolver` instance, as defined in the [DID resolver library](https://github.com/decentralized-identity/did-resolver).
### Resolving DIDs

```ts
export interface ResolverOptions {
registry?: ResolverRegistry
cache?: DIDCache | boolean
}
```
```js
import { DID } from 'dids'

### DIDOptions
// See https://github.com/decentralized-identity/did-resolver
const registry = { test: myTestResolver }
const did = new DID({ resolver: { registry } })

```ts
export interface DIDOptions {
provider?: DIDProvider
resolver?: Resolver | ResolverOptions
}
// Resolve a DID document
await did.resolve('did:test:...')
```

## API

### DID class

#### constructor

**Arguments**

1. `options?: DIDOptions`

#### did.authenticated

**Returns** `boolean`

#### did.id

> Accessing this property will throw an error if the instance is not authenticated
**Returns** `string`

#### did.setProvider()

> Calling this method will throw an error if a different provider is already set
**Arguments**

1. `provider: DIDProvider`

**Returns** `void`

#### did.setResolver()

**Arguments**

1. `resolver: Resolver | ResolverOptions`

**Returns** `void`

#### did.authenticate()

> Calling this method with a provider will throw an error if a different provider is already set
**Arguments**

1. `options?: AuthenticateOptions`

**Returns** `Promise<string>`

#### did.createJWS()

> The instance needs to be authenticated before calling this method
**Arguments**

1. `payload: Record<string, any>`
1. `options?: CreateJWSOptions` to specify the `protected` header

**Returns** `Promise<string>`

#### did.createDagJWS()

Creates a JWS that is compatible with [dag-jose](https://github.com/ceramicnetwork/js-dag-jose).

> The instance needs to be authenticated before calling this method
**Arguments**

1. `payload: Record<string, any>`
1. `options?: CreateJWSOptions` to specify the `protected` header, and did with keyFragment

**Returns** `Promise<DagJWSResult>`

#### did.resolve()

**Arguments**

1. `didUrl: string`

**Returns** `Promise<DIDDocument>`

## License

MIT
3 changes: 3 additions & 0 deletions docs/_config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
include:
- "_*_.html"
- "_*_.*.html"
1 change: 1 addition & 0 deletions docs/assets/css/main.css

Large diffs are not rendered by default.

Binary file added docs/assets/images/icons.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/assets/images/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/assets/images/widgets.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/assets/images/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 4fc79ba

Please sign in to comment.