@bcpros/crypto-wallet-core
Version:
A multi-currency support library for address derivation, private key creation, and transaction creation
37 lines • 1.79 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.BtcDeriver = exports.AbstractBitcoreLibDeriver = void 0;
const BitcoreLib = require('@bcpros/bitcore-lib');
class AbstractBitcoreLibDeriver {
deriveAddress(network, pubKey, addressIndex, isChange, addressType) {
const changeNum = isChange ? 1 : 0;
const path = `m/${changeNum}/${addressIndex}`;
return this.deriveAddressWithPath(network, pubKey, path, addressType);
}
derivePrivateKey(network, xPriv, addressIndex, isChange, addressType) {
const changeNum = isChange ? 1 : 0;
const path = `m/${changeNum}/${addressIndex}`;
return this.derivePrivateKeyWithPath(network, xPriv, path, addressType);
}
deriveAddressWithPath(network, xpubKey, path, addressType) {
const xpub = new this.bitcoreLib.HDPublicKey(xpubKey, network);
return this.getAddress(network, xpub.derive(path).publicKey, addressType);
}
derivePrivateKeyWithPath(network, xprivKey, path, addressType) {
const xpriv = new this.bitcoreLib.HDPrivateKey(xprivKey, network);
const privKey = xpriv.deriveChild(path).privateKey;
const pubKey = privKey.publicKey;
const address = this.getAddress(network, pubKey, addressType);
return { address, privKey: privKey.toString(), pubKey: pubKey.toString(), path };
}
getAddress(network, pubKey, addressType) {
pubKey = new this.bitcoreLib.PublicKey(pubKey);
return new this.bitcoreLib.Address(pubKey, network, addressType).toString();
}
}
exports.AbstractBitcoreLibDeriver = AbstractBitcoreLibDeriver;
class BtcDeriver extends AbstractBitcoreLibDeriver {
bitcoreLib = BitcoreLib;
}
exports.BtcDeriver = BtcDeriver;
//# sourceMappingURL=index.js.map