vaultody-verify 0.9.0rc1__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.
Files changed (30) hide show
  1. vaultody_verify-0.9.0rc1/LICENSE +202 -0
  2. vaultody_verify-0.9.0rc1/MANIFEST.in +3 -0
  3. vaultody_verify-0.9.0rc1/PKG-INFO +240 -0
  4. vaultody_verify-0.9.0rc1/README.md +224 -0
  5. vaultody_verify-0.9.0rc1/pyproject.toml +31 -0
  6. vaultody_verify-0.9.0rc1/setup.cfg +4 -0
  7. vaultody_verify-0.9.0rc1/vaultody_verify/__init__.py +56 -0
  8. vaultody_verify-0.9.0rc1/vaultody_verify/address/__init__.py +12 -0
  9. vaultody_verify-0.9.0rc1/vaultody_verify/address/chains.py +168 -0
  10. vaultody_verify-0.9.0rc1/vaultody_verify/address/check.py +240 -0
  11. vaultody_verify-0.9.0rc1/vaultody_verify/address/crypto/__init__.py +10 -0
  12. vaultody_verify-0.9.0rc1/vaultody_verify/address/crypto/base58.py +59 -0
  13. vaultody_verify-0.9.0rc1/vaultody_verify/address/crypto/bech32.py +89 -0
  14. vaultody_verify-0.9.0rc1/vaultody_verify/address/crypto/cashaddr.py +45 -0
  15. vaultody_verify-0.9.0rc1/vaultody_verify/address/crypto/ed25519.py +92 -0
  16. vaultody_verify-0.9.0rc1/vaultody_verify/address/crypto/keccak.py +87 -0
  17. vaultody_verify-0.9.0rc1/vaultody_verify/address/crypto/ripemd160.py +114 -0
  18. vaultody_verify-0.9.0rc1/vaultody_verify/address/crypto/secp256k1.py +117 -0
  19. vaultody_verify-0.9.0rc1/vaultody_verify/address/derivation.py +110 -0
  20. vaultody_verify-0.9.0rc1/vaultody_verify/address/errors.py +32 -0
  21. vaultody_verify-0.9.0rc1/vaultody_verify/errors.py +43 -0
  22. vaultody_verify-0.9.0rc1/vaultody_verify/keys.py +467 -0
  23. vaultody_verify-0.9.0rc1/vaultody_verify/response.py +367 -0
  24. vaultody_verify-0.9.0rc1/vaultody_verify/roots.py +220 -0
  25. vaultody_verify-0.9.0rc1/vaultody_verify/webhook.py +108 -0
  26. vaultody_verify-0.9.0rc1/vaultody_verify.egg-info/PKG-INFO +240 -0
  27. vaultody_verify-0.9.0rc1/vaultody_verify.egg-info/SOURCES.txt +28 -0
  28. vaultody_verify-0.9.0rc1/vaultody_verify.egg-info/dependency_links.txt +1 -0
  29. vaultody_verify-0.9.0rc1/vaultody_verify.egg-info/requires.txt +4 -0
  30. vaultody_verify-0.9.0rc1/vaultody_verify.egg-info/top_level.txt +1 -0
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright 2026 Vaultody
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
@@ -0,0 +1,3 @@
1
+ # The sdist leaves the tests out: they read the shared test vectors at the repository root, which it does
2
+ # not carry (scripts/release/check-packages.mjs checks what each package ships).
3
+ prune tests
@@ -0,0 +1,240 @@
1
+ Metadata-Version: 2.4
2
+ Name: vaultody-verify
3
+ Version: 0.9.0rc1
4
+ Summary: Verify that Vaultody API responses are signed by the Vaultody API gateway and unchanged.
5
+ License-Expression: Apache-2.0
6
+ Classifier: Programming Language :: Python :: 3
7
+ Classifier: Programming Language :: Python :: 3 :: Only
8
+ Classifier: Topic :: Security :: Cryptography
9
+ Requires-Python: >=3.10
10
+ Description-Content-Type: text/markdown
11
+ License-File: LICENSE
12
+ Requires-Dist: cryptography>=3.1
13
+ Provides-Extra: requests
14
+ Requires-Dist: requests>=2.20; extra == "requests"
15
+ Dynamic: license-file
16
+
17
+ # vaultody-verify (Python)
18
+
19
+ Checks that a Vaultody API response came from the Vaultody API gateway unchanged, and that it answers
20
+ the request you actually sent.
21
+
22
+ Cloudflare terminates TLS in front of the Vaultody API, so TLS alone doesn't prove where a response came
23
+ from. The gateway signs every response instead. A root key held in a hardware security module certifies
24
+ each gateway pod's short-lived signing key, and the pod signs every response together with the request
25
+ it answers. This package checks both signatures against roots it has pinned.
26
+
27
+ Python 3.10+. The only dependency is `cryptography`; `requests` support is an optional extra.
28
+
29
+ ```bash
30
+ pip install --pre 'vaultody-verify[requests]' # the release candidate from PyPI
31
+ pip install './python/vaultody-verify[requests]' # from a checkout
32
+ ```
33
+
34
+ ## Usage with `requests`
35
+
36
+ ```python
37
+ import requests
38
+ from vaultody_verify import ResponseSignatureError, verify_requests_response
39
+
40
+ resp = session.post(url, data=body_bytes, headers=signed_headers) # your usual signed API call
41
+
42
+ try:
43
+ verify_requests_response(resp) # raises unless the response is genuine
44
+ except ResponseSignatureError as error:
45
+ log.error("rejected Vaultody response: %s", error) # error.reason, e.g. "invalid"
46
+ raise
47
+
48
+ data = resp.json() # parse only after it verified
49
+ ```
50
+
51
+ `verify_requests_response` uses what `requests` really sent: `resp.request.method`,
52
+ `resp.request.path_url`, `resp.request.body` and the `x-api-sign` and `x-api-keypair-sign` headers, with `resp.status_code`,
53
+ `resp.headers` and `resp.content`. Send the body as `bytes` or `str` (including `json=`). A file or
54
+ generator body is gone by the time the response arrives, so it can't be verified and raises `TypeError`.
55
+
56
+ ## Any other HTTP client
57
+
58
+ ```python
59
+ from vaultody_verify import verify_response
60
+
61
+ result = verify_response(
62
+ request={
63
+ "method": "POST",
64
+ "url": "/vaults/6630f1c2a9e4b5d7c8e9f0a1/bitcoin/mainnet/addresses?context=yes", # path + query as sent
65
+ "body": request_body_bytes, # exactly the bytes sent; None if there was no body
66
+ "api_sign": x_api_sign, # the x-api-sign header you sent; None if none
67
+ "keypair_sign": x_api_keypair_sign, # the x-api-keypair-sign header you sent; None if none
68
+ },
69
+ response={
70
+ "status": 201,
71
+ "headers": response_headers, # dict or any Mapping; names are matched case-insensitively
72
+ "body": raw_response_bytes,
73
+ },
74
+ )
75
+ result.verified, result.key_id, result.root_id, result.timestamp
76
+ ```
77
+
78
+ - `url` is the path and query exactly as they went on the wire, percent-encoding untouched. It is not a
79
+ full URL. The query is everything after the first `?`.
80
+ - Bodies are raw bytes. A `str` is taken as UTF-8.
81
+ - If you compress request bodies yourself, pass the uncompressed bytes. The gateway hashes the body after
82
+ removing Content-Encoding.
83
+
84
+ ## Verify the raw bytes before parsing
85
+
86
+ The signature covers the exact response body bytes. Verify those bytes, then parse them. Don't parse
87
+ JSON and serialise it again, and don't read fields from a response that failed: until it verifies, an
88
+ address in it may be an attacker's. Use `resp.content`, not `resp.text` or `resp.json()`.
89
+
90
+ ## Strict by default
91
+
92
+ `require_response_signature=True` is the default: a response without a signature raises
93
+ `ResponseSignatureError("missing")`.
94
+
95
+ `require_response_signature=False` exists for rollout only. It tolerates exactly one thing: a response
96
+ with **none** of the four signature headers, which returns `VerificationResult(verified=False, reason="missing")`.
97
+ A response with some headers stripped, or with any other problem, still raises. An attacker who can
98
+ alter responses can also strip all four headers, so non-strict mode protects nothing; switch to strict
99
+ as soon as signing is on.
100
+
101
+ ## Roots and rotation
102
+
103
+ The package embeds the production roots in `vaultody_verify/roots.py` as SubjectPublicKeyInfo PEM, each
104
+ next to its fingerprint: the lowercase hex SHA-256 of the key's DER, which is also the `rootId` that
105
+ certificates name. At import, the package checks every embedded PEM against its fingerprint and refuses
106
+ to load on a mismatch.
107
+
108
+ There are two slots, **current** and **next**, so a root can be rotated without breaking clients:
109
+
110
+ 1. Vaultody creates a new root and publishes it as **next**, in a helper release and by email, ahead of
111
+ time. Clients upgrade, and from then on they trust both roots.
112
+ 2. The gateway switches to signing pod certificates with the new root.
113
+ 3. A later release drops the old root and pins a new **next**.
114
+
115
+ To compare the embedded fingerprints with the ones Vaultody sent you:
116
+
117
+ ```bash
118
+ python -c "from vaultody_verify.roots import embedded_fingerprints; print(embedded_fingerprints())"
119
+ openssl pkey -pubin -in root.pem -outform DER | shasum -a 256 # fingerprint of a PEM you were given
120
+ ```
121
+
122
+ `roots=` replaces the embedded roots, for example to trust a test environment's root or a key you
123
+ received out of band. It takes a list of PEM (`str` or `bytes`), DER `bytes`, `cryptography` EC
124
+ public keys, or a mapping such as `{"current": pem, "next": pem}`. A PEM or DER root must be an
125
+ uncompressed, named-curve P-256 SPKI, and its id is the SHA-256 of those exact DER bytes; a compressed
126
+ point or explicit curve parameters raise `ConfigurationError`:
127
+
128
+ ```python
129
+ verify_requests_response(resp, roots=[QA_ROOT_PEM])
130
+ ```
131
+
132
+ This build embeds the production root (fingerprint `0194e94f295844b04dbdfb885338fb4242095e55479a01a03caafee3e36cdd76`) as the current root; the
133
+ next one is empty until a rotation. Without `roots=` a signed response is checked against it. A `roots=` that holds
134
+ no key raises `ConfigurationError` on a signed response, so a missing root can never let a signed response
135
+ through unchecked. The roots are looked up only once a response carries a signature: in
136
+ non-strict mode a response with no signature headers at all still returns `verified=False`,
137
+ `reason="missing"` without any root, and in strict mode it raises `ResponseSignatureError("missing")`.
138
+ A malformed `roots=` value raises `ConfigurationError` at once, signed response or not.
139
+
140
+ ## Errors
141
+
142
+ `ResponseSignatureError` means: don't trust this response. `.reason` is one of:
143
+
144
+ | reason | meaning |
145
+ |---|---|
146
+ | `missing` | a signature header is absent or empty (strict mode, or only some headers missing) |
147
+ | `invalid` | malformed headers or certificate, a certificate valid for more than an hour (`notAfter - notBefore` over 3600 seconds), or a signature that does not verify for this request and response |
148
+ | `expired` | the signature time is outside the certificate window, or the local clock is more than `max_clock_skew_seconds` (300) past it |
149
+ | `unknown_root` | the certificate names a root that is not pinned (for example one already rotated out) |
150
+ | `request_unbound` | genuine, but the gateway did not read the request body (over 1 MB, or the upload was cut short), so the response is not bound to the body you sent |
151
+ | `unsupported_version` | a signature version this package does not know; upgrade it |
152
+
153
+ `ConfigurationError` means the verifier is set up wrong (no roots for a signed response, or an unusable
154
+ root key). It says
155
+ nothing about the response and is deliberately not a `ResponseSignatureError`. Wrong argument types raise
156
+ `TypeError`. An `url` that isn't a path raises `ValueError`. All errors this package raises on purpose
157
+ derive from `VaultodyVerifyError`.
158
+
159
+ Other options: `now=` (Unix seconds, for tests) and `max_clock_skew_seconds=` (default 300).
160
+
161
+ ## Signing requests
162
+
163
+ `vaultody_verify.keys` builds the authentication headers of an API request: `x-api-sign` (HMAC v2, with
164
+ a fresh `x-api-nonce` every time) and, for an API key with a request-signing key pair, `x-api-keypair-sign`
165
+ and `x-api-keypair-id`. The strings and signatures are the gateway's, pinned by
166
+ `test-vectors/request-hmac-v2.json`, `request-full-v1.json` and `request-keypair-v1.json`. The key-pair string
167
+ is `vaultody-request-keypair-v1`, the `x-api-key` the request is sent with, then the HMAC v2 fields
168
+ (timestamp, method, path, `x-api-version`, passphrase, nonce, query, body), joined with `\n`: two API keys
169
+ may hold the same public key, and a request signed for one is refused under the other.
170
+
171
+ ```python
172
+ from vaultody_verify import read_signing_key, sign_api_request, sign_hmac_v2_request
173
+
174
+ credentials = {"apiKey": api_key, "apiSecret": api_secret, "passphrase": passphrase}
175
+ request = {"method": "POST", "path": "/vaults/.../addresses", "query": "", "body": body_text, "apiVersion": "2026-03-20"}
176
+
177
+ headers = sign_hmac_v2_request(credentials, request) # an API key without a key pair
178
+ key = read_signing_key(open("private.pem").read()) # Ed25519 or P-256, PKCS#8 or SEC1 PEM
179
+ headers = sign_api_request(key, credentials, request) # with the key pair
180
+ ```
181
+
182
+ - The API secret must be the base64 text the dashboard shows (padding optional). Anything else raises
183
+ `UsageError` with `code == "invalid_api_secret"` before anything is signed; the secret is never in a
184
+ message. An API key that is not one line of text raises `UsageError` before the key pair signs.
185
+ - `p1363_to_der(signature)` turns a P-256 signature from Web Crypto, Azure Key Vault or a KMS that returns
186
+ IEEE P1363 (r || s, 64 bytes) into the DER the gateway requires (`SignatureFormatError` with
187
+ `invalid_length` or `zero_integer` otherwise; `test-vectors/p1363-der-v1.json`).
188
+ - `read_signing_keys_response(status, body, fingerprint)` reads a verified answer of
189
+ `GET /api-keys/current/signing-keys` and says whether your key is `active`, `inactive`, `not_listed`, or
190
+ the request was `refused` (`test-vectors/signing-keys-v1.json`). Verify the response first.
191
+
192
+ ## Webhook deliveries
193
+
194
+ ```python
195
+ from vaultody_verify import WebhookSignatureError, verify_webhook_signature
196
+
197
+ try:
198
+ verify_webhook_signature(request_body_bytes, request_headers, signing_secret)
199
+ except WebhookSignatureError as error: # error.reason: missing, malformed or mismatch
200
+ abort(401) # Flask, for example
201
+ event = json.loads(request_body_bytes) # parse only after it verified
202
+ ```
203
+
204
+ `X-Signature` must be exactly 64 lowercase hex characters: the HMAC-SHA256 of the raw body, keyed with the
205
+ signing secret. Pass a list of secrets during a rotation; the result's `secret_index` says which one
206
+ matched (`test-vectors/webhook-v1.json`).
207
+
208
+ The scheme has no timestamp, and Vaultody may deliver the same event more than once. Before you act on an event,
209
+ check whether you already acted on it, keyed on what the event is about, as the developer docs' Webhook
210
+ Notifications page says: for outgoing transaction events `data.item.requestId` and `data.event`; for incoming
211
+ ones `data.item.transactionId`, `data.item.address`, `data.event` and `data.item.currentConfirmations` (an
212
+ internal transfer has no `transactionId`: use `data.item.parentTransactionId` and `data.item.operationId` in its
213
+ place).
214
+
215
+ ## Vault addresses
216
+
217
+ ```python
218
+ from vaultody_verify import AddressVerificationError, check_address, network_of_vault
219
+
220
+ vault = {"type": "main", "keys": {"ECDSA": ecdsa_xpub, "EDDSA": eddsa_xpub}} # pinned from GET /vaults/main
221
+ check_address(vault, blockchain="bitcoin", network=network_of_vault("bitcoin", "main"), address=address,
222
+ account_index=0, is_change=False, address_index=7, address_format="p2wpkh")
223
+ ```
224
+
225
+ `check_address` derives the address from the vault's xpub at `m/account/change/index` and raises
226
+ `AddressVerificationError` (`reason`: `unsupported`, `network_mismatch`, `not_verifiable`, `missing_key` or
227
+ `mismatch`) unless it is exactly the one given. Where the xpub comes from is your trust decision.
228
+ `network_of_vault` gives the network of an address whose response omits it (`test-vectors/address-v1.json`).
229
+
230
+ ## Tests
231
+
232
+ ```bash
233
+ cd python/vaultody-verify
234
+ python3 -m venv .venv && .venv/bin/pip install -e '.[requests]' pytest
235
+ .venv/bin/pytest -q
236
+ ```
237
+
238
+ The tests run every case in `../../test-vectors/v1.json`, the canonical vectors generated by the gateway
239
+ with test-only keys, and require exactly the verdict and reason each one expects; likewise the key-pair,
240
+ HMAC v2, full header set, P1363, signing-keys and webhook vectors in the same directory.
@@ -0,0 +1,224 @@
1
+ # vaultody-verify (Python)
2
+
3
+ Checks that a Vaultody API response came from the Vaultody API gateway unchanged, and that it answers
4
+ the request you actually sent.
5
+
6
+ Cloudflare terminates TLS in front of the Vaultody API, so TLS alone doesn't prove where a response came
7
+ from. The gateway signs every response instead. A root key held in a hardware security module certifies
8
+ each gateway pod's short-lived signing key, and the pod signs every response together with the request
9
+ it answers. This package checks both signatures against roots it has pinned.
10
+
11
+ Python 3.10+. The only dependency is `cryptography`; `requests` support is an optional extra.
12
+
13
+ ```bash
14
+ pip install --pre 'vaultody-verify[requests]' # the release candidate from PyPI
15
+ pip install './python/vaultody-verify[requests]' # from a checkout
16
+ ```
17
+
18
+ ## Usage with `requests`
19
+
20
+ ```python
21
+ import requests
22
+ from vaultody_verify import ResponseSignatureError, verify_requests_response
23
+
24
+ resp = session.post(url, data=body_bytes, headers=signed_headers) # your usual signed API call
25
+
26
+ try:
27
+ verify_requests_response(resp) # raises unless the response is genuine
28
+ except ResponseSignatureError as error:
29
+ log.error("rejected Vaultody response: %s", error) # error.reason, e.g. "invalid"
30
+ raise
31
+
32
+ data = resp.json() # parse only after it verified
33
+ ```
34
+
35
+ `verify_requests_response` uses what `requests` really sent: `resp.request.method`,
36
+ `resp.request.path_url`, `resp.request.body` and the `x-api-sign` and `x-api-keypair-sign` headers, with `resp.status_code`,
37
+ `resp.headers` and `resp.content`. Send the body as `bytes` or `str` (including `json=`). A file or
38
+ generator body is gone by the time the response arrives, so it can't be verified and raises `TypeError`.
39
+
40
+ ## Any other HTTP client
41
+
42
+ ```python
43
+ from vaultody_verify import verify_response
44
+
45
+ result = verify_response(
46
+ request={
47
+ "method": "POST",
48
+ "url": "/vaults/6630f1c2a9e4b5d7c8e9f0a1/bitcoin/mainnet/addresses?context=yes", # path + query as sent
49
+ "body": request_body_bytes, # exactly the bytes sent; None if there was no body
50
+ "api_sign": x_api_sign, # the x-api-sign header you sent; None if none
51
+ "keypair_sign": x_api_keypair_sign, # the x-api-keypair-sign header you sent; None if none
52
+ },
53
+ response={
54
+ "status": 201,
55
+ "headers": response_headers, # dict or any Mapping; names are matched case-insensitively
56
+ "body": raw_response_bytes,
57
+ },
58
+ )
59
+ result.verified, result.key_id, result.root_id, result.timestamp
60
+ ```
61
+
62
+ - `url` is the path and query exactly as they went on the wire, percent-encoding untouched. It is not a
63
+ full URL. The query is everything after the first `?`.
64
+ - Bodies are raw bytes. A `str` is taken as UTF-8.
65
+ - If you compress request bodies yourself, pass the uncompressed bytes. The gateway hashes the body after
66
+ removing Content-Encoding.
67
+
68
+ ## Verify the raw bytes before parsing
69
+
70
+ The signature covers the exact response body bytes. Verify those bytes, then parse them. Don't parse
71
+ JSON and serialise it again, and don't read fields from a response that failed: until it verifies, an
72
+ address in it may be an attacker's. Use `resp.content`, not `resp.text` or `resp.json()`.
73
+
74
+ ## Strict by default
75
+
76
+ `require_response_signature=True` is the default: a response without a signature raises
77
+ `ResponseSignatureError("missing")`.
78
+
79
+ `require_response_signature=False` exists for rollout only. It tolerates exactly one thing: a response
80
+ with **none** of the four signature headers, which returns `VerificationResult(verified=False, reason="missing")`.
81
+ A response with some headers stripped, or with any other problem, still raises. An attacker who can
82
+ alter responses can also strip all four headers, so non-strict mode protects nothing; switch to strict
83
+ as soon as signing is on.
84
+
85
+ ## Roots and rotation
86
+
87
+ The package embeds the production roots in `vaultody_verify/roots.py` as SubjectPublicKeyInfo PEM, each
88
+ next to its fingerprint: the lowercase hex SHA-256 of the key's DER, which is also the `rootId` that
89
+ certificates name. At import, the package checks every embedded PEM against its fingerprint and refuses
90
+ to load on a mismatch.
91
+
92
+ There are two slots, **current** and **next**, so a root can be rotated without breaking clients:
93
+
94
+ 1. Vaultody creates a new root and publishes it as **next**, in a helper release and by email, ahead of
95
+ time. Clients upgrade, and from then on they trust both roots.
96
+ 2. The gateway switches to signing pod certificates with the new root.
97
+ 3. A later release drops the old root and pins a new **next**.
98
+
99
+ To compare the embedded fingerprints with the ones Vaultody sent you:
100
+
101
+ ```bash
102
+ python -c "from vaultody_verify.roots import embedded_fingerprints; print(embedded_fingerprints())"
103
+ openssl pkey -pubin -in root.pem -outform DER | shasum -a 256 # fingerprint of a PEM you were given
104
+ ```
105
+
106
+ `roots=` replaces the embedded roots, for example to trust a test environment's root or a key you
107
+ received out of band. It takes a list of PEM (`str` or `bytes`), DER `bytes`, `cryptography` EC
108
+ public keys, or a mapping such as `{"current": pem, "next": pem}`. A PEM or DER root must be an
109
+ uncompressed, named-curve P-256 SPKI, and its id is the SHA-256 of those exact DER bytes; a compressed
110
+ point or explicit curve parameters raise `ConfigurationError`:
111
+
112
+ ```python
113
+ verify_requests_response(resp, roots=[QA_ROOT_PEM])
114
+ ```
115
+
116
+ This build embeds the production root (fingerprint `0194e94f295844b04dbdfb885338fb4242095e55479a01a03caafee3e36cdd76`) as the current root; the
117
+ next one is empty until a rotation. Without `roots=` a signed response is checked against it. A `roots=` that holds
118
+ no key raises `ConfigurationError` on a signed response, so a missing root can never let a signed response
119
+ through unchecked. The roots are looked up only once a response carries a signature: in
120
+ non-strict mode a response with no signature headers at all still returns `verified=False`,
121
+ `reason="missing"` without any root, and in strict mode it raises `ResponseSignatureError("missing")`.
122
+ A malformed `roots=` value raises `ConfigurationError` at once, signed response or not.
123
+
124
+ ## Errors
125
+
126
+ `ResponseSignatureError` means: don't trust this response. `.reason` is one of:
127
+
128
+ | reason | meaning |
129
+ |---|---|
130
+ | `missing` | a signature header is absent or empty (strict mode, or only some headers missing) |
131
+ | `invalid` | malformed headers or certificate, a certificate valid for more than an hour (`notAfter - notBefore` over 3600 seconds), or a signature that does not verify for this request and response |
132
+ | `expired` | the signature time is outside the certificate window, or the local clock is more than `max_clock_skew_seconds` (300) past it |
133
+ | `unknown_root` | the certificate names a root that is not pinned (for example one already rotated out) |
134
+ | `request_unbound` | genuine, but the gateway did not read the request body (over 1 MB, or the upload was cut short), so the response is not bound to the body you sent |
135
+ | `unsupported_version` | a signature version this package does not know; upgrade it |
136
+
137
+ `ConfigurationError` means the verifier is set up wrong (no roots for a signed response, or an unusable
138
+ root key). It says
139
+ nothing about the response and is deliberately not a `ResponseSignatureError`. Wrong argument types raise
140
+ `TypeError`. An `url` that isn't a path raises `ValueError`. All errors this package raises on purpose
141
+ derive from `VaultodyVerifyError`.
142
+
143
+ Other options: `now=` (Unix seconds, for tests) and `max_clock_skew_seconds=` (default 300).
144
+
145
+ ## Signing requests
146
+
147
+ `vaultody_verify.keys` builds the authentication headers of an API request: `x-api-sign` (HMAC v2, with
148
+ a fresh `x-api-nonce` every time) and, for an API key with a request-signing key pair, `x-api-keypair-sign`
149
+ and `x-api-keypair-id`. The strings and signatures are the gateway's, pinned by
150
+ `test-vectors/request-hmac-v2.json`, `request-full-v1.json` and `request-keypair-v1.json`. The key-pair string
151
+ is `vaultody-request-keypair-v1`, the `x-api-key` the request is sent with, then the HMAC v2 fields
152
+ (timestamp, method, path, `x-api-version`, passphrase, nonce, query, body), joined with `\n`: two API keys
153
+ may hold the same public key, and a request signed for one is refused under the other.
154
+
155
+ ```python
156
+ from vaultody_verify import read_signing_key, sign_api_request, sign_hmac_v2_request
157
+
158
+ credentials = {"apiKey": api_key, "apiSecret": api_secret, "passphrase": passphrase}
159
+ request = {"method": "POST", "path": "/vaults/.../addresses", "query": "", "body": body_text, "apiVersion": "2026-03-20"}
160
+
161
+ headers = sign_hmac_v2_request(credentials, request) # an API key without a key pair
162
+ key = read_signing_key(open("private.pem").read()) # Ed25519 or P-256, PKCS#8 or SEC1 PEM
163
+ headers = sign_api_request(key, credentials, request) # with the key pair
164
+ ```
165
+
166
+ - The API secret must be the base64 text the dashboard shows (padding optional). Anything else raises
167
+ `UsageError` with `code == "invalid_api_secret"` before anything is signed; the secret is never in a
168
+ message. An API key that is not one line of text raises `UsageError` before the key pair signs.
169
+ - `p1363_to_der(signature)` turns a P-256 signature from Web Crypto, Azure Key Vault or a KMS that returns
170
+ IEEE P1363 (r || s, 64 bytes) into the DER the gateway requires (`SignatureFormatError` with
171
+ `invalid_length` or `zero_integer` otherwise; `test-vectors/p1363-der-v1.json`).
172
+ - `read_signing_keys_response(status, body, fingerprint)` reads a verified answer of
173
+ `GET /api-keys/current/signing-keys` and says whether your key is `active`, `inactive`, `not_listed`, or
174
+ the request was `refused` (`test-vectors/signing-keys-v1.json`). Verify the response first.
175
+
176
+ ## Webhook deliveries
177
+
178
+ ```python
179
+ from vaultody_verify import WebhookSignatureError, verify_webhook_signature
180
+
181
+ try:
182
+ verify_webhook_signature(request_body_bytes, request_headers, signing_secret)
183
+ except WebhookSignatureError as error: # error.reason: missing, malformed or mismatch
184
+ abort(401) # Flask, for example
185
+ event = json.loads(request_body_bytes) # parse only after it verified
186
+ ```
187
+
188
+ `X-Signature` must be exactly 64 lowercase hex characters: the HMAC-SHA256 of the raw body, keyed with the
189
+ signing secret. Pass a list of secrets during a rotation; the result's `secret_index` says which one
190
+ matched (`test-vectors/webhook-v1.json`).
191
+
192
+ The scheme has no timestamp, and Vaultody may deliver the same event more than once. Before you act on an event,
193
+ check whether you already acted on it, keyed on what the event is about, as the developer docs' Webhook
194
+ Notifications page says: for outgoing transaction events `data.item.requestId` and `data.event`; for incoming
195
+ ones `data.item.transactionId`, `data.item.address`, `data.event` and `data.item.currentConfirmations` (an
196
+ internal transfer has no `transactionId`: use `data.item.parentTransactionId` and `data.item.operationId` in its
197
+ place).
198
+
199
+ ## Vault addresses
200
+
201
+ ```python
202
+ from vaultody_verify import AddressVerificationError, check_address, network_of_vault
203
+
204
+ vault = {"type": "main", "keys": {"ECDSA": ecdsa_xpub, "EDDSA": eddsa_xpub}} # pinned from GET /vaults/main
205
+ check_address(vault, blockchain="bitcoin", network=network_of_vault("bitcoin", "main"), address=address,
206
+ account_index=0, is_change=False, address_index=7, address_format="p2wpkh")
207
+ ```
208
+
209
+ `check_address` derives the address from the vault's xpub at `m/account/change/index` and raises
210
+ `AddressVerificationError` (`reason`: `unsupported`, `network_mismatch`, `not_verifiable`, `missing_key` or
211
+ `mismatch`) unless it is exactly the one given. Where the xpub comes from is your trust decision.
212
+ `network_of_vault` gives the network of an address whose response omits it (`test-vectors/address-v1.json`).
213
+
214
+ ## Tests
215
+
216
+ ```bash
217
+ cd python/vaultody-verify
218
+ python3 -m venv .venv && .venv/bin/pip install -e '.[requests]' pytest
219
+ .venv/bin/pytest -q
220
+ ```
221
+
222
+ The tests run every case in `../../test-vectors/v1.json`, the canonical vectors generated by the gateway
223
+ with test-only keys, and require exactly the verdict and reason each one expects; likewise the key-pair,
224
+ HMAC v2, full header set, P1363, signing-keys and webhook vectors in the same directory.