Skip to content

Commit

Permalink
Merge dev repo
Browse files Browse the repository at this point in the history
  • Loading branch information
schnetzlerjoe committed Mar 15, 2024
1 parent 2b1fa77 commit c6c75da
Show file tree
Hide file tree
Showing 18 changed files with 5,092 additions and 1,201 deletions.
6 changes: 6 additions & 0 deletions packages/snap/babel.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
presets: [
['@babel/preset-env', { targets: { node: 'current' } }],
'@babel/preset-typescript',
],
};
6 changes: 6 additions & 0 deletions packages/snap/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
preset: '@metamask/snaps-jest',
transform: {
'^.+\\.(ts|tsx)$': 'ts-jest',
}
};
40 changes: 29 additions & 11 deletions packages/snap/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cosmsnap/snap",
"version": "0.1.17",
"version": "0.1.18",
"description": "The Cosmos extension for your Metamask wallet.",
"repository": {
"type": "git",
Expand All @@ -27,9 +27,13 @@
"serve": "mm-snap serve",
"start": "mm-snap watch",
"test": "npx ava tests/*",
"watch": "npx ava -w"
"test:e2e": "jest"
},
"ava": {
"timeout": "60s",
"files": [
"**/*-test.js"
],
"extensions": [
"ts"
],
Expand All @@ -38,40 +42,54 @@
]
},
"dependencies": {
"@agoric/cosmic-proto": "^0.3.0",
"@cosmjs/amino": "^0.31.1",
"@cosmjs/crypto": "^0.31.1",
"@cosmjs/encoding": "^0.31.1",
"@cosmjs/proto-signing": "^0.30.1",
"@cosmjs/stargate": "^0.31.1",
"@metamask/snaps-types": "^0.32.2",
"@metamask/snaps-ui": "^0.32.2",
"@cosmjs/stargate": "^0.30.1",
"@keplr-wallet/types": "^0.12.25",
"@metamask/key-tree": "^9.0.0",
"@metamask/snaps-sdk": "^3.1.1",
"@metamask/snaps-ui": "^3.1.0",
"buffer": "^6.0.3",
"cosmjs-types": "^0.8.0",
"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",
"@keplr-wallet/proto-types": "0.12.12",
"@lavamoat/allow-scripts": "^2.0.3",
"@metamask/auto-changelog": "^2.6.0",
"@metamask/eslint-config": "^10.0.0",
"@metamask/eslint-config-jest": "^10.0.0",
"@metamask/eslint-config-nodejs": "^10.0.0",
"@metamask/eslint-config-typescript": "^10.0.0",
"@metamask/snaps-cli": "^0.32.2",
"@metamask/auto-changelog": "^3.4.4",
"@metamask/eslint-config": "^12.2.0",
"@metamask/eslint-config-jest": "^12.1.0",
"@metamask/eslint-config-nodejs": "^12.1.0",
"@metamask/eslint-config-typescript": "^12.1.0",
"@metamask/snaps-cli": "^6.0.2",
"@metamask/snaps-jest": "^6.0.1",
"@types/jest": "^29.5.12",
"@types/node-fetch": "^2.6.4",
"@typescript-eslint/eslint-plugin": "^5.33.0",
"@typescript-eslint/parser": "^5.33.0",
"ava": "^5.3.0",
"babel-jest": "^29.7.0",
"eslint": "^8.21.0",
"eslint-config-prettier": "^8.1.0",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-jest": "^26.8.2",
"eslint-plugin-jsdoc": "^39.2.9",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-prettier": "^4.2.1",
"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-node": "^10.9.1",
"typescript": "^5.1.3"
},
Expand Down
24 changes: 0 additions & 24 deletions packages/snap/snap.config.js

This file was deleted.

14 changes: 14 additions & 0 deletions packages/snap/snap.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import type { SnapConfig } from '@metamask/snaps-cli';
import { resolve } from 'path';

const config: SnapConfig = {
input: resolve(__dirname, 'src/index.ts'),
server: {
port: 8028,
},
stats: {
buffer: false,
},
};

export default config;
4 changes: 2 additions & 2 deletions packages/snap/snap.manifest.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"version": "0.1.17",
"version": "0.1.18",
"description": "Cosmos Extension that adds Cosmos support to Metamask.",
"proposedName": "Cosmos Extension",
"repository": {
"type": "git",
"url": "https://github.com/cosmos/snap.git"
},
"source": {
"shasum": "O3k1mxMGVbP98FqhLnd57TJxwMWUsG+W0YwDNMrJpnY=",
"shasum": "70Y16tDS8kL5RMQhR1coQlID5bwKy0TeCmYgqwTIWlk=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
26 changes: 2 additions & 24 deletions packages/snap/src/address.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DirectSecp256k1Wallet } from "@cosmjs/proto-signing";
import { Chain } from "./types/chains";
import { getWallet } from "./wallet";

/**
* getAddress Gets the address for the chain specified.
Expand All @@ -9,29 +9,7 @@ import { Chain } from "./types/chains";
* @throws If an error occurs.
*/
export const getAddress = async (chain: Chain): Promise<string> => {
// get signer info
let node = await snap.request({
method: "snap_getBip44Entropy",
params: {
coinType: chain.slip44,
},
});

if (typeof node.privateKey === "undefined") {
throw Error("Private key from node is undefined");
}

// Create bytes key
let pk = node.privateKey;
if (pk.startsWith("0x")) {
pk = pk.substring(2);
}

// create the wallet
let wallet = await DirectSecp256k1Wallet.fromKey(
Uint8Array.from(Buffer.from(pk, "hex")),
chain.bech32_prefix
);
let wallet = await getWallet(chain)
let address = (await wallet.getAccounts())[0].address;

return address;
Expand Down
112 changes: 101 additions & 11 deletions packages/snap/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import { OnRpcRequestHandler } from "@metamask/snaps-types";
import { OnRpcRequestHandler } from "@metamask/snaps-sdk";
import { AccountData } from '@cosmjs/amino';
import { panel, text, heading, divider, copyable } from "@metamask/snaps-ui";
import { initializeChains } from "./initialize";
import { Chain, Chains, Fees, Msg } from "./types/chains";
import { Chain, Chains, Fees, Msg, UpdateChainParams } from "./types/chains";
import { Address } from "./types/address";
import { ChainState, AddressState } from "./state";
import { Result } from "./types/result";
import { sendTx, signAmino, signDirect, submitTransaction } from "./transaction";
import { COIN_TYPES, DEFAULT_FEES } from "./constants";
import { SignDoc, TxBody } from "cosmjs-types/cosmos/tx/v1beta1/tx";
import { StdSignDoc } from "@cosmjs/amino";
import { bigintReplacer, decodeProtoMessage } from "./parser";
import { bigintReplacer, decodeProtoMessage, decodeTxBodyIntoMessages } from "./parser";
import Long from "long";
import { Key } from '@keplr-wallet/types';
import { fromBech32 } from '@cosmjs/encoding';
import { isTxBodyEncodeObject } from "@cosmjs/proto-signing";

/**
* Handle incoming JSON-RPC requests, sent through `wallet_invokeSnap`.
Expand All @@ -25,7 +25,7 @@ import { fromBech32 } from '@cosmjs/encoding';
*/
export const onRpcRequest: OnRpcRequestHandler = async ({
request,
}): Promise<Result> => {
}) => {
let res: Object = {};
switch (request.method) {
case "initialized":
Expand Down Expand Up @@ -212,7 +212,7 @@ export const onRpcRequest: OnRpcRequestHandler = async ({
});

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

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

return {
data: txResponse,
data: JSON.stringify(txResponse),
success: false,
statusCode: 500,
};
Expand Down Expand Up @@ -328,27 +328,46 @@ 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;
}
let decMsg = await decodeProtoMessage(msg.typeUrl, msg.value);
msgs.push(decMsg);
}

// create all msg prompts
let ui = [
heading("Confirm Transaction"),
divider(),
heading("Chain"),
text(`${request.params.chain_id}`),
divider(),
heading("Transactions"),
divider(),
]

msgs.map(item => {
ui.push(heading(item.typeUrl)),
ui.push(text(JSON.stringify(bigintReplacer(item.value), null, 2))),
ui.push(divider())
ui.push(heading(item.typeUrl))
if (item.value == null) {
ui.push(text('Blind signing. ***Proceed with caution!***'))
} else {
ui.push(text(JSON.stringify(bigintReplacer(item.value), null, 2)))
}
});

if (txBody.memo) {
ui.push(divider())
ui.push(heading("Memo"))
ui.push(text(txBody.memo))
}

// Ensure user confirms transaction
let confirmationDirect = await snap.request({
method: "snap_dialog",
Expand Down Expand Up @@ -552,6 +571,77 @@ export const onRpcRequest: OnRpcRequestHandler = async ({
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,
statusCode: 201,
};
case "deleteChain":
// Delete a cosmos chain from the wallet state
if (
Expand Down
Loading

0 comments on commit c6c75da

Please sign in to comment.