Skip to content

Commit

Permalink
signatures
Browse files Browse the repository at this point in the history
  • Loading branch information
hazae41 committed Dec 12, 2023
1 parent d5f5a3f commit 87e3d2a
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 34 deletions.
48 changes: 35 additions & 13 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 @@ -38,7 +38,7 @@
"@hazae41/binary": "^1.3.4",
"@hazae41/box": "^1.0.14",
"@hazae41/bytes": "^1.2.9",
"@hazae41/cadenas": "^0.3.13",
"@hazae41/cadenas": "^0.3.15",
"@hazae41/cascade": "^1.2.1",
"@hazae41/chacha20poly1305": "^1.0.7",
"@hazae41/cleaner": "^2.0.7",
Expand Down Expand Up @@ -67,7 +67,7 @@
"@heroicons/react": "^2.0.18",
"@nuintun/qrcode": "^3.4.0",
"@paulmillr/qr": "^0.1.1",
"@scure/bip32": "^1.3.2",
"@scure/bip32": "^1.3.3",
"@scure/bip39": "^1.2.1",
"ethers": "^6.9.0",
"idna-uts46-hx": "^5.1.2",
Expand Down
22 changes: 10 additions & 12 deletions pages/popup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,12 @@ export function TransactPage() {

const zeroHexData = ZeroHexString.from(maybeData)

return maybeSignatures.map(({ text }) => {
return Result.unthrowSync<Result<{ text: string, decoded: string }, Error>>(t => {
const abi = Cubane.Abi.FunctionSignature.tryParse(text).throw(t)
const { args } = Cubane.Abi.tryDecode(abi.funcAndArgs, zeroHexData).throw(t)
return maybeSignatures.map(({ name }) => {
return Result.runAndWrapSync<{ name: string, decoded: string }>(() => {
const abi = Cubane.Abi.FunctionSignature.parseOrThrow(name)
const { args } = Cubane.Abi.decodeOrThrow(abi.funcAndArgs, zeroHexData)

function stringify(x: any): string {
function stringifyOrThrow(x: any): string {
if (typeof x === "string")
return x
if (typeof x === "boolean")
Expand All @@ -133,17 +133,15 @@ export function TransactPage() {
if (typeof x === "bigint")
return String(x)
if (x instanceof Uint8Array)
return ZeroHexString.from(Base16.get().tryEncode(x).throw(t))
return ZeroHexString.from(Base16.get().encodeOrThrow(x))
if (Array.isArray(x))
return `(${x.map(stringify).join(", ")})`
return `(${x.map(stringifyOrThrow).join(", ")})`
return "unknown"
}

const decoded = Result.runAndDoubleWrapSync(() => {
return args.intoOrThrow().map(stringify).join(", ")
}).throw(t)
const decoded = args.intoOrThrow().map(stringifyOrThrow).join(", ")

return new Ok({ text, decoded })
return { name, decoded }
}).inspectErrSync(e => console.warn({ e })).unwrapOr(undefined)
}).find(it => it != null)
}, [maybeData, maybeHash, maybeSignatures])
Expand Down Expand Up @@ -222,7 +220,7 @@ export function TransactPage() {
</div>}
{maybeSignature &&
<div className="grow w-full p-4 border border-contrast rounded-xl whitespace-pre-wrap mt-2 break-words">
Function: {maybeSignature.text}
Function: {maybeSignature.name}
</div>}
{(maybeSignature || maybeData) &&
<div className="grow w-full p-4 border border-contrast rounded-xl whitespace-pre-wrap mt-2 break-words">
Expand Down
66 changes: 59 additions & 7 deletions src/mods/background/service_worker/entities/signatures/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,55 @@ import { AbortSignals } from "@/libs/signals/signals";
import { Circuits } from "@/libs/tor/circuits/circuits";
import { ZeroHexString } from "@hazae41/cubane";
import { fetch } from "@hazae41/fleche";
import { Fetched, FetcherMore, IDBStorage, createQuery } from "@hazae41/glacier";
import { Data, Fail, Fetched, FetcherMore, IDBStorage, createQuery } from "@hazae41/glacier";
import { RpcRequestPreinit } from "@hazae41/jsonrpc";
import { Option } from "@hazae41/option";
import { Err, Ok, Result } from "@hazae41/result";
import { Catched, Err, Ok, Result } from "@hazae41/result";
import { BgEthereumContext, EthereumFetchParams, EthereumQueryKey } from "../wallets/data";

export type ApiResult<T> =
| ApiOk<T>
| ApiErr

export interface ApiOk<T> {
readonly ok: true,
readonly result: T
}

export interface ApiErr {
readonly ok: false,
readonly error: unknown
}

export class ApiError extends Error {
readonly #class = ApiError
readonly name = this.#class.name

constructor(options: ErrorOptions) {
super(`Could not fetch`, options)
}

static from(cause: unknown) {
return new ApiError({ cause })
}

}

export interface ApiData {
readonly event: Record<string, unknown>,
readonly function: Record<string, ApiFunction[]>
}

export interface ApiFunction {
readonly name: string
readonly filtered: boolean
}

export interface SignatureData {
/**
* Signature
*/
readonly text: string
readonly name: string
}

export async function tryFetchRaw<T>(ethereum: BgEthereumContext, url: string, init: EthereumFetchParams, more: FetcherMore = {}) {
Expand Down Expand Up @@ -100,7 +138,7 @@ export namespace BgSignature {

export function key(hash: ZeroHexString): EthereumQueryKey<unknown> {
return {
version: 2,
version: 3,
chainId: 1,
method: method,
params: [hash]
Expand All @@ -115,14 +153,28 @@ export namespace BgSignature {

export function schema(ethereum: BgEthereumContext, hash: ZeroHexString, storage: IDBStorage) {
const fetcher = async (key: unknown, more: FetcherMore) => {
const url = `https://sig.api.vechain.energy/${hash}`
return await tryFetchRaw<SignatureData[]>(ethereum, url, {}, more)
try {
const url = `https://sig.eth.samczsun.com/api/v1/signatures?function=${hash}`
const fetched = await tryFetchRaw<ApiResult<ApiData>>(ethereum, url, {}, more).then(r => r.unwrap())

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

const result = fetched.unwrap()

if (!result.ok)
return new Ok(new Fail(ApiError.from(result.error)))

return new Ok(new Data(result.result.function[hash]))
} catch (e: unknown) {
return new Ok(new Fail(Catched.from(e)))
}
}

return createQuery<EthereumQueryKey<unknown>, SignatureData[], Error>({
key: key(hash),
fetcher,
// storage
storage
})
}

Expand Down

0 comments on commit 87e3d2a

Please sign in to comment.