import struct
import base64


def pack_signature(r, s):
    """Combine R&S

    Note: This serialization exactly reflects de/serialization written in the
    uplink backend. Modifying the way ECDSA signatures are serialized may cause
    transactions sent by this SDK to be deemed invalid by the uplink node.

    """
    rb = str(r)
    lenrb = struct.pack(">h", len(rb))
    rEnc = lenrb + rb.encode()

    sb = str(s)
    lensb = struct.pack(">h", len(sb))
    sEnc = lensb + sb.encode()
    signature = base64.b64encode('{}:{}'.format(rEnc.decode(), sEnc.decode()).encode())
    return signature