@erc7824/nitrolite
Version:
The Nitrolite SDK empowers developers to build high-performance, scalable web3 applications using state channels. It's designed to provide near-instant transactions and significantly improved user experiences by minimizing direct blockchain interactions.
128 lines (127 loc) • 4.5 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.channelParamsParsers = void 0;
const zod_1 = require("zod");
const types_1 = require("../types");
const common_1 = require("./common");
const RPCAllocationSchema = zod_1.z.object({
destination: common_1.addressSchema,
token: common_1.addressSchema,
amount: zod_1.z.string().transform((a) => BigInt(a)),
});
const ServerSignatureSchema = zod_1.z.object({
v: zod_1.z.union([zod_1.z.string(), zod_1.z.number()]).transform((a) => Number(a)),
r: common_1.hexSchema,
s: common_1.hexSchema,
});
const ResizeChannelParamsSchema = zod_1.z
.array(zod_1.z
.object({
channel_id: common_1.hexSchema,
state_data: common_1.hexSchema,
intent: zod_1.z.number(),
version: zod_1.z.number(),
allocations: zod_1.z.array(RPCAllocationSchema),
state_hash: common_1.hexSchema,
server_signature: ServerSignatureSchema,
})
.transform((raw) => ({
channelId: raw.channel_id,
stateData: raw.state_data,
intent: raw.intent,
version: raw.version,
allocations: raw.allocations.map((a) => ({
destination: a.destination,
token: a.token,
amount: a.amount,
})),
stateHash: raw.state_hash,
serverSignature: {
v: +raw.server_signature.v,
r: raw.server_signature.r,
s: raw.server_signature.s,
},
})))
.refine((arr) => arr.length === 1)
.transform((arr) => arr[0]);
const CloseChannelParamsSchema = zod_1.z
.array(zod_1.z
.object({
channel_id: common_1.hexSchema,
state_data: common_1.hexSchema,
intent: zod_1.z.number(),
version: zod_1.z.number(),
allocations: zod_1.z.array(RPCAllocationSchema),
state_hash: common_1.hexSchema,
server_signature: ServerSignatureSchema,
})
.transform((raw) => ({
channelId: raw.channel_id,
stateData: raw.state_data,
intent: raw.intent,
version: raw.version,
allocations: raw.allocations.map((a) => ({
destination: a.destination,
token: a.token,
amount: a.amount,
})),
stateHash: raw.state_hash,
serverSignature: {
v: +raw.server_signature.v,
r: raw.server_signature.r,
s: raw.server_signature.s,
},
})))
.refine((arr) => arr.length === 1)
.transform((arr) => arr[0]);
const ChannelUpdateObjectSchema = zod_1.z
.object({
channel_id: common_1.hexSchema,
participant: common_1.addressSchema,
status: common_1.statusEnum,
token: common_1.addressSchema,
wallet: zod_1.z.union([common_1.addressSchema, zod_1.z.literal('')]),
amount: zod_1.z.union([zod_1.z.string(), zod_1.z.number()]).transform((a) => BigInt(a)),
chain_id: zod_1.z.number(),
adjudicator: common_1.addressSchema,
challenge: zod_1.z.number(),
nonce: zod_1.z.union([zod_1.z.string(), zod_1.z.number()]).transform((n) => BigInt(n)),
version: zod_1.z.number(),
created_at: zod_1.z.union([zod_1.z.string(), zod_1.z.date()]).transform((v) => new Date(v)),
updated_at: zod_1.z.union([zod_1.z.string(), zod_1.z.date()]).transform((v) => new Date(v)),
})
.transform((c) => ({
channelId: c.channel_id,
participant: c.participant,
status: c.status,
token: c.token,
wallet: c.wallet,
amount: c.amount,
chainId: c.chain_id,
adjudicator: c.adjudicator,
challenge: c.challenge,
nonce: c.nonce,
version: c.version,
createdAt: c.created_at,
updatedAt: c.updated_at,
}));
const GetChannelsParamsSchema = zod_1.z
.array(zod_1.z.array(ChannelUpdateObjectSchema))
.refine((arr) => arr.length === 1)
.transform((arr) => arr[0])
.transform((arr) => arr);
const ChannelUpdateParamsSchema = zod_1.z
.array(ChannelUpdateObjectSchema)
.refine((arr) => arr.length === 1)
.transform((arr) => arr[0]);
const ChannelsUpdateParamsSchema = zod_1.z
.array(zod_1.z.array(ChannelUpdateObjectSchema))
.refine((arr) => arr.length === 1)
.transform((arr) => arr[0]);
exports.channelParamsParsers = {
[types_1.RPCMethod.ResizeChannel]: (params) => ResizeChannelParamsSchema.parse(params),
[types_1.RPCMethod.CloseChannel]: (params) => CloseChannelParamsSchema.parse(params),
[types_1.RPCMethod.GetChannels]: (params) => GetChannelsParamsSchema.parse(params),
[types_1.RPCMethod.ChannelUpdate]: (params) => ChannelUpdateParamsSchema.parse(params),
[types_1.RPCMethod.ChannelsUpdate]: (params) => ChannelsUpdateParamsSchema.parse(params),
};