Socket
Socket
Sign inDemoInstall

@solana/keys

Package Overview
Dependencies
Maintainers
13
Versions
1316
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@solana/keys

Helpers for generating and transforming key material


Version published
Weekly downloads
4.4K
increased by10.25%
Maintainers
13
Weekly downloads
 
Created
Source

npm npm-downloads semantic-release
code-style-prettier

@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.

Types

Base58EncodedAddress

This type represents a string that validates as a Solana address or public key. Functions that require well-formed addresses should specify their inputs in terms of this type.

Whenever you need to validate an arbitrary string as a base58-encoded address, use the assertIsBase58EncodedAddress() function in this package.

Ed25519Signature

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

assertIsBase58EncodedAddress()

Client applications primarily deal with addresses and public keys in the form of base58-encoded strings. Addresses and public keys returned from the RPC API conform to the type Base58EncodedAddress. You can use a value of that type wherever a base58-encoded address or key is expected.

From time to time you might acquire a string, that you expect to validate as an address, from an untrusted network API or user input. To assert that such an arbitrary string is a base58-encoded address, use the assertIsBase58EncodedAddress function.

import { assertIsBase58EncodedAddress } from '@solana/keys';

// Imagine a function that fetches an account's balance when a user submits a form.
function handleSubmit() {
    // We know only that what the user typed conforms to the `string` type.
    const address: string = accountAddressInput.value;
    try {
        // If this type assertion function doesn't throw, then
        // Typescript will upcast `address` to `Base58EncodedAddress`.
        assertIsBase58EncodedAddress(address);
        // At this point, `address` is a `Base58EncodedAddress` that can be used with the RPC.
        const balanceInLamports = await rpc.getBalance(address).send();
    } catch (e) {
        // `address` turned out not to be a base58-encoded address
    }
}

generateKeyPair()

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

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

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

getBase58EncodedAddressFromPublicKey()

Given a public CryptoKey, this method will return its associated Base58EncodedAddress.

import { getBase58EncodedAddressFromPublicKey } from '@solana/keys';

const address = await getBase58EncodedAddressFromPublicKey(publicKey);

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.

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

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

verifySignature()

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

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`');
}

Keywords

FAQs

Package last updated on 22 Jul 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc