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.

55 lines (54 loc) 2.1 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getChannelId = getChannelId; exports.generateChannelNonce = generateChannelNonce; exports.convertRPCToClientChannel = convertRPCToClientChannel; exports.convertRPCToClientState = convertRPCToClientState; const viem_1 = require("viem"); function getChannelId(channel, chainId) { const encoded = (0, viem_1.encodeAbiParameters)([ { name: 'participants', type: 'address[]' }, { name: 'adjudicator', type: 'address' }, { name: 'challenge', type: 'uint64' }, { name: 'nonce', type: 'uint64' }, { name: 'chainId', type: 'uint256' }, ], [channel.participants, channel.adjudicator, channel.challenge, channel.nonce, chainId]); return (0, viem_1.keccak256)(encoded); } function generateChannelNonce(address) { const timestamp = BigInt(Math.floor(Date.now() / 1000)); const randomComponent = BigInt(Math.floor(Math.random() * 0xffffffff)); let combinedNonce = (timestamp << 32n) | randomComponent; if (address) { const cleanAddress = address.startsWith('0x') ? address.slice(2) : address; if (!/^[0-9a-fA-F]+$/.test(cleanAddress)) { throw new Error(`Invalid address format: ${address}. Address must be a valid hex string.`); } const addressComponent = BigInt(`0x${cleanAddress.slice(-16)}`); combinedNonce = combinedNonce ^ addressComponent; } const maxInt64 = 0x7fffffffffffffffn; const nonce = combinedNonce & maxInt64; return nonce; } function convertRPCToClientChannel(ch) { return { participants: ch.participants, adjudicator: ch.adjudicator, challenge: BigInt(ch.challenge), nonce: BigInt(ch.nonce), }; } function convertRPCToClientState(s, sig) { return { intent: s.intent, version: BigInt(s.version), data: s.stateData, allocations: s.allocations.map((a) => ({ token: a.token, destination: a.destination, amount: a.amount, })), sigs: [sig], }; }