This repository was archived by the owner on Jul 21, 2023. It is now read-only.
File tree 2 files changed +28
-4
lines changed
2 files changed +28
-4
lines changed Original file line number Diff line number Diff line change @@ -13,10 +13,25 @@ const SIGNATURE_BYTE_LENGTH = 64
13
13
export { PUBLIC_KEY_BYTE_LENGTH as publicKeyLength }
14
14
export { PRIVATE_KEY_BYTE_LENGTH as privateKeyLength }
15
15
16
- function derivePublicKey ( privateKey : Uint8Array ) {
17
- const hash = crypto . createHash ( 'sha512' )
18
- hash . update ( privateKey )
19
- return hash . digest ( ) . subarray ( 32 )
16
+ function derivePublicKey ( privateKey : Uint8Array ) : Uint8Array {
17
+ const keyObject = crypto . createPrivateKey ( {
18
+ format : 'jwk' ,
19
+ key : {
20
+ crv : 'Ed25519' ,
21
+ x : '' ,
22
+ d : uint8arrayToString ( privateKey , 'base64url' ) ,
23
+ kty : 'OKP'
24
+ }
25
+ } )
26
+ const jwk = keyObject . export ( {
27
+ format : 'jwk'
28
+ } )
29
+
30
+ if ( jwk . x == null || jwk . x === '' ) {
31
+ throw new Error ( 'Could not export JWK public key' )
32
+ }
33
+
34
+ return uint8arrayFromString ( jwk . x , 'base64url' )
20
35
}
21
36
22
37
export async function generateKey ( ) {
Original file line number Diff line number Diff line change @@ -148,6 +148,15 @@ describe('ed25519', function () {
148
148
expect ( valid ) . to . eql ( true )
149
149
} )
150
150
151
+ it ( 'sign and verify from seed' , async ( ) => {
152
+ const seed = new Uint8Array ( 32 ) . fill ( 1 )
153
+ const seededkey = await crypto . keys . generateKeyPairFromSeed ( 'Ed25519' , seed )
154
+ const data = uint8ArrayFromString ( 'hello world' )
155
+ const sig = await seededkey . sign ( data )
156
+ const valid = await seededkey . public . verify ( data , sig )
157
+ expect ( valid ) . to . eql ( true )
158
+ } )
159
+
151
160
it ( 'fails to verify for different data' , async ( ) => {
152
161
const data = uint8ArrayFromString ( 'hello world' )
153
162
const sig = await key . sign ( data )
You can’t perform that action at this time.
0 commit comments