Skip to content

Commit

Permalink
✨ Add checksum to evm
Browse files Browse the repository at this point in the history
  • Loading branch information
williamchong committed Nov 19, 2024
1 parent e948166 commit e243dd9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
16 changes: 14 additions & 2 deletions app.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<div>
<section>
<h1>Bech32 Wallet Address Prefix Web Converter</h1>
<p>Convert between Cosmos(cosmos1), Ethereum(0x) and different cosmos-based chain using bech32 address prefixes.</p>
<p>Convert between Cosmos(cosmos1), Ethereum(0x) with checksum and different cosmos or EVM based chain using bech32 address prefixes.</p>
</section>
<hr>
<section>
Expand Down Expand Up @@ -68,6 +68,7 @@ declare global {
interface Window {
keplr?: any;
ethereum?: any;
keccak256?: any;
}
}
Expand Down Expand Up @@ -110,7 +111,18 @@ const convertedCosmosAddress = computed(() => {
const convertedEvmAddress = computed(() => {
if (convertedWords.value.length === 0) return '-'
const data = bech32.fromWords(convertedWords.value)
return `0x${Buffer.from(data).toString('hex')}`;
const address = Buffer.from(data).toString('hex');
const hash = window.keccak256(address).toString('hex');
// Apply checksum
let checksumAddress = '0x';
for (let i = 0; i < address.length; i++) {
if (parseInt(hash[i], 16) >= 8) {
checksumAddress += address[i].toUpperCase();
} else {
checksumAddress += address[i];
}
}
return checksumAddress;
})
async function getKeplrCosmosAddress() {
Expand Down
6 changes: 6 additions & 0 deletions nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ export default defineNuxtConfig({
{ name: 'og:title', content: 'Bech32 Wallet Address Prefix Web Converter' },
{ name: 'description', content: 'Convert between Cosmos(cosmos1), Ethereum(0x) and different cosmos-based chain using bech32 address prefixes.' }
],
link: [
{ rel: 'preload', href: 'https://cdn.jsdelivr.net/npm/keccak256@latest/keccak256.js', as: 'script' },
],
script: [
{ src: 'https://cdn.jsdelivr.net/npm/keccak256@latest/keccak256.js', body: true },
],
},
},
compatibilityDate: '2024-04-03',
Expand Down

0 comments on commit e243dd9

Please sign in to comment.