5
5
import json
6
6
import os
7
7
8
- from nacl .bindings import crypto_sign_PUBLICKEYBYTES , crypto_sign_SEEDBYTES
9
8
from nacl .encoding import RawEncoder
10
9
from nacl .hash import blake2b
11
10
from nacl .public import PrivateKey
12
11
from nacl .signing import SigningKey as NACLSigningKey
13
12
13
+ from pycardano .crypto .bip32 import BIP32ED25519PrivateKey
14
14
from pycardano .exception import InvalidKeyTypeException
15
15
from pycardano .hash import VERIFICATION_KEY_HASH_SIZE , VerificationKeyHash
16
16
from pycardano .serialization import CBORSerializable
17
17
18
18
__all__ = [
19
19
"Key" ,
20
+ "ExtendedSigningKey" ,
21
+ "ExtendedVerificationKey" ,
20
22
"VerificationKey" ,
21
23
"SigningKey" ,
24
+ "PaymentExtendedSigningKey" ,
25
+ "PaymentExtendedVerificationKey" ,
22
26
"PaymentSigningKey" ,
23
27
"PaymentVerificationKey" ,
24
28
"PaymentKeyPair" ,
29
+ "StakeExtendedSigningKey" ,
30
+ "StakeExtendedVerificationKey" ,
25
31
"StakeSigningKey" ,
26
32
"StakeVerificationKey" ,
27
33
"StakeKeyPair" ,
@@ -132,16 +138,21 @@ def __repr__(self) -> str:
132
138
return self .to_json ()
133
139
134
140
135
- class VerificationKey (Key ):
141
+ class SigningKey (Key ):
142
+ def sign (self , data : bytes ) -> bytes :
143
+ signed_message = NACLSigningKey (self .payload ).sign (data )
144
+ return signed_message .signature
145
+
146
+ @classmethod
147
+ def generate (cls ) -> SigningKey :
148
+ signing_key = PrivateKey .generate ()
149
+ return cls (bytes (signing_key ))
136
150
137
- SIZE = crypto_sign_PUBLICKEYBYTES
138
151
152
+ class VerificationKey (Key ):
139
153
def hash (self ) -> VerificationKeyHash :
140
154
"""Compute a blake2b hash from the key
141
155
142
- Args:
143
- hash_size: Size of the hash output in bytes.
144
-
145
156
Returns:
146
157
VerificationKeyHash: Hash output in bytes.
147
158
"""
@@ -152,33 +163,65 @@ def hash(self) -> VerificationKeyHash:
152
163
@classmethod
153
164
def from_signing_key (cls , key : SigningKey ) -> VerificationKey :
154
165
verification_key = NACLSigningKey (bytes (key )).verify_key
155
- return cls (bytes (verification_key ))
166
+ return cls (
167
+ bytes (verification_key ),
168
+ key .key_type .replace ("Signing" , "Verification" ),
169
+ key .description .replace ("Signing" , "Verification" ),
170
+ )
156
171
157
172
158
- class SigningKey (Key ):
173
+ class ExtendedSigningKey (Key ):
174
+ def sign (self , data : bytes ) -> bytes :
175
+ private_key = BIP32ED25519PrivateKey (self .payload [:64 ], self .payload [96 :])
176
+ return private_key .sign (data )
159
177
160
- SIZE = crypto_sign_SEEDBYTES
161
178
162
- def sign (self , data : bytes ) -> bytes :
163
- signed_message = NACLSigningKey (self .payload ).sign (data )
164
- return signed_message .signature
179
+ class ExtendedVerificationKey (Key ):
180
+ def hash (self ) -> VerificationKeyHash :
181
+ """Compute a blake2b hash from the key, excluding chain code
182
+
183
+ Returns:
184
+ VerificationKeyHash: Hash output in bytes.
185
+ """
186
+ return self .to_non_extended ().hash ()
165
187
166
188
@classmethod
167
- def generate (cls ) -> SigningKey :
168
- signing_key = PrivateKey .generate ()
169
- return cls (bytes (signing_key ))
189
+ def from_signing_key (cls , key : ExtendedSigningKey ) -> ExtendedVerificationKey :
190
+ return cls (
191
+ key .payload [64 :],
192
+ key .key_type .replace ("Signing" , "Verification" ),
193
+ key .description .replace ("Signing" , "Verification" ),
194
+ )
195
+
196
+ def to_non_extended (self ) -> VerificationKey :
197
+ """Get the 32-byte verification with chain code trimmed off
198
+
199
+ Returns:
200
+ VerificationKey: 32-byte verification with chain code trimmed off
201
+ """
202
+ return VerificationKey (self .payload [:32 ])
170
203
171
204
172
205
class PaymentSigningKey (SigningKey ):
173
206
KEY_TYPE = "PaymentSigningKeyShelley_ed25519"
174
- DESCRIPTION = "Payment Verification Key"
207
+ DESCRIPTION = "Payment Signing Key"
175
208
176
209
177
210
class PaymentVerificationKey (VerificationKey ):
178
211
KEY_TYPE = "PaymentVerificationKeyShelley_ed25519"
179
212
DESCRIPTION = "Payment Verification Key"
180
213
181
214
215
+ class PaymentExtendedSigningKey (ExtendedSigningKey ):
216
+ KEY_TYPE = "PaymentExtendedSigningKeyShelley_ed25519_bip32"
217
+ DESCRIPTION = "Payment Signing Key"
218
+
219
+
220
+ class PaymentExtendedVerificationKey (ExtendedVerificationKey ):
221
+ KEY_TYPE = "PaymentExtendedVerificationKeyShelley_ed25519_bip32"
222
+ DESCRIPTION = "Payment Verification Key"
223
+
224
+
182
225
class PaymentKeyPair :
183
226
def __init__ (
184
227
self , signing_key : PaymentSigningKey , verification_key : PaymentVerificationKey
@@ -205,14 +248,24 @@ def __eq__(self, other):
205
248
206
249
class StakeSigningKey (SigningKey ):
207
250
KEY_TYPE = "StakeSigningKeyShelley_ed25519"
208
- DESCRIPTION = "Stake Verification Key"
251
+ DESCRIPTION = "Stake Signing Key"
209
252
210
253
211
254
class StakeVerificationKey (VerificationKey ):
212
255
KEY_TYPE = "StakeVerificationKeyShelley_ed25519"
213
256
DESCRIPTION = "Stake Verification Key"
214
257
215
258
259
+ class StakeExtendedSigningKey (ExtendedSigningKey ):
260
+ KEY_TYPE = "StakeExtendedSigningKeyShelley_ed25519_bip32"
261
+ DESCRIPTION = "Stake Signing Key"
262
+
263
+
264
+ class StakeExtendedVerificationKey (ExtendedVerificationKey ):
265
+ KEY_TYPE = "StakeExtendedVerificationKeyShelley_ed25519_bip32"
266
+ DESCRIPTION = "Stake Verification Key"
267
+
268
+
216
269
class StakeKeyPair :
217
270
def __init__ (
218
271
self , signing_key : StakeSigningKey , verification_key : StakeVerificationKey
0 commit comments