import { sha256 } from '@liquality/crypto' ; import { UnimplementedMethodError } from '@liquality/errors' ; import { SwapParams , SwapProvider , Transaction , EIP1559Fee } from '@liquality/types' ; export default class Swap implements SwapProvider { client : any ; constructor ( client ) { this . client = client ; } inheritdoc async findInitiateSwapTransaction ( swapParams , blockNumber ? ) { return this . client . getMethod ( 'findInitiateSwapTransaction' ) ( swapParams , blockNumber ) ; } inheritdoc async findClaimSwapTransaction ( swapParams , initiationTxHash , blockNumber ? ) { return this . client . getMethod ( 'findClaimSwapTransaction' ) ( swapParams , initiationTxHash , blockNumber ) ; } inheritdoc async findRefundSwapTransaction ( swapParams , initiationTxHash , blockNumber ? ) { return this . client . getMethod ( 'findRefundSwapTransaction' ) ( swapParams , initiationTxHash , blockNumber ) ; } inheritdoc async findFundSwapTransaction ( swapParams , initiationTxHash , blockNumber ? ) { return this . client . getMethod ( 'findFundSwapTransaction' ) ( swapParams , initiationTxHash , blockNumber ) ; } inheritdoc async generateSecret ( message ) { try { return this . client . getMethod ( 'generateSecret' ) ( message ) ; } catch ( e ) { if ( ! ( e instanceof UnimplementedMethodError ) ) throw e ; } const address = ( await this . client . getMethod ( 'getAddresses' ) ( ) ) [ 0 ] ; const signedMessage = await this . client . getMethod ( 'signMessage' ) ( message , address . address ) ; const secret = sha256 ( signedMessage ) ; return secret ; } inheritdoc async getSwapSecret ( claimTxHash ) { return this . client . getMethod ( 'getSwapSecret' ) ( claimTxHash ) ; } inheritdoc async initiateSwap ( swapParams , fee ) { const transaction = await this . client . getMethod ( 'initiateSwap' ) ( swapParams , fee ) ; this . client . assertValidTransaction ( transaction ) ; return transaction ; } inheritdoc async fundSwap ( swapParams , initiationTxHash , fee ) { return this . client . getMethod ( 'fundSwap' ) ( swapParams , initiationTxHash , fee ) ; } inheritdoc verifyInitiateSwapTransaction ( swapParams , initiationTxHash ) { return this . client . getMethod ( 'verifyInitiateSwapTransaction' ) ( swapParams , initiationTxHash ) ; } inheritdoc async claimSwap ( swapParams , initiationTxHash , secret , fee ) { if ( ! /[A-Fa-f0-9]{64}/ . test ( secret ) ) { throw new TypeError ( 'Secret should be a 32 byte hex string' ) ; } const transaction = await this . client . getMethod ( 'claimSwap' ) ( swapParams , initiationTxHash , secret , fee ) ; this . client . assertValidTransaction ( transaction ) ; return transaction ; } inheritdoc async refundSwap ( swapParams , initiationTxHash , fee ) { const transaction = await this . client . getMethod ( 'refundSwap' ) ( swapParams , initiationTxHash , fee ) ; this . client . assertValidTransaction ( transaction ) ; return transaction ; } inheritdoc get doesBlockScan ( ) : boolean { try { return this . client . getMethod ( 'doesBlockScan' ) ( ) ; } catch ( e ) { if ( ! ( e instanceof UnimplementedMethodError ) ) throw e ; } return true ; } }