Skip to content

Commit

Permalink
chore: remove bech32 dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
o-az committed Sep 4, 2024
1 parent ce81328 commit 1f36479
Show file tree
Hide file tree
Showing 9 changed files with 2,192 additions and 1,608 deletions.
2 changes: 1 addition & 1 deletion app/app.nix
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
{
packages = {
app = unstablePkgs.buildNpmPackage {
npmDepsHash = "sha256-dsbzL/WaaSxnQEX5f9o7/RMOrwrvy7a1X+5eVHxbUWE=";
npmDepsHash = "sha256-I9nDWfMbCtsf0hYvjJAe7AXLHdkvES2UsZ8jEowMkek=";
src = ./.;
sourceRoot = "app";
npmFlags = [ "--legacy-peer-deps" ];
Expand Down
3,322 changes: 1,957 additions & 1,365 deletions app/package-lock.json

Large diffs are not rendered by default.

2 changes: 0 additions & 2 deletions app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
"@union/client": "npm:@jsr/union__client@^0.0.7",
"@wagmi/connectors": "^5.1.8",
"@wagmi/core": "^2.13.4",
"bech32": "^2.0.0",
"bits-ui": "^0.21.13",
"cmdk-sv": "^0.0.18",
"gql.tada": "^1.8.6",
Expand All @@ -41,7 +40,6 @@
"svelte-ux": "^0.74.6",
"temporal-polyfill": "^0.2.5",
"three": "^0.168.0",
"uint8array-extras": "^1.4.0",
"valibot": "^0.41.0",
"vaul-svelte": "^0.3.2",
"viem": "^2.21.1"
Expand Down
66 changes: 34 additions & 32 deletions app/src/lib/components/address-multichain.svelte
Original file line number Diff line number Diff line change
@@ -1,26 +1,21 @@
<script lang="ts">
import type { Chain } from "$lib/types"
import { rawToBech32 } from "$lib/utilities/address"
import { hexAddressToBech32 } from "@union/client"
import { Badge } from "$lib/components/ui/badge/index.ts"
import { fade, blur, fly, slide, scale } from "svelte/transition"
export let address: { address: string; normalizedAddress: string }
export let chains: Array<Chain>
const addressChain = chains.find(c => address.address.startsWith(c.addr_prefix)) as Chain
const cosmosChains: Array<Chain> = chains.filter(c => c.rpc_type === "cosmos" && c.enabled_staging)
const rpc_type = address.address.startsWith("0x") ? "evm" : "cosmos"
// @ts-ignore
const fromHexString = hexString =>
Uint8Array.from(hexString.match(/.{1,2}/g).map(byte => Number.parseInt(byte, 16)))
const otherCosmosAddresses: Array<{ address: string; chain: Chain }> = chains
.filter(c => c.rpc_type === "cosmos")
.map(c => ({
address: rawToBech32(c.addr_prefix, fromHexString(address.normalizedAddress)),
chain: c
.filter(chain => chain.rpc_type === "cosmos")
.map(chain => ({
address: hexAddressToBech32({
bech32Prefix: chain.addr_prefix,
address: `0x${address.normalizedAddress}`
}),
chain: chain
}))
.filter(pair => pair.address !== address.address)
Expand Down Expand Up @@ -56,24 +51,31 @@ setInterval(() => {
</script>

{#if addressChain?.rpc_type === "evm"}
<div class="flex items-center gap-2">
<div class="text-sm sm:text-base md:text-lg font-bold flex items-center"><span class="text-muted-foreground">0x</span>{address.address.slice(2)}</div>
<Badge class="hidden md:block">EVM</Badge>
</div>
<div class="flex items-center gap-2">
<div class="text-sm sm:text-base md:text-lg font-bold flex items-center">
<span class="text-muted-foreground">0x</span>{address.address.slice(2)}
</div>
<Badge class="hidden md:block">EVM</Badge>
</div>
{:else}
<div class="flex items-center">
<ul>
{#each allCosmosAddressesDeduplicated as cosmosAddress, i}
{#if i === addressIndex}
<li
class="text-sm sm:text-base md:text-lg first:font-bold whitespace-pre">
<span class="select-none">{' '.repeat(longestPrefix - cosmosAddress.prefix.length)}</span><span class="text-muted-foreground mr-1">{cosmosAddress.prefix}</span>{cosmosAddress.body}<span class="ml-1 text-muted-foreground">{cosmosAddress.checksum}</span>
</li>
{/if}
{/each}
</ul>
<Badge class="hidden md:block">Cosmos</Badge>
</div>

<div class="flex items-center">
<ul>
{#each allCosmosAddressesDeduplicated as cosmosAddress, i}
{#if i === addressIndex}
<li
class="text-sm sm:text-base md:text-lg first:font-bold whitespace-pre"
>
<span class="select-none"
>{" ".repeat(longestPrefix - cosmosAddress.prefix.length)}</span
><span class="text-muted-foreground mr-1"
>{cosmosAddress.prefix}</span
>{cosmosAddress.body}<span class="ml-1 text-muted-foreground"
>{cosmosAddress.checksum}</span
>
</li>
{/if}
{/each}
</ul>
<Badge class="hidden md:block">Cosmos</Badge>
</div>
{/if}

9 changes: 6 additions & 3 deletions app/src/lib/queries/balance/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { isAddress, type Address } from "viem"
import { raise } from "$lib/utilities/index.ts"
import { bytesToBech32Address } from "@union/client"
import { getCosmosChainBalances } from "./cosmos.ts"
import { createQueries } from "@tanstack/svelte-query"
import { erc20ReadMulticall } from "./evm/multicall.ts"
import { rawToBech32 } from "$lib/utilities/address.ts"
import type { Chain, UserAddresses } from "$lib/types.ts"
import { getBalancesFromAlchemy } from "./evm/alchemy.ts"
import { getBalancesFromRoutescan } from "./evm/routescan.ts"
Expand Down Expand Up @@ -72,8 +72,11 @@ export function userBalancesQuery({
const url = chain.rpcs.filter(rpc => rpc.type === "rest").at(0)?.url
if (!url) raise(`No REST RPC available for chain ${chain.chain_id}`)

const bech32_addr = rawToBech32(chain.addr_prefix, userAddr.cosmos.bytes)
return getCosmosChainBalances({ url, walletAddress: bech32_addr })
const bech32Address = bytesToBech32Address({
toPrefix: chain.addr_prefix,
bytes: userAddr.cosmos.bytes
})
return getCosmosChainBalances({ url, walletAddress: bech32Address })
}

return []
Expand Down
26 changes: 4 additions & 22 deletions app/src/lib/utilities/address.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,12 @@
import { bech32 } from "bech32"
import { bytesToBech32Address } from "@union/client"
import type { Chain, UserAddresses } from "$lib/types"

export function convertCosmosAddress({ address, toPrefix }: { address: string; toPrefix: string }) {
const words = bech32.decode(address).words
return bech32.encode(toPrefix, words)
}

export const rawToHex = (raw: Uint8Array): string =>
`${Array.from(raw)
.map(i => i.toString(16).padStart(2, "0"))
.join("")
.toLowerCase()}`

export const rawToBech32 = (prefix: string, raw: Uint8Array): string => {
const words = bech32.toWords(raw)
return bech32.encode(prefix, words)
}

export const userAddrOnChain = (userAddr: UserAddresses, chain: Chain | null): string | null => {
if (!chain) {
return null
}
export const userAddrOnChain = (userAddr: UserAddresses, chain?: Chain): string | null => {
if (!chain) return null

if (chain.rpc_type === "cosmos") {
if (userAddr.cosmos?.bytes) {
return rawToBech32(chain.addr_prefix, userAddr.cosmos.bytes)
return bytesToBech32Address({ bytes: userAddr.cosmos.bytes, toPrefix: chain.addr_prefix })
}
console.log("userAddrOnChain got no cosmos address")
return null
Expand Down
10 changes: 4 additions & 6 deletions app/src/routes/faucet/(components)/dydx-faucet.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import { cosmosStore } from "$/lib/wallet/cosmos/config.ts"
import { derived, writable, type Writable } from "svelte/store"
import { dydxFaucetMutation } from "$lib/graphql/queries/faucet"
import { getCosmosChainBalances } from "$lib/queries/balance/cosmos"
import { isValidCosmosAddress } from "$lib/wallet/utilities/validate.ts"
import { createCosmosSdkAddressRegex } from "$lib/utilities/address.ts"
import { bech32ToBech32Address, isValidBech32Address } from "@union/client"
import type { AwaitedReturnType, DiscriminatedUnion } from "$lib/utilities/types.ts"
import { convertCosmosAddress, createCosmosSdkAddressRegex } from "$lib/utilities/address.ts"
type DydxFaucetState = DiscriminatedUnion<
"kind",
Expand All @@ -30,7 +30,7 @@ type DydxFaucetState = DiscriminatedUnion<
let dydxAddress = derived(cosmosStore, $cosmosStore =>
$cosmosStore.address
? convertCosmosAddress({
? bech32ToBech32Address({
address: $cosmosStore.address,
toPrefix: "dydx"
})
Expand Down Expand Up @@ -242,7 +242,7 @@ let dydxBalance = createQuery(
requestDydxFromFaucet()
}}
disabled={$dydxFaucetState.kind !== "IDLE" ||
isValidCosmosAddress($dydxAddress, ["dydx"]) === false}
isValidBech32Address($dydxAddress) === false}
class={cn(
"min-w-[110px] disabled:cursor-not-allowed disabled:opacity-50 rounded-md",
"bg-[#6866FF] text-[#ffffff] dark:bg-[#6866FF] dark:text-[#ffffff]"
Expand Down Expand Up @@ -270,5 +270,3 @@ let dydxBalance = createQuery(
{/if}
</Card.Content>
</Card.Root>

<style lang="postcss"></style>
10 changes: 4 additions & 6 deletions app/src/routes/faucet/(components)/stride-faucet.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import { cosmosStore } from "$/lib/wallet/cosmos/config.ts"
import { derived, writable, type Writable } from "svelte/store"
import { strideFaucetMutation } from "$lib/graphql/queries/faucet"
import { getCosmosChainBalances } from "$lib/queries/balance/cosmos"
import { isValidCosmosAddress } from "$lib/wallet/utilities/validate.ts"
import { createCosmosSdkAddressRegex } from "$lib/utilities/address.ts"
import { bech32ToBech32Address, isValidBech32Address } from "@union/client"
import type { AwaitedReturnType, DiscriminatedUnion } from "$lib/utilities/types.ts"
import { convertCosmosAddress, createCosmosSdkAddressRegex } from "$lib/utilities/address.ts"
type DydxFaucetState = DiscriminatedUnion<
"kind",
Expand All @@ -30,7 +30,7 @@ type DydxFaucetState = DiscriminatedUnion<
let strideAddress = derived(cosmosStore, $cosmosStore =>
$cosmosStore.address
? convertCosmosAddress({
? bech32ToBech32Address({
address: $cosmosStore.address,
toPrefix: "stride"
})
Expand Down Expand Up @@ -239,7 +239,7 @@ let strideBalance = createQuery(
requestStrdFromFaucet()
}}
disabled={$strideFaucetState.kind !== "IDLE" ||
isValidCosmosAddress($strideAddress, ["stride"]) === false}
isValidBech32Address($strideAddress) === false}
class={cn(
"min-w-[110px] disabled:cursor-not-allowed disabled:opacity-50 rounded-md",
"bg-[#E6007A] text-[#ffffff] dark:bg-[#E6007A] dark:text-[#ffffff]"
Expand Down Expand Up @@ -267,5 +267,3 @@ let strideBalance = createQuery(
{/if}
</Card.Content>
</Card.Root>

<style lang="postcss"></style>
Loading

0 comments on commit 1f36479

Please sign in to comment.