async def delete_wallet(*args):

    if not args:
        IDname = input('Who dyin? ').strip().lower()
    else:
        for arg in args:
            IDname = arg

    pickle_file = IDname + '.pickle'

    try:
        with open(pickle_file, 'rb') as f:
            name = pickle.load(f)
    except (FileNotFoundError) as e:
        await IDconfig(IDname)

    with open(pickle_file, 'rb') as f:
        name = pickle.load(f)

    msg = '\n Deleting ' + IDname + ' wallet\n'
    print_log(msg)

    try:
        await wallet.delete_wallet(name['wallet_config'],
                                   name['wallet_credentials'])
    except IndyError as ex:
        if ex.error_code == ErrorCode.WalletNotFoundError:
            pass

    # Once a wallet is deleted, the associated pickle path must also be deleted:
    pickle_path = os.path.dirname(os.path.realpath(__file__))
    path = pickle_path + '/' + pickle_file
    os.remove(path)

    print('Goodbye ' + IDname + ' :(')
async def pool_configuration(IP):

    pool_name = input('Pool name?:').strip().lower()
    file_name = pool_name + '.pickle'

    try:
        with open(file_name, 'rb') as f:
            pool_ = pickle.load(f)
    except (FileNotFoundError) as e:
        pool_ = {'name': pool_name}
        genesis_file_path = get_pool_genesis_txn_path(pool_name, IP)
        pool_['config'] = json.dumps({'genesis_txn': str(genesis_file_path)})

        # Set pool PROTOCAL VERSION:

    await pool.set_protocol_version(PROTOCOL_VERSION)
    genesis_file_path = get_pool_genesis_txn_path(pool_['name'], IP)

    # try:
    #     await pool.close_pool_ledger(pool_['handle'])
    # except IndyError as ex:
    #     if ex.error_code == ErrorCode.PoolLedgerInvalidPoolHandle:
    #         pass

    # try:
    #     await pool.delete_pool_ledger_config('pool')
    # except IndyError as ex:
    #     if ex.error_code == ErrorCode.CommonIOError:
    #         pass

    try:
        await pool.create_pool_ledger_config(config_name=pool_['name'],
                                             config=pool_['config'])
        print_log('\n Setting up pool configuration.\n')
    except IndyError as ex:
        if ex.error_code == ErrorCode.PoolLedgerConfigAlreadyExistsError:
            pass
    try:
        pool_['handle'] = await pool.open_pool_ledger(pool_['name'], None)
        print_log('\n Open ledger and get handle\n')
    except IndyError as ex:
        if ex.error_code == ErrorCode.PoolLedgerInvalidPoolHandle:
            pass

    print(genesis_file_path)
    print('Pool handle:', pool_['handle'])

    with open(file_name, 'wb') as f:
        pickle.dump(pool_, f)

    return pool_
async def query_did(IP, *args):  # Same as a GET_NYM request

    # Load input:
    #########################################################################

    pool_ = await pool_configuration(IP)

    if not args:
        sub = input('Submitter? ').strip().lower()
    else:
        for arg in args:
            sub = arg

    pickle_file = sub + '.pickle'

    await ID(sub)
    with open(pickle_file, 'rb') as f:
        name = pickle.load(f)

    submitter = name
    tar = input('Target? ').strip().lower()

    Bdid = tar + '_did'
    target = {'did': name[Bdid]}

    ############################################################################

    print_log('\n Building the GET_NYM request to query trust anchor verkey\n')

    get_nym_request = await ledger.build_get_nym_request(
        submitter_did=submitter['did'], target_did=target['did'])
    print_log('GET_NYM request: ')
    pprint.pprint(json.loads(get_nym_request))

    print_log('\n Sending the Get NYM request to the ledger\n')

    get_nym_response_json = await ledger.submit_request(
        pool_handle=pool_['handle'], request_json=get_nym_request)
    get_nym_response = json.loads(get_nym_response_json)

    print_log('GET_NYM response: ')
    pprint.pprint(get_nym_response)

    return get_nym_response
Exemple #4
0
async def connect(*args):

    if not args:
        Aname = input('Who u? ').strip().lower()
    else:
        for arg in args:
            Aname = arg

    Bname = input('Who dem? ').strip().lower()
    pickle_file = Aname + '.pickle'

    try:
        with open(pickle_file, 'rb') as f:
            A = pickle.load(f)

        await create_wallet(Aname)

    except (FileNotFoundError) as e:

        await IDconfig(Aname)
        await create_wallet(Aname)

    with open(pickle_file, 'rb') as f:
        A = pickle.load(f)

    AdidB = 'did_for_' + Bname
    AkeyB = 'key_for_' + Bname

    if AdidB not in A:

        try:
            print_log(
                '\n Generate and store unique Pairwise Pseudonymous DID and verkey in wallet\n'
            )
            A[AdidB], A[AkeyB] = await did.create_and_store_my_did(
                A['wallet'],
                "{}")  # Returns newly created steward DID and verkey
            # Save new version of A containing the did's and verkeys for the pairwise relationship:
        except IndyError as ex:
            if ex.error_code == ErrorCode.DidAlreadyExistsError:
                pass

    print_log(AdidB + ': ', A[AdidB])
    print_log(AkeyB + ': ', A[AkeyB])

    with open(pickle_file, 'wb') as f:
        pickle.dump(A, f)

    return A, Bname
Exemple #5
0
async def run():

    while True:
        print_log('\n')
        print_log('Welcome to Sovrin:')
        print_log(
            ' ______________________________________________________________')
        print_log('1. Connect to the Sovrin nodes pool.')
        print_log('2. Sovrin Messenger.')
        print_log('3. Create/Load identity.')
        print_log('4. Create/Open wallet for identity.')
        print_log('5. Create/Get DID and verkey for Identity (Public DID).')
        print_log('6. Create Connection (Private DID).')
        print_log('7. Create NYM Request.')
        print_log('8. Query DID (GET_NYM Request).')
        print_log('9. Replace Keys.')
        print_log('10. Get Verkey for DID on the ledger.')
        # print_log('11. Create Schema Request.')
        # print_log('12. Create Get Schema Request.')
        # print_log('13. Create Credential Definition.')
        # print_log('14. Create Prover Link Secret.')
        # print_log('15. Offer Credential.')
        # print_log('16. Request Credential.')
        # print_log('17. Create Credential.')
        # print_log('18. Process and Store Credential.')
        # print_log('19. Build Proof Request.')
        # print_log('20. Fetch Credentials.')
        # print_log('21. Create Proof.')
        # print_log('22. Verify Proof.')
        print_log('11. Clean-up.')
        print_log('12. Delete Identity Owner Wallet.')
        print_log('13. Quit.')
        print_log(
            ' ______________________________________________________________')

        Sov = input('Please select:').strip()

        # Pool config:

        if Sov == '1':

            await pool_configuration(IP)

        # Send secure message:
        elif Sov == '2':

            await messenger(clientname)

        # Create ID:
        elif Sov == '3':

            await ID()  # This step creates/loads and stores a new user ID

        # Create/Open wallet:
        elif Sov == '4':

            await create_wallet()

        # Create DID and Verkey for identity owner:
        elif Sov == '5':

            await did_and_verkey()

        # Create connection:
        elif Sov == '6':

            await connect()

        # Create NYM request:
        elif Sov == '7':

            await nym_request(IP)

        # Create GET_NYM request:
        elif Sov == '8':

            await query_did(IP)

        # Replace Keys:
        elif Sov == '9':

            await replace_keys(IP)

        # Get Verkey for DID on the Ledger:
        elif Sov == '10':

            await get_verkey(IP)

        # # Create Schema Request:
        # elif Sov==11:

        # # Create Get Schema Request:
        # elif Sov==12:

        # # Create Credential Definition:
        # elif Sov==13:

        # # Create Prover Link Secret:
        # elif Sov==14:

        # # Offer Credential:
        # elif Sov==15:

        # # Request Credential:
        # elif Sov==16:

        # # Create Credential:
        # elif Sov==17:

        # # Process and Store Credential:
        # elif Sov==18:

        # # Build Proof Request:
        # elif Sov==19:

        # # Fetch Credentials:
        # elif Sov==20:

        # # Create Proof:
        # elif Sov==21:

        # # Verify Proof:
        # elif Sov==22:

        # Close and Clean-up:
        elif Sov == '11':

            await cleanup(IP)

        elif Sov == '12':

            await delete_wallet()

        elif Sov == '13':

            await cleanup(IP)
            break

        else:
            print('Huh?')
async def nym_request(IP, *args):

    # Load input:
    #########################################################################

    pool_ = await pool_configuration(IP)

    if not args:
        sub = input('Submitter? ').strip().lower()
    else:
        for arg in args:
            sub = arg

    pickle_file = sub + '.pickle'
    await ID(sub)  # Name, wallet and DID/Verkey creation
    with open(pickle_file, 'rb') as f:
        name = pickle.load(f)

    submitter = name

    target = {'name': input('Target? ').strip().lower()}

    print('Submitting NYM Request for?')
    print('1. DID and Verkey FOR ' + target['name'] + '(Connection request).')
    print('2. DID and Verkey FROM ' + target['name'] +
          '(Connection response).')
    print('3. ' + target['name'] + ' DID and Verkey (Verinym).')

    tar = int(input('Please specify number:'))

    AdidB = 'did_for_' + target['name']
    AkeyB = 'key_for_' + target['name']
    BdidA = 'did_from_' + target['name']
    BkeyA = 'key_from_' + target['name']
    Bdid = target['name'] + '_did'
    Bkey = target['name'] + '_key'

    if tar == 1:
        target['did'] = name[AdidB]
        target['verkey'] = name[AkeyB]
        nymrole = None
    elif tar == 2:
        target['did'] = name[BdidA]
        target['verkey'] = name[BkeyA]
        nymrole = None
    else:
        target['did'] = name[Bdid]
        target['verkey'] = name[Bkey]
        nymrole = 'TRUST_ANCHOR'

    #########################################################################

    # Accepted values for nymrole:
    #
    #     :param role: Role of a user NYM record:
    #                          null (common USER)
    #                          TRUST_ANCHOR
    #                          NETWORK_MONITOR
    #                          empty string to reset role
    # :return: Request result as json.

    # print(' - None')
    # print(' - TRUST_ANCHOR')
    # print(' - NETWORK_MONITOR')
    # print(' - empty string to reset role')
    # nymrole = input('Role of target?')

    # Here, we are building the transaction payload that we'll send to write the Trust Anchor identity to the ledger.
    # We submit this transaction under the authority of the steward DID that the ledger already recognizes.
    # This call will look up the private key of the steward DID in our wallet, and use it to sign the transaction.

    print_log('\n Building NYM request\n')
    nym_transaction_request = await ledger.build_nym_request(
        submitter_did=submitter['did'],
        target_did=target['did'],
        ver_key=target['verkey'],
        alias=None,
        role=nymrole)

    print_log('NYM transaction request: ')
    pprint.pprint(json.loads(nym_transaction_request))

    # Now that we have the transaction ready, send it. The building and the sending are separate steps because some
    # clients may want to prepare transactions in one piece of code (e.g., that has access to privileged backend systems),
    # and communicate with the ledger in a different piece of code (e.g., that lives outside the safe internal
    # network).

    print_log('\n Sending NYM request to the ledger\n')
    nym_transaction_response = await ledger.sign_and_submit_request(
        pool_handle=pool_['handle'],
        wallet_handle=submitter['wallet'],
        submitter_did=submitter['did'],
        request_json=nym_transaction_request)

    print_log('NYM transaction response: ')
    pprint.pprint(json.loads(nym_transaction_response))
async def cleanup(IP, *args):  # Do some cleanup.

    # Load variables:
    ##########################################################################

    if not args:
        IDname = input('Who closin? ').strip().lower()
    else:
        for arg in args:
            IDname = arg

    pickle_file = IDname + '.pickle'

    try:
        with open(pickle_file, 'rb') as f:
            name = pickle.load(f)

        try:
            await wallet.close_wallet(name['wallet'])
            msg = '\n Closing ' + name['name'] + ' wallet\n'
            print_log(msg)
        except IndyError as ex:
            if ex.error_code == ErrorCode.WalletInvalidHandle:
                print(name['name'] + ' wallet already closed.')
                print(
                    'If shutdown error occurred, a wallet delete is recommended.'
                )
                pass

    except (FileNotFoundError) as e:
        print("ID data has been removed. Deleting wallet...")
        await delete_wallet(IDname)


#########################################################################

    pool_name = input('Pool name?:')
    file_name = pool_name + '.pickle'

    try:
        with open(file_name, 'rb') as f:
            pool_ = pickle.load(f)

            try:
                await pool.close_pool_ledger(pool_['handle'])
                print_log('\n Closing ledger...\n')
            except IndyError as ex:
                if ex.error_code == ErrorCode.PoolLedgerInvalidPoolHandle:
                    pass

            try:
                await pool.delete_pool_ledger_config(pool_['name'])
                print_log('\n Deleting pool ledger config\n')

                pickle_path = os.path.dirname(os.path.realpath(__file__))
                path = pickle_path + '/' + file_name
                os.remove(path)

            except IndyError as ex:
                if ex.error_code == ErrorCode.CommonIOError:
                    pass
    except (FileNotFoundError) as e:
        print('Pool ledger never opened.')
async def get_verkey(IP, *args):

    # Load input:
    #########################################################################

    pool_ = await pool_configuration(IP)

    if not args:
        sub = input('Who dis? ').strip().lower()
    else:
        for arg in args:
            sub = arg

    pickle_file = sub + '.pickle'

    await create_wallet(sub)
    with open(pickle_file, 'rb') as f:
        name = pickle.load(f)

    Bname = input("Who's key? ").strip().lower()

    print('Requesting key for?')
    print('1. Connection with ' + Bname +
          ' (Pairwise Pseudonymous Private DID)')
    print('2. ' + Bname + ' Verinym (Public DID)')

    sel = int(input('Please select a number:'))

    BdidA = 'did_from_' + Bname
    Bdid = Bname + '_did'
    BkeyA = 'key_from_' + Bname
    Bkey = Bname + '_key'

    if BdidA not in name:
        print()
        print('Make sure you have the matching DID for the requested verkey.')
        return

    if sel == 1:
        DID = name[BdidA]
        name[BkeyA] = await did.key_for_did(pool_handle=pool_['handle'],
                                            wallet_handle=name['wallet'],
                                            did=DID)
    else:
        DID = name[Bdid]
        name[Bkey] = await did.key_for_did(pool_handle=pool_['handle'],
                                           wallet_handle=name['wallet'],
                                           did=DID)

    if sel == 1:
        print()
        print_log('Successfully stored the matching Verkey from ' + Bname +
                  ' connection request (private pairwise DID).')

    else:
        print()
        print_log('Successfully stored the matching Verkey for ' + Bname +
                  ' Verinym (Public DID).')

    with open(pickle_file, 'wb') as f:
        pickle.dump(name, f)
async def replace_keys(IP, *args):

    # Load input:
    #########################################################################

    pool_ = await pool_configuration(IP)

    if not args:
        sub = input('Submitter? ').strip().lower()
    else:
        for arg in args:
            sub = arg

    pickle_file = sub + '.pickle'

    await ID(sub)
    with open(pickle_file, 'rb') as f:
        name = pickle.load(f)

    submitter = name

    print(' - None')
    print(' - TRUST_ANCHOR')
    print(' - NETWORK_MONITOR')
    print(' - empty string to reset role')
    nymrole = input('Role of new verkey?')

    #########################################################################

    print_log('\n Generating new verkey in wallet\n')

    new_verkey = await did.replace_keys_start(submitter['wallet'],
                                              submitter['did'], "{}")
    print_log('New Verkey: ', new_verkey)

    print_log('\n Building NYM request to update new verkey to ledger\n')

    nym_request = await ledger.build_nym_request(
        submitter_did=submitter['did'],
        target_did=submitter['did'],
        ver_key=new_verkey,
        alias=None,
        role=nymrole)

    print_log('NYM request:')
    pprint.pprint(json.loads(nym_request))

    print_log('\n Sending NYM request to the ledger\n')

    nym_response = await ledger.sign_and_submit_request(
        pool_handle=pool_['handle'],
        wallet_handle=submitter['wallet'],
        submitter_did=submitter['did'],
        request_json=nym_request)

    print_log('NYM response:')
    pprint.pprint(json.loads(nym_response))

    print_log('\n Apply new verkey in wallet\n')

    await did.replace_keys_apply(submitter['wallet'], submitter['did'])
    name['verkey'] = new_verkey

    with open(pickle_file, 'wb') as f:
        pickle.dump(name, f)