primea-objects
Advanced tools
Comparing version 0.0.3 to 0.0.4
31
index.js
@@ -0,1 +1,2 @@ | ||
const crypto = require('crypto') | ||
const cbor = require('borc') | ||
@@ -190,2 +191,29 @@ const EventEmitter = require('events') | ||
/** | ||
* returns the ID of an actor | ||
* @param {Object} id | ||
* @param {Number} id.nonce - the actor's nonce | ||
* @param {ID} id.parent - the actor's parent's ID | ||
* @return {ID} | ||
*/ | ||
function generateActorId (id) { | ||
const encoded = _encodeActorId(id) | ||
const hashed = _hash(encoded) | ||
return new ID(hashed) | ||
} | ||
function _encodeActorId (id) { | ||
if (id.parent) { | ||
return cbor.encode([id.nonce, id.parent.id]) | ||
} else { | ||
return cbor.encode([id.nonce, null]) | ||
} | ||
} | ||
function _hash (buf) { | ||
const hash = crypto.createHash('sha256') | ||
hash.update(buf) | ||
return hash.digest().slice(0, 20) | ||
} | ||
module.exports = { | ||
@@ -197,3 +225,4 @@ Message, | ||
decoder, | ||
getType | ||
getType, | ||
generateActorId | ||
} |
{ | ||
"name": "primea-objects", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "Object helper classes for Primea's system Objects", | ||
@@ -25,4 +25,4 @@ "main": "index.js", | ||
"coveralls": "^3.0.0", | ||
"documentation": "^6.1.0", | ||
"nyc": "^11.6.0", | ||
"documentation": "^6.3.0", | ||
"nyc": "^11.7.1", | ||
"standard": "^11.0.1", | ||
@@ -29,0 +29,0 @@ "tape": "^4.9.0" |
@@ -7,3 +7,2 @@ const Buffer = require('safe-buffer').Buffer | ||
tape('system objects', t => { | ||
t.equals(objects.getType(), 'invalid') | ||
@@ -53,1 +52,17 @@ t.equals(objects.getType(true), 'invalid') | ||
}) | ||
tape('actor IDs', t => { | ||
const id0 = { nonce: 0, parent: null } | ||
const hashedId0 = objects.generateActorId(id0) | ||
t.deepEquals(hashedId0.id, Buffer.from('372a08b828598122fc64c4aa94735c770f25bbbc', 'hex')) | ||
const id00 = { nonce: 0, parent: hashedId0 } | ||
const hashedId00 = objects.generateActorId(id00) | ||
t.deepEquals(hashedId00.id, Buffer.from('10d7d4be8663c37d8ea7cff89b7c01c059ebbc80', 'hex')) | ||
const id01 = { nonce: 1, parent: hashedId0 } | ||
const hashedId01 = objects.generateActorId(id01) | ||
t.deepEquals(hashedId01.id, Buffer.from('0ca311b75efd27e7daf6eec8b51b5c1fe33ff233', 'hex')) | ||
t.end() | ||
}) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
119845
676