@berrywallet/core
Version:
Berrywallet main Core for work with common cryptocurrencies like Bitcoin, Ethereum, Dash, Litecoin
54 lines (53 loc) • 2.08 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
}
Object.defineProperty(exports, "__esModule", { value: true });
const lodash_1 = require("lodash");
const bitcoinjs_lib_1 = __importDefault(require("bitcoinjs-lib"));
const bignumber_js_1 = __importDefault(require("bignumber.js"));
const _1 = require("../../");
var blockcypher;
(function (blockcypher) {
function toWalletTx(tx, coin) {
const txData = {
coin: coin.getUnit(),
txid: tx.hash,
blockHeight: (tx.block_height && tx.block_height > 0) ? tx.block_height : null,
blockTime: tx.confirmed ? new Date(tx.confirmed).getTime() : null,
scheme: _1.Coin.TransactionScheme.INPUTS_OUTPUTS,
version: tx.ver,
lockTime: tx.lock_time,
inputs: [],
outputs: [],
};
lodash_1.forEach(tx.inputs, (vin) => {
txData.inputs.push({
prevTxid: vin.prev_hash,
prevOutIndex: vin.output_index,
scriptSig: vin.script,
sequence: vin.sequence,
witness: vin.witness,
});
});
lodash_1.forEach(tx.outputs, (vout) => {
const buffer = _1.Utils.hexToBuffer(vout.script);
const type = bitcoinjs_lib_1.default.script.classifyOutput(buffer);
let address;
try {
address = bitcoinjs_lib_1.default.address.fromOutputScript(buffer, coin.networkInfo());
}
catch (e) {
}
txData.outputs.push({
scriptPubKey: vout.script,
scriptType: type,
addresses: address ? [address] : [],
value: new bignumber_js_1.default(vout.value).div(_1.Constants.SATOSHI_PER_COIN).toString(),
});
});
return txData;
}
blockcypher.toWalletTx = toWalletTx;
})(blockcypher || (blockcypher = {}));
exports.default = blockcypher;