2019-01-05 10:02:43 +01:00
|
|
|
from collections import namedtuple
|
2018-12-15 07:24:03 +01:00
|
|
|
|
2019-01-18 01:24:24 +01:00
|
|
|
from typing import Union
|
|
|
|
|
2019-01-05 10:02:43 +01:00
|
|
|
LEGACY = namedtuple('Legacy', 'payload')
|
|
|
|
NAMED_SECP256K1 = namedtuple('NamedSECP256k1', 'raw_signature certificate_id payload')
|
|
|
|
FLAGS = {
|
|
|
|
LEGACY: 0x80,
|
|
|
|
NAMED_SECP256K1: 0x01
|
|
|
|
}
|
2018-12-15 07:24:03 +01:00
|
|
|
|
|
|
|
class Signature:
|
|
|
|
|
2019-01-18 01:24:24 +01:00
|
|
|
def __init__(self, data: Union[LEGACY, NAMED_SECP256K1]):
|
2019-01-05 10:02:43 +01:00
|
|
|
self.data = data
|
|
|
|
|
|
|
|
@property
|
|
|
|
def payload(self):
|
|
|
|
return self.data.payload
|
|
|
|
|
|
|
|
@property
|
|
|
|
def certificate_id(self):
|
|
|
|
if type(self.data) == NAMED_SECP256K1:
|
|
|
|
return self.data.certificate_id
|
|
|
|
|
|
|
|
@property
|
|
|
|
def raw_signature(self):
|
|
|
|
if type(self.data) == NAMED_SECP256K1:
|
|
|
|
return self.data.raw_signature
|
2018-12-15 07:24:03 +01:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def flagged_parse(cls, binary: bytes):
|
2019-01-05 10:02:43 +01:00
|
|
|
flag = binary[0]
|
|
|
|
if flag == FLAGS[NAMED_SECP256K1]:
|
|
|
|
return cls(NAMED_SECP256K1(binary[1:65], binary[65:85], binary[85:]))
|
2018-12-15 07:24:03 +01:00
|
|
|
else:
|
2019-01-05 10:02:43 +01:00
|
|
|
return cls(LEGACY(binary))
|
2018-12-15 07:24:03 +01:00
|
|
|
|
|
|
|
@property
|
|
|
|
def serialized(self):
|
2019-01-05 10:02:43 +01:00
|
|
|
if isinstance(self.data, NAMED_SECP256K1):
|
|
|
|
return (bytes([FLAGS[type(self.data)]]) + self.data.raw_signature + self.data.certificate_id + self.payload)
|
|
|
|
elif isinstance(self.data, LEGACY):
|
|
|
|
return self.payload
|