UNPKG

@atomicport/evm

Version:

Support Cross-Chain-Swap with HTLC on any blockchains

49 lines (48 loc) 1.84 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.BaseHTLCService = void 0; const web3_1 = __importDefault(require("web3")); const crypto_1 = __importDefault(require("crypto")); /** * HTLC operations on the Ethereum Test Net. * Passing a value to the constructor will overwrite the specified value. */ class BaseHTLCService { web3; contract; constructor(providerEndpoint, contractAddress, abi) { this.web3 = new web3_1.default(new web3_1.default.providers.HttpProvider(providerEndpoint)); this.contract = new this.web3.eth.Contract(abi, contractAddress); } /** * create a new hash pair * If you specify an existing secret or proof in the constructor, take over that value */ createHashPair() { const s = crypto_1.default.randomBytes(32); const p1 = crypto_1.default.createHash('sha256').update(s).digest(); const p2 = crypto_1.default.createHash('sha256').update(p1).digest(); return { proof: '0x' + s.toString('hex'), secret: '0x' + p2.toString('hex'), }; } /** * Obtain contract information for the current instance */ getContractInfo(contractId) { return this.contract.methods.getContract(contractId).call(); } /** * Called by the sender if there was no withdraw AND the time lock has * expired. This will refund the contract amount. */ refund(contractId, senderAddress, gasLimit) { const gas = gasLimit ?? 1000000; return this.contract.methods.refund(contractId).send({ from: senderAddress, gas: gas.toString() }); } } exports.BaseHTLCService = BaseHTLCService;