├── .github └── workflows │ ├── release.yml │ └── verify.yml ├── .gitignore ├── LICENSE ├── MANIFEST.in ├── README.md ├── demo └── demo.py ├── docs └── testing.md ├── peerdid ├── __init__.py ├── core │ ├── __init__.py │ ├── jwk_okp.py │ ├── multibase.py │ ├── multicodec.py │ ├── peer_did_helper.py │ └── utils.py ├── dids.py ├── errors.py └── keys.py ├── renovate.json ├── setup.cfg ├── setup.py ├── tests ├── __init__.py ├── test_create_peer_did_numalgo_0.py ├── test_create_peer_did_numalgo_2.py ├── test_peer_did_helpers.py ├── test_resolve_peer_did_numalgo_0.py ├── test_resolve_peer_did_numalgo_2.py └── test_vectors.py └── tox.ini /.github/workflows/release.yml: -------------------------------------------------------------------------------- 1 | name: release 2 | 3 | on: 4 | push: 5 | branches: [stable] 6 | workflow_dispatch: 7 | inputs: 8 | devN: 9 | description: 'development release number' 10 | required: false 11 | default: '0' 12 | 13 | env: 14 | PKG_NAME: peerdid 15 | 16 | 17 | jobs: 18 | 19 | checks: 20 | name: check releases 21 | if: github.ref == 'refs/heads/stable' 22 | runs-on: ubuntu-latest 23 | outputs: 24 | current_version: ${{ steps.current_version.outputs.current_version }} 25 | release_info: ${{ steps.release_info.outputs.release_info }} 26 | asset_tgz_url: ${{ steps.release_info.outputs.asset_tgz_url }} 27 | asset_whl_url: ${{ steps.release_info.outputs.asset_whl_url }} 28 | upload_url: ${{ steps.release_info.outputs.upload_url }} 29 | already_in_pypi: ${{ steps.check_in_pypi.outputs.pypi_versions != '' }} 30 | 31 | steps: 32 | - uses: actions/checkout@v3 33 | 34 | - uses: actions/setup-python@v4 35 | with: 36 | python-version: '3.x' 37 | 38 | - name: Get current version 39 | id: current_version 40 | run: | 41 | python -m pip install . --no-deps 42 | out="$(pip show ${{ env.PKG_NAME }} | grep 'Version:' | awk '{print $2}')" 43 | echo "$out" 44 | echo "current_version=$out" >> $GITHUB_OUTPUT 45 | shell: bash 46 | 47 | - name: Get release info 48 | id: release_info 49 | run: | 50 | release_info="$(curl -s https://api.github.com/repos/${{ github.repository }}/releases \ 51 | | jq -c '.[] | select(.name == "v${{ steps.current_version.outputs.current_version }}")')" 52 | echo "release_info=$release_info" >> $GITHUB_OUTPUT 53 | echo "$release_info" 54 | 55 | asset_tgz_url="$(echo "$release_info" \ 56 | | jq -r '.assets[] | select(.name | match("^${{ env.PKG_NAME }}.*\\.tar.gz$")) | .browser_download_url')" 57 | echo "asset_tgz_url=$asset_tgz_url" >> $GITHUB_OUTPUT 58 | echo "$asset_tgz_url" 59 | 60 | asset_whl_url="$(echo "$release_info" \ 61 | | jq -r '.assets[] | select(.name | match("^${{ env.PKG_NAME }}.*\\.whl$")) | .browser_download_url')" 62 | echo "asset_whl_url=$asset_whl_url" >> $GITHUB_OUTPUT 63 | echo "$asset_whl_url" 64 | 65 | upload_url="$(echo "$release_info" | jq -r '.upload_url')" 66 | echo "upload_url=$upload_url" >> $GITHUB_OUTPUT 67 | echo "$upload_url" 68 | shell: bash 69 | 70 | - name: check if already deployed to PyPI 71 | id: check_in_pypi 72 | # Note. other options: 73 | # - use 'pip install --no-deps PKG==VERSION' with current version 74 | # - use 'pip index versions PKG==VERSION' 75 | # (but it's a kind of experimental feature of pip >= 21.2) 76 | run: | 77 | python -m pip install --upgrade pip 78 | out="$(pip install --use-deprecated=legacy-resolver ${{ env.PKG_NAME }}== 2>&1 \ 79 | | grep -E "Could not find .* ${{ steps.current_version.outputs.current_version }}(,|\))")" 80 | echo "pypi_versions=$out" >> $GITHUB_OUTPUT 81 | shell: bash {0} # to opt-out of default fail-fast behavior 82 | 83 | release-github: 84 | name: GitHub Release 85 | if: github.ref == 'refs/heads/stable' 86 | runs-on: ubuntu-latest 87 | needs: checks 88 | steps: 89 | - uses: actions/checkout@v3 90 | 91 | - uses: actions/setup-python@v4 92 | with: 93 | python-version: '3.x' 94 | 95 | - name: build dist 96 | id: build_assets 97 | if: ${{ !(needs.checks.outputs.asset_tgz_url && needs.checks.outputs.asset_whl_url) }} 98 | run: | 99 | python -m pip install --upgrade pip 100 | python -m pip install --upgrade build 101 | python -m build 102 | ls dist 103 | 104 | asset_tgz_name="$(find dist -name '*.tar.gz' -printf '%f')" 105 | echo "asset_tgz_name=$asset_tgz_name" >> $GITHUB_OUTPUT 106 | 107 | asset_whl_name="$(find dist -name '*.whl' -printf '%f')" 108 | echo "asset_whl_name=$asset_whl_name" >> $GITHUB_OUTPUT 109 | shell: bash 110 | 111 | - name: Create Release 112 | id: create_release 113 | if: ${{ ! needs.checks.outputs.release_info }} 114 | uses: actions/create-release@v1 115 | env: 116 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 117 | with: 118 | tag_name: v${{ needs.checks.outputs.current_version }} 119 | release_name: v${{ needs.checks.outputs.current_version }} 120 | 121 | - name: Set upload url 122 | id: upload_url 123 | if: ${{ !(needs.checks.outputs.asset_tgz_url && needs.checks.outputs.asset_whl_url) }} 124 | run: | 125 | if [[ -n "${{ needs.checks.outputs.upload_url }}" ]]; then 126 | echo "value=${{ needs.checks.outputs.upload_url }}" >> $GITHUB_OUTPUT 127 | else 128 | echo "value=${{ steps.create_release.outputs.upload_url }}" >> $GITHUB_OUTPUT 129 | fi 130 | 131 | - name: Upload the source archive 132 | if: ${{ !needs.checks.outputs.asset_tgz_url }} 133 | uses: actions/upload-release-asset@v1 134 | env: 135 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 136 | with: 137 | upload_url: ${{ steps.upload_url.outputs.value }} 138 | asset_path: dist/${{ steps.build_assets.outputs.asset_tgz_name }} 139 | asset_name: ${{ steps.build_assets.outputs.asset_tgz_name }} 140 | asset_content_type: application/x-gtar 141 | 142 | - name: Upload the wheel 143 | if: ${{ !needs.checks.outputs.asset_whl_url }} 144 | uses: actions/upload-release-asset@v1 145 | env: 146 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 147 | with: 148 | upload_url: ${{ steps.upload_url.outputs.value }} 149 | asset_path: dist/${{ steps.build_assets.outputs.asset_whl_name }} 150 | asset_name: ${{ steps.build_assets.outputs.asset_whl_name }} 151 | asset_content_type: application/zip 152 | 153 | deploy-pypi: 154 | name: Deploy to PyPI 155 | if: github.ref == 'refs/heads/stable' && needs.checks.outputs.already_in_pypi == 'false' 156 | runs-on: ubuntu-latest 157 | needs: [checks, release-github] 158 | steps: 159 | - uses: actions/checkout@v3 160 | 161 | - name: download GitHub artifacts 162 | run: | 163 | mkdir -p dist 164 | cd dist 165 | curl -s https://api.github.com/repos/${{ github.repository }}/releases/tags/v${{ needs.checks.outputs.current_version }} \ 166 | | jq -r ".assets[] | select(.name | contains(\"${{ env.PKG_NAME }}\")) | .browser_download_url" \ 167 | | wget -i - 168 | ls 169 | shell: bash 170 | 171 | # NOTE uncomment once community actions are allowed for the github org 172 | # - name: Publish to PyPI 173 | # uses: pypa/gh-action-pypi-publish@v1.4.2 174 | # with: 175 | # user: __token__ 176 | # password: ${{ secrets.PYPI_API_TOKEN }} 177 | 178 | - uses: actions/setup-python@v4 179 | with: 180 | python-version: '3.x' 181 | 182 | - name: Prepare python env 183 | run: | 184 | python -m pip install --upgrade pip 185 | python -m pip install --upgrade twine 186 | shell: bash 187 | 188 | - name: Publish to PyPI 189 | env: 190 | TWINE_PASSWORD: ${{ secrets.PYPI_API_TOKEN }} 191 | run: python -m twine upload --non-interactive --username __token__ dist/* 192 | shell: bash 193 | 194 | deploy-test-pypi: 195 | name: Deploy to TestPyPI 196 | if: github.ref != 'refs/heads/stable' && github.event_name == 'workflow_dispatch' 197 | runs-on: ubuntu-latest 198 | steps: 199 | - uses: actions/checkout@v3 200 | 201 | - uses: actions/setup-python@v4 202 | with: 203 | python-version: '3.x' 204 | 205 | - name: set dev version 206 | run: | 207 | sed -i -r "s~__version__ = \"(.+)\"~__version__ = \"\1.dev${{ github.event.inputs.devN }}\"~" ./peerdid/__init__.py 208 | grep version ./setup.py 209 | shell: bash 210 | 211 | - name: build dist 212 | run: | 213 | python -m pip install --upgrade pip 214 | python -m pip install --upgrade build 215 | python -m build 216 | ls dist 217 | shell: bash 218 | 219 | - name: install twine 220 | run: python -m pip install --upgrade twine 221 | shell: bash 222 | 223 | - name: Publish to TestPyPI 224 | env: 225 | TWINE_PASSWORD: ${{ secrets.TEST_PYPI_API_TOKEN }} 226 | run: python -m twine upload --non-interactive --username __token__ --repository testpypi dist/* 227 | shell: bash 228 | -------------------------------------------------------------------------------- /.github/workflows/verify.yml: -------------------------------------------------------------------------------- 1 | name: verify 2 | 3 | on: 4 | pull_request: 5 | # paths: 6 | # - '**.py' 7 | 8 | 9 | env: 10 | PKG_NAME: peerdid 11 | 12 | 13 | jobs: 14 | 15 | release-ready: 16 | runs-on: ubuntu-latest 17 | if: github.event_name == 'pull_request' && github.event.pull_request.base.ref == 'stable' 18 | steps: 19 | - uses: actions/checkout@v3 20 | 21 | - uses: actions/setup-python@v4 22 | with: 23 | python-version: '3.x' 24 | 25 | - name: Get current version 26 | id: current_version 27 | run: | 28 | python -m pip install --upgrade pip 29 | pip install . --no-deps 30 | out="$(pip show ${{ env.PKG_NAME }} | grep 'Version:' | awk '{print $2}')" 31 | echo "$out" 32 | echo "current_version=$out" >> $GITHUB_OUTPUT 33 | shell: bash 34 | 35 | - name: Check version format 36 | run: | 37 | # verify the version has "MAJOR.MINOR.PATCH" parts only 38 | echo "${{ steps.current_version.outputs.current_version }}" | grep -e '^[0-9]\+\.[0-9]\+\.[0-9]\+$' 39 | shell: bash 40 | 41 | # TODO improve (DRY): copy-paste from release.yml 42 | - name: Get release info 43 | id: release_info 44 | run: | 45 | release_info="$(curl -s https://api.github.com/repos/${{ github.repository }}/releases \ 46 | | jq -c '.[] | select(.name == "v${{ steps.current_version.outputs.current_version }}")')" 47 | echo "release_info=$release_info" >> $GITHUB_OUTPUT 48 | echo "$release_info" 49 | shell: bash 50 | 51 | - name: fail unless release not found 52 | # TODO check if greater than latest tag / release (?) 53 | if: steps.release_info.outputs.release_info 54 | run: exit 1 55 | 56 | static-black: 57 | runs-on: ubuntu-latest 58 | steps: 59 | - uses: actions/checkout@v3 60 | 61 | - name: Set up Python 62 | uses: actions/setup-python@v4 63 | with: 64 | python-version: 3.9 65 | 66 | - name: Install black 67 | run: pip install black 68 | 69 | - name: Black Format Check 70 | run: black --check . 71 | 72 | static-flake8: 73 | runs-on: ubuntu-latest 74 | steps: 75 | - uses: actions/checkout@v3 76 | 77 | - name: Set up Python 78 | uses: actions/setup-python@v4 79 | with: 80 | python-version: 3.9 81 | 82 | - name: Install flake8 83 | run: pip install flake8 84 | 85 | - name: Lint with flake8 86 | run: flake8 . 87 | 88 | unit: 89 | strategy: 90 | matrix: 91 | python-version: [ '3.8', '3.9', '3.10', '3.11' ] 92 | os: [ ubuntu-latest, windows-latest, macos-latest ] 93 | include: 94 | - {python-version: '3.11', toxenv: py311} 95 | - {python-version: '3.10', toxenv: py310} 96 | - {python-version: '3.9', toxenv: py39} 97 | - {python-version: '3.8', toxenv: py38} 98 | runs-on: ${{ matrix.os }} 99 | steps: 100 | - uses: actions/checkout@v3 101 | 102 | - name: Set up Python ${{ matrix.python-version }} 103 | uses: actions/setup-python@v4 104 | with: 105 | python-version: ${{ matrix.python-version }} 106 | 107 | - name: pip update 108 | run: python -m pip install -U pip 109 | 110 | - name: resolve pip cache dir 111 | id: pip_cache_dir 112 | shell: bash 113 | run: echo "value=$(pip cache dir)" >> $GITHUB_OUTPUT 114 | 115 | - name: cache pip 116 | uses: actions/cache@v3 117 | with: 118 | path: ${{ steps.pip_cache_dir.outputs.value }} 119 | key: ${{ runner.os }}-${{ matrix.python-version }}-${{ hashFiles('setup.py') }} 120 | 121 | - name: Install tox 122 | run: pip install tox 123 | 124 | - name: Test with pytest 125 | run: tox -e ${{ matrix.toxenv }} 126 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | pip-wheel-metadata/ 24 | share/python-wheels/ 25 | *.egg-info/ 26 | .installed.cfg 27 | *.egg 28 | MANIFEST 29 | 30 | # PyInstaller 31 | # Usually these files are written by a python script from a template 32 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 33 | *.manifest 34 | *.spec 35 | 36 | # Installer logs 37 | pip-log.txt 38 | pip-delete-this-directory.txt 39 | 40 | # Unit test / coverage reports 41 | htmlcov/ 42 | .tox/ 43 | .nox/ 44 | .coverage 45 | .coverage.* 46 | .cache 47 | nosetests.xml 48 | coverage.xml 49 | *.cover 50 | *.py,cover 51 | .hypothesis/ 52 | .pytest_cache/ 53 | 54 | # Translations 55 | *.mo 56 | *.pot 57 | 58 | # Django stuff: 59 | *.log 60 | local_settings.py 61 | db.sqlite3 62 | db.sqlite3-journal 63 | 64 | # Flask stuff: 65 | instance/ 66 | .webassets-cache 67 | 68 | # Scrapy stuff: 69 | .scrapy 70 | 71 | # Sphinx documentation 72 | docs/_build/ 73 | 74 | # PyBuilder 75 | target/ 76 | 77 | # Jupyter Notebook 78 | .ipynb_checkpoints 79 | 80 | # IPython 81 | profile_default/ 82 | ipython_config.py 83 | 84 | # pyenv 85 | .python-version 86 | 87 | # pipenv 88 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 89 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 90 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 91 | # install all needed dependencies. 92 | #Pipfile.lock 93 | 94 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow 95 | __pypackages__/ 96 | 97 | # Celery stuff 98 | celerybeat-schedule 99 | celerybeat.pid 100 | 101 | # SageMath parsed files 102 | *.sage.py 103 | 104 | # Environments 105 | .env 106 | .venv 107 | env/ 108 | venv/ 109 | ENV/ 110 | env.bak/ 111 | venv.bak/ 112 | 113 | # Spyder project settings 114 | .spyderproject 115 | .spyproject 116 | 117 | # Rope project settings 118 | .ropeproject 119 | 120 | # mkdocs documentation 121 | /site 122 | 123 | # mypy 124 | .mypy_cache/ 125 | .dmypy.json 126 | dmypy.json 127 | 128 | # Pyre type checker 129 | .pyre/ 130 | 131 | # Pycharm files 132 | .idea 133 | 134 | # vim files 135 | *.swp 136 | *.swo 137 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /MANIFEST.in: -------------------------------------------------------------------------------- 1 | include LICENSE README.md 2 | 3 | recursive-include provisioner LICENSE LICENSE* *LICENSE* 4 | 5 | global-exclude *.py[cod] __pycache__ *.so 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Peerdid Python 2 | 3 | [![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0) 4 | [![Unit Tests](https://github.com/sicpa-dlab/peer-did-python/workflows/verify/badge.svg)](https://github.com/sicpa-dlab/peer-did-python/actions/workflows/verify.yml) 5 | [![Python Package](https://img.shields.io/pypi/v/peerdid)](https://pypi.org/project/peerdid/) 6 | 7 | Implementation of the [Peer DID method specification](https://identity.foundation/peer-did-method-spec/) in Python. 8 | 9 | Implements [static layers of support (1, 2a, 2b)](https://identity.foundation/peer-did-method-spec/#layers-of-support) only. 10 | 11 | ## Installation 12 | ``` 13 | pip install peerdid 14 | ``` 15 | 16 | ## DIDComm + peerdid Demo 17 | See https://github.com/sicpa-dlab/didcomm-demo. 18 | 19 | ## Example 20 | 21 | Example code: 22 | 23 | ```python 24 | from peerdid.dids import ( 25 | create_peer_did_numalgo_0, 26 | create_peer_did_numalgo_2, 27 | resolve_peer_did, 28 | ) 29 | from peerdid.keys import Ed25519VerificationKey, X25519KeyAgreementKey 30 | 31 | encryption_keys = [ 32 | X25519KeyAgreementKey.from_base58( 33 | "DmgBSHMqaZiYqwNMEJJuxWzsGGC8jUYADrfSdBrC6L8s" 34 | ) 35 | ] 36 | signing_keys = [ 37 | Ed25519VerificationKey.from_base58( 38 | "ByHnpUCFb1vAfh9CFZ8ZkmUZguURW8nSw889hy6rD8L7" 39 | ) 40 | ] 41 | service = { 42 | "type": "DIDCommMessaging", 43 | "serviceEndpoint": "https://example.com/endpoint1", 44 | "routingKeys": ["did:example:somemediator#somekey1"], 45 | "accept": ["didcomm/v2", "didcomm/aip2;env=rfc587"], 46 | } 47 | 48 | peer_did_algo_0 = create_peer_did_numalgo_0(inception_key=signing_keys[0]) 49 | peer_did_algo_2 = create_peer_did_numalgo_2( 50 | encryption_keys=encryption_keys, signing_keys=signing_keys, service=service 51 | ) 52 | 53 | did_doc_algo_0 = resolve_peer_did(peer_did_algo_0) 54 | did_doc_algo_2 = resolve_peer_did(peer_did_algo_2) 55 | 56 | did_doc_algo_0_json = did_doc_algo_0.to_json() 57 | did_doc_algo_2_json = did_doc_algo_2.to_json() 58 | ``` 59 | 60 | Example of DID documents: 61 | 62 | ```jsonc 63 | // did_doc_algo_0_json 64 | { 65 | "@context": [ 66 | "https://www.w3.org/ns/did/v1", 67 | "https://w3id.org/security/suites/ed25519-2020/v1" 68 | ], 69 | "id": "did:peer:0z6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V", 70 | "verificationMethod": [ 71 | { 72 | "id": "#6MkqRYqQ", 73 | "type": "Ed25519VerificationKey2020", 74 | "controller": "did:peer:0z6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V", 75 | "publicKeyMultibase": "z6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V" 76 | } 77 | ], 78 | "authentication": ["#6MkqRYqQ"], 79 | "assertionMethod": ["#6MkqRYqQ"], 80 | "capabilityInvocation": ["#6MkqRYqQ"], 81 | "capabilityDelegation": ["#6MkqRYqQ"] 82 | } 83 | 84 | // did_doc_algo_2_json 85 | { 86 | "@context": [ 87 | "https://www.w3.org/ns/did/v1", 88 | "https://w3id.org/security/suites/x25519-2020/v1", 89 | "https://w3id.org/security/suites/ed25519-2020/v1" 90 | ], 91 | "id": "did:peer:2.Ez6LSpSrLxbAhg2SHwKk7kwpsH7DM7QjFS5iK6qP87eViohud.Vz6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V.SeyJ0IjoiZG0iLCJzIjoiaHR0cHM6Ly9leGFtcGxlLmNvbS9lbmRwb2ludDEiLCJyIjpbImRpZDpleGFtcGxlOnNvbWVtZWRpYXRvciNzb21la2V5MSJdLCJhIjpbImRpZGNvbW0vdjIiLCJkaWRjb21tL2FpcDI7ZW52PXJmYzU4NyJdfQ", 92 | "verificationMethod": [ 93 | { 94 | "id": "#6LSpSrLx", 95 | "type": "X25519KeyAgreementKey2020", 96 | "controller": "did:peer:2.Ez6LSpSrLxbAhg2SHwKk7kwpsH7DM7QjFS5iK6qP87eViohud.Vz6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V.SeyJ0IjoiZG0iLCJzIjoiaHR0cHM6Ly9leGFtcGxlLmNvbS9lbmRwb2ludDEiLCJyIjpbImRpZDpleGFtcGxlOnNvbWVtZWRpYXRvciNzb21la2V5MSJdLCJhIjpbImRpZGNvbW0vdjIiLCJkaWRjb21tL2FpcDI7ZW52PXJmYzU4NyJdfQ", 97 | "publicKeyMultibase": "z6LSpSrLxbAhg2SHwKk7kwpsH7DM7QjFS5iK6qP87eViohud" 98 | }, 99 | { 100 | "id": "#6MkqRYqQ", 101 | "type": "Ed25519VerificationKey2020", 102 | "controller": "did:peer:2.Ez6LSpSrLxbAhg2SHwKk7kwpsH7DM7QjFS5iK6qP87eViohud.Vz6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V.SeyJ0IjoiZG0iLCJzIjoiaHR0cHM6Ly9leGFtcGxlLmNvbS9lbmRwb2ludDEiLCJyIjpbImRpZDpleGFtcGxlOnNvbWVtZWRpYXRvciNzb21la2V5MSJdLCJhIjpbImRpZGNvbW0vdjIiLCJkaWRjb21tL2FpcDI7ZW52PXJmYzU4NyJdfQ", 103 | "publicKeyMultibase": "z6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V" 104 | } 105 | ], 106 | "authentication": ["#6MkqRYqQ"], 107 | "assertionMethod": ["#6MkqRYqQ"], 108 | "keyAgreement": ["#6LSpSrLx"], 109 | "capabilityInvocation": ["#6MkqRYqQ"], 110 | "capabilityDelegation": ["#6MkqRYqQ"], 111 | "service": [ 112 | { 113 | "id": "#didcommmessaging-0", 114 | "type": "DIDCommMessaging", 115 | "serviceEndpoint": "https://example.com/endpoint1", 116 | "accept": ["didcomm/v2", "didcomm/aip2;env=rfc587"], 117 | "routingKeys": ["did:example:somemediator#somekey1"] 118 | } 119 | ] 120 | } 121 | ``` 122 | 123 | ## Assumptions and limitations 124 | - Only static layers [1, 2a, 2b](https://identity.foundation/peer-did-method-spec/#layers-of-support) are supported 125 | - Only `X25519` keys are supported for key agreement 126 | - Only `Ed25519` keys are supported for authentication 127 | - Supported verification materials (input and in the resolved DID Document): 128 | - [Default] 2020 verification materials (`Ed25519VerificationKey2020` and `X25519KeyAgreementKey2020`) with multibase base58 (`publicKeyMultibase`) public key encoding. 129 | - JWK (`JsonWebKey2020`) using JWK (`publicKeyJwk`) public key encoding 130 | - 2018/2019 verification materials (`Ed25519VerificationKey2018` and `X25519KeyAgreementKey2019`) using base58 (`publicKeyBase58`) public key encoding. 131 | 132 | 133 | 134 | ## How to contribute 135 | 136 | Pull requests are welcome! 137 | 138 | Pull requests should have a descriptive name, include the summary of all changes made in the pull 139 | request description, and include unit tests that provide good coverage of the feature or fix. A Continuous Integration ( 140 | CI) 141 | pipeline is executed on all PRs before review and contributors are expected to address all CI issues identified. 142 | 143 | ### A Continuous Integration (CI) pipeline does the following jobs: 144 | 145 | - Executes all unit tests from the pull request. 146 | - Analyzes code style using Flake8. 147 | 148 | -------------------------------------------------------------------------------- /demo/demo.py: -------------------------------------------------------------------------------- 1 | """Demo application.""" 2 | 3 | from peerdid.dids import ( 4 | create_peer_did_numalgo_0, 5 | create_peer_did_numalgo_2, 6 | resolve_peer_did, 7 | ) 8 | from peerdid.keys import Ed25519VerificationKey, X25519KeyAgreementKey 9 | 10 | 11 | def demo(): 12 | """Generate peer DIDs using both numalgo methods.""" 13 | encryption_keys = [ 14 | X25519KeyAgreementKey.from_base58( 15 | "DmgBSHMqaZiYqwNMEJJuxWzsGGC8jUYADrfSdBrC6L8s" 16 | ) 17 | ] 18 | signing_keys = [ 19 | Ed25519VerificationKey.from_base58( 20 | "ByHnpUCFb1vAfh9CFZ8ZkmUZguURW8nSw889hy6rD8L7" 21 | ) 22 | ] 23 | service = { 24 | "type": "DIDCommMessaging", 25 | "serviceEndpoint": "https://example.com/endpoint1", 26 | "routingKeys": ["did:example:somemediator#somekey1"], 27 | "accept": ["didcomm/v2", "didcomm/aip2;env=rfc587"], 28 | } 29 | 30 | peer_did_algo_0 = create_peer_did_numalgo_0(inception_key=signing_keys[0]) 31 | peer_did_algo_2 = create_peer_did_numalgo_2( 32 | encryption_keys=encryption_keys, signing_keys=signing_keys, service=service 33 | ) 34 | 35 | print("peer_did_algo_0:", peer_did_algo_0) 36 | print("==================================") 37 | print("peer_did_algo_2:", peer_did_algo_2) 38 | print("==================================") 39 | 40 | did_doc_algo_0 = resolve_peer_did(peer_did_algo_0) 41 | did_doc_algo_2 = resolve_peer_did(peer_did_algo_2) 42 | print("did_doc_algo_0 as JSON:", did_doc_algo_0.to_json()) 43 | print("==================================") 44 | print("did_doc_algo_2 as JSON:", did_doc_algo_2.to_json()) 45 | 46 | 47 | if __name__ == "__main__": 48 | demo() 49 | -------------------------------------------------------------------------------- /docs/testing.md: -------------------------------------------------------------------------------- 1 | # peer-did-python Testing 2 | 3 | ## Table of Contents 4 | 5 | * [Development Environment Setup](#development-environment-setup) 6 | 7 | * [Static Testing](#static-testing) 8 | 9 | * [flake8](#flake8) 10 | * [black](#black) 11 | 12 | * [Unit Testing](#unit-testing) 13 | 14 | ## Development Environment Setup 15 | 16 | ```bash 17 | $ pip install -e .[tests] 18 | $ pip install black flake8 19 | ``` 20 | 21 | ## Static Testing 22 | 23 | ### flake8 24 | 25 | Run [flake8](https://flake8.pycqa.org/en/latest/) as follows: 26 | 27 | ```bash 28 | $ flake8 . 29 | ``` 30 | 31 | ### black 32 | 33 | Run [black](https://black.readthedocs.io/en/stable/usage_and_configuration/index.html) for a dry check as follows: 34 | 35 | ```bash 36 | $ black --check . 37 | ``` 38 | 39 | or to auto-format: 40 | 41 | ```bash 42 | $ black . 43 | ``` 44 | 45 | ## Unit Testing 46 | 47 | To run [pytest](https://docs.pytest.org/en/stable/) in your environment: 48 | 49 | ```bash 50 | pytest 51 | ``` 52 | 53 | To run tests in all supported environments (like CI does) using [tox](https://tox.wiki/en/latest/index.html): 54 | 55 | ```bash 56 | tox 57 | ``` 58 | -------------------------------------------------------------------------------- /peerdid/__init__.py: -------------------------------------------------------------------------------- 1 | """Peer DID document generation and resolution.""" 2 | 3 | from . import core, dids, errors, keys 4 | 5 | from pydid import DID, DIDDocument 6 | 7 | __version__ = "0.5.2" 8 | 9 | __all__ = ["__version__", "core", "errors", "dids", "keys", "DID", "DIDDocument"] 10 | -------------------------------------------------------------------------------- /peerdid/core/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sicpa-dlab/peer-did-python/fa9315981dfcdb35d22b6137052061fa512027da/peerdid/core/__init__.py -------------------------------------------------------------------------------- /peerdid/core/jwk_okp.py: -------------------------------------------------------------------------------- 1 | """JWK utility methods.""" 2 | 3 | import json 4 | 5 | from typing import Tuple, Union 6 | 7 | from .multicodec import Codec 8 | from .utils import urlsafe_b64encode, urlsafe_b64decode 9 | 10 | 11 | def public_key_to_jwk(public_key: bytes, codec: Codec) -> dict: 12 | x = urlsafe_b64encode(public_key).decode("utf-8") 13 | 14 | if codec == Codec.ED25519: 15 | crv = "Ed25519" 16 | elif codec == Codec.X25519: 17 | crv = "X25519" 18 | else: 19 | raise ValueError("Unsupported JWK codec: {}".format(codec)) 20 | 21 | return { 22 | "kty": "OKP", 23 | "crv": crv, 24 | "x": x, 25 | } 26 | 27 | 28 | def jwk_to_public_key(jwk: Union[str, dict]) -> Tuple[bytes, Codec]: 29 | parts = {} 30 | try: 31 | if isinstance(jwk, str): 32 | parts = json.loads(jwk) 33 | elif isinstance(jwk, dict): 34 | parts = jwk 35 | except json.JSONDecodeError: 36 | pass 37 | 38 | kty = parts.pop("kty", None) 39 | crv = parts.pop("crv", None) 40 | x = parts.pop("x", None) 41 | if not ( 42 | kty 43 | and isinstance(kty, str) 44 | and crv 45 | and isinstance(crv, str) 46 | and x 47 | and isinstance(x, str) 48 | ): 49 | raise ValueError("Invalid JWK: {}".format(jwk)) 50 | 51 | try: 52 | public_key = urlsafe_b64decode(x) 53 | except ValueError as e: 54 | raise ValueError("Invalid JWK: {}".format(jwk)) from e 55 | 56 | if crv == "Ed25519": 57 | codec = Codec.ED25519 58 | elif crv == "X25519": 59 | codec = Codec.X25519 60 | else: 61 | raise ValueError("Unsupported JWK codec: {}".format(crv)) 62 | 63 | return public_key, codec 64 | -------------------------------------------------------------------------------- /peerdid/core/multibase.py: -------------------------------------------------------------------------------- 1 | """Multibase utility methods.""" 2 | 3 | from enum import Enum 4 | from typing import Tuple 5 | 6 | import base58 7 | 8 | 9 | class MultibaseFormat(Enum): 10 | """Supported multibase formats.""" 11 | 12 | BASE58 = "z" 13 | 14 | 15 | def from_base58(base58encoded: str) -> bytes: 16 | """Convert from base58 to bytes.""" 17 | try: 18 | return base58.b58decode(base58encoded) 19 | except ValueError: 20 | raise ValueError( 21 | "Invalid key: Invalid base58 encoding: " + base58encoded 22 | ) from None 23 | 24 | 25 | def to_base58(value: bytes) -> str: 26 | """Convert a bytes value to base58 encoding.""" 27 | return base58.b58encode(value).decode("utf-8") 28 | 29 | 30 | def from_multibase(multibase: str) -> Tuple[str, bytes]: 31 | """Convert from multibase to bytes.""" 32 | if not multibase: 33 | raise ValueError("Invalid key: No transform part in multibase encoding") 34 | transform = multibase[0] 35 | if not transform == MultibaseFormat.BASE58.value: 36 | raise ValueError( 37 | "Invalid key: Unsupported transform part of peer_did: " + transform 38 | ) 39 | encnumbasis = multibase[1:] 40 | decoded = from_base58(encnumbasis) 41 | return encnumbasis, decoded 42 | 43 | 44 | def to_multibase(value: bytes, format: MultibaseFormat = None) -> str: 45 | """Convert to base58-encoded multibase.""" 46 | if not format or format == MultibaseFormat.BASE58: 47 | return MultibaseFormat.BASE58.value + to_base58(value) 48 | raise ValueError("Unsupported multibase format") 49 | -------------------------------------------------------------------------------- /peerdid/core/multicodec.py: -------------------------------------------------------------------------------- 1 | """Multicodec utility methods.""" 2 | 3 | from enum import Enum 4 | from typing import Tuple, Union 5 | 6 | import varint 7 | 8 | 9 | class Codec(Enum): 10 | """Multicodec supported codecs.""" 11 | 12 | X25519 = 0xEC 13 | ED25519 = 0xED 14 | 15 | def encode_multicodec(self, value: bytes) -> bytes: 16 | """Encode a value with this codec.""" 17 | prefix = varint.encode(self.value) 18 | return prefix + value 19 | 20 | 21 | def from_multicodec(value: Union[str, bytes]) -> Tuple[bytes, Codec]: 22 | """Decode a multicodec value.""" 23 | if isinstance(value, str): 24 | value = value.encode("utf-8") 25 | try: 26 | prefix_int = varint.decode_bytes(value) 27 | except Exception: 28 | raise ValueError( 29 | "Invalid key: Invalid multicodec prefix in {}".format(str(value)) 30 | ) 31 | 32 | try: 33 | codec = Codec(prefix_int) 34 | except ValueError: 35 | raise ValueError( 36 | "Invalid key: Unknown multicodec prefix {} in {}".format( 37 | str(prefix_int), str(value) 38 | ) 39 | ) 40 | 41 | prefix = varint.encode(prefix_int) 42 | return value[len(prefix) :], codec 43 | -------------------------------------------------------------------------------- /peerdid/core/peer_did_helper.py: -------------------------------------------------------------------------------- 1 | """Peer DID helper methods.""" 2 | 3 | import json 4 | 5 | from enum import Enum 6 | from typing import List, Optional, Union 7 | 8 | from pydid import Service 9 | 10 | from ..core.utils import urlsafe_b64encode, urlsafe_b64decode 11 | from ..errors import MalformedPeerDIDError 12 | from ..keys import KeyFormat, BaseKey 13 | 14 | SERVICE_ID = "id" 15 | SERVICE_TYPE = "type" 16 | SERVICE_ENDPOINT = "serviceEndpoint" 17 | SERVICE_DIDCOMM_MESSAGING = "DIDCommMessaging" 18 | SERVICE_ROUTING_KEYS = "routingKeys" 19 | SERVICE_ACCEPT = "accept" 20 | 21 | ServiceJson = Union[str, dict, list] 22 | 23 | 24 | class Numalgo2Prefix(Enum): 25 | """Numalgo prefix values.""" 26 | 27 | AUTHENTICATION = "V" 28 | KEY_AGREEMENT = "E" 29 | SERVICE = "S" 30 | 31 | 32 | class ServicePrefix(Enum): 33 | """Service short forms.""" 34 | 35 | SERVICE_TYPE = "t" 36 | SERVICE_ENDPOINT = "s" 37 | SERVICE_DIDCOMM_MESSAGING = "dm" 38 | SERVICE_ROUTING_KEYS = "r" 39 | SERVICE_ACCEPT = "a" 40 | 41 | 42 | def encode_service(service: ServiceJson) -> str: 43 | """ 44 | Generate encoded service according to the second algorithm. 45 | 46 | Reference: 47 | For this type of algorithm the DID Document can be obtained from the Peer DID. 48 | 49 | :param service: JSON conforming to the DID specification (https://www.w3.org/TR/did-core/#services) 50 | :return: encoded service 51 | """ 52 | if service is None or service == "" or service == []: 53 | return "" 54 | 55 | if isinstance(service, str): 56 | try: 57 | service = json.loads(service) 58 | except json.JSONDecodeError: 59 | pass 60 | 61 | if isinstance(service, list): 62 | service = list(map(_encode_service_entry, service)) 63 | elif isinstance(service, dict): 64 | service = _encode_service_entry(service) 65 | else: 66 | raise ValueError("Service is not valid JSON") 67 | 68 | return ( 69 | "." 70 | + Numalgo2Prefix.SERVICE.value 71 | + urlsafe_b64encode(json.dumps(service, separators=(",", ":"))).decode("utf-8") 72 | ) 73 | 74 | 75 | def _encode_service_entry(service: dict) -> dict: 76 | result = {} 77 | for k, v in service.items(): 78 | if k == SERVICE_TYPE: 79 | k = ServicePrefix.SERVICE_TYPE.value 80 | elif k == SERVICE_ENDPOINT: 81 | k = ServicePrefix.SERVICE_ENDPOINT.value 82 | elif k == SERVICE_ROUTING_KEYS: 83 | k = ServicePrefix.SERVICE_ROUTING_KEYS.value 84 | elif k == SERVICE_ACCEPT: 85 | k = ServicePrefix.SERVICE_ACCEPT.value 86 | 87 | if v == SERVICE_DIDCOMM_MESSAGING: 88 | v = ServicePrefix.SERVICE_DIDCOMM_MESSAGING.value 89 | 90 | result[k] = v 91 | return result 92 | 93 | 94 | def decode_service(service: str) -> Optional[List[Service]]: 95 | """ 96 | Decode service according to Peer DID spec. 97 | 98 | Reference: https://identity.foundation/peer-did-method-spec/index.html#example-2-abnf-for-peer-dids 99 | 100 | :param service: service to decode 101 | :param peer_did: peer_did which will be used as an ID 102 | :raises ValueError: if peer_did parameter is not valid 103 | :return: decoded service (list of dict) 104 | """ 105 | if not service: 106 | return None 107 | try: 108 | decoded_service = urlsafe_b64decode(service.encode()) 109 | list_of_service_dict = json.loads(decoded_service.decode("utf-8")) 110 | except (ValueError, json.JSONDecodeError) as e: 111 | raise MalformedPeerDIDError("Invalid service") from e 112 | 113 | if not isinstance(list_of_service_dict, list): 114 | list_of_service_dict = [list_of_service_dict] 115 | result = [] 116 | 117 | for i, svc_def in enumerate(list_of_service_dict): 118 | if not isinstance(svc_def, dict): 119 | raise MalformedPeerDIDError("Service entry is not an object") 120 | service_type = svc_def.pop(ServicePrefix.SERVICE_TYPE.value, "").replace( 121 | ServicePrefix.SERVICE_DIDCOMM_MESSAGING.value, SERVICE_DIDCOMM_MESSAGING 122 | ) 123 | if not service_type: 124 | raise MalformedPeerDIDError("Service doesn't contain a type") 125 | ident = "#" + service_type.lower() + "-" + str(i) 126 | endpoint = svc_def.pop(ServicePrefix.SERVICE_ENDPOINT.value, None) 127 | extra = {} 128 | for k, v in svc_def.items(): 129 | if k == ServicePrefix.SERVICE_ACCEPT.value: 130 | k = SERVICE_ACCEPT 131 | elif k == ServicePrefix.SERVICE_ROUTING_KEYS.value: 132 | k = SERVICE_ROUTING_KEYS 133 | extra[k] = v 134 | service = Service.make( 135 | id=ident, type=service_type, service_endpoint=endpoint, **extra 136 | ) 137 | result.append(service) 138 | 139 | return result 140 | 141 | 142 | def decode_multibase_numbasis( 143 | multibase: str, 144 | key_format: KeyFormat, 145 | ) -> BaseKey: 146 | """ 147 | Decode multibase-encoded numeric basis to a verification method for DID Document. 148 | 149 | :param multibase: multibase-encoded numeric basis to decode 150 | :param key_format: the format of public keys in the DID Document 151 | :return: decoded numeric basis as verification method for DID Document 152 | """ 153 | try: 154 | return BaseKey.from_multibase(multibase, format=key_format) 155 | except (ValueError, TypeError) as e: 156 | raise MalformedPeerDIDError("Invalid key: {}".format(multibase)) from e 157 | -------------------------------------------------------------------------------- /peerdid/core/utils.py: -------------------------------------------------------------------------------- 1 | """Utility methods.""" 2 | 3 | import base64 4 | 5 | from typing import Union 6 | 7 | 8 | def urlsafe_b64encode(s: Union[str, bytes]) -> bytes: 9 | """ 10 | Base64 URL-safe encoding with no padding. 11 | 12 | :param s: input str to be encoded 13 | :return: encoded bytes 14 | """ 15 | if isinstance(s, str): 16 | s = s.encode("utf-8") 17 | try: 18 | return base64.urlsafe_b64encode(s).rstrip(b"=") 19 | except Exception as e: 20 | raise ValueError("Can not encode from base64 URL safe: " + str(s)) from e 21 | 22 | 23 | def urlsafe_b64decode(s: Union[str, bytes]) -> bytes: 24 | """ 25 | Base64 URL-safe decoding with no padding. 26 | 27 | :param s: input bytes to be decoded 28 | :return: decoded bytes 29 | """ 30 | if isinstance(s, str): 31 | s = s.encode("utf-8") 32 | try: 33 | s += b"=" * (-len(s) % 4) 34 | return base64.urlsafe_b64decode(s) 35 | except Exception as e: 36 | raise ValueError("Can not decode base64 URL safe: " + str(s)) from e 37 | -------------------------------------------------------------------------------- /peerdid/dids.py: -------------------------------------------------------------------------------- 1 | """Peer DID document generation and resolution.""" 2 | 3 | import re 4 | 5 | from typing import Optional, Sequence, Union 6 | 7 | from pydid import DID, DIDDocument, DIDDocumentBuilder, DIDUrl, InvalidDIDError 8 | 9 | from .core.peer_did_helper import ( 10 | Numalgo2Prefix, 11 | ServiceJson, 12 | encode_service, 13 | decode_multibase_numbasis, 14 | decode_service, 15 | ) 16 | from .errors import MalformedPeerDIDError 17 | from .keys import KeyFormat, KeyRelationshipType, BaseKey 18 | 19 | PEER_DID_PATTERN = re.compile( 20 | r"^did:peer:(([0](z)([1-9a-km-zA-HJ-NP-Z]+))|(2((\.[AEVID](z)([1-9a-km-zA-HJ-NP-Z]+))+" 21 | r"(\.(S)[0-9a-zA-Z]*)?)))$" 22 | ) 23 | 24 | 25 | def is_peer_did(peer_did: Union[str, DID]) -> bool: 26 | """ 27 | Check if peer_did parameter matches the Peer DID spec. 28 | 29 | Reference: 30 | 31 | :param peer_did: peer_did to check 32 | :return: True if peer_did matches spec, otherwise False 33 | """ 34 | if peer_did is None: 35 | return False 36 | return bool(PEER_DID_PATTERN.match(peer_did)) 37 | 38 | 39 | def create_peer_did_numalgo_0( 40 | inception_key: BaseKey, 41 | ) -> str: 42 | """ 43 | Generate a Peer DID according to the zeroth algorithm. 44 | 45 | Reference: 46 | For this type of algorithm the DID Document is synthesized from the key material. 47 | 48 | :param inception_key: the key that creates the DID and authenticates when exchanging it with the first peer 49 | :raises ValueError: the inception key is not a BaseKey supporting authentication 50 | :return: generated Peer DID 51 | """ 52 | if KeyRelationshipType.AUTHENTICATION not in inception_key.relationships: 53 | raise ValueError( 54 | "Authentication not supported for key: {}.".format(inception_key) 55 | ) 56 | return "did:peer:0" + inception_key.to_multibase() 57 | 58 | 59 | def create_peer_did_numalgo_2( 60 | encryption_keys: Sequence[BaseKey], 61 | signing_keys: Sequence[BaseKey], 62 | service: Optional[ServiceJson], 63 | ) -> DID: 64 | """ 65 | Generate a Peer DID according to the second algorithm. 66 | 67 | Reference: 68 | For this type of algorithm the DID Document is synthesized from the key material. 69 | 70 | :param encryption_keys: list of encryption keys 71 | :param signing_keys: list of signing keys 72 | :param service: JSON conforming to the DID specification (https://www.w3.org/TR/did-core/#services) 73 | or None if there is no services expected for this DID 74 | :raises ValueError: 75 | 1. if at least one of signing keys is not a BaseKey supporting authentication 76 | 2. if at least one of encryption keys is not a BaseKey supporting key agreement 77 | 3. if service is not valid JSON 78 | :return: generated Peer DID 79 | """ 80 | for k in encryption_keys: 81 | if KeyRelationshipType.KEY_AGREEMENT not in k.relationships: 82 | raise ValueError("Key agreement not supported for key: {}.".format(k)) 83 | for k in signing_keys: 84 | if KeyRelationshipType.AUTHENTICATION not in k.relationships: 85 | raise ValueError("Authentication not supported for key: {}.".format(k)) 86 | 87 | enc_sep = "." + Numalgo2Prefix.KEY_AGREEMENT.value 88 | auth_sep = "." + Numalgo2Prefix.AUTHENTICATION.value 89 | encryption_keys_str = ( 90 | enc_sep + enc_sep.join(key.to_multibase() for key in encryption_keys) 91 | if encryption_keys 92 | else "" 93 | ) 94 | auth_keys_str = ( 95 | auth_sep + auth_sep.join(key.to_multibase() for key in signing_keys) 96 | if signing_keys 97 | else "" 98 | ) 99 | service_str = encode_service(service) 100 | 101 | peer_did = DID("did:peer:2" + encryption_keys_str + auth_keys_str + service_str) 102 | return peer_did 103 | 104 | 105 | def resolve_peer_did( 106 | peer_did: Union[str, DID], 107 | format: KeyFormat = KeyFormat.MULTIBASE, 108 | ) -> DIDDocument: 109 | """ 110 | Resolve a DID Document from a Peer DID. 111 | 112 | :param peer_did: Peer DID to resolve 113 | :param format: the format of public keys in the DID Document. Default format is multibase. 114 | :raises MalformedPeerDIDError: if peer_did parameter does not match Peer DID spec 115 | :return: resolved DID Document as a JSON string 116 | """ 117 | if not is_peer_did(peer_did): 118 | raise MalformedPeerDIDError("Does not match peer DID regexp") 119 | if peer_did[9] == "0": 120 | did_doc = _build_did_doc_numalgo_0(peer_did, format) 121 | else: 122 | did_doc = _build_did_doc_numalgo_2(peer_did, format) 123 | return did_doc 124 | 125 | 126 | def _did_document_builder(peer_did: Union[str, DID]) -> DIDDocumentBuilder: 127 | try: 128 | return DIDDocumentBuilder(peer_did) 129 | except InvalidDIDError as e: 130 | raise MalformedPeerDIDError("Invalid peer DID") from e 131 | 132 | 133 | def _add_key_to_document(builder: DIDDocumentBuilder, key: BaseKey): 134 | ver_method_result = key.verification_method(builder.id) 135 | builder.verification_method.methods.append(ver_method_result.method) 136 | ver_ident = DIDUrl.parse(ver_method_result.method.id) 137 | if ver_method_result.context and ver_method_result.context not in builder.context: 138 | builder.context.append(ver_method_result.context) 139 | for rel in key.relationships: 140 | if rel == KeyRelationshipType.AUTHENTICATION: 141 | builder.authentication.reference(ver_ident) 142 | builder.assertion_method.reference(ver_ident) 143 | builder.capability_delegation.reference(ver_ident) 144 | builder.capability_invocation.reference(ver_ident) 145 | elif rel == KeyRelationshipType.KEY_AGREEMENT: 146 | builder.key_agreement.reference(ver_ident) 147 | 148 | 149 | def _build_did_doc_numalgo_0( 150 | peer_did: Union[str, DID], format: KeyFormat 151 | ) -> DIDDocument: 152 | decoded_key = decode_multibase_numbasis(peer_did[10:], format) 153 | builder = _did_document_builder(peer_did) 154 | _add_key_to_document(builder, decoded_key) 155 | return builder.build() 156 | 157 | 158 | def _build_did_doc_numalgo_2( 159 | peer_did: Union[str, DID], format: KeyFormat 160 | ) -> DIDDocument: 161 | keys = peer_did[11:] 162 | keys = keys.split(".") 163 | builder = _did_document_builder(peer_did) 164 | 165 | for key in keys: 166 | if not key: 167 | raise MalformedPeerDIDError("Blank key entry") 168 | prefix = key[0] 169 | if prefix == Numalgo2Prefix.SERVICE.value: 170 | for svc in decode_service(key[1:]): 171 | builder.service.services.append(svc) 172 | elif prefix == Numalgo2Prefix.AUTHENTICATION.value: 173 | decoded_key = decode_multibase_numbasis(key[1:], format) 174 | if KeyRelationshipType.AUTHENTICATION not in decoded_key.relationships: 175 | raise MalformedPeerDIDError( 176 | "Authentication not supported for key: {}.".format(key) 177 | ) 178 | _add_key_to_document(builder, decoded_key) 179 | elif prefix == Numalgo2Prefix.KEY_AGREEMENT.value: 180 | decoded_key = decode_multibase_numbasis(key[1:], format) 181 | if KeyRelationshipType.KEY_AGREEMENT not in decoded_key.relationships: 182 | raise MalformedPeerDIDError( 183 | "Key agreement not supported for key: {}.".format(key) 184 | ) 185 | _add_key_to_document(builder, decoded_key) 186 | else: 187 | raise MalformedPeerDIDError("Unknown prefix: {}.".format(prefix)) 188 | 189 | return builder.build() 190 | -------------------------------------------------------------------------------- /peerdid/errors.py: -------------------------------------------------------------------------------- 1 | """Error classes.""" 2 | 3 | 4 | class PeerDIDError(Exception): 5 | """Base class for Peer DID exceptions.""" 6 | 7 | 8 | class MalformedPeerDIDError(PeerDIDError): 9 | """An invalid peer DID was provided.""" 10 | 11 | def __init__(self, msg: str) -> None: 12 | """Initializer.""" 13 | super().__init__("Invalid peer DID provided. {}.".format(msg)) 14 | -------------------------------------------------------------------------------- /peerdid/keys.py: -------------------------------------------------------------------------------- 1 | """Peer DID key handling.""" 2 | 3 | from abc import ABC, abstractmethod 4 | from enum import Enum 5 | from typing import List, Optional, NamedTuple, Type, Union 6 | from uuid import uuid4 7 | 8 | from pydid import DID, DIDUrl, VerificationMethod 9 | from pydid.verification_method import ( 10 | Ed25519VerificationKey2018, 11 | Ed25519VerificationKey2020, 12 | JsonWebKey2020, 13 | X25519KeyAgreementKey2019, 14 | X25519KeyAgreementKey2020, 15 | ) 16 | 17 | from .core.jwk_okp import jwk_to_public_key, public_key_to_jwk 18 | from .core.multibase import ( 19 | MultibaseFormat, 20 | from_base58, 21 | from_multibase, 22 | to_base58, 23 | to_multibase, 24 | ) 25 | from .core.multicodec import Codec, from_multicodec 26 | 27 | ED25519_KEY_LENGTH = 32 28 | ED25519_2020_CONTEXT = "https://w3id.org/security/suites/ed25519-2020/v1" 29 | X25519_KEY_LENGTH = 32 30 | X25519_2020_CONTEXT = "https://w3id.org/security/suites/x25519-2020/v1" 31 | JWS_2020_CONTEXT = "https://w3id.org/security/suites/jws-2020/v1" 32 | 33 | 34 | class KeyFormat(Enum): 35 | """Supported key output formats.""" 36 | 37 | JWK = 1 38 | BASE58 = 2 39 | MULTIBASE = 3 40 | 41 | 42 | class KeyRelationshipType(Enum): 43 | """Supported key relationship types.""" 44 | 45 | AUTHENTICATION = 1 46 | KEY_AGREEMENT = 2 47 | 48 | 49 | VerificationMethodResult = NamedTuple( 50 | "VerificationMethodResult", 51 | [ 52 | ("context", Optional[str]), 53 | ("method", VerificationMethod), 54 | ], 55 | ) 56 | 57 | 58 | class BaseKey(ABC): 59 | """Base class for key types.""" 60 | 61 | codec: Codec 62 | format: KeyFormat 63 | ident: Optional[Union[str, DIDUrl]] = None 64 | key_length: Optional[int] = None 65 | public_key: bytes 66 | relationships: List[KeyRelationshipType] 67 | 68 | @classmethod 69 | def for_codec(cls, codec: Codec) -> Type["BaseKey"]: 70 | """Get the BaseKey subclass for a specific codec and key format.""" 71 | cls_codec = getattr(cls, "codec", None) 72 | if cls_codec: 73 | if cls_codec != codec: 74 | raise ValueError( 75 | "Codec mismatch: expected {}, found {}".format(cls_codec, codec) 76 | ) 77 | return cls 78 | 79 | if codec == Codec.ED25519: 80 | return Ed25519VerificationKey 81 | elif codec == Codec.X25519: 82 | return X25519KeyAgreementKey 83 | raise ValueError("Unsupported codec") 84 | 85 | @classmethod 86 | def from_base58( 87 | cls, 88 | enc_key: str, 89 | codec: Codec = None, 90 | ident: Union[str, DIDUrl] = None, 91 | format: KeyFormat = None, 92 | ) -> "BaseKey": 93 | """Load a base58-encoded key.""" 94 | codec = codec or getattr(cls, "codec", None) 95 | if not codec: 96 | raise ValueError("Unspecified codec") 97 | public_key = from_base58(enc_key) 98 | key_type_cls = cls.for_codec(codec) 99 | return key_type_cls(public_key, ident=ident, format=format or KeyFormat.BASE58) 100 | 101 | @classmethod 102 | def from_multibase( 103 | cls, multibase: str, ident: Union[str, DIDUrl] = None, format: KeyFormat = None 104 | ) -> "BaseKey": 105 | """Load a multibase, multicodec-encoded key.""" 106 | encnumbasis, multicodec = from_multibase(multibase) 107 | ident = ident or "#" + encnumbasis[:8] 108 | public_key, codec = from_multicodec(multicodec) 109 | key_type_cls = cls.for_codec(codec) 110 | return key_type_cls( 111 | public_key, ident=ident, format=format or KeyFormat.MULTIBASE 112 | ) 113 | 114 | @classmethod 115 | def from_jwk( 116 | cls, 117 | jwk: Union[str, dict], 118 | ident: Union[str, DIDUrl] = None, 119 | format: KeyFormat = None, 120 | ) -> "BaseKey": 121 | """Load a JWK key.""" 122 | public_key, codec = jwk_to_public_key(jwk) 123 | key_type_cls = cls.for_codec(codec) 124 | return key_type_cls(public_key, ident=ident, format=format or KeyFormat.JWK) 125 | 126 | def __init__( 127 | self, 128 | public_key: bytes, 129 | ident: Union[str, DIDUrl] = None, 130 | format: KeyFormat = None, 131 | ): 132 | """Initializer.""" 133 | self.public_key = public_key 134 | self.ident = ident or "#" + str(uuid4) 135 | self.format = format or KeyFormat.MULTIBASE 136 | self.validate() 137 | 138 | def validate(self): 139 | """Validate the key. 140 | 141 | :raises ValueError: if the public key is invalid 142 | """ 143 | if self.key_length and len(self.public_key) != self.key_length: 144 | raise ValueError( 145 | "Invalid public key, expected {} bytes".format(self.key_length) 146 | ) 147 | 148 | @abstractmethod 149 | def verification_method( 150 | self, controller: Union[str, DID], format: KeyFormat = None, **extra 151 | ) -> VerificationMethodResult: 152 | """Generate a VerificationMethod entry for this key.""" 153 | 154 | def to_multibase(self, format: MultibaseFormat = None) -> str: 155 | """Encode this key in multibase format.""" 156 | return to_multibase(self.codec.encode_multicodec(self.public_key), format) 157 | 158 | def __eq__(self, other: object) -> bool: 159 | """Compare to another key for equality.""" 160 | if self.__class__ is not other.__class__: 161 | return False 162 | return self.public_key == other.public_key 163 | 164 | def __repr__(self) -> str: 165 | """Key representation.""" 166 | return "<{} {}>".format(self.__class__.__name__, self.to_multibase()) 167 | 168 | 169 | class Ed25519VerificationKey(BaseKey): 170 | """Ed25519 verification key.""" 171 | 172 | codec = Codec.ED25519 173 | key_length = ED25519_KEY_LENGTH 174 | relationships = [KeyRelationshipType.AUTHENTICATION] 175 | 176 | def verification_method( 177 | self, controller: Union[str, DID], format: KeyFormat = None, **extra 178 | ) -> VerificationMethodResult: 179 | """Generate a VerificationMethod entry for this key.""" 180 | method = None 181 | context = None 182 | 183 | format = format or self.format 184 | if format == KeyFormat.BASE58: 185 | method = Ed25519VerificationKey2018.make( 186 | id=self.ident, 187 | controller=controller, 188 | public_key_base58=to_base58(self.public_key), 189 | **extra 190 | ) 191 | elif format == KeyFormat.MULTIBASE: 192 | context = ED25519_2020_CONTEXT 193 | method = Ed25519VerificationKey2020.make( 194 | id=self.ident, 195 | controller=controller, 196 | public_key_multibase=to_multibase( 197 | self.codec.encode_multicodec(self.public_key) 198 | ), 199 | **extra 200 | ) 201 | elif format == KeyFormat.JWK: 202 | context = JWS_2020_CONTEXT 203 | jwk = public_key_to_jwk(self.public_key, self.codec) 204 | method = JsonWebKey2020.make( 205 | id=self.ident, controller=controller, public_key_jwk=jwk, **extra 206 | ) 207 | 208 | if not method: 209 | raise ValueError("Unsupported key format for export") 210 | return VerificationMethodResult(context, method) 211 | 212 | 213 | class X25519KeyAgreementKey(BaseKey): 214 | """X25519 public encryption key.""" 215 | 216 | codec = Codec.X25519 217 | key_length = X25519_KEY_LENGTH 218 | relationships = [KeyRelationshipType.KEY_AGREEMENT] 219 | 220 | def verification_method( 221 | self, controller: Union[str, DID], format: KeyFormat = None, **extra 222 | ) -> VerificationMethodResult: 223 | """Generate a VerificationMethod entry for this key.""" 224 | method = None 225 | context = None 226 | 227 | format = format or self.format 228 | if format == KeyFormat.BASE58: 229 | method = X25519KeyAgreementKey2019.make( 230 | id=self.ident, 231 | controller=controller, 232 | public_key_base58=to_base58(self.public_key), 233 | **extra 234 | ) 235 | elif format == KeyFormat.MULTIBASE: 236 | context = X25519_2020_CONTEXT 237 | method = X25519KeyAgreementKey2020.make( 238 | id=self.ident, 239 | controller=controller, 240 | public_key_multibase=to_multibase( 241 | self.codec.encode_multicodec(self.public_key) 242 | ), 243 | **extra 244 | ) 245 | elif format == KeyFormat.JWK: 246 | context = JWS_2020_CONTEXT 247 | jwk = public_key_to_jwk(self.public_key, self.codec) 248 | method = JsonWebKey2020.make( 249 | id=self.ident, controller=controller, public_key_jwk=jwk, **extra 250 | ) 251 | 252 | if not method: 253 | raise ValueError("Unsupported key format for export") 254 | return VerificationMethodResult(context, method) 255 | -------------------------------------------------------------------------------- /renovate.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": [ 3 | "config:base" 4 | ] 5 | } 6 | -------------------------------------------------------------------------------- /setup.cfg: -------------------------------------------------------------------------------- 1 | [metadata] 2 | name = peerdid 3 | version = attr: peerdid.__version__ 4 | url = https://github.com/sicpa-dlab/peer-did-python 5 | project_urls = 6 | # Documentation = 7 | Source Code = https://github.com/sicpa-dlab/peer-did-python 8 | Issue Tracker = https://github.com/sicpa-dlab/peer-did-python/issues/ 9 | license = Apache-2.0 10 | author = SICPA 11 | author_email = DLCHOpenSourceContrib@sicpa.com 12 | # maintainer = 13 | # maintainer_email = 14 | description = PeerDID for Python 15 | long_description = file: README.md 16 | long_description_content_type = text/markdown 17 | # TODO 18 | classifiers = 19 | # Development Status :: 20 | License :: OSI Approved :: Apache Software License 21 | Operating System :: OS Independent 22 | Programming Language :: Python 23 | Programming Language :: Python :: 3 24 | Programming Language :: Python :: 3.7 25 | Programming Language :: Python :: 3.8 26 | Programming Language :: Python :: 3.9 27 | Programming Language :: Python :: 3.10 28 | # Topic :: 29 | 30 | [options] 31 | packages = peerdid,peerdid.core 32 | include_package_data = true 33 | python_requires = >= 3.8 34 | # Dependencies are in setup.py for GitHub's dependency graph. 35 | 36 | [options.packages.find] 37 | exclude = 38 | tests 39 | tests.* 40 | docs 41 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | from setuptools import setup 2 | 3 | # TODO move remaining things 4 | setup( 5 | install_requires=["base58~=2.1.0", "pydid~=0.4.0.post1", "varint~=1.0.2"], 6 | extras_require={"tests": ["pytest==6.2.5", "pytest-xdist==2.3.0"]}, 7 | ) 8 | -------------------------------------------------------------------------------- /tests/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sicpa-dlab/peer-did-python/fa9315981dfcdb35d22b6137052061fa512027da/tests/__init__.py -------------------------------------------------------------------------------- /tests/test_create_peer_did_numalgo_0.py: -------------------------------------------------------------------------------- 1 | import json 2 | 3 | import pytest 4 | 5 | from peerdid.dids import create_peer_did_numalgo_0, is_peer_did 6 | from peerdid.keys import Ed25519VerificationKey, X25519KeyAgreementKey 7 | 8 | 9 | @pytest.mark.parametrize( 10 | "ver_material", 11 | [ 12 | pytest.param( 13 | Ed25519VerificationKey.from_base58( 14 | "ByHnpUCFb1vAfh9CFZ8ZkmUZguURW8nSw889hy6rD8L7" 15 | ), 16 | id="ED25519_VERIFICATION_KEY_2018_BASE58", 17 | ), 18 | pytest.param( 19 | Ed25519VerificationKey.from_multibase( 20 | "z6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V" 21 | ), 22 | id="ED25519_VERIFICATION_KEY_2020_MULTIBASE", 23 | ), 24 | pytest.param( 25 | Ed25519VerificationKey.from_jwk( 26 | { 27 | "kty": "OKP", 28 | "crv": "Ed25519", 29 | "x": "owBhCbktDjkfS6PdQddT0D3yjSitaSysP3YimJ_YgmA", 30 | }, 31 | ), 32 | id="JSON_WEB_KEY_2020_JWK_DICT", 33 | ), 34 | pytest.param( 35 | Ed25519VerificationKey.from_jwk( 36 | json.dumps( 37 | { 38 | "kty": "OKP", 39 | "crv": "Ed25519", 40 | "x": "owBhCbktDjkfS6PdQddT0D3yjSitaSysP3YimJ_YgmA", 41 | } 42 | ) 43 | ), 44 | id="JSON_WEB_KEY_2020_JWK_JSON", 45 | ), 46 | ], 47 | ) 48 | def test_create_numalgo_0_positive(ver_material): 49 | peer_did_algo_0 = create_peer_did_numalgo_0(inception_key=ver_material) 50 | assert ( 51 | peer_did_algo_0 == "did:peer:0z6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V" 52 | ) 53 | assert is_peer_did(peer_did_algo_0) 54 | 55 | 56 | @pytest.mark.parametrize( 57 | "ver_material", 58 | [ 59 | pytest.param( 60 | X25519KeyAgreementKey.from_base58( 61 | "ByHnpUCFb1vAfh9CFZ8ZkmUZguURW8nSw889hy6rD8L7" 62 | ), 63 | id="X25519_KEY_AGREEMENT_KEY_2019_BASE58", 64 | ), 65 | pytest.param( 66 | X25519KeyAgreementKey.from_multibase( 67 | "z6LSbysY2xFMRpGMhb7tFTLMpeuPRaqaWM1yECx2AtzE3KCc", 68 | ), 69 | id="X25519_KEY_AGREEMENT_KEY_2019_MULTIBASE", 70 | ), 71 | pytest.param( 72 | X25519KeyAgreementKey.from_jwk( 73 | { 74 | "kty": "OKP", 75 | "crv": "X25519", 76 | "x": "owBhCbktDjkfS6PdQddT0D3yjSitaSysP3YimJ_YgmA", 77 | } 78 | ), 79 | id="JSON_WEB_KEY_2020_JWK", 80 | ), 81 | ], 82 | ) 83 | def test_create_numalgo_0_invalid_inception_key_type(ver_material): 84 | with pytest.raises(ValueError, match=r"Authentication not supported for key"): 85 | create_peer_did_numalgo_0(inception_key=ver_material) 86 | -------------------------------------------------------------------------------- /tests/test_create_peer_did_numalgo_2.py: -------------------------------------------------------------------------------- 1 | import pytest 2 | 3 | from peerdid.dids import create_peer_did_numalgo_2, is_peer_did 4 | from peerdid.keys import Ed25519VerificationKey, X25519KeyAgreementKey 5 | 6 | VALID_X25519_KEY_AGREEMENT_KEY_2019 = X25519KeyAgreementKey.from_base58( 7 | "JhNWeSVLMYccCk7iopQW4guaSJTojqpMEELgSLhKwRr" 8 | ) 9 | VALID_X25519_KEY_AGREEMENT_KEY_2020 = X25519KeyAgreementKey.from_multibase( 10 | "z6LSbysY2xFMRpGMhb7tFTLMpeuPRaqaWM1yECx2AtzE3KCc" 11 | ) 12 | VALID_X25519_KEY_AGREEMENT_KEY_JWK = X25519KeyAgreementKey.from_jwk( 13 | { 14 | "kty": "OKP", 15 | "crv": "X25519", 16 | "x": "BIiFcQEn3dfvB2pjlhOQQour6jXy9d5s2FKEJNTOJik", 17 | } 18 | ) 19 | 20 | VALID_ED25519_VERIFICATION_KEY_2018_1 = Ed25519VerificationKey.from_base58( 21 | "ByHnpUCFb1vAfh9CFZ8ZkmUZguURW8nSw889hy6rD8L7" 22 | ) 23 | VALID_ED25519_VERIFICATION_KEY_2020_1 = Ed25519VerificationKey.from_multibase( 24 | "z6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V", 25 | ) 26 | VALID_ED25519_VERIFICATION_KEY_JWK_1 = Ed25519VerificationKey.from_jwk( 27 | { 28 | "kty": "OKP", 29 | "crv": "Ed25519", 30 | "x": "owBhCbktDjkfS6PdQddT0D3yjSitaSysP3YimJ_YgmA", 31 | }, 32 | ) 33 | 34 | VALID_ED25519_VERIFICATION_KEY_2018_2 = Ed25519VerificationKey.from_base58( 35 | "3M5RCDjPTWPkKSN3sxUmmMqHbmRPegYP1tjcKyrDbt9J" 36 | ) 37 | VALID_ED25519_VERIFICATION_KEY_2020_2 = Ed25519VerificationKey.from_multibase( 38 | "z6MkgoLTnTypo3tDRwCkZXSccTPHRLhF4ZnjhueYAFpEX6vg" 39 | ) 40 | VALID_ED25519_VERIFICATION_KEY_JWK_2 = Ed25519VerificationKey.from_jwk( 41 | { 42 | "kty": "OKP", 43 | "crv": "Ed25519", 44 | "x": "Itv8B__b1-Jos3LCpUe8EdTFGTCa_Dza6_3848P3R70", 45 | } 46 | ) 47 | 48 | VALID_SERVICE = """ 49 | { 50 | "type": "DIDCommMessaging", 51 | "serviceEndpoint": "https://example.com/endpoint", 52 | "routingKeys": ["did:example:somemediator#somekey"], 53 | "accept": ["didcomm/v2", "didcomm/aip2;env=rfc587"] 54 | } 55 | """ 56 | 57 | 58 | @pytest.mark.parametrize( 59 | "encryption_keys, signing_keys", 60 | [ 61 | pytest.param( 62 | [VALID_X25519_KEY_AGREEMENT_KEY_2019], 63 | [ 64 | VALID_ED25519_VERIFICATION_KEY_2018_1, 65 | VALID_ED25519_VERIFICATION_KEY_2018_2, 66 | ], 67 | id="BASE58", 68 | ), 69 | pytest.param( 70 | [VALID_X25519_KEY_AGREEMENT_KEY_2020], 71 | [ 72 | VALID_ED25519_VERIFICATION_KEY_2020_1, 73 | VALID_ED25519_VERIFICATION_KEY_2020_2, 74 | ], 75 | id="MULTIBASE", 76 | ), 77 | pytest.param( 78 | [VALID_X25519_KEY_AGREEMENT_KEY_JWK], 79 | [ 80 | VALID_ED25519_VERIFICATION_KEY_JWK_1, 81 | VALID_ED25519_VERIFICATION_KEY_JWK_2, 82 | ], 83 | id="JWK", 84 | ), 85 | ], 86 | ) 87 | def test_create_numalgo_2_positive(encryption_keys, signing_keys): 88 | service = """[ 89 | { 90 | "type": "DIDCommMessaging", 91 | "serviceEndpoint": "https://example.com/endpoint", 92 | "routingKeys": ["did:example:somemediator#somekey"] 93 | }, 94 | { 95 | "type": "example", 96 | "serviceEndpoint": "https://example.com/endpoint2", 97 | "routingKeys": ["did:example:somemediator#somekey2"], 98 | "accept": ["didcomm/v2", "didcomm/aip2;env=rfc587"] 99 | } 100 | ] 101 | """ 102 | 103 | peer_did_algo_2 = create_peer_did_numalgo_2( 104 | encryption_keys=encryption_keys, signing_keys=signing_keys, service=service 105 | ) 106 | assert ( 107 | peer_did_algo_2 108 | == "did:peer:2.Ez6LSbysY2xFMRpGMhb7tFTLMpeuPRaqaWM1yECx2AtzE3KCc" 109 | ".Vz6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V" 110 | ".Vz6MkgoLTnTypo3tDRwCkZXSccTPHRLhF4ZnjhueYAFpEX6vg" 111 | ".SW3sidCI6ImRtIiwicyI6Imh0dHBzOi8vZXhhbXBsZS5jb20vZW5kcG9pbnQiLCJyIjpbImRpZDpleGFtcGxlOnNvbWVtZWRpYXRvciNzb21la2V5Il19LHsidCI6ImV4YW1wbGUiLCJzIjoiaHR0cHM6Ly9leGFtcGxlLmNvbS9lbmRwb2ludDIiLCJyIjpbImRpZDpleGFtcGxlOnNvbWVtZWRpYXRvciNzb21la2V5MiJdLCJhIjpbImRpZGNvbW0vdjIiLCJkaWRjb21tL2FpcDI7ZW52PXJmYzU4NyJdfV0" 112 | ) 113 | assert is_peer_did(peer_did_algo_2) 114 | 115 | 116 | def test_create_numalgo_2_service_not_array(): 117 | encryption_keys = [VALID_X25519_KEY_AGREEMENT_KEY_2019] 118 | signing_keys = [ 119 | VALID_ED25519_VERIFICATION_KEY_2018_1, 120 | VALID_ED25519_VERIFICATION_KEY_2018_2, 121 | ] 122 | service = """ 123 | { 124 | "type": "DIDCommMessaging", 125 | "serviceEndpoint": "https://example.com/endpoint", 126 | "routingKeys": ["did:example:somemediator#somekey"] 127 | } 128 | """ 129 | 130 | peer_did_algo_2 = create_peer_did_numalgo_2( 131 | encryption_keys=encryption_keys, signing_keys=signing_keys, service=service 132 | ) 133 | assert is_peer_did(peer_did_algo_2) 134 | 135 | 136 | def test_create_numalgo_2_service_minimal_fields(): 137 | encryption_keys = [VALID_X25519_KEY_AGREEMENT_KEY_2019] 138 | signing_keys = [ 139 | VALID_ED25519_VERIFICATION_KEY_2018_1, 140 | VALID_ED25519_VERIFICATION_KEY_2018_2, 141 | ] 142 | service = """ 143 | { 144 | "type": "DIDCommMessaging", 145 | "serviceEndpoint": "https://example.com/endpoint" 146 | } 147 | """ 148 | 149 | peer_did_algo_2 = create_peer_did_numalgo_2( 150 | encryption_keys=encryption_keys, signing_keys=signing_keys, service=service 151 | ) 152 | assert is_peer_did(peer_did_algo_2) 153 | 154 | 155 | def test_create_numalgo_2_service_1_element_array(): 156 | encryption_keys = [VALID_X25519_KEY_AGREEMENT_KEY_2019] 157 | signing_keys = [ 158 | VALID_ED25519_VERIFICATION_KEY_2018_1, 159 | VALID_ED25519_VERIFICATION_KEY_2018_2, 160 | ] 161 | service = """ 162 | [ 163 | { 164 | "type": "DIDCommMessaging", 165 | "serviceEndpoint": "https://example.com/endpoint", 166 | "routingKeys": ["did:example:somemediator#somekey"] 167 | } 168 | ] 169 | """ 170 | 171 | peer_did_algo_2 = create_peer_did_numalgo_2( 172 | encryption_keys=encryption_keys, signing_keys=signing_keys, service=service 173 | ) 174 | assert is_peer_did(peer_did_algo_2) 175 | 176 | 177 | @pytest.mark.parametrize( 178 | "encryption_keys, signing_keys", 179 | [ 180 | pytest.param( 181 | [], 182 | [ 183 | VALID_ED25519_VERIFICATION_KEY_2018_1, 184 | VALID_ED25519_VERIFICATION_KEY_2018_2, 185 | ], 186 | id="BASE58", 187 | ), 188 | pytest.param( 189 | [], 190 | [ 191 | VALID_ED25519_VERIFICATION_KEY_2020_1, 192 | VALID_ED25519_VERIFICATION_KEY_2020_2, 193 | ], 194 | id="MULTIBASE", 195 | ), 196 | pytest.param( 197 | [], 198 | [ 199 | VALID_ED25519_VERIFICATION_KEY_JWK_1, 200 | VALID_ED25519_VERIFICATION_KEY_JWK_2, 201 | ], 202 | id="JWK", 203 | ), 204 | ], 205 | ) 206 | def test_create_numalgo_2_without_encryption_keys(encryption_keys, signing_keys): 207 | service = VALID_SERVICE 208 | 209 | peer_did_algo_2 = create_peer_did_numalgo_2( 210 | encryption_keys=encryption_keys, signing_keys=signing_keys, service=service 211 | ) 212 | assert ( 213 | peer_did_algo_2 214 | == "did:peer:2.Vz6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V" 215 | ".Vz6MkgoLTnTypo3tDRwCkZXSccTPHRLhF4ZnjhueYAFpEX6vg" 216 | ".SeyJ0IjoiZG0iLCJzIjoiaHR0cHM6Ly9leGFtcGxlLmNvbS9lbmRwb2ludCIsInIiOlsiZGlkOmV4YW1wbGU6c29tZW1lZGlhdG9yI3NvbWVrZXkiXSwiYSI6WyJkaWRjb21tL3YyIiwiZGlkY29tbS9haXAyO2Vudj1yZmM1ODciXX0" 217 | ) 218 | assert is_peer_did(peer_did_algo_2) 219 | 220 | 221 | @pytest.mark.parametrize( 222 | "encryption_keys, signing_keys", 223 | [ 224 | pytest.param([VALID_X25519_KEY_AGREEMENT_KEY_2019], [], id="BASE58"), 225 | pytest.param([VALID_X25519_KEY_AGREEMENT_KEY_2020], [], id="MULTIBASE"), 226 | pytest.param([VALID_X25519_KEY_AGREEMENT_KEY_JWK], [], id="JWK"), 227 | ], 228 | ) 229 | def test_create_numalgo_2_without_signing_keys(encryption_keys, signing_keys): 230 | service = VALID_SERVICE 231 | 232 | peer_did_algo_2 = create_peer_did_numalgo_2( 233 | encryption_keys=encryption_keys, signing_keys=signing_keys, service=service 234 | ) 235 | assert ( 236 | peer_did_algo_2 237 | == "did:peer:2.Ez6LSbysY2xFMRpGMhb7tFTLMpeuPRaqaWM1yECx2AtzE3KCc" 238 | ".SeyJ0IjoiZG0iLCJzIjoiaHR0cHM6Ly9leGFtcGxlLmNvbS9lbmRwb2ludCIsInIiOlsiZGlkOmV4YW1wbGU6c29tZW1lZGlhdG9yI3NvbWVrZXkiXSwiYSI6WyJkaWRjb21tL3YyIiwiZGlkY29tbS9haXAyO2Vudj1yZmM1ODciXX0" 239 | ) 240 | assert is_peer_did(peer_did_algo_2) 241 | 242 | 243 | def test_create_numalgo_2_wrong_service(): 244 | encryption_keys = [VALID_X25519_KEY_AGREEMENT_KEY_2019] 245 | signing_keys = [ 246 | VALID_ED25519_VERIFICATION_KEY_2018_1, 247 | VALID_ED25519_VERIFICATION_KEY_2018_2, 248 | ] 249 | service = "......." 250 | 251 | with pytest.raises(ValueError, match=r"Service is not valid JSON"): 252 | create_peer_did_numalgo_2( 253 | encryption_keys=encryption_keys, signing_keys=signing_keys, service=service 254 | ) 255 | 256 | 257 | def test_create_numalgo_2_encryption_key_as_signing(): 258 | encryption_keys = [VALID_X25519_KEY_AGREEMENT_KEY_2019] 259 | service = VALID_SERVICE 260 | with pytest.raises(ValueError, match=r"Authentication not supported for key"): 261 | create_peer_did_numalgo_2( 262 | encryption_keys=encryption_keys, 263 | signing_keys=encryption_keys, 264 | service=service, 265 | ) 266 | 267 | 268 | def test_create_numalgo_2_signing_key_as_encryption(): 269 | signing_keys = [ 270 | VALID_ED25519_VERIFICATION_KEY_2018_1, 271 | VALID_ED25519_VERIFICATION_KEY_2018_2, 272 | ] 273 | service = VALID_SERVICE 274 | with pytest.raises(ValueError, match=r"Key agreement not supported for key"): 275 | create_peer_did_numalgo_2( 276 | encryption_keys=signing_keys, signing_keys=signing_keys, service=service 277 | ) 278 | 279 | 280 | def test_create_numalgo_2_service_is_none(): 281 | encryption_keys = [VALID_X25519_KEY_AGREEMENT_KEY_2019] 282 | signing_keys = [ 283 | VALID_ED25519_VERIFICATION_KEY_2018_1, 284 | VALID_ED25519_VERIFICATION_KEY_2018_2, 285 | ] 286 | service = None 287 | 288 | peer_did_algo_2 = create_peer_did_numalgo_2( 289 | encryption_keys=encryption_keys, signing_keys=signing_keys, service=service 290 | ) 291 | assert ( 292 | peer_did_algo_2 293 | == "did:peer:2.Ez6LSbysY2xFMRpGMhb7tFTLMpeuPRaqaWM1yECx2AtzE3KCc" 294 | ".Vz6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V" 295 | ".Vz6MkgoLTnTypo3tDRwCkZXSccTPHRLhF4ZnjhueYAFpEX6vg" 296 | ) 297 | assert is_peer_did(peer_did_algo_2) 298 | 299 | 300 | def test_create_numalgo_2_encryption_and_signing_keys_are_1_element_array(): 301 | encryption_keys = [VALID_X25519_KEY_AGREEMENT_KEY_2019] 302 | signing_keys = [VALID_ED25519_VERIFICATION_KEY_2018_1] 303 | service = VALID_SERVICE 304 | 305 | peer_did_algo_2 = create_peer_did_numalgo_2( 306 | encryption_keys=encryption_keys, signing_keys=signing_keys, service=service 307 | ) 308 | assert is_peer_did(peer_did_algo_2) 309 | 310 | 311 | def test_create_numalgo_2_encryption_and_signing_keys_are_more_than_1_element_array(): 312 | encryption_keys = [ 313 | VALID_X25519_KEY_AGREEMENT_KEY_2019, 314 | VALID_X25519_KEY_AGREEMENT_KEY_2020, 315 | VALID_X25519_KEY_AGREEMENT_KEY_JWK, 316 | ] 317 | signing_keys = [ 318 | VALID_ED25519_VERIFICATION_KEY_2018_1, 319 | VALID_ED25519_VERIFICATION_KEY_2020_2, 320 | ] 321 | service = VALID_SERVICE 322 | 323 | peer_did_algo_2 = create_peer_did_numalgo_2( 324 | encryption_keys=encryption_keys, signing_keys=signing_keys, service=service 325 | ) 326 | assert is_peer_did(peer_did_algo_2) 327 | 328 | 329 | def test_create_numalgo_2_service_has_more_fields_than_in_conversion_table(): 330 | encryption_keys = [VALID_X25519_KEY_AGREEMENT_KEY_2019] 331 | signing_keys = [ 332 | VALID_ED25519_VERIFICATION_KEY_2018_1, 333 | VALID_ED25519_VERIFICATION_KEY_2018_2, 334 | ] 335 | service = """{ 336 | "type": "DIDCommMessaging", 337 | "serviceEndpoint": "https://example.com/endpoint", 338 | "routingKeys": ["did:example:somemediator#somekey"], 339 | "example1": "myExample1", 340 | "example2": "myExample2" 341 | } 342 | """ 343 | 344 | peer_did_algo_2 = create_peer_did_numalgo_2( 345 | encryption_keys=encryption_keys, signing_keys=signing_keys, service=service 346 | ) 347 | assert is_peer_did(peer_did_algo_2) 348 | 349 | 350 | def test_create_numalgo_2_service_is_not_didcommmessaging(): 351 | encryption_keys = [VALID_X25519_KEY_AGREEMENT_KEY_2019] 352 | signing_keys = [ 353 | VALID_ED25519_VERIFICATION_KEY_2018_1, 354 | VALID_ED25519_VERIFICATION_KEY_2018_2, 355 | ] 356 | service = """{ 357 | "type": "example1", 358 | "serviceEndpoint": "https://example.com/endpoint", 359 | "routingKeys": ["did:example:somemediator#somekey"] 360 | } 361 | """ 362 | 363 | peer_did_algo_2 = create_peer_did_numalgo_2( 364 | encryption_keys=encryption_keys, signing_keys=signing_keys, service=service 365 | ) 366 | assert is_peer_did(peer_did_algo_2) 367 | 368 | 369 | def test_create_numalgo_2_service_is_empty_string(): 370 | encryption_keys = [VALID_X25519_KEY_AGREEMENT_KEY_2019] 371 | signing_keys = [ 372 | VALID_ED25519_VERIFICATION_KEY_2018_1, 373 | VALID_ED25519_VERIFICATION_KEY_2018_2, 374 | ] 375 | service = "" 376 | 377 | peer_did_algo_2 = create_peer_did_numalgo_2( 378 | encryption_keys=encryption_keys, signing_keys=signing_keys, service=service 379 | ) 380 | assert is_peer_did(peer_did_algo_2) 381 | 382 | 383 | def test_create_numalgo_2_service_is_empty_array(): 384 | encryption_keys = [VALID_X25519_KEY_AGREEMENT_KEY_2019] 385 | signing_keys = [ 386 | VALID_ED25519_VERIFICATION_KEY_2018_1, 387 | VALID_ED25519_VERIFICATION_KEY_2018_2, 388 | ] 389 | service = [] 390 | 391 | peer_did_algo_2 = create_peer_did_numalgo_2( 392 | encryption_keys=encryption_keys, signing_keys=signing_keys, service=service 393 | ) 394 | assert is_peer_did(peer_did_algo_2) 395 | 396 | 397 | def test_create_numalgo_2_different_types_of_service(): 398 | encryption_keys = [VALID_X25519_KEY_AGREEMENT_KEY_2019] 399 | signing_keys = [ 400 | VALID_ED25519_VERIFICATION_KEY_2018_1, 401 | VALID_ED25519_VERIFICATION_KEY_2018_2, 402 | ] 403 | service = """ 404 | [ 405 | { 406 | "type": "example1", 407 | "serviceEndpoint": "https://example.com/endpoint", 408 | "routingKeys": ["did:example:somemediator#somekey"] 409 | }, 410 | { 411 | "type": "example2", 412 | "serviceEndpoint": "https://example.com/endpoint2", 413 | "routingKeys": ["did:example:somemediator#somekey2"] 414 | } 415 | ] 416 | """ 417 | 418 | peer_did_algo_2 = create_peer_did_numalgo_2( 419 | encryption_keys=encryption_keys, signing_keys=signing_keys, service=service 420 | ) 421 | assert is_peer_did(peer_did_algo_2) 422 | -------------------------------------------------------------------------------- /tests/test_peer_did_helpers.py: -------------------------------------------------------------------------------- 1 | import pytest 2 | 3 | from pydid import Service 4 | 5 | from peerdid.core.peer_did_helper import ( 6 | encode_service, 7 | decode_service, 8 | decode_multibase_numbasis, 9 | ) 10 | from peerdid.keys import ( 11 | Ed25519VerificationKey, 12 | X25519KeyAgreementKey, 13 | KeyFormat, 14 | ) 15 | 16 | 17 | def test_encode_service(): 18 | service = """{ 19 | "type": "DIDCommMessaging", 20 | "serviceEndpoint": "https://example.com/endpoint", 21 | "routingKeys": ["did:example:somemediator#somekey"], 22 | "accept": ["didcomm/v2", "didcomm/aip2;env=rfc587"] 23 | } 24 | """ 25 | 26 | assert ( 27 | encode_service(service) 28 | == ".SeyJ0IjoiZG0iLCJzIjoiaHR0cHM6Ly9leGFtcGxlLmNvbS9lbmRwb2ludCIsInIiOlsiZGlkOmV4YW1wbGU6c29tZW1lZGlhdG9yI3NvbWVrZXkiXSwiYSI6WyJkaWRjb21tL3YyIiwiZGlkY29tbS9haXAyO2Vudj1yZmM1ODciXX0" 29 | ) 30 | 31 | 32 | def test_decode_service(): 33 | service = decode_service( 34 | service="eyJ0IjoiZG0iLCJzIjoiaHR0cHM6Ly9leGFtcGxlLmNvbS9lbmRwb2ludCIsInIiOlsiZGlkOmV4YW1wbGU6c29tZW1lZGlhdG9yI3NvbWVrZXkiXSwiYSI6WyJkaWRjb21tL3YyIiwiZGlkY29tbS9haXAyO2Vudj1yZmM1ODciXX0", 35 | ) 36 | expected = [ 37 | Service.deserialize( 38 | { 39 | "id": "#didcommmessaging-0", 40 | "type": "DIDCommMessaging", 41 | "serviceEndpoint": "https://example.com/endpoint", 42 | "routingKeys": ["did:example:somemediator#somekey"], 43 | "accept": ["didcomm/v2", "didcomm/aip2;env=rfc587"], 44 | } 45 | ) 46 | ] 47 | assert service == expected 48 | 49 | 50 | def test_encode_service_minimal_fields(): 51 | service = """{ 52 | "type": "DIDCommMessaging", 53 | "serviceEndpoint": "https://example.com/endpoint" 54 | } 55 | """ 56 | assert ( 57 | encode_service(service) 58 | == ".SeyJ0IjoiZG0iLCJzIjoiaHR0cHM6Ly9leGFtcGxlLmNvbS9lbmRwb2ludCJ9" 59 | ) 60 | 61 | 62 | def test_decode_service_minimal_fields(): 63 | service = decode_service( 64 | service="eyJ0IjoiZG0iLCJzIjoiaHR0cHM6Ly9leGFtcGxlLmNvbS9lbmRwb2ludCJ9", 65 | ) 66 | expected = [ 67 | Service.deserialize( 68 | { 69 | "id": "#didcommmessaging-0", 70 | "type": "DIDCommMessaging", 71 | "serviceEndpoint": "https://example.com/endpoint", 72 | } 73 | ) 74 | ] 75 | assert service == expected 76 | 77 | 78 | def test_encode_service_with_multiple_entries_list(): 79 | services = """ 80 | [ 81 | { 82 | "type": "DIDCommMessaging", 83 | "serviceEndpoint": "https://example.com/endpoint", 84 | "routingKeys": ["did:example:somemediator#somekey"], 85 | "accept": ["didcomm/v2", "didcomm/aip2;env=rfc587"] 86 | }, 87 | { 88 | "type": "DIDCommMessaging", 89 | "serviceEndpoint": "https://example.com/endpoint2", 90 | "routingKeys": ["did:example:somemediator#somekey2"] 91 | } 92 | ] 93 | """ 94 | 95 | encoded_services = encode_service(services) 96 | assert ( 97 | encoded_services 98 | == ".SW3sidCI6ImRtIiwicyI6Imh0dHBzOi8vZXhhbXBsZS5jb20vZW5kcG9pbnQiLCJyIjpbImRpZDpleGFtcGxlOnNvbWVtZWRpYXRvciNzb21la2V5Il0sImEiOlsiZGlkY29tbS92MiIsImRpZGNvbW0vYWlwMjtlbnY9cmZjNTg3Il19LHsidCI6ImRtIiwicyI6Imh0dHBzOi8vZXhhbXBsZS5jb20vZW5kcG9pbnQyIiwiciI6WyJkaWQ6ZXhhbXBsZTpzb21lbWVkaWF0b3Ijc29tZWtleTIiXX1d" 99 | ) 100 | 101 | 102 | def test_decode_service_with_multiple_entries_list(): 103 | service = decode_service( 104 | service="W3sidCI6ImRtIiwicyI6Imh0dHBzOi8vZXhhbXBsZS5jb20vZW5kcG9pbnQiLCJyIjpbImRpZDpleGFtcGxlOnNvbWVtZWRpYXRvciNzb21la2V5Il0sImEiOlsiZGlkY29tbS92MiIsImRpZGNvbW0vYWlwMjtlbnY9cmZjNTg3Il19LHsidCI6ImRtIiwicyI6Imh0dHBzOi8vZXhhbXBsZS5jb20vZW5kcG9pbnQyIiwiciI6WyJkaWQ6ZXhhbXBsZTpzb21lbWVkaWF0b3Ijc29tZWtleTIiXX1d", 105 | ) 106 | expected = [ 107 | Service.deserialize( 108 | { 109 | "id": "#didcommmessaging-0", 110 | "type": "DIDCommMessaging", 111 | "serviceEndpoint": "https://example.com/endpoint", 112 | "routingKeys": ["did:example:somemediator#somekey"], 113 | "accept": ["didcomm/v2", "didcomm/aip2;env=rfc587"], 114 | } 115 | ), 116 | Service.deserialize( 117 | { 118 | "id": "#didcommmessaging-1", 119 | "type": "DIDCommMessaging", 120 | "serviceEndpoint": "https://example.com/endpoint2", 121 | "routingKeys": ["did:example:somemediator#somekey2"], 122 | } 123 | ), 124 | ] 125 | assert service == expected 126 | 127 | 128 | @pytest.mark.parametrize( 129 | "input_multibase,format,expected", 130 | [ 131 | pytest.param( 132 | "z6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V", 133 | KeyFormat.BASE58, 134 | Ed25519VerificationKey.from_base58( 135 | "ByHnpUCFb1vAfh9CFZ8ZkmUZguURW8nSw889hy6rD8L7" 136 | ), 137 | id="base58-ed25519", 138 | ), 139 | pytest.param( 140 | "z6LSbysY2xFMRpGMhb7tFTLMpeuPRaqaWM1yECx2AtzE3KCc", 141 | KeyFormat.BASE58, 142 | X25519KeyAgreementKey.from_base58( 143 | "JhNWeSVLMYccCk7iopQW4guaSJTojqpMEELgSLhKwRr" 144 | ), 145 | id="base58-x25519", 146 | ), 147 | pytest.param( 148 | "z6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V", 149 | KeyFormat.MULTIBASE, 150 | Ed25519VerificationKey.from_multibase( 151 | "z6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V" 152 | ), 153 | id="multibase-ed25519", 154 | ), 155 | pytest.param( 156 | "z6LSbysY2xFMRpGMhb7tFTLMpeuPRaqaWM1yECx2AtzE3KCc", 157 | KeyFormat.MULTIBASE, 158 | X25519KeyAgreementKey.from_multibase( 159 | "z6LSbysY2xFMRpGMhb7tFTLMpeuPRaqaWM1yECx2AtzE3KCc" 160 | ), 161 | id="multibase-x25519", 162 | ), 163 | pytest.param( 164 | "z6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V", 165 | KeyFormat.JWK, 166 | Ed25519VerificationKey.from_jwk( 167 | { 168 | "kty": "OKP", 169 | "crv": "Ed25519", 170 | "x": "owBhCbktDjkfS6PdQddT0D3yjSitaSysP3YimJ_YgmA", 171 | } 172 | ), 173 | id="jwk-ed25519", 174 | ), 175 | pytest.param( 176 | "z6LSbysY2xFMRpGMhb7tFTLMpeuPRaqaWM1yECx2AtzE3KCc", 177 | KeyFormat.JWK, 178 | X25519KeyAgreementKey.from_jwk( 179 | { 180 | "kty": "OKP", 181 | "crv": "X25519", 182 | "x": "BIiFcQEn3dfvB2pjlhOQQour6jXy9d5s2FKEJNTOJik", 183 | }, 184 | ), 185 | id="jwk-x25519", 186 | ), 187 | ], 188 | ) 189 | def test_decode_numbasis(input_multibase, format, expected): 190 | res = decode_multibase_numbasis(input_multibase, format) 191 | assert res == expected 192 | -------------------------------------------------------------------------------- /tests/test_resolve_peer_did_numalgo_0.py: -------------------------------------------------------------------------------- 1 | import pytest 2 | 3 | from peerdid import DIDDocument 4 | from peerdid.errors import MalformedPeerDIDError 5 | from peerdid.dids import resolve_peer_did 6 | from peerdid.keys import KeyFormat 7 | from tests.test_vectors import ( 8 | PEER_DID_NUMALGO_0, 9 | DID_DOC_NUMALGO_O_BASE58, 10 | DID_DOC_NUMALGO_O_MULTIBASE, 11 | DID_DOC_NUMALGO_O_JWK, 12 | ) 13 | 14 | 15 | def test_resolve_positive_default(): 16 | did_doc = resolve_peer_did(peer_did=PEER_DID_NUMALGO_0) 17 | assert did_doc == DIDDocument.from_json(DID_DOC_NUMALGO_O_MULTIBASE) 18 | 19 | 20 | def test_resolve_positive_base58(): 21 | did_doc = resolve_peer_did(peer_did=PEER_DID_NUMALGO_0, format=KeyFormat.BASE58) 22 | assert did_doc == DIDDocument.from_json(DID_DOC_NUMALGO_O_BASE58) 23 | 24 | 25 | def test_resolve_positive_multibase(): 26 | did_doc = resolve_peer_did(peer_did=PEER_DID_NUMALGO_0, format=KeyFormat.MULTIBASE) 27 | assert did_doc == DIDDocument.from_json(DID_DOC_NUMALGO_O_MULTIBASE) 28 | 29 | 30 | def test_resolve_positive_jwk(): 31 | did_doc = resolve_peer_did(peer_did=PEER_DID_NUMALGO_0, format=KeyFormat.JWK) 32 | assert did_doc == DIDDocument.from_json(DID_DOC_NUMALGO_O_JWK) 33 | 34 | 35 | def test_resolve_unsupported_did_method(): 36 | with pytest.raises( 37 | MalformedPeerDIDError, 38 | match=r"Invalid peer DID provided.*Does not match peer DID regexp", 39 | ): 40 | resolve_peer_did( 41 | peer_did="did:key:0z6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V" 42 | ) 43 | 44 | 45 | def test_resolve_invalid_peer_did(): 46 | with pytest.raises( 47 | MalformedPeerDIDError, 48 | match=r"Invalid peer DID provided.*Does not match peer DID regexp", 49 | ): 50 | resolve_peer_did( 51 | peer_did="did:peer:0z6MkqRYqQiSBytw86Qbs2ZWUkGv22od935YF4s8M7V!" 52 | ) 53 | 54 | 55 | def test_resolve_unsupported_numalgo_code(): 56 | with pytest.raises( 57 | MalformedPeerDIDError, 58 | match=r"Invalid peer DID provided.*Does not match peer DID regexp", 59 | ): 60 | resolve_peer_did( 61 | peer_did="did:peer:1z6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V" 62 | ) 63 | 64 | 65 | def test_resolve_numalgo_0_malformed_base58_encoding(): 66 | with pytest.raises( 67 | MalformedPeerDIDError, 68 | match=r"Invalid peer DID provided.*Does not match peer DID regexp", 69 | ): 70 | resolve_peer_did( 71 | peer_did="did:peer:0z6MkqRYqQiSgvZQd0Bytw86Qbs2ZWUkGv22od935YF4s8M7V" 72 | ) 73 | 74 | 75 | def test_resolve_numalgo_0_unsupported_transform_code(): 76 | with pytest.raises( 77 | MalformedPeerDIDError, 78 | match=r"Invalid peer DID provided.*Does not match peer DID regexp", 79 | ): 80 | resolve_peer_did( 81 | peer_did="did:peer:0a6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V" 82 | ) 83 | 84 | 85 | def test_resolve_numalgo_0_malformed_multicodec_encoding(): 86 | with pytest.raises( 87 | MalformedPeerDIDError, match=r"Invalid peer DID provided.*Invalid key" 88 | ): 89 | resolve_peer_did( 90 | peer_did="did:peer:0z6666RYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V" 91 | ) 92 | 93 | 94 | def test_resolve_numalgo_0_invalid_key_type(): 95 | with pytest.raises( 96 | MalformedPeerDIDError, match=r"Invalid peer DID provided.*Invalid key" 97 | ): 98 | resolve_peer_did( 99 | peer_did="did:peer:0z6QSbysY2xFMRpGMhb7tFTLMpeuPRaqaWM1yECx2AtzE3KCc" 100 | ) 101 | 102 | 103 | def test_resolve_numalgo_0_short_key(): 104 | with pytest.raises( 105 | MalformedPeerDIDError, 106 | match=r"Invalid peer DID provided.*Invalid key", 107 | ): 108 | resolve_peer_did(peer_did="did:peer:0z6LSbysY2xc") 109 | 110 | 111 | def test_resolve_numalgo_0_long_key(): 112 | with pytest.raises( 113 | MalformedPeerDIDError, 114 | match=r"Invalid peer DID provided.*Invalid key", 115 | ): 116 | resolve_peer_did( 117 | peer_did="did:peer:0z6LSbysY2xcccccccccccccccccccccccccccccccccccccccccccccccccccccc" 118 | ) 119 | -------------------------------------------------------------------------------- /tests/test_resolve_peer_did_numalgo_2.py: -------------------------------------------------------------------------------- 1 | import pytest 2 | 3 | from peerdid import DIDDocument 4 | from peerdid.errors import MalformedPeerDIDError 5 | from peerdid.dids import resolve_peer_did 6 | from peerdid.keys import KeyFormat 7 | from tests.test_vectors import ( 8 | DID_DOC_NUMALGO_2_BASE58, 9 | PEER_DID_NUMALGO_2, 10 | DID_DOC_NUMALGO_2_MULTIBASE, 11 | DID_DOC_NUMALGO_2_JWK, 12 | DID_DOC_NUMALGO_2_MULTIBASE_2_SERVICES, 13 | PEER_DID_NUMALGO_2_NO_SERVICES, 14 | DID_DOC_NUMALGO_2_MULTIBASE_NO_SERVICES, 15 | PEER_DID_NUMALGO_2_2_SERVICES, 16 | DID_DOC_NUMALGO_2_MULTIBASE_MINIMAL_SERVICES, 17 | PEER_DID_NUMALGO_2_MINIMAL_SERVICES, 18 | ) 19 | 20 | 21 | def test_resolve_numalgo_2_positive_default(): 22 | did_doc = resolve_peer_did(peer_did=PEER_DID_NUMALGO_2) 23 | assert did_doc == DIDDocument.from_json(DID_DOC_NUMALGO_2_MULTIBASE) 24 | 25 | 26 | def test_resolve_numalgo_2_positive_base58(): 27 | did_doc = resolve_peer_did(peer_did=PEER_DID_NUMALGO_2, format=KeyFormat.BASE58) 28 | assert did_doc == DIDDocument.from_json(DID_DOC_NUMALGO_2_BASE58) 29 | 30 | 31 | def test_resolve_numalgo_2_positive_multibase(): 32 | did_doc = resolve_peer_did(peer_did=PEER_DID_NUMALGO_2, format=KeyFormat.MULTIBASE) 33 | assert did_doc == DIDDocument.from_json(DID_DOC_NUMALGO_2_MULTIBASE) 34 | 35 | 36 | def test_resolve_numalgo_2_positive_jwk(): 37 | did_doc = resolve_peer_did(peer_did=PEER_DID_NUMALGO_2, format=KeyFormat.JWK) 38 | assert did_doc == DIDDocument.from_json(DID_DOC_NUMALGO_2_JWK) 39 | 40 | 41 | def test_resolve_numalgo_2_positive_service_is_2_element_array(): 42 | did_doc = resolve_peer_did(PEER_DID_NUMALGO_2_2_SERVICES) 43 | assert did_doc == DIDDocument.from_json(DID_DOC_NUMALGO_2_MULTIBASE_2_SERVICES) 44 | 45 | 46 | def test_resolve_numalgo_2_positive_no_service(): 47 | did_doc = resolve_peer_did(PEER_DID_NUMALGO_2_NO_SERVICES) 48 | assert did_doc == DIDDocument.from_json(DID_DOC_NUMALGO_2_MULTIBASE_NO_SERVICES) 49 | 50 | 51 | def test_resolve_numalgo_2_positive_minimal_service(): 52 | did_doc = resolve_peer_did(PEER_DID_NUMALGO_2_MINIMAL_SERVICES) 53 | assert did_doc == DIDDocument.from_json( 54 | DID_DOC_NUMALGO_2_MULTIBASE_MINIMAL_SERVICES 55 | ) 56 | 57 | 58 | def test_resolve_numalgo_2_unsupported_transform_code(): 59 | with pytest.raises( 60 | MalformedPeerDIDError, 61 | match=r"Invalid peer DID provided.*Does not match peer DID regexp", 62 | ): 63 | resolve_peer_did( 64 | "did:peer:2.Ea6LSbysY2xFMRpGMhb7tFTLMpeuPRaqaWM1yECx2AtzE3KCc" 65 | ".Va6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V" 66 | ".SeyJ0IjoiZG0iLCJzIjoiaHR0cHM6Ly9leGFtcGxlLmNvbS9lbmRwb2ludCIsInIiOlsiZGlkOmV4YW1wbGU6c29tZW1lZGlhdG9yI3NvbWVrZXkiXX0" 67 | ) 68 | 69 | 70 | def test_resolve_numalgo_2_signing_malformed_base58_encoding(): 71 | with pytest.raises( 72 | MalformedPeerDIDError, 73 | match=r"Invalid peer DID provided.*Does not match peer DID regexp", 74 | ): 75 | resolve_peer_did( 76 | "did:peer:2.Ez6LSbysY2xFMRpGMhb7tFTLMpeuPRaqaWM1yECx2AtzE3KCc" 77 | ".Vz6MkqRYqQiSgvZQdnBytw86Qbs0ZWUkGv22od935YF4s8M7V" 78 | ".SeyJ0IjoiZG0iLCJzIjoiaHR0cHM6Ly9leGFtcGxlLmNvbS9lbmRwb2ludCIsInIiOlsiZGlkOmV4YW1wbGU6c29tZW1lZGlhdG9yI3NvbWVrZXkiXX0" 79 | ) 80 | 81 | 82 | def test_resolve_numalgo_2_encryption_malformed_base58_encoding(): 83 | with pytest.raises( 84 | MalformedPeerDIDError, 85 | match=r"Invalid peer DID provided.*Does not match peer DID regexp", 86 | ): 87 | resolve_peer_did( 88 | "did:peer:2.Ez6LSbysY2xFMRpG0hb7tFTLMpeuPRaqaWM1yECx2AtzE3KCc" 89 | ".Vz6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V" 90 | ".SeyJ0IjoiZG0iLCJzIjoiaHR0cHM6Ly9leGFtcGxlLmNvbS9lbmRwb2ludCIsInIiOlsiZGlkOmV4YW1wbGU6c29tZW1lZGlhdG9yI3NvbWVrZXkiXX0=" 91 | ) 92 | 93 | 94 | def test_resolve_numalgo_2_signing_malformed_multicodec_encoding(): 95 | with pytest.raises( 96 | MalformedPeerDIDError, match=r"Invalid peer DID provided.*Invalid key" 97 | ): 98 | resolve_peer_did( 99 | "did:peer:2.Ez6LSbysY2xFMRpGMhb7tFTLMpeuPRaqaWM1yECx2AtzE3KCc" 100 | ".Vz6666YqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V" 101 | ".SeyJ0IjoiZG0iLCJzIjoiaHR0cHM6Ly9leGFtcGxlLmNvbS9lbmRwb2ludCIsInIiOlsiZGlkOmV4YW1wbGU6c29tZW1lZGlhdG9yI3NvbWVrZXkiXX0" 102 | ) 103 | 104 | 105 | def test_resolve_numalgo_2_encryption_malformed_multicodec_encoding(): 106 | with pytest.raises( 107 | MalformedPeerDIDError, match=r"Invalid peer DID provided.*Invalid key" 108 | ): 109 | resolve_peer_did( 110 | "did:peer:2.Ez7777sY2xFMRpGMhb7tFTLMpeuPRaqaWM1yECx2AtzE3KCc" 111 | ".Vz6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V" 112 | ".SeyJ0IjoiZG0iLCJzIjoiaHR0cHM6Ly9leGFtcGxlLmNvbS9lbmRwb2ludCIsInIiOlsiZGlkOmV4YW1wbGU6c29tZW1lZGlhdG9yI3NvbWVrZXkiXX0" 113 | ) 114 | 115 | 116 | def test_resolve_numalgo_2_signing_invalid_key_type(): 117 | with pytest.raises( 118 | MalformedPeerDIDError, 119 | match=r"Invalid peer DID provided.*Authentication not supported for key", 120 | ): 121 | resolve_peer_did( 122 | "did:peer:2.Vz6LSbysY2xFMRpGMhb7tFTLMpeuPRaqaWM1yECx2AtzE3KCc" 123 | ".Vz6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V" 124 | ".SeyJ0IjoiZG0iLCJzIjoiaHR0cHM6Ly9leGFtcGxlLmNvbS9lbmRwb2ludCIsInIiOlsiZGlkOmV4YW1wbGU6c29tZW1lZGlhdG9yI3NvbWVrZXkiXX0" 125 | ) 126 | 127 | 128 | def test_resolve_numalgo_2_encryption_invalid_key_type(): 129 | with pytest.raises( 130 | MalformedPeerDIDError, 131 | match=r"Invalid peer DID provided.*Key agreement not supported for key", 132 | ): 133 | resolve_peer_did( 134 | "did:peer:2.Ez6LSbysY2xFMRpGMhb7tFTLMpeuPRaqaWM1yECx2AtzE3KCc" 135 | ".Ez6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V" 136 | ".SeyJ0IjoiZG0iLCJzIjoiaHR0cHM6Ly9leGFtcGxlLmNvbS9lbmRwb2ludCIsInIiOlsiZGlkOmV4YW1wbGU6c29tZW1lZGlhdG9yI3NvbWVrZXkiXX0" 137 | ) 138 | 139 | 140 | def test_resolve_numalgo_2_malformed_service_encoding(): 141 | with pytest.raises( 142 | MalformedPeerDIDError, 143 | match=r"Invalid peer DID provided.*Does not match peer DID regexp", 144 | ): 145 | resolve_peer_did( 146 | "did:peer:2" 147 | + ".Ez6LSbysY2xFMRpGMhb7tFTLMpeuPRaqaWM1yECx2AtzE3KCc" 148 | + ".Vz6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V" 149 | + ".Vz6MkgoLTnTypo3tDRwCkZXSccTPHRLhF4ZnjhueYAFpEX6vg" 150 | + ".SeyJ0IjoiZG0iLCJzIjoiaHR0cHM6Ly\\9leGFtcGxlLmNvbS9lbmRwb2ludCIsInIiOlsiZGlkOmV4YW1wbGU6c29tZW1lZGlhdG9yI3NvbWVrZXkiXSwiYSI6WyJkaWRjb21tL3YyIiwiZGlkY29tbS9haXAyO2Vudj1yZmM1ODciXX0" 151 | ) 152 | 153 | 154 | def test_resolve_numalgo_2_malformed_service(): 155 | with pytest.raises( 156 | MalformedPeerDIDError, match=r"Invalid peer DID provided.*Invalid service" 157 | ): 158 | resolve_peer_did( 159 | "did:peer:2" 160 | + ".Ez6LSbysY2xFMRpGMhb7tFTLMpeuPRaqaWM1yECx2AtzE3KCc" 161 | + ".Vz6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V" 162 | + ".Vz6MkgoLTnTypo3tDRwCkZXSccTPHRLhF4ZnjhueYAFpEX6vg" 163 | + ".Sasdf123" 164 | ) 165 | 166 | 167 | def test_resolve_numalgo_2_invalid_prefix(): 168 | with pytest.raises( 169 | MalformedPeerDIDError, 170 | match=r"Invalid peer DID provided.*Does not match peer DID regexp", 171 | ): 172 | resolve_peer_did( 173 | "did:peer:2" 174 | ".Cz6LSbysY2xFMRpGMhb7tFTLMpeuPRaqaWM1yECx2AtzE3KCc" 175 | ".Vz6MkgoLTnTypo3tDRwCkZXSccTPHRLhF4ZnjhueYAFpEX6vg" 176 | ".SeyJ0IjoiZG0iLCJzIjoiaHR0cHM6Ly9leGFtcGxlLmNvbS9lbmRwb2ludCIsInIiOlsiZGlkOmV4YW1wbGU6c29tZW1lZGlhdG9yI3NvbWVrZXkiXX0" 177 | ) 178 | 179 | 180 | def test_resolve_numalgo_2_short_signing_key(): 181 | with pytest.raises( 182 | MalformedPeerDIDError, 183 | match=r"Invalid peer DID provided.*Invalid key", 184 | ): 185 | resolve_peer_did( 186 | "did:peer:2.Ez6LSbysY2xFMRpGMhb7tFTLMpeuPRaqaWM1yECx2AtzE3KCc" 187 | ".Vz6MkqRYqQiS" 188 | ".SeyJ0IjoiZG0iLCJzIjoiaHR0cHM6Ly9leGFtcGxlLmNvbS9lbmRwb2ludCIsInIiOlsiZGlkOmV4YW1wbGU6c29tZW1lZGlhdG9yI3NvbWVrZXkiXX0" 189 | ) 190 | 191 | 192 | def test_resolve_numalgo_2_short_encryption_key(): 193 | with pytest.raises( 194 | MalformedPeerDIDError, 195 | match=r"Invalid peer DID provided.*Invalid key", 196 | ): 197 | resolve_peer_did( 198 | "did:peer:2" 199 | + ".Ez6LSbysY2" 200 | + ".Vz6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V" 201 | + ".Vz6MkgoLTnTypo3tDRwCkZXSccTPHRLhF4ZnjhueYAFpEX6vg" 202 | + ".SeyJ0IjoiZG0iLCJzIjoiaHR0cHM6Ly9leGFtcGxlLmNvbS9lbmRwb2ludCIsInIiOlsiZGlkOmV4YW1wbGU6c29tZW1lZGlhdG9yI3NvbWVrZXkiXX0" 203 | ) 204 | 205 | 206 | def test_resolve_numalgo_2_long_signing_key(): 207 | with pytest.raises( 208 | MalformedPeerDIDError, 209 | match=r"Invalid peer DID provided.*Invalid key", 210 | ): 211 | resolve_peer_did( 212 | "did:peer:2" 213 | + ".Ez6LSbysY2xFMRpGMhb7tFTLMpeuPRaqaWM1yECx2AtzE3KCc" 214 | + ".Vz6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7Vcccccccccc" 215 | + ".Vz6MkgoLTnTypo3tDRwCkZXSccTPHRLhF4ZnjhueYAFpEX6vg" 216 | + ".SeyJ0IjoiZG0iLCJzIjoiaHR0cHM6Ly9leGFtcGxlLmNvbS9lbmRwb2ludCIsInIiOlsiZGlkOmV4YW1wbGU6c29tZW1lZGlhdG9yI3NvbWVrZXkiXX0" 217 | ) 218 | 219 | 220 | def test_resolve_numalgo_2_long_encryption_key(): 221 | with pytest.raises( 222 | MalformedPeerDIDError, 223 | match=r"Invalid peer DID provided.*Invalid key", 224 | ): 225 | resolve_peer_did( 226 | "did:peer:2" 227 | + ".Ez6LSbysY2xFMRpGMhb7tFTLMpeuPRaqaWM1yECx2AtzE3KCcccccccc" 228 | + ".Vz6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V" 229 | + ".Vz6MkgoLTnTypo3tDRwCkZXSccTPHRLhF4ZnjhueYAFpEX6vg" 230 | + ".SeyJ0IjoiZG0iLCJzIjoiaHR0cHM6Ly9leGFtcGxlLmNvbS9lbmRwb2ludCIsInIiOlsiZGlkOmV4YW1wbGU6c29tZW1lZGlhdG9yI3NvbWVrZXkiXX0" 231 | ) 232 | -------------------------------------------------------------------------------- /tests/test_vectors.py: -------------------------------------------------------------------------------- 1 | PEER_DID_NUMALGO_0 = "did:peer:0z6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V" 2 | 3 | DID_DOC_NUMALGO_O_BASE58 = """ 4 | { 5 | "@context": ["https://www.w3.org/ns/did/v1"], 6 | "id": "did:peer:0z6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V", 7 | "verificationMethod": [ 8 | { 9 | "id": "#6MkqRYqQ", 10 | "type": "Ed25519VerificationKey2018", 11 | "controller": "did:peer:0z6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V", 12 | "publicKeyBase58": "ByHnpUCFb1vAfh9CFZ8ZkmUZguURW8nSw889hy6rD8L7" 13 | } 14 | ], 15 | "authentication": ["#6MkqRYqQ"], 16 | "assertionMethod": ["#6MkqRYqQ"], 17 | "capabilityInvocation": ["#6MkqRYqQ"], 18 | "capabilityDelegation": ["#6MkqRYqQ"] 19 | } 20 | """ 21 | 22 | DID_DOC_NUMALGO_O_MULTIBASE = """ 23 | { 24 | "@context": [ 25 | "https://www.w3.org/ns/did/v1", 26 | "https://w3id.org/security/suites/ed25519-2020/v1" 27 | ], 28 | "id": "did:peer:0z6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V", 29 | "verificationMethod": [ 30 | { 31 | "id": "#6MkqRYqQ", 32 | "type": "Ed25519VerificationKey2020", 33 | "controller": "did:peer:0z6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V", 34 | "publicKeyMultibase": "z6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V" 35 | } 36 | ], 37 | "authentication": ["#6MkqRYqQ"], 38 | "assertionMethod": ["#6MkqRYqQ"], 39 | "capabilityInvocation": ["#6MkqRYqQ"], 40 | "capabilityDelegation": ["#6MkqRYqQ"] 41 | } 42 | """ 43 | 44 | DID_DOC_NUMALGO_O_JWK = """ 45 | { 46 | "@context": [ 47 | "https://www.w3.org/ns/did/v1", 48 | "https://w3id.org/security/suites/jws-2020/v1" 49 | ], 50 | "id": "did:peer:0z6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V", 51 | "verificationMethod": [ 52 | { 53 | "id": "#6MkqRYqQ", 54 | "type": "JsonWebKey2020", 55 | "controller": "did:peer:0z6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V", 56 | "publicKeyJwk": { 57 | "kty": "OKP", 58 | "crv": "Ed25519", 59 | "x": "owBhCbktDjkfS6PdQddT0D3yjSitaSysP3YimJ_YgmA" 60 | } 61 | } 62 | ], 63 | "authentication": ["#6MkqRYqQ"], 64 | "assertionMethod": ["#6MkqRYqQ"], 65 | "capabilityInvocation": ["#6MkqRYqQ"], 66 | "capabilityDelegation": ["#6MkqRYqQ"] 67 | } 68 | """ 69 | 70 | PEER_DID_NUMALGO_2 = ( 71 | "did:peer:2" 72 | + ".Ez6LSbysY2xFMRpGMhb7tFTLMpeuPRaqaWM1yECx2AtzE3KCc" 73 | + ".Vz6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V" 74 | + ".Vz6MkgoLTnTypo3tDRwCkZXSccTPHRLhF4ZnjhueYAFpEX6vg" 75 | + ".SeyJ0IjoiZG0iLCJzIjoiaHR0cHM6Ly9leGFtcGxlLmNvbS9lbmRwb2ludCIsInIiOlsiZGlkOmV4YW1wbGU6c29tZW1lZGlhdG9yI3NvbWVrZXkiXSwiYSI6WyJkaWRjb21tL3YyIiwiZGlkY29tbS9haXAyO2Vudj1yZmM1ODciXX0" 76 | ) 77 | 78 | DID_DOC_NUMALGO_2_BASE58 = """ 79 | { 80 | "@context": ["https://www.w3.org/ns/did/v1"], 81 | "id": "did:peer:2.Ez6LSbysY2xFMRpGMhb7tFTLMpeuPRaqaWM1yECx2AtzE3KCc.Vz6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V.Vz6MkgoLTnTypo3tDRwCkZXSccTPHRLhF4ZnjhueYAFpEX6vg.SeyJ0IjoiZG0iLCJzIjoiaHR0cHM6Ly9leGFtcGxlLmNvbS9lbmRwb2ludCIsInIiOlsiZGlkOmV4YW1wbGU6c29tZW1lZGlhdG9yI3NvbWVrZXkiXSwiYSI6WyJkaWRjb21tL3YyIiwiZGlkY29tbS9haXAyO2Vudj1yZmM1ODciXX0", 82 | "verificationMethod": [ 83 | { 84 | "id": "#6LSbysY2", 85 | "type": "X25519KeyAgreementKey2019", 86 | "controller": "did:peer:2.Ez6LSbysY2xFMRpGMhb7tFTLMpeuPRaqaWM1yECx2AtzE3KCc.Vz6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V.Vz6MkgoLTnTypo3tDRwCkZXSccTPHRLhF4ZnjhueYAFpEX6vg.SeyJ0IjoiZG0iLCJzIjoiaHR0cHM6Ly9leGFtcGxlLmNvbS9lbmRwb2ludCIsInIiOlsiZGlkOmV4YW1wbGU6c29tZW1lZGlhdG9yI3NvbWVrZXkiXSwiYSI6WyJkaWRjb21tL3YyIiwiZGlkY29tbS9haXAyO2Vudj1yZmM1ODciXX0", 87 | "publicKeyBase58": "JhNWeSVLMYccCk7iopQW4guaSJTojqpMEELgSLhKwRr" 88 | }, 89 | { 90 | "id": "#6MkqRYqQ", 91 | "type": "Ed25519VerificationKey2018", 92 | "controller": "did:peer:2.Ez6LSbysY2xFMRpGMhb7tFTLMpeuPRaqaWM1yECx2AtzE3KCc.Vz6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V.Vz6MkgoLTnTypo3tDRwCkZXSccTPHRLhF4ZnjhueYAFpEX6vg.SeyJ0IjoiZG0iLCJzIjoiaHR0cHM6Ly9leGFtcGxlLmNvbS9lbmRwb2ludCIsInIiOlsiZGlkOmV4YW1wbGU6c29tZW1lZGlhdG9yI3NvbWVrZXkiXSwiYSI6WyJkaWRjb21tL3YyIiwiZGlkY29tbS9haXAyO2Vudj1yZmM1ODciXX0", 93 | "publicKeyBase58": "ByHnpUCFb1vAfh9CFZ8ZkmUZguURW8nSw889hy6rD8L7" 94 | }, 95 | { 96 | "id": "#6MkgoLTn", 97 | "type": "Ed25519VerificationKey2018", 98 | "controller": "did:peer:2.Ez6LSbysY2xFMRpGMhb7tFTLMpeuPRaqaWM1yECx2AtzE3KCc.Vz6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V.Vz6MkgoLTnTypo3tDRwCkZXSccTPHRLhF4ZnjhueYAFpEX6vg.SeyJ0IjoiZG0iLCJzIjoiaHR0cHM6Ly9leGFtcGxlLmNvbS9lbmRwb2ludCIsInIiOlsiZGlkOmV4YW1wbGU6c29tZW1lZGlhdG9yI3NvbWVrZXkiXSwiYSI6WyJkaWRjb21tL3YyIiwiZGlkY29tbS9haXAyO2Vudj1yZmM1ODciXX0", 99 | "publicKeyBase58": "3M5RCDjPTWPkKSN3sxUmmMqHbmRPegYP1tjcKyrDbt9J" 100 | } 101 | ], 102 | "authentication": ["#6MkqRYqQ", "#6MkgoLTn"], 103 | "assertionMethod": ["#6MkqRYqQ", "#6MkgoLTn"], 104 | "keyAgreement": ["#6LSbysY2"], 105 | "capabilityInvocation": ["#6MkqRYqQ", "#6MkgoLTn"], 106 | "capabilityDelegation": ["#6MkqRYqQ", "#6MkgoLTn"], 107 | "service": [ 108 | { 109 | "id": "#didcommmessaging-0", 110 | "type": "DIDCommMessaging", 111 | "serviceEndpoint": "https://example.com/endpoint", 112 | "routingKeys": ["did:example:somemediator#somekey"], 113 | "accept": ["didcomm/v2", "didcomm/aip2;env=rfc587"] 114 | } 115 | ] 116 | } 117 | """ 118 | 119 | DID_DOC_NUMALGO_2_MULTIBASE = """ 120 | { 121 | "@context": [ 122 | "https://www.w3.org/ns/did/v1", 123 | "https://w3id.org/security/suites/x25519-2020/v1", 124 | "https://w3id.org/security/suites/ed25519-2020/v1" 125 | ], 126 | "id": "did:peer:2.Ez6LSbysY2xFMRpGMhb7tFTLMpeuPRaqaWM1yECx2AtzE3KCc.Vz6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V.Vz6MkgoLTnTypo3tDRwCkZXSccTPHRLhF4ZnjhueYAFpEX6vg.SeyJ0IjoiZG0iLCJzIjoiaHR0cHM6Ly9leGFtcGxlLmNvbS9lbmRwb2ludCIsInIiOlsiZGlkOmV4YW1wbGU6c29tZW1lZGlhdG9yI3NvbWVrZXkiXSwiYSI6WyJkaWRjb21tL3YyIiwiZGlkY29tbS9haXAyO2Vudj1yZmM1ODciXX0", 127 | "verificationMethod": [ 128 | { 129 | "id": "#6LSbysY2", 130 | "type": "X25519KeyAgreementKey2020", 131 | "controller": "did:peer:2.Ez6LSbysY2xFMRpGMhb7tFTLMpeuPRaqaWM1yECx2AtzE3KCc.Vz6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V.Vz6MkgoLTnTypo3tDRwCkZXSccTPHRLhF4ZnjhueYAFpEX6vg.SeyJ0IjoiZG0iLCJzIjoiaHR0cHM6Ly9leGFtcGxlLmNvbS9lbmRwb2ludCIsInIiOlsiZGlkOmV4YW1wbGU6c29tZW1lZGlhdG9yI3NvbWVrZXkiXSwiYSI6WyJkaWRjb21tL3YyIiwiZGlkY29tbS9haXAyO2Vudj1yZmM1ODciXX0", 132 | "publicKeyMultibase": "z6LSbysY2xFMRpGMhb7tFTLMpeuPRaqaWM1yECx2AtzE3KCc" 133 | }, 134 | { 135 | "id": "#6MkqRYqQ", 136 | "type": "Ed25519VerificationKey2020", 137 | "controller": "did:peer:2.Ez6LSbysY2xFMRpGMhb7tFTLMpeuPRaqaWM1yECx2AtzE3KCc.Vz6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V.Vz6MkgoLTnTypo3tDRwCkZXSccTPHRLhF4ZnjhueYAFpEX6vg.SeyJ0IjoiZG0iLCJzIjoiaHR0cHM6Ly9leGFtcGxlLmNvbS9lbmRwb2ludCIsInIiOlsiZGlkOmV4YW1wbGU6c29tZW1lZGlhdG9yI3NvbWVrZXkiXSwiYSI6WyJkaWRjb21tL3YyIiwiZGlkY29tbS9haXAyO2Vudj1yZmM1ODciXX0", 138 | "publicKeyMultibase": "z6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V" 139 | }, 140 | { 141 | "id": "#6MkgoLTn", 142 | "type": "Ed25519VerificationKey2020", 143 | "controller": "did:peer:2.Ez6LSbysY2xFMRpGMhb7tFTLMpeuPRaqaWM1yECx2AtzE3KCc.Vz6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V.Vz6MkgoLTnTypo3tDRwCkZXSccTPHRLhF4ZnjhueYAFpEX6vg.SeyJ0IjoiZG0iLCJzIjoiaHR0cHM6Ly9leGFtcGxlLmNvbS9lbmRwb2ludCIsInIiOlsiZGlkOmV4YW1wbGU6c29tZW1lZGlhdG9yI3NvbWVrZXkiXSwiYSI6WyJkaWRjb21tL3YyIiwiZGlkY29tbS9haXAyO2Vudj1yZmM1ODciXX0", 144 | "publicKeyMultibase": "z6MkgoLTnTypo3tDRwCkZXSccTPHRLhF4ZnjhueYAFpEX6vg" 145 | } 146 | ], 147 | "authentication": ["#6MkqRYqQ", "#6MkgoLTn"], 148 | "assertionMethod": ["#6MkqRYqQ", "#6MkgoLTn"], 149 | "keyAgreement": ["#6LSbysY2"], 150 | "capabilityInvocation": ["#6MkqRYqQ", "#6MkgoLTn"], 151 | "capabilityDelegation": ["#6MkqRYqQ", "#6MkgoLTn"], 152 | "service": [ 153 | { 154 | "id": "#didcommmessaging-0", 155 | "type": "DIDCommMessaging", 156 | "serviceEndpoint": "https://example.com/endpoint", 157 | "accept": ["didcomm/v2", "didcomm/aip2;env=rfc587"], 158 | "routingKeys": ["did:example:somemediator#somekey"] 159 | } 160 | ] 161 | } 162 | """ 163 | 164 | DID_DOC_NUMALGO_2_JWK = """ 165 | { 166 | "@context": [ 167 | "https://www.w3.org/ns/did/v1", 168 | "https://w3id.org/security/suites/jws-2020/v1" 169 | ], 170 | "id": "did:peer:2.Ez6LSbysY2xFMRpGMhb7tFTLMpeuPRaqaWM1yECx2AtzE3KCc.Vz6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V.Vz6MkgoLTnTypo3tDRwCkZXSccTPHRLhF4ZnjhueYAFpEX6vg.SeyJ0IjoiZG0iLCJzIjoiaHR0cHM6Ly9leGFtcGxlLmNvbS9lbmRwb2ludCIsInIiOlsiZGlkOmV4YW1wbGU6c29tZW1lZGlhdG9yI3NvbWVrZXkiXSwiYSI6WyJkaWRjb21tL3YyIiwiZGlkY29tbS9haXAyO2Vudj1yZmM1ODciXX0", 171 | "verificationMethod": [ 172 | { 173 | "id": "#6LSbysY2", 174 | "type": "JsonWebKey2020", 175 | "controller": "did:peer:2.Ez6LSbysY2xFMRpGMhb7tFTLMpeuPRaqaWM1yECx2AtzE3KCc.Vz6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V.Vz6MkgoLTnTypo3tDRwCkZXSccTPHRLhF4ZnjhueYAFpEX6vg.SeyJ0IjoiZG0iLCJzIjoiaHR0cHM6Ly9leGFtcGxlLmNvbS9lbmRwb2ludCIsInIiOlsiZGlkOmV4YW1wbGU6c29tZW1lZGlhdG9yI3NvbWVrZXkiXSwiYSI6WyJkaWRjb21tL3YyIiwiZGlkY29tbS9haXAyO2Vudj1yZmM1ODciXX0", 176 | "publicKeyJwk": { 177 | "kty": "OKP", 178 | "crv": "X25519", 179 | "x": "BIiFcQEn3dfvB2pjlhOQQour6jXy9d5s2FKEJNTOJik" 180 | } 181 | }, 182 | { 183 | "id": "#6MkqRYqQ", 184 | "type": "JsonWebKey2020", 185 | "controller": "did:peer:2.Ez6LSbysY2xFMRpGMhb7tFTLMpeuPRaqaWM1yECx2AtzE3KCc.Vz6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V.Vz6MkgoLTnTypo3tDRwCkZXSccTPHRLhF4ZnjhueYAFpEX6vg.SeyJ0IjoiZG0iLCJzIjoiaHR0cHM6Ly9leGFtcGxlLmNvbS9lbmRwb2ludCIsInIiOlsiZGlkOmV4YW1wbGU6c29tZW1lZGlhdG9yI3NvbWVrZXkiXSwiYSI6WyJkaWRjb21tL3YyIiwiZGlkY29tbS9haXAyO2Vudj1yZmM1ODciXX0", 186 | "publicKeyJwk": { 187 | "kty": "OKP", 188 | "crv": "Ed25519", 189 | "x": "owBhCbktDjkfS6PdQddT0D3yjSitaSysP3YimJ_YgmA" 190 | } 191 | }, 192 | { 193 | "id": "#6MkgoLTn", 194 | "type": "JsonWebKey2020", 195 | "controller": "did:peer:2.Ez6LSbysY2xFMRpGMhb7tFTLMpeuPRaqaWM1yECx2AtzE3KCc.Vz6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V.Vz6MkgoLTnTypo3tDRwCkZXSccTPHRLhF4ZnjhueYAFpEX6vg.SeyJ0IjoiZG0iLCJzIjoiaHR0cHM6Ly9leGFtcGxlLmNvbS9lbmRwb2ludCIsInIiOlsiZGlkOmV4YW1wbGU6c29tZW1lZGlhdG9yI3NvbWVrZXkiXSwiYSI6WyJkaWRjb21tL3YyIiwiZGlkY29tbS9haXAyO2Vudj1yZmM1ODciXX0", 196 | "publicKeyJwk": { 197 | "kty": "OKP", 198 | "crv": "Ed25519", 199 | "x": "Itv8B__b1-Jos3LCpUe8EdTFGTCa_Dza6_3848P3R70" 200 | } 201 | } 202 | ], 203 | "authentication": ["#6MkqRYqQ", "#6MkgoLTn"], 204 | "assertionMethod": ["#6MkqRYqQ", "#6MkgoLTn"], 205 | "keyAgreement": ["#6LSbysY2"], 206 | "capabilityInvocation": ["#6MkqRYqQ", "#6MkgoLTn"], 207 | "capabilityDelegation": ["#6MkqRYqQ", "#6MkgoLTn"], 208 | "service": [ 209 | { 210 | "id": "#didcommmessaging-0", 211 | "type": "DIDCommMessaging", 212 | "serviceEndpoint": "https://example.com/endpoint", 213 | "accept": ["didcomm/v2", "didcomm/aip2;env=rfc587"], 214 | "routingKeys": ["did:example:somemediator#somekey"] 215 | } 216 | ] 217 | } 218 | """ 219 | 220 | PEER_DID_NUMALGO_2_2_SERVICES = ( 221 | "did:peer:2" 222 | + ".Ez6LSpSrLxbAhg2SHwKk7kwpsH7DM7QjFS5iK6qP87eViohud" 223 | + ".Vz6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V" 224 | + ".SW3sidCI6ImRtIiwicyI6Imh0dHBzOi8vZXhhbXBsZS5jb20vZW5kcG9pbnQiLCJyIjpbImRpZDpleGFtcGxlOnNvbWVtZWRpYXRvciNzb21la2V5Il19LHsidCI6ImV4YW1wbGUiLCJzIjoiaHR0cHM6Ly9leGFtcGxlLmNvbS9lbmRwb2ludDIiLCJyIjpbImRpZDpleGFtcGxlOnNvbWVtZWRpYXRvciNzb21la2V5MiJdLCJhIjpbImRpZGNvbW0vdjIiLCJkaWRjb21tL2FpcDI7ZW52PXJmYzU4NyJdfV0" 225 | ) 226 | 227 | DID_DOC_NUMALGO_2_MULTIBASE_2_SERVICES = """ 228 | { 229 | "@context": [ 230 | "https://www.w3.org/ns/did/v1", 231 | "https://w3id.org/security/suites/x25519-2020/v1", 232 | "https://w3id.org/security/suites/ed25519-2020/v1" 233 | ], 234 | "id": "did:peer:2.Ez6LSpSrLxbAhg2SHwKk7kwpsH7DM7QjFS5iK6qP87eViohud.Vz6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V.SW3sidCI6ImRtIiwicyI6Imh0dHBzOi8vZXhhbXBsZS5jb20vZW5kcG9pbnQiLCJyIjpbImRpZDpleGFtcGxlOnNvbWVtZWRpYXRvciNzb21la2V5Il19LHsidCI6ImV4YW1wbGUiLCJzIjoiaHR0cHM6Ly9leGFtcGxlLmNvbS9lbmRwb2ludDIiLCJyIjpbImRpZDpleGFtcGxlOnNvbWVtZWRpYXRvciNzb21la2V5MiJdLCJhIjpbImRpZGNvbW0vdjIiLCJkaWRjb21tL2FpcDI7ZW52PXJmYzU4NyJdfV0", 235 | "verificationMethod": [ 236 | { 237 | "id": "#6LSpSrLx", 238 | "type": "X25519KeyAgreementKey2020", 239 | "controller": "did:peer:2.Ez6LSpSrLxbAhg2SHwKk7kwpsH7DM7QjFS5iK6qP87eViohud.Vz6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V.SW3sidCI6ImRtIiwicyI6Imh0dHBzOi8vZXhhbXBsZS5jb20vZW5kcG9pbnQiLCJyIjpbImRpZDpleGFtcGxlOnNvbWVtZWRpYXRvciNzb21la2V5Il19LHsidCI6ImV4YW1wbGUiLCJzIjoiaHR0cHM6Ly9leGFtcGxlLmNvbS9lbmRwb2ludDIiLCJyIjpbImRpZDpleGFtcGxlOnNvbWVtZWRpYXRvciNzb21la2V5MiJdLCJhIjpbImRpZGNvbW0vdjIiLCJkaWRjb21tL2FpcDI7ZW52PXJmYzU4NyJdfV0", 240 | "publicKeyMultibase": "z6LSpSrLxbAhg2SHwKk7kwpsH7DM7QjFS5iK6qP87eViohud" 241 | }, 242 | { 243 | "id": "#6MkqRYqQ", 244 | "type": "Ed25519VerificationKey2020", 245 | "controller": "did:peer:2.Ez6LSpSrLxbAhg2SHwKk7kwpsH7DM7QjFS5iK6qP87eViohud.Vz6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V.SW3sidCI6ImRtIiwicyI6Imh0dHBzOi8vZXhhbXBsZS5jb20vZW5kcG9pbnQiLCJyIjpbImRpZDpleGFtcGxlOnNvbWVtZWRpYXRvciNzb21la2V5Il19LHsidCI6ImV4YW1wbGUiLCJzIjoiaHR0cHM6Ly9leGFtcGxlLmNvbS9lbmRwb2ludDIiLCJyIjpbImRpZDpleGFtcGxlOnNvbWVtZWRpYXRvciNzb21la2V5MiJdLCJhIjpbImRpZGNvbW0vdjIiLCJkaWRjb21tL2FpcDI7ZW52PXJmYzU4NyJdfV0", 246 | "publicKeyMultibase": "z6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V" 247 | } 248 | ], 249 | "authentication": ["#6MkqRYqQ"], 250 | "assertionMethod": ["#6MkqRYqQ"], 251 | "keyAgreement": ["#6LSpSrLx"], 252 | "capabilityInvocation": ["#6MkqRYqQ"], 253 | "capabilityDelegation": ["#6MkqRYqQ"], 254 | "service": [ 255 | { 256 | "id": "#didcommmessaging-0", 257 | "type": "DIDCommMessaging", 258 | "serviceEndpoint": "https://example.com/endpoint", 259 | "routingKeys": ["did:example:somemediator#somekey"] 260 | }, 261 | { 262 | "id": "#example-1", 263 | "type": "example", 264 | "serviceEndpoint": "https://example.com/endpoint2", 265 | "accept": ["didcomm/v2", "didcomm/aip2;env=rfc587"], 266 | "routingKeys": ["did:example:somemediator#somekey2"] 267 | } 268 | ] 269 | } 270 | """ 271 | 272 | PEER_DID_NUMALGO_2_NO_SERVICES = ( 273 | "did:peer:2" 274 | + ".Ez6LSpSrLxbAhg2SHwKk7kwpsH7DM7QjFS5iK6qP87eViohud" 275 | + ".Vz6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V" 276 | ) 277 | 278 | DID_DOC_NUMALGO_2_MULTIBASE_NO_SERVICES = """ 279 | { 280 | "@context": [ 281 | "https://www.w3.org/ns/did/v1", 282 | "https://w3id.org/security/suites/x25519-2020/v1", 283 | "https://w3id.org/security/suites/ed25519-2020/v1" 284 | ], 285 | "id": "did:peer:2.Ez6LSpSrLxbAhg2SHwKk7kwpsH7DM7QjFS5iK6qP87eViohud.Vz6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V", 286 | "verificationMethod": [ 287 | { 288 | "id": "#6LSpSrLx", 289 | "type": "X25519KeyAgreementKey2020", 290 | "controller": "did:peer:2.Ez6LSpSrLxbAhg2SHwKk7kwpsH7DM7QjFS5iK6qP87eViohud.Vz6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V", 291 | "publicKeyMultibase": "z6LSpSrLxbAhg2SHwKk7kwpsH7DM7QjFS5iK6qP87eViohud" 292 | }, 293 | { 294 | "id": "#6MkqRYqQ", 295 | "type": "Ed25519VerificationKey2020", 296 | "controller": "did:peer:2.Ez6LSpSrLxbAhg2SHwKk7kwpsH7DM7QjFS5iK6qP87eViohud.Vz6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V", 297 | "publicKeyMultibase": "z6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V" 298 | } 299 | ], 300 | "authentication": ["#6MkqRYqQ"], 301 | "assertionMethod": ["#6MkqRYqQ"], 302 | "keyAgreement": ["#6LSpSrLx"], 303 | "capabilityInvocation": ["#6MkqRYqQ"], 304 | "capabilityDelegation": ["#6MkqRYqQ"] 305 | } 306 | """ 307 | 308 | PEER_DID_NUMALGO_2_MINIMAL_SERVICES = "did:peer:2.Ez6LSbysY2xFMRpGMhb7tFTLMpeuPRaqaWM1yECx2AtzE3KCc.Vz6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V.Vz6MkgoLTnTypo3tDRwCkZXSccTPHRLhF4ZnjhueYAFpEX6vg.SeyJ0IjoiZG0iLCJzIjoiaHR0cHM6Ly9leGFtcGxlLmNvbS9lbmRwb2ludCJ9" 309 | 310 | DID_DOC_NUMALGO_2_MULTIBASE_MINIMAL_SERVICES = """ 311 | { 312 | "@context": [ 313 | "https://www.w3.org/ns/did/v1", 314 | "https://w3id.org/security/suites/x25519-2020/v1", 315 | "https://w3id.org/security/suites/ed25519-2020/v1" 316 | ], 317 | "id": "did:peer:2.Ez6LSbysY2xFMRpGMhb7tFTLMpeuPRaqaWM1yECx2AtzE3KCc.Vz6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V.Vz6MkgoLTnTypo3tDRwCkZXSccTPHRLhF4ZnjhueYAFpEX6vg.SeyJ0IjoiZG0iLCJzIjoiaHR0cHM6Ly9leGFtcGxlLmNvbS9lbmRwb2ludCJ9", 318 | "verificationMethod": [ 319 | { 320 | "id": "#6LSbysY2", 321 | "type": "X25519KeyAgreementKey2020", 322 | "controller": "did:peer:2.Ez6LSbysY2xFMRpGMhb7tFTLMpeuPRaqaWM1yECx2AtzE3KCc.Vz6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V.Vz6MkgoLTnTypo3tDRwCkZXSccTPHRLhF4ZnjhueYAFpEX6vg.SeyJ0IjoiZG0iLCJzIjoiaHR0cHM6Ly9leGFtcGxlLmNvbS9lbmRwb2ludCJ9", 323 | "publicKeyMultibase": "z6LSbysY2xFMRpGMhb7tFTLMpeuPRaqaWM1yECx2AtzE3KCc" 324 | }, 325 | { 326 | "id": "#6MkqRYqQ", 327 | "type": "Ed25519VerificationKey2020", 328 | "controller": "did:peer:2.Ez6LSbysY2xFMRpGMhb7tFTLMpeuPRaqaWM1yECx2AtzE3KCc.Vz6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V.Vz6MkgoLTnTypo3tDRwCkZXSccTPHRLhF4ZnjhueYAFpEX6vg.SeyJ0IjoiZG0iLCJzIjoiaHR0cHM6Ly9leGFtcGxlLmNvbS9lbmRwb2ludCJ9", 329 | "publicKeyMultibase": "z6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V" 330 | }, 331 | { 332 | "id": "#6MkgoLTn", 333 | "type": "Ed25519VerificationKey2020", 334 | "controller": "did:peer:2.Ez6LSbysY2xFMRpGMhb7tFTLMpeuPRaqaWM1yECx2AtzE3KCc.Vz6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V.Vz6MkgoLTnTypo3tDRwCkZXSccTPHRLhF4ZnjhueYAFpEX6vg.SeyJ0IjoiZG0iLCJzIjoiaHR0cHM6Ly9leGFtcGxlLmNvbS9lbmRwb2ludCJ9", 335 | "publicKeyMultibase": "z6MkgoLTnTypo3tDRwCkZXSccTPHRLhF4ZnjhueYAFpEX6vg" 336 | } 337 | ], 338 | "authentication": ["#6MkqRYqQ", "#6MkgoLTn"], 339 | "assertionMethod": ["#6MkqRYqQ", "#6MkgoLTn"], 340 | "keyAgreement": ["#6LSbysY2"], 341 | "capabilityInvocation": ["#6MkqRYqQ", "#6MkgoLTn"], 342 | "capabilityDelegation": ["#6MkqRYqQ", "#6MkgoLTn"], 343 | "service": [ 344 | { 345 | "id": "#didcommmessaging-0", 346 | "type": "DIDCommMessaging", 347 | "serviceEndpoint": "https://example.com/endpoint" 348 | } 349 | ] 350 | } 351 | """ 352 | -------------------------------------------------------------------------------- /tox.ini: -------------------------------------------------------------------------------- 1 | [tox] 2 | envlist = 3 | py{311,310,39,38} 4 | skip_missing_interpreters = true 5 | 6 | [testenv] 7 | deps = 8 | .[tests] 9 | commands = pytest 10 | 11 | [pytest] 12 | # tells pytest-mock module use pypi backport of unittest.mock 13 | mock_use_standalone_module = true 14 | addopts = -n auto 15 | 16 | [flake8] 17 | # set the same as 'black' uses 18 | max-line-length = 88 19 | # TODO mention the reasons why we ignore the following 20 | # E203 slice notation whitespace 21 | # E501 line length 22 | # W503 line break before binary operator 23 | # W504 line break after binary operator 24 | # F841 local variable name is assigned to but never used 25 | ignore = E203,E501,W503,W504,F841 26 | --------------------------------------------------------------------------------