@thorwallet/xchain-bitcoincash
Version:
Custom bitcoincash client and utilities used by XChainJS clients
57 lines • 2.31 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getAddress = void 0;
const tslib_1 = require("tslib");
const lib_1 = require("@thorwallet/xchain-crypto/lib");
const BigInteger = require('bigi');
const bitcash = require('@psf/bitcoincashjs-lib');
const utils = tslib_1.__importStar(require("./utils"));
const addrCache = {};
const ENABLE_FAST = true;
const rootDerivationPaths = {
mainnet: `m/44'/145'/0'/0/`,
testnet: `m/44'/1'/0'/0/`,
};
const getBCHKeys = (network, phrase, derivationPath) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
try {
const rootSeed = yield lib_1.getSeed(phrase);
if (ENABLE_FAST) {
const master = yield (yield lib_1.bip32.fromSeed(rootSeed, utils.bchNetwork(network))).derivePath(derivationPath);
const d = BigInteger.fromBuffer(master.privateKey);
const btcKeyPair = new bitcash.ECPair(d, null, {
network: utils.bchNetwork(network),
compressed: true,
});
return btcKeyPair;
}
const masterHDNode = bitcash.HDNode.fromSeedBuffer(rootSeed, utils.bchNetwork(network));
const keyPair = yield masterHDNode.derivePath(derivationPath).keyPair;
return keyPair;
}
catch (error) {
throw new Error(`Getting key pair failed: ${(error === null || error === void 0 ? void 0 : error.message) || error.toString()}`);
}
});
const getFullDerivationPath = (network, index) => {
return rootDerivationPaths[network] + `${index}`;
};
const getAddress = ({ network, phrase, index = 0, }) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
if (addrCache[phrase] && addrCache[phrase][index]) {
return addrCache[phrase][index];
}
try {
const keys = yield getBCHKeys(network, phrase, getFullDerivationPath(network, index));
const address = yield keys.getAddress(index);
const addr = utils.stripPrefix(utils.toCashAddress(address));
if (!addrCache[phrase]) {
addrCache[phrase] = {};
}
addrCache[phrase][index] = addr;
return addr;
}
catch (error) {
throw new Error('Address not defined');
}
});
exports.getAddress = getAddress;
//# sourceMappingURL=get-address.js.map