Skip to content

Commit

Permalink
ens
Browse files Browse the repository at this point in the history
  • Loading branch information
hazae41 committed Oct 4, 2023
1 parent 19d862e commit f586f0e
Show file tree
Hide file tree
Showing 8 changed files with 164 additions and 67 deletions.
20 changes: 11 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"@hazae41/cascade": "^1.1.27",
"@hazae41/chacha20poly1305": "^1.0.2",
"@hazae41/cleaner": "^2.0.3",
"@hazae41/cubane": "^0.0.11",
"@hazae41/cubane": "^0.0.12",
"@hazae41/cursor": "^1.1.26",
"@hazae41/echalote": "^0.2.80",
"@hazae41/ed25519": "^2.1.6",
Expand Down Expand Up @@ -78,7 +78,7 @@
"@types/chrome": "^0.0.246",
"@types/node": "20.8.2",
"@types/react": "18.2.24",
"@types/react-dom": "18.2.8",
"@types/react-dom": "18.2.10",
"@types/w3c-web-usb": "^1.0.7",
"autoprefixer": "^10.4.16",
"eslint": "8.50.0",
Expand Down
42 changes: 0 additions & 42 deletions src/libs/ethereum/mods/ens/ens.ts

This file was deleted.

66 changes: 65 additions & 1 deletion src/mods/background/service_worker/entities/wallets/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,7 @@ export function getTokenBalance(ethereum: EthereumContext, account: string, toke
}).then(r => r.throw(t))

if (fetched.isErr())
return new Ok(new Fail(fetched.inner))
return new Ok(fetched)

const returns = Cubane.Abi.createStaticBigUint(32)
const balance = Cubane.Abi.tryDecode(returns, fetched.inner).throw(t).value
Expand Down Expand Up @@ -612,4 +612,68 @@ export function getTokenBalance(ethereum: EthereumContext, account: string, toke
indexer,
storage
})
}

async function tryFetchResolver(ethereum: EthereumContext, namehash: Uint8Array): Promise<Result<Fetched<ZeroHexString, Error>, Error>> {
return await Result.unthrow(async t => {
const registry = "0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e"

const signature = Cubane.Abi.FunctionSignature.tryParse("resolver(bytes32)").throw(t)
const data = Cubane.Abi.tryEncode(signature.args.from(namehash)).throw(t)

const fetched = await tryEthereumFetch<ZeroHexString>(ethereum, {
method: "eth_call",
params: [{
to: registry,
data: data
}, "pending"]
}).then(r => r.throw(t))

if (fetched.isErr())
return new Ok(fetched)

const returns = Cubane.Abi.StaticAddress
const address = Cubane.Abi.tryDecode(returns, fetched.inner).throw(t).value

return new Ok(new Data(address))
})
}

export function getENS(ethereum: EthereumContext, name: string, storage: IDBStorage) {
const fetcher = () => Result.unthrow<Result<Fetched<ZeroHexString, Error>, Error>>(async t => {
const namehash = Cubane.Ens.tryNamehash(name).throw(t)
const resolver = await tryFetchResolver(ethereum, namehash).then(r => r.throw(t))

if (resolver.isErr())
return new Ok(resolver)

const signature = Cubane.Abi.FunctionSignature.tryParse("addr(bytes32)").throw(t)
const data = Cubane.Abi.tryEncode(signature.args.from(namehash)).throw(t)

const fetched = await tryEthereumFetch<ZeroHexString>(ethereum, {
method: "eth_call",
params: [{
to: resolver.inner,
data: data
}, "pending"]
}).then(r => r.throw(t))

if (fetched.isErr())
return new Ok(fetched)

const returns = Cubane.Abi.StaticAddress
const address = Cubane.Abi.tryDecode(returns, fetched.inner).throw(t).value

return new Ok(new Data(address))
})

return createQuery<EthereumQueryKey<unknown>, ZeroHexString, Error>({
key: {
chainId: 1,
method: "eth_resolveEns",
params: [name]
},
fetcher,
storage
})
}
17 changes: 15 additions & 2 deletions src/mods/background/service_worker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { Bytes } from "@hazae41/bytes"
import { Cadenas } from "@hazae41/cadenas"
import { ChaCha20Poly1305 } from "@hazae41/chacha20poly1305"
import { Disposer } from "@hazae41/cleaner"
import { ZeroHexString } from "@hazae41/cubane"
import { Circuit, Echalote, Fallback, TorClientDuplex } from "@hazae41/echalote"
import { Ed25519 } from "@hazae41/ed25519"
import { Fleche } from "@hazae41/fleche"
Expand Down Expand Up @@ -52,7 +53,7 @@ import { ExSessionData, Session, SessionByOrigin, SessionData, SessionRef, WcSes
import { Status, StatusData } from "./entities/sessions/status/data"
import { Users } from "./entities/users/all/data"
import { User, UserData, UserInit, UserSession, getCurrentUser } from "./entities/users/data"
import { EthereumContext, EthereumQueryKey, Wallet, WalletData, WalletRef, getBalance, getEthereumUnknown, getPairPrice, getTokenBalance, tryEthereumFetch } from "./entities/wallets/data"
import { EthereumContext, EthereumQueryKey, Wallet, WalletData, WalletRef, getBalance, getENS, getEthereumUnknown, getPairPrice, getTokenBalance, tryEthereumFetch } from "./entities/wallets/data"
import { tryCreateUserStorage } from "./storage"

declare global {
Expand Down Expand Up @@ -648,6 +649,16 @@ export class Global {
})
}

async makeEthereumResolveEns(ethereum: EthereumContext, request: RpcRequestPreinit<unknown>, storage: IDBStorage): Promise<Result<SimpleFetcherfulQuery<EthereumQueryKey<unknown>, ZeroHexString, Error>, Error>> {
return await Result.unthrow(async t => {
const [name] = (request as RpcRequestPreinit<[string]>).params

const query = getENS(ethereum, name, storage)

return new Ok(query)
})
}

async eth_sendTransaction(ethereum: EthereumContext, request: RpcRequestPreinit<unknown>, mouse?: Mouse): Promise<Result<string, Error>> {
return await Result.unthrow(async t => {
const [{ from, to, gas, value, data }] = (request as RpcRequestPreinit<[{
Expand Down Expand Up @@ -1058,13 +1069,15 @@ export class Global {
return new Ok(getEthereumUnknown(ethereum, request, storage))
}

async routeAndMakeEthereum(ethereum: EthereumContext, request: RpcRequestPreinit<unknown>, storage: IDBStorage): Promise<Result<SimpleFetcherfulQuery<any, FixedInit, Error>, Error>> {
async routeAndMakeEthereum(ethereum: EthereumContext, request: RpcRequestPreinit<unknown>, storage: IDBStorage): Promise<Result<SimpleFetcherfulQuery<any, any, Error>, Error>> {
if (request.method === "eth_getBalance")
return await this.makeEthereumBalance(ethereum, request, storage)
if (request.method === "eth_getTokenBalance")
return await this.makeEthereumTokenBalance(ethereum, request, storage)
if (request.method === "eth_getPairPrice")
return await this.makeEthereumPairPrice(ethereum, request, storage)
if (request.method === "eth_resolveEns")
return await this.makeEthereumResolveEns(ethereum, request, storage)
return await this.makeEthereumUnknown(ethereum, request, storage)
}

Expand Down
25 changes: 20 additions & 5 deletions src/mods/foreground/entities/wallets/actions/send/contract.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { BigInts } from "@/libs/bigints/bigints";
import { UIError } from "@/libs/errors/errors";
import { ContractTokenInfo } from "@/libs/ethereum/mods/chain";
import { ContractTokenInfo, chainByChainId, chainIdByName } from "@/libs/ethereum/mods/chain";
import { Fixed } from "@/libs/fixed/fixed";
import { Radix } from "@/libs/hex/hex";
import { Outline } from "@/libs/icons/icons";
Expand All @@ -19,12 +19,14 @@ import { Ok, Result } from "@hazae41/result";
import { Transaction, ethers } from "ethers";
import { useDeferredValue, useMemo, useState } from "react";
import { useWalletData } from "../../context";
import { EthereumContextProps, EthereumWalletInstance, useGasPrice, useNonce, useTokenBalance } from "../../data";
import { EthereumContextProps, EthereumWalletInstance, useENS, useEthereumContext, useGasPrice, useNonce, useTokenBalance } from "../../data";

export function WalletDataSendContractTokenDialog(props: TitleProps & CloseProps & EthereumContextProps & { token: ContractTokenInfo }) {
const wallet = useWalletData()
const { title, context, token, close } = props

const mainnet = useEthereumContext(wallet, chainByChainId[chainIdByName.ETHEREUM])

const balanceQuery = useTokenBalance(wallet.address, token, context, [])
const maybeBalance = balanceQuery.data?.inner

Expand All @@ -42,14 +44,23 @@ export function WalletDataSendContractTokenDialog(props: TitleProps & CloseProps
setRawRecipientInput(e.currentTarget.value)
}, [])

const maybeEnsName = defRecipientInput.endsWith(".eth")
? defRecipientInput
: undefined
const ensAddressQuery = useENS(mainnet, maybeEnsName)

const maybeFinalAddress = defRecipientInput.endsWith(".eth")
? ensAddressQuery.data?.inner
: defRecipientInput

const RecipientInput = <>
<div className="">
Recipient
</div>
<div className="h-2" />
<Input.Contrast className="w-full"
value={rawRecipientInput}
placeholder="0x..."
placeholder="brume.eth"
onChange={onRecipientInputChange} />
</>

Expand Down Expand Up @@ -109,9 +120,13 @@ export function WalletDataSendContractTokenDialog(props: TitleProps & CloseProps
return new UIError(`Could not fetch or parse nonce`)
}).throw(t)

const address = Option.wrap(maybeFinalAddress).okOrElseSync(() => {
return new UIError(`Could not fetch or parse address`)
}).throw(t)

const signature = Cubane.Abi.FunctionSignature.tryParse("transfer(address,uint256)").throw(t)
const fixed = Fixed.fromDecimalString(defValueInput, token.decimals)
const args = signature.args.from(ethers.getAddress(defRecipientInput), fixed.value)
const args = signature.args.from(ethers.getAddress(address), fixed.value)
const data = Cubane.Abi.tryEncode(args).throw(t)

const gas = await context.background.tryRequest<string>({
Expand Down Expand Up @@ -158,7 +173,7 @@ export function WalletDataSendContractTokenDialog(props: TitleProps & CloseProps

return Ok.void()
}).then(Results.logAndAlert)
}, [context, wallet, maybeGasPrice, maybeFinalNonce, defRecipientInput, defValueInput])
}, [context, wallet, maybeGasPrice, maybeFinalNonce, maybeFinalAddress, defValueInput])

const TxHashDisplay = <>
<div className="">
Expand Down
Loading

0 comments on commit f586f0e

Please sign in to comment.