示例#1
0
def test_weighted():
    """
    Test multisig with weighted threshold

    """
    wesSalt = coring.Salter(raw=b'0123456789abcdef').qb64  # init wes Salter

    # init event DB and keep DB
    with dbing.openDB(name="wes") as wesDB, keeping.openKS(
            name="wes") as wesKS:
        # Init key pair manager
        wesMgr = keeping.Manager(keeper=wesKS, salt=wesSalt)

        # Init Kevery with event DB
        wesKvy = eventing.Kevery(db=wesDB)

        # create inception event for Wes with 3 keys each in incept and next sets
        # defaults are algo salty and rooted
        sith = ["1/2", "1/2", "1/2"]  #  2 of 3 but with weighted threshold
        nxtsith = ["1/2", "1/2", "1/2"]
        verfers, digers, cst, nst = wesMgr.incept(icount=3,
                                                  isith=sith,
                                                  ncount=3,
                                                  nsith=nxtsith,
                                                  stem='wes',
                                                  temp=True)
        assert cst == nst == sith

        wesSrdr = eventing.incept(
            keys=[verfer.qb64 for verfer in verfers],
            sith=sith,
            nxt=coring.Nexter(sith=nxtsith,
                              digs=[diger.qb64 for diger in digers]).qb64,
            code=coring.MtrDex.Blake3_256)

        wesPre = wesSrdr.ked["i"]

        wesMgr.move(old=verfers[0].qb64,
                    new=wesPre)  # move key pair label to prefix

        sigers = wesMgr.sign(ser=wesSrdr.raw, verfers=verfers)

        msg = bytearray(wesSrdr.raw)
        counter = coring.Counter(coring.CtrDex.ControllerIdxSigs,
                                 count=len(sigers))
        msg.extend(counter.qb64b)
        for siger in sigers:
            msg.extend(siger.qb64b)

        assert msg == bytearray(
            b'{"v":"KERI10JSON000154_","i":"EM8ac0UPJZCaWOw2uRcvx6FaygyxFvGzA5'
            b'MTob9WfbDQ","s":"0","t":"icp","kt":["1/2","1/2","1/2"],"k":["DK4'
            b'OJI8JOr6oEEUMeSF_X-SbKysfwpKwW-ho5KARvH5c","D1RZLgYke0GmfZm-CH8A'
            b'sW4HoTU4m-2mFgu8kbwp8jQU","DBVwzum-jPfuUXUcHEWdplB4YcoL3BWGXK0TM'
            b'oF_NeFU"],"n":"EhJGhyJQTpSlZ9oWfQT-lHNl1woMazLC42O89fRHocTI","wt'
            b'":"0","w":[],"c":[]}-AADAAc4jIKyjjpK7rJzywkX2AXXaNGgUGfcUgT6fm7P'
            b'iqL8H8tDsxHb6dcnybE7Hc34jtUq47OWWwCV3K9oCTUUAHAwABlP9qpCcMow8Lq5'
            b'bzE-DLHlItNuQYD9SqOQDNyJoTpk_BEW6Q8UIG012MJEM7GoFTMV5H9UUztQfSQp'
            b'l9Jh9lBQACVn_l3CTPIrCyGZpvW9qxVfZll0su-vIv1gvx0GQfo1qAMNk4c_7t-x'
            b'bXKTw3hwDPt46m5zGd38Y3qIEwQD3jCA')

        # apply msg to Wes's Kevery
        wesKvy.process(ims=bytearray(msg))  # process local copy of msg
        wesK = wesKvy.kevers[wesPre]  # kever created so event was validated
        assert wesK.prefixer.qb64 == wesPre
        assert wesK.serder.diger.qb64 == wesSrdr.dig  # key state updated so event was validated

        # create interaction event for Wes
        wesSrdr = eventing.interact(pre=wesK.prefixer.qb64,
                                    dig=wesK.serder.diger.qb64,
                                    sn=wesK.sn + 1,
                                    data=[])

        sigers = wesMgr.sign(ser=wesSrdr.raw, verfers=wesK.verfers)

        msg = bytearray(wesSrdr.raw)
        counter = coring.Counter(coring.CtrDex.ControllerIdxSigs,
                                 count=len(sigers))
        msg.extend(counter.qb64b)
        for siger in sigers:
            msg.extend(siger.qb64b)

        assert msg == bytearray(
            b'{"v":"KERI10JSON000098_","i":"EM8ac0UPJZCaWOw2uRcvx6FaygyxFvGzA5'
            b'MTob9WfbDQ","s":"1","t":"ixn","p":"E3-lhMd85oc8Uwrd_7c6xUy5tvZhr'
            b'b9ZHvcOO4HxHB1c","a":[]}-AADAAWmzu83wDFTn9Hc6_xskGe8Ed_PhiOpVQ2H'
            b'kxAx28qgLP_Zz7pwCsvmRDM1x9sL8Ygg7hQman5qDaeJS4fJm1DQABlc4hfziecy'
            b'_DXVN2a8AttmuBL_Oh0-Ro_Rz3Mf6KWOJTMLQIHaRJ62L01Q5vP6KmiSr2zwJUT_'
            b'urfGLZoaRUBwACt4l7pTFqmzfzk6p6FKlT1KGXYJ2ea2SmU7I-7agz0i4lCDNQf-'
            b'Y_NJWs6NTWEs5vsPOskNcGnr8nIpQ51N1qBQ')

        # apply msg to wes's Kevery
        wesKvy.process(ims=bytearray(msg))  # process local copy of msg
        assert wesK.serder.diger.qb64 == wesSrdr.dig  # key state updated so event was validated

        # Create rotation event for Wes
        # get current keys as verfers and next digests as digers
        nxtsith = ["1/2", "1/2", "1/2"]  #  2 of 3 but with weighted threshold
        verfers, digers, cst, nst = wesMgr.rotate(pre=wesPre,
                                                  count=3,
                                                  sith=nxtsith,
                                                  temp=True)
        assert nst == nxtsith

        wesSrdr = eventing.rotate(
            pre=wesK.prefixer.qb64,
            keys=[verfer.qb64 for verfer in verfers],
            sith=sith,
            dig=wesK.serder.diger.qb64,
            nxt=coring.Nexter(sith=nxtsith,
                              digs=[diger.qb64 for diger in digers]).qb64,
            sn=wesK.sn + 1,
            data=[])

        sigers = wesMgr.sign(ser=wesSrdr.raw, verfers=verfers)

        msg = bytearray(wesSrdr.raw)
        counter = coring.Counter(coring.CtrDex.ControllerIdxSigs,
                                 count=len(sigers))
        msg.extend(counter.qb64b)
        for siger in sigers:
            msg.extend(siger.qb64b)

        assert msg == bytearray(
            b'{"v":"KERI10JSON000190_","i":"EM8ac0UPJZCaWOw2uRcvx6FaygyxFvGzA5'
            b'MTob9WfbDQ","s":"2","t":"rot","p":"E6wjlP_oqJzmo65d56XuTL602ABcK'
            b'X0ZBEy9M-k7E1Eg","kt":["1/2","1/2","1/2"],"k":["DeonYM2bKnAwp6VZ'
            b'cuCXdX72kNFw56czlZ_Tc7XHHVGI","DQghKIy-2do9OkweSgazh3Ql1vCOt5bnc'
            b'5QF8x50tRoU","DNAUn-5dxm6b8Njo01O0jlStMRCjo9FYQA2mfqFW1_JA"],"n"'
            b':"EX5fxvjOg5VuDboWbqnTjTPpXa3nNIm99hlsB1EmhTo8","wt":"0","wr":[]'
            b',"wa":[],"a":[]}-AADAApZ3U4zacSPm5embDTRD2IxB1e4FrdAToP-tsXB-VVp'
            b'fX6Yk78iIdFyeNi9U_sgefzvhR3_mH5Bj_ZlfpEMCQDAABWURvCkE1HjbE_noEqj'
            b'BWEpdG1hUfP3_Oye5Ys0zquigDrOSv2ApXzlq1-ALDTZeqMX4lbVlqubRjDu3Qog'
            b'xrAgACtyNpfXHvly2emXyAdJ5sAVUVCnodONK2CG8WGipISYLGIlyfmNoTVeHw-f'
            b'_3ZY2tAgbmLZika4kEL8REfr5VCA')

        # apply msg to Wes's Kevery
        wesKvy.process(ims=bytearray(msg))  # process local copy of msg
        assert wesK.serder.diger.qb64 == wesSrdr.dig  # key state updated so event was validated

        # Create rotation event for Wes
        # get current keys as verfers and next digests as digers
        sith = nxtsith  # rotate so nxtsith is now current sith and need new nextsith
        #  2 of first 3 and 1 of last 2
        nxtsith = [["1/2", "1/2", "1/2"], ["1/1", "1/1"]]
        verfers, digers, cst, nst = wesMgr.rotate(pre=wesPre,
                                                  count=5,
                                                  sith=nxtsith,
                                                  temp=True)
        assert cst == sith
        assert nst == nxtsith

        wesSrdr = eventing.rotate(
            pre=wesK.prefixer.qb64,
            keys=[verfer.qb64 for verfer in verfers],
            sith=sith,
            dig=wesK.serder.diger.qb64,
            nxt=coring.Nexter(sith=nxtsith,
                              digs=[diger.qb64 for diger in digers]).qb64,
            sn=wesK.sn + 1,
            data=[])

        sigers = wesMgr.sign(ser=wesSrdr.raw, verfers=verfers)

        msg = bytearray(wesSrdr.raw)
        counter = coring.Counter(coring.CtrDex.ControllerIdxSigs,
                                 count=len(sigers))
        msg.extend(counter.qb64b)
        for siger in sigers:
            msg.extend(siger.qb64b)

        assert msg == bytearray(
            b'{"v":"KERI10JSON000190_","i":"EM8ac0UPJZCaWOw2uRcvx6FaygyxFvGzA5'
            b'MTob9WfbDQ","s":"3","t":"rot","p":"E9tuWqXCN31LqElTSdfGp3lWDetle'
            b'T4Pa9tuSUi2V87k","kt":["1/2","1/2","1/2"],"k":["D7WWKDLVwYxYMLAj'
            b'DceIEs66xPMY4Afzx-RQw2x0mQzI","Dmg6Aah8qyKKDiQyNXTiO71QJwizjZfGM'
            b'61BA-s0A5F4","DS3fhKpvPCDL5WmfN4_PkmJMMsSCdRTxG24OQuf_EmHQ"],"n"'
            b':"EcM4iw7fElXWhad8V-za4Px7nBKjndxoh3XZRkohghKY","wt":"0","wr":[]'
            b',"wa":[],"a":[]}-AADAAO0Ma_uiLbrXrqkNsLccCNgWcfvopoo2NwZ5aJLKBa9'
            b'7OMuZibsiVL6bDues9r65o2Tq1hzuuQQK6cHg_OH3xDAAB-cLMTqhogxrxyhMVoP'
            b'RXJ-rtQaV5oEsXSqcU3phI0bxFJvtydfnySe30LXbOwnFS-_HhCRMOulhBdcAvFR'
            b'dKAAACXhumJPsAS1UWSjlKiSby_TCC_W82jkTcvWBB4pwrcYmno8jRpQoB0ubPyG'
            b'96I2RqNql0Q9p5LcMPsLtT_Zt4DA')

        # apply msg to Wes's Kevery
        wesKvy.process(ims=bytearray(msg))  # process local copy of msg
        assert wesK.serder.diger.qb64 == wesSrdr.dig  # key state updated so event was validated

        # Create rotation event for Wes
        # get current keys as verfers and next digests as digers
        sith = nxtsith  # rotate so nxtsith is now current sith and need new nextsith
        #  2 of first 3 and 1 of last 2
        nxtsith = [["1/2", "1/2", "1/2"], ["1/1", "1/1"]]
        verfers, digers, cst, nst = wesMgr.rotate(pre=wesPre,
                                                  count=5,
                                                  sith=nxtsith,
                                                  temp=True)
        assert cst == nst == nxtsith

        wesSrdr = eventing.rotate(
            pre=wesK.prefixer.qb64,
            keys=[verfer.qb64 for verfer in verfers],
            sith=sith,
            dig=wesK.serder.diger.qb64,
            nxt=coring.Nexter(sith=nxtsith,
                              digs=[diger.qb64 for diger in digers]).qb64,
            sn=wesK.sn + 1,
            data=[])

        sigers = wesMgr.sign(ser=wesSrdr.raw, verfers=verfers)

        msg = bytearray(wesSrdr.raw)
        counter = coring.Counter(coring.CtrDex.ControllerIdxSigs,
                                 count=len(sigers))
        msg.extend(counter.qb64b)
        for siger in sigers:
            msg.extend(siger.qb64b)

        assert msg == bytearray(
            b'{"v":"KERI10JSON0001fe_","i":"EM8ac0UPJZCaWOw2uRcvx6FaygyxFvGzA5'
            b'MTob9WfbDQ","s":"4","t":"rot","p":"EkBxzyMDQGRCNmoMOWwh58wuNuERR'
            b'cLoMH2_F0w99Dw4","kt":[["1/2","1/2","1/2"],["1/1","1/1"]],"k":["'
            b'DToUWoemnetqJoLFIqDI7lxIJEfF0W7xG5ZlqAseVUQc","Drz-IZjko61q-sPMD'
            b'IW6n-0NGFubbXiZhzWZrO_BZ0Wc","DiGwL3hjQqiUgQlFPeA6kRR1EBXX0vSLm9'
            b'b6QhPS8IkQ","Dxj5pcStgZ6CbQ2YktNaj8KLE_g9YAOZF6AL9fyLcWQw","DE5z'
            b'r5eH8EUVQXyAaxWfQUWkGCId-QDCvvxMT77ibj2Q"],"n":"E3in3Z14va0kk4Wq'
            b'd3vcCAojKNtQq7ZTrQaavR8x0yu4","wt":"0","wr":[],"wa":[],"a":[]}-A'
            b'AFAAEjpPTMtLre--y96OaTckIov-qfWT1lqOvwNBAcdTfmsfCLIJgZO4Y2ybJqGw'
            b'l2Q6DqLdfNQWHiDwnyllo1zZBgABny8aZlKENxCnulxSzSWIbFsg1Kv7RrdgTt4r'
            b'19taFq-bmBmMTLrkidNbeMHwgsNhhT8f3KJnPTaHEZ2Myd3BDQACaJ2sc2SpEcM0'
            b'9qMbk-8maWuxjAdMCb8n5P1vJesnf7TW6p3Vu2Mart5HuXW44r79DQ91sAmyYB_0'
            b'4q--ZyNYAQAD5trFl0S9G0GQmFF7FCgMYWzKNe7x16622OvT1-HjDP-eXxf9dani'
            b'dlUIbVWqalLgXOdhhsCNUDasvOHLByjSBgAEs-ovUeu2--2wnCJLpfHzLZUbc5fL'
            b'8bpOShEoPUwxEH4H1Wxsn3xPlvL3_pe5Mun3sq2jIhl1EOjcDaKOHofZCA')

        # apply msg to Wes's Kevery
        wesKvy.process(ims=bytearray(msg))  # process local copy of msg
        assert wesK.serder.diger.qb64 == wesSrdr.dig  # key state updated so event was validated

    assert not os.path.exists(wesKS.path)
    assert not os.path.exists(wesDB.path)
    """End Test"""
示例#2
0
def test_delegation():
    """
    Test creation and validation of delegated identifer prefixes and events

    """
    # bob is the delegator del is bob's delegate

    bobSalt = coring.Salter(raw=b'0123456789abcdef').qb64
    delSalt = coring.Salter(raw=b'abcdef0123456789').qb64

    with dbing.openDB(name="bob") as bobDB, \
          keeping.openKS(name="bob") as bobKS, \
          dbing.openDB(name="del") as delDB, \
          keeping.openKS(name="del") as delKS:

        # Init key pair managers
        bobMgr = keeping.Manager(keeper=bobKS, salt=bobSalt)
        delMgr = keeping.Manager(keeper=delKS, salt=delSalt)

        # Init Keverys
        bobKvy = eventing.Kevery(db=bobDB)
        delKvy = eventing.Kevery(db=delDB)

        # Setup Bob by creating inception event
        verfers, digers, cst, nst = bobMgr.incept(
            stem='bob', temp=True)  # algo default salty and rooted
        bobSrdr = eventing.incept(
            keys=[verfer.qb64 for verfer in verfers],
            nxt=coring.Nexter(digs=[diger.qb64 for diger in digers]).qb64,
            code=coring.MtrDex.Blake3_256)

        bobPre = bobSrdr.ked["i"]
        assert bobPre == 'EiBlVttjqvySMbA4ShN19rSrz3D0ioNW-Uj92Ri7XnFE'

        bobMgr.move(old=verfers[0].qb64,
                    new=bobPre)  # move key pair label to prefix

        sigers = bobMgr.sign(ser=bobSrdr.raw, verfers=verfers)

        msg = bytearray(bobSrdr.raw)
        counter = coring.Counter(code=coring.CtrDex.ControllerIdxSigs,
                                 count=len(sigers))
        msg.extend(counter.qb64b)
        for siger in sigers:
            msg.extend(siger.qb64b)

        assert msg == bytearray(
            b'{"v":"KERI10JSON0000e6_","i":"EiBlVttjqvySMbA4ShN19rSrz3D0ioNW-U'
            b'j92Ri7XnFE","s":"0","t":"icp","kt":"1","k":["DqI2cOZ06RwGNwCovYU'
            b'WExmdKU983IasmUKMmZflvWdQ"],"n":"E7FuL3Z_KBgt_QAwuZi1lUFNC69wvyH'
            b'SxnMFUsKjZHss","wt":"0","w":[],"c":[]}-AABAAQPFdtnncXLz6dE6A-tXG'
            b'YYK0BHu3I3Pj-G8DxlbzC3yx5MV8yucZILqAA5toZNODnHVHZtPIMkDknqldL4utBQ'
        )

        # apply msg to bob's Kevery
        bobKvy.process(ims=bytearray(msg))  # process local copy of msg
        bobK = bobKvy.kevers[bobPre]
        assert bobK.prefixer.qb64 == bobPre
        assert bobK.serder.diger.qb64 == bobSrdr.dig
        assert bobK.serder.diger.qb64 == 'EvP2kWxEjTMI3auc6x64EpU-nMQZHiBeKeuavcGdRB24'

        # apply msg to del's Kevery
        delKvy.process(ims=bytearray(msg))  # process remote copy of msg
        assert bobPre in delKvy.kevers

        # Setup Del's inception event assuming that Bob's next event will be an ixn delegating event
        verfers, digers, cst, nst = delMgr.incept(
            stem='del', temp=True)  # algo default salty and rooted

        seal = eventing.SealLocation(i=bobK.prefixer.qb64,
                                     s="{:x}".format(bobK.sn + 1),
                                     t=coring.Ilks.ixn,
                                     p=bobK.serder.diger.qb64)

        assert seal._asdict() == dict(
            i='EiBlVttjqvySMbA4ShN19rSrz3D0ioNW-Uj92Ri7XnFE',
            s='1',
            t='ixn',
            p='EvP2kWxEjTMI3auc6x64EpU-nMQZHiBeKeuavcGdRB24')

        delSrdr = eventing.delcept(
            keys=[verfer.qb64 for verfer in verfers],
            seal=seal,
            nxt=coring.Nexter(digs=[diger.qb64 for diger in digers]).qb64)

        delPre = delSrdr.ked["i"]
        assert delPre == 'ErLe2qWp4VCmDp7v_R01tC-ha13ZEZY0VGcgYtPRhqPs'

        delMgr.move(old=verfers[0].qb64,
                    new=delPre)  # move key pair label to prefix
        assert delSrdr.dig == 'ESDuaqpoI8-HLD8-eLijUMZpXqYFkNArJFDvt3ABYr9I'

        # Now create delegating event
        seal = eventing.SealEvent(i=delPre, s=delSrdr.ked["s"], d=delSrdr.dig)
        bobSrdr = eventing.interact(pre=bobK.prefixer.qb64,
                                    dig=bobK.serder.diger.qb64,
                                    sn=bobK.sn + 1,
                                    data=[seal._asdict()])

        sigers = bobMgr.sign(ser=bobSrdr.raw, verfers=bobK.verfers)

        msg = bytearray(bobSrdr.raw)
        counter = coring.Counter(code=coring.CtrDex.ControllerIdxSigs,
                                 count=len(sigers))
        msg.extend(counter.qb64b)
        for siger in sigers:
            msg.extend(siger.qb64b)

        assert msg == bytearray(
            b'{"v":"KERI10JSON000107_","i":"EiBlVttjqvySMbA4ShN19rSrz3D0ioNW-U'
            b'j92Ri7XnFE","s":"1","t":"ixn","p":"EvP2kWxEjTMI3auc6x64EpU-nMQZH'
            b'iBeKeuavcGdRB24","a":[{"i":"ErLe2qWp4VCmDp7v_R01tC-ha13ZEZY0VGcg'
            b'YtPRhqPs","s":"0","d":"ESDuaqpoI8-HLD8-eLijUMZpXqYFkNArJFDvt3ABY'
            b'r9I"}]}-AABAAZ4V2cSIXYEPg5BtkJSHVBj-A0dGI6rH2XGaVt1kewqGeJjpy4uz'
            b'ObPWnoBpaEojFa5AnrUJEgMytORoWMqEhCw')

        # apply msg to bob's Kevery
        bobKvy.process(ims=bytearray(msg))  # process local copy of msg
        assert bobK.serder.diger.qb64 == bobSrdr.dig  # key state updated so event was validated
        assert bobK.serder.diger.qb64 == 'EtzXPztLsGC5DGyooSdHdBGIOHjhblBWtZ_AOhGS-hDE'

        # apply msg to del's Kevery
        delKvy.process(ims=bytearray(msg))  # process remote copy of msg
        assert delKvy.kevers[bobPre].serder.diger.qb64 == bobSrdr.dig

        # now create msg with Del's delegated inception event
        sigers = delMgr.sign(ser=delSrdr.raw, verfers=verfers)

        msg = bytearray(delSrdr.raw)
        counter = coring.Counter(code=coring.CtrDex.ControllerIdxSigs,
                                 count=len(sigers))
        msg.extend(counter.qb64b)
        for siger in sigers:
            msg.extend(siger.qb64b)

        assert msg == bytearray(
            b'{"v":"KERI10JSON000165_","i":"ErLe2qWp4VCmDp7v_R01tC-ha13ZEZY0VG'
            b'cgYtPRhqPs","s":"0","t":"dip","kt":"1","k":["DuK1x8ydpucu3480Jpd'
            b'1XBfjnCwb3dZ3x5b1CJmuUphA"],"n":"EWWkjZkZDXF74O2bOQ4H5hu4nXDlKg2'
            b'm4CBEBkUxibiU","wt":"0","w":[],"c":[],"da":{"i":"EiBlVttjqvySMbA'
            b'4ShN19rSrz3D0ioNW-Uj92Ri7XnFE","s":"1","t":"ixn","p":"EvP2kWxEjT'
            b'MI3auc6x64EpU-nMQZHiBeKeuavcGdRB24"}}-AABAADv-a3LeXEStuY1LHknepu'
            b'J7mBcTByugqQ1TNRMrIa0rctfjKsh-hkkkpwDj6M_OLLaFtLqBpmdNTUgBPANLzCQ'
        )

        # apply Del's delegated inception event message to bob's Kevery
        bobKvy.process(ims=bytearray(msg))  # process local copy of msg
        assert delPre in bobKvy.kevers  # successfully validated
        delK = bobKvy.kevers[delPre]
        assert delK.delegated
        assert delK.serder.diger.qb64 == delSrdr.dig  # key state updated so event was validated
        assert delK.serder.diger.qb64 == 'ESDuaqpoI8-HLD8-eLijUMZpXqYFkNArJFDvt3ABYr9I'

        # apply msg to del's Kevery
        delKvy.process(ims=bytearray(msg))  # process remote copy of msg
        assert delKvy.kevers[delPre].serder.diger.qb64 == delSrdr.dig

        # Setup Del rotation event assuming that Bob's next event will be an ixn delegating event
        verfers, digers, cst, nst = delMgr.rotate(pre=delPre, temp=True)

        seal = eventing.SealLocation(i=bobK.prefixer.qb64,
                                     s="{:x}".format(bobK.sn + 1),
                                     t=coring.Ilks.ixn,
                                     p=bobK.serder.diger.qb64)

        assert seal._asdict() == {
            'i': 'EiBlVttjqvySMbA4ShN19rSrz3D0ioNW-Uj92Ri7XnFE',
            's': '2',
            't': 'ixn',
            'p': 'EtzXPztLsGC5DGyooSdHdBGIOHjhblBWtZ_AOhGS-hDE'
        }

        delSrdr = eventing.deltate(
            pre=delK.prefixer.qb64,
            keys=[verfer.qb64 for verfer in verfers],
            dig=delK.serder.diger.qb64,
            seal=seal,
            sn=delK.sn + 1,
            nxt=coring.Nexter(digs=[diger.qb64 for diger in digers]).qb64)

        assert delSrdr.dig == 'E-dZsWLp2IIPVDbGdGS-yvuw4HeV_w_w76FHsofmuiq0'

        # Now create delegating rotation event
        seal = eventing.SealEvent(i=delK.prefixer.qb64,
                                  s=delSrdr.ked["s"],
                                  d=delSrdr.dig)
        bobSrdr = eventing.interact(pre=bobK.prefixer.qb64,
                                    dig=bobK.serder.diger.qb64,
                                    sn=bobK.sn + 1,
                                    data=[seal._asdict()])

        sigers = bobMgr.sign(ser=bobSrdr.raw, verfers=bobK.verfers)

        msg = bytearray(bobSrdr.raw)
        counter = coring.Counter(code=coring.CtrDex.ControllerIdxSigs,
                                 count=len(sigers))
        msg.extend(counter.qb64b)
        for siger in sigers:
            msg.extend(siger.qb64b)

        assert msg == bytearray(
            b'{"v":"KERI10JSON000107_","i":"EiBlVttjqvySMbA4ShN19rSrz3D0ioNW-U'
            b'j92Ri7XnFE","s":"2","t":"ixn","p":"EtzXPztLsGC5DGyooSdHdBGIOHjhb'
            b'lBWtZ_AOhGS-hDE","a":[{"i":"ErLe2qWp4VCmDp7v_R01tC-ha13ZEZY0VGcg'
            b'YtPRhqPs","s":"1","d":"E-dZsWLp2IIPVDbGdGS-yvuw4HeV_w_w76FHsofmu'
            b'iq0"}]}-AABAAmloDxOwz6ztvRR_4N8Hn-6ZJk6_0nQhfNE7bzX6NpJRfYDwmUw3'
            b'rXod0g46iFOLqEWw12oaFVzVH85NYAh67Ag')

        # apply msg to bob's Kevery
        bobKvy.process(ims=bytearray(msg))  # process local copy of msg
        assert bobK.serder.diger.qb64 == bobSrdr.dig  # key state updated so event was validated

        # apply msg to del's Kevery
        delKvy.process(ims=bytearray(msg))  # process remote copy of msg
        assert delKvy.kevers[bobPre].serder.diger.qb64 == bobSrdr.dig

        # now create msg from Del's delegated rotation event
        sigers = delMgr.sign(ser=delSrdr.raw, verfers=verfers)

        msg = bytearray(delSrdr.raw)
        counter = coring.Counter(code=coring.CtrDex.ControllerIdxSigs,
                                 count=len(sigers))
        msg.extend(counter.qb64b)
        for siger in sigers:
            msg.extend(siger.qb64b)

        assert msg == bytearray(
            b'{"v":"KERI10JSON0001a1_","i":"ErLe2qWp4VCmDp7v_R01tC-ha13ZEZY0VG'
            b'cgYtPRhqPs","s":"1","t":"drt","p":"ESDuaqpoI8-HLD8-eLijUMZpXqYFk'
            b'NArJFDvt3ABYr9I","kt":"1","k":["DTf6QZWoet154o9wvzeMuNhLQRr8JaAU'
            b'eiC6wjB_4_08"],"n":"E8kyiXDfkE7idwWnAZQjHbUZMz-kd_yIMH0miptIFFPo'
            b'","wt":"0","wr":[],"wa":[],"a":[],"da":{"i":"EiBlVttjqvySMbA4ShN'
            b'19rSrz3D0ioNW-Uj92Ri7XnFE","s":"2","t":"ixn","p":"EtzXPztLsGC5DG'
            b'yooSdHdBGIOHjhblBWtZ_AOhGS-hDE"}}-AABAAXcUl6KlY4VOx8ZumFMc0uR4iH'
            b'BGmPQo4IAx0nIiiEDB_u2ewkvgIDIp1ELDGxfc2VVUkl38Z7PqwydBdpIK0DA')

        # apply Del's delegated inception event message to bob's Kevery
        bobKvy.process(ims=bytearray(msg))  # process local copy of msg
        assert delK.delegated
        assert delK.serder.diger.qb64 == delSrdr.dig  # key state updated so event was validated
        assert delK.serder.diger.qb64 == 'E-dZsWLp2IIPVDbGdGS-yvuw4HeV_w_w76FHsofmuiq0'

        # apply msg to del's Kevery
        delKvy.process(ims=bytearray(msg))  # process remote copy of msg
        assert delKvy.kevers[delPre].serder.diger.qb64 == delSrdr.dig

    assert not os.path.exists(delKS.path)
    assert not os.path.exists(delDB.path)
    assert not os.path.exists(bobKS.path)
    assert not os.path.exists(bobDB.path)
    """End Test"""
示例#3
0
def test_manager():
    """
    test Manager class
    """
    manager = keeping.Manager()
    assert isinstance(manager, keeping.Manager)
    assert isinstance(manager.keeper, keeping.Keeper)
    assert manager.keeper.opened

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

    raw = b'0123456789abcdef'
    salt = coring.Salter(raw=raw).qb64
    stem = "red"

    assert salt == '0AMDEyMzQ1Njc4OWFiY2RlZg'

    ser = bytes(
        b'{"vs":"KERI10JSON0000fb_","pre":"EvEnZMhz52iTrJU8qKwtDxzmypyosgG'
        b'70m6LIjkiCdoI","sn":"0","ilk":"icp","sith":"1","keys":["DSuhyBcP'
        b'ZEZLK-fcw5tzHn2N46wRCG_ZOoeKtWTOunRA"],"nxt":"EPYuj8mq_PYYsoBKkz'
        b'X1kxSPGYBWaIya3slgCOyOtlqU","toad":"0","wits":[],"cnfg":[]}-AABA'
        b'ApYcYd1cppVg7Inh2YCslWKhUwh59TrPpIoqWxN2A38NCbTljvmBPBjSGIFDBNOv'
        b'VjHpdZlty3Hgk6ilF8pVpAQ')

    with keeping.openKS() as keeper:
        manager = keeping.Manager(keeper=keeper, salt=salt)
        assert manager.keeper.opened
        assert manager._pidx == 0
        assert manager._salt == ''  # zeroed out
        assert manager._tier == coring.Tiers.low

        # salty algorithm incept
        verfers, digers, cst, nst = manager.incept(
            salt=salt, temp=True)  # algo default salty
        assert len(verfers) == 1
        assert len(digers) == 1
        assert cst == '1'
        assert nst == '1'
        assert manager.getPidx() == 1

        spre = verfers[0].qb64b
        assert spre == b'DVG3IcCNK4lpFfpMM-9rfkY3XVUcCu5o5cxzv1lgMqxM'

        pp = json.loads(bytes(manager.keeper.getPrm(key=spre)).decode("utf-8"))
        pp = helping.datify(keeping.PrePrm, pp)
        assert pp.pidx == 0
        assert pp.algo == keeping.Algos.salty
        assert pp.salt == salt
        assert pp.stem == ''
        assert pp.tier == coring.Tiers.low

        ps = json.loads(bytes(manager.keeper.getSit(key=spre)).decode("utf-8"))
        ps = helping.datify(keeping.PreSit, ps)
        assert ps.old.pubs == []
        assert len(ps.new.pubs) == 1
        assert ps.new.pubs == ['DVG3IcCNK4lpFfpMM-9rfkY3XVUcCu5o5cxzv1lgMqxM']
        assert ps.new.ridx == 0
        assert ps.new.kidx == 0
        assert len(ps.nxt.pubs) == 1
        assert ps.nxt.pubs == ['DcHJWO4GszUP0rvVO4Tl2rUdUM1Ln5osP7BwiUeJWhdc']
        assert ps.nxt.ridx == 1
        assert ps.nxt.kidx == 1

        keys = [verfer.qb64 for verfer in verfers]
        assert keys == ps.new.pubs

        # test .pubs db
        pl = json.loads(
            bytes(manager.keeper.getPubs(
                key=keeping.riKey(spre, ps.new.ridx))).decode("utf-8"))
        assert pl == ps.new.pubs

        pl = json.loads(
            bytes(manager.keeper.getPubs(
                key=keeping.riKey(spre, ps.nxt.ridx))).decode("utf-8"))
        assert pl == ps.nxt.pubs

        digs = [diger.qb64 for diger in digers]
        assert digs == ['E8UYvbKn7KYw9e4F2DR-iduGtdA1o16ePAYjpyCYSeYo']

        oldspre = spre
        spre = b'DCu5o5cxzv1lgMqxMVG3IcCNK4lpFfpMM-9rfkY3XVUc'
        manager.move(old=oldspre, new=spre)

        # test .pubs db after move
        pl = json.loads(
            bytes(manager.keeper.getPubs(
                key=keeping.riKey(spre, ps.new.ridx))).decode("utf-8"))
        assert pl == ps.new.pubs

        pl = json.loads(
            bytes(manager.keeper.getPubs(
                key=keeping.riKey(spre, ps.nxt.ridx))).decode("utf-8"))
        assert pl == ps.nxt.pubs

        psigers = manager.sign(ser=ser, pubs=ps.new.pubs)
        for siger in psigers:
            assert isinstance(siger, coring.Siger)
        vsigers = manager.sign(ser=ser, verfers=verfers)
        psigs = [siger.qb64 for siger in psigers]
        vsigs = [siger.qb64 for siger in vsigers]
        assert psigs == vsigs
        assert psigs == [
            'AAGu9G-EJ0zrRjrDKnHszLVcwhbkSRxniDJFmB2eWcRiFzNFw1QM5GHQnmnXz385SgunZH4sLidCMyzhJWmp1IBw'
        ]

        # Test sign with indices
        indices = [3]

        # Test with pubs list
        psigers = manager.sign(ser=ser, pubs=ps.new.pubs, indices=indices)
        for siger in psigers:
            assert isinstance(siger, coring.Siger)
        assert psigers[0].index == indices[0]
        psigs = [siger.qb64 for siger in psigers]
        assert psigs == [
            'ADGu9G-EJ0zrRjrDKnHszLVcwhbkSRxniDJFmB2eWcRiFzNFw1QM5GHQnmnXz385SgunZH4sLidCMyzhJWmp1IBw'
        ]

        # Test with verfers list
        vsigers = manager.sign(ser=ser, verfers=verfers, indices=indices)
        for siger in vsigers:
            assert isinstance(siger, coring.Siger)
        assert psigers[0].index == indices[0]
        vsigs = [siger.qb64 for siger in vsigers]
        assert vsigs == psigs

        pcigars = manager.sign(ser=ser, pubs=ps.new.pubs, indexed=False)
        for cigar in pcigars:
            assert isinstance(cigar, coring.Cigar)
        vcigars = manager.sign(ser=ser, verfers=verfers, indexed=False)
        psigs = [cigar.qb64 for cigar in pcigars]
        vsigs = [cigar.qb64 for cigar in vcigars]
        assert psigs == vsigs
        assert psigs == [
            '0BGu9G-EJ0zrRjrDKnHszLVcwhbkSRxniDJFmB2eWcRiFzNFw1QM5GHQnmnXz385SgunZH4sLidCMyzhJWmp1IBw'
        ]

        # salty algorithm rotate
        oldpubs = [verfer.qb64 for verfer in verfers]
        verfers, digers, cst, nst = manager.rotate(pre=spre.decode("utf-8"))
        assert len(verfers) == 1
        assert len(digers) == 1
        assert cst == '1'
        assert nst == '1'

        pp = json.loads(bytes(manager.keeper.getPrm(key=spre)).decode("utf-8"))
        pp = helping.datify(keeping.PrePrm, pp)
        assert pp.pidx == 0
        assert pp.algo == keeping.Algos.salty
        assert pp.salt == salt
        assert pp.stem == ''
        assert pp.tier == coring.Tiers.low

        ps = json.loads(bytes(manager.keeper.getSit(key=spre)).decode("utf-8"))
        ps = helping.datify(keeping.PreSit, ps)
        assert ps.old.pubs == ['DVG3IcCNK4lpFfpMM-9rfkY3XVUcCu5o5cxzv1lgMqxM']
        assert len(ps.new.pubs) == 1
        assert ps.new.pubs == ['DcHJWO4GszUP0rvVO4Tl2rUdUM1Ln5osP7BwiUeJWhdc']
        assert ps.new.ridx == 1
        assert ps.new.kidx == 1
        assert len(ps.nxt.pubs) == 1
        assert ps.nxt.pubs == ['DRpGly44ejh01ur4ltL_LVrYcyqVCQyVLJnqWrVa57Yc']
        assert ps.nxt.ridx == 2
        assert ps.nxt.kidx == 2

        keys = [verfer.qb64 for verfer in verfers]
        assert keys == ps.new.pubs

        digs = [diger.qb64 for diger in digers]
        assert digs == ['EJUzDm_HbdIZDp94OlIoZH1gcaSdWLZhJwqKz2rVJZrc']

        assert oldpubs == ps.old.pubs

        # salty algorithm rotate
        oldpubs = [verfer.qb64 for verfer in verfers]
        deadpubs = ps.old.pubs

        verfers, digers, cst, nst = manager.rotate(pre=spre.decode("utf-8"))
        assert cst == '1'
        assert nst == '1'
        pp = json.loads(bytes(manager.keeper.getPrm(key=spre)).decode("utf-8"))
        pp = helping.datify(keeping.PrePrm, pp)
        assert pp.pidx == 0

        ps = json.loads(bytes(manager.keeper.getSit(key=spre)).decode("utf-8"))
        ps = helping.datify(keeping.PreSit, ps)

        assert oldpubs == ps.old.pubs

        for pub in deadpubs:
            assert not manager.keeper.getPri(key=pub.encode("utf-8"))

        # test .pubs db
        pl = json.loads(
            bytes(manager.keeper.getPubs(
                key=keeping.riKey(spre, ps.new.ridx))).decode("utf-8"))
        assert pl == ps.new.pubs

        pl = json.loads(
            bytes(manager.keeper.getPubs(
                key=keeping.riKey(spre, ps.nxt.ridx))).decode("utf-8"))
        assert pl == ps.nxt.pubs

        # salty algorithm rotate to null
        verfers, digers, cst, nst = manager.rotate(pre=spre.decode("utf-8"),
                                                   count=0)
        assert cst == '1'
        assert nst == '0'

        pp = json.loads(bytes(manager.keeper.getPrm(key=spre)).decode("utf-8"))
        pp = helping.datify(keeping.PrePrm, pp)
        assert pp.pidx == 0

        ps = json.loads(bytes(manager.keeper.getSit(key=spre)).decode("utf-8"))
        ps = helping.datify(keeping.PreSit, ps)

        assert digers == []
        assert ps.nxt.pubs == []

        #  attempt to rotate after null
        with pytest.raises(ValueError) as ex:  # attempt to reincept same pre
            verfers, digers, cst, nst = manager.rotate(
                pre=spre.decode("utf-8"))
        assert ex.value.args[0].startswith(
            'Attempt to rotate nontransferable ')

        # randy algo incept
        verfers, digers, cst, nst = manager.incept(algo=keeping.Algos.randy)
        assert len(verfers) == 1
        assert len(digers) == 1
        assert cst == '1'
        assert nst == '1'
        assert manager.getPidx() == 2
        rpre = verfers[0].qb64b

        pp = json.loads(bytes(manager.keeper.getPrm(key=rpre)).decode("utf-8"))
        pp = helping.datify(keeping.PrePrm, pp)
        assert pp.pidx == 1
        assert pp.algo == keeping.Algos.randy
        assert pp.salt == ''
        assert pp.stem == ''
        assert pp.tier == ''

        ps = json.loads(bytes(manager.keeper.getSit(key=rpre)).decode("utf-8"))
        ps = helping.datify(keeping.PreSit, ps)
        assert ps.old.pubs == []
        assert len(ps.new.pubs) == 1
        assert ps.new.ridx == 0
        assert ps.new.kidx == 0
        assert len(ps.nxt.pubs) == 1
        assert ps.nxt.ridx == 1
        assert ps.nxt.kidx == 1

        keys = [verfer.qb64 for verfer in verfers]
        for key in keys:
            val = bytes(manager.keeper.getPri(key.encode("utf-8")))

        digs = [diger.qb64 for diger in digers]
        assert len(digs) == 1

        oldrpre = rpre
        rpre = b'DMqxMVG3IcCNK4lpFfCu5o5cxzv1lgpMM-9rfkY3XVUc'
        manager.move(old=oldrpre, new=rpre)

        # randy algorithm rotate
        oldpubs = [verfer.qb64 for verfer in verfers]

        verfers, digers, cst, nst = manager.rotate(pre=rpre.decode("utf-8"))
        assert cst == '1'
        assert nst == '1'

        pp = json.loads(bytes(manager.keeper.getPrm(key=rpre)).decode("utf-8"))
        pp = helping.datify(keeping.PrePrm, pp)
        assert pp.pidx == 1

        ps = json.loads(bytes(manager.keeper.getSit(key=rpre)).decode("utf-8"))
        ps = helping.datify(keeping.PreSit, ps)

        assert oldpubs == ps.old.pubs

        # randy algo incept with null nxt
        verfers, digers, cst, nst = manager.incept(algo=keeping.Algos.randy,
                                                   ncount=0)
        assert manager.getPidx() == 3
        rpre = verfers[0].qb64b
        assert cst == '1'
        assert nst == '0'

        pp = json.loads(bytes(manager.keeper.getPrm(key=rpre)).decode("utf-8"))
        pp = helping.datify(keeping.PrePrm, pp)
        assert pp.pidx == 2

        ps = json.loads(bytes(manager.keeper.getSit(key=rpre)).decode("utf-8"))
        ps = helping.datify(keeping.PreSit, ps)

        assert digers == []
        assert ps.nxt.pubs == []

        #  attempt to rotate after null
        with pytest.raises(ValueError) as ex:  # attempt to reincept same pre
            verfers, digers, cst, nst = manager.rotate(
                pre=rpre.decode("utf-8"))

        # salty algorithm incept with stem
        verfers, digers, cst, nst = manager.incept(
            salt=salt, stem=stem, temp=True)  # algo default salty
        assert len(verfers) == 1
        assert len(digers) == 1
        assert cst == '1'
        assert nst == '1'
        assert manager.getPidx() == 4

        spre = verfers[0].qb64b
        assert spre == b'D627iBfehzh966wPzBYjKQuGOSmIkdcR7b14nZv_ULIw'

        pp = json.loads(bytes(manager.keeper.getPrm(key=spre)).decode("utf-8"))
        pp = helping.datify(keeping.PrePrm, pp)
        assert pp.pidx == 3
        assert pp.algo == keeping.Algos.salty
        assert pp.salt == salt
        assert pp.stem == stem == 'red'
        assert pp.tier == coring.Tiers.low

        ps = json.loads(bytes(manager.keeper.getSit(key=spre)).decode("utf-8"))
        ps = helping.datify(keeping.PreSit, ps)
        assert ps.old.pubs == []
        assert len(ps.new.pubs) == 1
        assert ps.new.pubs == ['D627iBfehzh966wPzBYjKQuGOSmIkdcR7b14nZv_ULIw']
        assert ps.new.ridx == 0
        assert ps.new.kidx == 0
        assert len(ps.nxt.pubs) == 1
        assert ps.nxt.pubs == ['DHNnq96NI0Bmle_VINGcgX8_VSpxbl3am7ZT6_66Fe8Q']
        assert ps.nxt.ridx == 1
        assert ps.nxt.kidx == 1

        keys = [verfer.qb64 for verfer in verfers]
        assert keys == ps.new.pubs

        digs = [diger.qb64 for diger in digers]
        assert digs == ['EAQ7QvfBLj0OrGTqzJZGutLJowUht_zBA6213agRQ8hA']

        #  attempt to reincept same first pub
        with pytest.raises(ValueError) as ex:  # attempt to reincept same pre
            verfers, digers, cst, nst = manager.incept(salt=salt,
                                                       stem=stem,
                                                       temp=True)
        assert ex.value.args[0].startswith('Already incepted pre')

        oldspre = spre
        spre = b'DCNK4lpFfpMM-9rfkY3XVUcCu5o5cxzv1lgMqxMVG3Ic'
        manager.move(old=oldspre, new=spre)

        #  attempt to reincept same first pub after move pre
        with pytest.raises(ValueError) as ex:  # attempt to reincept same pre
            verfers, digers, cst, nst = manager.incept(salt=salt,
                                                       stem=stem,
                                                       temp=True)
        assert ex.value.args[0].startswith('Already incepted pre')

        # Create nontransferable keys that are nontransferable identifier prefixes
        verfers, digers, cst, nst = manager.incept(ncount=0,
                                                   salt=salt,
                                                   stem="wit0",
                                                   transferable=False,
                                                   temp=True)
        assert cst == '1'
        assert nst == '0'
        wit0pre = verfers[0].qb64
        assert verfers[
            0].qb64 == 'B5M0jhHM3vTo15w12pOUYRwxJNaIVS96wSqbFZH-inyc'
        assert verfers[0].code == coring.MtrDex.Ed25519N
        assert not digers

        verfers, digers, cst, nst = manager.incept(ncount=0,
                                                   salt=salt,
                                                   stem="wit1",
                                                   transferable=False,
                                                   temp=True)
        assert cst == '1'
        assert nst == '0'
        wit1pre = verfers[0].qb64
        assert verfers[
            0].qb64 == 'BAH_nE1cfiGjEMK0Ac8U8N51npjBOjyZt3D-_QA4c4y0'
        assert verfers[0].code == coring.MtrDex.Ed25519N
        assert not digers

        assert wit0pre != wit1pre

        # test .ingest of sequences of keys
        secrecies = [
            ['ArwXoACJgOleVZ2PY7kXn7rA0II0mHYDhc6WrBH8fDAc'],
            ['A6zz7M08-HQSFq92sJ8KJOT2cZ47x7pXFQLPB0pckB3Q'],
            ['AcwFTk-wgk3ZT2buPRIbK-zxgPx-TKbaegQvPEivN90Y'],
            ['Alntkt3u6dDgiQxTATr01dy8M72uuaZEf9eTdM-70Gk8'],
            ['A1-QxDkso9-MR1A8rZz_Naw6fgaAtayda8hrbkRVVu1E'],
            ['AKuYMe09COczwf2nIoD5AE119n7GLFOVFlNLxZcKuswc'],
            ['AxFfJTcSuEE11FINfXMqWttkZGnUZ8KaREhrnyAXTsjw'],
            ['ALq-w1UKkdrppwZzGTtz4PWYEeWm0-sDHzOv5sq96xJY'],
        ]

        pidx, rsalt, rtier = manager.setup()  #  verify current state
        assert pidx == 6
        assert rsalt == salt == '0AMDEyMzQ1Njc4OWFiY2RlZg'
        assert rtier == coring.Tiers.low
        verferies, digers = manager.ingest(secrecies=secrecies)
        publicies = []
        for verfers in verferies:
            publicies.append([verfer.qb64 for verfer in verfers])
        assert publicies == [['DSuhyBcPZEZLK-fcw5tzHn2N46wRCG_ZOoeKtWTOunRA'],
                             ['DVcuJOOJF1IE8svqEtrSuyQjGTd2HhfAkt9y2QkUtFJI'],
                             ['DT1iAhBWCkvChxNWsby2J0pJyxBIxbAtbLA0Ljx-Grh8'],
                             ['DKPE5eeJRzkRTMOoRGVd2m18o8fLqM2j9kaxLhV3x8AQ'],
                             ['D1kcBE7h0ImWW6_Sp7MQxGYSshZZz6XM7OiUE5DXm0dU'],
                             ['D4JDgo3WNSUpt-NG14Ni31_GCmrU0r38yo7kgDuyGkQM'],
                             ['DVjWcaNX2gCkHOjk6rkmqPBCxkRCqwIJ-3OjdYmMwxf4'],
                             ['DT1nEDepd6CSAMCE7NY_jlLdG6_mKUlKS_mW-2HJY1hg']]

        ipre = publicies[0][0]

        # test .pris db
        for i, pubs in enumerate(publicies):
            pri0 = bytes(manager.keeper.getPri(key=pubs[0]))
            assert pri0.decode("utf-8") == secrecies[i][0]
            for pub in pubs:
                pri = bytes(manager.keeper.getPri(key=pub))
                assert pri

        pp = json.loads(bytes(manager.keeper.getPrm(key=ipre)).decode("utf-8"))
        pp = helping.datify(keeping.PrePrm, pp)
        assert pp.pidx == 6

        assert manager.getPidx() == 7

        ps = json.loads(bytes(manager.keeper.getSit(key=ipre)).decode("utf-8"))
        ps = helping.datify(keeping.PreSit, ps)
        assert ps.new.ridx == 7
        assert ps.new.pubs == publicies[ps.new.ridx]

        # test .pubs db
        for i, pubs in enumerate(publicies):
            pl = json.loads(
                bytes(manager.keeper.getPubs(
                    key=keeping.riKey(ipre, i))).decode("utf-8"))
            assert pl == pubs

        #  nxt pubs
        pl = json.loads(
            bytes(manager.keeper.getPubs(
                key=keeping.riKey(ipre, i + 1))).decode("utf-8"))
        assert pl

        assert [diger.qb64 for diger in digers
                ] == ['Ewt_7B0gfSE7DnMtmNEHiy8BGPVw5at2-e_JgJ1jAfEc']

        for i in range(len(publicies)):
            verfers, digers, cst, nst = manager.replay(ipre, i)
            assert verfers[0].qb64 == publicies[i][0]
            assert digers
            assert cst == nst == '1'

        with pytest.raises(IndexError):  # Test end of replay
            verfers, digers = manager.replay(ipre, i + 1)

        with pytest.raises(ValueError):  # Test past end of replay
            verfers, digers = manager.replay(ipre, i + 2)

        # test .ingest multi-sig of sequences of keys
        secrecies = [[
            'AgjD4nRlycmM5cPcAkfOATAp8wVldRsnc9f1tiwctXlw',
            'AKUotEE0eAheKdDJh9QvNmSEmO_bjIav8V_GmctGpuCQ',
            'AK-nVhMMJciMPvmF5VZE_9H-nhrgng9aJWf7_UHPtRNM'
        ], ['AT2cx-P5YUjIw_SLCHQ0pqoBWGk9s4N1brD-4pD_ANbs'],
                     [
                         'Ap5waegfnuP6ezC18w7jQiPyQwYYsp9Yv9rYMlKAYL8k',
                         'Aqlc_FWWrxpxCo7R12uIz_Y2pHUH2prHx1kjghPa8jT8',
                         'AagumsL8FeGES7tYcnr_5oN6qcwJzZfLKxoniKUpG4qc'
                     ], ['ADW3o9m3udwEf0aoOdZLLJdf1aylokP0lwwI_M2J9h0s']]

        pidx, rsalt, rtier = manager.setup()  #  verify current state
        assert pidx == 7
        assert rsalt == salt == '0AMDEyMzQ1Njc4OWFiY2RlZg'
        assert rtier == coring.Tiers.low
        verferies, digers = manager.ingest(secrecies=secrecies, ncount=3)
        publicies = []
        for verfers in verferies:
            publicies.append([verfer.qb64 for verfer in verfers])
        assert publicies == [
            [
                'D8KY1sKmgyjAiUDdUBPNPyrSz_ad_Qf9yzhDNZlEKiMc',
                'DbWeWTNGXPMQrVuJmScNQn81YF7T2fhh2kXwT8E_NbeI',
                'Dmis7BM1brr-1r4DgdO5KMcCf8AnGcUUPhZYUxprI97s'
            ], ['DfHMsSg0CJCou4erOqaJDr3OyDEikBp5QRp7HjcJGdgw'],
            [
                'DOaXCkU3Qd0oBSYxGfYtJxUbN6U7VjZiKthPHIHbzabs',
                'DLOmEabR-cYJLMrAd0HvQC4lecbF-j2r7w3UQIY3mGMQ',
                'DAIyL2yT9nU6kChGXWce8d6q07l0vBLPNImw_f9bazeQ'
            ], ['D69EflciVP9zgsihNU14Dbm2bPXoNGxKHK_BBVFMQ-YU']
        ]

        ipre = publicies[0][0]

        # test .pris db
        for i, pubs in enumerate(publicies):
            pri0 = bytes(manager.keeper.getPri(key=pubs[0]))
            assert pri0.decode("utf-8") == secrecies[i][0]
            for pub in pubs:
                pri = bytes(manager.keeper.getPri(key=pub))
                assert pri

        pp = json.loads(bytes(manager.keeper.getPrm(key=ipre)).decode("utf-8"))
        pp = helping.datify(keeping.PrePrm, pp)
        assert pp.pidx == 7

        assert manager.getPidx() == 8

        ps = json.loads(bytes(manager.keeper.getSit(key=ipre)).decode("utf-8"))
        ps = helping.datify(keeping.PreSit, ps)
        assert ps.new.ridx == 3
        assert ps.new.kidx == 7
        assert ps.new.pubs == publicies[ps.new.ridx]

        assert len(ps.nxt.pubs) == 3

        # test .pubs db
        for i, pubs in enumerate(publicies):
            pl = json.loads(
                bytes(manager.keeper.getPubs(
                    key=keeping.riKey(ipre, i))).decode("utf-8"))
            assert pl == pubs

        #  nxt pubs
        pl = json.loads(
            bytes(manager.keeper.getPubs(
                key=keeping.riKey(ipre, i + 1))).decode("utf-8"))
        assert pl

        assert [diger.qb64 for diger in digers] == [
            'E7Ch-T3dCZZ_i0u1ACi_Yv1lyyAMoQCT5ar81eUGoPYY',
            'EhwPuWbyrJRyU5HpJaoJrq04biTLWx3heNY3TvQrlbU8',
            'EJKLXis7QLnodqvtkbkTUKdciTuM-yzhEPUzS9jtxS6Y'
        ]

        for i in range(len(publicies)):
            verfers, digers, cst, nst = manager.replay(ipre, i)
            assert verfers[0].qb64 == publicies[i][0]
            assert digers

        with pytest.raises(IndexError):  # Test end of replay
            verfers, digers = manager.replay(ipre, i + 1)

        with pytest.raises(ValueError):  # Test past end of replay
            verfers, digers = manager.replay(ipre, i + 2)

    assert not os.path.exists(manager.keeper.path)
    assert not manager.keeper.opened
    """End Test"""
示例#4
0
def test_weighted():
    """
    Test multisig with weighted threshold

    """
    wesSalt = coring.Salter(raw=b'0123456789abcdef').qb64  # init wes Salter

    # init event DB and keep DB
    with dbing.openDB(name="wes") as wesDB, keeping.openKS(
            name="wes") as wesKS:
        # Init key pair manager
        wesMgr = keeping.Manager(keeper=wesKS, salt=wesSalt)

        # Init Kevery with event DB
        wesKvy = eventing.Kevery(db=wesDB)

        # create inception event for Wes with 3 keys each in incept and next sets
        # defaults are algo salty and rooted
        sith = ["1/2", "1/2", "1/2"]  #  2 of 3 but with weighted threshold
        nxtsith = ["1/2", "1/2", "1/2"]
        verfers, digers, cst, nst = wesMgr.incept(icount=3,
                                                  isith=sith,
                                                  ncount=3,
                                                  nsith=nxtsith,
                                                  stem='wes',
                                                  temp=True)
        assert cst == nst == sith

        wesSrdr = eventing.incept(
            keys=[verfer.qb64 for verfer in verfers],
            sith=sith,
            nxt=coring.Nexter(sith=nxtsith,
                              digs=[diger.qb64 for diger in digers]).qb64,
            code=coring.MtrDex.Blake3_256)

        wesPre = wesSrdr.ked["i"]

        wesMgr.move(old=verfers[0].qb64,
                    new=wesPre)  # move key pair label to prefix

        sigers = wesMgr.sign(ser=wesSrdr.raw, verfers=verfers)

        msg = bytearray(wesSrdr.raw)
        counter = coring.Counter(coring.CtrDex.ControllerIdxSigs,
                                 count=len(sigers))
        msg.extend(counter.qb64b)
        for siger in sigers:
            msg.extend(siger.qb64b)

        assert msg == bytearray(
            b'{"v":"KERI10JSON00015b_","i":"EX0WJtv6vc0IWzOqa92Pv9v9pgs1f0BfIV'
            b'rSch648Zf0","s":"0","t":"icp","kt":["1/2","1/2","1/2"],"k":["DK4'
            b'OJI8JOr6oEEUMeSF_X-SbKysfwpKwW-ho5KARvH5c","D1RZLgYke0GmfZm-CH8A'
            b'sW4HoTU4m-2mFgu8kbwp8jQU","DBVwzum-jPfuUXUcHEWdplB4YcoL3BWGXK0TM'
            b'oF_NeFU"],"n":"EhJGhyJQTpSlZ9oWfQT-lHNl1woMazLC42O89fRHocTI","bt'
            b'":"0","b":[],"c":[],"a":[]}-AADAAKWMK8k4Po2A0rBrUBjBom73DfTCNg_b'
            b'iwR-_LWm6DMHZHGDfCuOmEyR8sEdp7cPxhsavq4istIZ_QQ42yyUcDAABeTClYkN'
            b'-yjbW3Kz3ot6MvAt5Se-jmcjhu-Cfsv4m_GKYgc_qwel1SbAcqF0TiY0EHFdjNKv'
            b'Ikg3q19KlhSbuBgACA6QMnsnZuy66xrZVg3c84mTodZCEvOFrAIDQtm8jeXeCTg7'
            b'yFauoQECZyNIlUnnxVHuk2_Fqi5xK_Lu9Pt76Aw')

        # apply msg to Wes's Kevery
        wesKvy.process(ims=bytearray(msg))  # process local copy of msg
        wesK = wesKvy.kevers[wesPre]  # kever created so event was validated
        assert wesK.prefixer.qb64 == wesPre
        assert wesK.serder.diger.qb64 == wesSrdr.dig  # key state updated so event was validated

        # create interaction event for Wes
        wesSrdr = eventing.interact(pre=wesK.prefixer.qb64,
                                    dig=wesK.serder.diger.qb64,
                                    sn=wesK.sn + 1,
                                    data=[])

        sigers = wesMgr.sign(ser=wesSrdr.raw, verfers=wesK.verfers)

        msg = bytearray(wesSrdr.raw)
        counter = coring.Counter(coring.CtrDex.ControllerIdxSigs,
                                 count=len(sigers))
        msg.extend(counter.qb64b)
        for siger in sigers:
            msg.extend(siger.qb64b)

        assert msg == bytearray(
            b'{"v":"KERI10JSON000098_","i":"EX0WJtv6vc0IWzOqa92Pv9v9pgs1f0BfIV'
            b'rSch648Zf0","s":"1","t":"ixn","p":"EInuqF20s1O0JhVJaOuKCZZDDyw6D'
            b'YxATbiuC5Hv3WXs","a":[]}-AADAAmg8UoBCTlnENiySBpn6j7mR1w0gdkfC8Ai'
            b'aYL5NCFfePp5ZK3CmnP7oQdj_ggiNp9FZNE0Q69A99wNK5FrwzDgABQZZunmJr2X'
            b'3BYgX34-6SOpzkHZQS99ZL0tDsDyUIL7jZhD1e1203Fh6BnkDjzum2au2dGHJcIP'
            b'giFGfDsrI5DwACwwYqqYYdEXeLpx_o6LTSKU-Fz_WF2gebP9Z72bZ75atg7_ZM3H'
            b'g6hhztstGt8TthRQ0TJn6rkvatkbn6yzRjBw')

        # apply msg to wes's Kevery
        wesKvy.process(ims=bytearray(msg))  # process local copy of msg
        assert wesK.serder.diger.qb64 == wesSrdr.dig  # key state updated so event was validated

        # Create rotation event for Wes
        # get current keys as verfers and next digests as digers
        nxtsith = ["1/2", "1/2", "1/2"]  #  2 of 3 but with weighted threshold
        verfers, digers, cst, nst = wesMgr.rotate(pre=wesPre,
                                                  count=3,
                                                  sith=nxtsith,
                                                  temp=True)
        assert nst == nxtsith

        wesSrdr = eventing.rotate(
            pre=wesK.prefixer.qb64,
            keys=[verfer.qb64 for verfer in verfers],
            sith=sith,
            dig=wesK.serder.diger.qb64,
            nxt=coring.Nexter(sith=nxtsith,
                              digs=[diger.qb64 for diger in digers]).qb64,
            sn=wesK.sn + 1,
            data=[])

        sigers = wesMgr.sign(ser=wesSrdr.raw, verfers=verfers)

        msg = bytearray(wesSrdr.raw)
        counter = coring.Counter(coring.CtrDex.ControllerIdxSigs,
                                 count=len(sigers))
        msg.extend(counter.qb64b)
        for siger in sigers:
            msg.extend(siger.qb64b)

        assert bytearray(
            b'{"v":"KERI10JSON000190_","i":"EX0WJtv6vc0IWzOqa92Pv9v9pgs1f0BfIV'
            b'rSch648Zf0","s":"2","t":"rot","p":"EznUtmH2XJF04dyqpUHLzwkNgwk6D'
            b'jbDFbjXVI3UJLe0","kt":["1/2","1/2","1/2"],"k":["DeonYM2bKnAwp6VZ'
            b'cuCXdX72kNFw56czlZ_Tc7XHHVGI","DQghKIy-2do9OkweSgazh3Ql1vCOt5bnc'
            b'5QF8x50tRoU","DNAUn-5dxm6b8Njo01O0jlStMRCjo9FYQA2mfqFW1_JA"],"n"'
            b':"EX5fxvjOg5VuDboWbqnTjTPpXa3nNIm99hlsB1EmhTo8","bt":"0","br":[]'
            b',"ba":[],"a":[]}-AADAAahiASmZJY2KjXKRvVwdRSESmesNsfxUnOQ6buEk6S-'
            b'4rxRdztde_6_CX2Q4MyUSErHMtmLhesrKjenPBairZAQABbnbZ3lOKcKCMmLYtpT'
            b'hDEm-tRTsnEh_8loXpA6G3q1oJZNeVJphJjPm2HR0mX2ptC2DEt6p9i4GH1Y56HY'
            b'TsAgACqF6e_29QkxgXvqDLEUnAIB_XJ7SUhDNpt3cYk6pF1-ULgrhGdZLS1h-c_V'
            b'KpKITRx3ZTvme7sKbvr_NfR-0ECg')

        # apply msg to Wes's Kevery
        wesKvy.process(ims=bytearray(msg))  # process local copy of msg
        assert wesK.serder.diger.qb64 == wesSrdr.dig  # key state updated so event was validated

        # Create rotation event for Wes
        # get current keys as verfers and next digests as digers
        sith = nxtsith  # rotate so nxtsith is now current sith and need new nextsith
        #  2 of first 3 and 1 of last 2
        nxtsith = [["1/2", "1/2", "1/2"], ["1/1", "1/1"]]
        verfers, digers, cst, nst = wesMgr.rotate(pre=wesPre,
                                                  count=5,
                                                  sith=nxtsith,
                                                  temp=True)
        assert cst == sith
        assert nst == nxtsith

        wesSrdr = eventing.rotate(
            pre=wesK.prefixer.qb64,
            keys=[verfer.qb64 for verfer in verfers],
            sith=sith,
            dig=wesK.serder.diger.qb64,
            nxt=coring.Nexter(sith=nxtsith,
                              digs=[diger.qb64 for diger in digers]).qb64,
            sn=wesK.sn + 1,
            data=[])

        sigers = wesMgr.sign(ser=wesSrdr.raw, verfers=verfers)

        msg = bytearray(wesSrdr.raw)
        counter = coring.Counter(coring.CtrDex.ControllerIdxSigs,
                                 count=len(sigers))
        msg.extend(counter.qb64b)
        for siger in sigers:
            msg.extend(siger.qb64b)

        assert msg == bytearray(
            b'{"v":"KERI10JSON000190_","i":"EX0WJtv6vc0IWzOqa92Pv9v9pgs1f0BfIV'
            b'rSch648Zf0","s":"3","t":"rot","p":"EK1BTah7lTyaaol7sLdg9uObplAjP'
            b'o_JOz1m4WPaLJBw","kt":["1/2","1/2","1/2"],"k":["D7WWKDLVwYxYMLAj'
            b'DceIEs66xPMY4Afzx-RQw2x0mQzI","Dmg6Aah8qyKKDiQyNXTiO71QJwizjZfGM'
            b'61BA-s0A5F4","DS3fhKpvPCDL5WmfN4_PkmJMMsSCdRTxG24OQuf_EmHQ"],"n"'
            b':"EcM4iw7fElXWhad8V-za4Px7nBKjndxoh3XZRkohghKY","bt":"0","br":[]'
            b',"ba":[],"a":[]}-AADAAl6FtC6Ynm8RAoa2utkIqJX5xW1ZxIkEH7I_MUCXL0p'
            b'OUT8P0lCgPzn9dvmUagHbzZ4GIwOBqGI-lQJbeESnZBQAB_nJLQJTf9t1NJxNoP5'
            b'gve9QEQbHFkn2aX6O78_bzOGUf01y8KSl-ugi0_B9-w0dk4J7gjYbv7RhG-TmFPE'
            b'mEAAACEpJiOTGarwK4jWZ9ZKue05uYPDW5wp4HC8VIt6R93h7WHLqqLVH1m3n5jd'
            b'AkiM4RFdhUqBwt-jKBfHQmVoBBCA')

        # apply msg to Wes's Kevery
        wesKvy.process(ims=bytearray(msg))  # process local copy of msg
        assert wesK.serder.diger.qb64 == wesSrdr.dig  # key state updated so event was validated

        # Create rotation event for Wes
        # get current keys as verfers and next digests as digers
        sith = nxtsith  # rotate so nxtsith is now current sith and need new nextsith
        #  2 of first 3 and 1 of last 2
        nxtsith = [["1/2", "1/2", "1/2"], ["1/1", "1/1"]]
        verfers, digers, cst, nst = wesMgr.rotate(pre=wesPre,
                                                  count=5,
                                                  sith=nxtsith,
                                                  temp=True)
        assert cst == nst == nxtsith

        wesSrdr = eventing.rotate(
            pre=wesK.prefixer.qb64,
            keys=[verfer.qb64 for verfer in verfers],
            sith=sith,
            dig=wesK.serder.diger.qb64,
            nxt=coring.Nexter(sith=nxtsith,
                              digs=[diger.qb64 for diger in digers]).qb64,
            sn=wesK.sn + 1,
            data=[])

        sigers = wesMgr.sign(ser=wesSrdr.raw, verfers=verfers)

        msg = bytearray(wesSrdr.raw)
        counter = coring.Counter(coring.CtrDex.ControllerIdxSigs,
                                 count=len(sigers))
        msg.extend(counter.qb64b)
        for siger in sigers:
            msg.extend(siger.qb64b)

        assert msg == bytearray(
            b'{"v":"KERI10JSON0001fe_","i":"EX0WJtv6vc0IWzOqa92Pv9v9pgs1f0BfIV'
            b'rSch648Zf0","s":"4","t":"rot","p":"EVfMO5GK8tg4KE8XCelX1s_TG_Hqr'
            b'_kzb3ghIBYvzC6U","kt":[["1/2","1/2","1/2"],["1/1","1/1"]],"k":["'
            b'DToUWoemnetqJoLFIqDI7lxIJEfF0W7xG5ZlqAseVUQc","Drz-IZjko61q-sPMD'
            b'IW6n-0NGFubbXiZhzWZrO_BZ0Wc","DiGwL3hjQqiUgQlFPeA6kRR1EBXX0vSLm9'
            b'b6QhPS8IkQ","Dxj5pcStgZ6CbQ2YktNaj8KLE_g9YAOZF6AL9fyLcWQw","DE5z'
            b'r5eH8EUVQXyAaxWfQUWkGCId-QDCvvxMT77ibj2Q"],"n":"E3in3Z14va0kk4Wq'
            b'd3vcCAojKNtQq7ZTrQaavR8x0yu4","bt":"0","br":[],"ba":[],"a":[]}-A'
            b'AFAAdxx4UfoNYdXckLY9nSYvqYLJzvIRhixshBbqkQ6uwvqaVmwPqmvck0V9xl5x'
            b'3ssVclasm8Ga3FTkcbmbV2jXDgABBWUhku-qDq8wYn5XMQuKzidAsA6bth8-EsCx'
            b'9WwTIqWBR-AecW-3X1haAyJshqplDsS9MnZfVgmSHokwdLnRCQACp2tB0pFRv_C7'
            b'nUXPf9rFKvlWUllrsY6Y1_F4bAOMvyCU-PES4HwyUQv3kQnLxEqnf0fbOYdHJNGo'
            b'sXi-UqL9BAADW89YpsS5m3IASAtXvXEPez-0y11JRP8bAiUUBdIxGB9ms79jPZnQ'
            b'tF3045byf3m0Dvi91Y9d4sh-xkzZ15v9DAAE6QR9qNXnHXLisg4Mbadav9AdMjS4'
            b'uz6jNG1AL6UCa7P90Y53v1V6VRaOPu_RTWXcXYRGqA9BHJ2rLNYWJTJTBA')

        # apply msg to Wes's Kevery
        wesKvy.process(ims=bytearray(msg))  # process local copy of msg
        assert wesK.serder.diger.qb64 == wesSrdr.dig  # key state updated so event was validated

    assert not os.path.exists(wesKS.path)
    assert not os.path.exists(wesDB.path)
    """End Test"""