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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
//! Error types for the BSV SDK.
use thiserror::Error;
/// Main error type for the BSV SDK.
#[derive(Error, Debug, Clone, PartialEq, Eq)]
pub enum Error {
// ===================
// Primitives errors
// ===================
/// Invalid key length for cryptographic operations.
#[error("invalid key length: expected {expected}, got {actual}")]
InvalidKeyLength {
/// The length the operation requires, in bytes.
expected: usize,
/// The length supplied, in bytes.
actual: usize,
},
/// Invalid data length for cryptographic operations.
#[error("invalid data length: expected {expected}, got {actual}")]
InvalidDataLength {
/// The length the operation requires, in bytes.
expected: usize,
/// The length supplied, in bytes.
actual: usize,
},
/// Invalid hex string.
#[error("invalid hex string: {0}")]
InvalidHex(String),
/// Invalid base58 string.
#[error("invalid base58 string: {0}")]
InvalidBase58(String),
/// Invalid base64 string.
#[error("invalid base64 string: {0}")]
InvalidBase64(String),
/// Cryptographic operation failed.
#[error("cryptographic operation failed: {0}")]
CryptoError(String),
/// Invalid signature.
#[error("invalid signature: {0}")]
InvalidSignature(String),
/// Invalid public key.
#[error("invalid public key: {0}")]
InvalidPublicKey(String),
/// Invalid private key.
#[error("invalid private key: {0}")]
InvalidPrivateKey(String),
/// Point at infinity (invalid for most operations).
#[error("point at infinity")]
PointAtInfinity,
/// Decryption failed.
#[error("decryption failed")]
DecryptionFailed,
/// Invalid nonce.
#[error("invalid nonce: {0}")]
InvalidNonce(String),
/// Invalid tag (for authenticated encryption).
#[error("invalid authentication tag")]
InvalidTag,
/// Invalid UTF-8 sequence.
#[error("invalid UTF-8 sequence: {0}")]
InvalidUtf8(String),
/// Reader underflow (not enough bytes to read).
#[error("reader underflow: need {needed} bytes, only {available} available")]
ReaderUnderflow {
/// The bytes the read asked for.
needed: usize,
/// The bytes left in the reader.
available: usize,
},
/// A varint using more bytes than its value requires. Width includes the marker.
#[error("non-canonical varint: {value} encoded in {width} bytes")]
NonCanonicalVarInt {
/// The encoded width in bytes, the marker included.
width: u8,
/// The value the varint carries.
value: u64,
},
/// A node CompactSize above `MAX_COMPACT_SIZE`.
#[error("compact size too large: {0}")]
CompactSizeTooLarge(u64),
/// Invalid checksum.
#[error("invalid checksum")]
InvalidChecksum,
// ===================
// Script errors (Phase S1+)
// ===================
/// Script parse error.
#[cfg(feature = "script")]
#[error("script parse error: {0}")]
ScriptParseError(String),
/// Script execution error.
#[cfg(feature = "script")]
#[error("script execution error: {0}")]
ScriptExecutionError(String),
/// Invalid opcode.
#[cfg(feature = "script")]
#[error("invalid opcode: 0x{0:02x}")]
InvalidOpcode(u8),
/// Disabled opcode.
#[cfg(feature = "script")]
#[error("disabled opcode: 0x{0:02x}")]
DisabledOpcode(u8),
/// Stack underflow.
#[cfg(feature = "script")]
#[error("stack underflow")]
StackUnderflow,
/// Stack overflow.
#[cfg(feature = "script")]
#[error("stack overflow")]
StackOverflow,
/// BIP-276 encoding/decoding error.
#[cfg(feature = "script")]
#[error("BIP-276 error: {0}")]
Bip276Error(String),
/// Invalid address.
#[cfg(feature = "script")]
#[error("invalid address: {0}")]
InvalidAddress(String),
/// Invalid address length.
#[cfg(feature = "script")]
#[error("invalid address length for '{0}'")]
InvalidAddressLength(String),
/// Unsupported address type.
#[cfg(feature = "script")]
#[error("address not supported {0}")]
UnsupportedAddress(String),
// ===================
// Transaction errors
// ===================
/// Transaction error.
#[cfg(feature = "transaction")]
#[error("transaction error: {0}")]
TransactionError(String),
/// MerklePath error.
#[cfg(feature = "transaction")]
#[error("merkle path error: {0}")]
MerklePathError(String),
/// BEEF format error.
#[cfg(feature = "transaction")]
#[error("BEEF error: {0}")]
BeefError(String),
/// Fee model error.
#[cfg(feature = "transaction")]
#[error("fee model error: {0}")]
FeeModelError(String),
// ===================
// Wallet errors
// ===================
/// General wallet error.
#[cfg(feature = "wallet")]
#[error("wallet error: {0}")]
WalletError(String),
/// Key derivation error.
#[cfg(feature = "wallet")]
#[error("key derivation error: {0}")]
KeyDerivationError(String),
/// Protocol validation error.
#[cfg(feature = "wallet")]
#[error("protocol validation error: {0}")]
ProtocolValidationError(String),
/// Invalid counterparty.
#[cfg(feature = "wallet")]
#[error("invalid counterparty: {0}")]
InvalidCounterparty(String),
// ===================
// Messages errors
// ===================
/// Message version mismatch.
#[cfg(feature = "messages")]
#[error("message version mismatch: expected {expected}, got {actual}")]
MessageVersionMismatch {
/// The version prefix this crate reads, as hex.
expected: String,
/// The version prefix the message carries, as hex.
actual: String,
},
/// General message error.
#[cfg(feature = "messages")]
#[error("message error: {0}")]
MessageError(String),
/// Message recipient mismatch.
#[cfg(feature = "messages")]
#[error("message recipient mismatch: expected {expected}, got {actual}")]
MessageRecipientMismatch {
/// The recipient public key the message names, compressed, as hex.
expected: String,
/// The public key of the private key supplied to read it, compressed, as hex.
actual: String,
},
// ===================
// Compat errors
// ===================
/// Invalid mnemonic phrase.
#[cfg(feature = "compat")]
#[error("invalid mnemonic: {0}")]
InvalidMnemonic(String),
/// Invalid entropy length for mnemonic generation.
#[cfg(feature = "compat")]
#[error("invalid entropy length: expected {expected}, got {actual}")]
InvalidEntropyLength {
/// The entropy lengths accepted, in bits.
expected: String,
/// The entropy length supplied, in bits.
actual: usize,
},
/// Invalid word in mnemonic phrase.
#[cfg(feature = "compat")]
#[error("invalid word in mnemonic: {0}")]
InvalidMnemonicWord(String),
/// Invalid extended key.
#[cfg(feature = "compat")]
#[error("invalid extended key: {0}")]
InvalidExtendedKey(String),
/// Cannot derive hardened child from public key.
#[cfg(feature = "compat")]
#[error("cannot derive hardened child from public key")]
HardenedFromPublic,
/// Invalid derivation path.
#[cfg(feature = "compat")]
#[error("invalid derivation path: {0}")]
InvalidDerivationPath(String),
/// ECIES decryption failed.
#[cfg(feature = "compat")]
#[error("ECIES decryption failed: {0}")]
EciesDecryptionFailed(String),
/// ECIES HMAC verification failed.
#[cfg(feature = "compat")]
#[error("ECIES HMAC verification failed")]
EciesHmacMismatch,
// ===================
// Auth errors
// ===================
/// General authentication error.
#[cfg(feature = "auth")]
#[error("authentication error: {0}")]
AuthError(String),
/// Session not found.
#[cfg(feature = "auth")]
#[error("session not found: {0}")]
SessionNotFound(String),
/// Certificate validation failed.
#[cfg(feature = "auth")]
#[error("certificate validation failed: {0}")]
CertificateValidationError(String),
/// Transport error.
#[cfg(feature = "auth")]
#[error("transport error: {0}")]
TransportError(String),
// ===================
// Overlay errors
// ===================
/// General overlay error.
#[cfg(feature = "overlay")]
#[error("overlay error: {0}")]
OverlayError(String),
/// No hosts found for service.
#[cfg(feature = "overlay")]
#[error("no hosts found for service: {0}")]
NoHostsFound(String),
/// Broadcast to overlay failed.
#[cfg(feature = "overlay")]
#[error("overlay broadcast failed: {0}")]
OverlayBroadcastFailed(String),
// ===================
// Registry errors
// ===================
/// General registry error.
#[cfg(feature = "registry")]
#[error("registry error: {0}")]
RegistryError(String),
/// Definition not found in registry.
#[cfg(feature = "registry")]
#[error("definition not found: {0}")]
DefinitionNotFound(String),
/// Invalid definition data.
#[cfg(feature = "registry")]
#[error("invalid definition data: {0}")]
InvalidDefinitionData(String),
// ===================
// KVStore errors
// ===================
/// General kvstore error.
#[cfg(feature = "kvstore")]
#[error("kvstore error: {0}")]
KvStoreError(String),
/// Key not found in kvstore.
#[cfg(feature = "kvstore")]
#[error("kvstore key not found: {0}")]
KvStoreKeyNotFound(String),
/// Corrupted kvstore state.
#[cfg(feature = "kvstore")]
#[error("corrupted kvstore state: {0}")]
KvStoreCorruptedState(String),
/// Empty context (protocol_id) provided to kvstore.
#[cfg(feature = "kvstore")]
#[error("context cannot be empty")]
KvStoreEmptyContext,
/// Invalid key provided to kvstore (empty).
#[cfg(feature = "kvstore")]
#[error("invalid key")]
KvStoreInvalidKey,
/// Invalid value provided to kvstore (empty).
#[cfg(feature = "kvstore")]
#[error("invalid value")]
KvStoreInvalidValue,
// ===================
// Identity errors
// ===================
/// General identity error.
#[cfg(feature = "identity")]
#[error("identity error: {0}")]
IdentityError(String),
/// Identity not found.
#[cfg(feature = "identity")]
#[error("identity not found: {0}")]
IdentityNotFound(String),
/// Contact not found.
#[cfg(feature = "identity")]
#[error("contact not found: {0}")]
ContactNotFound(String),
}
/// Result type alias for BSV SDK operations.
pub type Result<T> = std::result::Result<T, Error>;