Skip to content

Commit 73c702f

Browse files
Pedro Brancoruimarinho
Pedro Branco
authored andcommitted
Fix bug when calling REST method with extension bin
1 parent 81296e8 commit 73c702f

File tree

2 files changed

+63
-45
lines changed

2 files changed

+63
-45
lines changed

src/index.js

+16-8
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,10 @@ class Client {
138138
const [[hash, { extension = 'json' } = {}], callback] = source(...args);
139139

140140
return Promise.try(() => {
141-
return this.request.getAsync(`/rest/tx/${hash}.${extension}`)
142-
.bind(this)
141+
return this.request.getAsync({
142+
encoding: extension === 'bin' ? null : undefined,
143+
url: `/rest/tx/${hash}.${extension}`
144+
}).bind(this)
143145
.then(this.parser.rest);
144146
}).asCallback(callback);
145147
}
@@ -154,8 +156,10 @@ class Client {
154156
const [[hash, { summary = false, extension = 'json' } = {}], callback] = source(...args);
155157

156158
return Promise.try(() => {
157-
return this.request.getAsync(`/rest/block${summary ? '/notxdetails/' : '/'}${hash}.${extension}`)
158-
.bind(this)
159+
return this.request.getAsync({
160+
encoding: extension === 'bin' ? null : undefined,
161+
url: `/rest/block${summary ? '/notxdetails/' : '/'}${hash}.${extension}`
162+
}).bind(this)
159163
.then(this.parser.rest);
160164
}).asCallback(callback);
161165
}
@@ -172,8 +176,10 @@ class Client {
172176
throw new Error(`Extension "${extension}" is not supported`);
173177
}
174178

175-
return this.request.getAsync(`/rest/headers/${count}/${hash}.${extension}`)
176-
.bind(this)
179+
return this.request.getAsync({
180+
encoding: extension === 'bin' ? null : undefined,
181+
url: `/rest/headers/${count}/${hash}.${extension}`
182+
}).bind(this)
177183
.then(this.parser.rest);
178184
}).asCallback(callback);
179185
}
@@ -205,8 +211,10 @@ class Client {
205211
return `${outpoint.id}-${outpoint.index}`;
206212
}).join('/');
207213

208-
return this.request.getAsync(`/rest/getutxos/checkmempool/${sets}.${extension}`)
209-
.bind(this)
214+
return this.request.getAsync({
215+
encoding: extension === 'bin' ? null : undefined,
216+
url: `/rest/getutxos/checkmempool/${sets}.${extension}`
217+
}).bind(this)
210218
.then(this.parser.rest)
211219
.asCallback(callback);
212220
}

test/index_test.js

+47-37
Original file line numberDiff line numberDiff line change
@@ -387,32 +387,37 @@ describe('Client', () => {
387387
});
388388

389389
describe('getTransactionByHash()', () => {
390-
it('should return a transaction binary-encoded if extension is `bin`', async () => {
391-
const transaction = await client.getTransactionByHash('b4dd08f32be15d96b7166fd77afd18aece7480f72af6c9c7f9c5cbeb01e686fe', { extension: 'bin' });
390+
it('should return a transaction json-encoded by default', async () => {
391+
const [{ txid }] = await client.listUnspent();
392+
const transaction = await client.getTransactionByHash(txid);
392393

393-
new Buffer(transaction, 'binary').toString('hex').should.endWith('206e6f7420666f756e640d0a');
394+
transaction.should.have.keys('blockhash', 'locktime', 'hash', 'size', 'txid', 'version', 'vin', 'vout', 'vsize');
394395
});
395396

396397
it('should return a transaction hex-encoded if extension is `hex`', async () => {
397-
const [transaction] = await client.listUnspent();
398-
const hex = await client.getTransactionByHash(transaction.txid, { extension: 'hex' });
398+
const [{ txid }] = await client.listUnspent();
399+
const { hex: rawTransaction } = await client.getTransaction(txid);
400+
const hexTransaction = await client.getTransactionByHash(txid, { extension: 'hex' });
399401

400-
hex.should.endWith('cf900000000\n');
402+
hexTransaction.should.equal(`${rawTransaction}\n`);
401403
});
402404

403-
it('should return a transaction json-encoded by default', async () => {
404-
const [transaction] = await client.listUnspent();
405-
const hex = await client.getTransactionByHash(transaction.txid);
405+
it('should return a transaction binary-encoded if extension is `bin`', async () => {
406+
const [{ txid }] = await client.listUnspent();
407+
const binaryTransaction = await client.getTransactionByHash(txid, { extension: 'bin' });
408+
const hexTransaction = await client.getTransactionByHash(txid, { extension: 'hex' });
406409

407-
hex.should.have.keys('blockhash', 'locktime', 'hash', 'size', 'txid', 'version', 'vin', 'vout', 'vsize');
410+
binaryTransaction.should.be.instanceOf(Buffer);
411+
hexTransaction.should.equal(`${binaryTransaction.toString('hex')}\n`);
408412
});
409413
});
410414

411415
describe('getBlockByHash()', () => {
412-
it('should return a block binary-encoded if extension is `bin`', async () => {
413-
const block = await client.getBlockByHash('0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206', { extension: 'bin' });
416+
it('should return a block json-encoded by default', async () => {
417+
const block = await client.getBlockByHash('0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206', { extension: 'json' });
414418

415-
new Buffer(block, 'binary').toString('hex').should.equal('0100000000000000000000000000000000000000000000000000000000000000000000003bfdfdfd7a7b12fd7afd2c3e6776fd617ffd1bc8fd51323afdfdfd4b1e5e4afdfd494dfdfd7f20020000000101000000010000000000000000000000000000000000000000000000000000000000000000fdfdfdfd4d04fdfd001d0104455468652054696d65732030332f4a616e2f32303039204368616e63656c6c6f72206f6e206272696e6b206f66207365636f6e64206261696c6f757420666f722062616e6b73fdfdfdfd0100fd052a0100000043410467fdfdfdfd5548271967fdfd7130fd105ca828fd3909fd7962fdfd1f61b649fdfd3f4cfd38fdfd5504fd1efd12fd5c384dfdfd0bfd57fd4c702b6bfd1d5ffd00000000');
419+
block.should.have.keys('bits', 'chainwork', 'confirmations', 'difficulty', 'hash', 'height', 'mediantime', 'merkleroot', 'nextblockhash', 'nonce', 'size', 'strippedsize', 'time', 'tx', 'version', 'versionHex', 'weight');
420+
block.tx.should.matchEach(value => value.should.be.an.Object());
416421
});
417422

418423
it('should return a block hex-encoded if extension is `hex`', async () => {
@@ -421,11 +426,11 @@ describe('Client', () => {
421426
block.should.equal('0100000000000000000000000000000000000000000000000000000000000000000000003ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4adae5494dffff7f20020000000101000000010000000000000000000000000000000000000000000000000000000000000000ffffffff4d04ffff001d0104455468652054696d65732030332f4a616e2f32303039204368616e63656c6c6f72206f6e206272696e6b206f66207365636f6e64206261696c6f757420666f722062616e6b73ffffffff0100f2052a01000000434104678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac00000000\n');
422427
});
423428

424-
it('should return a block json-encoded by default', async () => {
425-
const block = await client.getBlockByHash('0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206', { extension: 'json' });
429+
it('should return a block binary-encoded if extension is `bin`', async () => {
430+
const block = await client.getBlockByHash('0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206', { extension: 'bin' });
426431

427-
block.should.have.keys('bits', 'chainwork', 'confirmations', 'difficulty', 'hash', 'height', 'mediantime', 'merkleroot', 'nextblockhash', 'nonce', 'size', 'strippedsize', 'time', 'tx', 'version', 'versionHex', 'weight');
428-
block.tx.should.matchEach(value => value.should.be.an.Object());
432+
block.should.be.instanceOf(Buffer);
433+
block.toString('hex').should.equal('0100000000000000000000000000000000000000000000000000000000000000000000003ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4adae5494dffff7f20020000000101000000010000000000000000000000000000000000000000000000000000000000000000ffffffff4d04ffff001d0104455468652054696d65732030332f4a616e2f32303039204368616e63656c6c6f72206f6e206272696e6b206f66207365636f6e64206261696c6f757420666f722062616e6b73ffffffff0100f2052a01000000434104678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac00000000');
429434
});
430435

431436
it('should return a block summary json-encoded if `summary` is enabled', async () => {
@@ -437,10 +442,15 @@ describe('Client', () => {
437442
});
438443

439444
describe('getBlockHeadersByHash()', () => {
440-
it('should return block headers binary-encoded if extension is `bin`', async () => {
441-
const headers = await client.getBlockHeadersByHash('0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206', 1, { extension: 'bin' });
445+
it('should return a block json-encoded by default', async () => {
446+
try {
447+
await client.getBlockHeadersByHash('0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206', 1, { extension: 'json' });
442448

443-
new Buffer(headers, 'binary').toString('hex').should.equal('0100000000000000000000000000000000000000000000000000000000000000000000003bfdfdfd7a7b12fd7afd2c3e6776fd617ffd1bc8fd51323afdfdfd4b1e5e4afdfd494dfdfd7f2002000000');
449+
should.fail();
450+
} catch (e) {
451+
e.should.be.an.instanceOf(Error);
452+
e.message.should.equal('Extension "json" is not supported');
453+
}
444454
});
445455

446456
it('should return block headers hex-encoded if extension is `hex`', async () => {
@@ -449,15 +459,12 @@ describe('Client', () => {
449459
headers.should.equal('0100000000000000000000000000000000000000000000000000000000000000000000003ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4adae5494dffff7f2002000000\n');
450460
});
451461

452-
it('should return a block json-encoded by default', async () => {
453-
try {
454-
await client.getBlockHeadersByHash('0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206', 1, { extension: 'json' });
462+
it('should return block headers binary-encoded if extension is `bin`', async () => {
463+
const hash = '0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206';
464+
const binaryHeaders = await client.getBlockHeadersByHash(hash, 1, { extension: 'bin' });
465+
const hexHeaders = await client.getBlockHeadersByHash(hash, 1, { extension: 'hex' });
455466

456-
should.fail();
457-
} catch (e) {
458-
e.should.be.an.instanceOf(Error);
459-
e.message.should.equal('Extension "json" is not supported');
460-
}
467+
binaryHeaders.toString('hex').should.equal(`${hexHeaders.toString('hex').replace('\n', '')}`);
461468
});
462469
});
463470

@@ -470,16 +477,17 @@ describe('Client', () => {
470477
});
471478

472479
describe('getUnspentTransactionOutputs()', () => {
473-
it('should return unspent transaction outputs hex-encoded if extension is `bin`', async () => {
480+
it('should return unspent transaction outputs json-encoded by default', async () => {
474481
const result = await new Client(config.bitcoind).getUnspentTransactionOutputs([{
475482
id: '0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206',
476483
index: 0
477484
}, {
478485
id: '0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206',
479486
index: 1
480-
}], { extension: 'bin' });
487+
}]);
481488

482-
new Buffer(result, 'binary').toString('hex').should.endWith('010000');
489+
result.should.have.keys('bitmap', 'chainHeight', 'chaintipHash', 'utxos');
490+
result.chainHeight.should.be.a.Number();
483491
});
484492

485493
it('should return unspent transaction outputs hex-encoded if extension is `hex`', async () => {
@@ -491,20 +499,22 @@ describe('Client', () => {
491499
index: 1
492500
}], { extension: 'hex' });
493501

494-
result.should.endWith('010000\n');
502+
result.should.endWith('10000\n');
495503
});
496504

497-
it('should return unspent transaction outputs json-encoded by default', async () => {
498-
const result = await new Client(config.bitcoind).getUnspentTransactionOutputs([{
505+
it('should return unspent transaction outputs binary-encoded if extension is `bin`', async () => {
506+
const outputs = [{
499507
id: '0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206',
500508
index: 0
501509
}, {
502510
id: '0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206',
503511
index: 1
504-
}]);
512+
}];
513+
const binaryUnspents = await new Client(config.bitcoind).getUnspentTransactionOutputs(outputs, { extension: 'bin' });
514+
const hexUnspents = await new Client(config.bitcoind).getUnspentTransactionOutputs(outputs, { extension: 'hex' });
505515

506-
result.should.have.keys('bitmap', 'chainHeight', 'chaintipHash', 'utxos');
507-
result.chainHeight.should.be.a.Number();
516+
binaryUnspents.should.be.instanceOf(Buffer);
517+
hexUnspents.should.equal(`${binaryUnspents.toString('hex')}\n`);
508518
});
509519
});
510520

0 commit comments

Comments
 (0)