-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathderivations.rs
More file actions
263 lines (240 loc) · 6.39 KB
/
derivations.rs
File metadata and controls
263 lines (240 loc) · 6.39 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
use chia::{bls::PublicKey, protocol::Bytes32};
use sqlx::SqliteExecutor;
use crate::{
into_row, to_bytes, to_bytes32, Database, DatabaseTx, DerivationRow, DerivationSql, Result,
};
#[derive(Debug, Clone, Copy)]
pub struct SyntheticKeyInfo {
pub index: u32,
pub hardened: bool,
}
impl Database {
pub async fn derivations(
&self,
hardened: bool,
limit: u32,
offset: u32,
) -> Result<Vec<DerivationRow>> {
derivations(&self.pool, hardened, limit, offset).await
}
pub async fn p2_puzzle_hashes(&self) -> Result<Vec<Bytes32>> {
p2_puzzle_hashes(&self.pool).await
}
pub async fn synthetic_key(&self, p2_puzzle_hash: Bytes32) -> Result<PublicKey> {
synthetic_key(&self.pool, p2_puzzle_hash).await
}
pub async fn synthetic_key_info(
&self,
synthetic_key: PublicKey,
) -> Result<Option<SyntheticKeyInfo>> {
synthetic_key_info(&self.pool, synthetic_key).await
}
pub async fn is_p2_puzzle_hash(&self, p2_puzzle_hash: Bytes32) -> Result<bool> {
is_p2_puzzle_hash(&self.pool, p2_puzzle_hash).await
}
}
impl<'a> DatabaseTx<'a> {
pub async fn insert_derivation(
&mut self,
p2_puzzle_hash: Bytes32,
index: u32,
hardened: bool,
synthetic_key: PublicKey,
) -> Result<()> {
insert_derivation(
&mut *self.tx,
p2_puzzle_hash,
index,
hardened,
synthetic_key,
)
.await
}
pub async fn derivation_index(&mut self, hardened: bool) -> Result<u32> {
derivation_index(&mut *self.tx, hardened).await
}
pub async fn max_used_derivation_index(&mut self, hardened: bool) -> Result<Option<u32>> {
max_used_derivation_index(&mut *self.tx, hardened).await
}
pub async fn synthetic_key(&mut self, p2_puzzle_hash: Bytes32) -> Result<PublicKey> {
synthetic_key(&mut *self.tx, p2_puzzle_hash).await
}
pub async fn p2_puzzle_hash(&mut self, index: u32, hardened: bool) -> Result<Bytes32> {
p2_puzzle_hash(&mut *self.tx, index, hardened).await
}
pub async fn is_p2_puzzle_hash(&mut self, p2_puzzle_hash: Bytes32) -> Result<bool> {
is_p2_puzzle_hash(&mut *self.tx, p2_puzzle_hash).await
}
}
async fn insert_derivation(
conn: impl SqliteExecutor<'_>,
p2_puzzle_hash: Bytes32,
index: u32,
hardened: bool,
synthetic_key: PublicKey,
) -> Result<()> {
let p2_puzzle_hash = p2_puzzle_hash.as_ref();
let synthetic_key = synthetic_key.to_bytes();
let synthetic_key_ref = synthetic_key.as_ref();
sqlx::query!(
"
INSERT OR IGNORE INTO `derivations` (`p2_puzzle_hash`, `index`, `hardened`, `synthetic_key`)
VALUES (?, ?, ?, ?)
",
p2_puzzle_hash,
index,
hardened,
synthetic_key_ref
)
.execute(conn)
.await?;
Ok(())
}
async fn derivation_index(conn: impl SqliteExecutor<'_>, hardened: bool) -> Result<u32> {
Ok(sqlx::query!(
"
SELECT MAX(`index`) AS `max_index`
FROM `derivations`
WHERE `hardened` = ?
",
hardened
)
.fetch_one(conn)
.await?
.max_index
.map_or(0, |index| index + 1)
.try_into()?)
}
async fn max_used_derivation_index(
conn: impl SqliteExecutor<'_>,
hardened: bool,
) -> Result<Option<u32>> {
let row = sqlx::query!(
"
SELECT MAX(`index`) AS `max_index`
FROM `derivations`
WHERE EXISTS (
SELECT 1 FROM `coin_states`
WHERE `puzzle_hash` = `p2_puzzle_hash`
OR `hint` = `p2_puzzle_hash`
)
AND `hardened` = ?
",
hardened
)
.fetch_one(conn)
.await?;
Ok(row.max_index.map(TryInto::try_into).transpose()?)
}
async fn p2_puzzle_hashes(conn: impl SqliteExecutor<'_>) -> Result<Vec<Bytes32>> {
let rows = sqlx::query!(
"
SELECT `p2_puzzle_hash`
FROM `derivations`
ORDER BY `index` ASC, `hardened` ASC
"
)
.fetch_all(conn)
.await?;
rows.into_iter()
.map(|row| to_bytes32(&row.p2_puzzle_hash))
.collect::<Result<_>>()
}
async fn derivations(
conn: impl SqliteExecutor<'_>,
hardened: bool,
limit: u32,
offset: u32,
) -> Result<Vec<DerivationRow>> {
sqlx::query_as!(
DerivationSql,
"
SELECT * FROM `derivations`
WHERE `hardened` = ?
ORDER BY `index` ASC
LIMIT ? OFFSET ?
",
hardened,
limit,
offset
)
.fetch_all(conn)
.await?
.into_iter()
.map(into_row)
.collect()
}
async fn synthetic_key(
conn: impl SqliteExecutor<'_>,
p2_puzzle_hash: Bytes32,
) -> Result<PublicKey> {
let p2_puzzle_hash = p2_puzzle_hash.as_ref();
let row = sqlx::query!(
"
SELECT `synthetic_key`
FROM `derivations`
WHERE `p2_puzzle_hash` = ?
",
p2_puzzle_hash
)
.fetch_one(conn)
.await?;
let bytes = row.synthetic_key.as_slice();
Ok(PublicKey::from_bytes(&to_bytes(bytes)?)?)
}
async fn synthetic_key_info(
conn: impl SqliteExecutor<'_>,
synthetic_key: PublicKey,
) -> Result<Option<SyntheticKeyInfo>> {
let synthetic_key = synthetic_key.to_bytes();
let synthetic_key_ref = synthetic_key.as_ref();
sqlx::query!(
"
SELECT `index`, `hardened`
FROM `derivations`
WHERE `synthetic_key` = ?
",
synthetic_key_ref
)
.fetch_optional(conn)
.await?
.map(|row| {
Ok(SyntheticKeyInfo {
index: row.index.try_into()?,
hardened: row.hardened,
})
})
.transpose()
}
async fn p2_puzzle_hash(
conn: impl SqliteExecutor<'_>,
index: u32,
hardened: bool,
) -> Result<Bytes32> {
let row = sqlx::query!(
"
SELECT `p2_puzzle_hash`
FROM `derivations`
WHERE `index` = ?
AND `hardened` = ?
",
index,
hardened
)
.fetch_one(conn)
.await?;
to_bytes32(&row.p2_puzzle_hash)
}
async fn is_p2_puzzle_hash(conn: impl SqliteExecutor<'_>, p2_puzzle_hash: Bytes32) -> Result<bool> {
let p2_puzzle_hash = p2_puzzle_hash.as_ref();
Ok(sqlx::query!(
"
SELECT COUNT(*) AS `count` FROM `derivations` WHERE `p2_puzzle_hash` = ?
",
p2_puzzle_hash
)
.fetch_one(conn)
.await?
.count
> 0)
}