# @ssc-half-light/util

> Utility functions related to the webcrypto API

Latest version **0.11.5** (published 2024-03-16) · MIT license · 0 weekly downloads

## Install

```sh
npm install @ssc-half-light/util
pnpm add @ssc-half-light/util
yarn add @ssc-half-light/util
bun add @ssc-half-light/util
```

## 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.11.5 |
| Published | 2024-03-16 |
| First published | 2023-10-07 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 3 |
| Unpacked size | 374.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | nichoth |
| Maintainers | nichoth |
| Keywords | template, typescript |

## Links

- npm: https://www.npmjs.com/package/@ssc-half-light/util
- Repository: https://github.com/ssc-half-light/util
- Homepage: https://github.com/ssc-half-light/util#readme
- Issues: https://github.com/ssc-half-light/util/issues
- npm.io page: https://npm.io/package/@ssc-half-light/util

## Dependencies (3)

- [@oddjs/odd](https://npm.io/package/@oddjs/odd.md) ^0.37.2
- [uint8arrays](https://npm.io/package/uint8arrays.md) ^4.0.6
- [@noble/hashes](https://npm.io/package/@noble/hashes.md) ^1.3.0

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 0.11.5 (latest) — 2024-03-16
- 0.11.4 — 2024-02-13
- 0.11.3 — 2023-11-19
- 0.11.2 — 2023-11-18
- 0.11.1 — 2023-11-18
- 0.11.0 — 2023-11-18
- 0.10.4 — 2023-10-12
- 0.10.3 — 2023-10-07
- 0.10.2 — 2023-10-07

## README

# util
![tests](https://github.com/ssc-half-light/util/actions/workflows/nodejs.yml/badge.svg)
[![Socket Badge](https://socket.dev/api/badge/npm/package/@ssc-half-light/util)](https://socket.dev/npm/package/@ssc-half-light/util)
[![types](https://img.shields.io/npm/types/@ssc-half-light/util)](README.md)
[![module](https://img.shields.io/badge/module-ESM-blue)](README.md)
[![license](https://img.shields.io/badge/license-MIT-brightgreen)](LICENSE)

Utility functions

Depends on `uint8arrays` and `@oddjs/odd`.

## install

```bash
npm i @ssc-half-light/util
```

## API

### verify
Works in node or browsers.

```ts
async (did:string, sig:string, msg:string):Promise<boolean>
```

#### example &mdash; verify
```ts
import { verify } from '@ssc-half-light/util'

const DID = 'did:key:z13V3Sog2Ya...'
const sig = 'X8iGF4Lz4erw4UE...'

const isValid = await verify(DID, sig, 'my message')
```

### sign
```ts
(keystore:KeyStore, msg:string):Promise<Uint8Array>
```

#### example &mdash; sign
This depends on a `keystore` instance.

```ts
import { sign, toString } from '@ssc-half-light/util'
import * as odd from '@oddjs/odd'

const program = await odd.program({
    namespace: { creator: 'test', name: 'testing' },
    debug: true
})
const { keystore } = program.components.crypto
const sig = toString(await sign(keystore, 'my message'))
```


### toString
Return a `'base64url'` string. Good for getting a string version of a signature.

```ts
function toString (arr:Uint8Array):string
```

#### example &mdash; toString
```js
test('toString', t => {
    const myString = toString(myUint8Array)
    t.equal(typeof myString, 'string', 'should conver a Uint8Array to string')
})
```
### writeKeyToDid
Convert a given public key to a DID string.

```ts
async function writeKeyToDid (crypto:Crypto.Implementation):Promise<DID>
```

#### example
```js
import { writeKeyToDid } from '@ssc-half-light/util'

test('writeKeyToDid', async t => {
    const crypto = program.components.crypto
    const did = await writeKeyToDid(crypto)
    console.log('**did**', did)
    t.equal(typeof did, 'string', 'should create string')
    t.ok(did.includes('did:key:'), 'should return the right format string')
})
```

### didToPublicKey 
Convert a DID string to a `Uint8Array` and `type` string.

```ts
function didToPublicKey (did:string):({
    publicKey:Uint8Array,
    type: 'rsa' | 'ed25519' | 'bls12-381'
})
```

#### example &mdash; didToPublicKey
Get the DID version of the key used to sign messages.

```js
test('didToPublicKey', t => {
    const did = 'did:key:z13...'
    const pubKey = didToPublicKey(did)

    t.equal(pubKey.type, 'rsa', 'should return the right key type')
    t.ok(pubKey.publicKey instanceof Uint8Array,
        'should return the public key as a Uint8Array')
})
```

---
_Source: https://npm.io/package/@ssc-half-light/util · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
