Line data Source code
1 : import 'package:collection/collection.dart';
2 : import 'package:walletkit_dart/src/crypto/network_type.dart';
3 :
4 : const NS_PURPOSE = "m/0'";
5 : const BIP44_PURPOSE = "m/44'";
6 : const BIP49_PURPOSE = "m/49'";
7 : const BIP84_PURPOSE = "m/84'";
8 :
9 : enum HDWalletPurpose {
10 : NO_STRUCTURE("m/0'"), // (P2PKH)
11 :
12 : BIP44("m/44'"), // (P2PKH)
13 :
14 : BIP49("m/49'"), // (P2SH)
15 :
16 : BIP84("m/84'"); // (P2WPKH)
17 :
18 : final String string;
19 :
20 : const HDWalletPurpose(this.string);
21 :
22 0 : static HDWalletPurpose fromString(String purpose) {
23 : switch (purpose) {
24 0 : case "m/0'":
25 : return NO_STRUCTURE;
26 0 : case "m/44'":
27 : return BIP44;
28 0 : case "m/49'":
29 : return BIP49;
30 0 : case "m/84'":
31 : return BIP84;
32 : default:
33 0 : throw ArgumentError("Invalid purpose: $purpose");
34 : }
35 : }
36 : }
37 :
38 0 : final supportedPaths = [
39 : bitcoinNSHDPath,
40 : bitcoinBip44HDPath,
41 : bitcoinBip49HDPath,
42 : bitcoinBip84HDPath,
43 : tronBip44HDPath,
44 : litecoinBip44HDPath,
45 : ethereumBip44HDPath,
46 : ];
47 :
48 : sealed class HDWalletPath {
49 : final HDWalletPurpose purpose;
50 : final String coinType;
51 :
52 38 : const HDWalletPath(this.purpose, this.coinType);
53 :
54 0 : static HDWalletPath? fromBasePath(String basePath) {
55 0 : return supportedPaths.singleWhereOrNull(
56 0 : (hdPath) => hdPath.basePath == basePath,
57 : );
58 : }
59 :
60 14 : String get basePath {
61 56 : return "${purpose.string}/$coinType";
62 : }
63 :
64 2 : String get account0Path {
65 4 : return "$basePath/0'";
66 : }
67 :
68 4 : String get defaultPath {
69 4 : return getPath(0, 0, 0);
70 : }
71 :
72 0 : String getWithAccount(int account) {
73 0 : return "$basePath/$account'";
74 : }
75 :
76 4 : String getPath(int account, int change, int index) {
77 8 : return "$basePath/$account'/$change/$index";
78 : }
79 :
80 1 : factory HDWalletPath.fromPurpose(
81 : HDWalletPurpose purpose,
82 : UTXONetworkType network,
83 : ) {
84 2 : final coinType = "${network.coinType}'";
85 : return switch (purpose) {
86 2 : HDWalletPurpose.NO_STRUCTURE => NSHDWalletPath(coinType),
87 2 : HDWalletPurpose.BIP44 => Bip44HDWalletPath(coinType),
88 0 : HDWalletPurpose.BIP49 => Bip49HDWalletPath(coinType),
89 0 : HDWalletPurpose.BIP84 => Bip84HDWalletPath(coinType),
90 : };
91 : }
92 : }
93 :
94 : final class NSHDWalletPath extends HDWalletPath {
95 : final String coinType;
96 :
97 38 : const NSHDWalletPath(this.coinType)
98 1 : : super(HDWalletPurpose.NO_STRUCTURE, coinType);
99 : }
100 :
101 : final class Bip44HDWalletPath extends HDWalletPath {
102 : final String coinType;
103 :
104 38 : const Bip44HDWalletPath(this.coinType)
105 1 : : super(HDWalletPurpose.BIP44, coinType);
106 : }
107 :
108 : final class Bip49HDWalletPath extends HDWalletPath {
109 : final String coinType;
110 :
111 37 : const Bip49HDWalletPath(this.coinType)
112 0 : : super(HDWalletPurpose.BIP49, coinType);
113 : }
114 :
115 : final class Bip84HDWalletPath extends HDWalletPath {
116 : final String coinType;
117 :
118 37 : const Bip84HDWalletPath(this.coinType)
119 0 : : super(HDWalletPurpose.BIP84, coinType);
120 : }
121 :
122 : ///
123 : /// No Structure HD Wallet Paths
124 : ///
125 : const bitcoinNSHDPath = BitcoinNSHDPath();
126 :
127 : final class BitcoinNSHDPath extends NSHDWalletPath {
128 37 : const BitcoinNSHDPath() : super("0'");
129 : }
130 :
131 : ///
132 : /// BIP44 HD Wallet Paths
133 : ///
134 :
135 : const bitcoinBip44HDPath = BitcoinBip44HDPath();
136 :
137 : final class BitcoinBip44HDPath extends Bip44HDWalletPath {
138 37 : const BitcoinBip44HDPath() : super("0'");
139 : }
140 :
141 : const tronBip44HDPath = TronBip44HDPath();
142 :
143 : final class TronBip44HDPath extends Bip44HDWalletPath {
144 37 : const TronBip44HDPath() : super("195'");
145 : }
146 :
147 : const litecoinBip44HDPath = LitecoinBip44HDPath();
148 :
149 : final class LitecoinBip44HDPath extends Bip44HDWalletPath {
150 37 : const LitecoinBip44HDPath() : super("2'");
151 : }
152 :
153 : const ethereumBip44HDPath = EthereumBip44HDPath();
154 :
155 : final class EthereumBip44HDPath extends Bip44HDWalletPath {
156 37 : const EthereumBip44HDPath() : super("60'");
157 : }
158 :
159 : ///
160 : /// BIP49 HD Wallet Paths
161 : ///
162 :
163 : const bitcoinBip49HDPath = BitcoinBip49HDPath();
164 :
165 : final class BitcoinBip49HDPath extends Bip49HDWalletPath {
166 37 : const BitcoinBip49HDPath() : super("0'");
167 : }
168 :
169 : ///
170 : /// BIP84 HD Wallet Paths
171 : ///
172 :
173 : const bitcoinBip84HDPath = BitcoinBip84HDPath();
174 :
175 : final class BitcoinBip84HDPath extends Bip84HDWalletPath {
176 37 : const BitcoinBip84HDPath() : super("0'");
177 : }
|