raiden-ts
Version:
Raiden Light Client Typescript/Javascript SDK
231 lines • 7.97 kB
JavaScript
/**
* These io-ts codecs validate and decode JSON Raiden messages
* They include BigNumber strings validation, enum validation (if needed), Address checksum
* validation, etc, and converting everything to its respective object, where needed.
*/
import * as t from 'io-ts';
import { Lock } from '../channels/types';
import { Fee, Path, RoutesExtra } from '../services/types';
import { Address, Hash, Secret, Signature, UInt } from '../utils/types';
// types
export var MessageType;
(function (MessageType) {
MessageType["DELIVERED"] = "Delivered";
MessageType["PROCESSED"] = "Processed";
MessageType["SECRET_REQUEST"] = "SecretRequest";
MessageType["SECRET_REVEAL"] = "RevealSecret";
MessageType["LOCKED_TRANSFER"] = "LockedTransfer";
MessageType["UNLOCK"] = "Unlock";
MessageType["LOCK_EXPIRED"] = "LockExpired";
MessageType["WITHDRAW_REQUEST"] = "WithdrawRequest";
MessageType["WITHDRAW_CONFIRMATION"] = "WithdrawConfirmation";
MessageType["WITHDRAW_EXPIRED"] = "WithdrawExpired";
MessageType["PFS_CAPACITY_UPDATE"] = "PFSCapacityUpdate";
MessageType["PFS_FEE_UPDATE"] = "PFSFeeUpdate";
MessageType["MONITOR_REQUEST"] = "RequestMonitoring";
})(MessageType || (MessageType = {}));
// Mixin of a message that contains an identifier and should be ack'ed with a respective Delivered
const RetrieableMessage = t.readonly(t.type({ message_identifier: UInt(8) }));
// Acknowledges to the sender that a RetrieableMessage was received
const _Delivered = t.readonly(t.type({
type: t.literal(MessageType.DELIVERED),
delivered_message_identifier: UInt(8),
}));
export const Delivered = _Delivered;
// Confirms some message that required state validation was successfuly processed
const _Processed = t.readonly(t.intersection([
t.type({
type: t.literal(MessageType.PROCESSED),
}),
RetrieableMessage,
]));
export const Processed = _Processed;
// Requests the initiator to reveal the secret for a LockedTransfer targeted to us
const _SecretRequest = t.readonly(t.intersection([
t.type({
type: t.literal(MessageType.SECRET_REQUEST),
payment_identifier: UInt(8),
secrethash: Hash,
amount: UInt(32),
expiration: UInt(32),
}),
RetrieableMessage,
]));
export const SecretRequest = _SecretRequest;
// Reveal to the target or the previous hop a secret we just learned off-chain
const _SecretReveal = t.readonly(t.intersection([
t.type({
type: t.literal(MessageType.SECRET_REVEAL),
secret: Secret,
}),
RetrieableMessage,
]));
export const SecretReveal = _SecretReveal;
// Mixin for messages containing a balance proof
const EnvelopeMessage = t.readonly(t.intersection([
t.type({
chain_id: UInt(32),
token_network_address: Address,
channel_identifier: UInt(32),
nonce: UInt(8),
transferred_amount: UInt(32),
locked_amount: UInt(32),
locksroot: Hash,
}),
RetrieableMessage,
]));
const _RouteMetadata = t.readonly(t.intersection([t.type({ route: Path }), RoutesExtra]));
export const RouteMetadata = _RouteMetadata;
const _Metadata = t.readonly(t.type({
routes: t.readonlyArray(RouteMetadata),
}));
export const Metadata = _Metadata;
// base for locked transfers, they differentiate only on the type tag
const LockedTransferBase = t.readonly(t.intersection([
t.type({
payment_identifier: UInt(8),
token: Address,
recipient: Address,
lock: Lock,
target: Address,
initiator: Address,
// unknown metadata ensures decoder may not change it, and just passthrough; this allows us
// to properly verify its signature; therefore, anything is accepted, and you must decode
// wherever you need to access its properties (e.g. to inspect routes)
metadata: t.unknown,
}),
EnvelopeMessage,
]));
// a mediated transfer containing a locked amount
const _LockedTransfer = t.readonly(t.intersection([
t.type({
type: t.literal(MessageType.LOCKED_TRANSFER),
}),
LockedTransferBase,
]));
export const LockedTransfer = _LockedTransfer;
// when the secret is revealed, unlock sends a new balance proof without the lock and increasing
// the total transfered to finish the offchain transfer
const _Unlock = t.readonly(t.intersection([
t.type({
type: t.literal(MessageType.UNLOCK),
payment_identifier: UInt(8),
secret: Secret,
}),
EnvelopeMessage,
]));
export const Unlock = _Unlock;
// after mediated transfer fails and the lock expire, clean it from the locks tree
const _LockExpired = t.readonly(t.intersection([
t.type({
type: t.literal(MessageType.LOCK_EXPIRED),
recipient: Address,
secrethash: Hash,
}),
EnvelopeMessage,
]));
export const LockExpired = _LockExpired;
const WithdrawBase = t.readonly(t.type({
chain_id: UInt(32),
token_network_address: Address,
channel_identifier: UInt(32),
participant: Address,
total_withdraw: UInt(32),
nonce: UInt(8),
expiration: UInt(32),
}));
const _WithdrawRequest = t.readonly(t.intersection([
t.type({ type: t.literal(MessageType.WITHDRAW_REQUEST) }),
t.partial({ coop_settle: t.boolean }),
WithdrawBase,
RetrieableMessage,
]));
export const WithdrawRequest = _WithdrawRequest;
const _WithdrawConfirmation = t.readonly(t.intersection([
t.type({
type: t.literal(MessageType.WITHDRAW_CONFIRMATION),
}),
WithdrawBase,
RetrieableMessage,
]));
export const WithdrawConfirmation = _WithdrawConfirmation;
const _WithdrawExpired = t.readonly(t.intersection([
t.type({
type: t.literal(MessageType.WITHDRAW_EXPIRED),
}),
WithdrawBase,
RetrieableMessage,
]));
export const WithdrawExpired = _WithdrawExpired;
const _PFSCapacityUpdate = t.readonly(t.type({
type: t.literal(MessageType.PFS_CAPACITY_UPDATE),
canonical_identifier: t.readonly(t.type({
chain_identifier: UInt(32),
token_network_address: Address,
channel_identifier: UInt(32),
})),
updating_participant: Address,
other_participant: Address,
updating_nonce: UInt(8),
other_nonce: UInt(8),
updating_capacity: UInt(32),
other_capacity: UInt(32),
reveal_timeout: UInt(32),
}));
export const PFSCapacityUpdate = _PFSCapacityUpdate;
const _PFSFeeUpdate = t.readonly(t.type({
type: t.literal(MessageType.PFS_FEE_UPDATE),
canonical_identifier: t.readonly(t.type({
chain_identifier: UInt(32),
token_network_address: Address,
channel_identifier: UInt(32),
})),
updating_participant: Address,
timestamp: t.string,
fee_schedule: t.type({
cap_fees: t.boolean,
// if not null, it should be an array of [tokenAmount, fee] tuples
imbalance_penalty: t.union([
t.null,
t.readonlyArray(t.readonly(t.tuple([UInt(32), UInt(32)]))),
]),
proportional: Fee,
flat: Fee,
}),
}));
export const PFSFeeUpdate = _PFSFeeUpdate;
const _MonitorRequest = t.readonly(t.type({
type: t.literal(MessageType.MONITOR_REQUEST),
balance_proof: t.type({
chain_id: UInt(32),
token_network_address: Address,
channel_identifier: UInt(32),
nonce: UInt(8),
balance_hash: Hash,
additional_hash: Hash,
signature: Signature,
}),
monitoring_service_contract_address: Address,
non_closing_participant: Address,
non_closing_signature: Signature,
reward_amount: UInt(32),
}));
export const MonitorRequest = _MonitorRequest;
const _messages = [
Delivered,
Processed,
SecretRequest,
SecretReveal,
LockedTransfer,
Unlock,
LockExpired,
WithdrawRequest,
WithdrawConfirmation,
WithdrawExpired,
PFSCapacityUpdate,
PFSFeeUpdate,
MonitorRequest,
];
const messages = _messages;
export const Message = t.union(messages);
//# sourceMappingURL=types.js.map