UNPKG

raiden-ts

Version:

Raiden Light Client Typescript/Javascript SDK

257 lines 10.2 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Message = exports.MonitorRequest = exports.PFSFeeUpdate = exports.PFSCapacityUpdate = exports.WithdrawExpired = exports.WithdrawConfirmation = exports.WithdrawRequest = exports.LockExpired = exports.Unlock = exports.LockedTransfer = exports.Metadata = exports.RouteMetadata = exports.SecretReveal = exports.SecretRequest = exports.Processed = exports.Delivered = exports.MessageType = void 0; /** * 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. */ const t = __importStar(require("io-ts")); const types_1 = require("../channels/types"); const types_2 = require("../services/types"); const types_3 = require("../utils/types"); // types 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 = exports.MessageType || (exports.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: (0, types_3.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: (0, types_3.UInt)(8), })); exports.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, ])); exports.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: (0, types_3.UInt)(8), secrethash: types_3.Hash, amount: (0, types_3.UInt)(32), expiration: (0, types_3.UInt)(32), }), RetrieableMessage, ])); exports.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: types_3.Secret, }), RetrieableMessage, ])); exports.SecretReveal = _SecretReveal; // Mixin for messages containing a balance proof const EnvelopeMessage = t.readonly(t.intersection([ t.type({ chain_id: (0, types_3.UInt)(32), token_network_address: types_3.Address, channel_identifier: (0, types_3.UInt)(32), nonce: (0, types_3.UInt)(8), transferred_amount: (0, types_3.UInt)(32), locked_amount: (0, types_3.UInt)(32), locksroot: types_3.Hash, }), RetrieableMessage, ])); const _RouteMetadata = t.readonly(t.intersection([t.type({ route: types_2.Path }), types_2.RoutesExtra])); exports.RouteMetadata = _RouteMetadata; const _Metadata = t.readonly(t.type({ routes: t.readonlyArray(exports.RouteMetadata), })); exports.Metadata = _Metadata; // base for locked transfers, they differentiate only on the type tag const LockedTransferBase = t.readonly(t.intersection([ t.type({ payment_identifier: (0, types_3.UInt)(8), token: types_3.Address, recipient: types_3.Address, lock: types_1.Lock, target: types_3.Address, initiator: types_3.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, ])); exports.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: (0, types_3.UInt)(8), secret: types_3.Secret, }), EnvelopeMessage, ])); exports.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: types_3.Address, secrethash: types_3.Hash, }), EnvelopeMessage, ])); exports.LockExpired = _LockExpired; const WithdrawBase = t.readonly(t.type({ chain_id: (0, types_3.UInt)(32), token_network_address: types_3.Address, channel_identifier: (0, types_3.UInt)(32), participant: types_3.Address, total_withdraw: (0, types_3.UInt)(32), nonce: (0, types_3.UInt)(8), expiration: (0, types_3.UInt)(32), })); const _WithdrawRequest = t.readonly(t.intersection([ t.type({ type: t.literal(MessageType.WITHDRAW_REQUEST) }), t.partial({ coop_settle: t.boolean }), WithdrawBase, RetrieableMessage, ])); exports.WithdrawRequest = _WithdrawRequest; const _WithdrawConfirmation = t.readonly(t.intersection([ t.type({ type: t.literal(MessageType.WITHDRAW_CONFIRMATION), }), WithdrawBase, RetrieableMessage, ])); exports.WithdrawConfirmation = _WithdrawConfirmation; const _WithdrawExpired = t.readonly(t.intersection([ t.type({ type: t.literal(MessageType.WITHDRAW_EXPIRED), }), WithdrawBase, RetrieableMessage, ])); exports.WithdrawExpired = _WithdrawExpired; const _PFSCapacityUpdate = t.readonly(t.type({ type: t.literal(MessageType.PFS_CAPACITY_UPDATE), canonical_identifier: t.readonly(t.type({ chain_identifier: (0, types_3.UInt)(32), token_network_address: types_3.Address, channel_identifier: (0, types_3.UInt)(32), })), updating_participant: types_3.Address, other_participant: types_3.Address, updating_nonce: (0, types_3.UInt)(8), other_nonce: (0, types_3.UInt)(8), updating_capacity: (0, types_3.UInt)(32), other_capacity: (0, types_3.UInt)(32), reveal_timeout: (0, types_3.UInt)(32), })); exports.PFSCapacityUpdate = _PFSCapacityUpdate; const _PFSFeeUpdate = t.readonly(t.type({ type: t.literal(MessageType.PFS_FEE_UPDATE), canonical_identifier: t.readonly(t.type({ chain_identifier: (0, types_3.UInt)(32), token_network_address: types_3.Address, channel_identifier: (0, types_3.UInt)(32), })), updating_participant: types_3.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([(0, types_3.UInt)(32), (0, types_3.UInt)(32)]))), ]), proportional: types_2.Fee, flat: types_2.Fee, }), })); exports.PFSFeeUpdate = _PFSFeeUpdate; const _MonitorRequest = t.readonly(t.type({ type: t.literal(MessageType.MONITOR_REQUEST), balance_proof: t.type({ chain_id: (0, types_3.UInt)(32), token_network_address: types_3.Address, channel_identifier: (0, types_3.UInt)(32), nonce: (0, types_3.UInt)(8), balance_hash: types_3.Hash, additional_hash: types_3.Hash, signature: types_3.Signature, }), monitoring_service_contract_address: types_3.Address, non_closing_participant: types_3.Address, non_closing_signature: types_3.Signature, reward_amount: (0, types_3.UInt)(32), })); exports.MonitorRequest = _MonitorRequest; const _messages = [ exports.Delivered, exports.Processed, exports.SecretRequest, exports.SecretReveal, exports.LockedTransfer, exports.Unlock, exports.LockExpired, exports.WithdrawRequest, exports.WithdrawConfirmation, exports.WithdrawExpired, exports.PFSCapacityUpdate, exports.PFSFeeUpdate, exports.MonitorRequest, ]; const messages = _messages; exports.Message = t.union(messages); //# sourceMappingURL=types.js.map