Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@thorwallet/xchain-bitcoin

Package Overview
Dependencies
Maintainers
3
Versions
106
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@thorwallet/xchain-bitcoin - npm Package Compare versions

Comparing version 0.1.0-alpha.10 to 0.1.0-alpha.11

12

lib/blockstream-api.js

@@ -1,3 +0,6 @@

import { __awaiter } from "tslib";
import axios from 'axios';
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.broadcastTx = void 0;
const tslib_1 = require("tslib");
const axios_1 = tslib_1.__importDefault(require("axios"));
/**

@@ -11,3 +14,3 @@ * Broadcast transaction.

*/
export const broadcastTx = ({ network, txHex, blockstreamUrl }) => __awaiter(void 0, void 0, void 0, function* () {
const broadcastTx = ({ network, txHex, blockstreamUrl }) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
try {

@@ -30,3 +33,3 @@ if (network === 'mainnet') {

const url = network === 'testnet' ? `${blockstreamUrl}/testnet/api/tx` : `${blockstreamUrl}/api/tx`;
const txid = (yield axios.post(url, txHex)).data;
const txid = (yield axios_1.default.post(url, txHex)).data;
return txid;

@@ -38,2 +41,3 @@ }

});
exports.broadcastTx = broadcastTx;
//# sourceMappingURL=blockstream-api.js.map

@@ -1,9 +0,12 @@

import { __awaiter } from "tslib";
import * as Bitcoin from 'bitcoinjs-lib';
import * as Utils from './utils';
import * as sochain from './sochain-api';
import { validatePhrase, getSeed, bip32 } from '@thorwallet/xchain-crypto';
import { AssetBTC, assetAmount, assetToBase } from '@thorwallet/xchain-util';
import RNSimple from 'react-native-simple-crypto';
import { getAddress } from './get-address';
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Client = void 0;
const tslib_1 = require("tslib");
const Bitcoin = tslib_1.__importStar(require("bitcoinjs-lib"));
const Utils = tslib_1.__importStar(require("./utils"));
const sochain = tslib_1.__importStar(require("./sochain-api"));
const xchain_crypto_1 = require("@thorwallet/xchain-crypto");
const xchain_util_1 = require("@thorwallet/xchain-util");
const react_native_simple_crypto_1 = tslib_1.__importDefault(require("react-native-simple-crypto"));
const get_address_1 = require("./get-address");
/**

@@ -53,7 +56,7 @@ * Custom Bitcoin client

*/
this.setPhrase = (phrase, walletIndex = 0) => __awaiter(this, void 0, void 0, function* () {
if (validatePhrase(phrase)) {
this.setPhrase = (phrase, walletIndex = 0) => tslib_1.__awaiter(this, void 0, void 0, function* () {
if (xchain_crypto_1.validatePhrase(phrase)) {
this.phrase = phrase;
this.addrCache[phrase] = {};
return getAddress({ phrase, network: this.getNetwork(), index: walletIndex });
return get_address_1.getAddress({ phrase, network: this.getNetwork(), index: walletIndex });
}

@@ -133,6 +136,6 @@ else {

* */
this.getBtcKeys = (phrase, index = 0) => __awaiter(this, void 0, void 0, function* () {
this.getBtcKeys = (phrase, index = 0) => tslib_1.__awaiter(this, void 0, void 0, function* () {
const btcNetwork = Utils.btcNetwork(this.net);
const seed = yield getSeed(phrase);
const master = yield (yield bip32.fromSeed(seed, btcNetwork)).derivePath(this.getFullDerivationPath(index));
const seed = yield xchain_crypto_1.getSeed(phrase);
const master = yield (yield xchain_crypto_1.bip32.fromSeed(seed, btcNetwork)).derivePath(this.getFullDerivationPath(index));
if (!master.privateKey) {

@@ -157,3 +160,3 @@ throw new Error('Could not get private key from phrase');

*/
this.getTransactions = (params) => __awaiter(this, void 0, void 0, function* () {
this.getTransactions = (params) => tslib_1.__awaiter(this, void 0, void 0, function* () {
var _a;

@@ -179,10 +182,10 @@ // Sochain API doesn't have pagination parameter

const tx = {
asset: AssetBTC,
asset: xchain_util_1.AssetBTC,
from: rawTx.inputs.map((i) => ({
from: i.address,
amount: assetToBase(assetAmount(i.value, Utils.BTC_DECIMAL)),
amount: xchain_util_1.assetToBase(xchain_util_1.assetAmount(i.value, Utils.BTC_DECIMAL)),
})),
to: rawTx.outputs
.filter((i) => i.type !== 'nulldata')
.map((i) => ({ to: i.address, amount: assetToBase(assetAmount(i.value, Utils.BTC_DECIMAL)) })),
.map((i) => ({ to: i.address, amount: xchain_util_1.assetToBase(xchain_util_1.assetAmount(i.value, Utils.BTC_DECIMAL)) })),
date: new Date(rawTx.time * 1000),

@@ -219,3 +222,3 @@ type: 'transfer',

*/
this.getTransactionData = (txId) => __awaiter(this, void 0, void 0, function* () {
this.getTransactionData = (txId) => tslib_1.__awaiter(this, void 0, void 0, function* () {
try {

@@ -228,8 +231,8 @@ const rawTx = yield sochain.getTx({

return {
asset: AssetBTC,
asset: xchain_util_1.AssetBTC,
from: rawTx.inputs.map((i) => ({
from: i.address,
amount: assetToBase(assetAmount(i.value, Utils.BTC_DECIMAL)),
amount: xchain_util_1.assetToBase(xchain_util_1.assetAmount(i.value, Utils.BTC_DECIMAL)),
})),
to: rawTx.outputs.map((i) => ({ to: i.address, amount: assetToBase(assetAmount(i.value, Utils.BTC_DECIMAL)) })),
to: rawTx.outputs.map((i) => ({ to: i.address, amount: xchain_util_1.assetToBase(xchain_util_1.assetAmount(i.value, Utils.BTC_DECIMAL)) })),
date: new Date(rawTx.time * 1000),

@@ -259,3 +262,3 @@ type: 'transfer',

*/
this.getFeesWithRates = (memo) => __awaiter(this, void 0, void 0, function* () {
this.getFeesWithRates = (memo) => tslib_1.__awaiter(this, void 0, void 0, function* () {
const txFee = yield sochain.getSuggestedTxFee();

@@ -280,3 +283,3 @@ const rates = {

*/
this.getFees = () => __awaiter(this, void 0, void 0, function* () {
this.getFees = () => tslib_1.__awaiter(this, void 0, void 0, function* () {
try {

@@ -297,3 +300,3 @@ const { fees } = yield this.getFeesWithRates();

*/
this.getFeesWithMemo = (memo) => __awaiter(this, void 0, void 0, function* () {
this.getFeesWithMemo = (memo) => tslib_1.__awaiter(this, void 0, void 0, function* () {
try {

@@ -313,3 +316,3 @@ const { fees } = yield this.getFeesWithRates(memo);

*/
this.getFeeRates = () => __awaiter(this, void 0, void 0, function* () {
this.getFeeRates = () => tslib_1.__awaiter(this, void 0, void 0, function* () {
try {

@@ -331,4 +334,4 @@ const { rates } = yield this.getFeesWithRates();

*/
this.signMessage = (msg) => __awaiter(this, void 0, void 0, function* () {
const msgHash = yield RNSimple.SHA.sha256(msg);
this.signMessage = (msg) => tslib_1.__awaiter(this, void 0, void 0, function* () {
const msgHash = yield react_native_simple_crypto_1.default.SHA.sha256(msg);
const msgBuffer = Buffer.from(msgHash, 'hex');

@@ -346,3 +349,3 @@ const keys = yield this.getBtcKeys(this.phrase);

*/
this.transfer = (params) => __awaiter(this, void 0, void 0, function* () {
this.transfer = (params) => tslib_1.__awaiter(this, void 0, void 0, function* () {
try {

@@ -357,3 +360,3 @@ const fromAddressIndex = (params === null || params === void 0 ? void 0 : params.walletIndex) || 0;

const spendPendingUTXO = params.memo ? false : true;
const { psbt } = yield Utils.buildTx(Object.assign(Object.assign({}, params), { feeRate, sender: yield getAddress({ network: this.getNetwork(), phrase: this.phrase, index: fromAddressIndex }), sochainUrl: this.sochainUrl, network: this.net, spendPendingUTXO }));
const { psbt } = yield Utils.buildTx(Object.assign(Object.assign({}, params), { feeRate, sender: yield get_address_1.getAddress({ network: this.getNetwork(), phrase: this.phrase, index: fromAddressIndex }), sochainUrl: this.sochainUrl, network: this.net, spendPendingUTXO }));
const btcKeys = this.getBtcKeys(this.phrase, fromAddressIndex);

@@ -385,3 +388,3 @@ psbt.signAllInputs(yield btcKeys); // Sign all inputs

}
export { Client };
exports.Client = Client;
//# sourceMappingURL=client.js.map

@@ -0,1 +1,4 @@

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MIN_TX_FEE = void 0;
/**

@@ -6,3 +9,3 @@ * Minimum transaction fee

*/
export const MIN_TX_FEE = 1000;
exports.MIN_TX_FEE = 1000;
//# sourceMappingURL=const.js.map

@@ -1,6 +0,9 @@

import { __awaiter } from "tslib";
import * as Bitcoin from 'bitcoinjs-lib';
import * as Utils from './utils';
import { getSeed } from '@thorwallet/xchain-crypto/lib';
import { bip32 } from '@thorwallet/xchain-crypto';
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getAddress = void 0;
const tslib_1 = require("tslib");
const Bitcoin = tslib_1.__importStar(require("bitcoinjs-lib"));
const Utils = tslib_1.__importStar(require("./utils"));
const lib_1 = require("@thorwallet/xchain-crypto/lib");
const xchain_crypto_1 = require("@thorwallet/xchain-crypto");
const addrCache = {};

@@ -14,6 +17,6 @@ const rootDerivationPaths = {

};
const getBtcKeys = (network, phrase, index) => __awaiter(void 0, void 0, void 0, function* () {
const getBtcKeys = (network, phrase, index) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
const btcNetwork = Utils.btcNetwork(network);
const seed = yield getSeed(phrase);
const master = yield (yield bip32.fromSeed(seed, btcNetwork)).derivePath(getFullDerivationPath(network, index));
const seed = yield lib_1.getSeed(phrase);
const master = yield (yield xchain_crypto_1.bip32.fromSeed(seed, btcNetwork)).derivePath(getFullDerivationPath(network, index));
if (!master.privateKey) {

@@ -24,3 +27,3 @@ throw new Error('Could not get private key from phrase');

});
export const getAddress = ({ network, phrase, index, }) => __awaiter(void 0, void 0, void 0, function* () {
const getAddress = ({ network, phrase, index, }) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
if (index < 0) {

@@ -47,2 +50,3 @@ throw new Error('index must be greater than zero');

});
exports.getAddress = getAddress;
//# sourceMappingURL=get-address.js.map

@@ -1,3 +0,7 @@

import * as Utils from './utils';
export const getBalance = ({ network, address }) => {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getBalance = void 0;
const tslib_1 = require("tslib");
const Utils = tslib_1.__importStar(require("./utils"));
const getBalance = ({ network, address }) => {
return Utils.getBalance({

@@ -9,2 +13,3 @@ sochainUrl: 'https://sochain.com/api/v2',

};
exports.getBalance = getBalance;
//# sourceMappingURL=get-balance.js.map

@@ -1,24 +0,29 @@

import { __awaiter } from "tslib";
import { baseAmount } from '@thorwallet/xchain-util/lib';
import axios from 'axios';
import { getIsTxConfirmed } from './sochain-api';
import { BTC_DECIMAL } from './utils';
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getConfirmedUnspentTxs = exports.getUnspentTxs = exports.getBalance = void 0;
const tslib_1 = require("tslib");
const lib_1 = require("@thorwallet/xchain-util/lib");
const axios_1 = tslib_1.__importDefault(require("axios"));
const sochain_api_1 = require("./sochain-api");
const utils_1 = require("./utils");
const HASKOIN_API_URL = 'https://api.haskoin.com/btc';
const SOCHAIN_API_URL = 'https://sochain.com/api/v2';
export const getBalance = (address) => __awaiter(void 0, void 0, void 0, function* () {
const { data: { confirmed, unconfirmed }, } = yield axios.get(`${HASKOIN_API_URL}/address/${address}/balance`);
const confirmedAmount = baseAmount(confirmed, BTC_DECIMAL);
const unconfirmedAmount = baseAmount(unconfirmed, BTC_DECIMAL);
const getBalance = (address) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
const { data: { confirmed, unconfirmed }, } = yield axios_1.default.get(`${HASKOIN_API_URL}/address/${address}/balance`);
const confirmedAmount = lib_1.baseAmount(confirmed, utils_1.BTC_DECIMAL);
const unconfirmedAmount = lib_1.baseAmount(unconfirmed, utils_1.BTC_DECIMAL);
return confirmedAmount.plus(unconfirmedAmount);
});
export const getUnspentTxs = (address) => __awaiter(void 0, void 0, void 0, function* () {
const { data: response } = yield axios.get(`${HASKOIN_API_URL}/address/${address}/unspent`);
exports.getBalance = getBalance;
const getUnspentTxs = (address) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
const { data: response } = yield axios_1.default.get(`${HASKOIN_API_URL}/address/${address}/unspent`);
return response;
});
export const getConfirmedUnspentTxs = (address) => __awaiter(void 0, void 0, void 0, function* () {
exports.getUnspentTxs = getUnspentTxs;
const getConfirmedUnspentTxs = (address) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
try {
const allUtxos = yield getUnspentTxs(address);
const allUtxos = yield exports.getUnspentTxs(address);
const confirmedUTXOs = [];
yield Promise.all(allUtxos.map((tx) => __awaiter(void 0, void 0, void 0, function* () {
const { is_confirmed: isTxConfirmed } = yield getIsTxConfirmed({
yield Promise.all(allUtxos.map((tx) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
const { is_confirmed: isTxConfirmed } = yield sochain_api_1.getIsTxConfirmed({
sochainUrl: SOCHAIN_API_URL,

@@ -38,2 +43,3 @@ network: 'mainnet',

});
exports.getConfirmedUnspentTxs = getConfirmedUnspentTxs;
//# sourceMappingURL=haskoin-api.js.map

@@ -1,7 +0,23 @@

export { getAddress } from './get-address';
export { getBalance } from './get-balance';
export * from './types';
export * from './client';
export { broadcastTx, getDefaultFees, getDefaultFeesWithRates, getPrefix, BTC_DECIMAL, scanUTXOs, buildTx, validateAddress, calcFee, } from './utils';
export { createTxInfo } from './ledger';
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createTxInfo = exports.calcFee = exports.validateAddress = exports.buildTx = exports.scanUTXOs = exports.BTC_DECIMAL = exports.getPrefix = exports.getDefaultFeesWithRates = exports.getDefaultFees = exports.broadcastTx = exports.getBalance = exports.getAddress = void 0;
const tslib_1 = require("tslib");
var get_address_1 = require("./get-address");
Object.defineProperty(exports, "getAddress", { enumerable: true, get: function () { return get_address_1.getAddress; } });
var get_balance_1 = require("./get-balance");
Object.defineProperty(exports, "getBalance", { enumerable: true, get: function () { return get_balance_1.getBalance; } });
tslib_1.__exportStar(require("./types"), exports);
tslib_1.__exportStar(require("./client"), exports);
var utils_1 = require("./utils");
Object.defineProperty(exports, "broadcastTx", { enumerable: true, get: function () { return utils_1.broadcastTx; } });
Object.defineProperty(exports, "getDefaultFees", { enumerable: true, get: function () { return utils_1.getDefaultFees; } });
Object.defineProperty(exports, "getDefaultFeesWithRates", { enumerable: true, get: function () { return utils_1.getDefaultFeesWithRates; } });
Object.defineProperty(exports, "getPrefix", { enumerable: true, get: function () { return utils_1.getPrefix; } });
Object.defineProperty(exports, "BTC_DECIMAL", { enumerable: true, get: function () { return utils_1.BTC_DECIMAL; } });
Object.defineProperty(exports, "scanUTXOs", { enumerable: true, get: function () { return utils_1.scanUTXOs; } });
Object.defineProperty(exports, "buildTx", { enumerable: true, get: function () { return utils_1.buildTx; } });
Object.defineProperty(exports, "validateAddress", { enumerable: true, get: function () { return utils_1.validateAddress; } });
Object.defineProperty(exports, "calcFee", { enumerable: true, get: function () { return utils_1.calcFee; } });
var ledger_1 = require("./ledger");
Object.defineProperty(exports, "createTxInfo", { enumerable: true, get: function () { return ledger_1.createTxInfo; } });
//# sourceMappingURL=index.js.map

@@ -1,3 +0,6 @@

import { __awaiter } from "tslib";
import * as Utils from './utils';
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createTxInfo = void 0;
const tslib_1 = require("tslib");
const Utils = tslib_1.__importStar(require("./utils"));
/**

@@ -9,3 +12,3 @@ * Create transaction info.

*/
export const createTxInfo = (params) => __awaiter(void 0, void 0, void 0, function* () {
const createTxInfo = (params) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
try {

@@ -22,2 +25,3 @@ const { psbt, utxos } = yield Utils.buildTx(params);

});
exports.createTxInfo = createTxInfo;
//# sourceMappingURL=ledger.js.map

@@ -1,5 +0,8 @@

import { __awaiter } from "tslib";
import axios from 'axios';
import { assetToBase, assetAmount } from '@thorwallet/xchain-util';
import { BTC_DECIMAL } from './utils';
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getSuggestedTxFee = exports.getConfirmedUnspentTxs = exports.getIsTxConfirmed = exports.getUnspentTxs = exports.getBalance = exports.getTx = exports.getAddress = void 0;
const tslib_1 = require("tslib");
const axios_1 = tslib_1.__importDefault(require("axios"));
const xchain_util_1 = require("@thorwallet/xchain-util");
const utils_1 = require("./utils");
const DEFAULT_SUGGESTED_TRANSACTION_FEE = 127;

@@ -19,6 +22,6 @@ const toSochainNetwork = (net) => {

*/
export const getAddress = ({ sochainUrl, network, address }) => __awaiter(void 0, void 0, void 0, function* () {
const getAddress = ({ sochainUrl, network, address }) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
try {
const url = `${sochainUrl}/address/${toSochainNetwork(network)}/${address}`;
const response = yield axios.get(url);
const response = yield axios_1.default.get(url);
const addressResponse = response.data;

@@ -31,2 +34,3 @@ return addressResponse.data;

});
exports.getAddress = getAddress;
/**

@@ -42,6 +46,6 @@ * Get transaction by hash.

*/
export const getTx = ({ sochainUrl, network, hash }) => __awaiter(void 0, void 0, void 0, function* () {
const getTx = ({ sochainUrl, network, hash }) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
try {
const url = `${sochainUrl}/get_tx/${toSochainNetwork(network)}/${hash.toLowerCase()}`;
const response = yield axios.get(url);
const response = yield axios_1.default.get(url);
const tx = response.data;

@@ -54,2 +58,3 @@ return tx.data;

});
exports.getTx = getTx;
/**

@@ -65,11 +70,11 @@ * Get address balance.

*/
export const getBalance = ({ sochainUrl, network, address }) => __awaiter(void 0, void 0, void 0, function* () {
const getBalance = ({ sochainUrl, network, address }) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
try {
const url = `${sochainUrl}/get_address_balance/${toSochainNetwork(network)}/${address}`;
const response = yield axios.get(url);
const response = yield axios_1.default.get(url);
const balanceResponse = response.data;
const confirmed = assetAmount(balanceResponse.data.confirmed_balance, BTC_DECIMAL);
const unconfirmed = assetAmount(balanceResponse.data.unconfirmed_balance, BTC_DECIMAL);
const confirmed = xchain_util_1.assetAmount(balanceResponse.data.confirmed_balance, utils_1.BTC_DECIMAL);
const unconfirmed = xchain_util_1.assetAmount(balanceResponse.data.unconfirmed_balance, utils_1.BTC_DECIMAL);
const netAmt = confirmed.amount().plus(unconfirmed.amount());
const result = assetToBase(assetAmount(netAmt, BTC_DECIMAL));
const result = xchain_util_1.assetToBase(xchain_util_1.assetAmount(netAmt, utils_1.BTC_DECIMAL));
return result;

@@ -81,2 +86,3 @@ }

});
exports.getBalance = getBalance;
/**

@@ -92,10 +98,10 @@ * Get unspent txs

*/
export const getUnspentTxs = ({ sochainUrl, network, address, startingFromTxId, }) => __awaiter(void 0, void 0, void 0, function* () {
const getUnspentTxs = ({ sochainUrl, network, address, startingFromTxId, }) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
try {
let resp = null;
if (startingFromTxId) {
resp = yield axios.get(`${sochainUrl}/get_tx_unspent/${toSochainNetwork(network)}/${address}/${startingFromTxId}`);
resp = yield axios_1.default.get(`${sochainUrl}/get_tx_unspent/${toSochainNetwork(network)}/${address}/${startingFromTxId}`);
}
else {
resp = yield axios.get(`${sochainUrl}/get_tx_unspent/${toSochainNetwork(network)}/${address}`);
resp = yield axios_1.default.get(`${sochainUrl}/get_tx_unspent/${toSochainNetwork(network)}/${address}`);
}

@@ -107,3 +113,3 @@ const response = resp.data;

const lastTxId = txs[99].txid;
const nextBatch = yield getUnspentTxs({
const nextBatch = yield exports.getUnspentTxs({
sochainUrl,

@@ -124,2 +130,3 @@ network,

});
exports.getUnspentTxs = getUnspentTxs;
/**

@@ -135,5 +142,5 @@ * Get Tx Confirmation status

*/
export const getIsTxConfirmed = ({ sochainUrl, network, hash }) => __awaiter(void 0, void 0, void 0, function* () {
const getIsTxConfirmed = ({ sochainUrl, network, hash }) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
try {
const { data } = yield axios.get(`${sochainUrl}/is_tx_confirmed/${toSochainNetwork(network)}/${hash.toLowerCase()}`);
const { data } = yield axios_1.default.get(`${sochainUrl}/is_tx_confirmed/${toSochainNetwork(network)}/${hash.toLowerCase()}`);
return data.data;

@@ -145,2 +152,3 @@ }

});
exports.getIsTxConfirmed = getIsTxConfirmed;
/**

@@ -156,5 +164,5 @@ * Get unspent txs and filter out pending UTXOs

*/
export const getConfirmedUnspentTxs = ({ sochainUrl, network, address, }) => __awaiter(void 0, void 0, void 0, function* () {
const getConfirmedUnspentTxs = ({ sochainUrl, network, address, }) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
try {
const txs = yield getUnspentTxs({
const txs = yield exports.getUnspentTxs({
sochainUrl,

@@ -165,4 +173,4 @@ network,

const confirmedUTXOs = [];
yield Promise.all(txs.map((tx) => __awaiter(void 0, void 0, void 0, function* () {
const { is_confirmed: isTxConfirmed } = yield getIsTxConfirmed({
yield Promise.all(txs.map((tx) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
const { is_confirmed: isTxConfirmed } = yield exports.getIsTxConfirmed({
sochainUrl,

@@ -182,2 +190,3 @@ network,

});
exports.getConfirmedUnspentTxs = getConfirmedUnspentTxs;
/**

@@ -188,3 +197,3 @@ * Get Bitcoin suggested transaction fee.

*/
export const getSuggestedTxFee = () => __awaiter(void 0, void 0, void 0, function* () {
const getSuggestedTxFee = () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
//Note: sochain does not provide fee rate related data

@@ -194,3 +203,3 @@ //So use Bitgo API for fee estimation

try {
const response = yield axios.get('https://app.bitgo.com/api/v2/btc/tx/fee');
const response = yield axios_1.default.get('https://app.bitgo.com/api/v2/btc/tx/fee');
return response.data.feePerKb / 1000; // feePerKb to feePerByte

@@ -202,2 +211,3 @@ }

});
exports.getSuggestedTxFee = getSuggestedTxFee;
//# sourceMappingURL=sochain-api.js.map

@@ -1,2 +0,3 @@

export {};
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
//# sourceMappingURL=client-types.js.map

@@ -1,2 +0,3 @@

export {};
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
//# sourceMappingURL=common.js.map

@@ -1,4 +0,7 @@

export * from './client-types';
export * from './common';
export * from './ledger';
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
tslib_1.__exportStar(require("./client-types"), exports);
tslib_1.__exportStar(require("./common"), exports);
tslib_1.__exportStar(require("./ledger"), exports);
//# sourceMappingURL=index.js.map

@@ -1,2 +0,3 @@

export {};
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
//# sourceMappingURL=ledger.js.map

@@ -1,2 +0,3 @@

export {};
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
//# sourceMappingURL=sochain-api-types.js.map

@@ -1,11 +0,14 @@

import { __awaiter } from "tslib";
import * as Bitcoin from 'bitcoinjs-lib'; // https://github.com/bitcoinjs/bitcoinjs-lib
import * as sochain from './sochain-api';
import * as blockStream from './blockstream-api';
import { assetAmount, AssetBTC, assetToBase, baseAmount } from '@thorwallet/xchain-util';
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getPrefix = exports.getDefaultFees = exports.getDefaultFeesWithRates = exports.calcFee = exports.broadcastTx = exports.buildTx = exports.scanUTXOs = exports.validateAddress = exports.getBalance = exports.btcNetwork = exports.isTestnet = exports.arrayAverage = exports.getFee = exports.compileMemo = exports.BTC_DECIMAL = void 0;
const tslib_1 = require("tslib");
const Bitcoin = tslib_1.__importStar(require("bitcoinjs-lib")); // https://github.com/bitcoinjs/bitcoinjs-lib
const sochain = tslib_1.__importStar(require("./sochain-api"));
const blockStream = tslib_1.__importStar(require("./blockstream-api"));
const xchain_util_1 = require("@thorwallet/xchain-util");
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import accumulative from 'coinselect/accumulative';
import { MIN_TX_FEE } from './const';
import * as haskoinApi from './haskoin-api';
const accumulative_1 = tslib_1.__importDefault(require("coinselect/accumulative"));
const const_1 = require("./const");
const haskoinApi = tslib_1.__importStar(require("./haskoin-api"));
const TX_EMPTY_SIZE = 4 + 1 + 1 + 4; //10

@@ -16,3 +19,3 @@ const TX_INPUT_BASE = 32 + 4 + 1 + 4; // 41

const TX_OUTPUT_PUBKEYHASH = 25;
export const BTC_DECIMAL = 8;
exports.BTC_DECIMAL = 8;
const inputBytes = (input) => {

@@ -27,6 +30,7 @@ return TX_INPUT_BASE + (input.witnessUtxo.script ? input.witnessUtxo.script.length : TX_INPUT_PUBKEYHASH);

*/
export const compileMemo = (memo) => {
const compileMemo = (memo) => {
const data = Buffer.from(memo, 'utf8'); // converts MEMO to buffer
return Bitcoin.script.compile([Bitcoin.opcodes.OP_RETURN, data]); // Compile OP_RETURN script
};
exports.compileMemo = compileMemo;
/**

@@ -40,3 +44,3 @@ * Get the transaction fee.

*/
export const getFee = (inputs, feeRate, data = null) => {
const getFee = (inputs, feeRate, data = null) => {
let sum = TX_EMPTY_SIZE +

@@ -53,4 +57,5 @@ inputs.reduce((a, x) => a + inputBytes(x), 0) +

const fee = sum * feeRate;
return fee > MIN_TX_FEE ? fee : MIN_TX_FEE;
return fee > const_1.MIN_TX_FEE ? fee : const_1.MIN_TX_FEE;
};
exports.getFee = getFee;
/**

@@ -62,3 +67,3 @@ * Get the average value of an array.

*/
export const arrayAverage = (array) => {
const arrayAverage = (array) => {
let sum = 0;

@@ -68,2 +73,3 @@ array.forEach((value) => (sum += value));

};
exports.arrayAverage = arrayAverage;
/**

@@ -75,5 +81,6 @@ * Check if give network is a testnet.

*/
export const isTestnet = (network) => {
const isTestnet = (network) => {
return network === 'testnet';
};
exports.isTestnet = isTestnet;
/**

@@ -85,5 +92,6 @@ * Get Bitcoin network to be used with bitcoinjs.

*/
export const btcNetwork = (network) => {
return isTestnet(network) ? Bitcoin.networks.testnet : Bitcoin.networks.bitcoin;
const btcNetwork = (network) => {
return exports.isTestnet(network) ? Bitcoin.networks.testnet : Bitcoin.networks.bitcoin;
};
exports.btcNetwork = btcNetwork;
/**

@@ -97,3 +105,3 @@ * Get the balances of an address.

*/
export const getBalance = (params) => __awaiter(void 0, void 0, void 0, function* () {
const getBalance = (params) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
try {

@@ -103,3 +111,3 @@ const balance = params.network === 'testnet' ? yield sochain.getBalance(params) : yield haskoinApi.getBalance(params.address);

{
asset: AssetBTC,
asset: xchain_util_1.AssetBTC,
amount: balance,

@@ -113,2 +121,3 @@ },

});
exports.getBalance = getBalance;
/**

@@ -121,5 +130,5 @@ * Validate the BTC address.

*/
export const validateAddress = (address, network) => {
const validateAddress = (address, network) => {
try {
Bitcoin.address.toOutputScript(address, btcNetwork(network));
Bitcoin.address.toOutputScript(address, exports.btcNetwork(network));
return true;

@@ -131,2 +140,3 @@ }

};
exports.validateAddress = validateAddress;
/**

@@ -140,4 +150,4 @@ * Scan UTXOs from sochain.

*/
export const scanUTXOs = ({ sochainUrl, network, address, confirmedOnly = true, // default: scan only confirmed UTXOs
}) => __awaiter(void 0, void 0, void 0, function* () {
const scanUTXOs = ({ sochainUrl, network, address, confirmedOnly = true, // default: scan only confirmed UTXOs
}) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
if (network === 'testnet') {

@@ -159,5 +169,5 @@ let utxos = [];

index: utxo.output_no,
value: assetToBase(assetAmount(utxo.value, BTC_DECIMAL)).amount().toNumber(),
value: xchain_util_1.assetToBase(xchain_util_1.assetAmount(utxo.value, exports.BTC_DECIMAL)).amount().toNumber(),
witnessUtxo: {
value: assetToBase(assetAmount(utxo.value, BTC_DECIMAL)).amount().toNumber(),
value: xchain_util_1.assetToBase(xchain_util_1.assetAmount(utxo.value, exports.BTC_DECIMAL)).amount().toNumber(),
script: Buffer.from(utxo.script_hex, 'hex'),

@@ -177,5 +187,5 @@ },

index: utxo.index,
value: baseAmount(utxo.value, BTC_DECIMAL).amount().toNumber(),
value: xchain_util_1.baseAmount(utxo.value, exports.BTC_DECIMAL).amount().toNumber(),
witnessUtxo: {
value: baseAmount(utxo.value, BTC_DECIMAL).amount().toNumber(),
value: xchain_util_1.baseAmount(utxo.value, exports.BTC_DECIMAL).amount().toNumber(),
script: Buffer.from(utxo.pkscript, 'hex'),

@@ -185,2 +195,3 @@ },

});
exports.scanUTXOs = scanUTXOs;
/**

@@ -192,16 +203,16 @@ * Build transcation.

*/
export const buildTx = ({ amount, recipient, memo, feeRate, sender, network, sochainUrl, spendPendingUTXO = false, // default: prevent spending uncomfirmed UTXOs
}) => __awaiter(void 0, void 0, void 0, function* () {
const buildTx = ({ amount, recipient, memo, feeRate, sender, network, sochainUrl, spendPendingUTXO = false, // default: prevent spending uncomfirmed UTXOs
}) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
try {
// search only confirmed UTXOs if pending UTXO is not allowed
const confirmedOnly = !spendPendingUTXO;
const utxos = yield scanUTXOs({ sochainUrl, network, address: sender, confirmedOnly });
const utxos = yield exports.scanUTXOs({ sochainUrl, network, address: sender, confirmedOnly });
if (utxos.length === 0) {
return Promise.reject(Error('No utxos to send'));
}
if (!validateAddress(recipient, network)) {
if (!exports.validateAddress(recipient, network)) {
return Promise.reject(new Error('Invalid address'));
}
const feeRateWhole = Number(feeRate.toFixed(0));
const compiledMemo = memo ? compileMemo(memo) : null;
const compiledMemo = memo ? exports.compileMemo(memo) : null;
const targetOutputs = [];

@@ -217,3 +228,3 @@ //1. add output amount and recipient to targets

}
const { inputs, outputs } = accumulative(utxos, targetOutputs, feeRateWhole);
const { inputs, outputs } = accumulative_1.default(utxos, targetOutputs, feeRateWhole);
// .inputs and .outputs will be undefined if no solution was found

@@ -223,3 +234,3 @@ if (!inputs || !outputs) {

}
const psbt = new Bitcoin.Psbt({ network: btcNetwork(network) }); // Network-specific
const psbt = new Bitcoin.Psbt({ network: exports.btcNetwork(network) }); // Network-specific
// psbt add input from accumulative inputs

@@ -254,2 +265,3 @@ inputs.forEach((utxo) => psbt.addInput({

});
exports.buildTx = buildTx;
/**

@@ -261,5 +273,6 @@ * Broadcast the transaction.

*/
export const broadcastTx = ({ network, txHex, blockstreamUrl }) => __awaiter(void 0, void 0, void 0, function* () {
const broadcastTx = ({ network, txHex, blockstreamUrl }) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
return yield blockStream.broadcastTx({ network, txHex, blockstreamUrl });
});
exports.broadcastTx = broadcastTx;
/**

@@ -272,7 +285,8 @@ * Calculate fees based on fee rate and memo.

*/
export const calcFee = (feeRate, memo) => {
const compiledMemo = memo ? compileMemo(memo) : null;
const fee = getFee([], feeRate, compiledMemo);
return baseAmount(fee);
const calcFee = (feeRate, memo) => {
const compiledMemo = memo ? exports.compileMemo(memo) : null;
const fee = exports.getFee([], feeRate, compiledMemo);
return xchain_util_1.baseAmount(fee);
};
exports.calcFee = calcFee;
/**

@@ -283,3 +297,3 @@ * Get the default fees with rates.

*/
export const getDefaultFeesWithRates = () => {
const getDefaultFeesWithRates = () => {
const rates = {

@@ -292,5 +306,5 @@ fastest: 50,

type: 'byte',
fast: calcFee(rates.fast),
average: calcFee(rates.average),
fastest: calcFee(rates.fastest),
fast: exports.calcFee(rates.fast),
average: exports.calcFee(rates.average),
fastest: exports.calcFee(rates.fastest),
};

@@ -302,2 +316,3 @@ return {

};
exports.getDefaultFeesWithRates = getDefaultFeesWithRates;
/**

@@ -308,6 +323,7 @@ * Get the default fees.

*/
export const getDefaultFees = () => {
const { fees } = getDefaultFeesWithRates();
const getDefaultFees = () => {
const { fees } = exports.getDefaultFeesWithRates();
return fees;
};
exports.getDefaultFees = getDefaultFees;
/**

@@ -320,3 +336,4 @@ * Get address prefix based on the network.

**/
export const getPrefix = (network) => (network === 'testnet' ? 'tb1' : 'bc1');
const getPrefix = (network) => (network === 'testnet' ? 'tb1' : 'bc1');
exports.getPrefix = getPrefix;
//# sourceMappingURL=utils.js.map
{
"name": "@thorwallet/xchain-bitcoin",
"version": "0.1.0-alpha.10+6e3b743",
"version": "0.1.0-alpha.11+b5afdfe",
"description": "Custom Bitcoin client and utilities used by XChainJS clients",

@@ -34,5 +34,5 @@ "keywords": [

"devDependencies": {
"@thorwallet/xchain-client": "0.1.0-alpha.10+6e3b743",
"@thorwallet/xchain-crypto": "0.1.0-alpha.10+6e3b743",
"@thorwallet/xchain-util": "0.1.0-alpha.10+6e3b743",
"@thorwallet/xchain-client": "0.1.0-alpha.11+b5afdfe",
"@thorwallet/xchain-crypto": "0.1.0-alpha.11+b5afdfe",
"@thorwallet/xchain-util": "0.1.0-alpha.11+b5afdfe",
"@types/bitcoinjs-lib": "^5.0.0",

@@ -60,3 +60,3 @@ "@types/wif": "^2.0.1",

},
"gitHead": "6e3b743eef73050169948f91f38672654a931ae7"
"gitHead": "b5afdfef84291bd9546d576f42f3fbcc14ace2ea"
}

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc