UNPKG

@thorwallet/xchain-litecoin

Version:

Custom Litecoin client and utilities used by XChainJS clients

48 lines 1.93 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getAddress = void 0; const tslib_1 = require("tslib"); const Utils = tslib_1.__importStar(require("./utils")); const Litecoin = tslib_1.__importStar(require("bitcoinjs-lib")); // https://github.com/bitcoinjs/bitcoinjs-lib const lib_1 = require("@thorwallet/xchain-crypto/lib"); const addrCache = {}; const rootDerivationPaths = { mainnet: `m/84'/2'/0'/0/`, testnet: `m/84'/1'/0'/0/`, }; const getFullDerivationPath = (network, index) => { return rootDerivationPaths[network] + `${index}`; }; const getLtcKeys = ({ network, phrase, index, }) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { const ltcNetwork = Utils.ltcNetwork(network); const seed = yield lib_1.getSeed(phrase); const master = yield (yield lib_1.bip32.fromSeed(seed, ltcNetwork)).derivePath(getFullDerivationPath(network, index)); if (!master.privateKey) { throw new Error('Could not get private key from phrase'); } return Litecoin.ECPair.fromPrivateKey(master.privateKey, { network: ltcNetwork }); }); const getAddress = ({ network, phrase, index, }) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { if (index < 0) { throw new Error('index must be greater than zero'); } if (addrCache[phrase] && addrCache[phrase][index]) { return addrCache[phrase][index]; } const ltcNetwork = Utils.ltcNetwork(network); const ltcKeys = yield getLtcKeys({ network, phrase, index }); const { address } = Litecoin.payments.p2wpkh({ pubkey: ltcKeys.publicKey, network: ltcNetwork, }); if (!address) { throw new Error('Address not defined'); } if (!addrCache[phrase]) { addrCache[phrase] = {}; } addrCache[phrase][index] = address; return address; }); exports.getAddress = getAddress; //# sourceMappingURL=get-address.js.map