Пример #1
0
def issue(
    vcdig,
    regk,
    version=Version,
    kind=Serials.json,
):
    """

    Returns serder of iss message event
    Utility function to create a VC issuance event

    Parameters:
        vcdig is hash digest of vc content qb64
        regk is regsitry identifier prefix qb64

    """

    vs = Versify(version=version, kind=kind, size=0)
    ked = dict(
        v=vs,  # version string
        i=vcdig,  # qb64 prefix
        s="{:x}".format(0),  # hex string no leading zeros lowercase
        t=Ilks.iss,
        ri=regk)

    return Serder(ked=ked)  # return serialized ked
Пример #2
0
def revoke(
    vcdig,
    dig,
    version=Version,
    kind=Serials.json,
):
    """

    Returns serder of rev message event
    Utility function to create a VC revocation vent

    Parameters:
        vcdig is hash digest of vc content qb64
        regk is regsitry identifier prefix qb64
        dig is digest of previous event qb64

    """

    vs = Versify(version=version, kind=kind, size=0)
    isn = 1
    ilk = Ilks.rev

    ked = dict(
        v=vs,
        i=vcdig,
        s="{:x}".format(isn),  # hex string no leading zeros lowercase
        t=ilk,
        p=dig)

    return Serder(ked=ked)  # return serialized ked
Пример #3
0
def backer_revoke(
    vcdig,
    regk,
    regsn,
    regd,
    version=Version,
    kind=Serials.json,
):
    """

    Returns serder of brv message event
    Utility function to create a VC revocation event

    Parameters:
        vcdig is hash digest of vc content qb64
        regk is regsitry identifier prefix qb64
        regsn is int sequence number of anchoring registry TEL event
        regd is digest qb64 of anchoring registry TEL event
    """

    vs = Versify(version=version, kind=kind, size=0)
    isn = 1
    ilk = Ilks.rev

    seal = SealEvent(regk, regsn, regd)

    ked = dict(
        v=vs,
        i=vcdig,
        s="{:x}".format(isn),  # hex string no leading zeros lowercase
        t=ilk,
        ra=seal._asdict())

    return Serder(ked=ked)  # return serialized ked
Пример #4
0
def incept(
    pre,
    baks=None,
    cnfg=None,
    version=Version,
    kind=Serials.json,
):
    """

    Returns serder of vcp message event
    Utility function to create a Registry inception event

    Parameters:
         pre is issuer identifier prefix qb64
         cnfg is list of strings TraitDex of configuration traits
         baks is the initial list of backers prefixes for VCs in the Registry

         version is the API version
         kind is the event type

    """

    vs = Versify(version=version, kind=kind, size=0)
    isn = 0
    ilk = Ilks.vcp

    cnfg = cnfg if cnfg is not None else []

    if TraitDex.NoBackers in cnfg and len(baks) > 0:
        raise ValueError("{} backers specified for NB vcp, 0 allowed".format(
            len(baks)))

    ked = dict(
        v=vs,  # version string
        i="",  # qb64 prefix
        ii=pre,
        s="{:x}".format(isn),  # hex string no leading zeros lowercase
        t=ilk,
        c=cnfg,
        b=baks  # list of qb64 may be empty
    )

    prefixer = Prefixer(ked=ked)  # Derive AID from ked and code
    ked["i"] = prefixer.qb64  # update pre element in ked with pre qb64

    return Serder(ked=ked)  # return serialized ked
Пример #5
0
def rotate(
    pre,
    regk,
    sn=1,
    baks=None,
    version=Version,
    kind=Serials.json,
):
    """

    Returns serder of vrt message event
    Utility function to create a Registry rotation event

    Parameters:
        pre is identifier prefix qb64
        regk is regsitry identifier prefix qb64
        sn is int sequence number
        baks is new list of backers prefixes for VCs in the Registry


    """

    if sn < 1:
        raise ValueError("Invalid sn = {} for vrt.".format(sn))

    vs = Versify(version=version, kind=kind, size=0)
    ilk = Ilks.vrt

    ked = dict(
        v=vs,  # version string
        i=regk,  # qb64 prefix
        ii=pre,
        s="{:x}".format(sn),  # hex string no leading zeros lowercase
        t=ilk,
        b=baks,  # list of qb64 may be empty
    )

    return Serder(ked=ked)  # return serialized ked
Пример #6
0
def rotate(
    regk,
    dig,
    sn=1,
    toad=None,
    baks=None,
    cuts=None,
    adds=None,
    version=Version,
    kind=Serials.json,
):
    """

    Returns serder of vrt message event
    Utility function to create a Registry rotation event

    Parameters:
        pre is identifier prefix qb64
        regk is regsitry identifier prefix qb64
        sn is int sequence number
        toad is int or str hex of witness threshold
        baks is list of prior backers prefixes qb64
        cuts is list of witness prefixes to cut qb64
        adds is list of witness prefixes to add qb64

    """

    if sn < 1:
        raise ValueError("Invalid sn = {} for vrt.".format(sn))

    vs = Versify(version=version, kind=kind, size=0)
    ilk = Ilks.vrt

    baks = baks if baks is not None else []
    bakset = oset(baks)
    if len(bakset) != len(baks):
        raise ValueError("Invalid baks = {}, has duplicates.".format(baks))

    cuts = cuts if cuts is not None else []
    cutset = oset(cuts)
    if len(cutset) != len(cuts):
        raise ValueError("Invalid cuts = {}, has duplicates.".format(cuts))

    if (bakset & cutset) != cutset:  #  some cuts not in wits
        raise ValueError(
            "Invalid cuts = {}, not all members in baks.".format(cuts))

    adds = adds if adds is not None else []
    addset = oset(adds)
    if len(addset) != len(adds):
        raise ValueError("Invalid adds = {}, has duplicates.".format(adds))

    if cutset & addset:  # non empty intersection
        raise ValueError("Intersecting cuts = {} and  adds = {}.".format(
            cuts, adds))

    if bakset & addset:  # non empty intersection
        raise ValueError("Intersecting baks = {} and  adds = {}.".format(
            baks, adds))

    newbakset = (bakset - cutset) | addset

    if len(newbakset) != (len(baks) - len(cuts) + len(adds)):  # redundant?
        raise ValueError(
            "Invalid member combination among baks = {}, cuts ={}, "
            "and adds = {}.".format(baks, cuts, adds))

    if isinstance(toad, str):
        toad = "{:x}".format(toad)
    elif toad is None:
        if not newbakset:
            toad = 0
        else:  # compute default f and m for len(newbakset)
            toad = ample(len(newbakset))

    if newbakset:
        if toad < 1 or toad > len(newbakset):  # out of bounds toad
            raise ValueError("Invalid toad = {} for resultant wits = {}"
                             "".format(toad, list(newbakset)))
    else:
        if toad != 0:  # invalid toad
            raise ValueError("Invalid toad = {} for resultant wits = {}"
                             "".format(toad, list(newbakset)))

    ked = dict(
        v=vs,  # version string
        i=regk,  # qb64 prefix
        p=dig,
        s="{:x}".format(sn),  # hex string no leading zeros lowercase
        t=ilk,
        bt="{:x}".format(toad),  # hex string no leading zeros lowercase
        br=cuts,  # list of qb64 may be empty
        ba=adds,  # list of qb64 may be empty
    )

    return Serder(ked=ked)  # return serialized ked
Пример #7
0
def incept(
    pre,
    toad=None,
    baks=None,
    cnfg=None,
    version=Version,
    kind=Serials.json,
    code=None,
):
    """

    Returns serder of vcp message event
    Utility function to create a Registry inception event

    Parameters:
         pre is issuer identifier prefix qb64
         cnfg is list of strings TraitDex of configuration traits
         toad is int, or str hex of backer threshold
         baks is the initial list of backers prefixes for VCs in the Registry

         version is the API version
         kind is the event type
         code is default code for Prefixer

    """

    vs = Versify(version=version, kind=kind, size=0)
    isn = 0
    ilk = Ilks.vcp

    cnfg = cnfg if cnfg is not None else []

    baks = baks if baks is not None else []
    if TraitDex.NoBackers in cnfg and len(baks) > 0:
        raise ValueError("{} backers specified for NB vcp, 0 allowed".format(
            len(baks)))

    if len(oset(baks)) != len(baks):
        raise ValueError("Invalid baks = {}, has duplicates.".format(baks))

    if isinstance(toad, str):
        toad = "{:x}".format(toad)
    elif toad is None:
        if not baks:
            toad = 0
        else:  #  compute default f and m for len(baks)
            toad = ample(len(baks))

    if baks:
        if toad < 1 or toad > len(baks):  # out of bounds toad
            raise ValueError("Invalid toad = {} for baks = {}".format(
                toad, baks))
    else:
        if toad != 0:  # invalid toad
            raise ValueError("Invalid toad = {} for baks = {}".format(
                toad, baks))

    ked = dict(
        v=vs,  # version string
        i="",  # qb64 prefix
        ii=pre,
        s="{:x}".format(isn),  # hex string no leading zeros lowercase
        t=ilk,
        c=cnfg,
        bt="{:x}".format(toad),  # hex string no leading zeros lowercase
        b=baks  # list of qb64 may be empty
    )

    prefixer = Prefixer(ked=ked, code=code,
                        allows=[MtrDex.Blake3_256
                                ])  # Derive AID from ked and code
    ked["i"] = prefixer.qb64  # update pre element in ked with pre qb64

    return Serder(ked=ked)  # return serialized ked
Пример #8
0
def test_issuer():
    """
    Test Issuer Class
    """
    issuer = Registry()

    assert isinstance(issuer, Registry)
    assert issuer.name == "main"
    assert issuer.temp is False
    assert isinstance(issuer.env, lmdb.Environment)
    assert issuer.path.endswith("keri/db/main")
    assert issuer.env.path() == issuer.path
    assert os.path.exists(issuer.path)

    assert isinstance(issuer.tvts, lmdb._Database)

    issuer.close(clear=True)
    assert not os.path.exists(issuer.path)
    assert not issuer.opened

    # test not opened on init
    issuer = Registry(reopen=False)
    assert isinstance(issuer, Registry)
    assert issuer.name == "main"
    assert issuer.temp is False
    assert issuer.opened is False
    assert issuer.path is None
    assert issuer.env is None

    issuer.reopen()
    assert issuer.opened
    assert issuer.path is not None
    assert isinstance(issuer.env, lmdb.Environment)
    assert issuer.path.endswith("keri/db/main")
    assert issuer.env.path() == issuer.path
    assert os.path.exists(issuer.path)

    issuer.close(clear=True)
    assert not os.path.exists(issuer.path)
    assert not issuer.opened

    assert isinstance(issuer.tvts, lmdb._Database)

    with openLMDB(cls=Registry) as issuer:
        assert isinstance(issuer, Registry)
        assert issuer.name == "test"
        assert issuer.temp is True
        assert isinstance(issuer.env, lmdb.Environment)
        assert issuer.path.startswith("/tmp/keri_lmdb_")
        assert issuer.path.endswith("_test/keri/db/test")
        assert issuer.env.path() == issuer.path
        assert os.path.exists(issuer.path)

        assert isinstance(issuer.tvts, lmdb._Database)

    assert not os.path.exists(issuer.path)

    ipreb = "DYmJApMvMb8mgiG20BPlPcKLWSfNIUCC21DP0i2_BLjo".encode("utf-8")
    regb = "EOWdT7a7fZwRz0jiZ0DJxZEM3vsNbLDPEUk-ODnif3O0".encode("utf-8")
    rarb = "BijzaUuRMwh1ivT5BQrqNhbvx82lB-ofrHVHjL3WADbA".encode("utf-8")

    #  test with registry inception (vcp) event
    regk = nsKey([ipreb, regb])
    assert regk == b'DYmJApMvMb8mgiG20BPlPcKLWSfNIUCC21DP0i2_BLjo:EOWdT7a7fZwRz0jiZ0DJxZEM3vsNbLDPEUk-ODnif3O0'
    sn = 0
    vs = Versify(kind=Serials.json, size=20)

    vcp = dict(v=vs,
               i=regk.decode("utf-8"),
               s="{:x}".format(sn),
               b=[rarb.decode("utf-8")],
               t="vcp")

    vcpb = json.dumps(vcp, separators=(",", ":"),
                      ensure_ascii=False).encode("utf-8")
    assert vcpb == (
        b'{"v":"KERI10JSON000014_",'
        b'"i":"DYmJApMvMb8mgiG20BPlPcKLWSfNIUCC21DP0i2_BLjo:EOWdT7a7fZwRz0jiZ0DJxZEM3vsNbLDPEUk-ODnif3O0",'
        b'"s":"0","b":["BijzaUuRMwh1ivT5BQrqNhbvx82lB-ofrHVHjL3WADbA"],"t":"vcp"}'
    )
    vdig = Diger(ser=vcpb)

    with openLMDB(cls=Registry) as issuer:
        key = dgKey(regk, vdig.qb64b)

        assert issuer.getTvt(key) is None
        assert issuer.delTvt(key) is False
        assert issuer.putTvt(key, val=vcpb) is True
        assert issuer.getTvt(key) == vcpb
        assert issuer.putTvt(key, val=vcpb) is False
        assert issuer.setTvt(key, val=vcpb) is True
        assert issuer.getTvt(key) == vcpb
        assert issuer.delTvt(key) is True
        assert issuer.getTvt(key) is None

        telKey = snKey(regk, sn)
        assert issuer.getTel(telKey) is None
        assert issuer.delTel(telKey) is False
        assert issuer.putTel(telKey, val=vdig.qb64b)
        assert issuer.getTel(telKey) == vdig.qb64b
        assert issuer.putTel(telKey, val=vdig.qb64b) is False
        assert issuer.setTel(telKey, val=vdig.qb64b) is True
        assert issuer.getTel(telKey) == vdig.qb64b
        assert issuer.delTel(telKey) is True
        assert issuer.getTel(telKey) is None

        coupl01 = (
            "BPVuWC4Hc0izqPKn2LIwhp72SHJSRgfaL1RhtuiavIy4AAfiKvopJ0O2afOmxb5A6JtdY7Wkl_1uNx1Z8xQkg_"
            "gMzf-vTfEHDylFdgn2e_u_ppaFajIdvEvONX6dcSYzlfBQ").encode("utf-8")
        coupl02 = (
            "BW1gbapuOJ4TJKwLfKZs5cXEIs9k8EtBqxR1psVxnD7IABrSkjrgPGXdhBiOy6LUZpiqtsHkKHhfLGj_LhT1n6"
            "EqCIdDjrihzrdM1bm0ZNJDwbDGXoeeZujd7ZYsOsBPzRCw").encode("utf-8")
        coupl03 = (
            "BklrMm7GlYzNrPQunLJHFn_1wWjlUslGkXfs0KyoNOEAAC_6PB5Zre_E_7YLkM9OtRo-uYmwRyFmOH3Xo4JDiP"
            "jioY7Ycna6ouhSSH0QcKsEjce10HCXIW_XtmEYr9SrB5BA").encode("utf-8")
        coups = [coupl01, coupl02, coupl03]

        key = dgKey(regk, vdig.qb64b)
        assert issuer.getTibs(key) == []
        assert issuer.cntTibs(key) == 0
        assert issuer.delTibs(key) is False
        assert issuer.putTibs(key, vals=[coupl01]) is True
        assert issuer.getTibs(key) == [coupl01]
        assert issuer.cntTibs(key) == 1
        assert issuer.putTibs(key, vals=[coupl01]) is True  # add duplicate
        assert issuer.cntTibs(key) == 1
        assert issuer.addTib(key, coupl01) is False
        assert issuer.addTib(key, coupl02) is True
        assert issuer.cntTibs(key) == 2
        assert issuer.putTibs(key, vals=[coupl02, coupl03]) is True
        assert issuer.cntTibs(key) == 3
        assert issuer.delTibs(key) is True
        assert issuer.getTibs(key) == []
        for c in coups:
            assert issuer.addTib(key, c) is True
        assert issuer.cntTibs(key) == 3
        assert issuer.getTibs(key) == [coupl01, coupl02, coupl03]
        for c in issuer.getTibsIter(key):
            assert issuer.delTibs(key, c) is True
        assert issuer.getTibs(key) == []

        tweKey = snKey(regk, sn)
        assert issuer.getTwe(tweKey) is None
        assert issuer.delTwe(tweKey) is False
        assert issuer.putTwe(tweKey, val=vdig.qb64b)
        assert issuer.getTwe(tweKey) == vdig.qb64b
        assert issuer.putTwe(tweKey, val=vdig.qb64b) is False
        assert issuer.setTwe(tweKey, val=vdig.qb64b) is True
        assert issuer.getTwe(tweKey) == vdig.qb64b
        assert issuer.delTwe(tweKey) is True
        assert issuer.getTwe(tweKey) is None

        ooKey = snKey(regk, sn)
        assert issuer.getOot(ooKey) is None
        assert issuer.delOot(ooKey) is False
        assert issuer.putOot(ooKey, val=vdig.qb64b)
        assert issuer.getOot(ooKey) == vdig.qb64b
        assert issuer.putOot(ooKey, val=vdig.qb64b) is False
        assert issuer.setOot(ooKey, val=vdig.qb64b) is True
        assert issuer.getOot(ooKey) == vdig.qb64b
        assert issuer.delOot(ooKey) is True
        assert issuer.getOot(ooKey) is None

        anc01 = (
            "DYmJApMvMb8mgiG20BPlPcKLWSfNIUCC21DP0i2_BLjo"
            "0AAAAAAAAAAAAAAAAAAAAABA"
            "Ezpq06UecHwzy-K9FpNoRxCJp2wIGM9u2Edk-PLMZ1H4").encode("utf-8")

        key = dgKey(regk, vdig.qb64b)
        assert issuer.getAnc(key) is None
        assert issuer.delAnc(key) is False
        assert issuer.putAnc(key, val=anc01)
        assert issuer.getAnc(key) == anc01
        assert issuer.putAnc(key, val=anc01) is False
        assert issuer.setAnc(key, val=anc01) is True
        assert issuer.getAnc(key) == anc01
        assert issuer.delAnc(key) is True
        assert issuer.getAnc(key) is None

        #  test with verifiable credential issuance (iss) event
        vcdig = b'EXvR3p8V95W8J7Ui4-mEzZ79S-A1esAnJo1Kmzq80Jkc'
        vck = nsKey([ipreb, regb, vcdig])
        assert vck == (
            b'DYmJApMvMb8mgiG20BPlPcKLWSfNIUCC21DP0i2_BLjo:EOWdT7a7fZwRz0jiZ0DJxZEM3vsNbLDPEUk-ODnif3O0'
            b':EXvR3p8V95W8J7Ui4-mEzZ79S-A1esAnJo1Kmzq80Jkc')
        sn = 0
        vs = Versify(kind=Serials.json, size=20)

        vcp = dict(v=vs, i=vck.decode("utf-8"), s="{:x}".format(sn), t="iss")

        issb = json.dumps(vcp, separators=(",", ":"),
                          ensure_ascii=False).encode("utf-8")
        assert issb == (
            b'{"v":"KERI10JSON000014_",'
            b'"i":"DYmJApMvMb8mgiG20BPlPcKLWSfNIUCC21DP0i2_BLjo:'
            b'EOWdT7a7fZwRz0jiZ0DJxZEM3vsNbLDPEUk-ODnif3O0:EXvR3p8V95W8J7Ui4-mEzZ79S-A1esAnJo1Kmzq80Jkc",'
            b''
            b'"s":"0","t":"iss"}')
        idig = Diger(ser=issb)

        key = dgKey(vck, idig.qb64b)
        assert issuer.getTvt(key) is None
        assert issuer.delTvt(key) is False
        assert issuer.putTvt(key, val=issb) is True
        assert issuer.getTvt(key) == issb
        assert issuer.putTvt(key, val=issb) is False
        assert issuer.setTvt(key, val=issb) is True
        assert issuer.getTvt(key) == issb
        assert issuer.delTvt(key) is True
        assert issuer.getTvt(key) is None

        telKey = snKey(vck, sn)
        assert issuer.getTel(telKey) is None
        assert issuer.delTel(telKey) is False
        assert issuer.putTel(telKey, val=idig.qb64b)
        assert issuer.getTel(telKey) == idig.qb64b
        assert issuer.putTel(telKey, val=idig.qb64b) is False
        assert issuer.setTel(telKey, val=idig.qb64b) is True
        assert issuer.getTel(telKey) == idig.qb64b
        assert issuer.delTel(telKey) is True
        assert issuer.getTel(telKey) is None
    """End Test"""
Пример #9
0
def test_fetchkeldel():
    """
    Test fetching full KEL and full DEL from Logger
    """
    # Test using context manager
    preb = 'BWzwEHHzq7K0gzQPYGGwTmuupUhPx5_yZ-Wk1x4ejhcc'.encode("utf-8")
    digb = 'EGAPkzNZMtX-QiVgbRbyAIZGoXvbGv9IPb0foWTZvI_4'.encode("utf-8")
    sn = 3
    vs = Versify(kind=Serials.json, size=20)
    assert vs == 'KERI10JSON000014_'

    ked = dict(vs=vs,
               pre=preb.decode("utf-8"),
               sn="{:x}".format(sn),
               ilk="rot",
               dig=digb.decode("utf-8"))
    skedb = json.dumps(ked, separators=(",", ":"),
                       ensure_ascii=False).encode("utf-8")
    assert skedb == (
        b'{"vs":"KERI10JSON000014_","pre":"BWzwEHHzq7K0gzQPYGGwTmuupUhPx5_yZ-Wk1x4ejhc'
        b'c","sn":"3","ilk":"rot","dig":"EGAPkzNZMtX-QiVgbRbyAIZGoXvbGv9IPb0foWTZvI_4"'
        b'}')

    sig0b = 'AAz1KAV2z5IRqcFe4gPs9l3wsFKi1NsSZvBe8yQJmiu5AzJ91Timrykocna6Z_pQBl2gt59I_F6BsSwFbIOG1TDQ'.encode(
        "utf-8")
    sig1b = 'AB_pQBl2gt59I_F6BsSwFbIOG1TDQz1KAV2z5IRqcFe4gPs9l3wsFKi1NsSZvBe8yQJmiu5AzJ91Timrykocna6Z'.encode(
        "utf-8")

    wit0b = 'BmuupUhPx5_yZ-Wk1x4ejhccWzwEHHzq7K0gzQPYGGwT'.encode("utf-8")
    wit1b = 'BjhccWzwEHHzq7K0gzmuupUhPx5_yZ-Wk1x4eQPYGGwT'.encode("utf-8")
    wsig0b = '0A1Timrykocna6Z_pQBl2gt59I_F6BsSwFbIOG1TDQz1KAV2z5IRqcFe4gPs9l3wsFKi1NsSZvBe8yQJmiu5AzJ9'.encode(
        "utf-8")
    wsig1b = '0A5IRqcFe4gPs9l3wsFKi1NsSZvBe8yQJmiu5Az_pQBl2gt59I_F6BsSwFbIOG1TDQz1KAV2zJ91Timrykocna6Z'.encode(
        "utf-8")

    with openLogger() as lgr:
        # test getKelIter
        sn = 0
        key = snKey(preb, sn)
        assert key == (b'BWzwEHHzq7K0gzQPYGGwTmuupUhPx5_yZ-Wk1x4ejhcc.'
                       b'00000000000000000000000000000000')
        vals0 = [skedb]
        assert lgr.addKe(key, vals0[0]) == True

        vals1 = [b"mary", b"peter", b"john", b"paul"]
        sn += 1
        key = snKey(preb, sn)
        for val in vals1:
            assert lgr.addKe(key, val) == True

        vals2 = [b"dog", b"cat", b"bird"]
        sn += 1
        key = snKey(preb, sn)
        for val in vals2:
            assert lgr.addKe(key, val) == True

        vals = [bytes(val) for val in lgr.getKelIter(preb)]
        allvals = vals0 + vals1 + vals2
        assert vals == allvals

        # test getKelEstIter
        preb = 'B4ejhccWzwEHHzq7K0gzQPYGGwTmuupUhPx5_yZ-Wk1x'.encode("utf-8")
        sn = 0
        key = snKey(preb, sn)
        assert key == (b'B4ejhccWzwEHHzq7K0gzQPYGGwTmuupUhPx5_yZ-Wk1x.'
                       b'00000000000000000000000000000000')
        vals0 = [skedb]
        assert lgr.addKe(key, vals0[0]) == True

        vals1 = [b"mary", b"peter", b"john", b"paul"]
        sn += 1
        key = snKey(preb, sn)
        for val in vals1:
            assert lgr.addKe(key, val) == True

        vals2 = [b"dog", b"cat", b"bird"]
        sn += 1
        key = snKey(preb, sn)
        for val in vals2:
            assert lgr.addKe(key, val) == True

        vals = [bytes(val) for val in lgr.getKelEstIter(preb)]
        lastvals = [vals0[-1], vals1[-1], vals2[-1]]
        assert vals == lastvals

        # test getDelIter
        preb = 'BTmuupUhPx5_yZ-Wk1x4ejhccWzwEHHzq7K0gzQPYGGw'.encode("utf-8")
        sn = 1  # do not start at zero
        key = snKey(preb, sn)
        assert key == (b'BTmuupUhPx5_yZ-Wk1x4ejhccWzwEHHzq7K0gzQPYGGw.'
                       b'00000000000000000000000000000001')
        vals0 = [skedb]
        assert lgr.addDe(key, vals0[0]) == True

        vals1 = [b"mary", b"peter", b"john", b"paul"]
        sn += 1
        key = snKey(preb, sn)
        for val in vals1:
            assert lgr.addDe(key, val) == True

        vals2 = [b"dog", b"cat", b"bird"]
        sn += 3  # skip make gap in SN
        key = snKey(preb, sn)
        for val in vals2:
            assert lgr.addDe(key, val) == True

        vals = [bytes(val) for val in lgr.getDelIter(preb)]
        allvals = vals0 + vals1 + vals2
        assert vals == allvals

    assert not os.path.exists(lgr.path)
    """ End Test """
Пример #10
0
def test_logger():
    """
    Test Logger class
    """
    logger = Logger()
    assert isinstance(logger, Logger)
    assert logger.name == "main"
    assert isinstance(logger.env, lmdb.Environment)
    assert logger.path.endswith("keri/db/main")
    assert logger.env.path() == logger.path
    assert os.path.exists(logger.path)

    assert isinstance(logger.evts, lmdb._Database)
    assert isinstance(logger.sigs, lmdb._Database)
    assert isinstance(logger.dtss, lmdb._Database)
    assert isinstance(logger.rcts, lmdb._Database)
    assert isinstance(logger.ures, lmdb._Database)
    assert isinstance(logger.kels, lmdb._Database)
    assert isinstance(logger.ooes, lmdb._Database)
    assert isinstance(logger.pses, lmdb._Database)
    assert isinstance(logger.dels, lmdb._Database)
    assert isinstance(logger.ldes, lmdb._Database)

    logger.clearDirPath()
    assert not os.path.exists(logger.path)

    # Test using context manager
    with openDatabaser(cls=Logger) as logger:
        assert isinstance(logger, Logger)
        assert logger.name == "test"
        assert isinstance(logger.env, lmdb.Environment)
        assert logger.path.startswith("/tmp/keri_lmdb_")
        assert logger.path.endswith("_test/keri/db/test")
        assert logger.env.path() == logger.path
        assert os.path.exists(logger.path)

        assert isinstance(logger.evts, lmdb._Database)
        assert isinstance(logger.sigs, lmdb._Database)
        assert isinstance(logger.dtss, lmdb._Database)
        assert isinstance(logger.rcts, lmdb._Database)
        assert isinstance(logger.ures, lmdb._Database)
        assert isinstance(logger.kels, lmdb._Database)
        assert isinstance(logger.ooes, lmdb._Database)
        assert isinstance(logger.pses, lmdb._Database)
        assert isinstance(logger.dels, lmdb._Database)
        assert isinstance(logger.ldes, lmdb._Database)

    assert not os.path.exists(logger.path)

    preb = 'DWzwEHHzq7K0gzQPYGGwTmuupUhPx5_yZ-Wk1x4ejhcc'.encode("utf-8")
    digb = 'EGAPkzNZMtX-QiVgbRbyAIZGoXvbGv9IPb0foWTZvI_4'.encode("utf-8")
    sn = 3
    vs = Versify(kind=Serials.json, size=20)
    assert vs == 'KERI10JSON000014_'

    ked = dict(vs=vs,
               pre=preb.decode("utf-8"),
               sn="{:x}".format(sn),
               ilk="rot",
               dig=digb.decode("utf-8"))
    skedb = json.dumps(ked, separators=(",", ":"),
                       ensure_ascii=False).encode("utf-8")
    assert skedb == (
        b'{"vs":"KERI10JSON000014_","pre":"DWzwEHHzq7K0gzQPYGGwTmuupUhPx5_yZ-Wk1x4ejhc'
        b'c","sn":"3","ilk":"rot","dig":"EGAPkzNZMtX-QiVgbRbyAIZGoXvbGv9IPb0foWTZvI_4"'
        b'}')

    sig0b = 'AAz1KAV2z5IRqcFe4gPs9l3wsFKi1NsSZvBe8yQJmiu5AzJ91Timrykocna6Z_pQBl2gt59I_F6BsSwFbIOG1TDQ'.encode(
        "utf-8")
    sig1b = 'AB_pQBl2gt59I_F6BsSwFbIOG1TDQz1KAV2z5IRqcFe4gPs9l3wsFKi1NsSZvBe8yQJmiu5AzJ91Timrykocna6Z'.encode(
        "utf-8")

    wit0b = 'BmuupUhPx5_yZ-Wk1x4ejhccWzwEHHzq7K0gzQPYGGwT'.encode("utf-8")
    wit1b = 'BjhccWzwEHHzq7K0gzmuupUhPx5_yZ-Wk1x4eQPYGGwT'.encode("utf-8")
    wsig0b = '0B1Timrykocna6Z_pQBl2gt59I_F6BsSwFbIOG1TDQz1KAV2z5IRqcFe4gPs9l3wsFKi1NsSZvBe8yQJmiu5AzJ9'.encode(
        "utf-8")
    wsig1b = '0B5IRqcFe4gPs9l3wsFKi1NsSZvBe8yQJmiu5Az_pQBl2gt59I_F6BsSwFbIOG1TDQz1KAV2zJ91Timrykocna6Z'.encode(
        "utf-8")

    valb = 'EHzq7K0gzQPYGGwTmuupUhPx5_yZ-Wk1x4ejhccWzwEH'.encode("utf-8")
    vdigb = 'EQiVgbRbyAIZGoXvbGv9IPb0foWTZvI_4GAPkzNZMtX-'.encode("utf-8")
    vsig0b = 'AAKAV2z5IRqcFe4gPs9l3wsFKi1NsSZvBe81Timrykocna6Z_pQBl2gt59I_F6BsSwFbIOG1TDQz1yQJmiu5AzJ9'.encode(
        "utf-8")
    vsig1b = 'AB1KAV2zJ91Timrykocna6Z5IRqcFe4gPs9l3wsFKi1NsSZvBe8yQJmiu5Az_pQBl2gt59I_F6BsSwFbIOG1TDQz'.encode(
        "utf-8")

    with openLogger() as lgr:
        key = dgKey(preb, digb)
        assert key == (b'DWzwEHHzq7K0gzQPYGGwTmuupUhPx5_yZ-Wk1x4ejhcc.'
                       b'EGAPkzNZMtX-QiVgbRbyAIZGoXvbGv9IPb0foWTZvI_4')

        #  test .evts sub db methods
        assert lgr.getEvt(key) == None
        assert lgr.delEvt(key) == False
        assert lgr.putEvt(key, val=skedb) == True
        assert lgr.getEvt(key) == skedb
        assert lgr.putEvt(key, val=skedb) == False
        assert lgr.setEvt(key, val=skedb) == True
        assert lgr.getEvt(key) == skedb
        assert lgr.delEvt(key) == True
        assert lgr.getEvt(key) == None

        # test .dtss sub db methods
        val1 = b'2020-08-22T17:50:09.988921+00:00'
        val2 = b'2020-08-22T17:50:09.988921+00:00'

        assert lgr.getDts(key) == None
        assert lgr.delDts(key) == False
        assert lgr.putDts(key, val1) == True
        assert lgr.getDts(key) == val1
        assert lgr.putDts(key, val2) == False
        assert lgr.getDts(key) == val1
        assert lgr.setDts(key, val2) == True
        assert lgr.getDts(key) == val2
        assert lgr.delDts(key) == True
        assert lgr.getDts(key) == None

        # test .sigs sub db methods
        assert lgr.getSigs(key) == []
        assert lgr.cntSigs(key) == 0
        assert lgr.delSigs(key) == False

        # dup vals are lexocographic
        assert lgr.putSigs(key, vals=[b"z", b"m", b"x", b"a"]) == True
        assert lgr.getSigs(key) == [b'a', b'm', b'x', b'z']
        assert lgr.cntSigs(key) == 4
        assert lgr.putSigs(key, vals=[b'a']) == True  # duplicate but True
        assert lgr.getSigs(key) == [b'a', b'm', b'x', b'z']
        assert lgr.addSig(key, b'a') == False  # duplicate
        assert lgr.addSig(key, b'b') == True
        assert lgr.getSigs(key) == [b'a', b'b', b'm', b'x', b'z']
        assert [val for val in lgr.getSigsIter(key)
                ] == [b'a', b'b', b'm', b'x', b'z']
        assert lgr.delSigs(key) == True
        assert lgr.getSigs(key) == []

        assert lgr.putSigs(key, vals=[sig0b]) == True
        assert lgr.getSigs(key) == [sig0b]
        assert lgr.putSigs(key, vals=[sig1b]) == True
        assert lgr.getSigs(key) == [sig0b, sig1b]
        assert lgr.delSigs(key) == True
        assert lgr.putSigs(key, vals=[sig1b, sig0b]) == True
        assert lgr.getSigs(key) == [sig0b, sig1b]
        assert lgr.delSigs(key) == True
        assert lgr.getSigs(key) == []

        # test .rcts sub db methods dgkey
        assert lgr.getRcts(key) == []
        assert lgr.cntRcts(key) == 0
        assert lgr.delRcts(key) == False

        # dup vals are lexocographic
        assert lgr.putRcts(key, vals=[b"z", b"m", b"x", b"a"]) == True
        assert lgr.getRcts(key) == [b'a', b'm', b'x', b'z']
        assert lgr.cntRcts(key) == 4
        assert lgr.putRcts(key, vals=[b'a']) == True  # duplicate
        assert lgr.getRcts(key) == [b'a', b'm', b'x', b'z']
        assert lgr.addRct(key, b'a') == False  # duplicate
        assert lgr.addRct(key, b'b') == True
        assert lgr.getRcts(key) == [b'a', b'b', b'm', b'x', b'z']
        assert [val for val in lgr.getRctsIter(key)
                ] == [b'a', b'b', b'm', b'x', b'z']
        assert lgr.delRcts(key) == True
        assert lgr.getRcts(key) == []

        assert lgr.putRcts(key, vals=[wit0b + wsig0b, wit1b + wsig1b]) == True
        assert lgr.getRcts(key) == [wit1b + wsig1b,
                                    wit0b + wsig0b]  #  lex order
        assert lgr.putRcts(key, vals=[wit1b + wsig1b]) == True
        assert lgr.getRcts(key) == [wit1b + wsig1b,
                                    wit0b + wsig0b]  #  lex order
        assert lgr.delRcts(key) == True
        assert lgr.putRcts(key, vals=[wit1b + wsig1b, wit0b + wsig0b]) == True
        assert lgr.getRcts(key) == [wit1b + wsig1b,
                                    wit0b + wsig0b]  # lex order
        assert lgr.delRcts(key) == True
        assert lgr.getRcts(key) == []

        # test .ures sub db methods dgKey
        assert lgr.getUres(key) == []
        assert lgr.cntUres(key) == 0
        assert lgr.delUres(key) == False

        # dup vals are lexocographic
        assert lgr.putUres(key, vals=[b"z", b"m", b"x", b"a"]) == True
        assert lgr.getUres(key) == [b'a', b'm', b'x', b'z']
        assert lgr.cntUres(key) == 4
        assert lgr.putUres(key, vals=[b'a']) == True  # duplicate
        assert lgr.getUres(key) == [b'a', b'm', b'x', b'z']
        assert lgr.addUre(key, b'a') == False  # duplicate
        assert lgr.addUre(key, b'b') == True
        assert lgr.getUres(key) == [b'a', b'b', b'm', b'x', b'z']
        assert [val for val in lgr.getUresIter(key)
                ] == [b'a', b'b', b'm', b'x', b'z']
        assert lgr.delUres(key) == True
        assert lgr.getUres(key) == []

        assert lgr.putUres(key, vals=[wit0b + wsig0b, wit1b + wsig1b]) == True
        assert lgr.getUres(key) == [wit1b + wsig1b,
                                    wit0b + wsig0b]  #  lex order
        assert lgr.putUres(key, vals=[wit1b + wsig1b]) == True
        assert lgr.getUres(key) == [wit1b + wsig1b,
                                    wit0b + wsig0b]  #  lex order
        assert lgr.delUres(key) == True
        assert lgr.putUres(key, vals=[wit1b + wsig1b, wit0b + wsig0b]) == True
        assert lgr.getUres(key) == [wit1b + wsig1b,
                                    wit0b + wsig0b]  #  lex order
        assert lgr.delUres(key) == True
        assert lgr.getUres(key) == []

        # test .vrcs sub db methods dgkey
        assert lgr.getVrcs(key) == []
        assert lgr.cntVrcs(key) == 0
        assert lgr.delVrcs(key) == False

        # dup vals are lexocographic
        assert lgr.putVrcs(key, vals=[b"z", b"m", b"x", b"a"]) == True
        assert lgr.getVrcs(key) == [b'a', b'm', b'x', b'z']
        assert lgr.cntVrcs(key) == 4
        assert lgr.putVrcs(key, vals=[b'a']) == True  # duplicate
        assert lgr.getVrcs(key) == [b'a', b'm', b'x', b'z']
        assert lgr.addVrc(key, b'a') == False  # duplicate
        assert lgr.addVrc(key, b'b') == True
        assert lgr.getVrcs(key) == [b'a', b'b', b'm', b'x', b'z']
        assert [val for val in lgr.getVrcsIter(key)
                ] == [b'a', b'b', b'm', b'x', b'z']
        assert lgr.delVrcs(key) == True
        assert lgr.getVrcs(key) == []

        assert lgr.putVrcs(key,
                           vals=[valb + vdigb + vsig0b,
                                 valb + vdigb + vsig1b]) == True
        assert lgr.getVrcs(key) == [
            valb + vdigb + vsig0b, valb + vdigb + vsig1b
        ]  #  lex order
        assert lgr.putVrcs(key, vals=[valb + vdigb + vsig1b]) == True
        assert lgr.getVrcs(key) == [
            valb + vdigb + vsig0b, valb + vdigb + vsig1b
        ]  #  lex order
        assert lgr.delVrcs(key) == True
        assert lgr.putVrcs(key,
                           vals=[valb + vdigb + vsig1b,
                                 valb + vdigb + vsig0b]) == True
        assert lgr.getVrcs(key) == [
            valb + vdigb + vsig0b, valb + vdigb + vsig1b
        ]  #  lex order
        assert lgr.delVrcs(key) == True
        assert lgr.getVrcs(key) == []

        # test .vres sub db methods dgKey
        assert lgr.getVres(key) == []
        assert lgr.cntVres(key) == 0
        assert lgr.delVres(key) == False

        # dup vals are lexocographic
        assert lgr.putVres(key, vals=[b"z", b"m", b"x", b"a"]) == True
        assert lgr.getVres(key) == [b'a', b'm', b'x', b'z']
        assert lgr.cntVres(key) == 4
        assert lgr.putVres(key, vals=[b'a']) == True  # duplicate
        assert lgr.getVres(key) == [b'a', b'm', b'x', b'z']
        assert lgr.addVre(key, b'a') == False  # duplicate
        assert lgr.addVre(key, b'b') == True
        assert lgr.getVres(key) == [b'a', b'b', b'm', b'x', b'z']
        assert [val for val in lgr.getVresIter(key)
                ] == [b'a', b'b', b'm', b'x', b'z']
        assert lgr.delVres(key) == True
        assert lgr.getVres(key) == []

        assert lgr.putVres(key,
                           vals=[valb + vdigb + vsig0b,
                                 valb + vdigb + vsig1b]) == True
        assert lgr.getVres(key) == [
            valb + vdigb + vsig0b, valb + vdigb + vsig1b
        ]  #  lex order
        assert lgr.putVres(key, vals=[valb + vdigb + vsig1b]) == True
        assert lgr.getVres(key) == [
            valb + vdigb + vsig0b, valb + vdigb + vsig1b
        ]  #  lex order
        assert lgr.delVres(key) == True
        assert lgr.putVres(key,
                           vals=[valb + vdigb + vsig1b,
                                 valb + vdigb + vsig0b]) == True
        assert lgr.getVres(key) == [
            valb + vdigb + vsig0b, valb + vdigb + vsig1b
        ]  #  lex order
        assert lgr.delVres(key) == True
        assert lgr.getVres(key) == []

        # test .kels insertion order dup methods.  dup vals are insertion order
        key = snKey(preb, 0)
        vals = [b"z", b"m", b"x", b"a"]

        assert lgr.getKes(key) == []
        assert lgr.getKeLast(key) == None
        assert lgr.cntKes(key) == 0
        assert lgr.delKes(key) == False
        assert lgr.putKes(key, vals) == True
        assert lgr.getKes(key) == vals  # preserved insertion order
        assert lgr.cntKes(key) == len(vals) == 4
        assert lgr.getKeLast(key) == vals[-1]
        assert lgr.putKes(key, vals=[b'a']) == False  # duplicate
        assert lgr.getKes(key) == vals  #  no change
        assert lgr.addKe(key, b'a') == False  # duplicate
        assert lgr.addKe(key, b'b') == True
        assert lgr.getKes(key) == [b"z", b"m", b"x", b"a", b"b"]
        assert lgr.delKes(key) == True
        assert lgr.getKes(key) == []

        # test .pses insertion order dup methods.  dup vals are insertion order
        key = b'A'
        vals = [b"z", b"m", b"x", b"a"]

        assert lgr.getPses(key) == []
        assert lgr.getPsesLast(key) == None
        assert lgr.cntPses(key) == 0
        assert lgr.delPses(key) == False
        assert lgr.putPses(key, vals) == True
        assert lgr.getPses(key) == vals  # preserved insertion order
        assert lgr.cntPses(key) == len(vals) == 4
        assert lgr.getPsesLast(key) == vals[-1]
        assert lgr.putPses(key, vals=[b'a']) == False  # duplicate
        assert lgr.getPses(key) == vals  #  no change
        assert lgr.addPse(key, b'a') == False  # duplicate
        assert lgr.addPse(key, b'b') == True
        assert lgr.getPses(key) == [b"z", b"m", b"x", b"a", b"b"]
        assert lgr.delPses(key) == True
        assert lgr.getPses(key) == []

        # test .ooes insertion order dup methods.  dup vals are insertion order
        key = b'A'
        vals = [b"z", b"m", b"x", b"a"]

        assert lgr.getOoes(key) == []
        assert lgr.getOoesLast(key) == None
        assert lgr.cntOoes(key) == 0
        assert lgr.delOoes(key) == False
        assert lgr.putOoes(key, vals) == True
        assert lgr.getOoes(key) == vals  # preserved insertion order
        assert lgr.cntOoes(key) == len(vals) == 4
        assert lgr.getOoesLast(key) == vals[-1]
        assert lgr.putOoes(key, vals=[b'a']) == False  # duplicate
        assert lgr.getOoes(key) == vals  #  no change
        assert lgr.addOoe(key, b'a') == False  # duplicate
        assert lgr.addOoe(key, b'b') == True
        assert lgr.getOoes(key) == [b"z", b"m", b"x", b"a", b"b"]
        assert lgr.delOoes(key) == True
        assert lgr.getOoes(key) == []

        # test .dels insertion order dup methods.  dup vals are insertion order
        key = b'A'
        vals = [b"z", b"m", b"x", b"a"]

        assert lgr.getDes(key) == []
        assert lgr.getDesLast(key) == None
        assert lgr.cntDes(key) == 0
        assert lgr.delDes(key) == False
        assert lgr.putDes(key, vals) == True
        assert lgr.getDes(key) == vals  # preserved insertion order
        assert lgr.cntDes(key) == len(vals) == 4
        assert lgr.getDesLast(key) == vals[-1]
        assert lgr.putDes(key, vals=[b'a']) == False  # duplicate
        assert lgr.getDes(key) == vals  #  no change
        assert lgr.addDe(key, b'a') == False  # duplicate
        assert lgr.addDe(key, b'b') == True
        assert lgr.getDes(key) == [b"z", b"m", b"x", b"a", b"b"]
        assert lgr.delDes(key) == True
        assert lgr.getDes(key) == []

        # test .ldes insertion order dup methods.  dup vals are insertion order
        key = b'A'
        vals = [b"z", b"m", b"x", b"a"]

        assert lgr.getLdes(key) == []
        assert lgr.getLdesLast(key) == None
        assert lgr.cntLdes(key) == 0
        assert lgr.delLdes(key) == False
        assert lgr.putLdes(key, vals) == True
        assert lgr.getLdes(key) == vals  # preserved insertion order
        assert lgr.cntLdes(key) == len(vals) == 4
        assert lgr.getLdesLast(key) == vals[-1]
        assert lgr.putLdes(key, vals=[b'a']) == False  # duplicate
        assert lgr.getLdes(key) == vals  #  no change
        assert lgr.delLdes(key) == True
        assert lgr.getLdes(key) == []

    assert not os.path.exists(lgr.path)
    """ End Test """
Пример #11
0
def test_prefixer():
    pre = "DntNTPnDFBnmlO6J44LXCrzZTAmpe-82b7BmQGtL4QhM"
    vs = Versify(version=Version, kind=Serials.json, size=0)

    with pytest.raises(EmptyMaterialError):
        prefixer = Prefixer()

    # vcp, backers allowed no backers
    ked = dict(v=vs, i="", ii=pre, s="{:x}".format(0), t=Ilks.vcp, c=[], b=[])
    prefixer = Prefixer(ked=ked)
    assert prefixer.qb64 == "E_TB9WKVB4Zx-Wu3-u1_RQWy2ZrDccaOj2xUpHQcg0MA"
    assert prefixer.verify(ked=ked) is True
    assert prefixer.verify(ked=ked, prefixed=True) is False

    # Invalid event type
    ked = dict(v=vs, i="", ii=pre, s="{:x}".format(0), t=Ilks.iss, c=[], b=[])
    with pytest.raises(DerivationError):
        prefixer = Prefixer(ked=ked)

    # vcp, no backers allowed
    ked = dict(v=vs,
               i="",
               ii=pre,
               s="{:x}".format(0),
               t=Ilks.vcp,
               c=[TraitDex.NoBackers],
               b=[])
    prefixer = Prefixer(ked=ked)
    assert prefixer.qb64 == "EEDVlhKzGXA6C7n1igQF8m4WfTAEuwuvitgoM4DI3iCs"
    assert prefixer.verify(ked=ked) is True
    assert prefixer.verify(ked=ked, prefixed=True) is False

    bak1 = "EXvR3p8V95W8J7Ui4-mEzZ79S-A1esAnJo1Kmzq80Jkc"
    bak2 = "DSEpNJeSJjxo6oAxkNE8eCOJg2HRPstqkeHWBAvN9XNU"
    bak3 = "Dvxo-P4W_Z0xXTfoA3_4DMPn7oi0mLCElOWJDpC0nQXw"

    # vcp, one backer
    ked = dict(v=vs,
               i="",
               ii=pre,
               s="{:x}".format(0),
               t=Ilks.vcp,
               c=[],
               b=[bak1])
    prefixer = Prefixer(ked=ked)
    assert prefixer.qb64 == "E_e9zbZI8WCMNoaY1b-3aEVB59M6dc2Br8EDJ1_ozK-8"
    assert prefixer.verify(ked=ked) is True
    assert prefixer.verify(ked=ked, prefixed=True) is False

    # vcp, many backers
    ked = dict(v=vs,
               i="",
               ii=pre,
               s="{:x}".format(0),
               t=Ilks.vcp,
               c=[],
               b=[bak1, bak2, bak3])
    prefixer = Prefixer(ked=ked)
    assert prefixer.qb64 == "EEuFeIT3_0_IAaNg8D-5AxO6UtQCmD17n77iksL048Go"
    assert prefixer.verify(ked=ked) is True
    assert prefixer.verify(ked=ked, prefixed=True) is False
    """ End Test """
Пример #12
0
def gen_serialized_aid(witnesses):
    # taken from https://github.com/decentralized-identity/keripy/blob/6b85417ce1188543493efff48f95ebabd12fb0c6/tests/core/test_eventing.py
    with openDB() as bsr:
        # Transferable case
        # Setup inception key event dict
        # create current key
        sith = 1  #  one signer
        skp0 = Signer()  #  original signing keypair transferable default
        assert skp0.code == CryOneDex.Ed25519_Seed
        assert skp0.verfer.code == CryOneDex.Ed25519
        keys = [skp0.verfer.qb64]

        # create next key
        nxtsith = 1  #  one signer
        skp1 = Signer()  #  next signing keypair transferable is default
        assert skp1.code == CryOneDex.Ed25519_Seed
        assert skp1.verfer.code == CryOneDex.Ed25519
        nxtkeys = [skp1.verfer.qb64]
        # compute nxt digest
        nexter = Nexter(sith=nxtsith, keys=nxtkeys)
        nxt = nexter.qb64  # transferable so nxt is not empty

        sn = 0  #  inception event so 0
        toad = len(witnesses)
        nsigs = 1  #  one attached signature unspecified index

        ked0 = dict(
            vs=Versify(kind=Serials.json, size=0),
            pre="",  # qual base 64 prefix
            sn="{:x}".format(sn),  # hex string no leading zeros lowercase
            ilk=Ilks.icp,
            sith="{:x}".format(sith),  # hex string no leading zeros lowercase
            keys=keys,  # list of signing keys each qual Base64
            nxt=nxt,  # hash qual Base64
            toad="{:x}".format(toad),  # hex string no leading zeros lowercase
            # I think these are always necessary for direct mode
            wits=witnesses or [],  # list of qual Base64 may be empty
            cnfg=[],  # list of config ordered mappings may be empty
        )

        # Derive AID from ked
        aid0 = Prefixer(ked=ked0, code=CryOneDex.Ed25519)
        assert aid0.code == CryOneDex.Ed25519
        assert aid0.qb64 == skp0.verfer.qb64

        # update ked with pre
        ked0["pre"] = aid0.qb64

        # Serialize ked0
        tser0 = Serder(ked=ked0)

        # sign serialization
        tsig0 = skp0.sign(tser0.raw, index=0)

        # verify signature
        assert skp0.verfer.verify(tsig0.raw, tser0.raw)

        kever = Kever(serder=tser0, sigers=[tsig0], baser=bsr)  # no error

        # return base 64 serialization of signed aid
        # return tsig0.qb64
        return tsig0, tser0, tsig0.qb64
Пример #13
0
def test_clone():
    ipreb = "DYmJApMvMb8mgiG20BPlPcKLWSfNIUCC21DP0i2_BLjo".encode("utf-8")
    regk = "EOWdT7a7fZwRz0jiZ0DJxZEM3vsNbLDPEUk-ODnif3O0".encode("utf-8")
    rarb = "BijzaUuRMwh1ivT5BQrqNhbvx82lB-ofrHVHjL3WADbA".encode("utf-8")
    rarb2 = "BPVuWC4Hc0izqPKn2LIwhp72SHJSRgfaL1RhtuiavIy4".encode("utf-8")

    #  test with registry inception (vcp) event
    sn = 0
    vs = Versify(kind=Serials.json, size=20)

    vcp = dict(v=vs,
               i=regk.decode("utf-8"),
               s="{:x}".format(sn),
               b=[rarb.decode("utf-8")],
               t="vcp")

    vcpb = json.dumps(vcp, separators=(",", ":"),
                      ensure_ascii=False).encode("utf-8")
    vdig = Diger(ser=vcpb)
    anc01 = "0AAAAAAAAAAAAAAAAAAAAABAEzpq06UecHwzy-K9FpNoRxCJp2wIGM9u2Edk-PLMZ1H4".encode(
        "utf-8")
    tib01 = (
        "BPVuWC4Hc0izqPKn2LIwhp72SHJSRgfaL1RhtuiavIy4AAfiKvopJ0O2afOmxb5A6JtdY7Wkl_1uNx1Z8xQkg_"
        "gMzf-vTfEHDylFdgn2e_u_ppaFajIdvEvONX6dcSYzlfBQ").encode("utf-8")

    rot1 = dict(v=vs,
                i=regk.decode("utf-8"),
                s="{:x}".format(sn + 1),
                ba=[rarb2.decode("utf-8")],
                t="rot")
    rot1b = json.dumps(rot1, separators=(",", ":"),
                       ensure_ascii=False).encode("utf-8")
    r1dig = Diger(ser=rot1b)
    anc02 = "0AAAAAAAAAAAAAAAAAAAAABBEzpq06UecHwzy-K9FpNoRxCJp2wIGM9u2Edk-PLMZ1H4".encode(
        "utf-8")
    tib02 = (
        "BW1gbapuOJ4TJKwLfKZs5cXEIs9k8EtBqxR1psVxnD7IABrSkjrgPGXdhBiOy6LUZpiqtsHkKHhfLGj_LhT1n6"
        "EqCIdDjrihzrdM1bm0ZNJDwbDGXoeeZujd7ZYsOsBPzRCw").encode("utf-8")

    rot2 = dict(v=vs,
                i=regk.decode("utf-8"),
                s="{:x}".format(sn + 2),
                br=[rarb.decode("utf-8")],
                t="rot")
    rot2b = json.dumps(rot2, separators=(",", ":"),
                       ensure_ascii=False).encode("utf-8")
    r2dig = Diger(ser=rot2b)
    anc03 = "0AAAAAAAAAAAAAAAAAAAAABCEzpq06UecHwzy-K9FpNoRxCJp2wIGM9u2Edk-PLMZ1H4".encode(
        "utf-8")
    tib03 = (
        "BklrMm7GlYzNrPQunLJHFn_1wWjlUslGkXfs0KyoNOEAAC_6PB5Zre_E_7YLkM9OtRo-uYmwRyFmOH3Xo4JDiP"
        "jioY7Ycna6ouhSSH0QcKsEjce10HCXIW_XtmEYr9SrB5BA").encode("utf-8")

    with openLMDB(cls=Registry) as issuer:
        dgkey = dgKey(regk, vdig.qb64b)
        snkey = snKey(regk, sn)
        assert issuer.putTvt(dgkey, val=vcpb) is True
        assert issuer.putTel(snkey, val=vdig.qb64b)
        assert issuer.putAnc(dgkey, val=anc01) is True
        assert issuer.putTibs(dgkey, vals=[tib01]) is True

        dgkey = dgKey(regk, r1dig.qb64b)
        snkey = snKey(regk, sn + 1)
        assert issuer.putTvt(dgkey, val=rot1b) is True
        assert issuer.putTel(snkey, val=r1dig.qb64b)
        assert issuer.putAnc(dgkey, val=anc02) is True
        assert issuer.putTibs(dgkey, vals=[tib02]) is True

        dgkey = dgKey(regk, r2dig.qb64b)
        snkey = snKey(regk, sn + 2)
        assert issuer.putTvt(dgkey, val=rot2b) is True
        assert issuer.putTel(snkey, val=r2dig.qb64b)
        assert issuer.putAnc(dgkey, val=anc03) is True
        assert issuer.putTibs(dgkey, vals=[tib03]) is True

        msgs = bytearray()  # outgoing messages
        for msg in issuer.clonePreIter(regk):
            msgs.extend(msg)

        assert msgs == (
            b'{"v":"KERI10JSON000014_","i":"EOWdT7a7fZwRz0jiZ0DJxZEM3vsNbLDPEUk-ODnif3O0","s":"0",'
            b'"b":["BijzaUuRMwh1ivT5BQrqNhbvx82lB-ofrHVHjL3WADbA"],'
            b'"t":"vcp"}-VA0-BABBPVuWC4Hc0izqPKn2LIwhp72SHJSRgfaL1RhtuiavIy4AAfiKvopJ0O2afOmxb5A6JtdY7Wkl_1uNx1Z8xQk'
            b'g_gMzf-vTfEHDylFdgn2e_u_ppaFajIdvEvONX6dcSYzlfBQ-GAB0AAAAAAAAAAAAAAAAAAAAABAEzpq06UecHwzy-K9FpNoRxCJp2'
            b'wIGM9u2Edk-PLMZ1H4{"v":"KERI10JSON000014_","i":"EOWdT7a7fZwRz0jiZ0DJxZEM3vsNbLDPEUk-ODnif3O0","s":"1",'
            b'"ba":["BPVuWC4Hc0izqPKn2LIwhp72SHJSRgfaL1RhtuiavIy4"],"t":"rot"}-VA0-BABBW1gbapuOJ4TJKwLfKZs5cXEIs9k8Et'
            b'BqxR1psVxnD7IABrSkjrgPGXdhBiOy6LUZpiqtsHkKHhfLGj_LhT1n6EqCIdDjrihzrdM1bm0ZNJDwbDGXoeeZujd7ZYsOsBPzRCw-G'
            b'AB0AAAAAAAAAAAAAAAAAAAAABBEzpq06UecHwzy-K9FpNoRxCJp2wIGM9u2Edk-PLMZ1H4{"v":"KERI10JSON000014_","i":"EOWd'
            b'T7a7fZwRz0jiZ0DJxZEM3vsNbLDPEUk-ODnif3O0","s":"2","br":["BijzaUuRMwh1ivT5BQrqNhbvx82lB-ofrHVHjL3WADbA"]'
            b',"t":"rot"}-VA0-BABBklrMm7GlYzNrPQunLJHFn_1wWjlUslGkXfs0KyoNOEAAC_6PB5Zre_E_7YLkM9OtRo-uYmwRyFmOH3Xo4JDi'
            b'PjioY7Ycna6ouhSSH0QcKsEjce10HCXIW_XtmEYr9SrB5BA-GAB0AAAAAAAAAAAAAAAAAAAAABCEzpq06UecHwzy-K9FpNoRxCJp2wIG'
            b'M9u2Edk-PLMZ1H4')
Пример #14
0
def test_issuer():
    """
    Test Issuer Class
    """
    issuer = Registry()

    assert isinstance(issuer, Registry)
    assert issuer.name == "main"
    assert issuer.temp is False
    assert isinstance(issuer.env, lmdb.Environment)
    assert issuer.path.endswith("keri/reg/main")
    assert issuer.env.path() == issuer.path
    assert os.path.exists(issuer.path)

    assert isinstance(issuer.tvts, lmdb._Database)

    issuer.close(clear=True)
    assert not os.path.exists(issuer.path)
    assert not issuer.opened

    # test not opened on init
    issuer = Registry(reopen=False)
    assert isinstance(issuer, Registry)
    assert issuer.name == "main"
    assert issuer.temp is False
    assert issuer.opened is False
    assert issuer.path is None
    assert issuer.env is None

    issuer.reopen()
    assert issuer.opened
    assert issuer.path is not None
    assert isinstance(issuer.env, lmdb.Environment)
    assert issuer.path.endswith("keri/reg/main")
    assert issuer.env.path() == issuer.path
    assert os.path.exists(issuer.path)

    issuer.close(clear=True)
    assert not os.path.exists(issuer.path)
    assert not issuer.opened

    assert isinstance(issuer.tvts, lmdb._Database)

    with openLMDB(cls=Registry) as issuer:
        assert isinstance(issuer, Registry)
        assert issuer.name == "test"
        assert issuer.temp is True
        assert isinstance(issuer.env, lmdb.Environment)
        assert issuer.path.startswith("/tmp/keri_reg_")
        assert issuer.path.endswith("_test/keri/reg/test")
        assert issuer.env.path() == issuer.path
        assert os.path.exists(issuer.path)

        assert isinstance(issuer.tvts, lmdb._Database)

    assert not os.path.exists(issuer.path)

    ipreb = "DYmJApMvMb8mgiG20BPlPcKLWSfNIUCC21DP0i2_BLjo".encode("utf-8")
    regb = "EOWdT7a7fZwRz0jiZ0DJxZEM3vsNbLDPEUk-ODnif3O0".encode("utf-8")
    rarb = "BijzaUuRMwh1ivT5BQrqNhbvx82lB-ofrHVHjL3WADbA".encode("utf-8")

    #  test with registry inception (vcp) event
    regk = nsKey([ipreb, regb])
    assert regk == b'DYmJApMvMb8mgiG20BPlPcKLWSfNIUCC21DP0i2_BLjo:EOWdT7a7fZwRz0jiZ0DJxZEM3vsNbLDPEUk-ODnif3O0'
    sn = 0
    vs = Versify(kind=Serials.json, size=20)

    vcp = dict(v=vs,
               i=regk.decode("utf-8"),
               s="{:x}".format(sn),
               b=[rarb.decode("utf-8")],
               t="vcp")

    vcpb = json.dumps(vcp, separators=(",", ":"),
                      ensure_ascii=False).encode("utf-8")
    assert vcpb == (
        b'{"v":"KERI10JSON000014_",'
        b'"i":"DYmJApMvMb8mgiG20BPlPcKLWSfNIUCC21DP0i2_BLjo:EOWdT7a7fZwRz0jiZ0DJxZEM3vsNbLDPEUk-ODnif3O0",'
        b'"s":"0","b":["BijzaUuRMwh1ivT5BQrqNhbvx82lB-ofrHVHjL3WADbA"],"t":"vcp"}'
    )
    vdig = Diger(ser=vcpb)

    with openLMDB(cls=Registry) as issuer:
        key = dgKey(regk, vdig.qb64b)

        assert issuer.getTvt(key) is None
        assert issuer.delTvt(key) is False
        assert issuer.putTvt(key, val=vcpb) is True
        assert issuer.getTvt(key) == vcpb
        assert issuer.putTvt(key, val=vcpb) is False
        assert issuer.setTvt(key, val=vcpb) is True
        assert issuer.getTvt(key) == vcpb
        assert issuer.delTvt(key) is True
        assert issuer.getTvt(key) is None

        telKey = snKey(regk, sn)
        assert issuer.getTel(telKey) is None
        assert issuer.delTel(telKey) is False
        assert issuer.putTel(telKey, val=vdig.qb64b)
        assert issuer.getTel(telKey) == vdig.qb64b
        assert issuer.putTel(telKey, val=vdig.qb64b) is False
        assert issuer.setTel(telKey, val=vdig.qb64b) is True
        assert issuer.getTel(telKey) == vdig.qb64b
        assert issuer.delTel(telKey) is True
        assert issuer.getTel(telKey) is None

        coupl01 = (
            "BPVuWC4Hc0izqPKn2LIwhp72SHJSRgfaL1RhtuiavIy4AAfiKvopJ0O2afOmxb5A6JtdY7Wkl_1uNx1Z8xQkg_"
            "gMzf-vTfEHDylFdgn2e_u_ppaFajIdvEvONX6dcSYzlfBQ").encode("utf-8")
        coupl02 = (
            "BW1gbapuOJ4TJKwLfKZs5cXEIs9k8EtBqxR1psVxnD7IABrSkjrgPGXdhBiOy6LUZpiqtsHkKHhfLGj_LhT1n6"
            "EqCIdDjrihzrdM1bm0ZNJDwbDGXoeeZujd7ZYsOsBPzRCw").encode("utf-8")
        coupl03 = (
            "BklrMm7GlYzNrPQunLJHFn_1wWjlUslGkXfs0KyoNOEAAC_6PB5Zre_E_7YLkM9OtRo-uYmwRyFmOH3Xo4JDiP"
            "jioY7Ycna6ouhSSH0QcKsEjce10HCXIW_XtmEYr9SrB5BA").encode("utf-8")
        coups = [coupl01, coupl02, coupl03]

        key = dgKey(regk, vdig.qb64b)
        assert issuer.getTibs(key) == []
        assert issuer.cntTibs(key) == 0
        assert issuer.delTibs(key) is False
        assert issuer.putTibs(key, vals=[coupl01]) is True
        assert issuer.getTibs(key) == [coupl01]
        assert issuer.cntTibs(key) == 1
        assert issuer.putTibs(key, vals=[coupl01]) is True  # add duplicate
        assert issuer.cntTibs(key) == 1
        assert issuer.addTib(key, coupl01) is False
        assert issuer.addTib(key, coupl02) is True
        assert issuer.cntTibs(key) == 2
        assert issuer.putTibs(key, vals=[coupl02, coupl03]) is True
        assert issuer.cntTibs(key) == 3
        assert issuer.delTibs(key) is True
        assert issuer.getTibs(key) == []
        for c in coups:
            assert issuer.addTib(key, c) is True
        assert issuer.cntTibs(key) == 3
        assert issuer.getTibs(key) == [coupl01, coupl02, coupl03]
        for c in issuer.getTibsIter(key):
            assert issuer.delTibs(key, c) is True
        assert issuer.getTibs(key) == []

        tweKey = snKey(regk, sn)
        assert issuer.getTwe(tweKey) is None
        assert issuer.delTwe(tweKey) is False
        assert issuer.putTwe(tweKey, val=vdig.qb64b)
        assert issuer.getTwe(tweKey) == vdig.qb64b
        assert issuer.putTwe(tweKey, val=vdig.qb64b) is False
        assert issuer.setTwe(tweKey, val=vdig.qb64b) is True
        assert issuer.getTwe(tweKey) == vdig.qb64b
        assert issuer.delTwe(tweKey) is True
        assert issuer.getTwe(tweKey) is None

        ooKey = snKey(regk, sn)
        assert issuer.getOot(ooKey) is None
        assert issuer.delOot(ooKey) is False
        assert issuer.putOot(ooKey, val=vdig.qb64b)
        assert issuer.getOot(ooKey) == vdig.qb64b
        assert issuer.putOot(ooKey, val=vdig.qb64b) is False
        assert issuer.setOot(ooKey, val=vdig.qb64b) is True
        assert issuer.getOot(ooKey) == vdig.qb64b
        assert issuer.delOot(ooKey) is True
        assert issuer.getOot(ooKey) is None

        anc01 = (
            "0AAAAAAAAAAAAAAAAAAAAABA"
            "Ezpq06UecHwzy-K9FpNoRxCJp2wIGM9u2Edk-PLMZ1H4").encode("utf-8")

        key = dgKey(regk, vdig.qb64b)
        assert issuer.getAnc(key) is None
        assert issuer.delAnc(key) is False
        assert issuer.putAnc(key, val=anc01)
        assert issuer.getAnc(key) == anc01
        assert issuer.putAnc(key, val=anc01) is False
        assert issuer.setAnc(key, val=anc01) is True
        assert issuer.getAnc(key) == anc01
        assert issuer.delAnc(key) is True
        assert issuer.getAnc(key) is None

        #  test with verifiable credential issuance (iss) event
        vcdig = b'EXvR3p8V95W8J7Ui4-mEzZ79S-A1esAnJo1Kmzq80Jkc'
        sn = 0
        vs = Versify(kind=Serials.json, size=20)

        vcp = dict(v=vs, i=vcdig.decode("utf-8"), s="{:x}".format(sn), t="iss")

        issb = json.dumps(vcp, separators=(",", ":"),
                          ensure_ascii=False).encode("utf-8")
        assert issb == (b'{"v":"KERI10JSON000014_",'
                        b'"i":"EXvR3p8V95W8J7Ui4-mEzZ79S-A1esAnJo1Kmzq80Jkc",'
                        b''
                        b'"s":"0","t":"iss"}')
        idig = Diger(ser=issb)

        key = dgKey(vcdig, idig.qb64b)
        assert issuer.getTvt(key) is None
        assert issuer.delTvt(key) is False
        assert issuer.putTvt(key, val=issb) is True
        assert issuer.getTvt(key) == issb
        assert issuer.putTvt(key, val=issb) is False
        assert issuer.setTvt(key, val=issb) is True
        assert issuer.getTvt(key) == issb
        assert issuer.delTvt(key) is True
        assert issuer.getTvt(key) is None

        telKey = snKey(vcdig, sn)
        assert issuer.getTel(telKey) is None
        assert issuer.delTel(telKey) is False
        assert issuer.putTel(telKey, val=idig.qb64b)
        assert issuer.getTel(telKey) == idig.qb64b
        assert issuer.putTel(telKey, val=idig.qb64b) is False
        assert issuer.setTel(telKey, val=idig.qb64b) is True
        assert issuer.getTel(telKey) == idig.qb64b
        assert issuer.delTel(telKey) is True
        assert issuer.getTel(telKey) is None

        rev = dict(v=vs,
                   i=vcdig.decode("utf-8"),
                   s="{:x}".format(sn + 1),
                   t="rev")

        revb = json.dumps(rev, separators=(",", ":"),
                          ensure_ascii=False).encode("utf-8")
        assert revb == b'{"v":"KERI10JSON000014_","i":"EXvR3p8V95W8J7Ui4-mEzZ79S-A1esAnJo1Kmzq80Jkc","s":"1","t":"rev"}'
        rdig = Diger(raw=revb)

        assert issuer.putTel(snKey(vcdig, sn), val=idig.qb64b) is True
        assert issuer.putTel(snKey(vcdig, sn + 1), val=rdig.qb64b) is True
        assert issuer.putTel(snKey(vcdig, sn + 2), val=idig.qb64b) is True
        assert issuer.putTel(snKey(vcdig, sn + 3), val=rdig.qb64b) is True

        result = [(sn, dig) for sn, dig in issuer.getTelItemPreIter(vcdig)]
        assert result == [(0, idig.qb64b), (1, rdig.qb64b), (2, idig.qb64b),
                          (3, rdig.qb64b)]

        bak1 = b'Bm1Q98kT0HRn9R62lY-LufjjKdbCeL1mqu9arTgOmbqI'
        bak2 = b'DSEpNJeSJjxo6oAxkNE8eCOJg2HRPstqkeHWBAvN9XNU'
        bak3 = b'Dvxo-P4W_Z0xXTfoA3_4DMPn7oi0mLCElOWJDpC0nQXw'
        bak4 = b'BleAn9JkFuEOOwDhfkhnxtGsRQkMh2AH1oGB9QHAvl1U'
        bak5 = b'Behy5f2BIJbAYdgoy00OcOEZwEyxCGCUDlzbGkbz1RAI'
        baks = [bak1, bak2, bak3, bak4]

        # test .baks insertion order dup methods.  dup vals are insertion order
        assert issuer.getBaks(key) == []
        assert issuer.cntBaks(key) == 0
        assert issuer.delBaks(key) is False
        assert issuer.putBaks(key, baks) is True
        assert issuer.getBaks(key) == baks
        assert issuer.cntBaks(key) == len(baks) == 4
        assert issuer.putBaks(key, vals=[bak1]) is False
        assert issuer.getBaks(key) == baks
        assert issuer.addBak(key, bak1) is False
        assert issuer.addBak(key, bak5) is True
        assert issuer.getBaks(key) == [bak1, bak2, bak3, bak4, bak5]
        assert [val for val in issuer.getBaksIter(key)
                ] == [bak1, bak2, bak3, bak4, bak5]
        assert issuer.delBaks(key) is True
        assert issuer.getBaks(key) == []
    """End Test"""