Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/add assets whitelist add #4

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 32 additions & 2 deletions src/app/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ function App() {
request.assetId,
request.amount,
request.destinationAddress,
request.destinationChainId,
request.destinationChainId
],
});
chrome.storage?.local?.set?.({ pendingTx: request });
Expand Down Expand Up @@ -240,7 +240,7 @@ function App() {
result.pendingTx.assetId,
result.pendingTx.amount,
result.pendingTx.destinationAddress,
result.pendingTx.destinationChainId,
result.pendingTx.destinationChainId
],
});
setConfirmationModalOpen(true);
Expand Down Expand Up @@ -410,9 +410,39 @@ function App() {
}
}

async function getAssetWhitelistAddRequests() {
try {
const response = await fetchBackground({ method: "GET_ASSETS_WHITELIST_ADD_REQUESTS" });
const assetWhitelistAddRequests = response.data?.map((request) => {
return {
id: request.id,
method: "FINALIZE_ASSETS_WHITELIST_ADD_REQUESTS",
name: "Confirm add asset to whitelist",
params : [
{
key: "Asset id",
value: request.asset_id,
},
{
key: "Asset name",
value: request.asset_name,
}
]

}
})
if (assetWhitelistAddRequests && assetWhitelistAddRequests.length > 0) {
goTo(OuterConfirmation, { reqs: assetWhitelistAddRequests });
}
} catch (e) {
console.error(e);
}
}

await getAliasCreationRequests();
await getIonicSwapRequests();
await getSignRequests();
await getAssetWhitelistAddRequests();
}

if (appConnected && !connectOpened && loggedIn && state.isConnected) {
Expand Down
44 changes: 43 additions & 1 deletion src/background/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
getWhiteList,
getAssetInfo,
createAlias,
addAssetToWhitelist,
} from "./wallet";
import JSONbig from "json-bigint";

Expand Down Expand Up @@ -178,6 +179,7 @@ const savedRequests = {
IONIC_SWAP: {},
ACCEPT_IONIC_SWAP: {},
CREATE_ALIAS: {},
ASSETS_WHITELIST_ADD: {}
};

const allPopupIds = [];
Expand Down Expand Up @@ -217,6 +219,7 @@ const SELF_ONLY_REQUESTS = [
"PING_WALLET",
"SET_ACTIVE_WALLET",
"GET_WALLETS",
"GET_ASSETS_WHITELIST_ADD_REQUESTS"
];

chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
Expand Down Expand Up @@ -315,7 +318,8 @@ async function processRequest(request, sender, sendResponse) {
request.assetId,
request.destination,
request.amount,
request.decimalPoint
request.decimalPoint,
request.comment
)
.then((data) => {
sendResponse({ data });
Expand Down Expand Up @@ -374,6 +378,44 @@ async function processRequest(request, sender, sendResponse) {
break;
}

case "GET_ASSETS_WHITELIST_ADD_REQUESTS": {
PopupRequestsMethods.getRequestsList("ASSETS_WHITELIST_ADD", sendResponse);
break
}

case "ASSETS_WHITELIST_ADD": {
try {
const assetToAdd = await getAsset(request.asset_id);
if (!assetToAdd) throw new Error("Failed to fetch asset");
request.asset_name = assetToAdd.full_name;
} catch (error) {
return sendResponse({ error });
}
PopupRequestsMethods.onRequestCreate(
"ASSETS_WHITELIST_ADD",
request,
sendResponse,
request
);
break;

}

case "FINALIZE_ASSETS_WHITELIST_ADD_REQUESTS": {
PopupRequestsMethods.onRequestFinalize(
'ASSETS_WHITELIST_ADD',
request,
sendResponse,
(req) => addAssetToWhitelist(req.asset_id),
{
console: "Error accepting finalize add asset to whitelist:",
response: "An error occurred while finalize add asset to whitelist",
reqNotFound: "finalize add asset to whitelist accept request not found",
}
)
break
}

case "GET_ACCEPT_IONIC_SWAP_REQUESTS":
PopupRequestsMethods.getRequestsList("ACCEPT_IONIC_SWAP", sendResponse);
break;
Expand Down
15 changes: 14 additions & 1 deletion src/background/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,17 @@ export const ionicSwapAccept = async (swapParams) => {
return data;
};

export const addAssetToWhitelist = async (assetId) => {
const response = await fetchData("assets_whitelist_add", {
asset_id: assetId,
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
return data;
}

export const createAlias = async ({ alias, address }) => {
const response = await fetchData("register_alias", {
al: {
Expand All @@ -303,7 +314,8 @@ export const transfer = async (
assetId = "d6329b5b1f7c0805b5c345f4957554002a2f557845f64d7645dae0e051a6498a",
destination,
amount,
decimalPoint
decimalPoint,
comment
) => {
const destinations = [
{
Expand All @@ -320,6 +332,7 @@ export const transfer = async (
destinations,
fee: 10000000000,
mixin: 10,
comment
});

if (!response.ok) {
Expand Down