Bech32m address encoding and decoding for the Midnight network.
npm install @midnight-ntwrk/wallet-sdk-address-formatBech32m address encoding and decoding for the Midnight network.
``bash`
npm install @midnight-ntwrk/wallet-sdk-address-format
This package provides Bech32m address formatting for the Midnight blockchain. It supports encoding and decoding of
various address types including shielded addresses (for ZK transactions), unshielded addresses (for transparent
transactions), and dust addresses (for fee tokens).
Key features:
- Bech32m encoding/decoding with Midnight-specific prefix (mn_)
- Network-aware address formatting (mainnet vs testnet)
- Support for shielded, unshielded, and dust address types
- Type-safe codec system for custom address types
For transparent transactions on the Midnight network.
`typescript
import { UnshieldedAddress, MidnightBech32m, mainnet } from '@midnight-ntwrk/wallet-sdk-address-format';
import type { NetworkId } from '@midnight-ntwrk/wallet-sdk-address-format';
import { addressFromKey, signatureVerifyingKey } from '@midnight-ntwrk/ledger-v7';
import { randomBytes } from 'node:crypto';
const networkId: NetworkId = 'preview';
// Derive an unshielded address from a random unshielded secret key
const secretKey = randomBytes(32);
const publicKey = signatureVerifyingKey(secretKey.toString('hex'));
const addressHex = addressFromKey(publicKey);
const unshieldedAddress = new UnshieldedAddress(Buffer.from(addressHex, 'hex'));
// Encode to Bech32m string: mn_addr_preview1...
const encoded = MidnightBech32m.encode(networkId, unshieldedAddress).toString();
// Parse and decode back to UnshieldedAddress
const decoded = MidnightBech32m.parse(encoded).decode(UnshieldedAddress, networkId);
// Compare addresses
unshieldedAddress.equals(decoded); // true
// Mainnet addresses omit the network suffix: mn_addr1...
const mainnetEncoded = MidnightBech32m.encode(mainnet, unshieldedAddress).toString();
`
For zero-knowledge transactions on the Midnight network.
`typescript
import {
ShieldedAddress,
ShieldedCoinPublicKey,
ShieldedEncryptionPublicKey,
MidnightBech32m,
} from '@midnight-ntwrk/wallet-sdk-address-format';
import type { NetworkId } from '@midnight-ntwrk/wallet-sdk-address-format';
import * as ledger from '@midnight-ntwrk/ledger-v7';
import { randomBytes } from 'node:crypto';
const networkId: NetworkId = 'preview';
// Create shielded keys from a seed
const shieldedSeed = randomBytes(32); // Your 32-byte seed
const shieldedKeys = ledger.ZswapSecretKeys.fromSeed(shieldedSeed);
// Create a shielded address from the keys
const shieldedAddress = new ShieldedAddress(
new ShieldedCoinPublicKey(Buffer.from(shieldedKeys.coinPublicKey, 'hex')),
new ShieldedEncryptionPublicKey(Buffer.from(shieldedKeys.encryptionPublicKey, 'hex')),
);
// Encode to Bech32m string: mn_shield-addr_preview1...
const encoded = MidnightBech32m.encode(networkId, shieldedAddress).toString();
// Parse and decode back to ShieldedAddress
const decoded = MidnightBech32m.parse(encoded).decode(ShieldedAddress, networkId);
// Access individual key components
decoded.coinPublicKeyString();
decoded.encryptionPublicKeyString();
`
For fee token operations on the Midnight network.
`typescript
import { DustAddress, MidnightBech32m } from '@midnight-ntwrk/wallet-sdk-address-format';
import type { NetworkId } from '@midnight-ntwrk/wallet-sdk-address-format';
import * as ledger from '@midnight-ntwrk/ledger-v7';
import { randomBytes } from 'node:crypto';
const networkId: NetworkId = 'preview';
// Create a dust secret key from a seed
const dustSeed = randomBytes(32); // Your 32-byte seed
const dustSecretKey = ledger.DustSecretKey.fromSeed(dustSeed);
// Create a dust address from the public key
const dustAddress = new DustAddress(dustSecretKey.publicKey);
// Encode to Bech32m string: mn_dust_preview1...
const encoded = MidnightBech32m.encode(networkId, dustAddress).toString();
// Parse and decode back to DustAddress
const decoded = MidnightBech32m.parse(encoded).decode(DustAddress, networkId);
`
All Midnight addresses use the Bech32m encoding with the following structure:
``
mn_
- mn - Midnight prefixtype
- - Address type identifier (e.g., shield-addr, addr, dust)network
- - Optional network identifier (omitted for mainnet)data
- - Bech32m-encoded payload
| Type | Bech32m Type | Description |
| ----------------- | ------------- | ---------------------------------------------------------- |
| Shielded | shield-addr | Combined coin + encryption public keys for ZK transactions |shield-cpk
| Coin Public Key | | Shielded coin public key only |shield-epk
| Encryption Key | | Shielded encryption public key only |shield-esk
| Encryption Secret | | Shielded encryption secret key |addr
| Unshielded | | Public address for transparent transactions |dust
| Dust | | Address for fee token operations |
- MidnightBech32m - Main class for parsing and encoding Bech32m addressesBech32mCodec
- - Generic codec for creating custom address types
- ShieldedAddress - Full shielded address with coin and encryption keysShieldedCoinPublicKey
- - 32-byte coin public keyShieldedEncryptionPublicKey
- - 32-byte encryption public keyShieldedEncryptionSecretKey
- - Encryption secret key wrapperUnshieldedAddress
- - 32-byte transparent addressDustAddress
- - Fee token address using BLS scalar
- NetworkId - Type for network identification (mainnet symbol or string)mainnet
- - Symbol representing the mainnet networkFormatContext
- - Context type for encoding/decodingBLSScalar
- - BLS curve scalar field definitionScaleBigInt
- - SCALE codec utilities for bigint encodingBech32mSymbol
- - Symbol for codec attachmentHasCodec
- - Type helper for codec-enabled classesCodecTarget
- - Type helper for extracting codec target typeField` - Type for field definitions
-
Apache-2.0