bitcoin_key/keypool.rs
1/**
2 | अनित्य अनात्मन् मोक्ष
3 |
4 */
5
6crate::ix!();
7
8//-------------------------------------------[.cpp/bitcoin/src/wallet/scriptpubkeyman.h]
9
10/**
11 | Default for -keypool
12 |
13 */
14pub const DEFAULT_KEYPOOL_SIZE: u32 = 1000;
15
16/**
17 | A key from a CWallet's keypool
18 |
19 | The wallet holds one (for pre HD-split wallets)
20 | or several keypools. These are sets of keys
21 | that have not yet been used to provide
22 | addresses or receive change.
23 |
24 | The Bitcoin Core wallet was originally
25 | a collection of unrelated private keys with
26 | their associated addresses. If a non-HD wallet
27 | generated a key/address, gave that address out
28 | and then restored a backup from before that
29 | key's generation, then any funds sent to that
30 | address would be lost definitively.
31 |
32 | The keypool was implemented to avoid this
33 | scenario (commit: 10384941). The wallet would
34 | generate a set of keys (100 by default). When
35 | a new public key was required, either to give
36 | out as an address or to use in a change output,
37 | it would be drawn from the keypool. The keypool
38 | would then be topped up to maintain 100
39 | keys. This ensured that as long as the wallet
40 | hadn't used more than 100 keys since the
41 | previous backup, all funds would be safe, since
42 | a restored wallet would be able to scan for all
43 | owned addresses.
44 |
45 | A keypool also allowed encrypted wallets to
46 | give out addresses without having to be
47 | decrypted to generate a new private key.
48 |
49 | With the introduction of HD wallets (commit:
50 | f1902510), the keypool essentially became an
51 | address look-ahead pool. Restoring old backups
52 | can no longer definitively lose funds as long
53 | as the addresses used were from the wallet's HD
54 | seed (since all private keys can be rederived
55 | from the seed). However, if many addresses
56 | were used since the backup, then the wallet may
57 | not know how far ahead in the HD chain to look
58 | for its addresses. The keypool is used to
59 | implement a 'gap limit'. The keypool maintains
60 | a set of keys (by default 1000) ahead of the
61 | last used key and scans for the addresses of
62 | those keys. This avoids the risk of not seeing
63 | transactions involving the wallet's addresses,
64 | or of re-using the same address. In the
65 | unlikely case where none of the addresses in
66 | the `gap limit` are used on-chain, the
67 | look-ahead will not be incremented to keep
68 | a constant size and addresses beyond this range
69 | will not be detected by an old backup. For this
70 | reason, it is not recommended to decrease
71 | keypool size lower than default value.
72 |
73 | The HD-split wallet feature added a second
74 | keypool (commit: 02592f4c). There is an
75 | external keypool (for addresses to hand out)
76 | and an internal keypool (for change addresses).
77 |
78 | Keypool keys are stored in the
79 | wallet/keystore's keymap. The keypool data is
80 | stored as sets of indexes in the wallet
81 | (setInternalKeyPool, setExternalKeyPool and
82 | set_pre_split_keypool), and a map from the key
83 | to the index (m_pool_key_to_index). The
84 | CKeyPool object is used to
85 | serialize/deserialize the pool data to/from the
86 | database.
87 */
88pub struct KeyPool {
89
90 /**
91 | The time at which the key was generated.
92 | Set in AddKeypoolPubKeyWithDB
93 |
94 */
95 n_time: i64,
96
97 /**
98 | The public key
99 |
100 */
101 vch_pub_key: PubKey,
102
103 /**
104 | Whether this keypool entry is in the
105 | internal keypool (for change outputs)
106 |
107 */
108 internal: bool,
109
110 /**
111 | Whether this key was generated for a
112 | keypool before the wallet was upgraded
113 | to HD-split
114 |
115 */
116 pre_split: bool,
117}
118
119impl KeyPool {
120
121 pub fn new(
122 vch_pub_key_in: &PubKey,
123 internal_in: bool) -> Self {
124
125 todo!();
126 /*
127
128
129 */
130 }
131
132 pub fn serialize<Stream>(&self, s: &mut Stream) {
133
134 todo!();
135 /*
136 int nVersion = s.GetVersion();
137 if (!(s.GetType() & SER_GETHASH)) {
138 s << nVersion;
139 }
140 s << nTime << vchPubKey << fInternal << m_pre_split;
141 */
142 }
143
144 pub fn unserialize<Stream>(&mut self, s: &mut Stream) {
145
146 todo!();
147 /*
148 int nVersion = s.GetVersion();
149 if (!(s.GetType() & SER_GETHASH)) {
150 s >> nVersion;
151 }
152 s >> nTime >> vchPubKey;
153 try {
154 s >> fInternal;
155 } catch (std::ios_base::failure&) {
156 /* flag as external address if we can't read the internal boolean
157 (this will be the case for any wallet before the HD chain split version) */
158 fInternal = false;
159 }
160 try {
161 s >> m_pre_split;
162 } catch (std::ios_base::failure&) {
163 /* flag as postsplit address if we can't read the m_pre_split boolean
164 (this will be the case for any wallet that upgrades to HD chain split) */
165 m_pre_split = false;
166 }
167 */
168 }
169}