mirror of
https://seed.radicle.dev/rad:z4V1sjrXqjvFdnCUbxPFqd5p4DtH5.git
synced 2026-11-21 06:10:44 +08:00
Remove unused utility functions
This also lets us get rid of the deprecated multibase and multihashses dependencies. Signed-off-by: Rūdolfs Ošiņš <rudolfs@osins.org>
This commit is contained in:
-105
@@ -2,11 +2,8 @@ import { ethers } from "ethers";
|
||||
import type { RouteLocation } from "@app/index";
|
||||
import md5 from "md5";
|
||||
import { BigNumber } from "ethers";
|
||||
import multibase from "multibase";
|
||||
import katex from "katex";
|
||||
import multihashes from "multihashes";
|
||||
import type { Config } from "@app/config";
|
||||
import config from "@app/config.json";
|
||||
import { assert } from "@app/error";
|
||||
import type { EnsProfile } from "@app/base/registrations/registrar";
|
||||
import { getAddress, getResolver } from "@app/base/registrations/registrar";
|
||||
@@ -34,13 +31,6 @@ export interface Safe {
|
||||
threshold: number;
|
||||
}
|
||||
|
||||
export interface SafeTransaction {
|
||||
to: string;
|
||||
value: string;
|
||||
data: string;
|
||||
operation: number;
|
||||
}
|
||||
|
||||
export interface Token {
|
||||
name: string;
|
||||
symbol: string;
|
||||
@@ -116,22 +106,10 @@ export function formatLocationHash(hash: string | null): number | null {
|
||||
return null;
|
||||
}
|
||||
|
||||
export function formatIssueId(id: string): string {
|
||||
return id.substring(0, 11);
|
||||
}
|
||||
|
||||
export function formatSeedId(id: string): string {
|
||||
return id.substring(0, 6) + "…" + id.substring(id.length - 6, id.length);
|
||||
}
|
||||
|
||||
export function removePrefix(hash: string): string {
|
||||
if (!hash.startsWith("0x")) {
|
||||
return hash;
|
||||
}
|
||||
|
||||
return hash.substring(2);
|
||||
}
|
||||
|
||||
export function formatRadicleUrn(id: string): string {
|
||||
assert(isRadicleId(id));
|
||||
|
||||
@@ -153,19 +131,6 @@ export function formatAddress(input: string): string {
|
||||
);
|
||||
}
|
||||
|
||||
export function formatIpfsFile(ipfs: string | undefined): string | undefined {
|
||||
if (ipfs) return `${config.ipfs.gateway}${ipfs.replace("ipfs://", "")}`;
|
||||
return undefined;
|
||||
}
|
||||
|
||||
// If the string is less than 10 characters the entire string is returned.
|
||||
export function formatHash(hash: string): string {
|
||||
if (hash.length < 10) return hash;
|
||||
return (
|
||||
hash.substring(0, 6) + "…" + hash.substring(hash.length - 4, hash.length)
|
||||
);
|
||||
}
|
||||
|
||||
export function formatCommit(oid: string): string {
|
||||
return oid.substring(0, 7);
|
||||
}
|
||||
@@ -306,11 +271,6 @@ export function isUrl(input: string): boolean {
|
||||
return /^https?:\/\//.test(input);
|
||||
}
|
||||
|
||||
// Check whether the input is a DID
|
||||
export function isDid(input: string): boolean {
|
||||
return /^did:[a-zA-Z0-9]+:[a-zA-Z0-9]+$/.test(input);
|
||||
}
|
||||
|
||||
export function isENSName(input: string, config: Config): boolean {
|
||||
const domain = config.registrar.domain.replace(".", "\\.");
|
||||
const regEx = new RegExp(`^[a-zA-Z0-9]+.(${domain}|eth)$`);
|
||||
@@ -358,19 +318,6 @@ export function formatName(input: string, config: Config): string {
|
||||
return parseEnsLabel(input, config);
|
||||
}
|
||||
|
||||
// Create a Radicle ID from a root hash.
|
||||
export function formatRadicleId(hash: Uint8Array): string {
|
||||
// Remove any zero-padding from the byte array. SHA1 is 20 bytes long.
|
||||
const sha1Bytes = 20;
|
||||
const suffix = hash.slice(hash.length - sha1Bytes);
|
||||
|
||||
// Create a multihash by adding prefix 17 for SHA-1 and 20 for the hash length.
|
||||
const multihash = new Uint8Array([17, 20, ...suffix]);
|
||||
const payload = multibase.encode("base32z", multihash);
|
||||
|
||||
return `rad:git:${new TextDecoder().decode(payload)}`;
|
||||
}
|
||||
|
||||
// Parse a Radicle Id (URN).
|
||||
export function parseRadicleId(urn: string): string {
|
||||
return urn.replace(/^rad:[a-z]+:/, "");
|
||||
@@ -381,21 +328,6 @@ export function getDaysPassed(from: Date, to: Date): number {
|
||||
return Math.floor((to.getTime() - from.getTime()) / (24 * 60 * 60 * 1000));
|
||||
}
|
||||
|
||||
// Decode a Radicle Id (URN).
|
||||
export function decodeRadicleId(urn: string): Uint8Array {
|
||||
const encoded = parseRadicleId(urn);
|
||||
const multihash = multibase.decode(encoded);
|
||||
const hash = multihashes.decode(multihash);
|
||||
|
||||
return hash.digest;
|
||||
}
|
||||
|
||||
// Create a project hash from a hash and format.
|
||||
export function formatProjectHash(multihash: Uint8Array): string {
|
||||
const decoded = multihashes.decode(multihash);
|
||||
return ethers.utils.hexlify(decoded.digest).replace(/^0x/, "");
|
||||
}
|
||||
|
||||
export function parseEmoji(input: string): string {
|
||||
if (input in emojis) {
|
||||
return emojis[input];
|
||||
@@ -428,27 +360,6 @@ export async function identifyAddress(
|
||||
return AddressType.EOA;
|
||||
}
|
||||
|
||||
// Resolve a label under the radicle domain.
|
||||
export async function resolveLabel(
|
||||
label: string | undefined,
|
||||
config: Config,
|
||||
): Promise<string | null> {
|
||||
if (label) return config.provider.resolveName(label);
|
||||
return null;
|
||||
}
|
||||
|
||||
export async function resolveMultiLabel(
|
||||
labels: string[],
|
||||
config: Config,
|
||||
): Promise<Record<string, string | null>> {
|
||||
const addresses = await Promise.all(
|
||||
labels.map(label => config.provider.resolveName(label)),
|
||||
);
|
||||
return labels.reduce((arr, curr, index) => {
|
||||
return { ...arr, [curr]: addresses[index] };
|
||||
}, {});
|
||||
}
|
||||
|
||||
// Resolves an ENS profile or return null
|
||||
export async function resolveEnsProfile(
|
||||
addressOrName: string,
|
||||
@@ -568,22 +479,6 @@ export function gravatarURL(email: string): string {
|
||||
return `https://www.gravatar.com/avatar/${hash}`;
|
||||
}
|
||||
|
||||
export class EthSignSignature {
|
||||
signer: string;
|
||||
data: string;
|
||||
|
||||
constructor(signer: string, signature: string) {
|
||||
this.signer = signer;
|
||||
this.data = signature;
|
||||
}
|
||||
staticPart(): string {
|
||||
return this.data;
|
||||
}
|
||||
dynamicPart(): string {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
export const getCode = cache.cached(
|
||||
async (address: string, config: Config) => {
|
||||
return await config.provider.getCode(address);
|
||||
|
||||
Reference in New Issue
Block a user