bcoin-org / bcoin

Javascript bitcoin library for node.js and browsers
https://bcoin.io
Other
3.01k stars 811 forks source link

Lack of documentation #44

Closed koalalorenzo closed 8 years ago

koalalorenzo commented 8 years ago

I am trying to use this library to set up a SPV node, but I have hard time due to a lack of documentation. Is anybody taking care of that? I would like to help doing that, but I have no clue what to write.

chjj commented 8 years ago

I plan on writing some docs once the new API is stable. Bcoin is going through a lot of changes right now (particularly pertaining to the TX and Wallet objects to implement p2sh multisig, hd keys, and easier transaction building), but an example of an SPV client is at https://github.com/chjj/coined (lib) and https://github.com/chjj/termcoin (wallet).

koalalorenzo commented 8 years ago

Thanks for these links, I was looking at these, but I think I will dive in deeply. Since I am trying to build a small "programmable" explorer, things are getting hard without documentation. I still have to find a way to extract tx, blocks or addresses information. Any suggestion?

I was noticing that coined is saving in the database more details than bcoin, is this to make it easier to extract data?

chjj commented 8 years ago

bcoin is an SPV client by default. It does not download the entire blockchain (only merkleblocks -- which are the block headers with some extra fields).

The master branch has a fullNode option which will download the entire blockchain using the conventional blockchain sync method. This is probably what you want if you're making a blockchain explorer. You will have to hook into pool.on('block', ...) and save the blocks somewhere. You'll want to save the txes separately somewhere too so you can easily look them up.

In master, addresses and other information can be extracted from scripts with TX.getInputData(input) and TX.getInputData(output). Right now they work with pubkey, pubkeyhash, p2sh, and multisig transactions (+ checklocktimeverify if present).

And yeah, coined does what I mentioned above. It saves blocks and TXes by address, by txid, etc.

koalalorenzo commented 8 years ago

The reason I want to use this library is to avoid the download of a full blockchain. Ideally I need to run it on a RaspberryPi! I have seen part of code related to "checking" and "watching" addresses for the wallet. Because I want to "watch" only specific addresses and transactions that I am looking for. Is it possible to ask for specific data to other nodes? thanks

chjj commented 8 years ago

I see. Yes. You'll want to do something like the example in the readme to start up a pool. You can then do:

// Watch for a tx:
pool.watch(txHash);

var wallet = bcoin.wallet({
  // If you have a private key you want to use:
  priv: myPrivateKey,
  // If you have a p2sh multisig wallet:
  multisig: {
    type: 'scripthash',
    m: 2,
    n: 3,
    keys: [pub1, pub2, pub3]
  }
});

pool.addWallet(wallet);
// Will call this:
// pool.watch(wallet.getPublicKey());
// Among other things internally.

// To watch your friend's transactions:
pool.watch(pub2);
pool.watch(pub3);

The keys can be byte arrays or hex strings.

Then listen for:

// All txes watched for or containing your wallet's public key(s).
pool.on('tx', function(tx, peer) {
  console.log(tx);
  tx.inputs.forEach(function(input) {
    console.log(bcoin.tx.getInputData(input));
  });
});

The watch method adds a piece of data to the bloom filter. bcoin will send the entire bloom filter as a filterload packet as specified by bip37. If you add a wallet's public key, for example, peers will send you any transactions that have the wallet's public key in the input or output scripts.

koalalorenzo commented 8 years ago

Is the master version of this repository updated with the version on npm? I am noticing that some methods are not there ( bcoin.tx.getInputData )

chjj commented 8 years ago

All the new goodies only exist on the github master branch for now.

Also, some news: I've revamped inputs and outputs so tx.getInputData isn't necessary. They all have hidden non-enumerable properties which extract the data. You can do things like:

// Applicable to inputs and outputs depending
// on what data is available from either:
console.log(tx.inputs[0].addr);
console.log(tx.inputs[0].addrs);
console.log(tx.inputs[0].sigs);
console.log(tx.inputs[0].pubs);
console.log(tx.inputs[0].hashes);
console.log(tx.inputs[0].value);
console.log(tx.inputs[0].m);
console.log(tx.inputs[0].n);
console.log(tx.inputs[0].output);
console.log(tx.inputs[0].data.scripthash);
console.log(tx.inputs[0].data.scriptaddr);

// For all the data:
console.log(tx.inputs[0].data);
console.log(tx.outputs[0].data);