Exemplo n.º 1
0
var calcDifficulty = exports.calcDifficulty = function (target) {
  if (!Buffer.isBuffer(target)) {
    target = decodeDiffBits(target);
  }
  var targetBigint = bigint.fromBuffer(target, {order: 'forward'});
  var maxBigint = bigint.fromBuffer(MAX_TARGET, {order: 'forward'});
  return maxBigint.div(targetBigint).toNumber();
};
Exemplo n.º 2
0
 function (key) {
   var k = bidcrypto.loadPublicKeyFromObject(
     {
       algorithm: 'RS',
       n: bigint.fromBuffer(unb64(key.n)).toString(10),
       e: bigint.fromBuffer(unb64(key.e)).toString(10)
     }
   )
   k.kid = key.kid
   set[k.kid] = k
 }
Exemplo n.º 3
0
TestClient.prototype.getToken2 = function (tokenType, session, email, password, cb) {
  var json = session;
  var a = bigint.fromBuffer(crypto.randomBytes(32));
  var g = srpParams[json.srp.N_bits].g;
  var N = srpParams[json.srp.N_bits].N;
  var A = srp.getA(g, a, N);
  var B = bigint(json.srp.B, 16);
  var S = srp.client_getS(
    Buffer(json.srp.s, 'hex'),
    email,
    password,
    N,
    g,
    a,
    B,
    json.srp.alg
  );

  var M = srp.getM(A, B, S);
  var K = srp.getK(S, json.srp.alg).toBuffer();
  this.makeRequest(
    'POST',
    tokenTypes[tokenType].finishPath,
    {
      payload: {
        sessionId: json.sessionId,
        A: A.toBuffer().toString('hex'),
        M: M.toBuffer().toString('hex')
      }
    },
    function (res) {
      if (res.result.error) return cb(res);
      var json = res.result;
      util.srpResponseKeys(
        K,
        tokenTypes[tokenType].context,
        function (err, keys) {
          var blob = Buffer(json.bundle, 'hex');
          var cyphertext = blob.slice(0, blob.length - 32);
          var hmac = blob.slice(blob.length - 32, blob.length);

          var check = crypto.createHmac('sha256', keys.respHMACkey);
          check.update(cyphertext);
          if (hmac.toString('hex') !== check.digest('hex')) {
            return cb(new Error("Corrupted Message"));
          }
          var cleartext = bigint.fromBuffer(cyphertext)
            .xor(bigint.fromBuffer(keys.respXORkey))
            .toBuffer();
          var result = {
            kA: cleartext.slice(0, 32).toString('hex'),
            wrapKb: cleartext.slice(32, 64).toString('hex'),
            token: cleartext.slice(64).toString('hex')
          };
          cb(null, result);
        }
      );
    }
  );
};
Exemplo n.º 4
0
var valueToBigInt = exports.valueToBigInt = function (valueBuffer) {
  if (Buffer.isBuffer(valueBuffer)) {
    return bigint.fromBuffer(valueBuffer, {endian: 'little', size: 8});
  } else {
    return valueBuffer;
  }
};
Exemplo n.º 5
0
        function (err, keys) {
          var blob = Buffer(json.bundle, 'hex');
          var cyphertext = blob.slice(0, blob.length - 32);
          var hmac = blob.slice(blob.length - 32, blob.length);

          var check = crypto.createHmac('sha256', keys.respHMACkey);
          check.update(cyphertext);
          if (hmac.toString('hex') !== check.digest('hex')) {
            return cb(new Error("Corrupted Message"));
          }
          var cleartext = bigint.fromBuffer(cyphertext)
            .xor(bigint.fromBuffer(keys.respXORkey))
            .toBuffer();
          var result = {
            kA: cleartext.slice(0, 32).toString('hex'),
            wrapKb: cleartext.slice(32, 64).toString('hex'),
            token: cleartext.slice(64).toString('hex')
          };
          cb(null, result);
        }
Exemplo n.º 6
0
    function (err, keys) {

      // encrypt payload to the server
      var cyphertext = bigint.fromBuffer(cleartext)
        .xor(bigint.fromBuffer(keys.respXORkey))
        .toBuffer();

      var credentials = {
        id: keys.tokenId.toString('hex'),
        key: keys.reqHMACkey.toString('hex'),
        algorithm: 'sha256'
      };
      var payload = {
        bundle: cyphertext.toString('hex')
      };
      var verify = {
        credentials: credentials,
        contentType: 'application/json',
        payload: JSON.stringify(payload)
      };
      var header = hawk.client.header('http://localhost/resetAccount', 'POST', verify);

      this.makeRequest(
        'POST',
        '/resetAccount',
        {
          headers: {
            Authorization: header.field,
            Host: 'localhost',
            'Content-Type': 'application/json'
          },
          payload: payload
        },
        function (res) {
          var err = null;
          if (res.statusCode !== 200) { err = new Error(res.result.message); }
          cb(err, res);
        }
      );
    }.bind(this)
Exemplo n.º 7
0
var encodeDiffBits = exports.encodeDiffBits = function encodeDiffBits(target) {
  if (Buffer.isBuffer(target)) {
    target = bigint.fromBuffer(target);
  } else if ("function" === typeof target.toBuffer) { // duck-typing bigint
    // Nothing to do
  } else {
    throw new Error("Incorrect variable type for difficulty");
  }

  var mpiBuf = target.toBuffer("mpint");
  var size = mpiBuf.length - 4;

  var compact = size << 24;
  if (size >= 1) compact |= mpiBuf[4] << 16;
  if (size >= 2) compact |= mpiBuf[5] <<  8;
  if (size >= 3) compact |= mpiBuf[6]      ;

  return compact;
};
Exemplo n.º 8
0
 tx.ins.forEach(function(txin, index){
   var lasttx = txin.outpoint.hash.toString('base64');
   
   // No idea how to take a uint32 and turn it into a "regular" number
   //var lasttxpos = bigint(txin.outpoint.index, 10).toNumber();
   
   var assocOut = tx.outs[index];
   var value = Util.valueToBigInt(assocOut.value);
   var btc = value.div(Math.pow(10, 8)).toNumber();
   
   var lasttxvalue = bigint.fromBuffer(txin.outpoint).toNumber();
   
   if (lasttxvalue == 0) {
     console.log("Mining reward");
   } else {
     console.log("Coins from: " + lasttx);
   }
   
   console.log("Sent to: " + assocOut.script.toString('base64'));
   console.log("Value: " + btc + " BTC");
   
 });
Exemplo n.º 9
0
var valueToBigInt = exports.valueToBigInt = function (valueBuffer) {
	if (valueBuffer instanceof bigint) return valueBuffer;
	return bigint.fromBuffer(valueBuffer, {endian: 'little', size: 8});
};
Exemplo n.º 10
0
Block.method('getChainWork', function () {
  return bigint.fromBuffer(this.chainWork);
});
Exemplo n.º 11
0
    this._onData = function(buf) {
        if (this._requests[0] == undefined) return
        var index = 0        
        while (index != buf.length) {
            var bytes = 1
            var next = this._state + 1
            switch (this._state) {
                case states.ERROR:
                    // just eat the bytes until done
                    next = states.ERROR
                    break
                    
                case states.HEADER_LEN_0:
                    this._totalLen = buf[index] << 24
                    break

                case states.HEADER_LEN_1:
                    this._totalLen += buf[index] << 16
                    break

                case states.HEADER_LEN_2:
                    this._totalLen += buf[index] << 8
                    break

                case states.HEADER_LEN_3:
                    this._totalLen += buf[index]
                    break

                case states.HEADER_EC_0:
                    this._error = buf[index] << 8
                    this._totalLen--
                    break

                case states.HEADER_EC_1:
                    this._error += buf[index]
                    this._toRead = this._totalLen
                    next = this._requests[0].request.next
                    this._totalLen--
                    if (this._error != error.NoError) this.emit('messageerror', 
                                                                this._requests[0].request.name, 
                                                                this._requests[0].request.partition, 
                                                                this._error, 
                                                                error[this._error])
                    break

                case states.RESPONSE_MSG_0:
                    this._msgLen = buf[index] << 24
                    this._requests[0].request.last_offset = bigint(this._requests[0].request.offset)
                    this._requests[0].request.offset++
                    this._payloadLen = 0
                    break

                case states.RESPONSE_MSG_1:
                    this._msgLen += buf[index] << 16
                    this._requests[0].request.offset++
                    break

                case states.RESPONSE_MSG_2:
                    this._msgLen += buf[index] << 8
                    this._requests[0].request.offset++
                    break

                case states.RESPONSE_MSG_3:
                    this._msgLen += buf[index]
                    this._requests[0].request.offset++
                    if (this._msgLen > this._totalLen) {
                        console.log(buf)
                        this.emit("parseerror", 
                                  this._requests[0].request.name,
                                  this._requests[0].request.partition,
                                  "unexpected message len " + this._msgLen + " > " + this._totalLen 
                                  + " for topic: " + this._requests[0].request.name 
                                  + ", partition: " + this._requests[0].request.partition
                                  + ", original_offset:" + this._requests[0].request.original_offset
                                  + ", last_offset: " + this._requests[0].request.last_offset)
                       this._error = error.InvalidMessage
                       next = states.ERROR
                    }
                    break

                case states.RESPONSE_MAGIC:
                    this._magic = buf[index]
                    this._requests[0].request.offset++
                    this._msgLen--
                    if (false && Math.random()*20 > 18) this._magic = 5
                    switch (this._magic) {
                        case 0:
                          next = states.RESPONSE_CHKSUM_0
                          break
                        case 1:
                          next = states.RESPONSE_COMPRESSION
                          break
                        default:
                          this.emit("parseerror", 
                                    this._requests[0].request.name,
                                    this._requests[0].request.partition,
                                    "unexpected message format - bad magic value " + this._magic                                     
                                    + " for topic: " + this._requests[0].request.name 
                                    + ", partition: " + this._requests[0].request.partition
                                    + ", original_offset:" + this._requests[0].request.original_offset
                                    + ", last_offset: " + this._requests[0].request.last_offset)
                          this._error = error.InvalidMessage
                          next = states.ERROR
                    }
                    break

                case states.RESPONSE_COMPRESSION:
                    this._msgLen--
                    this._requests[0].request.offset++
                    if (buf[index] > 0) {
                        console.log(buf)
                        this.emit("parseerror",
                                  this._requests[0].request.name,
                                  this._requests[0].request.partition,
                                  "unexpected message format - bad compression flag " 
                                  + " for topic: " + this._requests[0].request.name 
                                  + ", partition: " + this._requests[0].request.partition
                                  + ", original_offset:" + this._requests[0].request.original_offset
                                  + ", last_offset: " + this._requests[0].request.last_offset)
                        this._error = error.InvalidMessage
                        next = states.ERROR
                    }
                    break
                
                case states.RESPONSE_CHKSUM_0:
                    this._chksum = buf[index] << 24
                    this._requests[0].request.offset++
                    this._msgLen--
                    break

                case states.RESPONSE_CHKSUM_1:
                    this._chksum += buf[index] << 16
                    this._requests[0].request.offset++
                    this._msgLen--
                    break

                case states.RESPONSE_CHKSUM_2:
                    this._chksum += buf[index] << 8
                    this._requests[0].request.offset++
                    this._msgLen--
                    break

                case states.RESPONSE_CHKSUM_3:
                    this._chksum += buf[index]
                    this._requests[0].request.offset++
                    this._msgLen--
                    break

                case states.RESPONSE_MSG:
                    next = states.RESPONSE_MSG

                    // try to avoid a memcpy if possible
                    var payload = null
                    if (this._payloadLen == 0 && buf.length - index >= this._msgLen) {
                        payload = buf.toString('utf8', index, index + this._msgLen)
                        bytes = this._msgLen
                    } else {
                        var end = index + this._msgLen - this._payloadLen
                        if (end > buf.length) end = buf.length
                        buf.copy(this._buffer, this._payloadLen, index, end)
                        this._payloadLen += end - index
                        bytes = end - index
                        if (this._payloadLen == this._msgLen) {
                            payload = this._buffer.toString('utf8', 0, this._payloadLen)
                        }
                    }
                    if (payload != null) {
                        this._requests[0].request.offset += this._msgLen
                        next = states.RESPONSE_MSG_0
                        this.emit('message', this._requests[0].request.name, payload, bigint(this._requests[0].request.offset))
                    }
                    break

                case states.OFFSET_LEN_0:
                    this._msgLen = buf[index] << 24
                    break

                case states.OFFSET_LEN_1:
                    this._msgLen += buf[index] << 16
                    break

                case states.OFFSET_LEN_2:
                    this._msgLen += buf[index] << 8
                    break

                case states.OFFSET_LEN_3:
                    this._msgLen += buf[index]
                    break

                case states.OFFSET_OFFSETS_0:
                    this._requests[0].request.offset_buffer = new Buffer(8)
                    this._requests[0].request.offset_buffer[0] = buf[index]
                    break

                case states.OFFSET_OFFSETS_1:
                    this._requests[0].request.offset_buffer[1] = buf[index]
                    break

                case states.OFFSET_OFFSETS_2:
                    this._requests[0].request.offset_buffer[2] = buf[index]
                    break

                case states.OFFSET_OFFSETS_3:
                    this._requests[0].request.offset_buffer[3] = buf[index]
                    break

                case states.OFFSET_OFFSETS_4:
                    this._requests[0].request.offset_buffer[4] = buf[index]
                    break

                case states.OFFSET_OFFSETS_5:
                    this._requests[0].request.offset_buffer[5] = buf[index]
                    break

                case states.OFFSET_OFFSETS_6:
                    this._requests[0].request.offset_buffer[6] = buf[index]
                    break

                case states.OFFSET_OFFSETS_7:
                    this._requests[0].request.offset_buffer[7] = buf[index]
                    this._requests[0].request.offset = bigint.fromBuffer(this._requests[0].request.offset_buffer)
                    next = states.OFFSET_OFFSETS_0
                    this.emit('offset', this._requests[0].request.name, bigint(this._requests[0].request.offset))
            }
            if (this._requests[0] == undefined) break
            this._requests[0].request.bytesRead += bytes
            index += bytes
            this._toRead -= bytes
            this._state = next
            if (this._toRead == 0) this._last()
        }
    }
Exemplo n.º 12
0
 crypto.randomBytes(bytes, function(err, buf) {
   if (err) return callback (err);
   return callback(null, bigint.fromBuffer(buf));
 });
Exemplo n.º 13
0
 get: function() {
   return bigint.fromBuffer(this._uploaded);
 },
Exemplo n.º 14
0
 get: function() {
   return bigint.fromBuffer(this._left);
 },
Exemplo n.º 15
0
 get: function() {
   return bigint.fromBuffer(this._connectionId);
 }