1
- import errcode from 'err-code '
1
+ import { CodeError } from '@libp2p/interfaces/errors '
2
2
import webcrypto from '../webcrypto.js'
3
3
import { base64urlToBuffer } from '../util.js'
4
4
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
@@ -17,7 +17,7 @@ const names = curveTypes.join(' / ')
17
17
18
18
export async function generateEphmeralKeyPair ( curve : string ) {
19
19
if ( curve !== 'P-256' && curve !== 'P-384' && curve !== 'P-521' ) {
20
- throw errcode ( new Error ( `Unknown curve: ${ curve } . Must be ${ names } ` ) , 'ERR_INVALID_CURVE' )
20
+ throw new CodeError ( `Unknown curve: ${ curve } . Must be ${ names } ` , 'ERR_INVALID_CURVE' )
21
21
}
22
22
23
23
const pair = await webcrypto . get ( ) . subtle . generateKey (
@@ -94,11 +94,11 @@ const curveLengths = {
94
94
// go-ipfs uses)
95
95
function marshalPublicKey ( jwk : JsonWebKey ) {
96
96
if ( jwk . crv == null || jwk . x == null || jwk . y == null ) {
97
- throw errcode ( new Error ( 'JWK was missing components' ) , 'ERR_INVALID_PARAMETERS' )
97
+ throw new CodeError ( 'JWK was missing components' , 'ERR_INVALID_PARAMETERS' )
98
98
}
99
99
100
100
if ( jwk . crv !== 'P-256' && jwk . crv !== 'P-384' && jwk . crv !== 'P-521' ) {
101
- throw errcode ( new Error ( `Unknown curve: ${ jwk . crv } . Must be ${ names } ` ) , 'ERR_INVALID_CURVE' )
101
+ throw new CodeError ( `Unknown curve: ${ jwk . crv } . Must be ${ names } ` , 'ERR_INVALID_CURVE' )
102
102
}
103
103
104
104
const byteLen = curveLengths [ jwk . crv ]
@@ -113,13 +113,13 @@ function marshalPublicKey (jwk: JsonWebKey) {
113
113
// Unmarshal converts a point, serialized by Marshal, into an jwk encoded key
114
114
function unmarshalPublicKey ( curve : string , key : Uint8Array ) {
115
115
if ( curve !== 'P-256' && curve !== 'P-384' && curve !== 'P-521' ) {
116
- throw errcode ( new Error ( `Unknown curve: ${ curve } . Must be ${ names } ` ) , 'ERR_INVALID_CURVE' )
116
+ throw new CodeError ( `Unknown curve: ${ curve } . Must be ${ names } ` , 'ERR_INVALID_CURVE' )
117
117
}
118
118
119
119
const byteLen = curveLengths [ curve ]
120
120
121
121
if ( ! uint8ArrayEquals ( key . subarray ( 0 , 1 ) , Uint8Array . from ( [ 4 ] ) ) ) {
122
- throw errcode ( new Error ( 'Cannot unmarshal public key - invalid key format' ) , 'ERR_INVALID_KEY_FORMAT' )
122
+ throw new CodeError ( 'Cannot unmarshal public key - invalid key format' , 'ERR_INVALID_KEY_FORMAT' )
123
123
}
124
124
125
125
return {
0 commit comments