@cosmsnap/snap

0.1.22 • Public • Published

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'
        },
    },
});

Readme

Keywords

none

Package Sidebar

Install

npm i @cosmsnap/snap

Weekly Downloads

2,838

Version

0.1.22

License

MIT

Unpacked Size

6.81 MB

Total Files

6

Last publish

Collaborators

  • mysticlabs