Wallet Creation Examples
Learn how to create and manage wallets using real-world patterns from the Hydra SDK.
Node.js Example
typescript
import { AppWallet, NETWORK_ID } from '@hydra-sdk/core'
// Create wallet from fresh mnemonic
const createNewWallet = () => {
const wallet = new AppWallet({
networkId: NETWORK_ID.PREPROD,
key: { type: 'mnemonic', words: AppWallet.brew(128) }
})
const account = wallet.getAccount(0, 0)
console.log('New Wallet Created:')
console.log('Base Address:', account.baseAddressBech32)
console.log('Enterprise Address:', account.enterpriseAddressBech32)
console.log('Reward Address:', account.rewardAddressBech32)
return wallet
}
// Restore wallet from existing mnemonic
const restoreWallet = () => {
const mnemonic = 'armed pink solve client dignity alarm earn impose acquire rib eyebrow engage dragon face funny'.split(' ')
const wallet = new AppWallet({
key: { type: 'mnemonic', words: mnemonic },
networkId: NETWORK_ID.PREPROD
})
const account = wallet.getAccount(0, 0)
console.log('Restored Wallet:')
console.log('Base Address:', account.baseAddressBech32)
return wallet
}
// Usage
const wallet = restoreWallet()
Vue.js Example
vue
<script setup lang="ts">
import { ref } from 'vue'
import { AppWallet, NETWORK_ID } from '@hydra-sdk/core'
interface WalletInfo {
mnemonic: string
baseAddress: string
enterpriseAddress: string
rewardAddress: string
paymentKeyHash: string
}
const walletInfo = ref<WalletInfo | null>(null)
const loading = ref(false)
const mnemonicInput = ref('')
const networkId = ref(NETWORK_ID.PREPROD)
const createNewWallet = async () => {
loading.value = true
try {
const mnemonic = AppWallet.brew(128).join(' ')
const wallet = new AppWallet({
key: { type: 'mnemonic', words: AppWallet.brew(128) },
networkId: networkId.value
})
const account = wallet.getAccount(0, 0)
walletInfo.value = {
mnemonic,
baseAddress: account.baseAddressBech32,
enterpriseAddress: account.enterpriseAddressBech32,
rewardAddress: account.rewardAddressBech32,
paymentKeyHash: account.paymentKeyHash
}
} catch (error) {
console.error('Error creating wallet:', error)
} finally {
loading.value = false
}
}
const restoreWallet = async () => {
if (!mnemonicInput.value.trim()) return
loading.value = true
try {
const words = mnemonicInput.value.trim().split(/\s+/)
const wallet = new AppWallet({
key: { type: 'mnemonic', words },
networkId: networkId.value
})
const account = wallet.getAccount(0, 0)
walletInfo.value = {
mnemonic: mnemonicInput.value.trim(),
baseAddress: account.baseAddressBech32,
enterpriseAddress: account.enterpriseAddressBech32,
rewardAddress: account.rewardAddressBech32,
paymentKeyHash: account.paymentKeyHash
}
} catch (error) {
console.error('Error restoring wallet:', error)
} finally {
loading.value = false
}
}
</script>
<template>
<div class="wallet-container">
<h1>Hydra Wallet Creation</h1>
<!-- Controls and wallet info display -->
<pre>{{ walletInfo }}</pre>
</div>
</template>
React Example
tsx
import React, { useState } from 'react';
import { AppWallet, NETWORK_ID } from '@hydra-sdk/core';
interface WalletInfo {
mnemonic: string;
baseAddress: string;
enterpriseAddress: string;
rewardAddress: string;
paymentKeyHash: string;
}
const WalletCreation: React.FC = () => {
const [walletInfo, setWalletInfo] = useState<WalletInfo | null>(null);
const [loading, setLoading] = useState(false);
const [mnemonicInput, setMnemonicInput] = useState('');
const [networkId] = useState(NETWORK_ID.PREPROD);
const createNewWallet = async () => {
setLoading(true);
try {
const words = AppWallet.brew(128);
const mnemonic = words.join(' ');
const wallet = new AppWallet({
key: { type: 'mnemonic', words },
networkId
});
const account = wallet.getAccount(0, 0);
setWalletInfo({
mnemonic,
baseAddress: account.baseAddressBech32,
enterpriseAddress: account.enterpriseAddressBech32,
rewardAddress: account.rewardAddressBech32,
paymentKeyHash: account.paymentKeyHash
});
} catch (error) {
console.error('Error:', error);
} finally {
setLoading(false);
}
};
const restoreWallet = async () => {
if (!mnemonicInput.trim()) return;
setLoading(true);
try {
const words = mnemonicInput.trim().split(/\s+/);
const wallet = new AppWallet({
key: { type: 'mnemonic', words },
networkId
});
const account = wallet.getAccount(0, 0);
setWalletInfo({
mnemonic: mnemonicInput.trim(),
baseAddress: account.baseAddressBech32,
enterpriseAddress: account.enterpriseAddressBech32,
rewardAddress: account.rewardAddressBech32,
paymentKeyHash: account.paymentKeyHash
});
} catch (error) {
console.error('Error:', error);
} finally {
setLoading(false);
}
};
return (
<div>
<h1>Hydra Wallet Creation</h1>
<button onClick={createNewWallet} disabled={loading}>Create New</button>
<textarea
value={mnemonicInput}
onChange={(e) => setMnemonicInput(e.target.value)}
placeholder="Enter mnemonic to restore"
/>
<button onClick={restoreWallet} disabled={loading}>Restore</button>
{walletInfo && <pre>{JSON.stringify(walletInfo, null, 2)}</pre>}
</div>
);
};
export default WalletCreation;
Key Features
- Mnemonic Generation:
AppWallet.brew(128)for 12/24 words - Multiple Key Types: mnemonic, root key, CLI keys
- HD Derivation:
getAccount(accountIndex, keyIndex) - Address Types: base, enterprise, reward addresses
- Network Support: Mainnet/Testnet/Preprod/Preview
- TypeScript First: Full type safety
See API Reference for complete wallet methods.
