aboutsummaryrefslogtreecommitdiff
path: root/frozen_deps/base58/__main__.py
blob: 3a6f5d26b7e0c6aad67afe957e3db3b762295c29 (plain) (blame)
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
import argparse
import sys
from typing import Callable, Dict, Tuple

from base58 import b58decode, b58decode_check, b58encode, b58encode_check

_fmap = {
    (False, False): b58encode,
    (False, True): b58encode_check,
    (True, False): b58decode,
    (True, True): b58decode_check
}  # type: Dict[Tuple[bool, bool], Callable[[bytes], bytes]]


def main() -> None:
    '''Base58 encode or decode FILE, or standard input, to standard output.'''

    stdout = sys.stdout.buffer

    parser = argparse.ArgumentParser(description=main.__doc__)
    parser.add_argument(
        'file',
        metavar='FILE',
        nargs='?',
        type=argparse.FileType('r'),
        default='-')
    parser.add_argument(
        '-d', '--decode',
        action='store_true',
        help='decode data')
    parser.add_argument(
        '-c', '--check',
        action='store_true',
        help='append a checksum before encoding')

    args = parser.parse_args()
    fun = _fmap[(args.decode, args.check)]

    data = args.file.buffer.read()

    try:
        result = fun(data)
    except Exception as e:
        sys.exit(e)

    stdout.write(result)


if __name__ == '__main__':
    main()