npm package: @sourcenetwork/hublet
Description: A lightweight JS wallet library for SourceHub. Built with Web Crypto API and secp256r1 for secure key management and transaction signing.
Latest version: 1.1.1

# Hublet

A lightweight JS wallet library for SourceHub. Built with Web Crypto API and secp256r1 for secure key management and transaction signing.

## Features

- **Secure Key Generation**: Uses Web Crypto API for secp256r1 key pair generation
- **Transaction Signing**: Sign and broadcast Cosmos SDK transactions
- **Address Derivation**: Generate bech32 addresses from public keys
- **Session Storage**: Secure storage of wallet data using IndexedDB
- **DID Support**: Generate and manage DIDs

## Installation

```bash
npm install @sourcenetwork/hublet
# or
yarn add @sourcenetwork/hublet
```

## Quick Start

### Creating a New Wallet

```typescript
import { createNewSigner } from '@sourcenetwork/hublet';

const apiUrl = 'http://localhost:1317';
const signer = await createNewSigner(apiUrl);

// Get wallet address
const address = await signer.getAddress();
console.log('Wallet address:', address);

// Get public key
const publicKey = await signer.getCompressedPubkey();
console.log('Public key:', Buffer.from(publicKey).toString('hex'));
```

### Sending Transactions

```typescript
import { sendTokens } from '@sourcenetwork/hublet';

// Get account info and keys from signer
const accountInfo = await signer.getAccountInfo();
const fromAddress = await signer.getAddress();
const publicKeyCompressed = await signer.getCompressedPubkey();
const privateKey = await signer.getPrivateKey();

const result = await sendTokens({
  apiUrl: 'http://localhost:1317',
  chainId: 'sourcehub-dev',
  fromAddress,
  toAddress: 'source1...',
  amount: '1000000',
  denom: 'uopen',
  sequence: accountInfo.sequence,
  accountNumber: accountInfo.accountNumber,
  publicKeyCompressed,
  privateKey,
  feeAmount: '200' // Optional - omit for zero-fee transactions
});

console.log('Transaction result:', result);
```

### Storing and Loading Wallets

```typescript
import { createAndStoreSigner, loadStoredSigner } from '@sourcenetwork/hublet';

// Create and store a new wallet
const signer = await createAndStoreSigner('http://localhost:1317');
const address = await signer.getAddress();

// Later, load the stored wallet
const loadedSigner = await loadStoredSigner(
  Buffer.from(await signer.getCompressedPubkey()).toString('hex'),
  'http://localhost:1317'
);
```

## API Reference

### Core Classes

#### `WebCryptoHubletSigner`

Main signer class that implements the `HubletSigner` interface.

```typescript
interface HubletSigner {
  getAddress(): Promise<string>;
  getPubkey(): Promise<Uint8Array>;
  getCompressedPubkey(): Promise<Uint8Array>;
  getPrivateKey(): Promise<CryptoKey>;
  getAccountInfo(): Promise<{ accountNumber: number; sequence: number }>;
}
```

### Factory Functions

#### `createNewSigner(apiUrl: string)`
Creates a new signer with a fresh key pair.

#### `createAndStoreSigner(apiUrl: string)`
Creates a new signer and stores it in session storage.

#### `loadStoredSigner(storageKey: string, apiUrl: string)`
Loads a previously stored signer from session storage.

### Transaction Functions

#### `sendTokens(args: SendTokensArgs)`
Sends tokens to another address.

#### `signAndBroadcastR1Tx(args: SignAndBroadcastR1TxArgs)`
Signs and broadcasts a transaction with custom messages.

### Utility Functions

#### `deriveSecp256r1SdkAddress(pubkey: Uint8Array, prefix?: string)`
Derives a bech32 address from a compressed public key.

#### `didKeyFromP256PubKey(uncompressedPubKey: Uint8Array)`
Generates a DID key from a P256 public key.

## Development

### Prerequisites

- Node.js 18+
- Yarn (recommended) or npm

### Building

```bash
# Install dependencies
yarn install
# Build the library
yarn build
# Build individual formats
yarn build:esm
yarn build:cjs
```

### Testing

```bash
yarn test
```

### Linting

```bash
yarn lint
```

### Proto Generation

```bash
# Generate protobuf files
./generate-proto.sh
```

## License

This project is licensed under the Apache License 2.0.