UNPKG

kad-encrypt

Version:
55 lines (43 loc) 1.25 kB
/** * @module kad-encrypt/contact-decorator */ 'use strict'; var assert = require('assert'); var inherits = require('util').inherits; var utils = require('./utils'); const KeyPair = require('./keypair') var AddressPortContact = require('kad').contacts.AddressPortContact function ContactDecorator(Contact) { /** * Creates a contact with pubkey * @constructor * @param {Object} options */ function CryptoContact(options) { if (!(this instanceof CryptoContact)) { return new CryptoContact(options); } if (options.pubkey) { this.pubkey = options.pubkey; } else { Object.defineProperty(this,'keypair',{ value:new KeyPair(options.privateKey), enumerable:false }) this.pubkey = this.keypair.getPublicKey(); } Contact.call(this, options); } inherits(CryptoContact, Contact); /** * Generates a nodeID * #_createNodeID */ CryptoContact.prototype._createNodeID = function() { return utils.getNodeIdFromPublicKey(this.pubkey); }; return CryptoContact; } var CryptoContact = ContactDecorator(AddressPortContact) module.exports.CryptoContact = CryptoContact; module.exports.ContactDecorator = ContactDecorator;