git ssb

0+

wanderer🌟 / js-primea-objects



Commit f1d4a7d1e7812fadbba3bc94b2eb7db678a8d06b

Merge pull request #2 from primea/actor-ids

add actor ID generation
wanderer 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.jschanged
package-lock.jsonchanged
tests/index.jschanged
index.jsView
@@ -1,4 +1,5 @@
1 +const crypto = require('crypto')
12 const cbor = require('borc')
23 const EventEmitter = require('events')
34 const Buffer = require('safe-buffer').Buffer
45
@@ -186,12 +187,40 @@
186187 }
187188 return 'invalid'
188189 }
189190
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 +
190218 module.exports = {
191219 Message,
192220 ID,
193221 FunctionRef,
194222 ModuleRef,
195223 decoder,
196- getType
224 + getType,
225 + generateActorId
197226 }
package-lock.jsonView
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.jsView
@@ -47,4 +47,20 @@
4747 t.equals(objects.getType(Buffer.from([])), 'data')
4848
4949 t.end()
5050 })
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