all files / src/ signing.js

64.44% Statements 29/45
50% Branches 3/6
77.78% Functions 7/9
60% Lines 24/40
1 statement, 1 function Ignored     
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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85              1×             1×   1× 4×     1× 10×     1× 1×   1×     1× 1× 1× 1×         1× 1× 4× 4×     1× 10× 10× 10×                   1×                                                 1×    
//  This module provides the signing functionality used by the DigitalBits network
//  The code below may look a little strange... this is because we try to provide
//  the most efficient signing method possible.  First, we try to load the 
//  native ed25519 package for node.js environments, and if that fails we
//  fallback to tweetnacl.js
 
 
var actualMethods = {};
 
/**
 * Use this flag to check if fast signing (provided by `ed25519` package) is available.
 * If your app is signing a large number of transaction or verifying a large number
 * of signatures make sure `ed25519` package is installed.
 */
export const FastSigning = checkFastSigning();
 
export function sign(data, secretKey) {
  return actualMethods.sign(data, secretKey);
}
 
export function verify(data, signature, publicKey) {
  return actualMethods.verify(data, signature, publicKey);
}
 
function checkFastSigning() {
  var ed25519Used;
  // if in node
  Eif (typeof window === 'undefined') {
    // NOTE: we use commonjs style require here because es6 imports
    // can only occur at the top level.  thanks, obama.
    let ed25519;
    try {
      ed25519 = require("ed25519");
      ed25519Used = true;
    } catch (err) {
      ed25519Used = false;
    }
 
    Eif (ed25519Used) {
      actualMethods.sign = function(data, secretKey) {
        data = new Buffer(data);
        return ed25519.Sign(data, secretKey);
      };
 
      actualMethods.verify = function(data, signature, publicKey) {
        data = new Buffer(data);
        try {
          return ed25519.Verify(data, signature, publicKey);
        } catch(e) {
          return false;
        }
      };
    }
  } else {
    ed25519Used = false;
  }
 
  Iif (!ed25519Used) {
    // fallback to tweetnacl.js if we're in the browser or
    // if there was a failure installing ed25519
    let nacl = require("tweetnacl");
 
    actualMethods.sign = function(data, secretKey) {
      data      = new Buffer(data);
      data      = new Uint8Array(data.toJSON().data);
      secretKey = new Uint8Array(secretKey.toJSON().data);
 
      let signature = nacl.sign.detached(data, secretKey);
 
      return new Buffer(signature);
    };
 
    actualMethods.verify = function(data, signature, publicKey) {
      data      = new Buffer(data);
      data      = new Uint8Array(data.toJSON().data);
      signature = new Uint8Array(signature.toJSON().data);
      publicKey = new Uint8Array(publicKey.toJSON().data);
 
      return nacl.sign.detached.verify(data, signature, publicKey);
    };
  }
 
  return ed25519Used;
}