Commit f1d4a7d1e7812fadbba3bc94b2eb7db678a8d06b
Merge pull request #2 from primea/actor-ids
add actor ID generationwanderer authored on 4/20/2018, 11:09:18 PM
GitHub committed on 4/20/2018, 11:09:18 PM
Parent: 9d65711f18037ce11d2627aa7db431836dd8d8e0
Parent: 8706ee72c8c3b2771f365432dc09557fb238d6b0
Files changed
index.js | changed |
package-lock.json | changed |
tests/index.js | changed |
index.js | |||
---|---|---|---|
@@ -1,4 +1,5 @@ | |||
1 … | +const crypto = require('crypto') | ||
1 | 2 … | const cbor = require('borc') | |
2 | 3 … | const EventEmitter = require('events') | |
3 | 4 … | const Buffer = require('safe-buffer').Buffer | |
4 | 5 … | ||
@@ -186,12 +187,40 @@ | |||
186 | 187 … | } | |
187 | 188 … | return 'invalid' | |
188 | 189 … | } | |
189 | 190 … | ||
191 … | +/** | ||
192 … | + * returns the ID of an actor | ||
193 … | + * @param {Object} id | ||
194 … | + * @param {Number} id.nonce - the actor's nonce | ||
195 … | + * @param {ID} id.parent - the actor's parent's ID | ||
196 … | + * @return {ID} | ||
197 … | + */ | ||
198 … | +function generateActorId (id) { | ||
199 … | + const encoded = _encodeActorId(id) | ||
200 … | + const hashed = _hash(encoded) | ||
201 … | + return new ID(hashed) | ||
202 … | +} | ||
203 … | + | ||
204 … | +function _encodeActorId (id) { | ||
205 … | + if (id.parent) { | ||
206 … | + return cbor.encode([id.nonce, id.parent.id]) | ||
207 … | + } else { | ||
208 … | + return cbor.encode([id.nonce, null]) | ||
209 … | + } | ||
210 … | +} | ||
211 … | + | ||
212 … | +function _hash (buf) { | ||
213 … | + const hash = crypto.createHash('sha256') | ||
214 … | + hash.update(buf) | ||
215 … | + return hash.digest().slice(0, 20) | ||
216 … | +} | ||
217 … | + | ||
190 | 218 … | module.exports = { | |
191 | 219 … | Message, | |
192 | 220 … | ID, | |
193 | 221 … | FunctionRef, | |
194 | 222 … | ModuleRef, | |
195 | 223 … | decoder, | |
196 | - getType | ||
224 … | + getType, | ||
225 … | + generateActorId | ||
197 | 226 … | } |
package-lock.json | ||
---|---|---|
The diff is too large to show. Use a local git client to view these changes. Old file size: 335961 bytes New file size: 335962 bytes |
tests/index.js | ||
---|---|---|
@@ -47,4 +47,20 @@ | ||
47 | 47 … | t.equals(objects.getType(Buffer.from([])), 'data') |
48 | 48 … | |
49 | 49 … | t.end() |
50 | 50 … | }) |
51 … | + | |
52 … | +tape('actor IDs', t => { | |
53 … | + const id0 = { nonce: 0, parent: null } | |
54 … | + const hashedId0 = objects.generateActorId(id0) | |
55 … | + t.deepEquals(hashedId0.id, Buffer.from('372a08b828598122fc64c4aa94735c770f25bbbc', 'hex')) | |
56 … | + | |
57 … | + const id00 = { nonce: 0, parent: hashedId0 } | |
58 … | + const hashedId00 = objects.generateActorId(id00) | |
59 … | + t.deepEquals(hashedId00.id, Buffer.from('10d7d4be8663c37d8ea7cff89b7c01c059ebbc80', 'hex')) | |
60 … | + | |
61 … | + const id01 = { nonce: 1, parent: hashedId0 } | |
62 … | + const hashedId01 = objects.generateActorId(id01) | |
63 … | + t.deepEquals(hashedId01.id, Buffer.from('0ca311b75efd27e7daf6eec8b51b5c1fe33ff233', 'hex')) | |
64 … | + | |
65 … | + t.end() | |
66 … | +}) |
Built with git-ssb-web