-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHDWallet.kt
More file actions
175 lines (139 loc) · 5.37 KB
/
Copy pathHDWallet.kt
File metadata and controls
175 lines (139 loc) · 5.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package examples
import com.mazekine.nekoton.crypto.*
import com.mazekine.nekoton.models.*
/**
* Hierarchical Deterministic (HD) Wallet example.
*
* Demonstrates:
* - BIP39 mnemonic generation
* - HD key derivation
* - Multiple account management
* - Seed phrase backup and recovery
*/
class HDWallet {
fun demonstrateHDWallet() {
println("=== HD Wallet Operations ===")
// 1. Generate new mnemonic
val seed = generateNewWallet()
// 2. Derive multiple accounts
val accounts = deriveAccounts(seed, 5)
// 3. Display account information
displayAccounts(accounts)
// 4. Demonstrate recovery
demonstrateRecovery(seed.phrase)
// 5. Show different derivation paths
demonstrateDifferentPaths(seed)
}
private fun generateNewWallet(): Seed {
println("\n--- Generating New HD Wallet ---")
// Generate 12-word mnemonic (can also use 15, 18, 21, 24)
val seed = Seed.generate(12)
println("Generated mnemonic:")
println("\"${seed.phrase}\"")
println("\n⚠️ IMPORTANT: Store this mnemonic securely!")
println("⚠️ Anyone with this phrase can access your funds!")
return seed
}
private fun deriveAccounts(seed: Seed, count: Int): List<Account> {
println("\n--- Deriving Accounts ---")
val accounts = mutableListOf<Account>()
repeat(count) { index ->
// Standard derivation path: m/44'/396'/0'/0/{index}
// 396 is the coin type for TON/Everscale
val derivationPath = "m/44'/396'/0'/0/$index"
val keyPair = seed.deriveKeyPair(derivationPath)
val address = Address.fromPublicKey(keyPair.publicKey, 0)
accounts.add(Account(
index = index,
derivationPath = derivationPath,
keyPair = keyPair,
address = address
))
}
return accounts
}
private fun displayAccounts(accounts: List<Account>) {
println("\n--- Account Information ---")
accounts.forEach { account ->
println("Account ${account.index}:")
println(" Path: ${account.derivationPath}")
println(" Address: ${account.address}")
println(" Public Key: ${account.keyPair.publicKey.toHex()}")
println()
}
}
private fun demonstrateRecovery(originalPhrase: String) {
println("--- Wallet Recovery ---")
try {
// Recover wallet from mnemonic
val recoveredSeed = Seed.fromPhrase(originalPhrase)
// Derive the same first account
val recoveredKeyPair = recoveredSeed.deriveKeyPair("m/44'/396'/0'/0/0")
val recoveredAddress = Address.fromPublicKey(recoveredKeyPair.publicKey, 0)
println("Wallet recovered successfully!")
println("First account address: $recoveredAddress")
} catch (e: Exception) {
println("Recovery failed: ${e.message}")
}
}
private fun demonstrateDifferentPaths(seed: Seed) {
println("--- Different Derivation Paths ---")
val paths = listOf(
"m/44'/396'/0'/0/0" to "Standard account 0",
"m/44'/396'/1'/0/0" to "Second wallet, account 0",
"m/44'/396'/0'/1/0" to "Change address 0",
"m/44'/60'/0'/0/0" to "Ethereum-style path",
"m/0'/0'/0'" to "Legacy path"
)
paths.forEach { (path, description) ->
try {
val keyPair = seed.deriveKeyPair(path)
val address = Address.fromPublicKey(keyPair.publicKey, 0)
println("$description:")
println(" Path: $path")
println(" Address: $address")
println()
} catch (e: Exception) {
println("Failed to derive $path: ${e.message}")
}
}
}
fun demonstrateMultipleWallets() {
println("\n=== Multiple Wallet Management ===")
// Create multiple independent wallets
val wallets = (1..3).map { index ->
val seed = Seed.generate(12)
val masterKeyPair = seed.deriveKeyPair("m/44'/396'/0'/0/0")
val address = Address.fromPublicKey(masterKeyPair.publicKey, 0)
WalletInfo(
name = "Wallet $index",
seed = seed,
masterKeyPair = masterKeyPair,
address = address
)
}
wallets.forEach { wallet ->
println("${wallet.name}:")
println(" Mnemonic: ${wallet.seed.phrase}")
println(" Address: ${wallet.address}")
println()
}
}
data class Account(
val index: Int,
val derivationPath: String,
val keyPair: KeyPair,
val address: Address
)
data class WalletInfo(
val name: String,
val seed: Seed,
val masterKeyPair: KeyPair,
val address: Address
)
}
fun main() {
val hdWallet = HDWallet()
hdWallet.demonstrateHDWallet()
hdWallet.demonstrateMultipleWallets()
}