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

feat: add rpc for adding reply addresses in the snap #6

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 1 addition & 1 deletion snap/snap.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/ethereum-push-notification-service/push-protocol-snaps"
},
"source": {
"shasum": "g4ULf+D5SCWqCidfH0PXnQM7hgSWF1hRM2sou0sC/RA=",
"shasum": "KF3oo+RZbKssmYih5dHhiHUsa9f7lQpkP4bKVl6FGdA=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
6 changes: 5 additions & 1 deletion snap/src/helper/snapstoragecheck.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@

export const SnapStorageCheck = async () => {
const defaultstate = { addresses: [], popuptoggle: 0 };
const defaultstate = {
addresses: [],
popuptoggle: 0,
userReplyAddresses: [],
};
let persistedData = await snap.request({
method: "snap_manageState",
params: { operation: "get" },
Expand Down
67 changes: 67 additions & 0 deletions snap/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
addAddress,
confirmAddress,
removeAddress,
addReplyAddress,
} from "./utils/fetchAddress";
import { fetchAllAddrNotifs } from "./utils/fetchnotifs";
import { popupHelper } from "./utils/popupHelper";
Expand Down Expand Up @@ -263,6 +264,72 @@ export const onRpcRequest: OnRpcRequestHandler = async ({
})
break;
}

case "pushproto_addaddressforreply": {
if (request.params != null && request.params.address != null) {
let addresscheck = await SnapStorageAddressCheck(
request.params.address
);
let isValidAddress = ethers.utils.isAddress(request.params.address);

if (addresscheck == false && isValidAddress == true) {
const res = await snap.request({
method: "snap_dialog",
params: {
type: "confirmation",
content: panel([
heading("Address Addition"),
divider(),
text("Do you want to add this reply address to the snap?"),
text(`${request.params.address}`),
]),
},
});
if (res) {
await addReplyAddress(request.params.address);
await confirmAddress();
} else {
await snap.request({
method: "snap_dialog",
params: {
type: "confirmation",
content: panel([
heading("Error"),
divider(),
text(`${request.params.address}`),
text("Address not added to the snap"),
]),
},
});
}
} else {
await snap.request({
method: "snap_dialog",
params: {
type: "alert",
content: panel([
heading("Error"),
divider(),
text("Address already added to the snap"),
]),
},
});
}
} else {
await snap.request({
method: "snap_dialog",
params: {
type: "alert",
content: panel([
heading("Error"),
divider(),
text("Error reading input, please try again"),
]),
},
});
}
break;
}
default:
throw new Error("Method not found.");
}
Expand Down
54 changes: 54 additions & 0 deletions snap/src/utils/fetchAddress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export const addAddress = async (address: string) => {
const data = {
addresses: [address],
popuptoggle: 0,
userReplyAddresses: [],
};
await snap.request({
method: "snap_manageState",
Expand All @@ -31,6 +32,59 @@ export const addAddress = async (address: string) => {
const data = {
addresses: addrlist,
popuptoggle: popuptoggle,
userReplyAddresses: [],
};
await snap.request({
method: "snap_manageState",
params: { operation: "update", newState: data },
});
}
}
} else {
await snap.request({
method: "snap_dialog",
params: {
type: "alert",
content: panel([
heading("Error"),
text("Invalid Ethereum Address address"),
]),
},
});
}
};

// function to add addresses to which user wants to reply in the snap
export const addReplyAddress = async (address: string) => {
const persistedData = await snap.request({
method: "snap_manageState",
params: { operation: "get" },
});

const isValidAddress = ethers.utils.isAddress(address);

if (isValidAddress) {
if (persistedData == null) {
const data = {
userReplyAddresses: [address],
popuptoggle: 0,
addresses: [],
};
await snap.request({
method: "snap_manageState",
params: { operation: "update", newState: data },
});
} else {
const replyAddrList = persistedData.userReplyAddresses;
const popuptoggle = persistedData.popuptoggle;
if (replyAddrList!.includes(address)) {
return;
} else {
replyAddrList!.push(address);
const data = {
userReplyAddresses: replyAddrList,
popuptoggle: popuptoggle,
addresses: [],
};
await snap.request({
method: "snap_manageState",
Expand Down