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 40 : 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 15 : String get basePath {
61 60 : 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 5 : String getPath(int account, int change, int index) {
77 10 : 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 41 : const NSHDWalletPath(this.coinType) : super(HDWalletPurpose.NO_STRUCTURE, coinType);
98 : }
99 :
100 : final class Bip44HDWalletPath extends HDWalletPath {
101 : final String coinType;
102 :
103 41 : const Bip44HDWalletPath(this.coinType) : super(HDWalletPurpose.BIP44, coinType);
104 : }
105 :
106 : final class Bip49HDWalletPath extends HDWalletPath {
107 : final String coinType;
108 :
109 39 : const Bip49HDWalletPath(this.coinType) : super(HDWalletPurpose.BIP49, coinType);
110 : }
111 :
112 : final class Bip84HDWalletPath extends HDWalletPath {
113 : final String coinType;
114 :
115 39 : const Bip84HDWalletPath(this.coinType) : super(HDWalletPurpose.BIP84, coinType);
116 : }
117 :
118 : ///
119 : /// No Structure HD Wallet Paths
120 : ///
121 : const bitcoinNSHDPath = BitcoinNSHDPath();
122 :
123 : final class BitcoinNSHDPath extends NSHDWalletPath {
124 39 : const BitcoinNSHDPath() : super("0'");
125 : }
126 :
127 : ///
128 : /// BIP44 HD Wallet Paths
129 : ///
130 :
131 : const bitcoinBip44HDPath = BitcoinBip44HDPath();
132 :
133 : final class BitcoinBip44HDPath extends Bip44HDWalletPath {
134 39 : const BitcoinBip44HDPath() : super("0'");
135 : }
136 :
137 : const tronBip44HDPath = TronBip44HDPath();
138 :
139 : final class TronBip44HDPath extends Bip44HDWalletPath {
140 39 : const TronBip44HDPath() : super("195'");
141 : }
142 :
143 : const litecoinBip44HDPath = LitecoinBip44HDPath();
144 :
145 : final class LitecoinBip44HDPath extends Bip44HDWalletPath {
146 39 : const LitecoinBip44HDPath() : super("2'");
147 : }
148 :
149 : const ethereumBip44HDPath = EthereumBip44HDPath();
150 :
151 : final class EthereumBip44HDPath extends Bip44HDWalletPath {
152 39 : const EthereumBip44HDPath() : super("60'");
153 : }
154 :
155 : ///
156 : /// BIP49 HD Wallet Paths
157 : ///
158 :
159 : const bitcoinBip49HDPath = BitcoinBip49HDPath();
160 :
161 : final class BitcoinBip49HDPath extends Bip49HDWalletPath {
162 39 : const BitcoinBip49HDPath() : super("0'");
163 : }
164 :
165 : ///
166 : /// BIP84 HD Wallet Paths
167 : ///
168 :
169 : const bitcoinBip84HDPath = BitcoinBip84HDPath();
170 :
171 : final class BitcoinBip84HDPath extends Bip84HDWalletPath {
172 39 : const BitcoinBip84HDPath() : super("0'");
173 : }
|