<!-- @format -->

# @dwn-protocol/id

Code Coverage
![Statements](https://img.shields.io/badge/statements-92.04%25-brightgreen.svg?style=flat) ![Branches](https://img.shields.io/badge/branches-95.04%25-brightgreen.svg?style=flat) ![Functions](https://img.shields.io/badge/functions-91.89%25-brightgreen.svg?style=flat) ![Lines](https://img.shields.io/badge/lines-92.04%25-brightgreen.svg?style=flat)

## Introduction

This repository contains ID++ v1 backwards compatibilty and a reference implementation of Decentralized Web Node (DWN) as per the [specification](https://identity.foundation/decentralized-web-node/spec/). This specification is in a draft state and very much so a WIP. For the foreseeable future, a lot of the work on DWN will be split across this repo and the repo that houses the specification.

Proposals and issues for the specification itself should be submitted as pull requests.

## Documentation

* <a href="docs/index.html">ID Dwn Protocol</a>
* <a href="">ID Dwn Guide</a>
* <a href="wp.pdf">ID Dwn Whitepaper</a>
* <a href="https://dwnprotocol.gitbook.io/id++-sdk-docs">ID SDK Docs</a>

## Installation

```bash
npm install @dwn-protocol/id
```

## Tests

To disable all tests

```bash
export TESTSOFF=true
```

## Added Steps

This package has dependency on [`@noble/ed25519`](https://github.com/paulmillr/noble-ed25519#usage) and [`@noble/secp256k1`](https://github.com/paulmillr/noble-secp256k1#usage) v2, additional steps are needed for some environments:

- Node.js <= 18

```js
// node.js 18 and earlier,  needs globalThis.crypto polyfill
import { webcrypto } from "node:crypto";
// @ts-ignore
if (!globalThis.crypto) globalThis.crypto = webcrypto;
```

## Usage

```javascript
import { 
  Dwn,
  DataStream,
  DidKeyResolver,
  DwnConfig,
  Jws,
  RecordsWrite,
  RecordsQuery,
  MessageStoreLevel
} from '@dwn-protocol/id';

let messageStore = new MessageStoreLevel({
  blockstoreLocation : 'BLOCKSTORE',
  indexLocation      : 'INDEX'
});

await messageStore.open();

// optional config
const config: DwnConfig = { messageStore };
const dwn = await Dwn.create(config);
// or 
const dwn = await Dwn.create();

// generate a did:key DID
const didKey = await DidKeyResolver.generate();

const signature = Jws.createSignatureInput(didKey);

// in node.js
const data = randomBytes(32);

// in web
const data = new Uint8Array(32);
window.crypto.getRandomValues(data);


const query = await RecordsWrite.create({
  data,
  dataFormat                  : 'application/json',
  published                   : true,
  protocol                    : 'authentication',
  schema                      : 'authentication/get',
  authorizationSignatureInput : signature
});

const dataStream = DataStream.fromBytes(data);
const response = await dwn.processMessage(didState.did, query.toJSON(), dataStream);

console.log('response', JSON.stringify(response));

```

_Note: built for node and browser environments_

## Architecture
>
>The diagram is a conceptual view, the actual component abstraction and names in the source code file may differ.

<img src="./images/id-architecture.png" alt="Architecture" width="700" >

## Project Resources

| Resource                                   | Description                                                                    |
| ------------------------------------------ | ------------------------------------------------------------------------------ |
| [CODEOWNERS](./CODEOWNERS)                 | Outlines the project lead(s)                                                   |
| [CODE_OF_CONDUCT.md](./CODE_OF_CONDUCT.md) | Expected behavior for project contributors, promoting a welcoming environment |
| [CONTRIBUTING.md](./CONTRIBUTING.md)       | Developer guide to build, test, run, access CI, chat, discuss, file issues     |
| [GOVERNANCE.md](./GOVERNANCE.md)           | Project governance                                                             |
| [LICENSE](./LICENSE)                       | Apache License, Version 2.0                                                    |


NOTES 
@TODO implement winston for any logging transports

https://www.npmjs.com/package/winston