raiden-ts
Version:
Raiden Light Client Typescript/Javascript SDK
128 lines (127 loc) • 5.26 kB
TypeScript
import type { Signer } from '@ethersproject/abstract-signer';
import type * as t from 'io-ts';
import logging from 'loglevel';
import type { BalanceProof } from '../channels/types';
import type { matrixPresence } from '../transport/actions';
import type { Caps } from '../transport/types';
import type { RaidenEpicDeps } from '../types';
import type { Address, Hash, HexString } from '../utils/types';
import { Signature, Signed } from '../utils/types';
import { messageReceived } from './actions';
import type { EnvelopeMessage } from './types';
import { Message } from './types';
export declare enum MessageTypeId {
BALANCE_PROOF = 1,
BALANCE_PROOF_UPDATE = 2,
WITHDRAW = 3,
COOP_SETTLE = 4,
IOU = 5,
MS_REWARD = 6
}
/**
* Returns a balance_hash from transferred&locked amounts & locksroot
*
* @param bp - BalanceProof-like object
* @param bp.transferredAmount - balanceProof's transferredAmount
* @param bp.lockedAmount - balanceProof's lockedAmount
* @param bp.locksroot - balanceProof's locksroot
* @returns Hash of the balance
*/
export declare function createBalanceHash({ transferredAmount, lockedAmount, locksroot, }: Pick<BalanceProof, 'transferredAmount' | 'lockedAmount' | 'locksroot'>): Hash;
/**
* Create the messageHash/additionalHash for a given EnvelopeMessage
*
* @param message - EnvelopeMessage to pack
* @returns Hash of the message pack
*/
export declare function createMessageHash(message: EnvelopeMessage): Hash;
/**
* Pack a message in a hex-string format, **without** signature
* This packed hex-byte-array can then be used for signing.
* On Raiden python client, this is the output of `_data_to_sign` method of the messages, as the
* actual packed encoding was once used for binary transport protocols, but nowadays is used only
* for generating data to be signed, which is the purpose of our implementation.
*
* @param message - Message to be packed
* @returns HexBytes hex-encoded string data representing message in binary format
*/
export declare function packMessage(message: Message): HexString<number>;
/**
* Typeguard to check if a message contains a valid signature
*
* @param message - May or may not be a signed message
* @returns Boolean if message is signed
*/
export declare function isSigned<M extends Message & {
signature?: Signature;
}>(message: M): message is Signed<M>;
/**
* Requires a signed message and returns its signer address
*
* @param message - Signed message to retrieve signer address
* @param caps - Sender's capabilities which may change how signature is verified
* @returns Address which signed message
*/
export declare function getMessageSigner(message: Signed<Message>, caps?: Caps): Address;
/**
* Get the signed BalanceProof associated with an EnvelopeMessage
*
* @param message - Signed EnvelopeMessage
* @returns Signed BalanceProof object for message
*/
export declare function getBalanceProofFromEnvelopeMessage(message: Signed<EnvelopeMessage>): Signed<BalanceProof>;
/**
* Encode a Message as a JSON string
* Uses io-ts codec to encode BigNumbers as JSON 'string' type, as Raiden
*
* @param message - Message object to be serialized
* @returns JSON string
*/
export declare function encodeJsonMessage(message: Message | Signed<Message>): string;
/**
* Try to decode text as a Message, using io-ts codec to decode BigNumbers
* Throws if can't decode, or message is invalid regarding any of the encoded constraints
*
* @param text - JSON string to try to decode
* @returns Message object
*/
export declare function decodeJsonMessage(text: string): Message | Signed<Message>;
/**
* Pack message and request signer to sign it, and returns signed message
*
* @param signer - Signer instance
* @param message - Unsigned message to pack and sign
* @param opts - Options
* @param opts.log - Logger instance
* @returns Promise to signed message
*/
export declare function signMessage<M extends Message>(signer: Signer, message: M, { log }?: {
log: logging.Logger;
}): Promise<Signed<M>>;
/**
* Type of a specific messageReceived action which validates & narrows payload.message type
*/
export declare type messageReceivedTyped<M extends Message> = messageReceived & {
payload: {
message: M;
};
};
/**
* Typeguard to ensure an action is a messageReceived of any of a set of Message types
*
* @param messageCodecs - Message codec to test action.payload.message against
* @returns Typeguard intersecting messageReceived action and payload.message schemas
*/
export declare function isMessageReceivedOfType<C extends t.Mixed>(messageCodecs: C | C[]): (action: unknown) => action is messageReceivedTyped<t.TypeOf<C>>;
/**
* Parse a received message into either a Message or Signed<Message>
* If Signed, the signer must match the sender's address.
* Errors are logged and undefined returned
*
* @param line - String to be parsed as a single message
* @param sender - Sender's presence
* @param deps - Dependencies
* @param deps.log - Logger instance
* @returns Validated Signed or unsigned Message, or undefined
*/
export declare function parseMessage(line: unknown, sender: matrixPresence.success, { log }: Pick<RaidenEpicDeps, 'log'>): Message | Signed<Message> | undefined;