Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docs for using bloom filter and BIP37 support #44

Open
braydonf opened this issue Mar 2, 2015 · 2 comments
Open

Docs for using bloom filter and BIP37 support #44

braydonf opened this issue Mar 2, 2015 · 2 comments

Comments

@braydonf
Copy link
Contributor

braydonf commented Mar 2, 2015

With the filter messages now included, we need docs on how to create a bloom filter, and send a filterload message and receive messages (such as an initial relay boolean).

@braydonf
Copy link
Contributor Author

braydonf commented Mar 2, 2015

Here is start for connecting with the relay disabled option, loading a filter and requesting filtered blocks, and handling txs:

'use strict';

var p2p = require('../');
var Pool = p2p.Pool;
var BloomFilter = p2p.BloomFilter;
var qrcode = require('qrcode-terminal');
var Messages = p2p.Messages;
var bitcore = require('bitcore');
var Networks = bitcore.Networks;

var pool = new Pool(Networks.testnet, {
  relay: false,
  dnsSeed: false,
  listenAddr: false,
  addrs: [{ip: {v4: 'localhost'}}]
});

pool.connect();
var privateKey = bitcore.PrivateKey.fromWIF('...');
var publicKey = privateKey.toPublicKey();

console.log('publickey buffer', publicKey.toBuffer());
console.log('address', publicKey.toAddress());

qrcode.generate('bitcoin:' + publicKey.toAddress() + '?amount=0.01');

var filter = BloomFilter.create(10, 0.01);
filter.insert(publicKey.toBuffer());
filter.insert(bitcore.crypto.Hash.sha256ripemd160(publicKey.toBuffer()));

pool.on('peerready', function(peer) {
  var message = new Messages.FilterLoad(filter);
  peer.sendMessage(message);
});


pool.on('peerinv', function(peer, message) {
  var inventory = message.inventory;
  var filteredInventory = [];
  inventory.forEach(function(inv) {
    // request a filtered block                                                                                                                                                                                       
    if (inv.type === Messages.Inventory.TYPE.BLOCK) {
      var filteredInv = new Messages.Inventory.forItem(Messages.Inventory.TYPE.FILTERED_BLOCK, inv.hash);
      filteredInventory.push(filteredInv);
    }
    if (inv.type === Messages.Inventory.TYPE.TX) {
      filteredInventory.push(inv);
    }
  });

  var getdata = new Messages.GetData(filteredInventory);
  peer.sendMessage(getdata);

});

pool.on('peermerkleblock', function(peer, message) {
  console.log('merkleblock', message);
});

pool.on('peertx', function(peer, message) {
  console.log('tx', message);
});

@braydonf
Copy link
Contributor Author

braydonf commented Mar 2, 2015

I thinking it may be good to have a bloomfilter.insertPublicKey(key) to handle Bitcore objects appropriately. Thoughts?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant