File: signature.py

package info (click to toggle)
python-bitcoinlib 0.12.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 1,356 kB
  • sloc: python: 8,212; makefile: 132; sh: 6
file content (53 lines) | stat: -rw-r--r-- 1,452 bytes parent folder | download
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
# Copyright (C) The python-bitcoinlib developers
#
# This file is part of python-bitcoinlib.
#
# It is subject to the license terms in the LICENSE file found in the top-level
# directory of this distribution.
#
# No part of python-bitcoinlib, including this file, may be copied, modified,
# propagated, or distributed except according to the terms contained in the
# LICENSE file.


from bitcoin.core.serialize import *

# Py3 compatibility
import sys

from io import BytesIO


class DERSignature(ImmutableSerializable):
    __slots__ = ['length', 'r', 's']

    def __init__(self, r, s, length):
        object.__setattr__(self, 'r', r)
        object.__setattr__(self, 's', s)
        object.__setattr__(self, 'length', length)

    @classmethod
    def stream_deserialize(cls, f):
        assert ser_read(f, 1) == b"\x30"
        rs = BytesSerializer.stream_deserialize(f)
        f = BytesIO(rs)
        assert ser_read(f, 1) == b"\x02"
        r = BytesSerializer.stream_deserialize(f)
        assert ser_read(f, 1) == b"\x02"
        s = BytesSerializer.stream_deserialize(f)
        return cls(r, s, len(r + s))

    def stream_serialize(self, f):
        f.write(b"\x30")
        f.write(b"\x02")
        BytesSerializer.stream_serialize(self.r, f)
        f.write(b"\x30")
        BytesSerializer.stream_serialize(self.s, f)

    def __repr__(self):
        return 'DERSignature(%s, %s)' % (self.r, self.s)


__all__ = (
    'DERSignature',
)