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
use flex_error::{define_error, DisplayOnly, TraceError};
use std::io::Error as IoError;

use super::KeyType;
use crate::config::AddressType;

define_error! {
    Error {
        InvalidPublicKey
            [ TraceError<signature::Error> ]
            |_| { "invalid key: could not build public key from public key bytes" },

        KeyNotFound
            |_| { "key not found" },

        KeyAlreadyExist
            |_| { "key already exist" },

        InvalidMnemonic
            [ DisplayOnly<anyhow::Error> ]
            |_| { "invalid mnemonic" },

        Bip32KeyGenerationFailed
            { key_type: KeyType }
            [ TraceError<anyhow::Error> ]
            |e| { format!("cannot generate {} private key from BIP-32 seed", e.key_type) },

        OnlySecp256k1PublicKeySupported
            { key_type: String }
            |e| {
                format!("unsupported public key: {}. only secp256k1 pub keys are currently supported",
                    e.key_type)
            },

        EncodedPublicKey
            {
                key: String,
            }
            [ TraceError<serde_json::Error> ]
            |e| {
                format!("cannot deserialize the encoded public key {0}",
                    e.key)
            },

        Bech32Account
            [ TraceError<bech32::Error> ]
            |_| { "cannot generate bech32 account" },

        Bech32
            [ TraceError<bech32::Error> ]
            |_| { "bech32 error" },

        PublicKeyMismatch
             { keyfile: Vec<u8>, mnemonic: Vec<u8> }
            |_| { "mismatch between the public key in the key file and the public key in the mnemonic" },

        KeyFileEncode
            { file_path: String }
            [ TraceError<serde_json::Error> ]
            |e| {
                format!("error encoding key file at '{}'",
                    e.file_path)
            },

        Encode
            [ TraceError<serde_json::Error> ]
            |_| { "error encoding key" },

        KeyFileDecode
            { file_path: String }
            [ TraceError<serde_json::Error> ]
            |e| {
                format!("error decoding key file at '{}'",
                    e.file_path)
            },

        KeyFileIo
            {
                file_path: String,
                description: String,
            }
            [ TraceError<IoError> ]
            |e| {
                format!("I/O error on key file at '{}': {}",
                    e.file_path, e.description)
            },

        KeyFileNotFound
            { file_path: String }
            |e| {
                format!("cannot find key file at '{}'",
                    e.file_path)
            },

        HomeLocationUnavailable
            |_| { "home location is unavailable" },

        RemoveIoFail
            {
                file_path: String,
            }
            [ TraceError<IoError> ]
            |e| {
                format!("I/O error while removing key file at location '{}'",
                    e.file_path)
            },

        InvalidHdPath
            {
                path: String,
            }
            |e| {
                format!("invalid HD path: {0}", e.path)
            },

        AddressTypeNotFound
            {
                address: Vec<u8>,
                public_key: Vec<u8>,
            }
            |e| {
                format!("No address type found for address {:?} that matches the public key {:?}",
                        e.address,
                        e.public_key)
            },

        InvalidAddressLength
            {
                address: Vec<u8>,
                expected_length: usize,
            }
            |e| {
                format!("Length of address {:?} did not match expected length {}",
                        e.address,
                        e.expected_length)
            },


        Bs58Decode
            [ TraceError<bs58::decode::Error> ]
            |_| { "bs58 decode error" },

        UnsupportedAddressType
            {
                address_type: AddressType,
                key_type: KeyType,
            }
            |e| {
                format!("Unsupported address type {} for key type {}", e.address_type, e.key_type)
            },

        InvalidPublicKeyLength
            {
                got: usize,
                expected: usize
            }
            |e| {
                format!("Invalid public key length: expected {}, got {}", e.expected, e.got)
            }
    }
}