Home Reference Source
public class | source

Client

Constructor Summary

Public Constructor
public

constructor(provider: string | Provider, version: string)

Client

Member Summary

Public Members
public
public
public
Private Members
private

Method Summary

Public Methods
public

Add a provider

public

async checkBlockSwap(blockNumber: number, recipientAddress: string, refundAddress: string, secretHash: string, expiration: number): Promise<string, TypeError>

Check if counterparty transaction has been confirmed

public

async claimSwap(secret: string, pubKey: string, signature: string): Promise<string, TypeError>

Generate redeem swap transaction data

public

async createSwapScript(bytecode: string): Promise<string, null>

Create swap script.

public

async decodeRawTransaction(rawTransaction: string): Promise<string>

Decode Transaction from Hex

public

Generate a block

public

async generateSecret(message: string): Promise<string>

Generate a secret.

public

async getAddresses(startingIndex: number, numAddresses: number): Promise<Address, InvalidProviderResponseError>

Get addresses/accounts of the user.

public

Get the balance of an account given its addresses.

public

async getBlockByHash(blockHash: string, includeTx: boolean): Promise<ChainAbstractionLayer.schemas.Block, TypeError|InvalidProviderResponseError>

Get a block given its hash.

public

async getBlockByNumber(blockNumber: number, includeTx: boolean): Promise<ChainAbstractionLayer.schemas.Block, TypeError|InvalidProviderResponseError>

Get a block given its number.

public

Get current block height of the chain.

public

getMethod(method: string, requestor: object): function

Helper method that returns method from a provider.

public

Check the availability of a method.

public

Get a raw hexadecimal transaction given its hash.

public

async getSecret(transaction: string): Promise<string>

Get secret from claim transaction hash.

public

async getSwapConfirmTransaction(blockNumber: string, initiationTxHash: string, secretHash: string): Promise<string>

Find swap confirmation transaction from parameters

public

async getSwapTransaction(blockNumber: string, recipientAddress: string, refundAddress: string, expiration: string): Promise<string>

Find swap transaction from parameters

public

async getTransactionByHash(txHash: string): Promise<ChainAbstractionLayer.schemas.Transaction, TypeError|InvalidProviderResponseError>

Get a transaction given its hash.

public

Get unused address/account of the user.

public

async initiateSwap(value: number, recipientAddress: string, refundAddress: string, secretHash: string, expiration: number): Promise<string, TypeError>

Initiate a swap

public

async isAddressUsed(addresses: string | Address): Promise<boolean>

Check if an address has been used or not.

public

async refundSwap(pubKey: string, signature: string): Promise<string, TypeError>

Generate refund swap transaction data

public

Broadcast a signed transaction to the network.

public

async sendTransaction(to: string, value: string, data: string, from: string): Promise<string>

Create, sign & broadcast a transaction.

public

async signMessage(message: string, from: string): Promise<string>

Sign a message.

Public Constructors

public constructor(provider: string | Provider, version: string) source

Client

Params:

NameTypeAttributeDescription
provider string | Provider
  • optional

Data source/provider for the instance

version string
  • optional

Minimum blockchain node version to support

Public Members

public validateBlock: * source

public validateTransaction: * source

public version: string source

Private Members

private _providers: Array source

Public Methods

public addProvider(provider: string | Provider): Client source

Add a provider

Params:

NameTypeAttributeDescription
provider string | Provider
  • nullable: false

The provider instance or RPC connection string

Return:

Client

Returns instance of Client

Throw:

InvalidProviderError

When invalid provider is provider

DuplicateProviderError

When same provider is added again

public async checkBlockSwap(blockNumber: number, recipientAddress: string, refundAddress: string, secretHash: string, expiration: number): Promise<string, TypeError> source

Check if counterparty transaction has been confirmed

Params:

NameTypeAttributeDescription
blockNumber number
  • nullable: false

The number of the desired block.

recipientAddress string
  • nullable: false

Recepient address for the swap in hex.

refundAddress string
  • nullable: false

Refund address for the swap in hex.

secretHash string
  • nullable: false

Secret hash for the swap in hex.

expiration number
  • nullable: false

Expiration time for the swap.

Return:

Promise<string, TypeError>

Resolves with txHash of desired swap or false if not found. Rejects with InvalidProviderResponseError if provider's response is invalid.

public async claimSwap(secret: string, pubKey: string, signature: string): Promise<string, TypeError> source

Generate redeem swap transaction data

Params:

NameTypeAttributeDescription
secret string
  • nullable: false

Secret for the swap in hex.

pubKey string
  • optional

PubKey for the swap in hex.

signature string
  • optional

Signature for the swap in hex.

Return:

Promise<string, TypeError>

Resolves with redeem swap contract bytecode. Rejects with InvalidProviderResponseError if provider's response is invalid.

public async createSwapScript(bytecode: string): Promise<string, null> source

Create swap script.

Params:

NameTypeAttributeDescription
bytecode string
  • nullable: false

Bytecode to be used for swap.

Return:

Promise<string, null>

Resolves with swap bytecode.

public async decodeRawTransaction(rawTransaction: string): Promise<string> source

Decode Transaction from Hex

Params:

NameTypeAttributeDescription
rawTransaction string
  • nullable: false

A raw transaction usually in the form of a hexadecimal string that represents the serialized transaction.

Return:

Promise<string>

Resolves with an decoded transaction object. Rejects with InvalidProviderResponseError if provider's response is invalid.

public async generateBlock(numberOfBlocks: number): Promise<string[], TypeError|InvalidProviderResponseError> source

Generate a block

Params:

NameTypeAttributeDescription
numberOfBlocks number
  • nullable: false

Number of blocks to be generated

Return:

Promise<string[], TypeError|InvalidProviderResponseError>

Resolves with Block hash of the generated blocks. Rejects with TypeError if input is invalid. Rejects with InvalidProviderResponseError if provider's response is invalid.

public async generateSecret(message: string): Promise<string> source

Generate a secret.

Params:

NameTypeAttributeDescription
message string
  • nullable: false

Message to be used for generating secret.

Return:

Promise<string>

Resolves with secret

public async getAddresses(startingIndex: number, numAddresses: number): Promise<Address, InvalidProviderResponseError> source

Get addresses/accounts of the user.

Params:

NameTypeAttributeDescription
startingIndex number
  • optional
  • default: 0
numAddresses number
  • optional
  • default: 1

Return:

Promise<Address, InvalidProviderResponseError>

Resolves with a list of accounts. Rejects with InvalidProviderResponseError if provider's response is invalid.

public async getBalance(addresses: string | string[] | Address | Address[]): Promise<number, InvalidProviderResponseError> source

Get the balance of an account given its addresses.

Params:

NameTypeAttributeDescription
addresses string | string[] | Address | Address[]
  • nullable: false

An address or a list of addresses.

Return:

Promise<number, InvalidProviderResponseError>

If addresses is given, returns the cumulative balance of the given addresses. Otherwise returns the balance of the addresses that the signing provider controls. Rejects with InvalidProviderResponseError if provider's response is invalid.

public async getBlockByHash(blockHash: string, includeTx: boolean): Promise<ChainAbstractionLayer.schemas.Block, TypeError|InvalidProviderResponseError> source

Get a block given its hash.

Params:

NameTypeAttributeDescription
blockHash string
  • nullable: false

A hexadecimal string that represents the hash of the desired block.

includeTx boolean
  • optional
  • default: false

If true, fetches transaction in the block.

Return:

Promise<ChainAbstractionLayer.schemas.Block, TypeError|InvalidProviderResponseError>

Resolves with a Block with the same hash as the given input. If includeTx is true, the transaction property is an array of Transactions; otherwise, it is a list of transaction hashes. Rejects with TypeError if input is invalid. Rejects with InvalidProviderResponseError if provider's response is invalid.

public async getBlockByNumber(blockNumber: number, includeTx: boolean): Promise<ChainAbstractionLayer.schemas.Block, TypeError|InvalidProviderResponseError> source

Get a block given its number.

Params:

NameTypeAttributeDescription
blockNumber number
  • nullable: false

The number of the desired block.

includeTx boolean
  • optional
  • default: false

If true, fetches transaction in the block.

Return:

Promise<ChainAbstractionLayer.schemas.Block, TypeError|InvalidProviderResponseError>

Resolves with a Block with the same number as the given input. If includeTx is true, the transaction property is an array of Transactions; otherwise, it is a list of transaction hashes. Rejects with TypeError if input is invalid. Rejects with InvalidProviderResponseError if provider's response is invalid.

public async getBlockHeight(): Promise<number, InvalidProviderResponseError> source

Get current block height of the chain.

Return:

Promise<number, InvalidProviderResponseError>

Resolves with chain height. Rejects with InvalidProviderResponseError if provider's response is invalid.

public getMethod(method: string, requestor: object): function source

Helper method that returns method from a provider.

Params:

NameTypeAttributeDescription
method string
  • nullable: false

Name of the method to look for in the provider stack

requestor object
  • optional

If provided, it returns method from providers only above the requestor in the stack.

Return:

function

Returns method from provider instance associated with the requested method

public getProviderForMethod(method: string, requestor: boolean | object): Provider source

Check the availability of a method.

Params:

NameTypeAttributeDescription
method string
  • nullable: false

Name of the method to look for in the provider stack

requestor boolean | object
  • optional
  • default: false

If provided, it returns providers only above the requestor in the stack.

Return:

Provider

Returns a provider instance associated with the requested method

Throw:

NoProviderError

When no provider is available in the stack.

UnimplementedMethodError

When the requested method is not provided by any provider above requestor in the provider stack

UnsupportedMethodError

When requested method is not supported by version specified

public async getRawTransactionByHash(txHash: string): Promise<string, TypeError|InvalidProviderResponseError> source

Get a raw hexadecimal transaction given its hash.

Params:

NameTypeAttributeDescription
txHash string
  • nullable: false

A hexadecimal string that represents the hash of the desired transaction.

Return:

Promise<string, TypeError|InvalidProviderResponseError>

Resolves with the raw Transaction with the same hash as the given output. Rejects with TypeError if input is invalid. Rejects with InvalidProviderResponseError if provider's response is invalid.

public async getSecret(transaction: string): Promise<string> source

Get secret from claim transaction hash.

Params:

NameTypeAttributeDescription
transaction string
  • nullable: false

hash - transaction hash of claim.

Return:

Promise<string>

Resolves with secret

public async getSwapConfirmTransaction(blockNumber: string, initiationTxHash: string, secretHash: string): Promise<string> source

Find swap confirmation transaction from parameters

Params:

NameTypeAttributeDescription
blockNumber string
  • nullable: false

Block number in which transaction was mined

initiationTxHash string
  • nullable: false

Swap initiation transaction hash/identifier

secretHash string
  • nullable: false

Secret hash

Return:

Promise<string>

Resolves with a transaction identifier.

public async getSwapTransaction(blockNumber: string, recipientAddress: string, refundAddress: string, expiration: string): Promise<string> source

Find swap transaction from parameters

Params:

NameTypeAttributeDescription
blockNumber string
  • nullable: false

Block number in which transaction was mined

recipientAddress string
  • nullable: false

Recepient address

refundAddress string
  • nullable: false

Refund address

expiration string
  • nullable: false

Expiration time

Return:

Promise<string>

Resolves with a transaction identifier.

public async getTransactionByHash(txHash: string): Promise<ChainAbstractionLayer.schemas.Transaction, TypeError|InvalidProviderResponseError> source

Get a transaction given its hash.

Params:

NameTypeAttributeDescription
txHash string
  • nullable: false

A hexadecimal string that represents the hash of the desired transaction.

Return:

Promise<ChainAbstractionLayer.schemas.Transaction, TypeError|InvalidProviderResponseError>

Resolves with a Transaction with the same hash as the given input. Rejects with TypeError if input is invalid. Rejects with InvalidProviderResponseError if provider's response is invalid.

public async getUnusedAddress(from: {}): Promise<string, InvalidProviderResponseError> source

Get unused address/account of the user.

Params:

NameTypeAttributeDescription
from {}
  • optional
  • default: {}

Return:

Promise<string, InvalidProviderResponseError>

Resolves with a address object. Rejects with InvalidProviderResponseError if provider's response is invalid.

public async initiateSwap(value: number, recipientAddress: string, refundAddress: string, secretHash: string, expiration: number): Promise<string, TypeError> source

Initiate a swap

Params:

NameTypeAttributeDescription
value number
  • nullable: false

The amount of native value to lock for the swap.

recipientAddress string
  • nullable: false

Recepient address for the swap in hex.

refundAddress string
  • nullable: false

Refund address for the swap in hex.

secretHash string
  • nullable: false

Secret hash for the swap in hex.

expiration number
  • nullable: false

Expiration time for the swap.

Return:

Promise<string, TypeError>

Resolves with the transaction ID for the swap. Rejects with InvalidProviderResponseError if provider's response is invalid.

public async isAddressUsed(addresses: string | Address): Promise<boolean> source

Check if an address has been used or not.

Params:

NameTypeAttributeDescription
addresses string | Address
  • nullable: false

An address to check for.

Return:

Promise<boolean>

Resolves to true if provided address is used

public async refundSwap(pubKey: string, signature: string): Promise<string, TypeError> source

Generate refund swap transaction data

Params:

NameTypeAttributeDescription
pubKey string
  • optional

PubKey for the swap in hex.

signature string
  • optional

Signature for the swap in hex.

Return:

Promise<string, TypeError>

Resolves with refund swap contract bytecode. Rejects with InvalidProviderResponseError if provider's response is invalid.

public async sendRawTransaction(rawTransaction: string): Promise<string, InvalidProviderResponseError> source

Broadcast a signed transaction to the network.

Params:

NameTypeAttributeDescription
rawTransaction string
  • nullable: false

A raw transaction usually in the form of a hexadecimal string that represents the serialized transaction.

Return:

Promise<string, InvalidProviderResponseError>

Resolves with an identifier for the broadcasted transaction. Rejects with InvalidProviderResponseError if provider's response is invalid.

public async sendTransaction(to: string, value: string, data: string, from: string): Promise<string> source

Create, sign & broadcast a transaction.

Params:

NameTypeAttributeDescription
to string
  • nullable: false

Recepient address.

value string
  • nullable: false

Value of transaction.

data string
  • nullable: false

Data to be passed to the transaction.

from string
  • nullable: false

The address from which the message is signed.

Return:

Promise<string>

Resolves with a signed transaction.

public async signMessage(message: string, from: string): Promise<string> source

Sign a message.

Params:

NameTypeAttributeDescription
message string
  • nullable: false

Message to be signed.

from string
  • nullable: false

The address from which the message is signed.

Return:

Promise<string>

Resolves with a signed message.