EzDevInfo.com

bitcore

A pure and powerful JavaScript Bitcoin library

NodeJS setInterval() not working correctly

Program doesn't call console.log(infos) in setInterval() method after some time. It continues working after several minutes but interval value differs from setted value, E.g. if I set interval to 200ms it will call console.log(infos) after 10 minutes and more. What I am doing wrong? Here is my code

var bitcore = require('bitcore');
var p2p = require('bitcore-p2p');
var Peer = p2p.Peer;
var Pool = p2p.Pool;
var Networks = bitcore.Networks;
var messages = new p2p.Messages();
peer = new Peer('127.0.0.1', 8333);
pool = new Pool(Networks.livenet);
pool.connect();

var  Infos = [],

process.nextTick(function(){
    setInterval(function(){
    console.log(Infos);
    Infos = [];
} , 200);
});

pool.on('peerinv', function (peer, message) {
peer.sendMessage(messages.GetData(message.inventory));
peer.on('tx', function (tx) {
var transaction = tx.transaction;
console.log(transaction.id);
Infos.push({ "txId" : transaction.id });
})
});

Source: (StackOverflow)

Can't get browserify to work, gives Reference Error

I'm trying to build bitcore so that t works in the browser. These are the commands I ran:

npm install bitcore

The I made a file called index.js:

var Address = require('bitcore/lib/address');
var Block = require('bitcore/lib/block');
var BlockHeader = require('bitcore/lib/blockheader');
var HDPrivateKey = require('bitcore/lib/hdprivatekey');
var HDPublicKey = require('bitcore/lib/hdpublickey');
var PaymentProtocol = require('bitcore/lib/paymentprotocol');
var PrivateKey = require('bitcore/lib/privatekey');
var PublicKey = require('bitcore/lib/publickey');
var Script = require('bitcore/lib/script');
var Transaction = require('bitcore/lib/transaction');
var URI = require('bitcore/lib/uri');
var Unit = require('bitcore/lib/unit');

(this taken straight from the bitcore docs: http://bitcore.io/guide/browser.html)

then I ran this command:

browserify index.js -o bitcore-browser.js

It seemed to work. Here is the first few lines of the output file:

(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
var Address = require('bitcore/lib/address');
var Block = require('bitcore/lib/block');
var BlockHeader = require('bitcore/lib/blockheader');
var HDPrivateKey = require('bitcore/lib/hdprivatekey');
var HDPublicKey = require('bitcore/lib/hdpublickey');
var PaymentProtocol = require('bitcore/lib/paymentprotocol');
var PrivateKey = require('bitcore/lib/privatekey');
var PublicKey = require('bitcore/lib/publickey');
var Script = require('bitcore/lib/script');
var Transaction = require('bitcore/lib/transaction');
var URI = require('bitcore/lib/uri');
var Unit = require('bitcore/lib/unit');

},{"bitcore/lib/address":2,"bitcore/lib/block":3,"bitcore/lib/blockheader":4,"bitcore/lib/hdprivatekey":18,"bitcore/lib/hdpublickey":19,"bitcore/lib/paymentprotocol":24,"bitcore/lib/privatekey":28,"bitcore/lib/publickey":29,"bitcore/lib/script":30,"bitcore/lib/transaction":33,"bitcore/lib/unit":42,"bitcore/lib/uri":43}],2:[function(require,module,exports){
(function (Buffer){
'use strict';

(which i assume means it worked correctly. The file is about 2MB.)

When I add the file to a page, none of the object are there:

<!DOCTYPE html>
<html>
    <head>
        <script src="bitcore.browser.js"></script>
    </head>
    <body>
        <script>
            var p = new PrivateKey(); // ReferenceError: PrivateKey is not defined
        </script>
    </body>
</html>

I even tried to run "uglify" but that had no effect.


Source: (StackOverflow)

Advertisements