Skip to content

Commit

Permalink
Merge branch 'main' into feat/safari-support
Browse files Browse the repository at this point in the history
  • Loading branch information
kvhnuke committed Feb 22, 2024
2 parents 5d47410 + ba254ad commit 3c3310e
Show file tree
Hide file tree
Showing 89 changed files with 2,329 additions and 917 deletions.
2 changes: 1 addition & 1 deletion packages/extension/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@enkryptcom/extension",
"version": "1.32.0",
"version": "1.33.0",
"private": true,
"scripts": {
"zip": "cd dist; zip -r release.zip *;",
Expand Down
60 changes: 33 additions & 27 deletions packages/extension/src/libs/activity-state/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ActivityOptions } from "./types";
import { Activity, ActivityStatus } from "@/types/activity";
const STORAGE_KEY = "activity";
const MAX_PENDING_TIME = 12 * 60 * 60 * 1000; // 12 hours

class ActivityState {
#storage: BrowserStorage;
constructor() {
Expand All @@ -19,39 +20,44 @@ class ActivityState {
activity: Activity[],
options: ActivityOptions
): Promise<void> {
const activities = await this.getActivitiesById(
this.getActivityId(options)
let activities = await this.getActivitiesById(this.getActivityId(options));
const liveHashesToRemove: string[] = [];
const oldHashesToRemove: string[] = [];
activities.forEach((act) => {
activity.forEach((lact) => {
if (act.transactionHash === lact.transactionHash) {
act.status = lact.status;
liveHashesToRemove.push(lact.transactionHash);
}
if (
act.nonce &&
act.nonce === lact.nonce &&
act.transactionHash !== lact.transactionHash &&
(lact.status === ActivityStatus.success ||
lact.status === ActivityStatus.failed)
) {
oldHashesToRemove.push(act.transactionHash);
}
});
});
activity = activity.filter(
(a) => !liveHashesToRemove.includes(a.transactionHash)
);
const combined = activity.concat(activities);
activities = activities.filter(
(a) => !oldHashesToRemove.includes(a.transactionHash)
);
let combined = activities.concat(activity);
combined.sort((a, b) => {
return b.timestamp - a.timestamp;
});
const existingHashes: string[] = [];
const cleanArr: Activity[] = [];
const currentTime = new Date().getTime();
const minedNonces = cleanArr
.filter(
(item) =>
(item.status === ActivityStatus.success ||
item.status === ActivityStatus.failed) &&
item.nonce
)
.map((item) => item.nonce);
for (let i = 0; i < combined.length; i++) {
if (!existingHashes.includes(combined[i].transactionHash)) {
if (
combined[i].status !== ActivityStatus.pending ||
!combined[i].nonce ||
!minedNonces.includes(combined[i].nonce) ||
(combined[i].status === ActivityStatus.pending &&
combined[i].timestamp > currentTime - MAX_PENDING_TIME)
)
cleanArr.push(combined[i]);
}
existingHashes.push(combined[i].transactionHash);
}
combined = combined.filter(
(a) =>
a.status !== ActivityStatus.pending ||
a.timestamp > currentTime - MAX_PENDING_TIME
);
await this.setActivitiesById(
cleanArr.slice(0, 50),
combined.slice(0, 50),
this.getActivityId(options)
);
}
Expand Down
5 changes: 5 additions & 0 deletions packages/extension/src/libs/json-tree-view/JsonTreeView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
:class="[{ 'root-item': true, dark: colorScheme === 'dark' }]"
:data="parsed"
:max-depth="maxDepth"
:root-key-string="rootKeyString"
@selected="itemSelected"
/>
</template>
Expand All @@ -13,6 +14,10 @@ import JsonTreeViewItem from "./JsonTreeViewItem.vue";
import { ItemType, type ValueTypes, type ItemData } from "./types";
const props = defineProps({
rootKeyString: {
type: String,
default: "Sign typed data",
},
data: {
type: String,
required: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
@click.stop="toggleOpen"
>
<div :class="classes"></div>
{{ data.key === "/" ? "Sign typed data" : data.key }} :
{{ data.key === "/" ? rootKeyString : data.key }} :
<span class="properties">{{ lengthString }}</span>
</button>
<div v-if="state.open">
Expand Down Expand Up @@ -48,6 +48,10 @@ const props = defineProps({
required: true,
type: Object as PropType<ItemData>,
},
rootKeyString: {
type: String,
default: "Sign typed data",
},
maxDepth: {
type: Number,
required: false,
Expand Down
7 changes: 5 additions & 2 deletions packages/extension/src/libs/keyring/public-keyring.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,11 @@ class PublicKeyRing {
walletType: WalletType.mnemonic,
isHardware: false,
};
allKeys["ltc1qccf4af6j3xm9v3r6ujt7dlmvazywzq82hnuwgx"] = {
address: "ltc1qccf4af6j3xm9v3r6ujt7dlmvazywzq82hnuwgx",
allKeys[
"bc1puzz9tmxawd7zdd7klfgtywrgpma3u22fz5ecxhucd4j8tygqe5ms2vdd9y"
] = {
address:
"bc1puzz9tmxawd7zdd7klfgtywrgpma3u22fz5ecxhucd4j8tygqe5ms2vdd9y",
basePath: "m/49'/2'/0'/1",
name: "fake ltc account #4",
pathIndex: 0,
Expand Down
1 change: 1 addition & 0 deletions packages/extension/src/libs/nft-handlers/mew.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export default async (
image: getBestImageURL(asset.urls),
name: asset.name,
url: `https://rarible.com/token/${item.contract_address}:${asset.token_id}?ref=0x5bA9576c214FC7C6649f6F3C73dcbC2769b1761F`,
type: item.schema_type,
};
return retAsset;
}),
Expand Down
83 changes: 83 additions & 0 deletions packages/extension/src/libs/nft-handlers/simplehash-ordinals.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { NFTCollection, NFTItem, NFTType } from "@/types/nft";
import cacheFetch from "../cache-fetch";
import { NetworkNames } from "@enkryptcom/types";
import { SHOrdinalsNFTType, SHOrdinalsResponse } from "./types/simplehash";
import { BaseNetwork } from "@/types/base-network";
const SH_ENDPOINT = "https://partners.mewapi.io/nfts/";
const CACHE_TTL = 1 * 1000;
export default async (
network: BaseNetwork,
address: string
): Promise<NFTCollection[]> => {
const supportedNetworks = {
[NetworkNames.Bitcoin]: "bitcoin",
};
if (!Object.keys(supportedNetworks).includes(network.name))
throw new Error("Simplehash: network not supported");
let allItems: SHOrdinalsNFTType[] = [];
const fetchAll = (continuation?: string): Promise<void> => {
const query = continuation
? continuation
: `${SH_ENDPOINT}owners?chains=${
supportedNetworks[network.name as keyof typeof supportedNetworks]
}&wallet_addresses=${network.displayAddress(address)}`;
return cacheFetch(
{
url: query,
},
CACHE_TTL
).then((json) => {
const items: SHOrdinalsNFTType[] = (json.result as SHOrdinalsResponse)
.nfts;
allItems = allItems.concat(items);
if (json.result.next) return fetchAll(json.result.next);
});
};
await fetchAll();
if (!allItems || !allItems.length) return [];
const collections: Record<string, NFTCollection> = {};
allItems.forEach((item) => {
const collectionName =
item.extra_metadata.ordinal_details.protocol_name === "brc-20"
? "BRC20"
: item.collection.name
? item.collection.name
: "Unknown";
const contractAddress =
item.collection.collection_id || item.contract_address;
if (!item.image_url && !item.previews.image_medium_url) return;
if (!collectionName) return;
if (collections[contractAddress]) {
const tItem: NFTItem = {
contract: contractAddress,
id: item.extra_metadata.ordinal_details.location,
image: item.previews.image_medium_url,
name: item.contract.name,
url: `https://ordinals.com/inscription/${item.contract_address}`,
type: NFTType.Ordinals,
};
collections[contractAddress].items.push(tItem);
} else {
const ret: NFTCollection = {
name: collectionName,
description: item.collection.description,
image:
item.collection.image_url ||
require("@action/assets/common/not-found.jpg"),
contract: contractAddress,
items: [
{
contract: contractAddress,
id: item.extra_metadata.ordinal_details.location,
image: item.image_url || item.previews.image_medium_url,
name: item.contract.name,
url: `https://ordinals.com/inscription/${item.contract_address}`,
type: NFTType.Ordinals,
},
],
};
collections[contractAddress] = ret;
}
});
return Object.values(collections);
};
13 changes: 10 additions & 3 deletions packages/extension/src/libs/nft-handlers/simplehash.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { NFTCollection, NFTItem } from "@/types/nft";
import { NFTCollection, NFTItem, NFTType } from "@/types/nft";
import { NodeType } from "@/types/provider";
import cacheFetch from "../cache-fetch";
import { NetworkNames } from "@enkryptcom/types";
Expand Down Expand Up @@ -33,9 +33,9 @@ export default async (
const fetchAll = (continuation?: string): Promise<void> => {
const query = continuation
? continuation
: `${SH_ENDPOINT}owners?chains=${
: `${SH_ENDPOINT}owners_v2?chains=${
supportedNetworks[network.name as keyof typeof supportedNetworks]
}&wallet_addresses=${address}`;
}&wallet_addresses=${address}&filters=spam_score__lte=75`;
return cacheFetch(
{
url: query,
Expand All @@ -52,6 +52,11 @@ export default async (
const collections: Record<string, NFTCollection> = {};
allItems.forEach((item) => {
if (!item.image_url && !item.previews.image_medium_url) return;
if (
item.contract.type !== NFTType.ERC1155 &&
item.contract.type !== NFTType.ERC721
)
return;
if (collections[item.contract_address]) {
const tItem: NFTItem = {
contract: item.contract_address,
Expand All @@ -61,6 +66,7 @@ export default async (
url:
item.external_url ||
getExternalURL(network, item.contract_address, item.token_id),
type: item.contract.type,
};
collections[item.contract_address].items.push(tItem);
} else {
Expand All @@ -80,6 +86,7 @@ export default async (
url:
item.external_url ||
getExternalURL(network, item.contract_address, item.token_id),
type: item.contract.type,
},
],
};
Expand Down
8 changes: 3 additions & 5 deletions packages/extension/src/libs/nft-handlers/types/mew.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { NFTType } from "@/types/nft";

export enum ContentRepresentation {
IMAGE = "IMAGE",
MEDIA = "MEDIA",
}
export enum SchemaType {
ERC721 = "ERC721",
ERC1155 = "ERC1155",
}
export interface Trait {
trait: string;
count: number;
Expand Down Expand Up @@ -36,7 +34,7 @@ export interface NFTCollection {
name: string;
description: string;
image: string;
schema_type: SchemaType;
schema_type: NFTType;
contract_address: string;
contract_name: string;
contract_symbol: string;
Expand Down
22 changes: 22 additions & 0 deletions packages/extension/src/libs/nft-handlers/types/simplehash.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { NFTType } from "@/types/nft";

export interface SHNFTType {
nft_id: string;
chain: string;
Expand All @@ -19,10 +21,30 @@ export interface SHNFTType {
description: string;
image_url: string;
external_url: string;
collection_id: string;
spam_score: number;
};
contract: {
name: string;
type: NFTType;
};
}
export interface SHOrdinalsNFTType extends SHNFTType {
extra_metadata: {
ordinal_details: {
location: string;
protocol_name: string;
};
};
}
export interface SHResponse {
next: string;
previous: string;
nfts: SHNFTType[];
}

export interface SHOrdinalsResponse {
next: string;
previous: string;
nfts: SHOrdinalsNFTType[];
}
13 changes: 13 additions & 0 deletions packages/extension/src/libs/settings-state/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
EVMSettingsType,
SubstrateSettingsType,
SettingsType,
BtcSettingsType,
} from "./types";
import { merge } from "lodash";
class SettingsState {
Expand Down Expand Up @@ -34,6 +35,13 @@ class SettingsState {
};
return merge(settings, state);
}
async getBtcSettings(): Promise<BtcSettingsType> {
const state = await this.getStateByKey(StorageKeys.btcState);
const settings: BtcSettingsType = {
injectUnisat: false,
};
return merge(settings, state);
}
async deleteStateByKey(key: string): Promise<void> {
await this.#storage.remove(key);
}
Expand All @@ -46,12 +54,17 @@ class SettingsState {
async setSubstrateSettings(state: SubstrateSettingsType): Promise<void> {
await this.#storage.set(StorageKeys.substrateState, state);
}
async setBtcSettings(state: BtcSettingsType): Promise<void> {
await this.#storage.set(StorageKeys.btcState, state);
}
async getAllSettings(): Promise<SettingsType> {
const evmstate = await this.getEVMSettings();
const substratestate = await this.getSubstrateSettings();
const btcstate = await this.getBtcSettings();
return {
evm: evmstate,
substrate: substratestate,
btc: btcstate,
manifestVersion: Browser.runtime.getManifest().manifest_version,
};
}
Expand Down
5 changes: 5 additions & 0 deletions packages/extension/src/libs/settings-state/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export enum StorageKeys {
evmState = "evm-settings-state",
substrateState = "substrate-settings-state",
btcState = "btc-settings-state",
}
export interface EVMInjectSettings {
disabled: boolean;
Expand All @@ -12,8 +13,12 @@ export interface EVMSettingsType {
export interface SubstrateSettingsType {
injectPolkadotjs: boolean;
}
export interface BtcSettingsType {
injectUnisat: boolean;
}
export interface SettingsType {
evm: EVMSettingsType;
substrate: SubstrateSettingsType;
btc: BtcSettingsType;
manifestVersion: number;
}
Loading

1 comment on commit 3c3310e

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.