1- import errcode from 'err-code '
1+ import { CodeError } from '@libp2p/interfaces/errors '
22import webcrypto from '../webcrypto.js'
33import { base64urlToBuffer } from '../util.js'
44import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
@@ -17,7 +17,7 @@ const names = curveTypes.join(' / ')
1717
1818export async function generateEphmeralKeyPair ( curve : string ) {
1919 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' )
2121 }
2222
2323 const pair = await webcrypto . get ( ) . subtle . generateKey (
@@ -94,11 +94,11 @@ const curveLengths = {
9494// go-ipfs uses)
9595function marshalPublicKey ( jwk : JsonWebKey ) {
9696 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' )
9898 }
9999
100100 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' )
102102 }
103103
104104 const byteLen = curveLengths [ jwk . crv ]
@@ -113,13 +113,13 @@ function marshalPublicKey (jwk: JsonWebKey) {
113113// Unmarshal converts a point, serialized by Marshal, into an jwk encoded key
114114function unmarshalPublicKey ( curve : string , key : Uint8Array ) {
115115 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' )
117117 }
118118
119119 const byteLen = curveLengths [ curve ]
120120
121121 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' )
123123 }
124124
125125 return {
0 commit comments