@mictonode/widget 0.3.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/FUNDING.yml +3 -0
- package/.github/workflows/npm-publish.yml +34 -0
- package/.prettierrc.json +9 -0
- package/.vscode/extensions.json +3 -0
- package/README.md +41 -0
- package/dist/main-172a6585.js +404361 -0
- package/dist/ping-widget.js +13 -0
- package/dist/ping-widget.umd.cjs +96 -0
- package/dist/query.lcd-2adf1c0c.js +129 -0
- package/dist/query.rpc.Query-b53908e7.js +2316 -0
- package/dist/tx.rpc.msg-d234ff40.js +37 -0
- package/dist/tx.rpc.msg-f7a80a78.js +21 -0
- package/example/.vscode/extensions.json +3 -0
- package/example/README.md +7 -0
- package/example/index.html +12 -0
- package/example/package.json +19 -0
- package/example/pnpm-lock.yaml +465 -0
- package/example/public/cdn.html +19 -0
- package/example/src/App.vue +73 -0
- package/example/src/main.js +4 -0
- package/example/vite.config.js +7 -0
- package/index.html +12 -0
- package/lib/components/ConnectWallet/index.vue +267 -0
- package/lib/components/TokenConvert/index.vue +1033 -0
- package/lib/components/TokenConvert/tokens.ts +37 -0
- package/lib/components/TxDialog/index.vue +432 -0
- package/lib/components/TxDialog/messages/Delegate.vue +194 -0
- package/lib/components/TxDialog/messages/Deposit.vue +97 -0
- package/lib/components/TxDialog/messages/MsgDelegate.ts +0 -0
- package/lib/components/TxDialog/messages/Redelegate.vue +189 -0
- package/lib/components/TxDialog/messages/Send.vue +142 -0
- package/lib/components/TxDialog/messages/Transfer.vue +248 -0
- package/lib/components/TxDialog/messages/Unbond.vue +137 -0
- package/lib/components/TxDialog/messages/Vote.vue +63 -0
- package/lib/components/TxDialog/messages/Withdraw.vue +56 -0
- package/lib/components/TxDialog/messages/WithdrawCommission.vue +75 -0
- package/lib/components/TxDialog/wasm/ClearAdmin.vue +56 -0
- package/lib/components/TxDialog/wasm/ExecuteContract.vue +99 -0
- package/lib/components/TxDialog/wasm/InstantiateContract.vue +109 -0
- package/lib/components/TxDialog/wasm/MigrateContract.vue +77 -0
- package/lib/components/TxDialog/wasm/MigrateContract2.vue +77 -0
- package/lib/components/TxDialog/wasm/StoreCode.vue +101 -0
- package/lib/components/TxDialog/wasm/UpdateAdmin.vue +75 -0
- package/lib/main.css +7 -0
- package/lib/main.ts +23 -0
- package/lib/utils/TokenUnitConverter.ts +45 -0
- package/lib/utils/format.ts +16 -0
- package/lib/utils/http.ts +223 -0
- package/lib/utils/type.ts +77 -0
- package/lib/wallet/EthermintMessageAdapter.ts +136 -0
- package/lib/wallet/UniClient.ts +152 -0
- package/lib/wallet/Wallet.ts +138 -0
- package/lib/wallet/wallets/InitiaWallet.ts +189 -0
- package/lib/wallet/wallets/KeplerWallet.ts +158 -0
- package/lib/wallet/wallets/LeapWallet.ts +142 -0
- package/lib/wallet/wallets/LedgerWallet.ts +203 -0
- package/lib/wallet/wallets/MetamaskSnapWallet.ts +105 -0
- package/lib/wallet/wallets/MetamaskWallet.ts +173 -0
- package/lib/wallet/wallets/OKXWallet.ts +202 -0
- package/lib/wallet/wallets/UnisatWallet.ts +198 -0
- package/package.json +102 -0
- package/postcss.config.js +6 -0
- package/src/App.vue +241 -0
- package/src/main.ts +4 -0
- package/src/vite-env.d.ts +1 -0
- package/tailwind.config.js +34 -0
- package/tsconfig.json +25 -0
- package/tsconfig.node.json +10 -0
- package/vite.config.ts +62 -0
- package/vue-shim.d.ts +6 -0
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import { EncodeObject } from "@cosmjs/proto-signing"
|
|
2
|
+
import {
|
|
3
|
+
createMsgWithdrawDelegatorReward,
|
|
4
|
+
createIBCMsgTransfer,
|
|
5
|
+
createMsgSend,
|
|
6
|
+
createMsgBeginRedelegate,
|
|
7
|
+
createMsgDelegate,
|
|
8
|
+
createMsgUndelegate,
|
|
9
|
+
createMsgVote,
|
|
10
|
+
// createMsgWithdrawValidatorCommission,
|
|
11
|
+
} from '@tharsis/proto'
|
|
12
|
+
import {
|
|
13
|
+
MSG_WITHDRAW_DELEGATOR_REWARD_TYPES,
|
|
14
|
+
MSG_BEGIN_REDELEGATE_TYPES,
|
|
15
|
+
MSG_DELEGATE_TYPES,
|
|
16
|
+
MSG_SEND_TYPES,
|
|
17
|
+
MSG_UNDELEGATE_TYPES,
|
|
18
|
+
MSG_VOTE_TYPES,
|
|
19
|
+
IBC_MSG_TRANSFER_TYPES,
|
|
20
|
+
MSG_WITHDRAW_VALIDATOR_COMMISSION_TYPES,
|
|
21
|
+
} from "@tharsis/eip712"
|
|
22
|
+
|
|
23
|
+
export interface MessageAdapter {
|
|
24
|
+
toProto(message: EncodeObject): object
|
|
25
|
+
getTypes: ()=> object
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export class WithdrawMessageAdapter implements MessageAdapter {
|
|
29
|
+
toProto(message: EncodeObject) {
|
|
30
|
+
const param = message.value
|
|
31
|
+
return createMsgWithdrawDelegatorReward(param.delegatorAddress, param.validatorAddress)
|
|
32
|
+
}
|
|
33
|
+
getTypes() {
|
|
34
|
+
return MSG_WITHDRAW_DELEGATOR_REWARD_TYPES
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export class WithdrawCommissionMessageAdapter implements MessageAdapter {
|
|
39
|
+
toProto(message: EncodeObject) {
|
|
40
|
+
const param = message.value
|
|
41
|
+
return createMsgWithdrawDelegatorReward(param.delegatorAddress, param.validatorAddress)
|
|
42
|
+
}
|
|
43
|
+
getTypes() {
|
|
44
|
+
return MSG_WITHDRAW_VALIDATOR_COMMISSION_TYPES
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export class BeginRedelegateMessageAdapter implements MessageAdapter {
|
|
49
|
+
toProto(message: EncodeObject) {
|
|
50
|
+
const param = message.value
|
|
51
|
+
return createMsgBeginRedelegate(
|
|
52
|
+
param.delegatorAddress,
|
|
53
|
+
param.validatorSrcAddress,
|
|
54
|
+
param.validatorDstAddress,
|
|
55
|
+
param.amount.amount,
|
|
56
|
+
param.amount.denom)
|
|
57
|
+
}
|
|
58
|
+
getTypes() {
|
|
59
|
+
return MSG_BEGIN_REDELEGATE_TYPES
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export class DelegateMessageAdapter implements MessageAdapter {
|
|
64
|
+
toProto(message: EncodeObject) {
|
|
65
|
+
const param = message.value
|
|
66
|
+
const amount = Array.isArray(param.amount) ? param.amount[0] : param.amount
|
|
67
|
+
return createMsgDelegate(param.delegatorAddress, param.validatorAddress, amount.amount, amount.denom)
|
|
68
|
+
}
|
|
69
|
+
getTypes() {
|
|
70
|
+
return MSG_DELEGATE_TYPES
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export class SendMessageAdapter implements MessageAdapter {
|
|
75
|
+
toProto(message: EncodeObject) {
|
|
76
|
+
const param = message.value
|
|
77
|
+
const amount = Array.isArray(param.amount) ? param.amount[0] : param.amount
|
|
78
|
+
return createMsgSend(param.fromAddress, param.toAddress, amount.amount, amount.denom)
|
|
79
|
+
}
|
|
80
|
+
getTypes() {
|
|
81
|
+
return MSG_SEND_TYPES
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export class UndelegateMessageAdapter implements MessageAdapter {
|
|
86
|
+
toProto(message: EncodeObject) {
|
|
87
|
+
const param = message.value
|
|
88
|
+
const amount = Array.isArray(param.amount) ? param.amount[0] : param.amount
|
|
89
|
+
return createMsgUndelegate(param.delegatorAddress, param.validatorAddress, amount.amount, amount.denom)
|
|
90
|
+
}
|
|
91
|
+
getTypes() {
|
|
92
|
+
return MSG_UNDELEGATE_TYPES
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export class VoteMessageAdapter implements MessageAdapter {
|
|
97
|
+
toProto(message: EncodeObject) {
|
|
98
|
+
const param = message.value
|
|
99
|
+
return createMsgVote(param.proposalId, param.option, param.voter)
|
|
100
|
+
}
|
|
101
|
+
getTypes() {
|
|
102
|
+
return MSG_VOTE_TYPES
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export class IBCMessageAdapter implements MessageAdapter {
|
|
107
|
+
toProto(message: EncodeObject) {
|
|
108
|
+
const param = message.value
|
|
109
|
+
return createIBCMsgTransfer(
|
|
110
|
+
param.sourcePort,
|
|
111
|
+
param.sourceChannel,
|
|
112
|
+
param.amount.amount,
|
|
113
|
+
param.amount.denom,
|
|
114
|
+
param.sender,
|
|
115
|
+
param.receiver,
|
|
116
|
+
param.revisionNumber,
|
|
117
|
+
param.revisionHeight,
|
|
118
|
+
param.timeoutTimestamp
|
|
119
|
+
)
|
|
120
|
+
}
|
|
121
|
+
getTypes() {
|
|
122
|
+
return IBC_MSG_TRANSFER_TYPES
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
export const defaultMessageAdapter: Record<string, MessageAdapter> = {
|
|
128
|
+
"/cosmos.distribution.v1beta1.MsgWithdrawDelegatorReward": new WithdrawMessageAdapter(),
|
|
129
|
+
"/cosmos.staking.v1beta1.MsgDelegate": new DelegateMessageAdapter(),
|
|
130
|
+
"/cosmos.staking.v1beta1.MsgBeginRedelegate": new BeginRedelegateMessageAdapter(),
|
|
131
|
+
"/cosmos.staking.v1beta1.MsgUndelegate": new UndelegateMessageAdapter(),
|
|
132
|
+
"/cosmos.bank.v1beta1.MsgSend": new SendMessageAdapter(),
|
|
133
|
+
"/cosmos.gov.v1beta1.MsgVote": new VoteMessageAdapter(),
|
|
134
|
+
"/ibc.applications.transfer.v1.MsgTransfer": new IBCMessageAdapter(),
|
|
135
|
+
"/cosmos.distribution.v1beta1.MsgWithdrawValidatorCommission": new WithdrawCommissionMessageAdapter()
|
|
136
|
+
}
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import { toBase64, fromBase64, toHex, fromBech32 } from "@cosmjs/encoding";
|
|
2
|
+
import { EncodeObject, encodePubkey, Registry } from '@cosmjs/proto-signing'
|
|
3
|
+
import { encodeSecp256k1Pubkey } from "@cosmjs/amino";
|
|
4
|
+
import { defaultRegistryTypes } from "@cosmjs/stargate";
|
|
5
|
+
import { AuthInfo, TxRaw } from "cosmjs-types/cosmos/tx/v1beta1/tx";
|
|
6
|
+
import { SignMode } from "cosmjs-types/cosmos/tx/signing/v1beta1/signing";
|
|
7
|
+
import { AbstractWallet, WalletArgument, WalletName, createWallet } from "./Wallet";
|
|
8
|
+
import { post } from "../utils/http";
|
|
9
|
+
import { BroadcastMode, Transaction, TxResponse } from "../utils/type";
|
|
10
|
+
import { wasmTypes } from "@cosmjs/cosmwasm-stargate/build/modules";
|
|
11
|
+
import { makeAuthInfoBytes, makeSignDoc, TxBodyEncodeObject } from "@cosmjs/proto-signing/build";
|
|
12
|
+
import { Any } from "cosmjs-types/google/protobuf/any";
|
|
13
|
+
import { TimeoutError, createAminoTypes, createRegistry, defined, getRPC } from "@initia/utils"
|
|
14
|
+
export function isEthermint(chainId: string) {
|
|
15
|
+
return chainId.search(/\w+_\d+-\d+/g) > -1
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
export class UniClient {
|
|
20
|
+
registry: Registry
|
|
21
|
+
wallet: AbstractWallet
|
|
22
|
+
aminoTypes: any;
|
|
23
|
+
createAminoConverters = createAminoTypes()
|
|
24
|
+
constructor(name: WalletName, arg: WalletArgument) {
|
|
25
|
+
this.registry = createRegistry();
|
|
26
|
+
//this.registry = new Registry([...defaultRegistryTypes, ...wasmTypes])
|
|
27
|
+
this.wallet = createWallet(name, arg, this.registry)
|
|
28
|
+
this.aminoTypes = createAminoTypes();
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
async getAccounts() {
|
|
32
|
+
return this.wallet.getAccounts()
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
async sign(transaction: Transaction): Promise<TxRaw | TxResponse> {
|
|
36
|
+
// const { signature, signed } = await this.wallet.sign(transaction);
|
|
37
|
+
// return TxRaw.fromPartial({
|
|
38
|
+
// bodyBytes: signed.bodyBytes,
|
|
39
|
+
// authInfoBytes: signed.authInfoBytes,
|
|
40
|
+
// signatures: [fromBase64(signature.signature)],
|
|
41
|
+
// });
|
|
42
|
+
return this.wallet.sign(transaction)
|
|
43
|
+
}
|
|
44
|
+
/** */
|
|
45
|
+
async simulate (
|
|
46
|
+
endpoint: string,
|
|
47
|
+
transaction: Transaction,
|
|
48
|
+
mode: BroadcastMode = BroadcastMode.SYNC,
|
|
49
|
+
|
|
50
|
+
) {
|
|
51
|
+
|
|
52
|
+
const pubkey = Any.fromPartial({
|
|
53
|
+
typeUrl: '/cosmos.crypto.ed25519.PubKey',
|
|
54
|
+
value: new Uint8Array()
|
|
55
|
+
})
|
|
56
|
+
const txBodyEncodeObject: TxBodyEncodeObject = {
|
|
57
|
+
typeUrl: "/cosmos.tx.v1beta1.TxBody",
|
|
58
|
+
value: {
|
|
59
|
+
messages: transaction.messages,
|
|
60
|
+
memo: transaction.memo,
|
|
61
|
+
},
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
const txBodyBytes = this.registry.encode(txBodyEncodeObject);
|
|
65
|
+
const gasLimit = Number(transaction.fee.gas);
|
|
66
|
+
const authInfoBytes = makeAuthInfoBytes(
|
|
67
|
+
[{ pubkey, sequence: transaction.signerData.sequence }],
|
|
68
|
+
transaction.fee.amount,
|
|
69
|
+
gasLimit,
|
|
70
|
+
transaction.fee.granter,
|
|
71
|
+
transaction.fee.payer,
|
|
72
|
+
);
|
|
73
|
+
|
|
74
|
+
const txRaw = TxRaw.fromPartial({
|
|
75
|
+
bodyBytes: txBodyBytes,
|
|
76
|
+
authInfoBytes: authInfoBytes,
|
|
77
|
+
signatures: [new Uint8Array()],
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
const txbytes = toBase64(TxRaw.encode(txRaw).finish())
|
|
81
|
+
const request = {
|
|
82
|
+
tx_bytes: txbytes,
|
|
83
|
+
mode, // BROADCAST_MODE_SYNC, BROADCAST_MODE_BLOCK, BROADCAST_MODE_ASYNC
|
|
84
|
+
}
|
|
85
|
+
return post(`${endpoint}/cosmos/tx/v1beta1/simulate`, request).then(res => {
|
|
86
|
+
if (res.code && res.code !== 0) {
|
|
87
|
+
throw new Error(res.message)
|
|
88
|
+
}
|
|
89
|
+
if (res.tx_response && res.tx_response.code !== 0) {
|
|
90
|
+
throw new Error(res.tx_response.raw_log)
|
|
91
|
+
}
|
|
92
|
+
return Number(res.gas_info.gas_used)
|
|
93
|
+
})
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** async simulate2(
|
|
97
|
+
endpoint: string,
|
|
98
|
+
messages: readonly EncodeObject[],
|
|
99
|
+
memo: string | undefined,
|
|
100
|
+
sequence: number
|
|
101
|
+
) {
|
|
102
|
+
|
|
103
|
+
const [first] = await this.wallet.getAccounts()
|
|
104
|
+
const pubkey = encodeSecp256k1Pubkey(first.pubkey);
|
|
105
|
+
const anyMsgs = messages.map((m) => this.registry.encodeAsAny(m));
|
|
106
|
+
const url = `${endpoint}/cosmos/tx/v1beta1/simulate`
|
|
107
|
+
const tx = Tx.fromPartial({
|
|
108
|
+
authInfo: AuthInfo.fromPartial({
|
|
109
|
+
fee: Fee.fromPartial({}),
|
|
110
|
+
signerInfos: [
|
|
111
|
+
{
|
|
112
|
+
publicKey: encodePubkey(pubkey),
|
|
113
|
+
sequence: sequence,
|
|
114
|
+
modeInfo: { single: { mode: SignMode.SIGN_MODE_UNSPECIFIED } },
|
|
115
|
+
},
|
|
116
|
+
],
|
|
117
|
+
}),
|
|
118
|
+
body: TxBody.fromPartial({
|
|
119
|
+
messages: Array.from(anyMsgs),
|
|
120
|
+
memo: memo,
|
|
121
|
+
}),
|
|
122
|
+
signatures: [new Uint8Array()],
|
|
123
|
+
});
|
|
124
|
+
const request = SimulateRequest.fromPartial({
|
|
125
|
+
tx
|
|
126
|
+
// txBytes: Tx.encode(tx).finish(),
|
|
127
|
+
});
|
|
128
|
+
console.log(tx, request)
|
|
129
|
+
return await post(url, request)
|
|
130
|
+
}
|
|
131
|
+
*/
|
|
132
|
+
|
|
133
|
+
async broadcastTx(endpoint: string, bodyBytes: TxRaw, mode: BroadcastMode = BroadcastMode.SYNC) : Promise<{tx_response: TxResponse}> {
|
|
134
|
+
// const txbytes = bodyBytes.authInfoBytes ? TxRaw.encode(bodyBytes).finish() : bodyBytes
|
|
135
|
+
const txbytes = TxRaw.encode(bodyBytes).finish()
|
|
136
|
+
const txString = toBase64(txbytes)
|
|
137
|
+
const txRaw = {
|
|
138
|
+
tx_bytes: txString,
|
|
139
|
+
mode, // BROADCAST_MODE_SYNC, BROADCAST_MODE_BLOCK, BROADCAST_MODE_ASYNC
|
|
140
|
+
}
|
|
141
|
+
return post(`${endpoint}/cosmos/tx/v1beta1/txs`, txRaw).then(res => {
|
|
142
|
+
if (res.code && res.code !== 0) {
|
|
143
|
+
throw new Error(res.message)
|
|
144
|
+
}
|
|
145
|
+
if (res.tx_response && res.tx_response.code !== 0) {
|
|
146
|
+
throw new Error(res.tx_response.raw_log)
|
|
147
|
+
}
|
|
148
|
+
return res
|
|
149
|
+
})
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
}
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { Registry } from '@cosmjs/proto-signing'
|
|
2
|
+
import { defaultRegistryTypes } from "@cosmjs/stargate";
|
|
3
|
+
import { Transaction } from "../utils/type";
|
|
4
|
+
import { KeplerWallet } from './wallets/KeplerWallet';
|
|
5
|
+
import { LedgerWallet } from './wallets/LedgerWallet';
|
|
6
|
+
import { MetamaskWallet } from './wallets/MetamaskWallet';
|
|
7
|
+
import { MetamaskSnapWallet } from './wallets/MetamaskSnapWallet';
|
|
8
|
+
import { LeapWallet } from './wallets/LeapWallet';
|
|
9
|
+
import { OKXWallet } from "./wallets/OKXWallet";
|
|
10
|
+
import { UnisatWallet } from "./wallets/UnisatWallet";
|
|
11
|
+
import { InitiaWallet } from './wallets/InitiaWallet';
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
export enum WalletName {
|
|
16
|
+
Keplr = "Keplr",
|
|
17
|
+
Initia = "Initia Wallet",
|
|
18
|
+
Ledger = "LedgerUSB",
|
|
19
|
+
LedgerBLE = "LedgerBLE",
|
|
20
|
+
Metamask = "Metamask",
|
|
21
|
+
MetamaskSnap = "MetamaskSnap",
|
|
22
|
+
Leap = "Leap",
|
|
23
|
+
OKX = "OKX Wallet",
|
|
24
|
+
Unisat = "UniSat Wallet",
|
|
25
|
+
// None Signning
|
|
26
|
+
Address = "Address",
|
|
27
|
+
NameService = "Nameservice",
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface IChain {
|
|
31
|
+
chainID: string;
|
|
32
|
+
name: string;
|
|
33
|
+
prefix: string;
|
|
34
|
+
rpcUrl: string;
|
|
35
|
+
restUrl: string;
|
|
36
|
+
denom: string;
|
|
37
|
+
hdPath: string;
|
|
38
|
+
logo: string;
|
|
39
|
+
faucetUrl: string;
|
|
40
|
+
explorerUrl: string;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export interface ConnectedWallet {
|
|
44
|
+
wallet: WalletName,
|
|
45
|
+
cosmosAddress: string
|
|
46
|
+
hdPath?: string
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export interface Account {
|
|
50
|
+
address: string,
|
|
51
|
+
algo: string,
|
|
52
|
+
pubkey: Uint8Array,
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export interface WalletArgument {
|
|
56
|
+
chainId?: string,
|
|
57
|
+
hdPath?: string,
|
|
58
|
+
address?: string,
|
|
59
|
+
name?: string,
|
|
60
|
+
transport?: string
|
|
61
|
+
prefix?: string,
|
|
62
|
+
endpoint?: string;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export interface AbstractWallet {
|
|
66
|
+
name: WalletName
|
|
67
|
+
/**
|
|
68
|
+
* The the accounts from the wallet (addresses)
|
|
69
|
+
*/
|
|
70
|
+
getAccounts(): Promise<Account[]>
|
|
71
|
+
supportCoinType(coinType?: string): Promise<boolean>
|
|
72
|
+
sign(transaction: Transaction): Promise<any>
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export const DEFAULT_HDPATH = "m/44'/118/0'/0/0";
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
export function keyType(chainId: string) {
|
|
79
|
+
switch (true) {
|
|
80
|
+
case chainId.search(/\w+_\d+-\d+/g) > -1: // ethermint like chain: evmos_9002-1
|
|
81
|
+
return "/ethermint.crypto.v1.ethsecp256k1.PubKey"
|
|
82
|
+
case chainId.startsWith("injective"):
|
|
83
|
+
return "/injective.crypto.v1beta1.ethsecp256k1.PubKey";
|
|
84
|
+
case chainId.startsWith("stratos"):
|
|
85
|
+
return "/stratos.crypto.v1.ethsecp256k1.PubKey";
|
|
86
|
+
case chainId.startsWith("stratos"):
|
|
87
|
+
return "/stratos.crypto.v1.ethsecp256k1.PubKey";
|
|
88
|
+
default:
|
|
89
|
+
return "/cosmos.crypto.secp256k1.PubKey"
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export function readWallet(hdPath?: string) {
|
|
94
|
+
return JSON.parse(
|
|
95
|
+
localStorage.getItem(hdPath || DEFAULT_HDPATH) || '{}'
|
|
96
|
+
) as ConnectedWallet
|
|
97
|
+
}
|
|
98
|
+
export function writeWallet(connected: ConnectedWallet, hdPath?: string) {
|
|
99
|
+
localStorage.setItem(hdPath || DEFAULT_HDPATH, JSON.stringify(connected))
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export function removeWallet(hdPath?: string) {
|
|
103
|
+
localStorage.removeItem(hdPath || DEFAULT_HDPATH);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export function extractChainId(chainId: string) {
|
|
107
|
+
const start = chainId.indexOf('_')
|
|
108
|
+
const end = chainId.indexOf('-')
|
|
109
|
+
if (end > start && start > 0) {
|
|
110
|
+
return Number(chainId.substring(start + 1, end))
|
|
111
|
+
}
|
|
112
|
+
return 0
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export function createWallet(name: WalletName, arg: WalletArgument, registry?: Registry, chain?: IChain,): AbstractWallet {
|
|
116
|
+
const reg = registry || new Registry(defaultRegistryTypes)
|
|
117
|
+
switch (name) {
|
|
118
|
+
case WalletName.OKX:
|
|
119
|
+
return new OKXWallet(arg, <IChain>chain, reg);
|
|
120
|
+
case WalletName.Unisat:
|
|
121
|
+
return new UnisatWallet(arg, <IChain>chain, reg);
|
|
122
|
+
case WalletName.Keplr:
|
|
123
|
+
return new KeplerWallet(arg, reg)
|
|
124
|
+
case WalletName.Initia:
|
|
125
|
+
return new InitiaWallet(arg, <IChain>chain, reg)
|
|
126
|
+
case WalletName.Ledger:
|
|
127
|
+
return new LedgerWallet(arg, reg)
|
|
128
|
+
case WalletName.Leap:
|
|
129
|
+
return new LeapWallet(arg, reg)
|
|
130
|
+
case WalletName.MetamaskSnap:
|
|
131
|
+
return new MetamaskSnapWallet(arg, reg)
|
|
132
|
+
case WalletName.Metamask:
|
|
133
|
+
return arg.hdPath &&
|
|
134
|
+
(arg.hdPath.startsWith('m/44/60') || arg.hdPath.startsWith("m/44'/60"))
|
|
135
|
+
? new MetamaskWallet(arg, reg) : new MetamaskSnapWallet(arg, reg)
|
|
136
|
+
}
|
|
137
|
+
throw new Error("No wallet connected")
|
|
138
|
+
}
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
import { fromBase64, fromHex } from '@cosmjs/encoding';
|
|
2
|
+
import { makeAuthInfoBytes, makeSignBytes, makeSignDoc, Registry, TxBodyEncodeObject } from '@cosmjs/proto-signing';
|
|
3
|
+
|
|
4
|
+
import { AbstractWallet, Account, IChain, WalletArgument, WalletName, keyType } from '../Wallet';
|
|
5
|
+
import { Transaction, TxResponse } from '../../utils/type';
|
|
6
|
+
import { TxRaw } from 'cosmjs-types/cosmos/tx/v1beta1/tx';
|
|
7
|
+
import { Any } from 'cosmjs-types/google/protobuf/any';
|
|
8
|
+
import { PubKey } from 'cosmjs-types/cosmos/crypto/secp256k1/keys';
|
|
9
|
+
import { SignMode } from 'cosmjs-types/cosmos/tx/signing/v1beta1/signing';
|
|
10
|
+
import { AminoTypes, createDefaultAminoConverters, createIbcAminoConverters } from '@cosmjs/stargate';
|
|
11
|
+
import { makeSignDoc as makeSignDocAmino } from '@cosmjs/amino';
|
|
12
|
+
import { createWasmAminoConverters } from '@cosmjs/cosmwasm-stargate';
|
|
13
|
+
import { Buffer } from 'buffer';
|
|
14
|
+
import { TimeoutError, createAminoTypes, createRegistry, defined, getRPC } from "@initia/utils"
|
|
15
|
+
import { serializeSignDoc } from '@cosmjs/amino/build/signdoc';
|
|
16
|
+
import { Signature } from '@initia/initia.js';
|
|
17
|
+
declare global {
|
|
18
|
+
interface Window {
|
|
19
|
+
initia: any; // Adjust the type as needed
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
export class InitiaWallet implements AbstractWallet {
|
|
23
|
+
name: WalletName.Initia = WalletName.Initia;
|
|
24
|
+
chainId: string;
|
|
25
|
+
chain: IChain;
|
|
26
|
+
registry: Registry;
|
|
27
|
+
conf: WalletArgument;
|
|
28
|
+
aminoTypes: any;
|
|
29
|
+
wallet: any;
|
|
30
|
+
connectEventNamesOnWindow: string[] = [];
|
|
31
|
+
createAminoConverters = createAminoTypes()
|
|
32
|
+
constructor(arg: WalletArgument, chain: IChain, registry: Registry) {
|
|
33
|
+
this.chainId = arg.chainId || "cosmoshub";
|
|
34
|
+
this.aminoTypes = createAminoTypes();
|
|
35
|
+
// @ts-ignore
|
|
36
|
+
|
|
37
|
+
this.registry = createRegistry();
|
|
38
|
+
this.conf = arg;
|
|
39
|
+
this.chain = chain;
|
|
40
|
+
this.wallet = window.initia;
|
|
41
|
+
}
|
|
42
|
+
async getAccounts(): Promise<Account[]> {
|
|
43
|
+
// const chainId = 'cosmoshub'
|
|
44
|
+
// @ts-ignore
|
|
45
|
+
await this.wallet.getOfflineSigner(this.chainId)
|
|
46
|
+
// @ts-ignore
|
|
47
|
+
const offlineSigner = this.wallet.getOfflineSigner(this.chainId)
|
|
48
|
+
return offlineSigner.getAccounts()
|
|
49
|
+
}
|
|
50
|
+
supportCoinType(): Promise<boolean> {
|
|
51
|
+
return Promise.resolve(true);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
isEthermint() {
|
|
55
|
+
return this.conf.hdPath && this.conf.hdPath.startsWith("m/44'/60");
|
|
56
|
+
}
|
|
57
|
+
// @deprecated use signAmino instead
|
|
58
|
+
// because signDirect is not supported ledger wallet
|
|
59
|
+
async sign(transaction: Transaction): Promise<TxResponse> {
|
|
60
|
+
const accouts = await this.getAccounts();
|
|
61
|
+
|
|
62
|
+
const accountFromSigner = accouts[0];
|
|
63
|
+
if (!accountFromSigner) {
|
|
64
|
+
throw new Error("Failed to retrieve account from signer");
|
|
65
|
+
}
|
|
66
|
+
const pubkey = Any.fromPartial({
|
|
67
|
+
typeUrl: keyType(transaction.chainId),
|
|
68
|
+
value: PubKey.encode({
|
|
69
|
+
key: accountFromSigner.pubkey,
|
|
70
|
+
}).finish()
|
|
71
|
+
})
|
|
72
|
+
|
|
73
|
+
const txBodyEncodeObject: TxBodyEncodeObject = {
|
|
74
|
+
typeUrl: "/cosmos.tx.v1beta1.TxBody",
|
|
75
|
+
value: {
|
|
76
|
+
messages: transaction.messages,
|
|
77
|
+
memo: transaction.memo,
|
|
78
|
+
},
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
const txBodyBytes = this.registry.encode(txBodyEncodeObject);
|
|
82
|
+
console.log("txBodyBytes: ", this.registry);
|
|
83
|
+
const gasLimit = Number(transaction.fee.gas);
|
|
84
|
+
const authInfoBytes = makeAuthInfoBytes(
|
|
85
|
+
[{ pubkey, sequence: transaction.signerData.sequence }],
|
|
86
|
+
transaction.fee.amount,
|
|
87
|
+
gasLimit,
|
|
88
|
+
transaction.fee.granter,
|
|
89
|
+
transaction.fee.payer
|
|
90
|
+
);
|
|
91
|
+
|
|
92
|
+
const signDoc = makeSignDoc(
|
|
93
|
+
txBodyBytes,
|
|
94
|
+
authInfoBytes,
|
|
95
|
+
transaction.chainId,
|
|
96
|
+
transaction.signerData.accountNumber
|
|
97
|
+
);
|
|
98
|
+
console.log("signDoc: ", signDoc);
|
|
99
|
+
|
|
100
|
+
const signDocBuffer = makeSignBytes(signDoc);
|
|
101
|
+
|
|
102
|
+
const signString = new TextDecoder().decode(signDocBuffer);
|
|
103
|
+
|
|
104
|
+
// @ts-ignore
|
|
105
|
+
const signature = await this.wallet.signAndBroadcastBlock(this.chainId, txBodyBytes, gasLimit);
|
|
106
|
+
console.log("result22: ", signature);
|
|
107
|
+
|
|
108
|
+
const signed = signDoc;
|
|
109
|
+
console.log("signed: ", signed);
|
|
110
|
+
const res: TxResponse = {
|
|
111
|
+
txhash: signature.transactionHash,
|
|
112
|
+
codespace: signature.codespace,
|
|
113
|
+
code: signature.code,
|
|
114
|
+
raw_log: signature.rawLog,
|
|
115
|
+
height: signature.height,
|
|
116
|
+
data: signature.data,
|
|
117
|
+
// bodyBytes: signed.bodyBytes,
|
|
118
|
+
// authInfoBytes: signed.authInfoBytes,
|
|
119
|
+
// signatures: [fromBase64(signature)],
|
|
120
|
+
};
|
|
121
|
+
return res
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
async signAmino(tx: Transaction): Promise<TxRaw> {
|
|
125
|
+
const accouts = await this.getAccounts();
|
|
126
|
+
|
|
127
|
+
const accountFromSigner = accouts[0];
|
|
128
|
+
if (!accountFromSigner) {
|
|
129
|
+
throw new Error("Failed to retrieve account from signer");
|
|
130
|
+
}
|
|
131
|
+
const pubkey = Any.fromPartial({
|
|
132
|
+
typeUrl: keyType(tx.chainId),
|
|
133
|
+
value: PubKey.encode({
|
|
134
|
+
key: accountFromSigner.pubkey,
|
|
135
|
+
}).finish()
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
const signMode = SignMode.SIGN_MODE_LEGACY_AMINO_JSON;
|
|
139
|
+
const msgs = tx.messages.map((msg) => this.aminoTypes.toAmino(msg));
|
|
140
|
+
const signDoc = makeSignDocAmino(
|
|
141
|
+
msgs,
|
|
142
|
+
tx.fee,
|
|
143
|
+
tx.chainId,
|
|
144
|
+
tx.memo,
|
|
145
|
+
tx.signerData.accountNumber,
|
|
146
|
+
tx.signerData.sequence
|
|
147
|
+
);
|
|
148
|
+
|
|
149
|
+
const signed = signDoc;
|
|
150
|
+
|
|
151
|
+
const signDocBuffer = serializeSignDoc(signDoc);
|
|
152
|
+
|
|
153
|
+
const signString = Buffer.from(signDocBuffer).toString();
|
|
154
|
+
|
|
155
|
+
// @ts-ignore
|
|
156
|
+
|
|
157
|
+
const signature = await window.initia.signArbitrary(signString);
|
|
158
|
+
console.log("signature: ", signature);
|
|
159
|
+
|
|
160
|
+
const signedTxBody = {
|
|
161
|
+
messages: signed.msgs.map((msg) => this.aminoTypes.fromAmino(msg)),
|
|
162
|
+
memo: signed.memo,
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
const signedTxBodyEncodeObject: TxBodyEncodeObject = {
|
|
166
|
+
typeUrl: "/cosmos.tx.v1beta1.TxBody",
|
|
167
|
+
value: signedTxBody,
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
const signedTxBodyBytes = this.registry.encode(signedTxBodyEncodeObject);
|
|
171
|
+
|
|
172
|
+
const signedGasLimit = Number(signed.fee.gas);
|
|
173
|
+
const signedSequence = Number(signed.sequence);
|
|
174
|
+
const signedAuthInfoBytes = makeAuthInfoBytes(
|
|
175
|
+
[{ pubkey, sequence: signedSequence }],
|
|
176
|
+
signed.fee.amount,
|
|
177
|
+
signedGasLimit,
|
|
178
|
+
signed.fee.granter,
|
|
179
|
+
signed.fee.payer,
|
|
180
|
+
signMode
|
|
181
|
+
);
|
|
182
|
+
|
|
183
|
+
return TxRaw.fromPartial({
|
|
184
|
+
bodyBytes: signedTxBodyBytes,
|
|
185
|
+
authInfoBytes: signedAuthInfoBytes,
|
|
186
|
+
signatures: [fromBase64(signature)],
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
}
|