|
| 1 | +import { Chain, chains, chainToPlatform, contracts, rpc } from '@wormhole-foundation/sdk-base'; |
| 2 | +import { |
| 3 | + callContractMethod, |
| 4 | + getMethodId, |
| 5 | + GuardianSetInfo, |
| 6 | + GuardianSetInfoByChain, |
| 7 | + makeRpcCall, |
| 8 | + queryContractSmart, |
| 9 | +} from '@wormhole-foundation/wormhole-monitor-common'; |
| 10 | +import { Firestore } from 'firebase-admin/firestore'; |
| 11 | +import { assertEnvironmentVariable } from './utils'; |
| 12 | +import { utils } from '@wormhole-foundation/sdk-solana-core'; |
| 13 | +import axios from 'axios'; |
| 14 | + |
| 15 | +export async function computeGuardianSetInfo(req: any, res: any) { |
| 16 | + res.set('Access-Control-Allow-Origin', '*'); |
| 17 | + if (req.method === 'OPTIONS') { |
| 18 | + // Send response to OPTIONS requests |
| 19 | + res.set('Access-Control-Allow-Methods', 'GET'); |
| 20 | + res.set('Access-Control-Allow-Headers', 'Content-Type'); |
| 21 | + res.set('Access-Control-Max-Age', '3600'); |
| 22 | + res.status(204).send(''); |
| 23 | + return; |
| 24 | + } |
| 25 | + const infosByChain: GuardianSetInfoByChain = await getGuardianSetInfoByChain(); |
| 26 | + |
| 27 | + await updateFirestore(infosByChain); |
| 28 | + res.status(200).send('successfully stored guardian set info'); |
| 29 | + return; |
| 30 | +} |
| 31 | + |
| 32 | +async function getGuardianSetInfoByChain(): Promise<GuardianSetInfoByChain> { |
| 33 | + let infosByChain: GuardianSetInfoByChain = {}; |
| 34 | + for (const chain of chains) { |
| 35 | + const contract = contracts.coreBridge.get('Mainnet', chain); // Only support Mainnet for now |
| 36 | + if (!contract) { |
| 37 | + console.log(`No contract found for ${chain}`); |
| 38 | + continue; |
| 39 | + } |
| 40 | + const info: GuardianSetInfo = await fetchGuardianSetInfo(chain, contract); |
| 41 | + infosByChain[chain] = info; |
| 42 | + } |
| 43 | + console.log('Guardian set info:', infosByChain); |
| 44 | + return infosByChain; |
| 45 | +} |
| 46 | + |
| 47 | +async function fetchGuardianSetInfo(chain: Chain, address: string): Promise<GuardianSetInfo> { |
| 48 | + const timestamp: string = new Date().toISOString(); |
| 49 | + const mt: GuardianSetInfo = { |
| 50 | + timestamp, |
| 51 | + contract: '', |
| 52 | + guardianSetIndex: '0', |
| 53 | + guardianSet: '', |
| 54 | + }; |
| 55 | + if (!address) throw new Error('Address not found'); |
| 56 | + const rpcUrl = |
| 57 | + chain === 'Klaytn' |
| 58 | + ? 'https://rpc.ankr.com/klaytn' |
| 59 | + : chain === 'Near' |
| 60 | + ? 'https://rpc.mainnet.near.org' |
| 61 | + : chain === 'Pythnet' |
| 62 | + ? 'http://pythnet.rpcpool.com' |
| 63 | + : rpc.rpcAddress('Mainnet', chain); |
| 64 | + if (!rpcUrl) { |
| 65 | + console.error(`Mainnet ${chain} rpc url not found`); |
| 66 | + return mt; |
| 67 | + } |
| 68 | + const platform = chainToPlatform(chain); |
| 69 | + try { |
| 70 | + if (platform === 'Evm') { |
| 71 | + const gsi = await callContractMethod( |
| 72 | + rpcUrl, |
| 73 | + address, |
| 74 | + getMethodId('getCurrentGuardianSetIndex()') |
| 75 | + ); |
| 76 | + const gs = await callContractMethod( |
| 77 | + rpcUrl, |
| 78 | + address, |
| 79 | + getMethodId('getGuardianSet(uint32)'), |
| 80 | + gsi.substring(2) // strip 0x |
| 81 | + ); |
| 82 | + return { timestamp, contract: address, guardianSetIndex: gsi, guardianSet: gs }; |
| 83 | + } else if (platform === 'Cosmwasm') { |
| 84 | + const guardianSet = await queryContractSmart(rpcUrl, address, { |
| 85 | + guardian_set_info: {}, |
| 86 | + }); |
| 87 | + return { |
| 88 | + timestamp, |
| 89 | + contract: address, |
| 90 | + guardianSetIndex: guardianSet.guardian_set_index.toString(), |
| 91 | + guardianSet: guardianSet.addresses |
| 92 | + .map( |
| 93 | + (address: { bytes: string }) => |
| 94 | + `0x${Buffer.from(address.bytes, 'base64').toString('hex')}` |
| 95 | + ) |
| 96 | + .join(','), |
| 97 | + }; |
| 98 | + } else if (platform === 'Solana') { |
| 99 | + let gsIdx = 0; |
| 100 | + let gsAddress = utils.deriveGuardianSetKey(address, gsIdx); |
| 101 | + // console.log(chain, gsIdx, gsAddress); |
| 102 | + let gsAccountInfo = await makeRpcCall(rpcUrl, 'getAccountInfo', [gsAddress], 'jsonParsed'); |
| 103 | + let ret: GuardianSetInfo = { |
| 104 | + timestamp, |
| 105 | + contract: '', |
| 106 | + guardianSetIndex: '0', |
| 107 | + guardianSet: '', |
| 108 | + }; |
| 109 | + while ( |
| 110 | + gsAccountInfo && |
| 111 | + gsAccountInfo.value && |
| 112 | + gsAccountInfo.value.data && |
| 113 | + gsAccountInfo.value.data[0] !== null |
| 114 | + ) { |
| 115 | + const gs = utils.GuardianSetData.deserialize( |
| 116 | + Buffer.from(gsAccountInfo.value.data[0], 'base64') |
| 117 | + ); |
| 118 | + ret = { |
| 119 | + timestamp, |
| 120 | + contract: address, |
| 121 | + guardianSetIndex: gsIdx.toString(), |
| 122 | + guardianSet: gs.keys.map((k) => `0x${k.toString('hex')}`).join(','), |
| 123 | + }; |
| 124 | + gsIdx++; |
| 125 | + gsAddress = utils.deriveGuardianSetKey(address, gsIdx); |
| 126 | + // console.log(chain, gsIdx, gsAddress); |
| 127 | + gsAccountInfo = await makeRpcCall(rpcUrl, 'getAccountInfo', [gsAddress], 'jsonParsed'); |
| 128 | + } |
| 129 | + return ret; |
| 130 | + } else if (platform === 'Algorand') { |
| 131 | + const response = await axios.get(`${rpcUrl}/v2/applications/${address}`); |
| 132 | + const currentGuardianSetIndexState = response.data.params['global-state'].find( |
| 133 | + (s: any) => Buffer.from(s.key, 'base64').toString('ascii') === 'currentGuardianSetIndex' |
| 134 | + ); |
| 135 | + return { |
| 136 | + timestamp, |
| 137 | + contract: address, |
| 138 | + guardianSetIndex: currentGuardianSetIndexState.value.uint.toString(), |
| 139 | + guardianSet: '', |
| 140 | + }; |
| 141 | + } else if (platform === 'Near') { |
| 142 | + const response = await axios.post(rpcUrl, { |
| 143 | + jsonrpc: '2.0', |
| 144 | + id: 'dontcare', |
| 145 | + method: 'query', |
| 146 | + params: { |
| 147 | + request_type: 'view_state', |
| 148 | + finality: 'final', |
| 149 | + account_id: address, |
| 150 | + prefix_base64: 'U1RBVEU=', // STATE |
| 151 | + }, |
| 152 | + }); |
| 153 | + const state = Buffer.from( |
| 154 | + response.data.result.values.find( |
| 155 | + (s: any) => Buffer.from(s.key, 'base64').toString('ascii') === 'STATE' |
| 156 | + ).value, |
| 157 | + 'base64' |
| 158 | + ).toString('hex'); |
| 159 | + // a tiny hack - instead of parsing the whole state, just find the expiry, which comes before the guardian set index |
| 160 | + // https://github.com/wormhole-foundation/wormhole/blob/main/near/contracts/wormhole/src/lib.rs#L109 |
| 161 | + const expiry = `00004f91944e0000`; // = 24 * 60 * 60 * 1_000_000_000, // 24 hours in nanoseconds |
| 162 | + const expiryIndex = state.indexOf(expiry); |
| 163 | + const gsiIndex = expiryIndex + 16; // u64 len in hex |
| 164 | + const gsi = `0x${state |
| 165 | + .substring(gsiIndex, gsiIndex + 8) |
| 166 | + .match(/../g) |
| 167 | + ?.reverse() |
| 168 | + .join('')}`; |
| 169 | + return { timestamp, contract: address, guardianSetIndex: gsi, guardianSet: '' }; |
| 170 | + } else if (platform === 'Aptos') { |
| 171 | + const response = await axios.get( |
| 172 | + `${rpcUrl}/accounts/${address}/resource/${address}::state::WormholeState` |
| 173 | + ); |
| 174 | + const gsi = response.data.data.guardian_set_index.number.toString(); |
| 175 | + // const gsHandle = response.data.data.guardian_sets |
| 176 | + return { timestamp, contract: address, guardianSetIndex: gsi, guardianSet: '' }; |
| 177 | + } else if (platform === 'Sui') { |
| 178 | + const response = await axios.post(rpcUrl, { |
| 179 | + jsonrpc: '2.0', |
| 180 | + id: 1, |
| 181 | + method: 'sui_getObject', |
| 182 | + params: [ |
| 183 | + address, |
| 184 | + { |
| 185 | + showType: false, |
| 186 | + showOwner: false, |
| 187 | + showPreviousTransaction: false, |
| 188 | + showDisplay: false, |
| 189 | + showContent: true, |
| 190 | + showBcs: false, |
| 191 | + showStorageRebate: false, |
| 192 | + }, |
| 193 | + ], |
| 194 | + }); |
| 195 | + const gsi = response.data.result.data.content.fields.guardian_set_index.toString(); |
| 196 | + // const gsTable = response.data.result.data.content.fields.guardian_sets); |
| 197 | + return { timestamp, contract: address, guardianSetIndex: gsi, guardianSet: '' }; |
| 198 | + } |
| 199 | + } catch (e) { |
| 200 | + console.error(`Failed to get guardian set for ${chain}:`, e); |
| 201 | + } |
| 202 | + return mt; |
| 203 | +} |
| 204 | + |
| 205 | +async function updateFirestore(data: GuardianSetInfoByChain): Promise<void> { |
| 206 | + const firestore = new Firestore(); |
| 207 | + const collection = firestore.collection( |
| 208 | + assertEnvironmentVariable('FIRESTORE_GUARDIAN_SET_INFO_COLLECTION') |
| 209 | + ); |
| 210 | + try { |
| 211 | + for (const chain in data) { |
| 212 | + if (data.hasOwnProperty(chain)) { |
| 213 | + const chainData: GuardianSetInfo | undefined = data[chain as Chain]; |
| 214 | + if (chainData) { |
| 215 | + const docRef = collection.doc(chain); |
| 216 | + await docRef.set(chainData); |
| 217 | + } |
| 218 | + } |
| 219 | + } |
| 220 | + } catch (e) { |
| 221 | + console.error('Error adding document: ', e); |
| 222 | + } |
| 223 | +} |
0 commit comments