@@ -4,7 +4,7 @@ import { base64urlToBuffer } from '../util.js'
4
4
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
5
5
import { concat as uint8ArrayConcat } from 'uint8arrays/concat'
6
6
import { equals as uint8ArrayEquals } from 'uint8arrays/equals'
7
- import type { ECDHKey , ECDHKeyPair } from './interface.js'
7
+ import type { ECDHKey , ECDHKeyPair , JWKEncodedPrivateKey , JWKEncodedPublicKey } from './interface.js'
8
8
9
9
const bits = {
10
10
'P-256' : 256 ,
@@ -15,7 +15,7 @@ const bits = {
15
15
const curveTypes = Object . keys ( bits )
16
16
const names = curveTypes . join ( ' / ' )
17
17
18
- export async function generateEphmeralKeyPair ( curve : string ) {
18
+ export async function generateEphmeralKeyPair ( curve : string ) : Promise < ECDHKey > {
19
19
if ( curve !== 'P-256' && curve !== 'P-384' && curve !== 'P-521' ) {
20
20
throw new CodeError ( `Unknown curve: ${ curve } . Must be ${ names } ` , 'ERR_INVALID_CURVE' )
21
21
}
@@ -30,7 +30,7 @@ export async function generateEphmeralKeyPair (curve: string) {
30
30
)
31
31
32
32
// forcePrivate is used for testing only
33
- const genSharedKey = async ( theirPub : Uint8Array , forcePrivate ?: ECDHKeyPair ) => {
33
+ const genSharedKey = async ( theirPub : Uint8Array , forcePrivate ?: ECDHKeyPair ) : Promise < Uint8Array > => {
34
34
let privateKey
35
35
36
36
if ( forcePrivate != null ) {
@@ -92,7 +92,7 @@ const curveLengths = {
92
92
// Marshal converts a jwk encoded ECDH public key into the
93
93
// form specified in section 4.3.6 of ANSI X9.62. (This is the format
94
94
// go-ipfs uses)
95
- function marshalPublicKey ( jwk : JsonWebKey ) {
95
+ function marshalPublicKey ( jwk : JsonWebKey ) : Uint8Array {
96
96
if ( jwk . crv == null || jwk . x == null || jwk . y == null ) {
97
97
throw new CodeError ( 'JWK was missing components' , 'ERR_INVALID_PARAMETERS' )
98
98
}
@@ -111,7 +111,7 @@ function marshalPublicKey (jwk: JsonWebKey) {
111
111
}
112
112
113
113
// Unmarshal converts a point, serialized by Marshal, into an jwk encoded key
114
- function unmarshalPublicKey ( curve : string , key : Uint8Array ) {
114
+ function unmarshalPublicKey ( curve : string , key : Uint8Array ) : JWKEncodedPublicKey {
115
115
if ( curve !== 'P-256' && curve !== 'P-384' && curve !== 'P-521' ) {
116
116
throw new CodeError ( `Unknown curve: ${ curve } . Must be ${ names } ` , 'ERR_INVALID_CURVE' )
117
117
}
@@ -131,7 +131,7 @@ function unmarshalPublicKey (curve: string, key: Uint8Array) {
131
131
}
132
132
}
133
133
134
- const unmarshalPrivateKey = ( curve : string , key : ECDHKeyPair ) => ( {
134
+ const unmarshalPrivateKey = ( curve : string , key : ECDHKeyPair ) : JWKEncodedPrivateKey => ( {
135
135
...unmarshalPublicKey ( curve , key . public ) ,
136
136
d : uint8ArrayToString ( key . private , 'base64url' )
137
137
} )
0 commit comments