From 1eafe41bab26f2cebcfb61c1fffef22f05fdbbe8 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Fri, 3 Oct 2014 18:27:06 -0700 Subject: [PATCH] add fromhex methods. --- example/index.js | 6 +++++ lib/bitcoind.js | 17 +++++++++---- src/bitcoindjs.cc | 64 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 82 insertions(+), 5 deletions(-) diff --git a/example/index.js b/example/index.js index 47a2439c..10e46e9f 100755 --- a/example/index.js +++ b/example/index.js @@ -18,6 +18,8 @@ var bitcoind = require('../')(); var genesisBlock = '0x000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f'; var genesisTx = '0x4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b'; +var testTx = "01000000010b26e9b7735eb6aabdf358bab62f9816a21ba9ebdb719d5299e88607d722c190000000008b4830450220070aca44506c5cef3a16ed519d7c3c39f8aab192c4e1c90d065f37b8a4af6141022100a8e160b856c2d43d27d8fba71e5aef6405b8643ac4cb7cb3c462aced7f14711a0141046d11fee51b0e60666d5049a9101a72741df480b96ee26488a4d3466b95c9a40ac5eeef87e10a5cd336c19a84565f80fa6c547957b7700ff4dfbdefe76036c339ffffffff021bff3d11000000001976a91404943fdd508053c75000106d3bc6e2754dbcff1988ac2f15de00000000001976a914a266436d2965547608b9e15d9032a7b9d64fa43188ac00000000"; + bitcoind.on('error', function(err) { print('error="%s"', err.message); }); @@ -33,6 +35,10 @@ bitcoind.on('open', function(status) { getBlocks(bitcoind); } + // var tx = bitcoind.tx.fromHex(testTx); + // console.log(tx); + // console.log(tx.txid === tx.getHash('hex')); + function compareObj(obj) { // Hash if (obj.txid) { diff --git a/lib/bitcoind.js b/lib/bitcoind.js index 4c6ff36d..0b6ce6db 100644 --- a/lib/bitcoind.js +++ b/lib/bitcoind.js @@ -33,6 +33,10 @@ function Bitcoin(options) { Bitcoin.global = this; + Object.keys(exports).forEach(function(key) { + self[key] = exports[key]; + }); + this.on('newListener', function(name) { if (name === 'open') { self.start(); @@ -380,6 +384,10 @@ Block.isBlock = function(block) { return block._blockFlag === _blockFlag; }; +Block.fromHex = function(hex) { + return bitcoin.block(bitcoindjs.blockFromHex(hex)); +}; + Block.prototype.getHash = function(enc) { var data = bitcoindjs.getBlockHex(this); if (!this.hash || this.hash !== data.hash) { @@ -458,6 +466,10 @@ Transaction.isTx = function(tx) { return tx._txFlag === _txFlag; }; +Transaction.fromHex = function(hex) { + return bitcoin.tx(bitcoindjs.txFromHex(hex)); +}; + Transaction.prototype.verify = function() { return this.verified = this.verified || bitcoindjs.verifyTransaction(this); }; @@ -707,11 +719,6 @@ exports.bitcoind = bitcoin; exports.native = bitcoindjs; exports.bitcoindjs = bitcoindjs; -bitcoindjs.Block = Block; -bitcoindjs.Transaction = Transaction; -bitcoindjs.blockToHex = Block.toHex; -bitcoindjs.txToHex = Transaction.toHex; - exports.Block = Block; exports.block = Block; diff --git a/src/bitcoindjs.cc b/src/bitcoindjs.cc index 37d7ac98..c7b180e5 100644 --- a/src/bitcoindjs.cc +++ b/src/bitcoindjs.cc @@ -144,6 +144,8 @@ NAN_METHOD(VerifyTransaction); NAN_METHOD(FillTransaction); NAN_METHOD(GetBlockHex); NAN_METHOD(GetTxHex); +NAN_METHOD(BlockFromHex); +NAN_METHOD(TxFromHex); NAN_METHOD(WalletNewAddress); NAN_METHOD(WalletGetAccountAddress); @@ -1353,6 +1355,64 @@ NAN_METHOD(GetTxHex) { NanReturnValue(data); } +/** + * BlockFromHex + */ + +NAN_METHOD(BlockFromHex) { + NanScope(); + + if (args.Length() < 1 || !args[0]->IsString()) { + return NanThrowError( + "Usage: bitcoindjs.blockFromHex(hex)"); + } + + String::AsciiValue hex_string_(args[0]->ToString()); + std::string hex_string = *hex_string_; + + CBlock cblock; + CDataStream ssData(ParseHex(hex_string), SER_NETWORK, PROTOCOL_VERSION); + try { + ssData >> cblock; + } catch (std::exception &e) { + NanThrowError("Bad Block decode"); + } + + Local jsblock = NanNew(); + cblock_to_jsblock(cblock, 0, jsblock); + + NanReturnValue(jsblock); +} + +/** + * TxFromHex + */ + +NAN_METHOD(TxFromHex) { + NanScope(); + + if (args.Length() < 1 || !args[0]->IsString()) { + return NanThrowError( + "Usage: bitcoindjs.txFromHex(hex)"); + } + + String::AsciiValue hex_string_(args[0]->ToString()); + std::string hex_string = *hex_string_; + + CTransaction ctx; + CDataStream ssData(ParseHex(hex_string), SER_NETWORK, PROTOCOL_VERSION); + try { + ssData >> ctx; + } catch (std::exception &e) { + NanThrowError("Bad Block decode"); + } + + Local jstx = NanNew(); + ctx_to_jstx(ctx, 0, jstx); + + NanReturnValue(jstx); +} + /** * Wallet */ @@ -2677,6 +2737,8 @@ jstx_to_ctx(const Local jstx, CTransaction& ctx) { return; + // XXX This is returning bad hex values for some reason: + ctx.nMinTxFee = (int64_t)jstx->Get(NanNew("mintxfee"))->IntegerValue(); ctx.nMinRelayTxFee = (int64_t)jstx->Get(NanNew("minrelaytxfee"))->IntegerValue(); // ctx.CURRENT_VERSION = (unsigned int)jstx->Get(NanNew("current_version"))->Int32Value(); @@ -2784,6 +2846,8 @@ init(Handle target) { NODE_SET_METHOD(target, "fillTransaction", FillTransaction); NODE_SET_METHOD(target, "getBlockHex", GetBlockHex); NODE_SET_METHOD(target, "getTxHex", GetTxHex); + NODE_SET_METHOD(target, "blockFromHex", BlockFromHex); + NODE_SET_METHOD(target, "txFromHex", TxFromHex); NODE_SET_METHOD(target, "walletNewAddress", WalletNewAddress); NODE_SET_METHOD(target, "walletGetAccountAddress", WalletGetAccountAddress);