From 87e3d2a94b4df2bdb8e85edfac7fd3f39d522afb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Haz=20=C3=86=2041?= <4405263+hazae41@users.noreply.github.com> Date: Tue, 12 Dec 2023 18:03:28 +0100 Subject: [PATCH] signatures --- package-lock.json | 48 ++++++++++---- package.json | 4 +- pages/popup.tsx | 22 +++---- .../entities/signatures/data.ts | 66 +++++++++++++++++-- 4 files changed, 106 insertions(+), 34 deletions(-) diff --git a/package-lock.json b/package-lock.json index 13e17884..3c8dd019 100644 --- a/package-lock.json +++ b/package-lock.json @@ -18,7 +18,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", @@ -47,7 +47,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", @@ -2229,9 +2229,9 @@ } }, "node_modules/@hazae41/cadenas": { - "version": "0.3.13", - "resolved": "https://registry.npmjs.org/@hazae41/cadenas/-/cadenas-0.3.13.tgz", - "integrity": "sha512-5PMeloH5kCE0AgYIb8k7bNFnkdKQCV/DisDJfyiqHf0Uehijhb4GViORs41K7kps3ElBWLG+/cYA2YVCWbgQqw==", + "version": "0.3.15", + "resolved": "https://registry.npmjs.org/@hazae41/cadenas/-/cadenas-0.3.15.tgz", + "integrity": "sha512-FenM8E+UGvtcsM2bs1QeFPz2Iu04yci5JkvvmnYkE5ufeuECc8MswmiB/9sEn5KAUK7n50uG3O+nqrExcyB7iA==", "dependencies": { "@hazae41/asn1": "^1.3.26", "@hazae41/base16": "^1.0.16", @@ -3397,21 +3397,43 @@ "dev": true }, "node_modules/@scure/base": { - "version": "1.1.3", - "resolved": "https://registry.npmjs.org/@scure/base/-/base-1.1.3.tgz", - "integrity": "sha512-/+SgoRjLq7Xlf0CWuLHq2LUZeL/w65kfzAPG5NH9pcmBhs+nunQTn4gvdwgMTIXnt9b2C/1SeL2XiysZEyIC9Q==", + "version": "1.1.4", + "resolved": "https://registry.npmjs.org/@scure/base/-/base-1.1.4.tgz", + "integrity": "sha512-wznebWtt+ejH8el87yuD4i9xLSbYZXf1Pe4DY0o/zq/eg5I0VQVXVbFs6XIM0pNVCJ/uE3t5wI9kh90mdLUxtw==", "funding": { "url": "https://paulmillr.com/funding/" } }, "node_modules/@scure/bip32": { - "version": "1.3.2", - "resolved": "https://registry.npmjs.org/@scure/bip32/-/bip32-1.3.2.tgz", - "integrity": "sha512-N1ZhksgwD3OBlwTv3R6KFEcPojl/W4ElJOeCZdi+vuI5QmTFwLq3OFf2zd2ROpKvxFdgZ6hUpb0dx9bVNEwYCA==", + "version": "1.3.3", + "resolved": "https://registry.npmjs.org/@scure/bip32/-/bip32-1.3.3.tgz", + "integrity": "sha512-LJaN3HwRbfQK0X1xFSi0Q9amqOgzQnnDngIt+ZlsBC3Bm7/nE7K0kwshZHyaru79yIVRv/e1mQAjZyuZG6jOFQ==", "dependencies": { - "@noble/curves": "~1.2.0", + "@noble/curves": "~1.3.0", "@noble/hashes": "~1.3.2", - "@scure/base": "~1.1.2" + "@scure/base": "~1.1.4" + }, + "funding": { + "url": "https://paulmillr.com/funding/" + } + }, + "node_modules/@scure/bip32/node_modules/@noble/curves": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/@noble/curves/-/curves-1.3.0.tgz", + "integrity": "sha512-t01iSXPuN+Eqzb4eBX0S5oubSqXbK/xXa1Ne18Hj8f9pStxztHCE2gfboSp/dZRLSqfuLpRK2nDXDK+W9puocA==", + "dependencies": { + "@noble/hashes": "1.3.3" + }, + "funding": { + "url": "https://paulmillr.com/funding/" + } + }, + "node_modules/@scure/bip32/node_modules/@noble/hashes": { + "version": "1.3.3", + "resolved": "https://registry.npmjs.org/@noble/hashes/-/hashes-1.3.3.tgz", + "integrity": "sha512-V7/fPHgl+jsVPXqqeOzT8egNj2iBIVt+ECeMMG8TdcnTikP3oaBtUVqpT/gYCR68aEBJSF+XbYUxStjbFMqIIA==", + "engines": { + "node": ">= 16" }, "funding": { "url": "https://paulmillr.com/funding/" diff --git a/package.json b/package.json index dd2ad158..d989c316 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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", diff --git a/pages/popup.tsx b/pages/popup.tsx index f9ec44d5..7b7a0171 100644 --- a/pages/popup.tsx +++ b/pages/popup.tsx @@ -118,12 +118,12 @@ export function TransactPage() { const zeroHexData = ZeroHexString.from(maybeData) - return maybeSignatures.map(({ text }) => { - return Result.unthrowSync>(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") @@ -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]) @@ -222,7 +220,7 @@ export function TransactPage() { } {maybeSignature &&
- Function: {maybeSignature.text} + Function: {maybeSignature.name}
} {(maybeSignature || maybeData) &&
diff --git a/src/mods/background/service_worker/entities/signatures/data.ts b/src/mods/background/service_worker/entities/signatures/data.ts index a3f4bdc8..68088812 100644 --- a/src/mods/background/service_worker/entities/signatures/data.ts +++ b/src/mods/background/service_worker/entities/signatures/data.ts @@ -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 = + | ApiOk + | ApiErr + +export interface ApiOk { + 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, + readonly function: Record +} + +export interface ApiFunction { + readonly name: string + readonly filtered: boolean +} + export interface SignatureData { /** * Signature */ - readonly text: string + readonly name: string } export async function tryFetchRaw(ethereum: BgEthereumContext, url: string, init: EthereumFetchParams, more: FetcherMore = {}) { @@ -100,7 +138,7 @@ export namespace BgSignature { export function key(hash: ZeroHexString): EthereumQueryKey { return { - version: 2, + version: 3, chainId: 1, method: method, params: [hash] @@ -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(ethereum, url, {}, more) + try { + const url = `https://sig.eth.samczsun.com/api/v1/signatures?function=${hash}` + const fetched = await tryFetchRaw>(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, SignatureData[], Error>({ key: key(hash), fetcher, - // storage + storage }) }