ptokens-deposit-address
Version:
repo holding the code related to deposit addresses
236 lines (230 loc) • 10.2 kB
JavaScript
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
var _asyncToGenerator = _interopDefault(require('@babel/runtime/helpers/asyncToGenerator'));
var _classCallCheck = _interopDefault(require('@babel/runtime/helpers/classCallCheck'));
var _createClass = _interopDefault(require('@babel/runtime/helpers/createClass'));
var _defineProperty = _interopDefault(require('@babel/runtime/helpers/defineProperty'));
var _regeneratorRuntime = _interopDefault(require('@babel/runtime/regenerator'));
var Web3PromiEvent = _interopDefault(require('web3-core-promievent'));
var BigNumber = _interopDefault(require('bignumber.js'));
var bitcoin = require('bitcoinjs-lib');
var utils = require('ptokens-utils');
var _Bitcoin, _Litecoin, _NETWORKS;
var HOST_NODE_POLLING_TIME_INTERVAL = 3000;
var POLLING_TIME = 3000;
var confirmations = {
btc: 1,
ltc: 4,
doge: 1,
rvn: 25
};
var hostBlockchainEvents = {
ethereum: 'onEthTxConfirmed',
eosio: 'onEosTxConfirmed'
};
bitcoin.networks.litecoin = {
messagePrefix: '\x19Litecoin Signed Message:\n',
bech32: 'ltc',
bip32: {
public: 0x019da462,
private: 0x019d9cfe
},
pubKeyHash: 0x30,
scriptHash: 0x32,
wif: 0xb0
};
bitcoin.networks.litecoinTestnet = {
messagePrefix: '\x19Litecoin Signed Message:\n',
bech32: 'ltc',
bip32: {
public: 0x019da462,
private: 0x019d9cfe
},
pubKeyHash: 0x6f,
scriptHash: 0x3a,
wif: 0xb0
};
bitcoin.networks.dogecoin = {
messagePrefix: '\x1aRavencoin Signed Message:\n',
bip32: {
public: 0x02facafd,
private: 0x02fac398
},
pubKeyHash: 0x1e,
scriptHash: 0x16,
wif: 0x9e
};
bitcoin.networks.ravencoin = {
messagePrefix: '\x1aRavencoin Signed Message:\n',
bip32: {
public: 0x0488b21e,
private: 0x0488ade4
},
pubKeyHash: 0x3c,
scriptHash: 0x7a,
wif: 0x80
};
bitcoin.networks.lbc = {
pubKeyHash: 0x55,
scriptHash: 0x7a
};
var _utils$constants = utils.constants,
_utils$constants$netw = _utils$constants.networks,
BitcoinMainnet = _utils$constants$netw.BitcoinMainnet,
BitcoinTestnet = _utils$constants$netw.BitcoinTestnet,
LitecoinMainnet = _utils$constants$netw.LitecoinMainnet,
LitecoinTestnet = _utils$constants$netw.LitecoinTestnet,
DogecoinMainnet = _utils$constants$netw.DogecoinMainnet,
RavencoinMainnet = _utils$constants$netw.RavencoinMainnet,
LbryMainnet = _utils$constants$netw.LbryMainnet,
_utils$constants$bloc = _utils$constants.blockchains,
Bitcoin = _utils$constants$bloc.Bitcoin,
Litecoin = _utils$constants$bloc.Litecoin,
Dogecoin = _utils$constants$bloc.Dogecoin,
Ravencoin = _utils$constants$bloc.Ravencoin,
Lbry = _utils$constants$bloc.Lbry;
var NETWORKS = (_NETWORKS = {}, _defineProperty(_NETWORKS, Bitcoin, (_Bitcoin = {}, _defineProperty(_Bitcoin, BitcoinMainnet, bitcoin.networks.bitcoin), _defineProperty(_Bitcoin, BitcoinTestnet, bitcoin.networks.testnet), _Bitcoin)), _defineProperty(_NETWORKS, Litecoin, (_Litecoin = {}, _defineProperty(_Litecoin, LitecoinMainnet, bitcoin.networks.litecoin), _defineProperty(_Litecoin, LitecoinTestnet, bitcoin.networks.litecoinTestnet), _Litecoin)), _defineProperty(_NETWORKS, Dogecoin, _defineProperty({}, DogecoinMainnet, bitcoin.networks.dogecoin)), _defineProperty(_NETWORKS, Ravencoin, _defineProperty({}, RavencoinMainnet, bitcoin.networks.ravencoin)), _defineProperty(_NETWORKS, Lbry, _defineProperty({}, LbryMainnet, bitcoin.networks.lbc)), _NETWORKS);
var DepositAddress = function () {
function DepositAddress(_configs) {
_classCallCheck(this, DepositAddress);
var nativeBlockchain = _configs.nativeBlockchain,
nativeNetwork = _configs.nativeNetwork,
hostBlockchain = _configs.hostBlockchain,
hostNetwork = _configs.hostNetwork,
hostApi = _configs.hostApi,
node = _configs.node;
this.hostBlockchain = hostBlockchain;
this.hostNetwork = hostNetwork;
this.nativeBlockchain = nativeBlockchain;
this.nativeNetwork = nativeNetwork;
this.node = node;
this.hostApi = hostApi;
}
_createClass(DepositAddress, [{
key: "generate",
value: function () {
var _generate = _asyncToGenerator( _regeneratorRuntime.mark(function _callee(_hostAddress) {
var _yield$this$node$getN, nonce, enclavePublicKey, nativeDepositAddress;
return _regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
_context.prev = 0;
_context.next = 3;
return this.node.getNativeDepositAddress(_hostAddress);
case 3:
_yield$this$node$getN = _context.sent;
nonce = _yield$this$node$getN.nonce;
enclavePublicKey = _yield$this$node$getN.enclavePublicKey;
nativeDepositAddress = _yield$this$node$getN.nativeDepositAddress;
this.nonce = nonce;
this.enclavePublicKey = enclavePublicKey;
this.value = nativeDepositAddress;
this.hostAddress = _hostAddress;
return _context.abrupt("return", this.value);
case 14:
_context.prev = 14;
_context.t0 = _context["catch"](0);
throw new Error('Error during deposit address generation');
case 17:
case "end":
return _context.stop();
}
}
}, _callee, this, [[0, 14]]);
}));
function generate(_x) {
return _generate.apply(this, arguments);
}
return generate;
}()
}, {
key: "toString",
value: function toString() {
return this.value;
}
}, {
key: "verify",
value: function verify() {
var _utils$constants$bloc2 = utils.constants.blockchains,
Eosio = _utils$constants$bloc2.Eosio,
Telos = _utils$constants$bloc2.Telos;
var network = NETWORKS[this.nativeBlockchain][this.nativeNetwork];
if (!network) throw new Error('Please use a valid combination of nativeNetwork and nativeBlockchain');
var hostAddressBuf = this.hostBlockchain === Eosio || this.hostBlockchain === Telos ? Buffer.from(this.hostAddress, 'utf-8') : Buffer.from(utils.eth.removeHexPrefix(this.hostAddress), 'hex');
var nonceBuf = utils.converters.encodeUint64le(this.nonce);
var enclavePublicKeyBuf = Buffer.from(utils.eth.removeHexPrefix(this.enclavePublicKey), 'hex');
var hostAddressAndNonceHashBuf = bitcoin.crypto.hash256(Buffer.concat([hostAddressBuf, nonceBuf]));
var output = bitcoin.script.compile([].concat(hostAddressAndNonceHashBuf, bitcoin.opcodes.OP_DROP, enclavePublicKeyBuf, bitcoin.opcodes.OP_CHECKSIG));
var p2sh = bitcoin.payments.p2sh({
redeem: {
output: output,
network: network
},
network: network
});
return p2sh.address === this.value;
}
}, {
key: "waitForDeposit",
value: function waitForDeposit() {
var _this = this;
var promiEvent = Web3PromiEvent();
var start = function () {
var _ref = _asyncToGenerator( _regeneratorRuntime.mark(function _callee2() {
var shortNativeBlockchain, shortHostBlockchain, nativeTxId, broadcastedHostTxReport, hostTxReceipt;
return _regeneratorRuntime.wrap(function _callee2$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
if (_this.hostApi) {
_context2.next = 3;
break;
}
promiEvent.reject('Provider not specified. Impossible to monitor the tx');
return _context2.abrupt("return");
case 3:
if (!_this.value) promiEvent.reject('Please generate a deposit address');
shortNativeBlockchain = utils.helpers.getBlockchainShortType(_this.nativeBlockchain);
shortHostBlockchain = utils.helpers.getBlockchainShortType(_this.hostBlockchain);
_context2.next = 8;
return utils[shortNativeBlockchain].monitorUtxoByAddress(_this.nativeNetwork, _this.value, promiEvent.eventEmitter, POLLING_TIME, 'nativeTxBroadcasted', 'nativeTxConfirmed', confirmations[shortNativeBlockchain]);
case 8:
nativeTxId = _context2.sent;
_context2.next = 11;
return _this.node.monitorIncomingTransaction(nativeTxId, promiEvent.eventEmitter);
case 11:
broadcastedHostTxReport = _context2.sent;
_context2.next = 14;
return utils[shortHostBlockchain === 'bsc' || shortHostBlockchain === 'polygon' || shortHostBlockchain === 'arbitrum' ? 'eth' : shortHostBlockchain].waitForTransactionConfirmation(_this.hostApi, broadcastedHostTxReport.broadcast_tx_hash, HOST_NODE_POLLING_TIME_INTERVAL);
case 14:
hostTxReceipt = _context2.sent;
if (_this.hostBlockchain !== utils.constants.blockchains.Telos) {
promiEvent.eventEmitter.emit(hostBlockchainEvents[_this.hostBlockchain], hostTxReceipt);
}
promiEvent.eventEmitter.emit('hostTxConfirmed', hostTxReceipt);
promiEvent.resolve({
amount: utils.eth.offChainFormat(new BigNumber(broadcastedHostTxReport.host_tx_amount), 8).toFixed(),
nativeTx: nativeTxId,
hostTx: broadcastedHostTxReport.broadcast_tx_hash,
to: _this.hostAddress
});
case 18:
case "end":
return _context2.stop();
}
}
}, _callee2);
}));
return function start() {
return _ref.apply(this, arguments);
};
}();
start();
return promiEvent.eventEmitter;
}
}]);
return DepositAddress;
}();
exports.DepositAddress = DepositAddress;