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.

32 lines (31 loc) 1.43 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getChannelId = getChannelId; exports.generateChannelNonce = generateChannelNonce; 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; }