stellar-strkey 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.
- stellar_strkey-0.1.0/CHANGELOG.md +12 -0
- stellar_strkey-0.1.0/LICENSE +201 -0
- stellar_strkey-0.1.0/PKG-INFO +165 -0
- stellar_strkey-0.1.0/README.md +134 -0
- stellar_strkey-0.1.0/docs/api.md +41 -0
- stellar_strkey-0.1.0/docs/guide.md +215 -0
- stellar_strkey-0.1.0/docs/index.md +44 -0
- stellar_strkey-0.1.0/pyproject.toml +137 -0
- stellar_strkey-0.1.0/pyproject.toml.orig +123 -0
- stellar_strkey-0.1.0/src/stellar_strkey/__init__.py +58 -0
- stellar_strkey-0.1.0/src/stellar_strkey/_codec.py +82 -0
- stellar_strkey-0.1.0/src/stellar_strkey/_errors.py +69 -0
- stellar_strkey-0.1.0/src/stellar_strkey/_keys.py +526 -0
- stellar_strkey-0.1.0/src/stellar_strkey/py.typed +0 -0
- stellar_strkey-0.1.0/tests/__init__.py +0 -0
- stellar_strkey-0.1.0/tests/support.py +66 -0
- stellar_strkey-0.1.0/tests/test_construction.py +179 -0
- stellar_strkey-0.1.0/tests/test_decoding.py +163 -0
- stellar_strkey-0.1.0/tests/test_docs.py +17 -0
- stellar_strkey-0.1.0/tests/test_parsing.py +106 -0
- stellar_strkey-0.1.0/tests/test_private_key.py +147 -0
- stellar_strkey-0.1.0/tests/test_python_api.py +130 -0
- stellar_strkey-0.1.0/tests/test_vectors.py +129 -0
- stellar_strkey-0.1.0/tests/vectors/strkeys.json +848 -0
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project are documented in this file. The format is based on
|
|
4
|
+
[Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project follows
|
|
5
|
+
[Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
6
|
+
|
|
7
|
+
## [0.1.0](https://github.com/lightsail-network/py-stellar-strkey/releases/tag/v0.1.0) - 2026-11-07
|
|
8
|
+
|
|
9
|
+
### Features
|
|
10
|
+
|
|
11
|
+
- Add stellar-strkey, a typed SEP-23 strkey library
|
|
12
|
+
|
|
@@ -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,165 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: stellar-strkey
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Strict, typed, dependency-free encoding and decoding of Stellar strkeys (SEP-23).
|
|
5
|
+
Keywords: stellar,strkey,sep-23,base32,soroban
|
|
6
|
+
Author: overcat
|
|
7
|
+
Author-email: overcat <hi@overcat.dev>
|
|
8
|
+
License-Expression: Apache-2.0
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Classifier: Development Status :: 4 - Beta
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: Operating System :: OS Independent
|
|
13
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.15
|
|
19
|
+
Classifier: Programming Language :: Python :: Free Threading :: 3 - Stable
|
|
20
|
+
Classifier: Programming Language :: Python :: Implementation :: CPython
|
|
21
|
+
Classifier: Programming Language :: Python :: Implementation :: PyPy
|
|
22
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
23
|
+
Classifier: Typing :: Typed
|
|
24
|
+
Requires-Python: >=3.11
|
|
25
|
+
Project-URL: Homepage, https://github.com/lightsail-network/py-stellar-strkey
|
|
26
|
+
Project-URL: Documentation, https://stellar-strkey.readthedocs.io/
|
|
27
|
+
Project-URL: Repository, https://github.com/lightsail-network/py-stellar-strkey
|
|
28
|
+
Project-URL: Issues, https://github.com/lightsail-network/py-stellar-strkey/issues
|
|
29
|
+
Project-URL: Changelog, https://github.com/lightsail-network/py-stellar-strkey/blob/main/CHANGELOG.md
|
|
30
|
+
Description-Content-Type: text/markdown
|
|
31
|
+
|
|
32
|
+
# stellar-strkey
|
|
33
|
+
|
|
34
|
+
Encode and decode [Stellar strkeys](https://github.com/stellar/stellar-protocol/blob/master/ecosystem/sep-0023.md),
|
|
35
|
+
the `G...`, `C...` and `M...` strings that identify accounts, contracts, signers and
|
|
36
|
+
other things on the Stellar network.
|
|
37
|
+
|
|
38
|
+
- **Complete.** Supports every strkey type defined by SEP-23.
|
|
39
|
+
- **Strict.** Accepts a strkey only in its one canonical form and rejects everything else
|
|
40
|
+
with a clear error, so invalid input never gets through.
|
|
41
|
+
- **Thoroughly tested.** Verified against the SEP-23 test cases and a shared set of test
|
|
42
|
+
vectors, with property-based tests and 100% branch coverage.
|
|
43
|
+
- **Safe with secrets.** Secret seeds are never printed or serialized by accident, and
|
|
44
|
+
error messages never include the input.
|
|
45
|
+
- **Lightweight.** Fully typed, pure Python, no dependencies.
|
|
46
|
+
|
|
47
|
+
The [documentation](https://stellar-strkey.readthedocs.io/) has the guide and the API
|
|
48
|
+
reference.
|
|
49
|
+
|
|
50
|
+
## Install
|
|
51
|
+
|
|
52
|
+
```shell
|
|
53
|
+
pip install stellar-strkey
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
## Usage
|
|
57
|
+
|
|
58
|
+
```python
|
|
59
|
+
from stellar_strkey import (
|
|
60
|
+
Contract,
|
|
61
|
+
Ed25519PublicKey,
|
|
62
|
+
MuxedEd25519Account,
|
|
63
|
+
StrkeyError,
|
|
64
|
+
parse,
|
|
65
|
+
)
|
|
66
|
+
|
|
67
|
+
# When you know the type, use its class. Anything else is rejected.
|
|
68
|
+
key = Ed25519PublicKey.from_string(
|
|
69
|
+
"GA7QYNF7SOWQ3GLR2BGMZEHXAVIRZA4KVWLTJJFC7MGXUA74P7UJVSGZ"
|
|
70
|
+
)
|
|
71
|
+
assert key.key.hex() == (
|
|
72
|
+
"3f0c34bf93ad0d9971d04ccc90f705511c838aad9734a4a2fb0d7a03fc7fe89a"
|
|
73
|
+
)
|
|
74
|
+
# The same 32 bytes as a contract give a different strkey.
|
|
75
|
+
assert str(Contract(key.key)) == (
|
|
76
|
+
"CA7QYNF7SOWQ3GLR2BGMZEHXAVIRZA4KVWLTJJFC7MGXUA74P7UJUWDA"
|
|
77
|
+
)
|
|
78
|
+
|
|
79
|
+
# When you don't, parse() returns the matching class.
|
|
80
|
+
match parse("MA7QYNF7SOWQ3GLR2BGMZEHXAVIRZA4KVWLTJJFC7MGXUA74P7UJVAAAAAAAAAAAAAJLK"):
|
|
81
|
+
case Ed25519PublicKey(key):
|
|
82
|
+
description = f"account {key.hex()}"
|
|
83
|
+
case MuxedEd25519Account(key, id):
|
|
84
|
+
description = f"muxed account {key.hex()} with ID {id}"
|
|
85
|
+
case other:
|
|
86
|
+
description = f"something else: {other!r}"
|
|
87
|
+
assert description.endswith("with ID 9223372036854775808")
|
|
88
|
+
|
|
89
|
+
# Invalid input raises StrkeyError, which is a ValueError.
|
|
90
|
+
try:
|
|
91
|
+
parse("GA7QYNF7SOWQ3GLR2BGMZEHXAVIRZA4KVWLTJJFC7MGXUA74P7UJVSGA")
|
|
92
|
+
except StrkeyError as error:
|
|
93
|
+
message = str(error)
|
|
94
|
+
assert message == "strkey checksum does not match"
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
### Secret seeds
|
|
98
|
+
|
|
99
|
+
Secret seeds (`S...`) have their own parser, and `parse()` refuses them, so code that
|
|
100
|
+
handles arbitrary addresses can't pick up a secret by accident:
|
|
101
|
+
|
|
102
|
+
```python
|
|
103
|
+
from stellar_strkey import Ed25519PrivateKey, WrongStrkeyTypeError, parse
|
|
104
|
+
|
|
105
|
+
# In real code, load the secret from the environment or a secret store, never from
|
|
106
|
+
# source code. This is a well-known test seed.
|
|
107
|
+
secret = "SBU2RRGLXH3E5CQHTD3ODLDF2BWDCYUSSBLLZ5GNW7JXHDIYKXZWHOKR"
|
|
108
|
+
|
|
109
|
+
key = Ed25519PrivateKey.from_string(secret)
|
|
110
|
+
seed = key.seed # the raw 32 bytes, for example for an ed25519 signing library
|
|
111
|
+
assert len(seed) == 32
|
|
112
|
+
|
|
113
|
+
# Printing or logging the key never shows the seed.
|
|
114
|
+
assert str(key) == "<redacted>"
|
|
115
|
+
assert repr(key) == "Ed25519PrivateKey(<redacted>)"
|
|
116
|
+
|
|
117
|
+
# Get the strkey back only where you mean to.
|
|
118
|
+
assert key.reveal() == secret
|
|
119
|
+
|
|
120
|
+
try:
|
|
121
|
+
parse(secret)
|
|
122
|
+
except WrongStrkeyTypeError as error:
|
|
123
|
+
refused = error.found
|
|
124
|
+
assert refused is Ed25519PrivateKey
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
## Types
|
|
128
|
+
|
|
129
|
+
| Prefix | Class | Fields | `to_bytes()` / `from_bytes()` layout |
|
|
130
|
+
| ------ | ---------------------- | ---------------- | ------------------------------------- |
|
|
131
|
+
| `G` | `Ed25519PublicKey` | `key` | 32-byte key |
|
|
132
|
+
| `S` | `Ed25519PrivateKey` | `seed` | 32-byte seed |
|
|
133
|
+
| `M` | `MuxedEd25519Account` | `key`, `id` | 32-byte key, 8-byte big-endian ID |
|
|
134
|
+
| `P` | `Ed25519SignedPayload` | `key`, `payload` | 32-byte key, XDR `opaque<64>` payload |
|
|
135
|
+
| `T` | `PreAuthTx` | `hash` | 32-byte hash |
|
|
136
|
+
| `X` | `HashX` | `hash` | 32-byte hash |
|
|
137
|
+
| `C` | `Contract` | `hash` | 32-byte hash |
|
|
138
|
+
| `L` | `LiquidityPool` | `hash` | 32-byte hash |
|
|
139
|
+
| `B` | `ClaimableBalance` | `hash` | `0x00` version byte, 32-byte hash |
|
|
140
|
+
| `W` | `MuxedContract` | `hash`, `id` | 32-byte hash, 8-byte big-endian ID |
|
|
141
|
+
|
|
142
|
+
See the guide for how to handle errors, and for what private keys are protected
|
|
143
|
+
against and what they aren't.
|
|
144
|
+
|
|
145
|
+
## Development
|
|
146
|
+
|
|
147
|
+
```shell
|
|
148
|
+
uv sync --all-groups # create the environment, including the docs tools
|
|
149
|
+
uv run prek install # optional: check before each commit
|
|
150
|
+
make check # format check, lint, type checks and tests with coverage
|
|
151
|
+
make docs-serve # preview the documentation
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
`tests/vectors/strkeys.json` holds the shared test vectors in a language neutral form.
|
|
155
|
+
Add new cases there by hand, with the expected result that SEP-23 requires.
|
|
156
|
+
|
|
157
|
+
To release, run `make release-prep VERSION=<version>`, which sets the version and adds the
|
|
158
|
+
commits since the last release to `CHANGELOG.md`. Edit the new section where a commit
|
|
159
|
+
title isn't enough, commit it, then push a `v<version>` tag. The publish workflow verifies
|
|
160
|
+
that the tag matches the version and publishes to PyPI with trusted publishing, along with
|
|
161
|
+
PEP 740 attestations.
|
|
162
|
+
|
|
163
|
+
## License
|
|
164
|
+
|
|
165
|
+
[Apache-2.0](https://github.com/lightsail-network/py-stellar-strkey/blob/main/LICENSE)
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
# stellar-strkey
|
|
2
|
+
|
|
3
|
+
Encode and decode [Stellar strkeys](https://github.com/stellar/stellar-protocol/blob/master/ecosystem/sep-0023.md),
|
|
4
|
+
the `G...`, `C...` and `M...` strings that identify accounts, contracts, signers and
|
|
5
|
+
other things on the Stellar network.
|
|
6
|
+
|
|
7
|
+
- **Complete.** Supports every strkey type defined by SEP-23.
|
|
8
|
+
- **Strict.** Accepts a strkey only in its one canonical form and rejects everything else
|
|
9
|
+
with a clear error, so invalid input never gets through.
|
|
10
|
+
- **Thoroughly tested.** Verified against the SEP-23 test cases and a shared set of test
|
|
11
|
+
vectors, with property-based tests and 100% branch coverage.
|
|
12
|
+
- **Safe with secrets.** Secret seeds are never printed or serialized by accident, and
|
|
13
|
+
error messages never include the input.
|
|
14
|
+
- **Lightweight.** Fully typed, pure Python, no dependencies.
|
|
15
|
+
|
|
16
|
+
The [documentation](https://stellar-strkey.readthedocs.io/) has the guide and the API
|
|
17
|
+
reference.
|
|
18
|
+
|
|
19
|
+
## Install
|
|
20
|
+
|
|
21
|
+
```shell
|
|
22
|
+
pip install stellar-strkey
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## Usage
|
|
26
|
+
|
|
27
|
+
```python
|
|
28
|
+
from stellar_strkey import (
|
|
29
|
+
Contract,
|
|
30
|
+
Ed25519PublicKey,
|
|
31
|
+
MuxedEd25519Account,
|
|
32
|
+
StrkeyError,
|
|
33
|
+
parse,
|
|
34
|
+
)
|
|
35
|
+
|
|
36
|
+
# When you know the type, use its class. Anything else is rejected.
|
|
37
|
+
key = Ed25519PublicKey.from_string(
|
|
38
|
+
"GA7QYNF7SOWQ3GLR2BGMZEHXAVIRZA4KVWLTJJFC7MGXUA74P7UJVSGZ"
|
|
39
|
+
)
|
|
40
|
+
assert key.key.hex() == (
|
|
41
|
+
"3f0c34bf93ad0d9971d04ccc90f705511c838aad9734a4a2fb0d7a03fc7fe89a"
|
|
42
|
+
)
|
|
43
|
+
# The same 32 bytes as a contract give a different strkey.
|
|
44
|
+
assert str(Contract(key.key)) == (
|
|
45
|
+
"CA7QYNF7SOWQ3GLR2BGMZEHXAVIRZA4KVWLTJJFC7MGXUA74P7UJUWDA"
|
|
46
|
+
)
|
|
47
|
+
|
|
48
|
+
# When you don't, parse() returns the matching class.
|
|
49
|
+
match parse("MA7QYNF7SOWQ3GLR2BGMZEHXAVIRZA4KVWLTJJFC7MGXUA74P7UJVAAAAAAAAAAAAAJLK"):
|
|
50
|
+
case Ed25519PublicKey(key):
|
|
51
|
+
description = f"account {key.hex()}"
|
|
52
|
+
case MuxedEd25519Account(key, id):
|
|
53
|
+
description = f"muxed account {key.hex()} with ID {id}"
|
|
54
|
+
case other:
|
|
55
|
+
description = f"something else: {other!r}"
|
|
56
|
+
assert description.endswith("with ID 9223372036854775808")
|
|
57
|
+
|
|
58
|
+
# Invalid input raises StrkeyError, which is a ValueError.
|
|
59
|
+
try:
|
|
60
|
+
parse("GA7QYNF7SOWQ3GLR2BGMZEHXAVIRZA4KVWLTJJFC7MGXUA74P7UJVSGA")
|
|
61
|
+
except StrkeyError as error:
|
|
62
|
+
message = str(error)
|
|
63
|
+
assert message == "strkey checksum does not match"
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
### Secret seeds
|
|
67
|
+
|
|
68
|
+
Secret seeds (`S...`) have their own parser, and `parse()` refuses them, so code that
|
|
69
|
+
handles arbitrary addresses can't pick up a secret by accident:
|
|
70
|
+
|
|
71
|
+
```python
|
|
72
|
+
from stellar_strkey import Ed25519PrivateKey, WrongStrkeyTypeError, parse
|
|
73
|
+
|
|
74
|
+
# In real code, load the secret from the environment or a secret store, never from
|
|
75
|
+
# source code. This is a well-known test seed.
|
|
76
|
+
secret = "SBU2RRGLXH3E5CQHTD3ODLDF2BWDCYUSSBLLZ5GNW7JXHDIYKXZWHOKR"
|
|
77
|
+
|
|
78
|
+
key = Ed25519PrivateKey.from_string(secret)
|
|
79
|
+
seed = key.seed # the raw 32 bytes, for example for an ed25519 signing library
|
|
80
|
+
assert len(seed) == 32
|
|
81
|
+
|
|
82
|
+
# Printing or logging the key never shows the seed.
|
|
83
|
+
assert str(key) == "<redacted>"
|
|
84
|
+
assert repr(key) == "Ed25519PrivateKey(<redacted>)"
|
|
85
|
+
|
|
86
|
+
# Get the strkey back only where you mean to.
|
|
87
|
+
assert key.reveal() == secret
|
|
88
|
+
|
|
89
|
+
try:
|
|
90
|
+
parse(secret)
|
|
91
|
+
except WrongStrkeyTypeError as error:
|
|
92
|
+
refused = error.found
|
|
93
|
+
assert refused is Ed25519PrivateKey
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
## Types
|
|
97
|
+
|
|
98
|
+
| Prefix | Class | Fields | `to_bytes()` / `from_bytes()` layout |
|
|
99
|
+
| ------ | ---------------------- | ---------------- | ------------------------------------- |
|
|
100
|
+
| `G` | `Ed25519PublicKey` | `key` | 32-byte key |
|
|
101
|
+
| `S` | `Ed25519PrivateKey` | `seed` | 32-byte seed |
|
|
102
|
+
| `M` | `MuxedEd25519Account` | `key`, `id` | 32-byte key, 8-byte big-endian ID |
|
|
103
|
+
| `P` | `Ed25519SignedPayload` | `key`, `payload` | 32-byte key, XDR `opaque<64>` payload |
|
|
104
|
+
| `T` | `PreAuthTx` | `hash` | 32-byte hash |
|
|
105
|
+
| `X` | `HashX` | `hash` | 32-byte hash |
|
|
106
|
+
| `C` | `Contract` | `hash` | 32-byte hash |
|
|
107
|
+
| `L` | `LiquidityPool` | `hash` | 32-byte hash |
|
|
108
|
+
| `B` | `ClaimableBalance` | `hash` | `0x00` version byte, 32-byte hash |
|
|
109
|
+
| `W` | `MuxedContract` | `hash`, `id` | 32-byte hash, 8-byte big-endian ID |
|
|
110
|
+
|
|
111
|
+
See the guide for how to handle errors, and for what private keys are protected
|
|
112
|
+
against and what they aren't.
|
|
113
|
+
|
|
114
|
+
## Development
|
|
115
|
+
|
|
116
|
+
```shell
|
|
117
|
+
uv sync --all-groups # create the environment, including the docs tools
|
|
118
|
+
uv run prek install # optional: check before each commit
|
|
119
|
+
make check # format check, lint, type checks and tests with coverage
|
|
120
|
+
make docs-serve # preview the documentation
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
`tests/vectors/strkeys.json` holds the shared test vectors in a language neutral form.
|
|
124
|
+
Add new cases there by hand, with the expected result that SEP-23 requires.
|
|
125
|
+
|
|
126
|
+
To release, run `make release-prep VERSION=<version>`, which sets the version and adds the
|
|
127
|
+
commits since the last release to `CHANGELOG.md`. Edit the new section where a commit
|
|
128
|
+
title isn't enough, commit it, then push a `v<version>` tag. The publish workflow verifies
|
|
129
|
+
that the tag matches the version and publishes to PyPI with trusted publishing, along with
|
|
130
|
+
PEP 740 attestations.
|
|
131
|
+
|
|
132
|
+
## License
|
|
133
|
+
|
|
134
|
+
[Apache-2.0](https://github.com/lightsail-network/py-stellar-strkey/blob/main/LICENSE)
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# API reference
|
|
2
|
+
|
|
3
|
+
## Parsing
|
|
4
|
+
|
|
5
|
+
::: stellar_strkey.parse
|
|
6
|
+
|
|
7
|
+
::: stellar_strkey.Strkey
|
|
8
|
+
|
|
9
|
+
## Strkey types
|
|
10
|
+
|
|
11
|
+
::: stellar_strkey.Ed25519PublicKey
|
|
12
|
+
|
|
13
|
+
::: stellar_strkey.Ed25519PrivateKey
|
|
14
|
+
|
|
15
|
+
::: stellar_strkey.MuxedEd25519Account
|
|
16
|
+
|
|
17
|
+
::: stellar_strkey.Ed25519SignedPayload
|
|
18
|
+
|
|
19
|
+
::: stellar_strkey.PreAuthTx
|
|
20
|
+
|
|
21
|
+
::: stellar_strkey.HashX
|
|
22
|
+
|
|
23
|
+
::: stellar_strkey.Contract
|
|
24
|
+
|
|
25
|
+
::: stellar_strkey.LiquidityPool
|
|
26
|
+
|
|
27
|
+
::: stellar_strkey.ClaimableBalance
|
|
28
|
+
|
|
29
|
+
::: stellar_strkey.MuxedContract
|
|
30
|
+
|
|
31
|
+
## Errors
|
|
32
|
+
|
|
33
|
+
::: stellar_strkey.StrkeyError
|
|
34
|
+
|
|
35
|
+
::: stellar_strkey.MalformedStrkeyError
|
|
36
|
+
|
|
37
|
+
::: stellar_strkey.ChecksumMismatchError
|
|
38
|
+
|
|
39
|
+
::: stellar_strkey.WrongStrkeyTypeError
|
|
40
|
+
|
|
41
|
+
::: stellar_strkey.UnsupportedVersionError
|