Skip to content

Commit

Permalink
Name updates and UI additions + optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
schnetzlerjoe committed Sep 6, 2023
1 parent bbc8087 commit f583a31
Show file tree
Hide file tree
Showing 36 changed files with 971 additions and 280 deletions.
51 changes: 46 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ await window.ethereum.request({
snapId: 'npm:@cosmsnap/snap',
request: {
method: 'addChain',
param: {
params: {
chain_info: JSON.stringify(chainInfo),
}
},
Expand Down Expand Up @@ -106,7 +106,7 @@ const chain = await window.ethereum.request({
snapId: 'npm:@cosmsnap/snap',
request: {
method: 'deleteChain',
param: {
params: {
chain_id: 'cosmoshub-4',
}
},
Expand Down Expand Up @@ -142,7 +142,48 @@ const address = await window.ethereum.request({
snapId: 'npm:@cosmsnap/snap',
request: {
method: 'transact',
param: {
params: {
chain_id: 'cosmoshub-4',
msgs: JSON.stringify(msgs),
// Optional: Uses default fees for chain if not specified
fees: JSON.stringify(fees)
}
},
},
});
```

## Sign Transaction
This will sign the transaction and return the transaction bytes.
NOTE: This does not broadcast the transaction.
```javascript
const msgs = [
{
typeUrl: "/cosmos.bank.v1beta1.MsgSend",
value: {
fromAddress: senderAddress,
toAddress: recipientAddress,
amount: [{
denom: "uatom",
amount: "500000"
}],
},
}
];
const fees = {
amount: [{
denom: "uatom",
amount: "500"
}],
gas: "200000"
};
const address = await window.ethereum.request({
method: 'wallet_invokeSnap',
params: {
snapId: 'npm:@cosmsnap/snap',
request: {
method: 'signTx',
params: {
chain_id: 'cosmoshub-4',
msgs: JSON.stringify(msgs),
// Optional: Uses default fees for chain if not specified
Expand All @@ -161,7 +202,7 @@ const address = await window.ethereum.request({
snapId: 'npm:@cosmsnap/snap',
request: {
method: 'addAddress',
param: {
params: {
chain_id: 'cosmoshub-4',
address: 'cosmos123456789',
name: 'John Cosmos'
Expand Down Expand Up @@ -208,7 +249,7 @@ const address = await window.ethereum.request({
snapId: 'npm:@cosmsnap/snap',
request: {
method: 'getChainAddress',
param: {
params: {
chain_id: 'cosmoshub-4',
}
},
Expand Down
52 changes: 47 additions & 5 deletions packages/snap/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ await window.ethereum.request({
snapId: 'npm:@cosmsnap/snap',
request: {
method: 'addChain',
param: {
params: {
chain_info: JSON.stringify(chainInfo),
}
},
Expand Down Expand Up @@ -92,7 +92,7 @@ const chain = await window.ethereum.request({
snapId: 'npm:@cosmsnap/snap',
request: {
method: 'deleteChain',
param: {
params: {
chain_id: 'cosmoshub-4',
}
},
Expand All @@ -101,6 +101,7 @@ const chain = await window.ethereum.request({
```

## Send Transaction
This will sign and broadcast the transaction.
```javascript
const msgs = [
{
Expand Down Expand Up @@ -128,7 +129,48 @@ const address = await window.ethereum.request({
snapId: 'npm:@cosmsnap/snap',
request: {
method: 'transact',
param: {
params: {
chain_id: 'cosmoshub-4',
msgs: JSON.stringify(msgs),
// Optional: Uses default fees for chain if not specified
fees: JSON.stringify(fees)
}
},
},
});
```

## Sign Transaction
This will sign the transaction and return the transaction bytes.
NOTE: This does not broadcast the transaction.
```javascript
const msgs = [
{
typeUrl: "/cosmos.bank.v1beta1.MsgSend",
value: {
fromAddress: senderAddress,
toAddress: recipientAddress,
amount: [{
denom: "uatom",
amount: "500000"
}],
},
}
];
const fees = {
amount: [{
denom: "uatom",
amount: "500"
}],
gas: "200000"
};
const address = await window.ethereum.request({
method: 'wallet_invokeSnap',
params: {
snapId: 'npm:@cosmsnap/snap',
request: {
method: 'signTx',
params: {
chain_id: 'cosmoshub-4',
msgs: JSON.stringify(msgs),
// Optional: Uses default fees for chain if not specified
Expand All @@ -147,7 +189,7 @@ const address = await window.ethereum.request({
snapId: 'npm:@cosmsnap/snap',
request: {
method: 'addAddress',
param: {
params: {
chain_id: 'cosmoshub-4',
address: 'cosmos123456789',
name: 'John Cosmos'
Expand Down Expand Up @@ -194,7 +236,7 @@ const address = await window.ethereum.request({
snapId: 'npm:@cosmsnap/snap',
request: {
method: 'getChainAddress',
param: {
params: {
chain_id: 'cosmoshub-4',
}
},
Expand Down
2 changes: 1 addition & 1 deletion packages/snap/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cosmsnap/snap",
"version": "0.1.6",
"version": "0.1.7",
"description": "The Cosmos extension for your Metamask wallet.",
"repository": {
"type": "git",
Expand Down
8 changes: 4 additions & 4 deletions packages/snap/snap.manifest.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"version": "0.1.6",
"description": "Cosmos Metamask Snap that adds Cosmos support to Metamask.",
"proposedName": "Cosmos Metamask Snap",
"version": "0.1.7",
"description": "Cosmos Extension that adds Cosmos support to Metamask.",
"proposedName": "Cosmos Extension",
"repository": {
"type": "git",
"url": "https://github.com/cosmos/snap.git"
},
"source": {
"shasum": "z5MiND1FM0FftB3U1mnaXtDh2sOwXOWQAH57NrUZ7i4=",
"shasum": "vTr7wXctqn/NZmTVkjwj2nJYkPM9GEYvrnRAswshDxY=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
4 changes: 4 additions & 0 deletions packages/snap/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,7 @@ export const U_MULTIPLIER = 1000000;

// This is the default gas in {denom} (note not in u{denom})
export const DEFAULT_AVG_GAS = 0.05;

export const COIN_TYPES = [
118, 564, 60, 459, 529, 330, 494, 639, 483, 4444, 701, 990, 394, 852, 7777777, 880, 931, 371, 370, 505, 234, 5555
]
122 changes: 117 additions & 5 deletions packages/snap/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { Chain, Chains, Fees } from "./types/chains";
import { Address } from "./types/address";
import { ChainState, AddressState } from "./state";
import { Result } from "./types/result";
import { submitTransaction } from "./transaction";
import { DEFAULT_FEES } from "./constants";
import { signTx, submitTransaction } from "./transaction";
import { COIN_TYPES, DEFAULT_FEES } from "./constants";

/**
* Handle incoming JSON-RPC requests, sent through `wallet_invokeSnap`.
Expand Down Expand Up @@ -61,6 +61,27 @@ export const onRpcRequest: OnRpcRequestHandler = async ({
if (!confirmation) {
throw new Error("Initialize Cosmos chain support was denied.");
}
// Make sure not initialized already
let checkInit = await snap.request({
method: "snap_manageState",
params: { operation: "get" },
});
if (checkInit != null && checkInit.initialized) {
await snap.request({
method: "snap_dialog",
params: {
type: "alert",
content: panel([
heading("Already Initialized"),
text(
"The Cosmos Snap has already been initialized."
),
]),
},
});
throw new Error("The Cosmos Snap has already been initialized.");
};

let chainList = await initializeChains();
let chains = new Chains(chainList);
// Initialize with initial state
Expand Down Expand Up @@ -136,8 +157,8 @@ export const onRpcRequest: OnRpcRequestHandler = async ({
divider(),
heading("Transaction"),
text(JSON.stringify(messages, null, 2)),
heading("Fees Amount"),
text(`${fees}`),
heading("Gas & Fees"),
text(`${JSON.stringify(fees)}`),
]),
},
});
Expand Down Expand Up @@ -199,6 +220,81 @@ export const onRpcRequest: OnRpcRequestHandler = async ({
statusCode: 500,
};
}
case "signTx":
// Sign a transaction with the wallet
if (
!(
request.params != null &&
typeof request.params == "object" &&
"msgs" in request.params &&
"chain_id" in request.params &&
typeof request.params.msgs == "string" &&
typeof request.params.chain_id == "string"
)
) {
throw new Error("Invalid transact request");
}

//Calculate fees for transaction
let feesTx: Fees = DEFAULT_FEES;

if (request.params.fees) {
if (typeof request.params.fees == "string") {
feesTx = JSON.parse(request.params.fees);
}
}

//Get messages if any from JSON string
let messagesTx;

if (request.params.msgs) {
if (typeof request.params.msgs == "string") {
messagesTx = JSON.parse(request.params.msgs);
}
}

// Ensure user confirms transaction
confirmation = await snap.request({
method: "snap_dialog",
params: {
type: "confirmation",
content: panel([
heading("Confirm Transaction"),
divider(),
heading("Chain"),
text(`${request.params.chain_id}`),
divider(),
heading("Transaction"),
text(JSON.stringify(messagesTx, null, 2)),
heading("Gas & Fees"),
text(`${JSON.stringify(feesTx)}`),
]),
},
});

if (!confirmation) {
throw new Error("Transaction was denied.");
}

let resultTx = await signTx(
request.params.chain_id,
messagesTx,
feesTx
);

if (typeof resultTx === "undefined") {
return {
data: {},
success: false,
statusCode: 500,
};
}

return {
data: resultTx,
success: true,
statusCode: 201,
};
case "addChain":
if (
!(
Expand Down Expand Up @@ -244,7 +340,6 @@ export const onRpcRequest: OnRpcRequestHandler = async ({

// Ensure chain id doesn't already exist
let get_chain = await ChainState.getChain(new_chain.chain_id);

if (get_chain != null) {
await snap.request({
method: "snap_dialog",
Expand All @@ -261,6 +356,23 @@ export const onRpcRequest: OnRpcRequestHandler = async ({
);
}

// Ensure the coin type is supported (NOTE: 60 is blocked by Metamask)
if (!COIN_TYPES.includes(new_chain.slip44)) {
await snap.request({
method: "snap_dialog",
params: {
type: "alert",
content: panel([
heading("Error Occured"),
text(`Coin type ${new_chain.slip44} is not supported.`),
]),
},
});
throw new Error(
`Coin type ${new_chain.slip44} is not supported.`
);
}

let new_chains = await ChainState.addChain(new_chain);

await snap.request({
Expand Down
Loading

0 comments on commit f583a31

Please sign in to comment.