UNPKG

@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.

118 lines (117 loc) 5.18 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 () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports._prepareAndSignInitialState = _prepareAndSignInitialState; exports._prepareAndSignChallengeState = _prepareAndSignChallengeState; exports._prepareAndSignResizeState = _prepareAndSignResizeState; exports._prepareAndSignFinalState = _prepareAndSignFinalState; const Errors = __importStar(require("../errors")); const utils_1 = require("../utils"); const types_1 = require("./types"); async function _prepareAndSignInitialState(deps, params) { const { channel, unsignedInitialState, serverSignature } = params; if (!unsignedInitialState) { throw new Errors.MissingParameterError('Initial state is required for creating the channel'); } if (!unsignedInitialState.data) { throw new Errors.MissingParameterError('State data is required for creating the channel'); } if (!unsignedInitialState.allocations || unsignedInitialState.allocations.length !== 2) { throw new Errors.InvalidParameterError('Initial allocation amounts must be provided for both participants.'); } if (!channel) { throw new Errors.MissingParameterError("Channel's fixed part is required for creating the channel"); } if (!channel?.adjudicator) { throw new Errors.MissingParameterError('Adjudicator address is required for creating the channel'); } if (!channel.participants || channel.participants.length !== 2) { throw new Errors.InvalidParameterError('Channel must have exactly two participants.'); } const channelId = (0, utils_1.getChannelId)(channel, deps.chainId); const accountSignature = await deps.stateSigner.signState(channelId, unsignedInitialState); const signedInitialState = { ...unsignedInitialState, sigs: [accountSignature, serverSignature], }; return { initialState: signedInitialState, channelId }; } async function _prepareAndSignChallengeState(deps, params) { const { channelId, candidateState, proofStates = [] } = params; const challengeMsg = (0, utils_1.getPackedChallengeState)(channelId, candidateState); const challengerSig = await deps.stateSigner.signRawMessage(challengeMsg); return { channelId, candidateState, proofs: proofStates, challengerSig }; } async function _prepareAndSignResizeState(deps, params) { const { resizeState, proofStates } = params; if (!resizeState.data) { throw new Errors.MissingParameterError('State data is required for closing the channel.'); } const channelId = resizeState.channelId; const stateToSign = { data: resizeState.data, intent: resizeState.intent, allocations: resizeState.allocations, version: resizeState.version, sigs: [], }; const accountSignature = await deps.stateSigner.signState(channelId, stateToSign); const resizeStateWithSigs = { ...stateToSign, sigs: [accountSignature, resizeState.serverSignature], }; let proofs = [...proofStates]; return { resizeStateWithSigs, proofs, channelId }; } async function _prepareAndSignFinalState(deps, params) { const { stateData, finalState } = params; if (!stateData) { throw new Errors.MissingParameterError('State data is required for closing the channel.'); } const channelId = finalState.channelId; const stateToSign = { data: stateData, intent: types_1.StateIntent.FINALIZE, allocations: finalState.allocations, version: finalState.version, sigs: [], }; const accountSignature = await deps.stateSigner.signState(channelId, stateToSign); const finalStateWithSigs = { ...stateToSign, sigs: [accountSignature, finalState.serverSignature], }; return { finalStateWithSigs, channelId }; }