def pem(b, name):
    """Encode a payload bytearray into a PEM string.

    The input will be base64 encoded, then wrapped in a PEM prefix/postfix
    based on the name string, e.g. for name="CERTIFICATE":

    -----BEGIN CERTIFICATE-----
    MIIBXDCCAUSgAwIBAgIBADANBgkqhkiG9w0BAQUFADAPMQ0wCwYDVQQDEwRUQUNL
    ...
    KoZIhvcNAQEFBQADAwA5kw==
    -----END CERTIFICATE-----
    """
    s1 = b2a_base64(b)[:-1] # remove terminating \n
    s2 = b""
    while s1:
        s2 += s1[:64] + b"\n"
        s1 = s1[64:]
    s = ("-----BEGIN %s-----\n" % name).encode('ascii') + s2 + \
        ("-----END %s-----\n" % name).encode('ascii')
    return s
Encode a payload bytearray into a PEM string.

    The input will be base64 encoded, then wrapped in a PEM prefix/postfix
    based on the name string, e.g. for name="CERTIFICATE":

    -----BEGIN CERTIFICATE-----
    MIIBXDCCAUSgAwIBAgIBADANBgkqhkiG9w0BAQUFADAPMQ0wCwYDVQQDEwRUQUNL
    ...
    KoZIhvcNAQEFBQADAwA5kw==
    -----END CERTIFICATE-----
    
electrum/pem.py