Quick Start
This guide will walk you through creating your first Cardano wallet application using Hydra SDK. In just a few minutes, you'll have a working wallet that can create accounts, build transactions, and connect to Hydra Layer 2.
Step 1: Installation
Install the essential packages:
bash
npm install @hydra-sdk/core @hydra-sdk/bridge @hydra-sdk/transaction
Step 2: Create Your First Wallet
Create a new wallet instance with the core package:
typescript
import { AppWallet, NETWORK_ID } from '@hydra-sdk/core'
// Generate a new mnemonic phrase
const mnemonic = AppWallet.brew()
console.log('Generated mnemonic:', mnemonic.join(' '))
// Create wallet instance
const wallet = new AppWallet({
networkId: NETWORK_ID.PREPROD, // Use PREPROD for testing
key: {
type: 'mnemonic',
words: mnemonic
}
})
// Get the first account
const account = wallet.getAccount(0, 0)
console.log('Wallet Address:', account.baseAddressBech32)
Restore Existing Wallet
To restore a wallet from an existing mnemonic:
typescript
const existingMnemonic = 'your existing mnemonic phrase here'.split(' ')
const restoredWallet = new AppWallet({
networkId: NETWORK_ID.PREPROD,
key: {
type: 'mnemonic',
words: existingMnemonic
}
})
Step 3: Query UTxOs and Build Transactions
First, query UTxOs from your wallet address, then use the transaction builder:
typescript
import { TxBuilder } from '@hydra-sdk/transaction'
import { ProviderUtils } from '@hydra-sdk/core'
// Initialize a provider (Blockfrost or Ogmios)
const blockfrostProvider = new ProviderUtils.BlockfrostProvider({
apiKey: 'your-blockfrost-api-key',
network: 'preprod'
})
// Set provider for wallet
const wallet = new AppWallet({
networkId: NETWORK_ID.PREPROD,
key: {
type: 'mnemonic',
words: mnemonic
},
fetcher: blockfrostProvider.fetcher,
submitter: blockfrostProvider.submitter
})
// Get wallet address
const account = wallet.getAccount(0, 0)
const walletAddress = account.baseAddressBech32
// Query UTxOs from wallet
const utxos = await wallet.queryUTxOs(walletAddress)
console.log('Available UTxOs:', utxos.length)
// Build a simple ADA transfer
const recipientAddress = 'addr_test1...' // Recipient's address
const amountToSend = '1000000' // 1 ADA in lovelace
try {
// Initialize transaction builder
const txBuilder = new TxBuilder()
const tx = await txBuilder
.setInputs(utxos) // Use queried UTxOs as inputs
.addOutput({
address: recipientAddress,
amount: [{ unit: 'lovelace', quantity: amountToSend }]
})
.changeAddress(walletAddress) // Change returns to sender
.complete()
console.log('Transaction built:', tx.to_hex())
// Sign the transaction
const signedTx = await wallet.signTx(tx.to_hex())
console.log('Transaction signed')
// Submit transaction
const txHash = await wallet.submitTx(signedTx)
console.log('Transaction submitted! Hash:', txHash)
} catch (error) {
console.error('Transaction failed:', error)
}
Advanced Transaction Example
Build a transaction with multiple outputs and native tokens:
typescript
import { TxBuilder } from '@hydra-sdk/transaction'
const txBuilder = new TxBuilder()
// Query UTxOs
const utxos = await wallet.queryUTxOs(walletAddress)
const tx = await txBuilder
.setInputs(utxos)
// Send ADA to first recipient
.addOutput({
address: 'addr_test1qr...',
amount: [{ unit: 'lovelace', quantity: '2000000' }]
})
// Send ADA + Native Token to second recipient
.addOutput({
address: 'addr_test1qs...',
amount: [
{ unit: 'lovelace', quantity: '1500000' },
{ unit: 'policyId.assetName', quantity: '100' }
]
})
.changeAddress(walletAddress)
.complete()
const signedTx = await wallet.signTx(tx.to_hex())
const txHash = await wallet.submitTx(signedTx)
console.log('Transaction hash:', txHash)
Step 4: Connect to Hydra Layer 2
Create a Hydra Bridge to connect to Hydra Heads for fast Layer 2 transactions:
typescript
import { HydraBridge, HydraPayload } from '@hydra-sdk/bridge'
// Create Hydra Bridge instance
const bridge = new HydraBridge({
url: 'ws://localhost:4001', // Your Hydra Node WebSocket URL
})
// Listen for connection events
bridge.events.on('onConnected', () => {
console.log('Connected to Hydra Head!')
})
bridge.events.on('onMessage', (payload: HydraPayload) => {
console.log('>>> / onMessage:', payload)
})
// Connect to the Hydra Node
await bridge.connect()
Connect to Hydra Layer 2 using HexcoreConnector
Create a Hydra Bridge to connect to Hydra Heads using HexcoreConnector:
typescript
import { HydraBridge, HexcoreConnector } from '@hydra-sdk/bridge'
const hexcoreConnector = new HexcoreConnector({
socketIoUrl: 'wss://example.hexcore.io.vn/hydra',
socketIoOptions: {
auth: {
token: 'your_auth_token'
}
}
})
// Create Hydra Bridge instance
const bridge = new HydraBridge({
connector: hexcoreConnector
})
// Listen for connection events
bridge.events.on('onConnected', () => {
console.log('Connected to Hydra Head!')
})
bridge.events.on('onMessage', (payload: HydraPayload) => {
console.log('>>> / onMessage:', payload)
})
// Connect to the Hydra Node
await bridge.connect()
Next Steps
Now that you have a working wallet application, explore these advanced topics:
- Configuration - Environment setup and network configuration
- API Reference - Complete API documentation
- Transaction Examples - Advanced transaction patterns
- Hydra Integration - Deep dive into Hydra Layer 2
- Full React App - Complete React application example
- Full Vue.js App - Complete Vue.js application example
Need Help?
- Check the Examples section for more code samples
- Visit our GitHub Repository for issues and discussions
- Examples Repository - Complete working applications
