mirror of
https://seed.radicle.dev/rad:z4V1sjrXqjvFdnCUbxPFqd5p4DtH5.git
synced 2026-11-16 03:40:37 +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:
@@ -10,17 +10,6 @@ describe("Conversions", () => {
|
||||
});
|
||||
|
||||
describe("Format functions", () => {
|
||||
test.each([
|
||||
{
|
||||
cid: "Qm1234567890123456789012345678901234567890",
|
||||
expected:
|
||||
"https://ipfs.io/ipfs/Qm1234567890123456789012345678901234567890",
|
||||
},
|
||||
{ cid: undefined, expected: undefined },
|
||||
])("formatIpfsFile $cid => $expected", ({ cid, expected }) => {
|
||||
expect(utils.formatIpfsFile(cid)).toEqual(expected);
|
||||
});
|
||||
|
||||
test.each([
|
||||
{ amount: "1000", digits: 2, expected: "10.0" },
|
||||
{ amount: "10000000000000000000", expected: "10.0" },
|
||||
@@ -30,28 +19,6 @@ describe("Format functions", () => {
|
||||
);
|
||||
});
|
||||
|
||||
test("formatRadicleId", () => {
|
||||
expect(
|
||||
utils.formatRadicleId(
|
||||
new Uint8Array([
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 156, 38, 115, 99, 61, 118, 237,
|
||||
10, 20, 115, 111, 188, 10, 117, 137, 59, 107, 76, 77, 86,
|
||||
]),
|
||||
),
|
||||
).toEqual("rad:git:hnrkjajuucc6zp5eknt3s9xykqsrus44cjimy");
|
||||
});
|
||||
|
||||
test("formatProjectHash", () => {
|
||||
expect(
|
||||
utils.formatProjectHash(
|
||||
new Uint8Array([
|
||||
17, 20, 69, 234, 87, 208, 172, 127, 33, 22, 110, 216, 52, 69, 104,
|
||||
219, 88, 66, 50, 152, 115, 23,
|
||||
]),
|
||||
),
|
||||
).toEqual("45ea57d0ac7f21166ed8344568db584232987317");
|
||||
});
|
||||
|
||||
test.each([
|
||||
{ hash: "#L42", expected: 42 },
|
||||
{ hash: "#ETH", expected: null },
|
||||
@@ -107,16 +74,6 @@ describe("Format functions", () => {
|
||||
expect(utils.parseEmoji(input)).toEqual(expected);
|
||||
});
|
||||
|
||||
test.each([
|
||||
{
|
||||
hash: "0x8f918133b56bb85c18ea192549503f0ea59e3beb1f88023f442656c660018e3a",
|
||||
expected: "0x8f91…8e3a",
|
||||
},
|
||||
{ hash: "0x8f91813", expected: "0x8f91813" }, // If the string length is less than 10 characters the entire string is returned.
|
||||
])("formatHash $hash => $expected", ({ hash, expected }) => {
|
||||
expect(utils.formatHash(hash)).toEqual(expected);
|
||||
});
|
||||
|
||||
test.each([
|
||||
{ commit: "a8a6a979a6261a2ec1ea85fc9a65a4a30aa22cc8", expected: "a8a6a97" },
|
||||
{ commit: "a8a6a97", expected: "a8a6a97" },
|
||||
@@ -197,19 +154,6 @@ describe("String Assertions", () => {
|
||||
])("isUrl $url => $expected", ({ url, expected }) => {
|
||||
expect(utils.isUrl(url)).toBe(expected);
|
||||
});
|
||||
|
||||
test.each([
|
||||
{
|
||||
did: "did:3:kjzl6cwe1jw1481xu9oyww9bhmueqr8f5uryk4xha9jzhj6vi063e0blpnil383",
|
||||
expected: true,
|
||||
},
|
||||
{
|
||||
did: "did:kjzl6cwe1jw1481xu9oyww9bhmueqr8f5uryk4xha9jzhj6vi063e0blpnil383",
|
||||
expected: false,
|
||||
},
|
||||
])("isDid $did => $expected", ({ did, expected }) => {
|
||||
expect(utils.isDid(did)).toBe(expected);
|
||||
});
|
||||
});
|
||||
|
||||
describe("Others", () => {
|
||||
@@ -259,25 +203,6 @@ describe("Others", () => {
|
||||
} as Config),
|
||||
).toEqual(expected);
|
||||
});
|
||||
|
||||
test.each([
|
||||
{
|
||||
id: "rad:git:hnrkjajuucc6zp5eknt3s9xykqsrus44cjimy",
|
||||
expected: new Uint8Array([
|
||||
156, 38, 115, 99, 61, 118, 237, 10, 20, 115, 111, 188, 10, 117, 137, 59,
|
||||
107, 76, 77, 86,
|
||||
]),
|
||||
},
|
||||
{
|
||||
id: "hnrkjajuucc6zp5eknt3s9xykqsrus44cjimy",
|
||||
expected: new Uint8Array([
|
||||
156, 38, 115, 99, 61, 118, 237, 10, 20, 115, 111, 188, 10, 117, 137, 59,
|
||||
107, 76, 77, 86,
|
||||
]),
|
||||
},
|
||||
])("decodeRadicleId", ({ id, expected }) => {
|
||||
expect(utils.decodeRadicleId(id)).toEqual(expected);
|
||||
});
|
||||
});
|
||||
|
||||
describe("Parse Strings", () => {
|
||||
|
||||
-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