0.1.20 • Published 2 months ago

@cosmsnap/snap v0.1.20

Weekly downloads
-
License
MIT
Repository
github
Last release
2 months ago

Cosmos MetaMask Snap

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.

Contribution

Your contributions are always welcome! Please have a look at the contribution guidelines first.

Install & Initialize

// Check if the Snap is installed
let result = await window.ethereum.request({ method: 'wallet_getSnaps' });
const installed = Object.keys(result).includes("npm:@cosmsnap/snap");

// Install Snap
if (!installed) {
    const result = await window.ethereum.request({
        method: 'wallet_requestSnaps',
        params: {
            'npm:@cosmsnap/snap': {
            version: '^0.1.0',
            },
        },
    });
}

// Initialize the Snap with default chains
await ethereum.request({
    method: 'wallet_invokeSnap',
    params: {
        snapId: 'npm:@cosmsnap/snap',
        request: {
            method: 'initialize',
        },
    },
});

Check If Initialized

// Boolean is returned
const initialized = await ethereum.request({
    method: 'wallet_invokeSnap',
    params: {
        snapId: 'npm:@cosmsnap/snap',
        request: {
            method: 'initialized',
        },
    },
});

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 and we will gladly add it.

Chain info is structured like in the chain registry (i.e Agoric)

// chainInfo should be structured like this https://github.com/cosmos/chain-registry/tree/master/agoric
await window.ethereum.request({
    method: 'wallet_invokeSnap',
    params: {
        snapId: 'npm:@cosmsnap/snap',
        request: {
            method: 'addChain',
            params: {
                chain_info: JSON.stringify(chainInfo),
            }
        },
    },
});

Get Chains

const chains = await window.ethereum.request({
    method: 'wallet_invokeSnap',
    params: {
        snapId: 'npm:@cosmsnap/snap',
        request: {
            method: 'getChains'
        },
    },
});

Delete Chain

const chain = await window.ethereum.request({
    method: 'wallet_invokeSnap',
    params: {
        snapId: 'npm:@cosmsnap/snap',
        request: {
            method: 'deleteChain',
            params: {
                chain_id: 'cosmoshub-4',
            }
        },
    },
});

Send Transaction

This will sign and broadcast the transaction.

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: 'transact',
            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.

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
                fees: JSON.stringify(fees)
            }
        },
    },
});

Add Address (Address Book)

const address = await window.ethereum.request({
    method: 'wallet_invokeSnap',
    params: {
        snapId: 'npm:@cosmsnap/snap',
        request: {
            method: 'addAddress',
            params: {
                chain_id: 'cosmoshub-4',
                address: 'cosmos123456789',
                name: 'John Cosmos'
            }
        },
    },
});

Get Addresses (Address Book)

const address = await window.ethereum.request({
    method: 'wallet_invokeSnap',
    params: {
        snapId: 'npm:@cosmsnap/snap',
        request: {
            method: 'getAddresses'
        },
    },
});

Delete Address (Address Book)

const address = await window.ethereum.request({
    method: 'wallet_invokeSnap',
    params: {
        snapId: 'npm:@cosmsnap/snap',
        request: {
            method: 'deleteAddress',
            params: {
                address: 'cosmos123456789'
            }
        },
    },
});

Get Bech32 Address

const address = await window.ethereum.request({
    method: 'wallet_invokeSnap',
    params: {
        snapId: 'npm:@cosmsnap/snap',
        request: {
            method: 'getChainAddress',
            params: {
                chain_id: 'cosmoshub-4',
            }
        },
    },
});

Get Bech32 Addresses

const address = await window.ethereum.request({
    method: 'wallet_invokeSnap',
    params: {
        snapId: 'npm:@cosmsnap/snap',
        request: {
            method: 'getChainAddresses'
        },
    },
});
0.1.20

2 months ago

0.1.18

2 months ago

0.1.17

8 months ago

0.1.16

8 months ago

0.1.15

8 months ago

0.1.14

8 months ago

0.1.13

8 months ago

0.1.12

8 months ago

0.1.11

8 months ago

0.1.10

8 months ago

0.1.9

8 months ago

0.1.8

8 months ago

0.1.7

8 months ago

0.1.6

9 months ago

0.1.5

9 months ago

0.1.4

9 months ago

0.1.3

9 months ago

0.1.1

9 months ago

0.1.0

9 months ago