@@ -76,44 +76,44 @@ const _create = async (privateKey, value, seq, isoValidity, validityType) => {
76
76
*
77
77
* @param {Object } publicKey public key for validating the record.
78
78
* @param {Object } entry ipns entry record.
79
- * @returns {Promise } the promise will reject if the entry is invalid.
80
79
*/
81
- const validate = ( publicKey , entry ) => {
80
+ const validate = async ( publicKey , entry ) => {
82
81
const { value, validityType, validity } = entry
83
82
const dataForSignature = ipnsEntryDataForSig ( value , validityType , validity )
84
83
85
- return new Promise ( ( resolve , reject ) => {
86
- // Validate Signature
87
- publicKey . verify ( dataForSignature , entry . signature , ( err , isValid ) => {
88
- if ( err || ! isValid ) {
89
- log . error ( 'record signature verification failed' )
90
- return reject ( errorWithCode ( 'record signature verification failed' , ERRORS . ERR_SIGNATURE_VERIFICATION ) )
91
- }
92
-
93
- // Validate according to the validity type
94
- if ( validityType === ipnsEntryProto . ValidityType . EOL ) {
95
- let validityDate
96
-
97
- try {
98
- validityDate = parseRFC3339 ( validity . toString ( ) )
99
- } catch ( e ) {
100
- log . error ( 'unrecognized validity format (not an rfc3339 format)' )
101
- return reject ( errorWithCode ( 'unrecognized validity format (not an rfc3339 format)' , ERRORS . ERR_UNRECOGNIZED_FORMAT ) )
102
- }
103
-
104
- if ( validityDate < Date . now ( ) ) {
105
- log . error ( 'record has expired' )
106
- return reject ( errorWithCode ( 'record has expired' , ERRORS . ERR_IPNS_EXPIRED_RECORD ) )
107
- }
108
- } else if ( validityType ) {
109
- log . error ( 'unrecognized validity type' )
110
- return reject ( errorWithCode ( 'unrecognized validity type' , ERRORS . ERR_UNRECOGNIZED_VALIDITY ) )
111
- }
112
-
113
- log ( `ipns entry for ${ value } is valid` )
114
- resolve ( )
115
- } )
116
- } )
84
+ // Validate Signature
85
+ let isValid
86
+ try {
87
+ isValid = await publicKey . verify ( dataForSignature , entry . signature )
88
+ } catch ( err ) {
89
+ isValid = false
90
+ }
91
+ if ( ! isValid ) {
92
+ log . error ( 'record signature verification failed' )
93
+ throw errorWithCode ( 'record signature verification failed' , ERRORS . ERR_SIGNATURE_VERIFICATION )
94
+ }
95
+
96
+ // Validate according to the validity type
97
+ if ( validityType === ipnsEntryProto . ValidityType . EOL ) {
98
+ let validityDate
99
+
100
+ try {
101
+ validityDate = parseRFC3339 ( validity . toString ( ) )
102
+ } catch ( e ) {
103
+ log . error ( 'unrecognized validity format (not an rfc3339 format)' )
104
+ throw errorWithCode ( 'unrecognized validity format (not an rfc3339 format)' , ERRORS . ERR_UNRECOGNIZED_FORMAT )
105
+ }
106
+
107
+ if ( validityDate < Date . now ( ) ) {
108
+ log . error ( 'record has expired' )
109
+ throw errorWithCode ( 'record has expired' , ERRORS . ERR_IPNS_EXPIRED_RECORD )
110
+ }
111
+ } else if ( validityType ) {
112
+ log . error ( 'unrecognized validity type' )
113
+ throw errorWithCode ( 'unrecognized validity type' , ERRORS . ERR_UNRECOGNIZED_VALIDITY )
114
+ }
115
+
116
+ log ( `ipns entry for ${ value } is valid` )
117
117
}
118
118
119
119
/**
@@ -132,42 +132,39 @@ const validate = (publicKey, entry) => {
132
132
const embedPublicKey = async ( publicKey , entry ) => {
133
133
if ( ! publicKey || ! publicKey . bytes || ! entry ) {
134
134
const error = 'one or more of the provided parameters are not defined'
135
-
136
135
log . error ( error )
137
136
throw Object . assign ( new Error ( error ) , { code : ERRORS . ERR_UNDEFINED_PARAMETER } )
138
137
}
139
138
140
- return new Promise ( ( resolve , reject ) => {
141
- // Create a peer id from the public key.
142
- PeerId . createFromPubKey ( publicKey . bytes , ( err , peerId ) => {
143
- if ( err ) {
144
- log . error ( err )
145
- reject ( Object . assign ( new Error ( err ) , { code : ERRORS . ERR_PEER_ID_FROM_PUBLIC_KEY } ) )
146
- }
147
-
148
- // Try to extract the public key from the ID. If we can, no need to embed it
149
- let extractedPublicKey
150
- try {
151
- extractedPublicKey = extractPublicKeyFromId ( peerId )
152
- } catch ( err ) {
153
- log . error ( err )
154
- reject ( Object . assign ( new Error ( err ) , { code : ERRORS . ERR_PUBLIC_KEY_FROM_ID } ) )
155
- }
156
-
157
- if ( extractedPublicKey ) {
158
- return resolve ( null )
159
- }
160
-
161
- // If we failed to extract the public key from the peer ID, embed it in the record.
162
- try {
163
- entry . pubKey = crypto . keys . marshalPublicKey ( publicKey )
164
- } catch ( err ) {
165
- log . error ( err )
166
- reject ( err )
167
- }
168
- resolve ( entry )
169
- } )
170
- } )
139
+ // Create a peer id from the public key.
140
+ let peerId
141
+ try {
142
+ peerId = await PeerId . createFromPubKey ( publicKey . bytes )
143
+ } catch ( err ) {
144
+ throw Object . assign ( new Error ( err ) , { code : ERRORS . ERR_PEER_ID_FROM_PUBLIC_KEY } )
145
+ }
146
+
147
+ // Try to extract the public key from the ID. If we can, no need to embed it
148
+ let extractedPublicKey
149
+ try {
150
+ extractedPublicKey = extractPublicKeyFromId ( peerId )
151
+ } catch ( err ) {
152
+ log . error ( err )
153
+ throw Object . assign ( new Error ( err ) , { code : ERRORS . ERR_PUBLIC_KEY_FROM_ID } )
154
+ }
155
+
156
+ if ( extractedPublicKey ) {
157
+ return null
158
+ }
159
+
160
+ // If we failed to extract the public key from the peer ID, embed it in the record.
161
+ try {
162
+ entry . pubKey = crypto . keys . marshalPublicKey ( publicKey )
163
+ } catch ( err ) {
164
+ log . error ( err )
165
+ throw err
166
+ }
167
+ return entry
171
168
}
172
169
173
170
/**
@@ -232,21 +229,18 @@ const getIdKeys = (pid) => {
232
229
// Sign ipns record data
233
230
const sign = ( privateKey , value , validityType , validity ) => {
234
231
const dataForSignature = ipnsEntryDataForSig ( value , validityType , validity )
235
-
236
- return new Promise ( ( resolve , reject ) => {
237
- privateKey . sign ( dataForSignature , ( err , signature ) => err ? reject ( err ) : resolve ( signature ) )
238
- } )
232
+ return privateKey . sign ( dataForSignature )
239
233
}
240
234
241
235
// Utility for getting the validity type code name of a validity
242
236
const getValidityType = ( validityType ) => {
243
237
if ( validityType . toString ( ) === '0' ) {
244
238
return 'EOL'
245
- } else {
246
- const error = `unrecognized validity type ${ validityType . toString ( ) } `
247
- log . error ( error )
248
- throw Object . assign ( new Error ( error ) , { code : ERRORS . ERR_UNRECOGNIZED_VALIDITY } )
249
239
}
240
+
241
+ const error = `unrecognized validity type ${ validityType . toString ( ) } `
242
+ log . error ( error )
243
+ throw Object . assign ( new Error ( error ) , { code : ERRORS . ERR_UNRECOGNIZED_VALIDITY } )
250
244
}
251
245
252
246
// Utility for creating the record data for being signed
0 commit comments