# @didtools/pkh-ethereum

> Implements support to authenticate, authorize and verify with Ethereum accounts as a did:pkh with SIWE(X) and CACAO. Primarly used with `did-session` and `@didtools/cacao`.

Latest version **0.6.0** (published 2024-07-15) · (Apache-2.0 OR MIT) license · 0 weekly downloads

## Install

```sh
npm install @didtools/pkh-ethereum
pnpm add @didtools/pkh-ethereum
yarn add @didtools/pkh-ethereum
bun add @didtools/pkh-ethereum
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.6.0 |
| Published | 2024-07-15 |
| First published | 2022-09-22 |
| Weekly downloads | 0 |
| License | (Apache-2.0 OR MIT) |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=14.14 |
| Dependencies | 5 |
| Unpacked size | 20.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 103 |
| Author | 3Box Labs |
| Maintainers | dav1do, dbcfd, cb3box, ukstv, jpham2023, oed, paul_lecam |
| Keywords | DID, identity, did-provider, self-sovereign |

## Links

- npm: https://www.npmjs.com/package/@didtools/pkh-ethereum
- Repository: https://github.com/ceramicnetwork/js-did
- Homepage: https://github.com/ceramicnetwork/js-did#readme
- Issues: https://github.com/ceramicnetwork/js-did/issues
- npm.io page: https://npm.io/package/@didtools/pkh-ethereum

## Dependencies (5)

- [caip](https://npm.io/package/caip.md) ^1.1.0
- [@noble/curves](https://npm.io/package/@noble/curves.md) ^1.2.0
- [@noble/hashes](https://npm.io/package/@noble/hashes.md) ^1.3.2
- [@didtools/cacao](https://npm.io/package/@didtools/cacao.md) ^3.0.0
- [@stablelib/random](https://npm.io/package/@stablelib/random.md) ^1.0.2

## Alternatives

- [@sindresorhus/slugify](https://npm.io/package/@sindresorhus/slugify.md) — 3.7M weekly downloads
- [solid-js](https://npm.io/package/solid-js.md) — 2.7M weekly downloads
- [expo-glass-effect](https://npm.io/package/expo-glass-effect.md) — 2.5M weekly downloads
- [nanoassert](https://npm.io/package/nanoassert.md) — 780.8K weekly downloads
- [@ffmpeg/ffmpeg](https://npm.io/package/@ffmpeg/ffmpeg.md) — 529.5K weekly downloads

## Recent versions

- 0.6.0 (latest) — 2024-07-15
- 0.4.2-next.0 (next) — 2023-10-27
- 0.5.0 — 2024-01-04
- 0.4.1 — 2023-08-14
- 0.4.0 — 2023-06-20
- 0.3.0 — 2023-03-08
- 0.2.1 — 2023-03-01
- 0.2.0 — 2023-03-01
- 0.1.0 — 2023-02-24
- 0.0.3 — 2022-12-01
- 0.0.2 — 2022-09-28
- 0.0.1 — 2022-09-22

## README

## Ethereum AuthMethod and Verifier
Implements support to authenticate, authorize and verify with Ethereum accounts as a did:pkh with SIWE(X) and CACAO. 
Primarly used with `did-session` and `@didtools/cacao`. 

## Installation

```
npm install --save @didtools/pkh-ethereum
```

## Auth Usage

To Auth in a web based env, use any injected Ethereum provider that implements the standard interface with `EthereumWebAuth`. A standard Ethereum provider should impelement the interface as defined by [EIP-1193](https://eips.ethereum.org/EIPS/eip-1193).

```js
// Web Auth Usage
import { EthereumWebAuth, getAccountId } from '@didtools/pkh-ethereum'
// ...

const ethProvider = // import/get your web3 eth provider
const addresses = await ethProvider.request({ method: 'eth_requestAccounts' })
const accountId = await getAccountId(ethProvider, addresses[0])

const authMethod = await EthereumWebAuth.getAuthMethod(ethProvider, accountId)
```

To Auth in a Node based env, use any standard web3 provider interface with `EthereumNodeAuth`

```js
// Node Auth Usage
import { EthereumNodeAuth, getAccountId } from '@didtools/pkh-ethereum'
// ...

const ethProvider = // import/get your web3 eth provider
const addresses = await ethProvider.request({ method: 'eth_requestAccounts' })
const accountId = await getAccountId(ethProvider, addresses[0])
const appName = 'MyNodeApp'

const authMethod = await EthereumNodeAuth.getAuthMethod(ethProvider, accountId, appName)
```

To use with did-session and reference did-session docs for more details.

```js
const client = new ComposeClient({ceramic, definition})
const resources = client.resources

const session = await DIDSession.authorize(authMethod, { resources })
client.setDID(session.did)
```

## Configuration

AuthMethod creators consume a standard Ethereum provider and an AccountId. AccountID follows the 
CAIP10 standard. The helper method `getAccountID` is provided, but you can also create an AccountID
using the CAIP library directly. 

```js
import { AccountId } from 'caip'
import { getAccountId } from '@didtools/pkh-ethereum'

const accountId = await getAccountId(ethProvider, addresses[0])

const ethMainnetChainId = '1'
const chainNameSpace = 'eip155'
const chainId = `${chainNameSpace}:${ethMainnetChainId}`
const accountIdCAIP = new AccountId({ address, chainId })

// accountId = accountIdCAIP
```

The `EthereumNodeAuth` additionally consumes an application name. The 'EthereumWebAuth' method uses your 
application domain name by default.

```js
import { EthereumNodeAuth  } from '@didtools/pkh-ethereum'

const appName = 'MyNodeApp'
const authMethod = EthereumNodeAuth.getAuthMethod(ethProvider, accountId, appName)
```

## Verifier Usage

Verifiers are needed to verify different did:pkh signed payloads using CACAO. Libraries that need them will
consume a verifiers map allowing your to register the verifiers you want to support. 

```js
import { Cacao } from '@didtools/cacao'
import { getEIP191Verifier } from '@didtools/pkh-ethereum'
import { DID } from 'dids'

const verifiers = {
	...getEIP191Verifier()
}

// Directly with cacao
Cacao.verify(cacao, { verifiers, ...opts})

// With DIDS, reference DIDS for more details
const dids = //configured dids instance
await dids.verifyJWS(jws, { capability, verifiers, ...opts})
```

## License

Apache-2.0 OR MIT

---
_Source: https://npm.io/package/@didtools/pkh-ethereum · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
