mappum / bitcoin-protocol Goto Github PK
View Code? Open in Web Editor NEW:symbols: Bitcoin network protocol streams
:symbols: Bitcoin network protocol streams
babel
here?create-hash
instead crypto.createHash
? create-hash
is a part of crypto-browserify
Thank you!
If a node is set up behind a proxy (or perhaps some other configuration) and does not return the expected response (a well formed p2p message), the decoder goes into an infinite loop.
Line 97 of types.js assumes there will be a 0 in the buffer array.
for (stop = 0; bvalue[stop] !== 0; ++stop);
A 400 response will not have a 0 in the array.
This could be easily fixed with (error checking may also need to be addressed)
for (stop = 0; bvalue[stop] !== 0 && stop < 12; ++stop);
A short term workaround is to not pipe the decoder through the socket.
//socket.pipe(decoder);
socket.on('data', (message) => {
if (message.length > 132) {
socket.end();
console.log('Service does not appear to be a node');
return;
}
decoder.write(message);
});
It would also be very helpful to document the available methods. I had to dig through the code for the write method.
bitcoin-protocol/src/messages.js
Line 50 in 12e07d2
21*1e6*1e8 < Number.MAX_SAFE_INTEGER
=> 2100000000000000 < 9007199254740991
Reference:
@mappum please add somewhere constants for inventory vectors: ERROR, MSG_TX, MSG_BLOCK, MSG_FILTERED_BLOCK
https://en.bitcoin.it/wiki/Protocol_documentation#Inventory_Vectors
@mappum can you clarify next moments:
writeInt32LE
instead writeUInt32LE
and for what you do & -1
instead | 0
or >>> 0
? bitcoin-protocol/src/dataTypes.js
Line 82 in 12e07d2
0xffffffff
instead 0x0100000000
? bitcoin-protocol/src/dataTypes.js
Line 107 in 12e07d2
and btw | 0
is faster than Math.floor
:)
I have set the console log on stream.js at line 59.
var payload = bl.slice(0, message.length)
console.log(payload)
console.log('----------------------')
But it look different from the wireshark and the actual console
var net = require('net')
var bp = require('bitcoin-protocol')
var decoder = bp.createDecodeStream()
decoder.on('data', function (message) { //console.log(message)
})
var encoder = bp.createEncodeStream()
var socket = net.connect(8333, '46.4.60.36', function () {
socket.pipe(decoder)
encoder.pipe(socket)
encoder.write({
magic: 0xd9b4bef9,
command: 'version',
payload: {
version: 70012,
services: Buffer(8).fill(0),
timestamp: Math.round(Date.now() / 1000),
receiverAddress: {
services: Buffer('0100000000000000', 'hex'),
address: '0.0.0.0',
port: 8333
},
senderAddress: {
services: Buffer(8).fill(0),
address: '0.0.0.0',
port: 8333
},
nonce: Buffer(8).fill(123),
userAgent: 'foobar',
startHeight: 0,
relay: true
}
})
})
I also try to using the double sha256 of the message payload to get the checksum,but always different from the wireshark,can you take a look of this?Thanks.
EX:
const crypto = require('crypto');
function double_sha256(payload) {
let sha256 = crypto.createHash('sha256').update(payload).digest();
return crypto.createHash('sha256').update(sha256).digest().slice(0, 4);
}
console.log(double_sha256('0e06a71b67ab03e9'))
which will output <Buffer b1 ea bf f7>
not <Buffer e5 a8 96 7b>
I was unable to get the example up and running, the data is sent to the node but no response data is emitted on the stream.
Running the integration tests have a similar result
$ npm run test-integration 130 ↵
> [email protected] test-integration /Users/jnordberg/Development/bitcoin-protocol
> tape test/integration/*.js | faucet
✓ Integration with bitcoin core in regtest mode
⨯ spawn process
not ok 1 should be equivalent
---
operator: deepEqual
expected: |-
null
actual: |-
{ [Error: Loading wallet...] code: -28 }
at: IncomingMessage.<anonymous> (/Users/jnordberg/Development/bitcoin-protocol/node_modules/bitcoin/lib/jsonrpc.js:127:15)
...
✓ handshake
# send ping -> wait pong
ok 19 correct payload
Just hangs indefinitely waiting for a response.
$ bitcoind -version
Bitcoin Core Daemon version v0.14.99.0-cf5782508
$ node --version
v7.9.0
@mappum this package definitely need API for adding custom messages, maybe through createDecodeStream/createEncodeStream
it will be helpful for other cryptocurrencies (like dash for example -- https://github.com/UdjinM6/dash/blob/43712ff4deec1c1cd606e70710279ba8bbb986d7/src/darksend.cpp#L48)
/cc @jprichardson
bip-0152 looks like it has made its way into version 70015
, which now supports 4 additional messages:
These are currently unhandled.
When I have time I'm going to further read the specification of these protocols, but in the meantime, dropping an issue here.
They might be related to BIP 141 and 144, and thus may be related to #14
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.