# @mcintyre94/keys

> Helpers for generating and transforming key material

Latest version **2.0.0-experimental.f9b30bb** (published 2023-11-29) · MIT license · 0 weekly downloads

## Install

```sh
npm install @mcintyre94/keys
pnpm add @mcintyre94/keys
yarn add @mcintyre94/keys
bun add @mcintyre94/keys
```

## Health

**Score 40/100 (D)** — status: abandoned.

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

Warnings: low downloads.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 2.0.0-experimental.f9b30bb |
| Published | 2023-11-29 |
| First published | 2023-11-29 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 3 |
| Unpacked size | 76.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2782 |
| Author | Solana Labs Maintainers |
| Maintainers | mcintyre94 |
| Keywords | blockchain, solana, web3 |

## Links

- npm: https://www.npmjs.com/package/@mcintyre94/keys
- Repository: https://github.com/solana-labs/solana-web3.js
- Homepage: https://github.com/solana-labs/solana-web3.js#readme
- Issues: http://github.com/solana-labs/solana-web3.js/issues
- npm.io page: https://npm.io/package/@mcintyre94/keys

## Dependencies (3)

- [@mcintyre94/assertions](https://npm.io/package/@mcintyre94/assertions.md) 2.0.0-experimental.f9b30bb
- [@mcintyre94/codecs-core](https://npm.io/package/@mcintyre94/codecs-core.md) 2.0.0-experimental.f9b30bb
- [@mcintyre94/codecs-strings](https://npm.io/package/@mcintyre94/codecs-strings.md) 2.0.0-experimental.f9b30bb

## Recent versions

- 2.0.0-experimental.f9b30bb (latest) — 2023-11-29
- 2.0.0-experimental.e807d29 (experimental) — 2023-12-01
- 2.0.0-experimental.a537d8d — 2023-12-01

## README

[![npm][npm-image]][npm-url]
[![npm-downloads][npm-downloads-image]][npm-url]
[![semantic-release][semantic-release-image]][semantic-release-url]
<br />
[![code-style-prettier][code-style-prettier-image]][code-style-prettier-url]

[code-style-prettier-image]: https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square
[code-style-prettier-url]: https://github.com/prettier/prettier
[npm-downloads-image]: https://img.shields.io/npm/dm/@solana/keys/experimental.svg?style=flat
[npm-image]: https://img.shields.io/npm/v/@solana/keys/experimental.svg?style=flat
[npm-url]: https://www.npmjs.com/package/@solana/keys/v/experimental
[semantic-release-image]: https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg
[semantic-release-url]: https://github.com/semantic-release/semantic-release

# @solana/keys

This package contains utilities for validating, generating, and manipulating addresses and key material. It can be used standalone, but it is also exported as part of the Solana JavaScript SDK [`@solana/web3.js@experimental`](https://github.com/solana-labs/solana-web3.js/tree/master/packages/library).

## Types

### `Signature`

This type represents a 64-byte Ed25519 signature of some data with a private key, as a base58-encoded string.

### `SignatureBytes`

This type represents a 64-byte Ed25519 signature of some data with a private key.

Whenever you need to verify that a particular signature is, in fact, the one that would have been produced by signing some known bytes using the private key associated with some known public key, use the `verifySignature()` function in this package.

## Functions

### `assertIsSignature()`

From time to time you might acquire a string that you expect to be a base58-encoded signature (eg. of a transaction) from an untrusted network API or user input. To assert that such an arbitrary string is in fact an Ed25519 signature, use the `assertIsSignature` function.

```ts
import { assertIsSignature } from '@solana/keys';

// Imagine a function that asserts whether a user-supplied signature is valid or not.
function handleSubmit() {
    // We know only that what the user typed conforms to the `string` type.
    const signature: string = signatureInput.value;
    try {
        // If this type assertion function doesn't throw, then
        // Typescript will upcast `signature` to `Signature`.
        assertIsSignature(signature);
        // At this point, `signature` is a `Signature` that can be used with the RPC.
        const {
            value: [status],
        } = await rpc.getSignatureStatuses([signature]).send();
    } catch (e) {
        // `signature` turned out not to be a base58-encoded signature
    }
}
```

### `generateKeyPair()`

Generates an Ed25519 public/private key pair for use with other methods in this package that accept `CryptoKey` objects.

```ts
import { generateKeyPair } from '@solana/keys';

const { privateKey, publicKey } = await generateKeyPair();
```

### `isSignature()`

This is a type guard that accepts a string as input. It will both return `true` if the string conforms to the `Signature` type and will refine the type for use in your program.

```ts
import { isSignature } from '@solana/keys';

if (isSignature(signature)) {
    // At this point, `signature` has been refined to a
    // `Signature` that can be used with the RPC.
    const {
        value: [status],
    } = await rpc.getSignatureStatuses([signature]).send();
    setSignatureStatus(status);
} else {
    setError(`${signature} is not a transaction signature`);
}
```

### `signBytes()`

Given a private `CryptoKey` and a `Uint8Array` of bytes, this method will return the 64-byte Ed25519 signature of that data as a `Uint8Array`.

```ts
import { signBytes } from '@solana/keys';

const data = new Uint8Array([1, 2, 3]);
const signature = await signBytes(privateKey, data);
```

### `signature()`

This helper combines _asserting_ that a string is an Ed25519 signature with _coercing_ it to the `Signature` type. It's best used with untrusted input.

```ts
import { signature } from '@solana/keys';

const signature = signature(userSuppliedSignature);
const {
    value: [status],
} = await rpc.getSignatureStatuses([signature]).send();
```

### `verifySignature()`

Given a public `CryptoKey`, some `SignatureBytes`, and a `Uint8Array` of data, this method will return `true` if the signature was produced by signing the data using the private key associated with the public key, and `false` otherwise.

```ts
import { verifySignature } from '@solana/keys';

const data = new Uint8Array([1, 2, 3]);
if (!(await verifySignature(publicKey, signature, data))) {
    throw new Error('The data were *not* signed by the private key associated with `publicKey`');
}
```

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