|
| 1 | +import { Wallet } from '@ethersproject/wallet'; |
| 2 | +import { keccak_256 } from '@noble/hashes/sha3'; |
| 3 | +import { encode } from '@msgpack/msgpack'; |
| 4 | +import { arrayify } from '@ethersproject/bytes'; |
| 5 | +import { hexToBytes, bytesToHex, concatBytes } from './secp256k1Wrapper'; |
| 6 | +import { getBigBlocksConfig } from '../config/bigBlocksConfig'; |
| 7 | + |
| 8 | +interface Action { |
| 9 | + type: 'evmUserModify'; |
| 10 | + usingBigBlocks: boolean; |
| 11 | +} |
| 12 | + |
| 13 | +interface SignatureResult { |
| 14 | + r: string; |
| 15 | + s: string; |
| 16 | + v: number; |
| 17 | +} |
| 18 | + |
| 19 | +interface ApiResponse { |
| 20 | + status: 'ok' | 'err'; |
| 21 | + response?: string; |
| 22 | +} |
| 23 | + |
| 24 | +interface L1ActionHashParams { |
| 25 | + action: Action; |
| 26 | + nonce: number; |
| 27 | + vaultAddress?: string; |
| 28 | + expiresAfter?: number; |
| 29 | +} |
| 30 | + |
| 31 | +interface SignL1ActionParams { |
| 32 | + wallet: Wallet; |
| 33 | + action: Action; |
| 34 | + nonce: number; |
| 35 | + chainId: number; |
| 36 | +} |
| 37 | + |
| 38 | +function getBigBlocksUrl(chainId: number): string { |
| 39 | + const config = getBigBlocksConfig(chainId); |
| 40 | + if (!config) throw new Error(`Chain ${chainId} does not support BigBlocks`); |
| 41 | + return config.apiUrl; |
| 42 | +} |
| 43 | + |
| 44 | +/** |
| 45 | + * Helper: encode nonce to 8 bytes |
| 46 | + */ |
| 47 | +function toUint64Bytes(n: number): Uint8Array { |
| 48 | + const bytes = new Uint8Array(8); |
| 49 | + new DataView(bytes.buffer).setBigUint64(0, BigInt(n)); |
| 50 | + return bytes; |
| 51 | +} |
| 52 | + |
| 53 | +/** |
| 54 | + * Sort action keys to match SDK format |
| 55 | + */ |
| 56 | +function sortAction(action: Action): Action { |
| 57 | + return { |
| 58 | + type: action.type, |
| 59 | + usingBigBlocks: action.usingBigBlocks |
| 60 | + }; |
| 61 | +} |
| 62 | + |
| 63 | +/** |
| 64 | + * Create the connectionId hash from action, nonce, etc. |
| 65 | + */ |
| 66 | +async function createL1ActionHash({ |
| 67 | + action, |
| 68 | + nonce, |
| 69 | + vaultAddress, |
| 70 | + expiresAfter |
| 71 | +}: L1ActionHashParams): Promise<string> { |
| 72 | + const actionBytes = encode(action); |
| 73 | + const nonceBytes = toUint64Bytes(nonce); |
| 74 | + const vaultMarker = vaultAddress ? new Uint8Array([1]) : new Uint8Array([0]); |
| 75 | + const vaultBytes = vaultAddress |
| 76 | + ? await hexToBytes(vaultAddress.slice(2)) |
| 77 | + : new Uint8Array(); |
| 78 | + const expiresMarker = |
| 79 | + expiresAfter !== undefined ? new Uint8Array([0]) : new Uint8Array(); |
| 80 | + const expiresBytes = |
| 81 | + expiresAfter !== undefined ? toUint64Bytes(expiresAfter) : new Uint8Array(); |
| 82 | + |
| 83 | + const bytes = await concatBytes( |
| 84 | + actionBytes, |
| 85 | + nonceBytes, |
| 86 | + vaultMarker, |
| 87 | + vaultBytes, |
| 88 | + expiresMarker, |
| 89 | + expiresBytes |
| 90 | + ); |
| 91 | + |
| 92 | + const hash = keccak_256(bytes); |
| 93 | + return `0x${await bytesToHex(hash)}`; |
| 94 | +} |
| 95 | + |
| 96 | +/** |
| 97 | + * EIP-712 signer for the L1 action |
| 98 | + */ |
| 99 | +async function signL1Action({ |
| 100 | + wallet, |
| 101 | + action, |
| 102 | + nonce, |
| 103 | + chainId |
| 104 | +}: SignL1ActionParams): Promise<SignatureResult> { |
| 105 | + const connectionId = await createL1ActionHash({ action, nonce }); |
| 106 | + const config = getBigBlocksConfig(chainId); |
| 107 | + if (!config) throw new Error(`Chain ${chainId} does not support BigBlocks`); |
| 108 | + |
| 109 | + const domain = { |
| 110 | + name: 'Exchange', |
| 111 | + version: '1', |
| 112 | + chainId: config.bigBlocksChainId, |
| 113 | + verifyingContract: '0x0000000000000000000000000000000000000000' |
| 114 | + }; |
| 115 | + |
| 116 | + const types = { |
| 117 | + Agent: [ |
| 118 | + { name: 'source', type: 'string' }, |
| 119 | + { name: 'connectionId', type: 'bytes32' } |
| 120 | + ] |
| 121 | + }; |
| 122 | + |
| 123 | + const message = { |
| 124 | + source: config.isTestnet ? 'b' : 'a', |
| 125 | + connectionId: arrayify(connectionId) |
| 126 | + }; |
| 127 | + |
| 128 | + const signature = await wallet._signTypedData(domain, types, message); |
| 129 | + |
| 130 | + const r = '0x' + signature.slice(2, 66); |
| 131 | + const s = '0x' + signature.slice(66, 130); |
| 132 | + const v = parseInt(signature.slice(130, 132), 16); |
| 133 | + |
| 134 | + return { r, s, v }; |
| 135 | +} |
| 136 | + |
| 137 | +/** |
| 138 | + * Main function to enable/disable BigBlocks |
| 139 | + */ |
| 140 | +export async function enableBigBlocks( |
| 141 | + privateKey: string, |
| 142 | + enable: boolean = true, |
| 143 | + chainId: number |
| 144 | +): Promise<ApiResponse> { |
| 145 | + const config = getBigBlocksConfig(chainId); |
| 146 | + if (!config) throw new Error(`Chain ${chainId} does not support BigBlocks`); |
| 147 | + try { |
| 148 | + const wallet = new Wallet(privateKey); |
| 149 | + const nonce = Date.now(); |
| 150 | + |
| 151 | + const action: Action = { |
| 152 | + type: 'evmUserModify', |
| 153 | + usingBigBlocks: enable |
| 154 | + }; |
| 155 | + |
| 156 | + const signature = await signL1Action({ |
| 157 | + wallet, |
| 158 | + action, |
| 159 | + nonce, |
| 160 | + chainId |
| 161 | + }); |
| 162 | + |
| 163 | + const apiUrl = getBigBlocksUrl(chainId); |
| 164 | + const res = await fetch(apiUrl, { |
| 165 | + method: 'POST', |
| 166 | + headers: { |
| 167 | + 'Content-Type': 'application/json', |
| 168 | + 'Accept-Encoding': 'gzip, deflate, br, zstd' |
| 169 | + }, |
| 170 | + body: JSON.stringify({ |
| 171 | + action: sortAction(action), |
| 172 | + nonce, |
| 173 | + signature |
| 174 | + }) |
| 175 | + }); |
| 176 | + |
| 177 | + if (!res.ok) { |
| 178 | + const text = await res.text(); |
| 179 | + throw new Error(`HTTP ${res.status}: ${text}`); |
| 180 | + } |
| 181 | + |
| 182 | + const result = (await res.json()) as ApiResponse; |
| 183 | + console.log(`Response from BigBlocks API: ${JSON.stringify(result)}`); |
| 184 | + if (result.status === 'err') { |
| 185 | + throw new Error(`API error: ${result.response}`); |
| 186 | + } |
| 187 | + |
| 188 | + console.log( |
| 189 | + `✅ BigBlocks ${ |
| 190 | + enable ? 'enabled' : 'disabled' |
| 191 | + } successfully for wallet ${wallet.address} on ${config.name}` |
| 192 | + ); |
| 193 | + return result; |
| 194 | + } catch (err) { |
| 195 | + console.error('❌ Error:', (err as Error).message); |
| 196 | + throw err; |
| 197 | + } |
| 198 | +} |
0 commit comments