Skip to content

Commit

Permalink
Merge pull request #3 from mystic-lab/RpcRequest-address
Browse files Browse the repository at this point in the history
RPC Requests for addresses
  • Loading branch information
schnetzlerjoe authored Jun 20, 2023
2 parents a02efc9 + 10b6c64 commit 9570797
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
2 changes: 1 addition & 1 deletion packages/snap/snap.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/cosmos/snap.git"
},
"source": {
"shasum": "n/5WeQYG/UJjweRdx6ub4gV4Xr+S8UuXHg0CTaWaefg=",
"shasum": "6olA6PxeC6F7OYbgqkNcbpFtyv7eYqMBqdBjIkwmaro=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
36 changes: 32 additions & 4 deletions packages/snap/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { OnRpcRequestHandler } from "@metamask/snaps-types";
import { panel, text } from "@metamask/snaps-ui";
import { initializeChains } from "./initialize";
import { Chains } from "./types/chains";
import { ChainState } from "./state";
import { Address, Addresses } from "./types/address"
import { ChainState, AddressState } from "./state";

/**
* Handle incoming JSON-RPC requests, sent through `wallet_invokeSnap`.
Expand Down Expand Up @@ -48,13 +49,40 @@ export const onRpcRequest: OnRpcRequestHandler = async ({ request }) => {
return;
case "addAddress":
// Add a new address into the address book in wallet state
return;
if (
!(
request.params != null &&
typeof request.params == "object" &&
"address" in request.params &&
typeof request.params.address == "string"
)
) {
throw new Error("Invalid addAddress request");
}

let new_address : Address = JSON.parse(request.params.address);

return await AddressState.addAddress(new_address)

case "deleteAddress":
// Delete an address from the address book in wallet state
return;
if (
!(
request.params != null &&
typeof request.params == "object" &&
"chain_id" in request.params &&
typeof request.params.chain_id == "string"
)
) {
throw new Error("Invalid addAddress request");
}

return await AddressState.removeAddress(request.params.chain_id)

case "getAddresses":
// Get all addresses from the address book in wallet state
return;
return await AddressState.getAddressBook();

default:
throw new Error("Method not found.");
}
Expand Down

0 comments on commit 9570797

Please sign in to comment.