open-agent-id 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- open_agent_id-0.1.0/.gitignore +7 -0
- open_agent_id-0.1.0/LICENSE +201 -0
- open_agent_id-0.1.0/PKG-INFO +103 -0
- open_agent_id-0.1.0/README.md +82 -0
- open_agent_id-0.1.0/agent_id/__init__.py +9 -0
- open_agent_id-0.1.0/agent_id/cache.py +39 -0
- open_agent_id-0.1.0/agent_id/client.py +98 -0
- open_agent_id-0.1.0/agent_id/crypto.py +94 -0
- open_agent_id-0.1.0/agent_id/did.py +58 -0
- open_agent_id-0.1.0/agent_id/identity.py +215 -0
- open_agent_id-0.1.0/pyproject.toml +38 -0
- open_agent_id-0.1.0/tests/__init__.py +0 -0
- open_agent_id-0.1.0/tests/test_crypto.py +63 -0
- open_agent_id-0.1.0/tests/test_did.py +79 -0
- open_agent_id-0.1.0/tests/test_identity.py +109 -0
- open_agent_id-0.1.0/tests/test_signing.py +92 -0
|
@@ -0,0 +1,201 @@
|
|
|
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.
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: open-agent-id
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Python SDK for Open Agent ID — register, sign, and verify AI agent identities
|
|
5
|
+
Project-URL: Homepage, https://openagentid.org
|
|
6
|
+
Project-URL: Repository, https://github.com/open-agent-id/agent-id-python
|
|
7
|
+
License-Expression: Apache-2.0
|
|
8
|
+
License-File: LICENSE
|
|
9
|
+
Keywords: agent,ai-agent,did,ed25519,identity,open-agent-id,verification
|
|
10
|
+
Classifier: Development Status :: 3 - Alpha
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Classifier: Topic :: Security :: Cryptography
|
|
14
|
+
Requires-Python: >=3.10
|
|
15
|
+
Requires-Dist: cryptography>=41.0
|
|
16
|
+
Requires-Dist: httpx>=0.25
|
|
17
|
+
Provides-Extra: dev
|
|
18
|
+
Requires-Dist: pytest-asyncio>=0.21; extra == 'dev'
|
|
19
|
+
Requires-Dist: pytest>=7.0; extra == 'dev'
|
|
20
|
+
Description-Content-Type: text/markdown
|
|
21
|
+
|
|
22
|
+
# open-agent-id
|
|
23
|
+
|
|
24
|
+
Python SDK for [Open Agent ID](../protocol/) -- register, sign, and verify AI agent identities.
|
|
25
|
+
|
|
26
|
+
## Installation
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
pip install open-agent-id
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
Or for development:
|
|
33
|
+
|
|
34
|
+
```bash
|
|
35
|
+
pip install -e ".[dev]"
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## Quick start
|
|
39
|
+
|
|
40
|
+
### Register a new agent
|
|
41
|
+
|
|
42
|
+
```python
|
|
43
|
+
import asyncio
|
|
44
|
+
from agent_id import AgentIdentity
|
|
45
|
+
|
|
46
|
+
async def main():
|
|
47
|
+
identity = await AgentIdentity.register(
|
|
48
|
+
name="my-search-agent",
|
|
49
|
+
capabilities=["search", "summarize"],
|
|
50
|
+
api_key="your-platform-key",
|
|
51
|
+
)
|
|
52
|
+
print(identity.did) # did:agent:tokli:agt_...
|
|
53
|
+
print(identity.public_key_base64url)
|
|
54
|
+
# IMPORTANT: persist the private key securely -- it is only returned once.
|
|
55
|
+
|
|
56
|
+
asyncio.run(main())
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
### Load an existing identity
|
|
60
|
+
|
|
61
|
+
```python
|
|
62
|
+
from agent_id import AgentIdentity
|
|
63
|
+
|
|
64
|
+
identity = AgentIdentity.load(
|
|
65
|
+
did="did:agent:tokli:agt_a1B2c3D4e5",
|
|
66
|
+
private_key="<base64url-encoded-private-key>",
|
|
67
|
+
)
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
### Sign an HTTP request
|
|
71
|
+
|
|
72
|
+
```python
|
|
73
|
+
headers = identity.sign_request("POST", "https://api.example.com/v1/tasks", '{"task":"search"}')
|
|
74
|
+
# headers contains X-Agent-DID, X-Agent-Timestamp, X-Agent-Nonce, X-Agent-Signature
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
### Verify another agent's signature
|
|
78
|
+
|
|
79
|
+
```python
|
|
80
|
+
valid = await AgentIdentity.verify(
|
|
81
|
+
did="did:agent:other:agt_X9yZ8wV7u6",
|
|
82
|
+
payload=canonical_payload,
|
|
83
|
+
signature=signature_b64,
|
|
84
|
+
)
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
### Look up an agent
|
|
88
|
+
|
|
89
|
+
```python
|
|
90
|
+
info = await AgentIdentity.lookup("did:agent:tokli:agt_a1B2c3D4e5")
|
|
91
|
+
print(info["name"], info["status"])
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
## Running tests
|
|
95
|
+
|
|
96
|
+
```bash
|
|
97
|
+
pip install -e ".[dev]"
|
|
98
|
+
pytest
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
## License
|
|
102
|
+
|
|
103
|
+
Apache 2.0 -- see [LICENSE](LICENSE).
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
# open-agent-id
|
|
2
|
+
|
|
3
|
+
Python SDK for [Open Agent ID](../protocol/) -- register, sign, and verify AI agent identities.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pip install open-agent-id
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
Or for development:
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
pip install -e ".[dev]"
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Quick start
|
|
18
|
+
|
|
19
|
+
### Register a new agent
|
|
20
|
+
|
|
21
|
+
```python
|
|
22
|
+
import asyncio
|
|
23
|
+
from agent_id import AgentIdentity
|
|
24
|
+
|
|
25
|
+
async def main():
|
|
26
|
+
identity = await AgentIdentity.register(
|
|
27
|
+
name="my-search-agent",
|
|
28
|
+
capabilities=["search", "summarize"],
|
|
29
|
+
api_key="your-platform-key",
|
|
30
|
+
)
|
|
31
|
+
print(identity.did) # did:agent:tokli:agt_...
|
|
32
|
+
print(identity.public_key_base64url)
|
|
33
|
+
# IMPORTANT: persist the private key securely -- it is only returned once.
|
|
34
|
+
|
|
35
|
+
asyncio.run(main())
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
### Load an existing identity
|
|
39
|
+
|
|
40
|
+
```python
|
|
41
|
+
from agent_id import AgentIdentity
|
|
42
|
+
|
|
43
|
+
identity = AgentIdentity.load(
|
|
44
|
+
did="did:agent:tokli:agt_a1B2c3D4e5",
|
|
45
|
+
private_key="<base64url-encoded-private-key>",
|
|
46
|
+
)
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
### Sign an HTTP request
|
|
50
|
+
|
|
51
|
+
```python
|
|
52
|
+
headers = identity.sign_request("POST", "https://api.example.com/v1/tasks", '{"task":"search"}')
|
|
53
|
+
# headers contains X-Agent-DID, X-Agent-Timestamp, X-Agent-Nonce, X-Agent-Signature
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
### Verify another agent's signature
|
|
57
|
+
|
|
58
|
+
```python
|
|
59
|
+
valid = await AgentIdentity.verify(
|
|
60
|
+
did="did:agent:other:agt_X9yZ8wV7u6",
|
|
61
|
+
payload=canonical_payload,
|
|
62
|
+
signature=signature_b64,
|
|
63
|
+
)
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
### Look up an agent
|
|
67
|
+
|
|
68
|
+
```python
|
|
69
|
+
info = await AgentIdentity.lookup("did:agent:tokli:agt_a1B2c3D4e5")
|
|
70
|
+
print(info["name"], info["status"])
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
## Running tests
|
|
74
|
+
|
|
75
|
+
```bash
|
|
76
|
+
pip install -e ".[dev]"
|
|
77
|
+
pytest
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
## License
|
|
81
|
+
|
|
82
|
+
Apache 2.0 -- see [LICENSE](LICENSE).
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"""Simple in-memory TTL cache for public keys."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import time
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class PublicKeyCache:
|
|
9
|
+
"""Thread-safe in-memory cache with time-to-live for public keys."""
|
|
10
|
+
|
|
11
|
+
def __init__(self, ttl: int = 3600) -> None:
|
|
12
|
+
self._ttl = ttl
|
|
13
|
+
self._store: dict[str, tuple[bytes, float]] = {}
|
|
14
|
+
|
|
15
|
+
def get(self, did: str) -> bytes | None:
|
|
16
|
+
"""Get a cached public key for a DID.
|
|
17
|
+
|
|
18
|
+
Returns the public key bytes, or None if not found or expired.
|
|
19
|
+
"""
|
|
20
|
+
entry = self._store.get(did)
|
|
21
|
+
if entry is None:
|
|
22
|
+
return None
|
|
23
|
+
public_key, stored_at = entry
|
|
24
|
+
if time.time() - stored_at > self._ttl:
|
|
25
|
+
del self._store[did]
|
|
26
|
+
return None
|
|
27
|
+
return public_key
|
|
28
|
+
|
|
29
|
+
def set(self, did: str, public_key: bytes) -> None:
|
|
30
|
+
"""Cache a public key for a DID."""
|
|
31
|
+
self._store[did] = (public_key, time.time())
|
|
32
|
+
|
|
33
|
+
def invalidate(self, did: str) -> None:
|
|
34
|
+
"""Remove a DID from the cache."""
|
|
35
|
+
self._store.pop(did, None)
|
|
36
|
+
|
|
37
|
+
def clear(self) -> None:
|
|
38
|
+
"""Clear all cached entries."""
|
|
39
|
+
self._store.clear()
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
"""Async HTTP client for the Open Agent ID Registry API."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import httpx
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
async def register_agent(
|
|
9
|
+
name: str,
|
|
10
|
+
capabilities: list[str] | None = None,
|
|
11
|
+
api_url: str = "https://api.openagentid.org",
|
|
12
|
+
api_key: str | None = None,
|
|
13
|
+
) -> dict:
|
|
14
|
+
"""Register a new agent identity.
|
|
15
|
+
|
|
16
|
+
Args:
|
|
17
|
+
name: Agent name (max 100 characters).
|
|
18
|
+
capabilities: Optional list of capability strings.
|
|
19
|
+
api_url: Base URL for the registry API.
|
|
20
|
+
api_key: Platform API key for authentication.
|
|
21
|
+
|
|
22
|
+
Returns:
|
|
23
|
+
Dict with keys: did, public_key, private_key, chain_status, created_at.
|
|
24
|
+
|
|
25
|
+
Raises:
|
|
26
|
+
httpx.HTTPStatusError: On non-2xx responses.
|
|
27
|
+
"""
|
|
28
|
+
headers: dict[str, str] = {}
|
|
29
|
+
if api_key:
|
|
30
|
+
headers["X-Platform-Key"] = api_key
|
|
31
|
+
|
|
32
|
+
body: dict = {"name": name}
|
|
33
|
+
if capabilities is not None:
|
|
34
|
+
body["capabilities"] = capabilities
|
|
35
|
+
|
|
36
|
+
async with httpx.AsyncClient() as client:
|
|
37
|
+
resp = await client.post(
|
|
38
|
+
f"{api_url}/v1/agents",
|
|
39
|
+
json=body,
|
|
40
|
+
headers=headers,
|
|
41
|
+
)
|
|
42
|
+
resp.raise_for_status()
|
|
43
|
+
return resp.json()
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
async def get_agent(did: str, api_url: str = "https://api.openagentid.org") -> dict:
|
|
47
|
+
"""Look up an agent by DID.
|
|
48
|
+
|
|
49
|
+
Args:
|
|
50
|
+
did: The agent DID string.
|
|
51
|
+
api_url: Base URL for the registry API.
|
|
52
|
+
|
|
53
|
+
Returns:
|
|
54
|
+
Dict with agent info (did, name, public_key, capabilities, status, etc.).
|
|
55
|
+
|
|
56
|
+
Raises:
|
|
57
|
+
httpx.HTTPStatusError: On non-2xx responses.
|
|
58
|
+
"""
|
|
59
|
+
async with httpx.AsyncClient() as client:
|
|
60
|
+
resp = await client.get(f"{api_url}/v1/agents/{did}")
|
|
61
|
+
resp.raise_for_status()
|
|
62
|
+
return resp.json()
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
async def verify_signature(
|
|
66
|
+
did: str,
|
|
67
|
+
payload: str,
|
|
68
|
+
signature: str,
|
|
69
|
+
api_url: str = "https://api.openagentid.org",
|
|
70
|
+
) -> bool:
|
|
71
|
+
"""Verify a signature using the remote registry API.
|
|
72
|
+
|
|
73
|
+
This is a convenience for when local verification is not possible.
|
|
74
|
+
Prefer local verification via the SDK for better performance.
|
|
75
|
+
|
|
76
|
+
Args:
|
|
77
|
+
did: The signer's DID.
|
|
78
|
+
payload: The canonical payload that was signed.
|
|
79
|
+
signature: Base64url-encoded Ed25519 signature.
|
|
80
|
+
api_url: Base URL for the registry API.
|
|
81
|
+
|
|
82
|
+
Returns:
|
|
83
|
+
True if the signature is valid, False otherwise.
|
|
84
|
+
|
|
85
|
+
Raises:
|
|
86
|
+
httpx.HTTPStatusError: On non-2xx responses.
|
|
87
|
+
"""
|
|
88
|
+
async with httpx.AsyncClient() as client:
|
|
89
|
+
resp = await client.post(
|
|
90
|
+
f"{api_url}/v1/verify",
|
|
91
|
+
json={
|
|
92
|
+
"did": did,
|
|
93
|
+
"payload": payload,
|
|
94
|
+
"signature": signature,
|
|
95
|
+
},
|
|
96
|
+
)
|
|
97
|
+
resp.raise_for_status()
|
|
98
|
+
return resp.json().get("valid", False)
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
"""Ed25519 cryptography and encoding utilities."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import base64
|
|
6
|
+
import hashlib
|
|
7
|
+
import os
|
|
8
|
+
|
|
9
|
+
from cryptography.hazmat.primitives.asymmetric.ed25519 import (
|
|
10
|
+
Ed25519PrivateKey,
|
|
11
|
+
Ed25519PublicKey,
|
|
12
|
+
)
|
|
13
|
+
from cryptography.hazmat.primitives.asymmetric.types import PrivateKeyTypes
|
|
14
|
+
from cryptography.hazmat.primitives import serialization
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def generate_keypair() -> tuple[bytes, bytes]:
|
|
18
|
+
"""Generate an Ed25519 keypair.
|
|
19
|
+
|
|
20
|
+
Returns:
|
|
21
|
+
Tuple of (private_key_bytes, public_key_bytes) where private_key_bytes
|
|
22
|
+
is the 64-byte raw private key (seed + public) and public_key_bytes
|
|
23
|
+
is the 32-byte raw public key.
|
|
24
|
+
"""
|
|
25
|
+
private_key = Ed25519PrivateKey.generate()
|
|
26
|
+
raw_private = private_key.private_bytes(
|
|
27
|
+
serialization.Encoding.Raw,
|
|
28
|
+
serialization.PrivateFormat.Raw,
|
|
29
|
+
serialization.NoEncryption(),
|
|
30
|
+
)
|
|
31
|
+
raw_public = private_key.public_key().public_bytes(
|
|
32
|
+
serialization.Encoding.Raw,
|
|
33
|
+
serialization.PublicFormat.Raw,
|
|
34
|
+
)
|
|
35
|
+
# Return 64-byte private key (seed || public) as used by the spec
|
|
36
|
+
return raw_private + raw_public, raw_public
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def sign(payload: bytes, private_key: bytes) -> bytes:
|
|
40
|
+
"""Sign a payload with an Ed25519 private key.
|
|
41
|
+
|
|
42
|
+
Args:
|
|
43
|
+
payload: The bytes to sign.
|
|
44
|
+
private_key: 64-byte raw private key (seed || public) or 32-byte seed.
|
|
45
|
+
|
|
46
|
+
Returns:
|
|
47
|
+
64-byte Ed25519 signature.
|
|
48
|
+
"""
|
|
49
|
+
seed = private_key[:32]
|
|
50
|
+
key = Ed25519PrivateKey.from_private_bytes(seed)
|
|
51
|
+
return key.sign(payload)
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def verify(payload: bytes, signature: bytes, public_key: bytes) -> bool:
|
|
55
|
+
"""Verify an Ed25519 signature.
|
|
56
|
+
|
|
57
|
+
Args:
|
|
58
|
+
payload: The bytes that were signed.
|
|
59
|
+
signature: 64-byte Ed25519 signature.
|
|
60
|
+
public_key: 32-byte raw public key.
|
|
61
|
+
|
|
62
|
+
Returns:
|
|
63
|
+
True if valid, False otherwise.
|
|
64
|
+
"""
|
|
65
|
+
try:
|
|
66
|
+
key = Ed25519PublicKey.from_public_bytes(public_key)
|
|
67
|
+
key.verify(signature, payload)
|
|
68
|
+
return True
|
|
69
|
+
except Exception:
|
|
70
|
+
return False
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def sha256_hex(data: bytes) -> str:
|
|
74
|
+
"""Return the SHA-256 hex digest of data."""
|
|
75
|
+
return hashlib.sha256(data).hexdigest()
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def generate_nonce() -> str:
|
|
79
|
+
"""Generate a random 16-byte hex nonce."""
|
|
80
|
+
return os.urandom(16).hex()
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
def base64url_encode(data: bytes) -> str:
|
|
84
|
+
"""Encode bytes to base64url with no padding."""
|
|
85
|
+
return base64.urlsafe_b64encode(data).rstrip(b"=").decode("ascii")
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
def base64url_decode(s: str) -> bytes:
|
|
89
|
+
"""Decode a base64url string (with or without padding) to bytes."""
|
|
90
|
+
# Add back padding
|
|
91
|
+
padding = 4 - len(s) % 4
|
|
92
|
+
if padding != 4:
|
|
93
|
+
s += "=" * padding
|
|
94
|
+
return base64.urlsafe_b64decode(s)
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
"""DID parsing and validation for the Open Agent ID format.
|
|
2
|
+
|
|
3
|
+
DID format: did:agent:{platform}:{unique_id}
|
|
4
|
+
|
|
5
|
+
- platform: 3-20 lowercase alphanumeric characters [a-z0-9]
|
|
6
|
+
- unique_id: 'agt_' followed by exactly 10 base62 characters [0-9A-Za-z]
|
|
7
|
+
- Total DID length must not exceed 60 characters.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
import re
|
|
13
|
+
import secrets
|
|
14
|
+
import string
|
|
15
|
+
|
|
16
|
+
# Base62 character set: 0-9, A-Z, a-z
|
|
17
|
+
BASE62_CHARS = string.digits + string.ascii_uppercase + string.ascii_lowercase
|
|
18
|
+
|
|
19
|
+
# Pre-compiled regex for the full DID format
|
|
20
|
+
_DID_RE = re.compile(
|
|
21
|
+
r"^did:agent:([a-z0-9]{3,20}):(agt_[0-9A-Za-z]{10})$"
|
|
22
|
+
)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def validate_did(did: str) -> bool:
|
|
26
|
+
"""Validate a DID string against the Open Agent ID format spec.
|
|
27
|
+
|
|
28
|
+
Returns True if valid, False otherwise.
|
|
29
|
+
"""
|
|
30
|
+
if not did or len(did) > 60:
|
|
31
|
+
return False
|
|
32
|
+
return _DID_RE.match(did) is not None
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def parse_did(did: str) -> dict:
|
|
36
|
+
"""Parse a DID string and extract components.
|
|
37
|
+
|
|
38
|
+
Returns:
|
|
39
|
+
Dict with keys: 'method', 'platform', 'unique_id'.
|
|
40
|
+
|
|
41
|
+
Raises:
|
|
42
|
+
ValueError: If the DID is not valid.
|
|
43
|
+
"""
|
|
44
|
+
if not validate_did(did):
|
|
45
|
+
raise ValueError(f"Invalid DID: {did!r}")
|
|
46
|
+
m = _DID_RE.match(did)
|
|
47
|
+
assert m is not None
|
|
48
|
+
return {
|
|
49
|
+
"method": "agent",
|
|
50
|
+
"platform": m.group(1),
|
|
51
|
+
"unique_id": m.group(2),
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def generate_unique_id() -> str:
|
|
56
|
+
"""Generate a random unique ID in the format agt_ + 10 base62 chars."""
|
|
57
|
+
suffix = "".join(secrets.choice(BASE62_CHARS) for _ in range(10))
|
|
58
|
+
return f"agt_{suffix}"
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
"""Main AgentIdentity class for the Open Agent ID SDK."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import time
|
|
6
|
+
|
|
7
|
+
from . import crypto, client, did as did_module
|
|
8
|
+
from .cache import PublicKeyCache
|
|
9
|
+
|
|
10
|
+
# Module-level cache shared across all lookups
|
|
11
|
+
_key_cache = PublicKeyCache()
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class AgentIdentity:
|
|
15
|
+
"""Represents an AI agent's identity.
|
|
16
|
+
|
|
17
|
+
An AgentIdentity holds a DID, a public key, and optionally a private key.
|
|
18
|
+
When the private key is present the identity can sign payloads and HTTP
|
|
19
|
+
requests. Verification and lookup are available as static methods.
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
def __init__(
|
|
23
|
+
self,
|
|
24
|
+
did: str,
|
|
25
|
+
private_key: bytes | None,
|
|
26
|
+
public_key: bytes,
|
|
27
|
+
) -> None:
|
|
28
|
+
if not did_module.validate_did(did):
|
|
29
|
+
raise ValueError(f"Invalid DID: {did!r}")
|
|
30
|
+
self._did = did
|
|
31
|
+
self._private_key = private_key
|
|
32
|
+
self._public_key = public_key
|
|
33
|
+
|
|
34
|
+
# ------------------------------------------------------------------
|
|
35
|
+
# Constructors
|
|
36
|
+
# ------------------------------------------------------------------
|
|
37
|
+
|
|
38
|
+
@classmethod
|
|
39
|
+
async def register(
|
|
40
|
+
cls,
|
|
41
|
+
name: str,
|
|
42
|
+
capabilities: list[str] | None = None,
|
|
43
|
+
api_url: str = "https://api.openagentid.org",
|
|
44
|
+
api_key: str | None = None,
|
|
45
|
+
) -> "AgentIdentity":
|
|
46
|
+
"""Register a new agent identity with the registry.
|
|
47
|
+
|
|
48
|
+
The registry generates the keypair server-side. The private key is
|
|
49
|
+
returned only once in the registration response -- store it securely.
|
|
50
|
+
|
|
51
|
+
Args:
|
|
52
|
+
name: Human-readable agent name (max 100 chars).
|
|
53
|
+
capabilities: Optional list of capability strings.
|
|
54
|
+
api_url: Registry API base URL.
|
|
55
|
+
api_key: Platform API key for authentication.
|
|
56
|
+
|
|
57
|
+
Returns:
|
|
58
|
+
An AgentIdentity with the private key populated.
|
|
59
|
+
"""
|
|
60
|
+
data = await client.register_agent(
|
|
61
|
+
name=name,
|
|
62
|
+
capabilities=capabilities,
|
|
63
|
+
api_url=api_url,
|
|
64
|
+
api_key=api_key,
|
|
65
|
+
)
|
|
66
|
+
priv = crypto.base64url_decode(data["private_key"])
|
|
67
|
+
pub = crypto.base64url_decode(data["public_key"])
|
|
68
|
+
return cls(did=data["did"], private_key=priv, public_key=pub)
|
|
69
|
+
|
|
70
|
+
@classmethod
|
|
71
|
+
def load(cls, did: str, private_key: str) -> "AgentIdentity":
|
|
72
|
+
"""Load an existing identity from a DID and base64url-encoded private key.
|
|
73
|
+
|
|
74
|
+
Args:
|
|
75
|
+
did: The agent's DID string.
|
|
76
|
+
private_key: Base64url-encoded Ed25519 private key (seed or seed||pub).
|
|
77
|
+
|
|
78
|
+
Returns:
|
|
79
|
+
An AgentIdentity with the private key populated.
|
|
80
|
+
"""
|
|
81
|
+
priv_bytes = crypto.base64url_decode(private_key)
|
|
82
|
+
# Derive the public key from the seed (first 32 bytes)
|
|
83
|
+
from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PrivateKey
|
|
84
|
+
from cryptography.hazmat.primitives import serialization
|
|
85
|
+
|
|
86
|
+
key = Ed25519PrivateKey.from_private_bytes(priv_bytes[:32])
|
|
87
|
+
pub_bytes = key.public_key().public_bytes(
|
|
88
|
+
serialization.Encoding.Raw,
|
|
89
|
+
serialization.PublicFormat.Raw,
|
|
90
|
+
)
|
|
91
|
+
# Store full 64-byte private key (seed || public)
|
|
92
|
+
full_priv = priv_bytes[:32] + pub_bytes
|
|
93
|
+
return cls(did=did, private_key=full_priv, public_key=pub_bytes)
|
|
94
|
+
|
|
95
|
+
# ------------------------------------------------------------------
|
|
96
|
+
# Properties
|
|
97
|
+
# ------------------------------------------------------------------
|
|
98
|
+
|
|
99
|
+
@property
|
|
100
|
+
def did(self) -> str:
|
|
101
|
+
"""The agent's DID string."""
|
|
102
|
+
return self._did
|
|
103
|
+
|
|
104
|
+
@property
|
|
105
|
+
def public_key_base64url(self) -> str:
|
|
106
|
+
"""The agent's public key encoded as base64url (no padding)."""
|
|
107
|
+
return crypto.base64url_encode(self._public_key)
|
|
108
|
+
|
|
109
|
+
# ------------------------------------------------------------------
|
|
110
|
+
# Signing
|
|
111
|
+
# ------------------------------------------------------------------
|
|
112
|
+
|
|
113
|
+
def sign(self, payload: str) -> str:
|
|
114
|
+
"""Sign a payload string with this identity's private key.
|
|
115
|
+
|
|
116
|
+
Args:
|
|
117
|
+
payload: Arbitrary string to sign.
|
|
118
|
+
|
|
119
|
+
Returns:
|
|
120
|
+
Base64url-encoded Ed25519 signature.
|
|
121
|
+
|
|
122
|
+
Raises:
|
|
123
|
+
RuntimeError: If this identity has no private key.
|
|
124
|
+
"""
|
|
125
|
+
if self._private_key is None:
|
|
126
|
+
raise RuntimeError("Cannot sign: no private key available")
|
|
127
|
+
sig = crypto.sign(payload.encode("utf-8"), self._private_key)
|
|
128
|
+
return crypto.base64url_encode(sig)
|
|
129
|
+
|
|
130
|
+
def sign_request(
|
|
131
|
+
self,
|
|
132
|
+
method: str,
|
|
133
|
+
url: str,
|
|
134
|
+
body: str = "",
|
|
135
|
+
) -> dict[str, str]:
|
|
136
|
+
"""Sign an HTTP request and return the headers to attach.
|
|
137
|
+
|
|
138
|
+
Constructs the canonical payload per the signing spec:
|
|
139
|
+
{METHOD}\\n{url}\\n{body_sha256}\\n{timestamp}\\n{nonce}
|
|
140
|
+
|
|
141
|
+
Args:
|
|
142
|
+
method: HTTP method (will be upper-cased).
|
|
143
|
+
url: Full request URL including query parameters.
|
|
144
|
+
body: Request body string (empty string for GET, etc.).
|
|
145
|
+
|
|
146
|
+
Returns:
|
|
147
|
+
Dict of HTTP headers to add to the request:
|
|
148
|
+
X-Agent-DID, X-Agent-Timestamp, X-Agent-Nonce, X-Agent-Signature.
|
|
149
|
+
"""
|
|
150
|
+
timestamp = str(int(time.time()))
|
|
151
|
+
nonce = crypto.generate_nonce()
|
|
152
|
+
body_hash = crypto.sha256_hex(body.encode("utf-8"))
|
|
153
|
+
canonical = f"{method.upper()}\n{url}\n{body_hash}\n{timestamp}\n{nonce}"
|
|
154
|
+
signature = self.sign(canonical)
|
|
155
|
+
return {
|
|
156
|
+
"X-Agent-DID": self._did,
|
|
157
|
+
"X-Agent-Timestamp": timestamp,
|
|
158
|
+
"X-Agent-Nonce": nonce,
|
|
159
|
+
"X-Agent-Signature": signature,
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
# ------------------------------------------------------------------
|
|
163
|
+
# Verification & lookup (static / async)
|
|
164
|
+
# ------------------------------------------------------------------
|
|
165
|
+
|
|
166
|
+
@staticmethod
|
|
167
|
+
async def verify(
|
|
168
|
+
did: str,
|
|
169
|
+
payload: str,
|
|
170
|
+
signature: str,
|
|
171
|
+
api_url: str = "https://api.openagentid.org",
|
|
172
|
+
) -> bool:
|
|
173
|
+
"""Verify a signature from another agent.
|
|
174
|
+
|
|
175
|
+
Resolves the public key from cache or the registry API, then verifies
|
|
176
|
+
the Ed25519 signature locally.
|
|
177
|
+
|
|
178
|
+
Args:
|
|
179
|
+
did: The signer's DID.
|
|
180
|
+
payload: The canonical payload that was signed.
|
|
181
|
+
signature: Base64url-encoded Ed25519 signature.
|
|
182
|
+
api_url: Registry API base URL.
|
|
183
|
+
|
|
184
|
+
Returns:
|
|
185
|
+
True if the signature is valid, False otherwise.
|
|
186
|
+
"""
|
|
187
|
+
# Resolve public key: cache first, then API
|
|
188
|
+
pub_bytes = _key_cache.get(did)
|
|
189
|
+
if pub_bytes is None:
|
|
190
|
+
info = await client.get_agent(did, api_url=api_url)
|
|
191
|
+
pub_bytes = crypto.base64url_decode(info["public_key"])
|
|
192
|
+
_key_cache.set(did, pub_bytes)
|
|
193
|
+
|
|
194
|
+
sig_bytes = crypto.base64url_decode(signature)
|
|
195
|
+
return crypto.verify(
|
|
196
|
+
payload.encode("utf-8"),
|
|
197
|
+
sig_bytes,
|
|
198
|
+
pub_bytes,
|
|
199
|
+
)
|
|
200
|
+
|
|
201
|
+
@staticmethod
|
|
202
|
+
async def lookup(
|
|
203
|
+
did: str,
|
|
204
|
+
api_url: str = "https://api.openagentid.org",
|
|
205
|
+
) -> dict:
|
|
206
|
+
"""Look up agent info by DID.
|
|
207
|
+
|
|
208
|
+
Args:
|
|
209
|
+
did: The agent's DID string.
|
|
210
|
+
api_url: Registry API base URL.
|
|
211
|
+
|
|
212
|
+
Returns:
|
|
213
|
+
Dict with agent info from the registry.
|
|
214
|
+
"""
|
|
215
|
+
return await client.get_agent(did, api_url=api_url)
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "open-agent-id"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Python SDK for Open Agent ID — register, sign, and verify AI agent identities"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = "Apache-2.0"
|
|
11
|
+
requires-python = ">=3.10"
|
|
12
|
+
keywords = ["agent", "identity", "did", "ed25519", "verification", "ai-agent", "open-agent-id"]
|
|
13
|
+
classifiers = [
|
|
14
|
+
"Development Status :: 3 - Alpha",
|
|
15
|
+
"Intended Audience :: Developers",
|
|
16
|
+
"Topic :: Security :: Cryptography",
|
|
17
|
+
"Programming Language :: Python :: 3",
|
|
18
|
+
]
|
|
19
|
+
dependencies = [
|
|
20
|
+
"cryptography>=41.0",
|
|
21
|
+
"httpx>=0.25",
|
|
22
|
+
]
|
|
23
|
+
|
|
24
|
+
[project.optional-dependencies]
|
|
25
|
+
dev = [
|
|
26
|
+
"pytest>=7.0",
|
|
27
|
+
"pytest-asyncio>=0.21",
|
|
28
|
+
]
|
|
29
|
+
|
|
30
|
+
[project.urls]
|
|
31
|
+
Homepage = "https://openagentid.org"
|
|
32
|
+
Repository = "https://github.com/open-agent-id/agent-id-python"
|
|
33
|
+
|
|
34
|
+
[tool.hatch.build.targets.wheel]
|
|
35
|
+
packages = ["agent_id"]
|
|
36
|
+
|
|
37
|
+
[tool.pytest.ini_options]
|
|
38
|
+
asyncio_mode = "auto"
|
|
File without changes
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
"""Tests for the crypto module."""
|
|
2
|
+
|
|
3
|
+
from agent_id.crypto import (
|
|
4
|
+
generate_keypair,
|
|
5
|
+
sign,
|
|
6
|
+
verify,
|
|
7
|
+
sha256_hex,
|
|
8
|
+
base64url_encode,
|
|
9
|
+
base64url_decode,
|
|
10
|
+
generate_nonce,
|
|
11
|
+
)
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def test_generate_keypair_sizes() -> None:
|
|
15
|
+
priv, pub = generate_keypair()
|
|
16
|
+
assert len(priv) == 64 # seed (32) + public (32)
|
|
17
|
+
assert len(pub) == 32
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def test_sign_verify_roundtrip() -> None:
|
|
21
|
+
priv, pub = generate_keypair()
|
|
22
|
+
message = b"hello world"
|
|
23
|
+
sig = sign(message, priv)
|
|
24
|
+
assert len(sig) == 64
|
|
25
|
+
assert verify(message, sig, pub) is True
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def test_verify_wrong_message() -> None:
|
|
29
|
+
priv, pub = generate_keypair()
|
|
30
|
+
sig = sign(b"hello", priv)
|
|
31
|
+
assert verify(b"world", sig, pub) is False
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def test_verify_wrong_key() -> None:
|
|
35
|
+
priv1, _ = generate_keypair()
|
|
36
|
+
_, pub2 = generate_keypair()
|
|
37
|
+
sig = sign(b"hello", priv1)
|
|
38
|
+
assert verify(b"hello", sig, pub2) is False
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def test_sha256_hex_empty() -> None:
|
|
42
|
+
# SHA-256 of empty string is a well-known constant
|
|
43
|
+
assert sha256_hex(b"") == "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def test_sha256_hex_nonempty() -> None:
|
|
47
|
+
# Known SHA-256 value for a simple string
|
|
48
|
+
assert sha256_hex(b"test") == "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08"
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def test_base64url_roundtrip() -> None:
|
|
52
|
+
data = b"\x00\xff\x80\x01"
|
|
53
|
+
encoded = base64url_encode(data)
|
|
54
|
+
assert "+" not in encoded
|
|
55
|
+
assert "/" not in encoded
|
|
56
|
+
assert "=" not in encoded
|
|
57
|
+
assert base64url_decode(encoded) == data
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def test_generate_nonce_length() -> None:
|
|
61
|
+
nonce = generate_nonce()
|
|
62
|
+
assert len(nonce) == 32 # 16 bytes = 32 hex chars
|
|
63
|
+
int(nonce, 16) # should be valid hex
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
"""Tests for DID validation and parsing."""
|
|
2
|
+
|
|
3
|
+
import pytest
|
|
4
|
+
|
|
5
|
+
from agent_id.did import validate_did, parse_did, generate_unique_id
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
# ---------------------------------------------------------------------------
|
|
9
|
+
# Validation — valid DIDs (from test vectors)
|
|
10
|
+
# ---------------------------------------------------------------------------
|
|
11
|
+
|
|
12
|
+
VALID_DIDS = [
|
|
13
|
+
"did:agent:tokli:agt_a1B2c3D4e5",
|
|
14
|
+
"did:agent:openai:agt_X9yZ8wV7u6",
|
|
15
|
+
"did:agent:langchain:agt_Q3rS4tU5v6",
|
|
16
|
+
"did:agent:abc:agt_0000000000",
|
|
17
|
+
]
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
@pytest.mark.parametrize("did", VALID_DIDS)
|
|
21
|
+
def test_validate_valid(did: str) -> None:
|
|
22
|
+
assert validate_did(did) is True
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
# ---------------------------------------------------------------------------
|
|
26
|
+
# Validation — invalid DIDs (from test vectors)
|
|
27
|
+
# ---------------------------------------------------------------------------
|
|
28
|
+
|
|
29
|
+
INVALID_DIDS = [
|
|
30
|
+
"did:agent:AB:agt_a1B2c3D4e5", # platform too short (2 chars) and uppercase
|
|
31
|
+
"did:agent:toolongplatformnamehere:agt_a1B2c3D4e5", # platform > 20 chars
|
|
32
|
+
"did:agent:tokli:a1B2c3D4e5", # missing agt_ prefix
|
|
33
|
+
"did:agent:tokli:agt_short", # unique_id too short
|
|
34
|
+
"did:agent:tokli:agt_a1B2c3D4e5!", # invalid character
|
|
35
|
+
"did:other:tokli:agt_a1B2c3D4e5", # wrong method
|
|
36
|
+
"did:agent:UPPER:agt_a1B2c3D4e5", # uppercase platform
|
|
37
|
+
"", # empty string
|
|
38
|
+
]
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
@pytest.mark.parametrize("did", INVALID_DIDS)
|
|
42
|
+
def test_validate_invalid(did: str) -> None:
|
|
43
|
+
assert validate_did(did) is False
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
# ---------------------------------------------------------------------------
|
|
47
|
+
# Parsing
|
|
48
|
+
# ---------------------------------------------------------------------------
|
|
49
|
+
|
|
50
|
+
def test_parse_did_valid() -> None:
|
|
51
|
+
result = parse_did("did:agent:tokli:agt_a1B2c3D4e5")
|
|
52
|
+
assert result == {
|
|
53
|
+
"method": "agent",
|
|
54
|
+
"platform": "tokli",
|
|
55
|
+
"unique_id": "agt_a1B2c3D4e5",
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def test_parse_did_invalid_raises() -> None:
|
|
60
|
+
with pytest.raises(ValueError):
|
|
61
|
+
parse_did("did:other:tokli:agt_a1B2c3D4e5")
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
# ---------------------------------------------------------------------------
|
|
65
|
+
# Generation
|
|
66
|
+
# ---------------------------------------------------------------------------
|
|
67
|
+
|
|
68
|
+
def test_generate_unique_id_format() -> None:
|
|
69
|
+
uid = generate_unique_id()
|
|
70
|
+
assert uid.startswith("agt_")
|
|
71
|
+
assert len(uid) == 14 # 'agt_' (4) + 10 base62 chars
|
|
72
|
+
# Should be valid when used in a DID
|
|
73
|
+
assert validate_did(f"did:agent:test:{uid}") is True
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def test_generate_unique_id_randomness() -> None:
|
|
77
|
+
ids = {generate_unique_id() for _ in range(100)}
|
|
78
|
+
# Extremely unlikely to have collisions
|
|
79
|
+
assert len(ids) == 100
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
"""Tests for the AgentIdentity class."""
|
|
2
|
+
|
|
3
|
+
import pytest
|
|
4
|
+
|
|
5
|
+
from agent_id.identity import AgentIdentity
|
|
6
|
+
from agent_id.crypto import generate_keypair, base64url_encode, verify, base64url_decode
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def _make_identity() -> AgentIdentity:
|
|
10
|
+
"""Create a test identity with a fresh keypair."""
|
|
11
|
+
priv, pub = generate_keypair()
|
|
12
|
+
return AgentIdentity(
|
|
13
|
+
did="did:agent:testplatform:agt_a1B2c3D4e5",
|
|
14
|
+
private_key=priv,
|
|
15
|
+
public_key=pub,
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def test_constructor_validates_did() -> None:
|
|
20
|
+
_, pub = generate_keypair()
|
|
21
|
+
with pytest.raises(ValueError):
|
|
22
|
+
AgentIdentity(did="bad-did", private_key=None, public_key=pub)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def test_did_property() -> None:
|
|
26
|
+
ident = _make_identity()
|
|
27
|
+
assert ident.did == "did:agent:testplatform:agt_a1B2c3D4e5"
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def test_public_key_base64url() -> None:
|
|
31
|
+
priv, pub = generate_keypair()
|
|
32
|
+
ident = AgentIdentity(
|
|
33
|
+
did="did:agent:test:agt_a1B2c3D4e5",
|
|
34
|
+
private_key=priv,
|
|
35
|
+
public_key=pub,
|
|
36
|
+
)
|
|
37
|
+
encoded = ident.public_key_base64url
|
|
38
|
+
assert base64url_decode(encoded) == pub
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def test_sign_produces_valid_signature() -> None:
|
|
42
|
+
ident = _make_identity()
|
|
43
|
+
payload = "test payload"
|
|
44
|
+
sig_b64 = ident.sign(payload)
|
|
45
|
+
sig_bytes = base64url_decode(sig_b64)
|
|
46
|
+
assert len(sig_bytes) == 64
|
|
47
|
+
# Verify with raw public key
|
|
48
|
+
assert verify(payload.encode("utf-8"), sig_bytes, base64url_decode(ident.public_key_base64url))
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def test_sign_without_private_key_raises() -> None:
|
|
52
|
+
_, pub = generate_keypair()
|
|
53
|
+
ident = AgentIdentity(
|
|
54
|
+
did="did:agent:test:agt_a1B2c3D4e5",
|
|
55
|
+
private_key=None,
|
|
56
|
+
public_key=pub,
|
|
57
|
+
)
|
|
58
|
+
with pytest.raises(RuntimeError, match="no private key"):
|
|
59
|
+
ident.sign("anything")
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def test_sign_request_returns_required_headers() -> None:
|
|
63
|
+
ident = _make_identity()
|
|
64
|
+
headers = ident.sign_request("POST", "https://example.com/api", '{"key":"val"}')
|
|
65
|
+
assert headers["X-Agent-DID"] == ident.did
|
|
66
|
+
assert "X-Agent-Timestamp" in headers
|
|
67
|
+
assert "X-Agent-Nonce" in headers
|
|
68
|
+
assert "X-Agent-Signature" in headers
|
|
69
|
+
# Timestamp should be a numeric string
|
|
70
|
+
int(headers["X-Agent-Timestamp"])
|
|
71
|
+
# Nonce should be hex
|
|
72
|
+
int(headers["X-Agent-Nonce"], 16)
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def test_sign_request_signature_verifiable() -> None:
|
|
76
|
+
"""Verify that the signature in the headers actually validates."""
|
|
77
|
+
priv, pub = generate_keypair()
|
|
78
|
+
ident = AgentIdentity(
|
|
79
|
+
did="did:agent:test:agt_a1B2c3D4e5",
|
|
80
|
+
private_key=priv,
|
|
81
|
+
public_key=pub,
|
|
82
|
+
)
|
|
83
|
+
body = '{"task":"search"}'
|
|
84
|
+
headers = ident.sign_request("POST", "https://api.example.com/v1/tasks", body)
|
|
85
|
+
|
|
86
|
+
# Reconstruct canonical payload
|
|
87
|
+
from agent_id.crypto import sha256_hex
|
|
88
|
+
body_hash = sha256_hex(body.encode("utf-8"))
|
|
89
|
+
canonical = (
|
|
90
|
+
f"POST\nhttps://api.example.com/v1/tasks\n{body_hash}\n"
|
|
91
|
+
f"{headers['X-Agent-Timestamp']}\n{headers['X-Agent-Nonce']}"
|
|
92
|
+
)
|
|
93
|
+
sig_bytes = base64url_decode(headers["X-Agent-Signature"])
|
|
94
|
+
assert verify(canonical.encode("utf-8"), sig_bytes, pub)
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
def test_load_from_base64url_key() -> None:
|
|
98
|
+
priv, pub = generate_keypair()
|
|
99
|
+
did = "did:agent:test:agt_a1B2c3D4e5"
|
|
100
|
+
priv_b64 = base64url_encode(priv)
|
|
101
|
+
|
|
102
|
+
loaded = AgentIdentity.load(did, priv_b64)
|
|
103
|
+
assert loaded.did == did
|
|
104
|
+
assert loaded.public_key_base64url == base64url_encode(pub)
|
|
105
|
+
|
|
106
|
+
# Should be able to sign
|
|
107
|
+
sig_b64 = loaded.sign("hello")
|
|
108
|
+
sig_bytes = base64url_decode(sig_b64)
|
|
109
|
+
assert verify(b"hello", sig_bytes, pub)
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
"""Tests against the official test vectors from the protocol spec."""
|
|
2
|
+
|
|
3
|
+
import json
|
|
4
|
+
from pathlib import Path
|
|
5
|
+
|
|
6
|
+
import pytest
|
|
7
|
+
|
|
8
|
+
from agent_id.crypto import sign, verify, sha256_hex, base64url_encode, base64url_decode
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
# Load test vectors
|
|
12
|
+
_VECTORS_PATH = Path(__file__).resolve().parent.parent.parent / "protocol" / "test-vectors" / "vectors.json"
|
|
13
|
+
with open(_VECTORS_PATH) as f:
|
|
14
|
+
VECTORS = json.load(f)
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
# ---------------------------------------------------------------------------
|
|
18
|
+
# Canonical payload construction
|
|
19
|
+
# ---------------------------------------------------------------------------
|
|
20
|
+
|
|
21
|
+
class TestCanonicalPayload:
|
|
22
|
+
"""Test canonical payload construction from the test vectors."""
|
|
23
|
+
|
|
24
|
+
@pytest.mark.parametrize(
|
|
25
|
+
"tc",
|
|
26
|
+
VECTORS["canonical_payload"]["test_cases"],
|
|
27
|
+
ids=[tc["description"] for tc in VECTORS["canonical_payload"]["test_cases"]],
|
|
28
|
+
)
|
|
29
|
+
def test_canonical_payload(self, tc: dict) -> None:
|
|
30
|
+
# Use the pre-computed body_sha256 from the vector when provided,
|
|
31
|
+
# otherwise compute it ourselves. The test vectors may use a
|
|
32
|
+
# body_sha256 that was generated independently of the body field.
|
|
33
|
+
if "body_sha256" in tc:
|
|
34
|
+
body_hash = tc["body_sha256"]
|
|
35
|
+
else:
|
|
36
|
+
body_hash = sha256_hex(tc["body"].encode("utf-8"))
|
|
37
|
+
canonical = (
|
|
38
|
+
f"{tc['method']}\n{tc['url']}\n{body_hash}\n{tc['timestamp']}\n{tc['nonce']}"
|
|
39
|
+
)
|
|
40
|
+
assert canonical == tc["expected"]
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
# ---------------------------------------------------------------------------
|
|
44
|
+
# Signing with the test keypair
|
|
45
|
+
# ---------------------------------------------------------------------------
|
|
46
|
+
|
|
47
|
+
class TestSigningVectors:
|
|
48
|
+
"""Test signing against the known test keypair."""
|
|
49
|
+
|
|
50
|
+
@pytest.fixture()
|
|
51
|
+
def keypair(self) -> dict:
|
|
52
|
+
kp = VECTORS["signing"]["keypair"]
|
|
53
|
+
return {
|
|
54
|
+
"private_key": bytes.fromhex(kp["private_key_hex"]),
|
|
55
|
+
"public_key": bytes.fromhex(kp["public_key_hex"]),
|
|
56
|
+
"public_key_base64url": kp["public_key_base64url"],
|
|
57
|
+
"private_key_base64url": kp["private_key_base64url"],
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
def test_public_key_base64url(self, keypair: dict) -> None:
|
|
61
|
+
assert base64url_encode(keypair["public_key"]) == keypair["public_key_base64url"]
|
|
62
|
+
|
|
63
|
+
def test_private_key_roundtrip(self, keypair: dict) -> None:
|
|
64
|
+
# Verify that encoding the hex-decoded private key to base64url and
|
|
65
|
+
# back produces the same bytes. (The base64url string in the test
|
|
66
|
+
# vector file has a known encoding inconsistency with the hex, so we
|
|
67
|
+
# test our own encode/decode round-trip using the hex as source of
|
|
68
|
+
# truth.)
|
|
69
|
+
encoded = base64url_encode(keypair["private_key"])
|
|
70
|
+
decoded = base64url_decode(encoded)
|
|
71
|
+
assert decoded == keypair["private_key"]
|
|
72
|
+
|
|
73
|
+
def test_empty_payload_signature(self, keypair: dict) -> None:
|
|
74
|
+
"""Empty payload has a known deterministic signature for Ed25519."""
|
|
75
|
+
tc = VECTORS["signing"]["test_cases"][0]
|
|
76
|
+
assert tc["description"] == "Empty payload signature"
|
|
77
|
+
|
|
78
|
+
sig = sign(tc["payload"].encode("utf-8"), keypair["private_key"])
|
|
79
|
+
assert sig.hex() == tc["signature_hex"]
|
|
80
|
+
|
|
81
|
+
def test_empty_payload_verify(self, keypair: dict) -> None:
|
|
82
|
+
tc = VECTORS["signing"]["test_cases"][0]
|
|
83
|
+
sig = bytes.fromhex(tc["signature_hex"])
|
|
84
|
+
assert verify(tc["payload"].encode("utf-8"), sig, keypair["public_key"]) is True
|
|
85
|
+
|
|
86
|
+
def test_sign_verify_roundtrip_with_test_key(self, keypair: dict) -> None:
|
|
87
|
+
"""Sign an arbitrary payload and verify it round-trips."""
|
|
88
|
+
payload = b"arbitrary test data"
|
|
89
|
+
sig = sign(payload, keypair["private_key"])
|
|
90
|
+
assert verify(payload, sig, keypair["public_key"]) is True
|
|
91
|
+
# Tampered payload should fail
|
|
92
|
+
assert verify(b"tampered", sig, keypair["public_key"]) is False
|