All files / crypto/src/Encryption/Pascal/KeyCoding KeyCoding.js

20.83% Statements 5/24
0% Branches 0/2
0% Functions 0/4
20.83% Lines 5/24

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 461x 1x 1x 1x                                                                                 1x  
const Endian = require('@pascalcoin-sbx/common').Endian;
const Coding = require('@pascalcoin-sbx/common').Coding;
const CompositeType = require('@pascalcoin-sbx/common').Coding.CompositeType;
const KeyData = require('./KeyData');
 
class KeyCoding extends CompositeType {
  constructor() {
    super('pascalcoin_ecdh');
    this.description('Coding for an pascalcoin encrypted ECDH message');
 
    this.addSubType(new Coding.Core.Int8('publicKeyLength', true));
    this.addSubType(new Coding.Core.Int8('macLength', true));
    this.addSubType(new Coding.Core.Int16('originalDataLength', true, Endian.LITTLE_ENDIAN));
    this.addSubType(new Coding.Core.Int16('originalDataLengthIncPadLength', true, Endian.LITTLE_ENDIAN));
    this.addSubType(new Coding.Decissive('publicKey', 'publicKeyLength', function (publicKeyLength) {
      return new Coding.Core.BytesFixedLength('publicKey', publicKeyLength);
    }));
    this.addSubType(new Coding.Decissive('mac', 'macLength', function (macLength) {
      return new Coding.Core.BytesFixedLength('mac', macLength);
    }));
    this.addSubType(new Coding.Core.BytesWithoutLength('encryptedData'));
  }
 
  /**
   *
   * @param bc
   * @param options
   * @param all
   * @return {KeyData}
   */
  decodeFromBytes(bc, options = {}, all = null) {
    let decoded = super.decodeFromBytes(bc, options, all);
    let keyData = new KeyData();
 
    keyData.withPublicKey(decoded.publicKey);
    keyData.withOriginalDataLength(decoded.originalDataLength);
    keyData.withOriginalDataLengthIncPadLength(decoded.originalDataLengthIncPadLength);
    keyData.withMac(decoded.mac);
    keyData.withEncryptedData(decoded.encryptedData);
 
    return keyData;
  }
}
 
module.exports = KeyCoding;