-
Notifications
You must be signed in to change notification settings - Fork 2
/
botutils.py
65 lines (57 loc) · 1.72 KB
/
botutils.py
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
#!/usr/bin/env python3
import bech32
import datetime
import os
import sys
def bech32ToHex(bech32Input):
hrp, e2 = bech32.bech32_decode(bech32Input)
if hrp is None: return ""
tlv_bytes = bech32.convertbits(e2, 5, 8)[:-1]
if len(tlv_bytes) > 32:
tlv_length = tlv_bytes[1]
tlv_value = tlv_bytes[2:tlv_length+2]
hexOutput = bytes(tlv_value).hex()
else:
hexOutput = bytes(tlv_bytes).hex()
return hexOutput
def hexToBech32(hexInput, hrp):
b = bytes.fromhex(hexInput)
bits = bech32.convertbits(b,8,5)
bech32output = bech32.bech32_encode(hrp, bits)
return bech32output
def isHex(s):
return set(s).issubset(set('abcdefABCDEF0123456789'))
def normalizeToBech32(v, hrp):
# v can be hex of length 64, or bech32
if v == "0": v = ""
if str(v).startswith("nostr:"): v = v[6:]
if len(v) > 0:
if str(v).startswith("n"):
v = bech32ToHex(v)
if isHex(v) and len(v) == 64:
return hexToBech32(v, hrp)
return None
def normalizeToHex(v):
if len(v) == 0: return ""
if str(v).startswith("n"): v = bech32ToHex(v)
if isHex(v): return v
return None
def getCommandArg(p):
b = False
v = None
l = str(p).lower()
for a in sys.argv:
if b:
v = a
b = False
elif f"--{l}" == str(a).lower():
b = True
return v
def getTimes(aDate=None):
theDate = aDate
if aDate is None: theDate = datetime.datetime.now() # utcnow()
secTime = int(theDate.timestamp())
isoTime = theDate.utcfromtimestamp(theDate.timestamp()).isoformat(timespec="seconds")
return secTime, isoTime
def makeFolderIfNotExists(path):
if not os.path.exists(path): os.makedirs(path)