-
Notifications
You must be signed in to change notification settings - Fork 243
Feat/helios light client verification #792
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
pheobeayo
wants to merge
5
commits into
enkryptcom:main
Choose a base branch
from
pheobeayo:feat/helios-light-client-verification
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
307880b
Merge pull request #794 from enkryptcom/develop
kvhnuke 248714a
update
pheobeayo faf4bcd
fix suggested changes
pheobeayo abdc7dc
still fixing suggestions
pheobeayo eccf4aa
Merge branch 'main' into feat/helios-light-client-verification
pheobeayo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
139 changes: 139 additions & 0 deletions
139
packages/extension/src/providers/ethereum/libs/helios-verifier.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,139 @@ | ||
|
|
||
| export interface VerificationResult { | ||
| verified: boolean; | ||
| tampered: boolean; | ||
| message: string; | ||
| provenBalance?: string; | ||
| } | ||
|
|
||
| const SKIP: VerificationResult = { verified: false, tampered: false, message: '' }; | ||
| const MAINNET_CHAIN_ID = '0x1'; | ||
| const DEFAULT_CONSENSUS_RPC = 'https://ethereum.operationsolarstorm.org'; | ||
| const CHECKPOINT_FETCH_TIMEOUT_MS = 5_000; | ||
|
|
||
| let heliosProvider: any = null; | ||
| let initPromise: Promise<void> | null = null; | ||
| let syncPromise: Promise<void> | null = null; | ||
| let isSynced = false; | ||
| let currentExecutionRpc = ''; | ||
| let sessionId = 0; | ||
|
|
||
| async function fetchFreshCheckpoint(consensusRpc: string): Promise<string | undefined> { | ||
| const url = `${consensusRpc.replace(/\/$/, '')}/eth/v1/beacon/headers/finalized`; | ||
| const controller = new AbortController(); | ||
| const timer = setTimeout(() => controller.abort(), CHECKPOINT_FETCH_TIMEOUT_MS); | ||
| try { | ||
| const res = await fetch(url, { signal: controller.signal }); | ||
| if (!res.ok) return undefined; | ||
| const json = (await res.json()) as { data?: { root?: string } }; | ||
| const root = json?.data?.root; | ||
| if (typeof root === 'string' && root.startsWith('0x')) return root; | ||
| return undefined; | ||
| } catch { | ||
| return undefined; | ||
| } finally { | ||
| clearTimeout(timer); | ||
| } | ||
| } | ||
|
|
||
| export async function initHelios( | ||
| executionRpc: string, | ||
| consensusRpc: string = DEFAULT_CONSENSUS_RPC, | ||
| ): Promise<void> { | ||
| if (initPromise) return initPromise; | ||
| if (heliosProvider && currentExecutionRpc === executionRpc) return; | ||
| const session = ++sessionId; | ||
| heliosProvider = null; | ||
| isSynced = false; | ||
| currentExecutionRpc = executionRpc; | ||
| initPromise = (async () => { | ||
| try { | ||
| const checkpoint = await fetchFreshCheckpoint(consensusRpc); | ||
| const { createHeliosProvider } = await import('@a16z/helios'); | ||
| const config: Record<string, string> = { | ||
| executionRpc, | ||
| consensusRpc, | ||
| network: 'mainnet', | ||
| }; | ||
| if (checkpoint) config['checkpoint'] = checkpoint; | ||
| const provider = await createHeliosProvider(config, 'ethereum'); | ||
| if (session !== sessionId) return; | ||
| heliosProvider = provider; | ||
| syncPromise = provider.waitSynced().then(() => { | ||
| if (session === sessionId && heliosProvider === provider) { | ||
| isSynced = true; | ||
| console.log('[helios-verifier] synced and ready'); | ||
| } | ||
| }); | ||
| await syncPromise; | ||
| } catch (err) { | ||
| if (session === sessionId) { | ||
| console.warn('[helios-verifier] failed to initialise:', err); | ||
| resetHelios(); | ||
| } | ||
| } finally { | ||
| if (session === sessionId) initPromise = null; | ||
| } | ||
| })(); | ||
| return initPromise; | ||
| } | ||
|
|
||
| export function resetHelios(): void { | ||
| sessionId++; | ||
| heliosProvider = null; | ||
| isSynced = false; | ||
| initPromise = null; | ||
| syncPromise = null; | ||
| currentExecutionRpc = ''; | ||
| } | ||
|
|
||
| function encodeBalanceOf(address: string): string { | ||
| const addr = address.toLowerCase().replace(/^0x/, '').padStart(64, '0'); | ||
| return `0x70a08231${addr}`; | ||
| } | ||
|
|
||
| function decodeUint256(hex: string): bigint { | ||
| const clean = hex.startsWith('0x') ? hex.slice(2) : hex; | ||
| if (!clean || /^0+$/.test(clean)) return 0n; | ||
| return BigInt(`0x${clean}`); | ||
| } | ||
|
|
||
| export async function verifyErc20Balance( | ||
| contractAddress: string, | ||
| walletAddress: string, | ||
| rpcBalance: string, | ||
| blockTag: string, | ||
| chainId: string, | ||
| executionRpc: string, | ||
| ): Promise<VerificationResult> { | ||
| if (chainId.toLowerCase() !== MAINNET_CHAIN_ID) return SKIP; | ||
| await initHelios(executionRpc); | ||
| if (!isSynced || !heliosProvider) return SKIP; | ||
| let heliosBalanceHex: string; | ||
| try { | ||
| heliosBalanceHex = (await heliosProvider.request({ | ||
| method: 'eth_call', | ||
| params: [{ to: contractAddress, data: encodeBalanceOf(walletAddress) }, blockTag], | ||
| })) as string; | ||
| } catch (err) { | ||
| console.warn('[helios-verifier] eth_call failed:', err); | ||
| return SKIP; | ||
| } | ||
| const rpcValue = decodeUint256(rpcBalance); | ||
| const heliosValue = decodeUint256(heliosBalanceHex); | ||
| if (rpcValue === heliosValue) { | ||
| return { | ||
| verified: true, | ||
| tampered: false, | ||
| message: 'Balance verified by Helios light client.', | ||
| provenBalance: heliosBalanceHex, | ||
| }; | ||
| } | ||
| console.error(`[helios-verifier] MISMATCH: RPC=${rpcValue} Helios=${heliosValue}`); | ||
| return { | ||
| verified: false, | ||
| tampered: true, | ||
| message: `Your RPC provider returned ${rpcValue.toString()} but Helios proved the real balance is ${heliosValue.toString()}. Your RPC provider may be lying.`, | ||
| provenBalance: heliosBalanceHex, | ||
| }; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
packages/extension/src/ui/action/icons/common/shield-alert-icon.vue
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| <template> | ||
| <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"> | ||
| <path d="M12 22s8-4 8-10V5l-8-3-8 3v7c0 6 8 10 8 10z" /> | ||
| <line x1="12" y1="8" x2="12" y2="12" /> | ||
| <line x1="12" y1="16" x2="12.01" y2="16" /> | ||
| </svg> | ||
| </template> |
75 changes: 75 additions & 0 deletions
75
packages/extension/src/ui/action/views/network-assets/components/helios-warning-banner.vue
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| <template> | ||
| <transition name="helios-banner-slide"> | ||
| <div v-if="show" class="helios-warning-banner" role="alert"> | ||
| <div class="helios-warning-banner__icon"> | ||
| <shield-alert-icon /> | ||
| </div> | ||
| <div class="helios-warning-banner__body"> | ||
| <p class="helios-warning-banner__title">RPC provider may be lying</p> | ||
| <p class="helios-warning-banner__detail"> | ||
| The Helios light client cryptographically verified that the balance | ||
| shown for <strong>{{ tokenSymbol }}</strong> differs from what your RPC provider reported. | ||
| </p> | ||
| <p class="helios-warning-banner__values"> | ||
| RPC reported: <code>{{ rpcBalanceFormatted }}</code> · Proven on-chain: <code>{{ provenBalanceFormatted }}</code> | ||
| </p> | ||
| <a class="helios-warning-banner__learn-more" href="https://walletbeat.eth.limo/docs/chain-verification" target="_blank" rel="noopener noreferrer"> | ||
| Learn more about chain verification → | ||
| </a> | ||
| </div> | ||
| <button class="helios-warning-banner__dismiss" aria-label="Dismiss warning" @click="$emit('dismiss')">✕</button> | ||
| </div> | ||
| </transition> | ||
| </template> | ||
|
|
||
| <script setup lang="ts"> | ||
| import { computed } from 'vue'; | ||
| import ShieldAlertIcon from '@action/icons/common/shield-alert-icon.vue'; | ||
| import { fromBase } from '@enkryptcom/utils'; | ||
| import { formatFloatingPointValue } from '@/libs/utils/number-formatter'; | ||
|
|
||
| const props = defineProps<{ | ||
| show: boolean; | ||
| tokenSymbol: string; | ||
| tokenDecimals: number; | ||
| rpcBalance: string; | ||
| provenBalance: string; | ||
| }>(); | ||
|
|
||
| defineEmits<{ (e: 'dismiss'): void }>(); | ||
|
|
||
| function hexToDecimalDisplay(hex: string, decimals: number): string { | ||
| try { | ||
| const raw = BigInt(hex).toString(); | ||
| return formatFloatingPointValue(fromBase(raw, decimals)).value; | ||
| } catch { | ||
| return hex; | ||
| } | ||
| } | ||
|
|
||
| const rpcBalanceFormatted = computed(() => hexToDecimalDisplay(props.rpcBalance, props.tokenDecimals)); | ||
| const provenBalanceFormatted = computed(() => hexToDecimalDisplay(props.provenBalance, props.tokenDecimals)); | ||
| </script> | ||
|
|
||
| <style lang="less" scoped> | ||
| @import '@action/styles/theme.less'; | ||
| .helios-warning-banner { | ||
| display: flex; | ||
| align-items: flex-start; | ||
| gap: 10px; | ||
| margin: 8px 12px; | ||
| padding: 12px 14px; | ||
| border-radius: 12px; | ||
| background: rgba(239, 68, 68, 0.07); | ||
| border: 1.5px solid rgba(239, 68, 68, 0.35); | ||
| &__icon { flex-shrink: 0; margin-top: 1px; color: #dc2626; svg { width: 20px; height: 20px; } } | ||
| &__body { flex: 1; min-width: 0; } | ||
| &__title { font-size: 13px; font-weight: 700; color: #b91c1c; margin: 0 0 4px 0; } | ||
| &__detail { font-size: 12px; color: @primaryLabel; margin: 0 0 4px 0; line-height: 1.5; } | ||
| &__values { font-size: 11px; color: @secondaryLabel; margin: 0 0 6px 0; code { font-family: monospace; background: rgba(0,0,0,0.05); padding: 1px 4px; border-radius: 4px; } } | ||
| &__learn-more { font-size: 11px; color: #7559d1; text-decoration: none; font-weight: 600; &:hover { text-decoration: underline; } } | ||
| &__dismiss { flex-shrink: 0; background: none; border: none; cursor: pointer; font-size: 14px; color: @tertiaryLabel; padding: 0 0 0 4px; &:hover { color: @primaryLabel; } } | ||
| } | ||
| .helios-banner-slide-enter-active, .helios-banner-slide-leave-active { transition: opacity 0.25s ease, transform 0.25s ease; } | ||
| .helios-banner-slide-enter-from, .helios-banner-slide-leave-to { opacity: 0; transform: translateY(-6px); } | ||
| </style> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.