Skip to content

Commit

Permalink
Merge branch 'main' into merge-main
Browse files Browse the repository at this point in the history
  • Loading branch information
schnetzlerjoe committed May 29, 2024
2 parents 318d518 + 0fe0c60 commit 4cc8885
Show file tree
Hide file tree
Showing 9 changed files with 2,980 additions and 110 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Cosmos Metamask Snap aims to add full support of Metamask, a highly popular Ethereum wallet, to all Cosmos SDK blockchains, potentially opening the door to over 30 million Ethereum users and stimulating growth for every project in the Cosmos ecosystem.

## Developer Preview Software
Please note, to develop this Metamask Snap you need to use Metamask Flask, a canary distribution for developers that provides access to upcoming features wihtin Metamask.
Please note, to develop this Metamask Snap you need to use Metamask Flask, a canary distribution for developers that provides access to upcoming features within Metamask.

## Contribution
Your contributions are always welcome! Please have a look at the [contribution guidelines](CONTRIBUTING.md) first.
Expand Down Expand Up @@ -66,7 +66,7 @@ const initialized = await ethereum.request({
```

## Suggest Chain
This Snap has default support for coin types `118, 564, 60, 459, 529, 330, 494, 639, 483, 4444, 701, 990, 394, 852, 7777777, 880, 931, 371, 370, 505, 234, 5555`. All the coin types within the chain registry. If you need any other please add an issue to the repo [here](https://github.com/cosmos/snap/issues) and we will gladly add it.
This Snap has default support for coin types `118, 564, 459, 529, 330, 494, 639, 483, 4444, 701, 990, 394, 852, 7777777, 880, 931, 371, 370, 505, 234, 5555`. All the coin types within the chain registry except for coin type 60 (Ethermint, which is already supported in standard MetaMask). If you need any other please add an issue to the repo [here](https://github.com/cosmos/snap/issues) and we will gladly add it.

Chain info is structured like in the chain registry (i.e [Agoric](https://github.com/cosmos/chain-registry/tree/master/agoric))
```javascript
Expand Down Expand Up @@ -268,4 +268,4 @@ const address = await window.ethereum.request({
},
},
});
```
```
17 changes: 17 additions & 0 deletions packages/snap/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"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 @@ -28,8 +29,13 @@
"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 @@ -42,6 +48,10 @@
]
},
"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",
"@cosmjs/amino": "^0.31.1",
"@cosmjs/crypto": "^0.31.1",
Expand All @@ -54,10 +64,14 @@
"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 @@ -77,6 +91,7 @@
"@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 @@ -85,12 +100,14 @@
"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
1 change: 1 addition & 0 deletions packages/snap/snap.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"repository": {
"type": "git",
"url": "git+https://github.com/cosmos/snap.git"
"url": "git+https://github.com/cosmos/snap.git"
},
"source": {
"shasum": "iy7sFNnki+rvhkmOaWGfKE5ZiaEqOYkuE1AVb5dEiN0=",
Expand Down
76 changes: 76 additions & 0 deletions packages/snap/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ 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";
Expand All @@ -28,6 +29,7 @@ import { snapNotify } from "./notification";
*/
export const onRpcRequest: OnRpcRequestHandler = async ({
request,
}) => {
}) => {
let res: Object = {};
switch (request.method) {
Expand Down Expand Up @@ -215,6 +217,7 @@ export const onRpcRequest: OnRpcRequestHandler = async ({
});

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

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

return {
data: JSON.stringify(txResponse),
data: JSON.stringify(txResponse),
success: false,
statusCode: 500,
Expand Down Expand Up @@ -666,6 +671,77 @@ 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
2 changes: 1 addition & 1 deletion packages/snapper/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cosmsnap/snapper",
"version": "0.1.26",
"version": "0.1.29",
"description": "A helper package with utilities to interact with the Cosmos Extension for MetaMask.",
"repository": {
"type": "git",
Expand Down
36 changes: 35 additions & 1 deletion packages/snapper/src/provider.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AccountData, ChainInfo, Key, OfflineAminoSigner, OfflineDirectSigner } from '@keplr-wallet/types';
import { AccountData, ChainInfo, Key, OfflineAminoSigner, OfflineDirectSigner, StdSignature } from '@keplr-wallet/types';
import { DirectSignResponse } from "@cosmjs/proto-signing";
import { AminoSignResponse, StdSignDoc } from "@cosmjs/amino";
import { Long } from 'long';
Expand Down Expand Up @@ -39,6 +39,11 @@ export interface SnapProvider {
accountNumber?: Long | null;
},
): Promise<DirectSignResponse>;
signArbitrary(
chainId: string,
signer: string,
data: string | Uint8Array
): Promise<StdSignature>;
sendTx(
chainId: string,
tx: Uint8Array,
Expand Down Expand Up @@ -165,6 +170,30 @@ export class CosmosSnap implements SnapProvider {
let res = await signDirect(chainId, signer, signDoc, this.snap_id);
return res;
}
async signArbitrary(chainId: string, signer: string, data: string | Uint8Array): Promise<StdSignature> {
let dataBase64: string = typeof data != "string" ? Buffer.from(signer).toString('base64') : data;
let signDoc: StdSignDoc = {
chain_id: '',
account_number: '0',
sequence: '0',
fee: {
"gas": "0",
amount: []
},
msgs: [
{
type: 'sign/MsgSignData',
value: {
signer,
data: dataBase64
}
}
],
memo: ''
}
let res = await signAmino(chainId, signer, signDoc);
return res.signature
}
async sendTx(chainId: string, tx: Uint8Array): Promise<DeliverTxResponse> {
let res = await sendTx(chainId, tx, this.snap_id);
return res
Expand All @@ -180,4 +209,9 @@ export class CosmosSnap implements SnapProvider {
return signer
}
}
getOfflineSignerOnlyAmino(chainId): OfflineAminoSigner {
let signer = new CosmJSOfflineSigner(chainId, this.snap_id);
signer.signDirect = undefined;
return signer
}
}
14 changes: 9 additions & 5 deletions packages/ui/src/components/Balance.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@
<p class="price inter-medium-white-12px">
${_.round(dollarAmount, 2)}
</p>
<p class="percent inter-medium-white-14px w-[100%]">
{_.round(tokenAmount, 2) + " " + tokenDenom}
<p class="percent inter-medium-white-14px w-full ellipsis-wrap">
{new Intl.NumberFormat('en-US').format(_.round(tokenAmount, 2)) + " " + tokenDenom}
</p>
</div>
</div>
Expand All @@ -73,6 +73,13 @@
</div>

<style>
.ellipsis-wrap {
white-space: nowrap; /* Prevents the text from wrapping */
overflow: hidden; /* Hides overflowed content */
text-overflow: ellipsis; /* Adds ellipsis to overflowed content */
width: 100%; /* Ensures the container's width is used */
max-width: 250px; /* Set a max-width to trigger ellipsis */
}
.group-55 {
align-items: flex-start;
backdrop-filter: blur(15px) brightness(100%);
Expand All @@ -83,10 +90,8 @@
display: flex;
flex-direction: column;
gap: 17px;
left: 264px;
min-height: 110px;
padding: 20px 17px;
top: 405px;
width: 100%;
}
Expand Down Expand Up @@ -177,7 +182,6 @@
letter-spacing: -0.28px;
line-height: normal;
min-height: 17px;
min-width: 46px;
opacity: 0.45;
text-align: right;
overflow: hidden;
Expand Down
3 changes: 2 additions & 1 deletion packages/ui/src/utils/snap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,8 @@ export const chainInfoToChain = (chainInfo: ChainInfo): Chain => {
provider: chainInfo.nodeProvider?.name
}]
},
address: undefined
address: undefined,
type: "cosmos"
}
}

Expand Down
Loading

0 comments on commit 4cc8885

Please sign in to comment.