~pierrenn/barkaelogy

fc02e2cc802d5270b4396b95a5e21a9328017f45 — pierrenn 3 months ago 388ad82 master
remove useless Parse::write helper
3 files changed, 4 insertions(+), 17 deletions(-)

M README.md
M lib/index.js
M package.json
M README.md => README.md +3 -3
@@ 35,7 35,7 @@ Pass connection information to the `Parser` constructor as described [here](http

### Basic example

Read the [bitcoin whitepaper stored in the blockchain](https://btc.com/54e48e5f5c656b26c3bca14a8c95aa583d07ebe84dde3b7dd4a78f4e4186e713) into a local file `satoshi.pdf`:
Read the [bitcoin whitepaper stored in the blockchain](https://btc.com/54e48e5f5c656b26c3bca14a8c95aa583d07ebe84dde3b7dd4a78f4e4186e713) into a local buffer:

```
const bark = require('barkaelogy');


@@ 43,14 43,14 @@ const whitepaper_txid = "54e48e5f5c656b26c3bca14a8c95aa583d07ebe84dde3b7dd4a78f4

const parser = new bark.Parser({network:"mainnet", username: "foo", password: "bar"});
const data = await parser.extractData([whitepaper_txid]);
await parser.write(data, "satoshi");
// data.file is a buffer containing the file, data.mimetype contains it's mimetype
```

Some helpful functions :

* `getInputData/getOutputData` : retrieves parts of data contained in a raw transaction
* `parseData` : convert parts of data into a file by detecting it's mimetype
* `extractData(txid, parse_input=false)` : extracts data from a list of sequential txids, only checks input if `parse_input=true` (calls previous functions for you)
* `write` : write data to a file, guessing it's extension according to it's mimetype

See the files in the `tests/` directory for more helpers and usage examples.


M lib/index.js => lib/index.js +1 -13
@@ 21,7 21,6 @@ const util = require('util');
const fs = require('fs');
const crc32 = require('buffer-crc32');

const { extension } = require('mime-types');
const { Magic, MAGIC_MIME_TYPE } = require('mmmagic');
const { Transaction, Script, crypto } = require('bitcore-lib');



@@ 52,8 51,7 @@ class Parser {
  constructor (connectionDetails) {
    this.client = new BRPCHelper(connectionDetails);

    // promisify some common functions
    this._writeFile = util.promisify(fs.writeFile);
    // promisify mmmagic detect function
    const myMagic = new Magic(MAGIC_MIME_TYPE);
    this._magicDetect = (buffer) => {
      return new Promise((resolve, reject) => {


@@ 235,16 233,6 @@ class Parser {
    const possible_txids = await this.client.getScripts(root_txids);
    return this._fromApertus(possible_txids);
  }

  // Write data.file to filename trying to guess it's extension via data.mimetype
  async write (data, filename) {
    if (data.mimetype === UNKNOWN_MIME) {
      throw new Error("writeBuffer: can't write a buffer if we don't know it's type");
    }

    const complete_name = filename + '.' + extension(data.mimetype);
    return this._writeFile(complete_name, data.file);
  }
}

// ************************

M package.json => package.json +0 -1
@@ 32,7 32,6 @@
    "bitcoin-core": "^3.0.0",
    "bitcore-lib": "^8.16.2",
    "buffer-crc32": "^0.2.13",
    "mime-types": "^2.1.26",
    "mmmagic": "^0.5.3"
  },
  "devDependencies": {