@@ -6,6 +6,7 @@ const { Key } = require('interface-datastore')
6
6
const crypto = require ( 'libp2p-crypto' )
7
7
const PeerId = require ( 'peer-id' )
8
8
const multihash = require ( 'multihashes' )
9
+ const errCode = require ( 'err-code' )
9
10
10
11
const debug = require ( 'debug' )
11
12
const log = debug ( 'jsipns' )
@@ -67,7 +68,7 @@ const _create = async (privateKey, value, seq, isoValidity, validityType) => {
67
68
return entry
68
69
} catch ( error ) {
69
70
log . error ( 'record signature creation failed' )
70
- throw errorWithCode ( 'record signature verification failed' , ERRORS . ERR_SIGNATURE_CREATION )
71
+ throw errCode ( 'record signature verification failed' , ERRORS . ERR_SIGNATURE_CREATION )
71
72
}
72
73
}
73
74
@@ -90,7 +91,7 @@ const validate = async (publicKey, entry) => {
90
91
}
91
92
if ( ! isValid ) {
92
93
log . error ( 'record signature verification failed' )
93
- throw errorWithCode ( 'record signature verification failed' , ERRORS . ERR_SIGNATURE_VERIFICATION )
94
+ throw errCode ( 'record signature verification failed' , ERRORS . ERR_SIGNATURE_VERIFICATION )
94
95
}
95
96
96
97
// Validate according to the validity type
@@ -101,16 +102,16 @@ const validate = async (publicKey, entry) => {
101
102
validityDate = parseRFC3339 ( validity . toString ( ) )
102
103
} catch ( e ) {
103
104
log . error ( 'unrecognized validity format (not an rfc3339 format)' )
104
- throw errorWithCode ( 'unrecognized validity format (not an rfc3339 format)' , ERRORS . ERR_UNRECOGNIZED_FORMAT )
105
+ throw errCode ( 'unrecognized validity format (not an rfc3339 format)' , ERRORS . ERR_UNRECOGNIZED_FORMAT )
105
106
}
106
107
107
108
if ( validityDate < Date . now ( ) ) {
108
109
log . error ( 'record has expired' )
109
- throw errorWithCode ( 'record has expired' , ERRORS . ERR_IPNS_EXPIRED_RECORD )
110
+ throw errCode ( 'record has expired' , ERRORS . ERR_IPNS_EXPIRED_RECORD )
110
111
}
111
112
} else if ( validityType ) {
112
113
log . error ( 'unrecognized validity type' )
113
- throw errorWithCode ( 'unrecognized validity type' , ERRORS . ERR_UNRECOGNIZED_VALIDITY )
114
+ throw errCode ( 'unrecognized validity type' , ERRORS . ERR_UNRECOGNIZED_VALIDITY )
114
115
}
115
116
116
117
log ( `ipns entry for ${ value } is valid` )
@@ -133,15 +134,15 @@ const embedPublicKey = async (publicKey, entry) => {
133
134
if ( ! publicKey || ! publicKey . bytes || ! entry ) {
134
135
const error = 'one or more of the provided parameters are not defined'
135
136
log . error ( error )
136
- throw Object . assign ( new Error ( error ) , { code : ERRORS . ERR_UNDEFINED_PARAMETER } )
137
+ throw errCode ( error , ERRORS . ERR_UNDEFINED_PARAMETER )
137
138
}
138
139
139
140
// Create a peer id from the public key.
140
141
let peerId
141
142
try {
142
143
peerId = await PeerId . createFromPubKey ( publicKey . bytes )
143
144
} catch ( err ) {
144
- throw Object . assign ( new Error ( err ) , { code : ERRORS . ERR_PEER_ID_FROM_PUBLIC_KEY } )
145
+ throw errCode ( err , ERRORS . ERR_PEER_ID_FROM_PUBLIC_KEY )
145
146
}
146
147
147
148
// Try to extract the public key from the ID. If we can, no need to embed it
@@ -150,7 +151,7 @@ const embedPublicKey = async (publicKey, entry) => {
150
151
extractedPublicKey = extractPublicKeyFromId ( peerId )
151
152
} catch ( err ) {
152
153
log . error ( err )
153
- throw Object . assign ( new Error ( err ) , { code : ERRORS . ERR_PUBLIC_KEY_FROM_ID } )
154
+ throw errCode ( err , ERRORS . ERR_PUBLIC_KEY_FROM_ID )
154
155
}
155
156
156
157
if ( extractedPublicKey ) {
@@ -179,7 +180,7 @@ const extractPublicKey = (peerId, entry) => {
179
180
const error = 'one or more of the provided parameters are not defined'
180
181
181
182
log . error ( error )
182
- throw Object . assign ( new Error ( error ) , { code : ERRORS . ERR_UNDEFINED_PARAMETER } )
183
+ throw errCode ( error , ERRORS . ERR_UNDEFINED_PARAMETER )
183
184
}
184
185
185
186
if ( entry . pubKey ) {
@@ -240,7 +241,7 @@ const getValidityType = (validityType) => {
240
241
241
242
const error = `unrecognized validity type ${ validityType . toString ( ) } `
242
243
log . error ( error )
243
- throw Object . assign ( new Error ( error ) , { code : ERRORS . ERR_UNRECOGNIZED_VALIDITY } )
244
+ throw errCode ( error , ERRORS . ERR_UNRECOGNIZED_VALIDITY )
244
245
}
245
246
246
247
// Utility for creating the record data for being signed
@@ -263,8 +264,6 @@ const extractPublicKeyFromId = (peerId) => {
263
264
return crypto . keys . unmarshalPublicKey ( decodedId . digest )
264
265
}
265
266
266
- const errorWithCode = ( err , code ) => Object . assign ( new Error ( err ) , { code } )
267
-
268
267
const marshal = ipnsEntryProto . encode
269
268
270
269
const unmarshal = ipnsEntryProto . decode
0 commit comments