Metamask 宇宙扩展提供库
Snapper是一个用于与Metamask的官方Cosmos扩展交互的实用库.
Global 提供者 (窗口. kepler 正在替换中)
在您的 dApp 中添加提供者为全局对象 .
import { CosmosSnap } from "@cosmsnap/snapper";
window.cosmos = new CosmosSnap();
const memo = "Hello from Metamask!";
let offlineSigner = await window.cosmos.getOfflineSigner("cosmoshub-4");
let wallet = await window.cosmos.getAccount("cosmoshub-4");
// Create a send token message
const msg = {
typeUrl: "/cosmos.bank.v1beta1.MsgSend",
value: {
fromAddress: wallet.address,
toAddress: "cosmos123456789",
amount: [
{
denom: "uatom",
amount: "10000",
},
],
},
};
// Create fee
const fee = {
amount: [],
gas: "200000",
};
const signingClient = await SigningStargateClient.connectWithSigner(
"https://rpc-cosmoshub.whispernode.com:443",
offlineSigner
);
const result = await signingClient.sign(wallet.address, [msg], fee, memo);
用于开发模式.
Note: 你必须在本地运行,才能正常运行.
window.cosmos.changeSnapId("local:http://localhost:8080");
提供者的结构.
export interface SnapProvider {
experimentalSuggestChain(chainInfo: ChainInfo): Promise<void>;
signAmino(
chainId: string,
signer: string,
signDoc: StdSignDoc,
): Promise<AminoSignResponse>;
signDirect(
chainId: string,
signer: string,
signDoc: {
/** SignDoc bodyBytes */
bodyBytes?: Uint8Array | null;
/** SignDoc authInfoBytes */
authInfoBytes?: Uint8Array | null;
/** SignDoc chainId */
chainId?: string | null;
/** SignDoc accountNumber */
accountNumber?: Long | null;
},
): Promise<DirectSignResponse>;
sendTx(
chainId: string,
tx: Uint8Array,
): Promise<DeliverTxResponse>;
getOfflineSigner(chainId: string): Promise<OfflineAminoSigner & OfflineDirectSigner>;
enabled(): Promise<boolean>;
install(): Promise<void>;
getChains(): Promise<Chain[]>;
deleteChain(chain_id: string): Promise<void>;
signAndBroadcast(chain_id: string, msgs: Msg[], fees: Fees): Promise<DeliverTxResponse>;
sign(chain_id: string, msgs: Msg[], fees: Fees): Promise<Int8Array>;
addAddressToBook(chain_id: string, address: string, name: string): Promise<void>;
getAddressBook(): Promise<Address[]>;
deleteAddressFromBook(address: string): Promise<void>;
getBech32Addresses(): Promise<CosmosAddress[]>;
getBech32Address(chain_id: string): Promise<CosmosAddress>;
getAccount(chain_id: string): Promise<AccountData>;
changeSnapId(snap_id: string): void;
}
直接交互函数
您可以直接与这些功能交互, 尽管我们强烈建议通过窗口使用提供商, 因为它是其他 Cosmos 钱包的倒置 .
选中已安装
import { isSnapInstalled } from '@cosmsnap/snapper';
let result: boolean = await isSnapInstalled();
检查是否初始化
import { isSnapInitialized } from '@cosmsnap/snapper';
let result: boolean = await isSnapInitialized();
安装宇宙扩展 .
import { installSnap } from '@cosmsnap/snapper';
await installSnap();
建议链.
使用这样的格式 这里 在链式登记册中。 资料越多越好.
import { suggestChain } from '@cosmsnap/snapper';
let chain: Chain = {};
await suggestChain(chain);
拿链子来.
得到所有的锁链 处于状态在扣子.
import { getChains } from '@cosmsnap/snapper';
let chains: []Chain = await getChains();
删除链条.
从钱包状态删除一个链条 .
import { deleteChain } from '@cosmsnap/snapper';
await deleteChain("cosmoshub-4");
签名广播( B) .
签名并广播交易。 注意,您可以使用我们的制作级基础设施(RPC)使用此功能广播交易.
import { signAndBroadcast } from '@cosmsnap/snapper';
import { DeliverTxResponse } from "@cosmjs/stargate";
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"
};
let txResult: DeliverTxResponse = await signAndBroadcast("cosmoshub-4", msgs, fees);
签署交易.
签个协议,然后退货 注意,这不使用我们的生产等级基础设施。 你必须用你自己的 广播交易.
import { sign } from '@cosmsnap/snapper';
import { DeliverTxResponse } from "@cosmjs/stargate";
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"
};
let txResult: DeliverTxResponse = await sign("cosmoshub-4", msgs, fees);
添加地址到地址簿 .
import { addAddressToBook } from '@cosmsnap/snapper';
await addAddressToBook("cosmoshub-4", "cosmos123456789", "John Doe");
获取地址簿
import { getAddressBook } from '@cosmsnap/snapper';
let book: Address[] = await getAddressBook();
从地址簿中删除地址
import { deleteAddressFromBook } from '@cosmsnap/snapper';
await deleteAddressFromBook("cosmos123456789");
获取全部 Bech32 地址
获取州内所有链条的 Bech32 地址 。 注意,这是一个昂贵的沉重行动 所以明智使用.
import { getBech32Addresses } from '@cosmsnap/snapper';
let allAddresses: CosmosAddress[] = await getBech32Addresses();
获取全部 Bech32 地址
获得链式Bech32地址.
import { getBech32Addresses } from '@cosmsnap/snapper';
let address: CosmosAddress = await getBech32Address("cosmoshub-4");