Skip to content

Commit

Permalink
Proto type additions: Akash & Agoric force
Browse files Browse the repository at this point in the history
  • Loading branch information
schnetzlerjoe committed Jul 3, 2024
1 parent 52d77eb commit 32c3b72
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 140 deletions.
24 changes: 4 additions & 20 deletions packages/snap/package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
{
"name": "@cosmsnap/snap",
"version": "0.1.22",
"version": "0.1.23",
"description": "The Cosmos extension for your Metamask wallet.",
"repository": {
"type": "git",
"url": "git+https://github.com/cosmos/snap.git"
"url": "git+https://github.com/cosmos/snap.git"
},
"license": "MIT",
"author": "Mystic Labs, Inc",
Expand All @@ -17,7 +16,7 @@
"README.md"
],
"scripts": {
"build": "mm-snap build",
"build": "NODE_OPTIONS=--max-old-space-size=4096 mm-snap build",
"build:clean": "yarn clean && yarn build",
"clean": "rimraf dist",
"lint": "yarn lint:eslint && yarn lint:misc --check",
Expand All @@ -29,13 +28,8 @@
"start": "mm-snap watch",
"test": "npx ava tests/*",
"test:e2e": "jest"
"test:e2e": "jest"
},
"ava": {
"timeout": "60s",
"files": [
"**/*-test.js"
],
"timeout": "60s",
"files": [
"**/*-test.js"
Expand All @@ -48,11 +42,8 @@
]
},
"dependencies": {
"@agoric/cosmic-proto": "^0.3.0",
"@cosmjs/amino": "^0.31.1",
"@cosmjs/crypto": "^0.31.1",
"@cosmjs/encoding": "^0.31.1",
"@agoric/cosmic-proto": "^0.3.0",
"@agoric/cosmic-proto": "^0.4.1-u16.0",
"@akashnetwork/akashjs": "^0.10.0",
"@cosmjs/amino": "^0.31.1",
"@cosmjs/crypto": "^0.31.1",
"@cosmjs/encoding": "^0.31.1",
Expand All @@ -64,14 +55,10 @@
"buffer": "^6.0.3",
"cosmjs-types": "^0.8.0",
"lodash": "^4.17.21",
"lodash": "^4.17.21",
"osmojs": "^16.5.1",
"ses": "^0.18.4"
},
"devDependencies": {
"@babel/core": "^7.24.0",
"@babel/preset-env": "^7.24.0",
"@babel/preset-typescript": "^7.23.3",
"@babel/core": "^7.24.0",
"@babel/preset-env": "^7.24.0",
"@babel/preset-typescript": "^7.23.3",
Expand All @@ -91,7 +78,6 @@
"@typescript-eslint/parser": "^5.33.0",
"ava": "^5.3.0",
"babel-jest": "^29.7.0",
"babel-jest": "^29.7.0",
"eslint": "^8.21.0",
"eslint-config-prettier": "^8.1.0",
"eslint-plugin-import": "^2.26.0",
Expand All @@ -100,14 +86,12 @@
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-prettier": "^4.2.1",
"jest": "^29.7.0",
"jest": "^29.7.0",
"node-fetch": "2",
"prettier": "^2.2.1",
"prettier-plugin-packagejson": "^2.2.11",
"rimraf": "^3.0.2",
"through2": "^4.0.2",
"ts-jest": "^29.1.2",
"ts-jest": "^29.1.2",
"ts-node": "^10.9.1",
"typescript": "^5.4.5"
},
Expand Down
7 changes: 3 additions & 4 deletions packages/snap/snap.manifest.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
{
"version": "0.1.22",
"version": "0.1.23",
"description": "Cosmos Extension that adds Cosmos support to Metamask.",
"proposedName": "Cosmos Extension",
"repository": {
"type": "git",
"url": "git+https://github.com/cosmos/snap.git"
"url": "git+https://github.com/cosmos/snap.git"
},
"source": {
"shasum": "iy7sFNnki+rvhkmOaWGfKE5ZiaEqOYkuE1AVb5dEiN0=",
"shasum": "IWERZuFb2xO0t8RLmZz6O4hhsapa2MjU8Lqr39IDGw0=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down Expand Up @@ -97,7 +96,7 @@
"endowment:cronjob": {
"jobs": [
{
"expression": "*/30 * * * *",
"expression": "*/5 * * * *",
"request": {
"method": "notification",
"params": {}
Expand Down
89 changes: 1 addition & 88 deletions packages/snap/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@ import { OnHomePageHandler, OnRpcRequestHandler, panel, text, heading, divider,
import { AccountData } from '@cosmjs/amino';
import { initializeChains } from "./initialize";
import { Chain, Chains, Fees, Msg, UpdateChainParams } from "./types/chains";
import { Chain, Chains, Fees, Msg, UpdateChainParams } from "./types/chains";
import { Address } from "./types/address";
import { ChainState, AddressState } from "./state";
import { sendTx, signAmino, signDirect, submitTransaction } from "./transaction";
import { COIN_TYPES, DEFAULT_FEES } from "./constants";
import { SignDoc, TxBody, AuthInfo } from "cosmjs-types/cosmos/tx/v1beta1/tx";
import { StdSignDoc } from "@cosmjs/amino";
import { bigintReplacer, decodeProtoMessage, decodeTxBodyIntoMessages } from "./parser";
import { bigintReplacer, decodeProtoMessage, decodeTxBodyIntoMessages } from "./parser";
import Long from "long";
import { Key } from '@keplr-wallet/types';
import { fromBech32 } from '@cosmjs/encoding';
Expand All @@ -29,7 +27,6 @@ import { snapNotify } from "./notification";
*/
export const onRpcRequest: OnRpcRequestHandler = async ({
request,
}) => {
}) => {
let res: Object = {};
switch (request.method) {
Expand Down Expand Up @@ -100,7 +97,7 @@ export const onRpcRequest: OnRpcRequestHandler = async ({
method: "snap_manageState",
params: {
operation: "update",
newState: { chains: chains.string(), addresses: JSON.stringify([]), initialized: true, hd: (request.params && request.params.hd) ? request.params.hd : true },
newState: { chains: chains.string(), addresses: JSON.stringify([]), initialized: true },
},
});

Expand Down Expand Up @@ -217,7 +214,6 @@ export const onRpcRequest: OnRpcRequestHandler = async ({
});

return {
data: JSON.stringify(result),
data: JSON.stringify(result),
success: true,
statusCode: 201,
Expand All @@ -236,7 +232,6 @@ export const onRpcRequest: OnRpcRequestHandler = async ({
});

return {
data: JSON.stringify(result),
data: JSON.stringify(result),
success: false,
statusCode: 500,
Expand Down Expand Up @@ -303,7 +298,6 @@ export const onRpcRequest: OnRpcRequestHandler = async ({
});

return {
data: JSON.stringify(txResponse),
data: JSON.stringify(txResponse),
success: false,
statusCode: 500,
Expand Down Expand Up @@ -337,16 +331,7 @@ export const onRpcRequest: OnRpcRequestHandler = async ({
let txBody = TxBody.decode(signDocNew.bodyBytes);
const msgs = [];


for (const msg of txBody.messages) {
if (isTxBodyEncodeObject(msg)) {
const messages = await decodeTxBodyIntoMessages(msg.typeUrl, msg.value);
for (const message of messages) {
let decMsgTxBody = await decodeProtoMessage(message.typeUrl, message.value);
msgs.push(decMsgTxBody);
}
continue;
}
if (isTxBodyEncodeObject(msg)) {
const messages = await decodeTxBodyIntoMessages(msg.typeUrl, msg.value);
for (const message of messages) {
Expand All @@ -366,7 +351,6 @@ export const onRpcRequest: OnRpcRequestHandler = async ({
let ui = [
heading("Confirm Transaction"),
divider(),
divider(),
heading("Chain"),
text(`${request.params.chain_id}`),
divider(),
Expand Down Expand Up @@ -671,77 +655,6 @@ export const onRpcRequest: OnRpcRequestHandler = async ({
},
});

return {
data: request.params,
success: true,
statusCode: 201,
};
case "changeChain":
if (
!(
request.params != null &&
typeof request.params == "object" &&
"chain_id" in request.params &&
typeof request.params.chain_id == "string"
)
) {
throw new Error("Invalid changeChain request");
}

// We only allow changing the rpc and the slip44 for now. So if we do not have these alert.
if (!request.params.rpc && !request.params.slip44) {
await snap.request({
method: "snap_dialog",
params: {
type: "alert",
content: panel([
heading("Error Occured"),
text(`No RPC or Coin Type changes provided. Please provide "rpc" or "slip44".`),
]),
},
});
}
const changes: UpdateChainParams = {
slip44: request.params.slip44,
rpc: request.params.rpc,
};

// Ensure user confirms changeChain
let confirmChangeChain = await snap.request({
method: "snap_dialog",
params: {
type: "confirmation",
content: panel([
heading(`Confirm Change for Chain ${request.params.chain_id}`),
divider(),
heading("Chain Info"),
text(`${JSON.stringify(JSON.stringify(changes), null, 4)}`),
divider(),
text("Note: this is an advanced, experimental feature so handle it with care."),
]),
},
});
if (!confirmChangeChain) {
throw new Error("Chain change was denied.");
}

// Update the chain in wallet state
await ChainState.updateChain(request.params.chain_id, changes);

await snap.request({
method: "snap_dialog",
params: {
type: "alert",
content: panel([
heading("Chain Changed"),
text(
`Successfully changed the following for chain ${request.params.chain_id}.`
),
text(JSON.stringify(changes, null, 4)),
]),
},
});

return {
data: request.params,
success: true,
Expand Down
52 changes: 29 additions & 23 deletions packages/snap/src/notification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,28 +75,34 @@ export class Notifications {
}
}

// IMPORTANT: The notification cannot have characters larger than 50 characters
export const snapNotify = async (akash_address: string) => {
const notifications = new Notifications();
// Update the notifications in Appwrite database
await notifications.updateAkashNotifications(akash_address);
// Get the notifications that are unread in Appwrite database
const notifs = await notifications.getAkashNotifications(akash_address);
if (!Array.isArray(notifs)) {
throw new Error("Expected an array of notifications, received:", notifs);
}
// Post notifications to Metamask after filtering for unread
const filtered = notifs.filter((notif) => !notif.read);
for (const notif of filtered) {
await snap.request({
method: "snap_notify",
params: {
type: "inApp",
message: notif.notification,
},
});
// Update notification as read
await notifications.markNotificationRead(
notif.$id
);
}
try {
const notifications = new Notifications();
// Update the notifications in Appwrite database
await notifications.updateAkashNotifications(akash_address);
// Get the notifications that are unread in Appwrite database
const notifs = await notifications.getAkashNotifications(akash_address);
if (!Array.isArray(notifs)) {
throw new Error("Expected an array of notifications, received:", notifs);
}
// Post notifications to Metamask after filtering for unread
const filtered = notifs.filter((notif) => !notif.read);
for (const notif of filtered) {
await snap.request({
method: "snap_notify",
params: {
type: "inApp",
message: notif.notification,
},
});
// Update notification as read
await notifications.markNotificationRead(
notif.$id
);
}
} catch (err) {
console.error(err);
throw new Error(`${err}`);
}
};
21 changes: 18 additions & 3 deletions packages/snap/src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ import {
osmosisProtoRegistry
} from 'osmojs';

import { getAkashTypeRegistry } from "@akashnetwork/akashjs/build/stargate/index";

import {
MsgProvision,
MsgWalletSpendAction,
} from '@agoric/cosmic-proto/swingset/msgs.js';

export const bigintReplacer = (value: any): any => {
if (typeof value === 'bigint') {
return value.toString();
Expand Down Expand Up @@ -36,15 +43,23 @@ export const bigintReplacerObject = (value: any): any => {

export const decodeProtoMessage = async (typeUrl: string, value: Uint8Array) => {

const akashRegistryTypes = getAkashTypeRegistry();

const agoricRegistryTypes: [string, GeneratedType][] = [
['/agoric.swingset.MsgWalletSpendAction', MsgWalletSpendAction as any],
['/agoric.swingset.MsgProvision', MsgProvision as any],
];

const protoRegistry: ReadonlyArray<[string, GeneratedType]> = [
const protoRegistry = [
...cosmosProtoRegistry,
...cosmwasmProtoRegistry,
...ibcProtoRegistry,
...osmosisProtoRegistry
...osmosisProtoRegistry,
...akashRegistryTypes,
...agoricRegistryTypes,
];

const registry = new Registry(protoRegistry);
const registry = new Registry(protoRegistry as Iterable<[string, GeneratedType]>);

// Get the proto type from the registry
let protoType = registry.lookupType(typeUrl);
Expand Down
Loading

0 comments on commit 32c3b72

Please sign in to comment.