Last active
June 14, 2019 20:47
-
-
Save acidsploit/20aee78487a107d6079e1348867f038d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Example use of BCHD's gRPC bchrpc with nodejs. | |
// If your bchd uses a self signed cert set env var: | |
// export NODE_TLS_REJECT_UNAUTHORIZED=0 | |
var PROTO_PATH = __dirname + '/bchrpc.proto'; | |
var async = require('async'); | |
var fs = require('fs'); | |
var grpc = require('@grpc/grpc-js'); | |
var protoLoader = require('@grpc/proto-loader'); | |
var packageDefinition = protoLoader.loadSync( | |
PROTO_PATH, | |
{keepCase: true, | |
longs: String, | |
enums: String, | |
defaults: true, | |
oneofs: true | |
}); | |
var pb = grpc.loadPackageDefinition(packageDefinition).pb; | |
// console.log(pb) | |
var client = new pb.bchrpc('bchd.your.node:8335', grpc.credentials.createSsl()); | |
// Get current state of the mempool | |
client.GetMempoolInfo(pb.MempoolInfoRequest, function(error, resp) { | |
if (error) { | |
console.log("Error: " + error.code + ": " + error.message) | |
console.log(error) | |
} else { | |
var mempool = resp | |
console.log("\nGetMempoolInfo:") | |
console.log(mempool) | |
} | |
}); | |
// Get a raw tx from tx hash | |
const changeEndianness = (string) => { | |
const result = []; | |
let len = string.length - 2; | |
while (len >= 0) { | |
result.push(string.substr(len, 2)); | |
len -= 2; | |
} | |
return result.join(''); | |
} | |
var hex = changeEndianness("fe58d09c218d6ea1a0d1ce726d1c5aa6e9c01a9e760aab621484aa21b1f673fb") | |
var bytes = [] | |
for(var i=0; i< hex.length-1; i+=2){ | |
bytes.push(parseInt(hex.substr(i, 2), 16)); | |
} | |
// console.log(bytes) | |
var getRawTransactionRequest = pb.GetRawTransactionRequest | |
getRawTransactionRequest.hash = bytes | |
client.GetRawTransaction(getRawTransactionRequest, function(error, resp) { | |
if (error) { | |
console.log("Error: " + error.code + ": " + error.message) | |
console.log(error) | |
} else { | |
var tx = resp | |
console.log("\nGetRawTransaction:") | |
console.log(tx) | |
} | |
}); | |
// Get deserialized tx from tx hash | |
var getTransactionRequest = pb.GetTransactionRequest | |
getTransactionRequest.hash = bytes | |
client.GetTransaction(getTransactionRequest, function(error, resp) { | |
if (error) { | |
console.log("Error: " + error.code + ": " + error.message) | |
console.log(error) | |
} else { | |
var tx = resp | |
console.log("\nGetTransaction:") | |
console.log(tx) | |
} | |
}); | |
// Setup live transaction stream | |
var transactionFilter = pb.TransactionFilter | |
transactionFilter.all_transactions = true | |
var subscribreTransactionsRequest = pb.SubscribeTransactionsRequest | |
subscribreTransactionsRequest.include_mempool = true | |
subscribreTransactionsRequest.subscribe = transactionFilter | |
var stream = client.SubscribeTransactions(subscribreTransactionsRequest) | |
stream.on('data', function(message) { | |
var tx = message | |
console.log("\nSubscribeTransactions stream:") | |
console.log(tx) | |
}); | |
stream.on('status', function(status) { | |
console.log(status) | |
}); | |
stream.on('end', function(status) { | |
console.log(status) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
please add note about
var grpc = require('@grpc/grpc-js');