UNPKG

@atomicport/evm

Version:

Support Cross-Chain-Swap with HTLC on any blockchains

42 lines (41 loc) 1.46 kB
import Web3 from 'web3'; import crypto from 'crypto'; /** * HTLC operations on the Ethereum Test Net. * Passing a value to the constructor will overwrite the specified value. */ export class BaseHTLCService { web3; contract; constructor(providerEndpoint, contractAddress, abi) { this.web3 = new Web3(new Web3.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.randomBytes(32); const p1 = crypto.createHash('sha256').update(s).digest(); const p2 = crypto.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() }); } }