npm.io
3.0.2 • Published 3 months ago

@midnightntwrk/wallet-sdk-shielded

Licence
Apache-2.0
Version
3.0.2
Deps
9
Size
116 kB
Vulns
0
Weekly
4.5K
Stars
13

@midnightntwrk/wallet-sdk-shielded

Manages shielded tokens on the Midnight network using zero-knowledge proofs.

Installation

npm install @midnightntwrk/wallet-sdk-shielded

Overview

The Shielded Wallet handles private token operations where transaction values and addresses are hidden from observers while maintaining verifiability. It provides:

  • Zero-knowledge proof generation
  • Coin commitment tracking
  • Encrypted output decryption
  • Shielded transfer transactions
  • Transaction balancing for shielded tokens

Usage

Starting the Wallet
import { ShieldedWallet } from '@midnightntwrk/wallet-sdk-shielded';
import * as ledger from '@midnight-ntwrk/ledger-v7';
import { randomBytes } from 'node:crypto';

// Configuration for the wallet
const configuration = {
  networkId: 'preview',
  provingServerUrl: new URL('http://localhost:6300'),
  relayURL: new URL('ws://localhost:9944'),
  indexerClientConnection: {
    indexerHttpUrl: 'http://localhost:8088/api/v3/graphql',
    indexerWsUrl: 'ws://localhost:8088/api/v3/graphql/ws',
  },
};

// Create secret keys from a shielded seed
const seed = randomBytes(32);
const shieldedSecretKeys = ledger.ZswapSecretKeys.fromSeed(seed);

// Create and start the wallet
const shieldedWallet = ShieldedWallet(configuration).startWithSecretKeys(shieldedSecretKeys);

// Start syncing with the network
await shieldedWallet.start(shieldedSecretKeys);
Alternative: Start with Seed Directly
// Start directly with a shielded seed
const shieldedWallet = ShieldedWallet(configuration).startWithSeed(seed);
await shieldedWallet.start(ledger.ZswapSecretKeys.fromSeed(seed));
Restoring from Serialized State
// Restore a wallet from previously serialized state
const shieldedWallet = ShieldedWallet(configuration).restore(serializedState);
await shieldedWallet.start(shieldedSecretKeys);
Observing State
wallet.state.subscribe((state) => {
  console.log('Progress:', state.state.progress);
  console.log('Coins:', state.state.coins);
});
Creating Transfer Transactions
const tx = await wallet.transferTransaction(shieldedSecretKeys, [
  { type: 'NIGHT', receiverAddress: 'mn_shield-addr1...', amount: 1000n },
]);
Balancing Transactions
const balancingTx = await wallet.balanceTransaction(shieldedSecretKeys, transactionToBalance);
Creating Swap Offers
const swapTx = await wallet.initSwap(
  shieldedSecretKeys,
  { NIGHT: 500n }, // inputs
  [{ type: 'TOKEN_A', receiverAddress: shieldedAddress, amount: 100n }], // outputs
);

Privacy Model

Shielded transactions use zero-knowledge proofs to hide:

  • Transaction amounts
  • Sender addresses
  • Receiver addresses

While still proving:

  • The sender has sufficient balance
  • No tokens are created or destroyed
  • The transaction is valid

Exports

  • ShieldedWallet - Main wallet class
  • ShieldedWalletState - Wallet state type
  • Version 1 exports via @midnightntwrk/wallet-sdk-shielded/v1

License

Apache-2.0