diff --git a/package-lock.json b/package-lock.json index d3f8d1c4f..2d3977a48 100644 --- a/package-lock.json +++ b/package-lock.json @@ -23,8 +23,6 @@ "lru-cache": "^7.14.0", "marked": "^4.1.0", "md5": "^2.3.0", - "multibase": "^4.0.4", - "multihashes": "^4.0.2", "pure-svg-code": "^1.0.6", "siwe": "^2.0.5", "svelte": "^3.50.1", @@ -944,11 +942,6 @@ "@jridgewell/sourcemap-codec": "^1.4.10" } }, - "node_modules/@multiformats/base-x": { - "version": "4.0.1", - "resolved": "https://registry.npmjs.org/@multiformats/base-x/-/base-x-4.0.1.tgz", - "integrity": "sha512-eMk0b9ReBbV23xXU693TAIrLyeO5iTgBZGSJfpqriG8UkYvr/hC9u9pyMlAakDNHWmbhMZCDs6KQO0jzKD8OTw==" - }, "node_modules/@nodelib/fs.scandir": { "version": "2.1.5", "resolved": "https://registry.npmjs.org/@nodelib/fs.scandir/-/fs.scandir-2.1.5.tgz", @@ -5131,51 +5124,6 @@ "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==", "dev": true }, - "node_modules/multibase": { - "version": "4.0.6", - "resolved": "https://registry.npmjs.org/multibase/-/multibase-4.0.6.tgz", - "integrity": "sha512-x23pDe5+svdLz/k5JPGCVdfn7Q5mZVMBETiC+ORfO+sor9Sgs0smJzAjfTbM5tckeCqnaUuMYoz+k3RXMmJClQ==", - "deprecated": "This module has been superseded by the multiformats module", - "dependencies": { - "@multiformats/base-x": "^4.0.1" - }, - "engines": { - "node": ">=12.0.0", - "npm": ">=6.0.0" - } - }, - "node_modules/multiformats": { - "version": "9.9.0", - "resolved": "https://registry.npmjs.org/multiformats/-/multiformats-9.9.0.tgz", - "integrity": "sha512-HoMUjhH9T8DDBNT+6xzkrd9ga/XiBI4xLr58LJACwK6G3HTOPeMz4nB4KJs33L2BelrIJa7P0VuNaVF3hMYfjg==" - }, - "node_modules/multihashes": { - "version": "4.0.3", - "resolved": "https://registry.npmjs.org/multihashes/-/multihashes-4.0.3.tgz", - "integrity": "sha512-0AhMH7Iu95XjDLxIeuCOOE4t9+vQZsACyKZ9Fxw2pcsRmlX4iCn1mby0hS0bb+nQOVpdQYWPpnyusw4da5RPhA==", - "dependencies": { - "multibase": "^4.0.1", - "uint8arrays": "^3.0.0", - "varint": "^5.0.2" - }, - "engines": { - "node": ">=12.0.0", - "npm": ">=6.0.0" - } - }, - "node_modules/multihashes/node_modules/uint8arrays": { - "version": "3.1.0", - "resolved": "https://registry.npmjs.org/uint8arrays/-/uint8arrays-3.1.0.tgz", - "integrity": "sha512-ei5rfKtoRO8OyOIor2Rz5fhzjThwIHJZ3uyDPnDHTXbP0aMQ1RN/6AI5B5d9dBxJOU+BvOAk7ZQ1xphsX8Lrog==", - "dependencies": { - "multiformats": "^9.4.2" - } - }, - "node_modules/multihashes/node_modules/varint": { - "version": "5.0.2", - "resolved": "https://registry.npmjs.org/varint/-/varint-5.0.2.tgz", - "integrity": "sha512-lKxKYG6H03yCZUpAGOPOsMcGxd1RHCu1iKvEHYDPmTyq2HueGhD73ssNBqqQWfvYs04G9iUFRvmAVLW20Jw6ow==" - }, "node_modules/nanoid": { "version": "3.3.4", "resolved": "https://registry.npmjs.org/nanoid/-/nanoid-3.3.4.tgz", @@ -7626,11 +7574,6 @@ "@jridgewell/sourcemap-codec": "^1.4.10" } }, - "@multiformats/base-x": { - "version": "4.0.1", - "resolved": "https://registry.npmjs.org/@multiformats/base-x/-/base-x-4.0.1.tgz", - "integrity": "sha512-eMk0b9ReBbV23xXU693TAIrLyeO5iTgBZGSJfpqriG8UkYvr/hC9u9pyMlAakDNHWmbhMZCDs6KQO0jzKD8OTw==" - }, "@nodelib/fs.scandir": { "version": "2.1.5", "resolved": "https://registry.npmjs.org/@nodelib/fs.scandir/-/fs.scandir-2.1.5.tgz", @@ -10734,44 +10677,6 @@ "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==", "dev": true }, - "multibase": { - "version": "4.0.6", - "resolved": "https://registry.npmjs.org/multibase/-/multibase-4.0.6.tgz", - "integrity": "sha512-x23pDe5+svdLz/k5JPGCVdfn7Q5mZVMBETiC+ORfO+sor9Sgs0smJzAjfTbM5tckeCqnaUuMYoz+k3RXMmJClQ==", - "requires": { - "@multiformats/base-x": "^4.0.1" - } - }, - "multiformats": { - "version": "9.9.0", - "resolved": "https://registry.npmjs.org/multiformats/-/multiformats-9.9.0.tgz", - "integrity": "sha512-HoMUjhH9T8DDBNT+6xzkrd9ga/XiBI4xLr58LJACwK6G3HTOPeMz4nB4KJs33L2BelrIJa7P0VuNaVF3hMYfjg==" - }, - "multihashes": { - "version": "4.0.3", - "resolved": "https://registry.npmjs.org/multihashes/-/multihashes-4.0.3.tgz", - "integrity": "sha512-0AhMH7Iu95XjDLxIeuCOOE4t9+vQZsACyKZ9Fxw2pcsRmlX4iCn1mby0hS0bb+nQOVpdQYWPpnyusw4da5RPhA==", - "requires": { - "multibase": "^4.0.1", - "uint8arrays": "^3.0.0", - "varint": "^5.0.2" - }, - "dependencies": { - "uint8arrays": { - "version": "3.1.0", - "resolved": "https://registry.npmjs.org/uint8arrays/-/uint8arrays-3.1.0.tgz", - "integrity": "sha512-ei5rfKtoRO8OyOIor2Rz5fhzjThwIHJZ3uyDPnDHTXbP0aMQ1RN/6AI5B5d9dBxJOU+BvOAk7ZQ1xphsX8Lrog==", - "requires": { - "multiformats": "^9.4.2" - } - }, - "varint": { - "version": "5.0.2", - "resolved": "https://registry.npmjs.org/varint/-/varint-5.0.2.tgz", - "integrity": "sha512-lKxKYG6H03yCZUpAGOPOsMcGxd1RHCu1iKvEHYDPmTyq2HueGhD73ssNBqqQWfvYs04G9iUFRvmAVLW20Jw6ow==" - } - } - }, "nanoid": { "version": "3.3.4", "resolved": "https://registry.npmjs.org/nanoid/-/nanoid-3.3.4.tgz", diff --git a/package.json b/package.json index cd12668ae..c23181bbf 100644 --- a/package.json +++ b/package.json @@ -52,8 +52,6 @@ "lru-cache": "^7.14.0", "marked": "^4.1.0", "md5": "^2.3.0", - "multibase": "^4.0.4", - "multihashes": "^4.0.2", "pure-svg-code": "^1.0.6", "siwe": "^2.0.5", "svelte": "^3.50.1", diff --git a/src/utils.test.ts b/src/utils.test.ts index 112e9363e..44a6824f9 100644 --- a/src/utils.test.ts +++ b/src/utils.test.ts @@ -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", () => { diff --git a/src/utils.ts b/src/utils.ts index e59c2e15d..ca9ae7f70 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -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 { - if (label) return config.provider.resolveName(label); - return null; -} - -export async function resolveMultiLabel( - labels: string[], - config: Config, -): Promise> { - 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);