@@ -7,6 +7,7 @@ const crypto = require('libp2p-crypto')
7
7
const PeerId = require ( 'peer-id' )
8
8
const multihash = require ( 'multihashes' )
9
9
const errCode = require ( 'err-code' )
10
+ const promisify = require ( 'promisify-es6' )
10
11
11
12
const debug = require ( 'debug' )
12
13
const log = debug ( 'jsipns' )
@@ -91,13 +92,15 @@ const validate = async (publicKey, entry) => {
91
92
// Validate Signature
92
93
let isValid
93
94
try {
94
- isValid = await publicKey . verify ( dataForSignature , entry . signature )
95
+ isValid = await promisify ( publicKey . verify , {
96
+ context : publicKey
97
+ } ) ( dataForSignature , entry . signature )
95
98
} catch ( err ) {
96
99
isValid = false
97
100
}
98
101
if ( ! isValid ) {
99
102
log . error ( 'record signature verification failed' )
100
- throw errCode ( 'record signature verification failed' , ERRORS . ERR_SIGNATURE_VERIFICATION )
103
+ throw errCode ( new Error ( 'record signature verification failed' ) , ERRORS . ERR_SIGNATURE_VERIFICATION )
101
104
}
102
105
103
106
// Validate according to the validity type
@@ -108,16 +111,16 @@ const validate = async (publicKey, entry) => {
108
111
validityDate = parseRFC3339 ( validity . toString ( ) )
109
112
} catch ( e ) {
110
113
log . error ( 'unrecognized validity format (not an rfc3339 format)' )
111
- throw errCode ( 'unrecognized validity format (not an rfc3339 format)' , ERRORS . ERR_UNRECOGNIZED_FORMAT )
114
+ throw errCode ( new Error ( 'unrecognized validity format (not an rfc3339 format)' ) , ERRORS . ERR_UNRECOGNIZED_FORMAT )
112
115
}
113
116
114
117
if ( validityDate < Date . now ( ) ) {
115
118
log . error ( 'record has expired' )
116
- throw errCode ( 'record has expired' , ERRORS . ERR_IPNS_EXPIRED_RECORD )
119
+ throw errCode ( new Error ( 'record has expired' ) , ERRORS . ERR_IPNS_EXPIRED_RECORD )
117
120
}
118
121
} else if ( validityType ) {
119
122
log . error ( 'unrecognized validity type' )
120
- throw errCode ( 'unrecognized validity type' , ERRORS . ERR_UNRECOGNIZED_VALIDITY )
123
+ throw errCode ( new Error ( 'unrecognized validity type' ) , ERRORS . ERR_UNRECOGNIZED_VALIDITY )
121
124
}
122
125
123
126
log ( `ipns entry for ${ value } is valid` )
@@ -138,15 +141,15 @@ const validate = async (publicKey, entry) => {
138
141
*/
139
142
const embedPublicKey = async ( publicKey , entry ) => {
140
143
if ( ! publicKey || ! publicKey . bytes || ! entry ) {
141
- const error = 'one or more of the provided parameters are not defined'
144
+ const error = new Error ( 'one or more of the provided parameters are not defined' )
142
145
log . error ( error )
143
146
throw errCode ( error , ERRORS . ERR_UNDEFINED_PARAMETER )
144
147
}
145
148
146
149
// Create a peer id from the public key.
147
150
let peerId
148
151
try {
149
- peerId = await PeerId . createFromPubKey ( publicKey . bytes )
152
+ peerId = await promisify ( PeerId . createFromPubKey ) ( publicKey . bytes )
150
153
} catch ( err ) {
151
154
throw errCode ( err , ERRORS . ERR_PEER_ID_FROM_PUBLIC_KEY )
152
155
}
@@ -183,7 +186,7 @@ const embedPublicKey = async (publicKey, entry) => {
183
186
*/
184
187
const extractPublicKey = ( peerId , entry ) => {
185
188
if ( ! entry || ! peerId ) {
186
- const error = 'one or more of the provided parameters are not defined'
189
+ const error = new Error ( 'one or more of the provided parameters are not defined' )
187
190
188
191
log . error ( error )
189
192
throw errCode ( error , ERRORS . ERR_UNDEFINED_PARAMETER )
@@ -241,10 +244,13 @@ const getIdKeys = (pid) => {
241
244
const sign = ( privateKey , value , validityType , validity ) => {
242
245
try {
243
246
const dataForSignature = ipnsEntryDataForSig ( value , validityType , validity )
244
- return privateKey . sign ( dataForSignature )
247
+
248
+ return promisify ( privateKey . sign , {
249
+ context : privateKey
250
+ } ) ( dataForSignature )
245
251
} catch ( error ) {
246
252
log . error ( 'record signature creation failed' )
247
- throw errCode ( 'record signature creation failed: ' + error . message , ERRORS . ERR_SIGNATURE_CREATION )
253
+ throw errCode ( new Error ( 'record signature creation failed: ' + error . message ) , ERRORS . ERR_SIGNATURE_CREATION )
248
254
}
249
255
}
250
256
@@ -254,7 +260,7 @@ const getValidityType = (validityType) => {
254
260
return 'EOL'
255
261
}
256
262
257
- const error = `unrecognized validity type ${ validityType . toString ( ) } `
263
+ const error = new Error ( `unrecognized validity type ${ validityType . toString ( ) } ` )
258
264
log . error ( error )
259
265
throw errCode ( error , ERRORS . ERR_UNRECOGNIZED_VALIDITY )
260
266
}
@@ -287,9 +293,7 @@ const validator = {
287
293
validate : async ( marshalledData , key ) => {
288
294
const receivedEntry = unmarshal ( marshalledData )
289
295
const bufferId = key . slice ( '/ipns/' . length )
290
- let peerId
291
-
292
- peerId = PeerId . createFromBytes ( bufferId )
296
+ const peerId = PeerId . createFromBytes ( bufferId )
293
297
294
298
// extract public key
295
299
const pubKey = extractPublicKey ( peerId , receivedEntry )
0 commit comments