@midnight-ntwrk/wallet-sdk-address-format
v3.0.1
Published
Bech32m address encoding and decoding for the Midnight network.
Downloads
8,718
Readme
@midnight-ntwrk/wallet-sdk-address-format
Bech32m address encoding and decoding for the Midnight network.
Installation
npm install @midnight-ntwrk/wallet-sdk-address-formatOverview
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
Usage
Unshielded Address
For transparent transactions on the Midnight network.
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();Shielded Address
For zero-knowledge transactions on the Midnight network.
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();Dust Address
For fee token operations on the Midnight network.
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);Address Format
All Midnight addresses use the Bech32m encoding with the following structure:
mn_<type>[_<network>]1<data>mn- Midnight prefixtype- Address type identifier (e.g.,shield-addr,addr,dust)network- Optional network identifier (omitted for mainnet)data- Bech32m-encoded payload
Address Types
| Type | Bech32m Type | Description |
| ----------------- | ------------- | ---------------------------------------------------------- |
| Shielded | shield-addr | Combined coin + encryption public keys for ZK transactions |
| Coin Public Key | shield-cpk | Shielded coin public key only |
| Encryption Key | shield-epk | Shielded encryption public key only |
| Encryption Secret | shield-esk | Shielded encryption secret key |
| Unshielded | addr | Public address for transparent transactions |
| Dust | dust | Address for fee token operations |
Exports
Core Classes
MidnightBech32m- Main class for parsing and encoding Bech32m addressesBech32mCodec- Generic codec for creating custom address types
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
Types and Constants
NetworkId- Type for network identification (mainnetsymbol 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
License
Apache-2.0
