Skip to content

Commit 65d80f6

Browse files
committed
chore: depend on async branch of crypto / peer-id
1 parent 9f13397 commit 65d80f6

File tree

3 files changed

+73
-83
lines changed

3 files changed

+73
-83
lines changed

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@
3737
"debug": "^4.1.1",
3838
"interface-datastore": "~0.6.0",
3939
"left-pad": "^1.3.0",
40-
"libp2p-crypto": "~0.16.0",
40+
"libp2p-crypto": "libp2p/js-libp2p-crypto#feat/async-await",
4141
"multihashes": "~0.4.14",
42-
"peer-id": "~0.12.2",
42+
"peer-id": "libp2p/js-peer-id#feat/async-await",
4343
"protons": "^1.0.1",
4444
"timestamp-nano": "^1.0.0"
4545
},

src/index.js

+68-74
Original file line numberDiff line numberDiff line change
@@ -76,44 +76,44 @@ const _create = async (privateKey, value, seq, isoValidity, validityType) => {
7676
*
7777
* @param {Object} publicKey public key for validating the record.
7878
* @param {Object} entry ipns entry record.
79-
* @returns {Promise} the promise will reject if the entry is invalid.
8079
*/
81-
const validate = (publicKey, entry) => {
80+
const validate = async (publicKey, entry) => {
8281
const { value, validityType, validity } = entry
8382
const dataForSignature = ipnsEntryDataForSig(value, validityType, validity)
8483

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`)
117117
}
118118

119119
/**
@@ -132,42 +132,39 @@ const validate = (publicKey, entry) => {
132132
const embedPublicKey = async (publicKey, entry) => {
133133
if (!publicKey || !publicKey.bytes || !entry) {
134134
const error = 'one or more of the provided parameters are not defined'
135-
136135
log.error(error)
137136
throw Object.assign(new Error(error), { code: ERRORS.ERR_UNDEFINED_PARAMETER })
138137
}
139138

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
171168
}
172169

173170
/**
@@ -232,21 +229,18 @@ const getIdKeys = (pid) => {
232229
// Sign ipns record data
233230
const sign = (privateKey, value, validityType, validity) => {
234231
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)
239233
}
240234

241235
// Utility for getting the validity type code name of a validity
242236
const getValidityType = (validityType) => {
243237
if (validityType.toString() === '0') {
244238
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 })
249239
}
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 })
250244
}
251245

252246
// Utility for creating the record data for being signed

test/index.spec.js

+3-7
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,9 @@ describe('ipns', function () {
4747
})
4848
}
4949

50-
before(function (done) {
51-
crypto.keys.generateKeyPair('RSA', 2048, (err, keypair) => {
52-
expect(err).to.not.exist()
53-
rsa = keypair
54-
55-
spawnDaemon(done)
56-
})
50+
before(async () => {
51+
rsa = await crypto.keys.generateKeyPair('RSA', 2048)
52+
return new Promise((resolve, reject) => spawnDaemon(err => err ? reject(err) : resolve()))
5753
})
5854

5955
after(function (done) {

0 commit comments

Comments
 (0)