@mictonode/widget 0.3.16 → 0.3.18
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 -3
- package/.github/workflows/npm-publish.yml +34 -34
- package/.prettierrc.json +9 -9
- package/.vscode/extensions.json +3 -3
- package/LICENSE +674 -674
- package/README.md +41 -41
- package/dist/{main-d0e5d1e4.js → main-bbea3e54.js} +155 -153
- package/dist/ping-widget.js +2 -2
- package/dist/ping-widget.umd.cjs +18 -18
- package/dist/{query.lcd-3d4d3495.js → query.lcd-88036522.js} +1 -1
- package/dist/{query.rpc.Query-b7807c29.js → query.rpc.Query-3a2b14f8.js} +1 -1
- package/dist/{tx.rpc.msg-59c3408a.js → tx.rpc.msg-39f0b824.js} +1 -1
- package/example/.vscode/extensions.json +3 -3
- package/example/README.md +7 -7
- package/example/index.html +12 -12
- package/example/package.json +19 -19
- package/example/pnpm-lock.yaml +465 -465
- package/example/public/cdn.html +19 -19
- package/example/src/App.vue +73 -73
- package/example/src/main.js +4 -4
- package/example/vite.config.js +7 -7
- package/index.html +12 -12
- package/lib/components/ConnectWallet/index.vue +262 -262
- package/lib/components/TokenConvert/index.vue +1033 -1033
- package/lib/components/TokenConvert/tokens.ts +36 -36
- package/lib/components/TxDialog/index.vue +422 -422
- package/lib/components/TxDialog/messages/Delegate.vue +174 -174
- package/lib/components/TxDialog/messages/Deposit.vue +96 -96
- package/lib/components/TxDialog/messages/Redelegate.vue +160 -160
- package/lib/components/TxDialog/messages/Send.vue +142 -142
- package/lib/components/TxDialog/messages/Transfer.vue +248 -248
- package/lib/components/TxDialog/messages/Unbond.vue +103 -103
- package/lib/components/TxDialog/messages/Vote.vue +62 -62
- package/lib/components/TxDialog/messages/Withdraw.vue +55 -55
- package/lib/components/TxDialog/messages/WithdrawCommission.vue +74 -74
- package/lib/components/TxDialog/wasm/ClearAdmin.vue +55 -55
- package/lib/components/TxDialog/wasm/ExecuteContract.vue +98 -98
- package/lib/components/TxDialog/wasm/InstantiateContract.vue +108 -108
- package/lib/components/TxDialog/wasm/MigrateContract.vue +76 -76
- package/lib/components/TxDialog/wasm/MigrateContract2.vue +76 -76
- package/lib/components/TxDialog/wasm/StoreCode.vue +100 -100
- package/lib/components/TxDialog/wasm/UpdateAdmin.vue +74 -74
- package/lib/main.css +591 -7
- package/lib/main.ts +24 -23
- package/lib/utils/TokenUnitConverter.ts +44 -44
- package/lib/utils/format.ts +16 -16
- package/lib/utils/http.ts +154 -154
- package/lib/utils/type.ts +56 -56
- package/lib/wallet/EthermintMessageAdapter.ts +135 -135
- package/lib/wallet/UniClient.ts +144 -144
- package/lib/wallet/Wallet.ts +142 -142
- package/lib/wallet/wallets/KeplerWallet.ts +141 -141
- package/lib/wallet/wallets/LeapWallet.ts +141 -141
- package/lib/wallet/wallets/LedgerWallet.ts +202 -202
- package/lib/wallet/wallets/MetamaskSnapWallet.ts +105 -105
- package/lib/wallet/wallets/MetamaskWallet.ts +173 -173
- package/lib/wallet/wallets/OKXWallet.ts +202 -202
- package/lib/wallet/wallets/UnisatWallet.ts +198 -198
- package/package.json +5 -6
- package/postcss.config.js +6 -6
- package/src/App.vue +225 -225
- package/src/main.ts +7 -4
- package/src/styles/design-system.css +184 -0
- package/src/vite-env.d.ts +1 -1
- package/tailwind.config.js +158 -34
- package/tsconfig.json +25 -25
- package/tsconfig.node.json +10 -10
- package/vite.config.ts +62 -62
- package/vue-shim.d.ts +6 -6
package/lib/wallet/Wallet.ts
CHANGED
|
@@ -1,142 +1,142 @@
|
|
|
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
|
-
|
|
12
|
-
export enum WalletName {
|
|
13
|
-
Keplr = "Keplr",
|
|
14
|
-
Ledger = "LedgerUSB",
|
|
15
|
-
LedgerBLE = "LedgerBLE",
|
|
16
|
-
Metamask = "Metamask",
|
|
17
|
-
MetamaskSnap = "MetamaskSnap",
|
|
18
|
-
Leap = "Leap",
|
|
19
|
-
OKX = "OKX Wallet",
|
|
20
|
-
Unisat = "UniSat Wallet",
|
|
21
|
-
// None Signning
|
|
22
|
-
Address = "Address",
|
|
23
|
-
NameService = "Nameservice",
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
export interface IChain {
|
|
27
|
-
chainID: string;
|
|
28
|
-
name: string;
|
|
29
|
-
prefix: string;
|
|
30
|
-
rpcUrl: string;
|
|
31
|
-
restUrl: string;
|
|
32
|
-
denom: string;
|
|
33
|
-
hdPath: string;
|
|
34
|
-
logo: string;
|
|
35
|
-
faucetUrl: string;
|
|
36
|
-
explorerUrl: string;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
export interface ConnectedWallet {
|
|
40
|
-
wallet: WalletName,
|
|
41
|
-
cosmosAddress: string
|
|
42
|
-
hdPath?: string
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
export interface Account {
|
|
46
|
-
address: string,
|
|
47
|
-
algo: string,
|
|
48
|
-
pubkey: Uint8Array,
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
export interface WalletArgument {
|
|
52
|
-
chainId?: string,
|
|
53
|
-
hdPath?: string,
|
|
54
|
-
address?: string,
|
|
55
|
-
name?: string,
|
|
56
|
-
transport?: string
|
|
57
|
-
prefix?: string,
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
export interface AbstractWallet {
|
|
61
|
-
name: WalletName
|
|
62
|
-
/**
|
|
63
|
-
* The the accounts from the wallet (addresses)
|
|
64
|
-
*/
|
|
65
|
-
getAccounts(): Promise<Account[]>
|
|
66
|
-
supportCoinType(coinType?: string): Promise<boolean>
|
|
67
|
-
sign(transaction: Transaction): Promise<any>
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
export const DEFAULT_HDPATH = "m/44'/118/0'/0/0";
|
|
71
|
-
|
|
72
|
-
export function keyType(chainId: string) {
|
|
73
|
-
switch (true) {
|
|
74
|
-
// Cosmos EVM chains (Warden, etc.) - uses cosmos.evm module
|
|
75
|
-
case chainId.startsWith("warden"):
|
|
76
|
-
case chainId.startsWith("barra"): // Warden testnet
|
|
77
|
-
return "/cosmos.evm.crypto.v1.ethsecp256k1.PubKey";
|
|
78
|
-
// Ethermint-like chains: evmos_9002-1, cronos_25-1, etc.
|
|
79
|
-
case chainId.search(/\w+_\d+-\d+/g) > -1:
|
|
80
|
-
return "/ethermint.crypto.v1.ethsecp256k1.PubKey"
|
|
81
|
-
case chainId.startsWith("injective"):
|
|
82
|
-
return "/injective.crypto.v1beta1.ethsecp256k1.PubKey";
|
|
83
|
-
case chainId.startsWith("stratos"):
|
|
84
|
-
return "/stratos.crypto.v1.ethsecp256k1.PubKey";
|
|
85
|
-
default:
|
|
86
|
-
return "/cosmos.crypto.secp256k1.PubKey"
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
// Check if chain is EVM-based (requires higher fees)
|
|
91
|
-
export function isEvmChain(chainId: string): boolean {
|
|
92
|
-
return chainId.startsWith("warden") ||
|
|
93
|
-
chainId.startsWith("barra") ||
|
|
94
|
-
chainId.search(/\w+_\d+-\d+/g) > -1 ||
|
|
95
|
-
chainId.startsWith("injective") ||
|
|
96
|
-
chainId.startsWith("stratos");
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
export function readWallet(hdPath?: string) {
|
|
100
|
-
return JSON.parse(
|
|
101
|
-
localStorage.getItem(hdPath || DEFAULT_HDPATH) || '{}'
|
|
102
|
-
) as ConnectedWallet
|
|
103
|
-
}
|
|
104
|
-
export function writeWallet(connected: ConnectedWallet, hdPath?: string) {
|
|
105
|
-
localStorage.setItem(hdPath || DEFAULT_HDPATH, JSON.stringify(connected))
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
export function removeWallet(hdPath?: string) {
|
|
109
|
-
localStorage.removeItem(hdPath || DEFAULT_HDPATH);
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
export function extractChainId(chainId: string) {
|
|
113
|
-
const start = chainId.indexOf('_')
|
|
114
|
-
const end = chainId.indexOf('-')
|
|
115
|
-
if (end > start && start > 0) {
|
|
116
|
-
return Number(chainId.substring(start + 1, end))
|
|
117
|
-
}
|
|
118
|
-
return 0
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
export function createWallet(name: WalletName, arg: WalletArgument, registry?: Registry, chain?: IChain,): AbstractWallet {
|
|
122
|
-
const reg = registry || new Registry(defaultRegistryTypes)
|
|
123
|
-
switch (name) {
|
|
124
|
-
case WalletName.OKX:
|
|
125
|
-
return new OKXWallet(arg, <IChain>chain, reg);
|
|
126
|
-
case WalletName.Unisat:
|
|
127
|
-
return new UnisatWallet(arg, <IChain>chain, reg);
|
|
128
|
-
case WalletName.Keplr:
|
|
129
|
-
return new KeplerWallet(arg, reg)
|
|
130
|
-
case WalletName.Ledger:
|
|
131
|
-
return new LedgerWallet(arg, reg)
|
|
132
|
-
case WalletName.Leap:
|
|
133
|
-
return new LeapWallet(arg, reg)
|
|
134
|
-
case WalletName.MetamaskSnap:
|
|
135
|
-
return new MetamaskSnapWallet(arg, reg)
|
|
136
|
-
case WalletName.Metamask:
|
|
137
|
-
return arg.hdPath &&
|
|
138
|
-
(arg.hdPath.startsWith('m/44/60') || arg.hdPath.startsWith("m/44'/60"))
|
|
139
|
-
? new MetamaskWallet(arg, reg) : new MetamaskSnapWallet(arg, reg)
|
|
140
|
-
}
|
|
141
|
-
throw new Error("No wallet connected")
|
|
142
|
-
}
|
|
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
|
+
|
|
12
|
+
export enum WalletName {
|
|
13
|
+
Keplr = "Keplr",
|
|
14
|
+
Ledger = "LedgerUSB",
|
|
15
|
+
LedgerBLE = "LedgerBLE",
|
|
16
|
+
Metamask = "Metamask",
|
|
17
|
+
MetamaskSnap = "MetamaskSnap",
|
|
18
|
+
Leap = "Leap",
|
|
19
|
+
OKX = "OKX Wallet",
|
|
20
|
+
Unisat = "UniSat Wallet",
|
|
21
|
+
// None Signning
|
|
22
|
+
Address = "Address",
|
|
23
|
+
NameService = "Nameservice",
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface IChain {
|
|
27
|
+
chainID: string;
|
|
28
|
+
name: string;
|
|
29
|
+
prefix: string;
|
|
30
|
+
rpcUrl: string;
|
|
31
|
+
restUrl: string;
|
|
32
|
+
denom: string;
|
|
33
|
+
hdPath: string;
|
|
34
|
+
logo: string;
|
|
35
|
+
faucetUrl: string;
|
|
36
|
+
explorerUrl: string;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface ConnectedWallet {
|
|
40
|
+
wallet: WalletName,
|
|
41
|
+
cosmosAddress: string
|
|
42
|
+
hdPath?: string
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export interface Account {
|
|
46
|
+
address: string,
|
|
47
|
+
algo: string,
|
|
48
|
+
pubkey: Uint8Array,
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export interface WalletArgument {
|
|
52
|
+
chainId?: string,
|
|
53
|
+
hdPath?: string,
|
|
54
|
+
address?: string,
|
|
55
|
+
name?: string,
|
|
56
|
+
transport?: string
|
|
57
|
+
prefix?: string,
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export interface AbstractWallet {
|
|
61
|
+
name: WalletName
|
|
62
|
+
/**
|
|
63
|
+
* The the accounts from the wallet (addresses)
|
|
64
|
+
*/
|
|
65
|
+
getAccounts(): Promise<Account[]>
|
|
66
|
+
supportCoinType(coinType?: string): Promise<boolean>
|
|
67
|
+
sign(transaction: Transaction): Promise<any>
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export const DEFAULT_HDPATH = "m/44'/118/0'/0/0";
|
|
71
|
+
|
|
72
|
+
export function keyType(chainId: string) {
|
|
73
|
+
switch (true) {
|
|
74
|
+
// Cosmos EVM chains (Warden, etc.) - uses cosmos.evm module
|
|
75
|
+
case chainId.startsWith("warden"):
|
|
76
|
+
case chainId.startsWith("barra"): // Warden testnet
|
|
77
|
+
return "/cosmos.evm.crypto.v1.ethsecp256k1.PubKey";
|
|
78
|
+
// Ethermint-like chains: evmos_9002-1, cronos_25-1, etc.
|
|
79
|
+
case chainId.search(/\w+_\d+-\d+/g) > -1:
|
|
80
|
+
return "/ethermint.crypto.v1.ethsecp256k1.PubKey"
|
|
81
|
+
case chainId.startsWith("injective"):
|
|
82
|
+
return "/injective.crypto.v1beta1.ethsecp256k1.PubKey";
|
|
83
|
+
case chainId.startsWith("stratos"):
|
|
84
|
+
return "/stratos.crypto.v1.ethsecp256k1.PubKey";
|
|
85
|
+
default:
|
|
86
|
+
return "/cosmos.crypto.secp256k1.PubKey"
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// Check if chain is EVM-based (requires higher fees)
|
|
91
|
+
export function isEvmChain(chainId: string): boolean {
|
|
92
|
+
return chainId.startsWith("warden") ||
|
|
93
|
+
chainId.startsWith("barra") ||
|
|
94
|
+
chainId.search(/\w+_\d+-\d+/g) > -1 ||
|
|
95
|
+
chainId.startsWith("injective") ||
|
|
96
|
+
chainId.startsWith("stratos");
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export function readWallet(hdPath?: string) {
|
|
100
|
+
return JSON.parse(
|
|
101
|
+
localStorage.getItem(hdPath || DEFAULT_HDPATH) || '{}'
|
|
102
|
+
) as ConnectedWallet
|
|
103
|
+
}
|
|
104
|
+
export function writeWallet(connected: ConnectedWallet, hdPath?: string) {
|
|
105
|
+
localStorage.setItem(hdPath || DEFAULT_HDPATH, JSON.stringify(connected))
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export function removeWallet(hdPath?: string) {
|
|
109
|
+
localStorage.removeItem(hdPath || DEFAULT_HDPATH);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export function extractChainId(chainId: string) {
|
|
113
|
+
const start = chainId.indexOf('_')
|
|
114
|
+
const end = chainId.indexOf('-')
|
|
115
|
+
if (end > start && start > 0) {
|
|
116
|
+
return Number(chainId.substring(start + 1, end))
|
|
117
|
+
}
|
|
118
|
+
return 0
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export function createWallet(name: WalletName, arg: WalletArgument, registry?: Registry, chain?: IChain,): AbstractWallet {
|
|
122
|
+
const reg = registry || new Registry(defaultRegistryTypes)
|
|
123
|
+
switch (name) {
|
|
124
|
+
case WalletName.OKX:
|
|
125
|
+
return new OKXWallet(arg, <IChain>chain, reg);
|
|
126
|
+
case WalletName.Unisat:
|
|
127
|
+
return new UnisatWallet(arg, <IChain>chain, reg);
|
|
128
|
+
case WalletName.Keplr:
|
|
129
|
+
return new KeplerWallet(arg, reg)
|
|
130
|
+
case WalletName.Ledger:
|
|
131
|
+
return new LedgerWallet(arg, reg)
|
|
132
|
+
case WalletName.Leap:
|
|
133
|
+
return new LeapWallet(arg, reg)
|
|
134
|
+
case WalletName.MetamaskSnap:
|
|
135
|
+
return new MetamaskSnapWallet(arg, reg)
|
|
136
|
+
case WalletName.Metamask:
|
|
137
|
+
return arg.hdPath &&
|
|
138
|
+
(arg.hdPath.startsWith('m/44/60') || arg.hdPath.startsWith("m/44'/60"))
|
|
139
|
+
? new MetamaskWallet(arg, reg) : new MetamaskSnapWallet(arg, reg)
|
|
140
|
+
}
|
|
141
|
+
throw new Error("No wallet connected")
|
|
142
|
+
}
|
|
@@ -1,141 +1,141 @@
|
|
|
1
|
-
import { fromBase64, fromBech32, toHex } from "@cosmjs/encoding";
|
|
2
|
-
import { Registry, TxBodyEncodeObject, encodePubkey, makeAuthInfoBytes, makeSignDoc } from "@cosmjs/proto-signing"
|
|
3
|
-
import { AbstractWallet, Account, WalletArgument, WalletName, keyType } from "../Wallet"
|
|
4
|
-
import { Transaction } from "../../utils/type"
|
|
5
|
-
import { TxRaw } from "cosmjs-types/cosmos/tx/v1beta1/tx";
|
|
6
|
-
import { Any } from "cosmjs-types/google/protobuf/any";
|
|
7
|
-
import { PubKey } from 'cosmjs-types/cosmos/crypto/secp256k1/keys'
|
|
8
|
-
import { SignMode } from "cosmjs-types/cosmos/tx/signing/v1beta1/signing";
|
|
9
|
-
import { AminoTypes, createDefaultAminoConverters } from "@cosmjs/stargate";
|
|
10
|
-
import { encodeSecp256k1Pubkey, makeSignDoc as makeSignDocAmino } from "@cosmjs/amino";
|
|
11
|
-
import { createWasmAminoConverters } from "@cosmjs/cosmwasm-stargate";
|
|
12
|
-
|
|
13
|
-
export class KeplerWallet implements AbstractWallet {
|
|
14
|
-
name: WalletName.Keplr
|
|
15
|
-
chainId: string
|
|
16
|
-
registry: Registry
|
|
17
|
-
conf: WalletArgument
|
|
18
|
-
aminoTypes = new AminoTypes( {...createDefaultAminoConverters(), ...createWasmAminoConverters()})
|
|
19
|
-
constructor(arg: WalletArgument, registry: Registry) {
|
|
20
|
-
this.chainId = arg.chainId || "cosmoshub"
|
|
21
|
-
// @ts-ignore
|
|
22
|
-
if (!window.getOfflineSigner || !window.keplr) {
|
|
23
|
-
throw new Error('Please install keplr extension')
|
|
24
|
-
}
|
|
25
|
-
this.registry = registry
|
|
26
|
-
this.conf = arg
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
async getAccounts(): Promise<Account[]> {
|
|
30
|
-
// const chainId = 'cosmoshub'
|
|
31
|
-
// @ts-ignore
|
|
32
|
-
await window.keplr.enable(this.chainId)
|
|
33
|
-
// @ts-ignore
|
|
34
|
-
const offlineSigner = window.getOfflineSigner(this.chainId)
|
|
35
|
-
return offlineSigner.getAccounts()
|
|
36
|
-
}
|
|
37
|
-
supportCoinType(coinType?: string | undefined): Promise<boolean> {
|
|
38
|
-
return Promise.resolve(true);
|
|
39
|
-
}
|
|
40
|
-
isEthermint() {
|
|
41
|
-
return this.conf.hdPath && this.conf.hdPath.startsWith("m/44'/60")
|
|
42
|
-
}
|
|
43
|
-
async sign(transaction: Transaction): Promise<TxRaw> {
|
|
44
|
-
// sign wasm tx with signDirect
|
|
45
|
-
if(transaction.messages.findIndex(x => x.typeUrl.startsWith("/cosmwasm.wasm")) > -1) {
|
|
46
|
-
return this.signDirect(transaction)
|
|
47
|
-
}
|
|
48
|
-
return this.signAmino(transaction)
|
|
49
|
-
}
|
|
50
|
-
// @deprecated use signAmino instead
|
|
51
|
-
// because signDirect is not supported ledger wallet
|
|
52
|
-
async signDirect(transaction: Transaction): Promise<TxRaw> {
|
|
53
|
-
const accouts = await this.getAccounts()
|
|
54
|
-
const hex = toHex(fromBech32(transaction.signerAddress).data)
|
|
55
|
-
const accountFromSigner = accouts.find((account) => toHex(fromBech32(account.address).data) === hex);
|
|
56
|
-
if (!accountFromSigner) {
|
|
57
|
-
throw new Error("Failed to retrieve account from signer");
|
|
58
|
-
}
|
|
59
|
-
const pubkey = Any.fromPartial({
|
|
60
|
-
typeUrl: keyType(transaction.chainId),
|
|
61
|
-
value: PubKey.encode({
|
|
62
|
-
key: accountFromSigner.pubkey,
|
|
63
|
-
}).finish()
|
|
64
|
-
})
|
|
65
|
-
const txBodyEncodeObject: TxBodyEncodeObject = {
|
|
66
|
-
typeUrl: "/cosmos.tx.v1beta1.TxBody",
|
|
67
|
-
value: {
|
|
68
|
-
messages: transaction.messages,
|
|
69
|
-
memo: transaction.memo,
|
|
70
|
-
},
|
|
71
|
-
};
|
|
72
|
-
const txBodyBytes = this.registry.encode(txBodyEncodeObject);
|
|
73
|
-
const gasLimit = Number(transaction.fee.gas);
|
|
74
|
-
const authInfoBytes = makeAuthInfoBytes(
|
|
75
|
-
[{ pubkey, sequence: transaction.signerData.sequence }],
|
|
76
|
-
transaction.fee.amount,
|
|
77
|
-
gasLimit,
|
|
78
|
-
transaction.fee.granter,
|
|
79
|
-
transaction.fee.payer,
|
|
80
|
-
);
|
|
81
|
-
const signDoc = makeSignDoc(txBodyBytes, authInfoBytes, transaction.chainId, transaction.signerData.accountNumber);
|
|
82
|
-
|
|
83
|
-
// @ts-ignore
|
|
84
|
-
const offlineSigner = window.getOfflineSigner(this.chainId)
|
|
85
|
-
const { signature, signed } = await offlineSigner.signDirect(transaction.signerAddress, signDoc);;
|
|
86
|
-
return TxRaw.fromPartial({
|
|
87
|
-
bodyBytes: signed.bodyBytes,
|
|
88
|
-
authInfoBytes: signed.authInfoBytes,
|
|
89
|
-
signatures: [fromBase64(signature.signature)],
|
|
90
|
-
});
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
async signAmino(tx: Transaction): Promise<TxRaw> {
|
|
94
|
-
const accouts = await this.getAccounts()
|
|
95
|
-
const hex = toHex(fromBech32(tx.signerAddress).data)
|
|
96
|
-
const accountFromSigner = accouts.find((account) => toHex(fromBech32(account.address).data) === hex);
|
|
97
|
-
if (!accountFromSigner) {
|
|
98
|
-
throw new Error("Failed to retrieve account from signer");
|
|
99
|
-
}
|
|
100
|
-
// const pubkey = encodePubkey(encodeSecp256k1Pubkey(accountFromSigner.pubkey));
|
|
101
|
-
const pubkey = Any.fromPartial({
|
|
102
|
-
typeUrl: keyType(tx.chainId),
|
|
103
|
-
value: PubKey.encode({
|
|
104
|
-
key: accountFromSigner.pubkey,
|
|
105
|
-
}).finish()
|
|
106
|
-
})
|
|
107
|
-
const signMode = SignMode.SIGN_MODE_LEGACY_AMINO_JSON;
|
|
108
|
-
const msgs = tx.messages.map((msg) => this.aminoTypes.toAmino(msg));
|
|
109
|
-
const signDoc = makeSignDocAmino(msgs, tx.fee, tx.chainId, tx.memo, tx.signerData.accountNumber, tx.signerData.sequence);
|
|
110
|
-
|
|
111
|
-
// @ts-ignore
|
|
112
|
-
const offlineSigner = window.getOfflineSigner(this.chainId)
|
|
113
|
-
const { signature, signed } = await offlineSigner.signAmino(tx.signerAddress, signDoc);
|
|
114
|
-
|
|
115
|
-
const signedTxBody = {
|
|
116
|
-
messages: signed.msgs.map((msg) => this.aminoTypes.fromAmino(msg)),
|
|
117
|
-
memo: signed.memo,
|
|
118
|
-
};
|
|
119
|
-
const signedTxBodyEncodeObject: TxBodyEncodeObject = {
|
|
120
|
-
typeUrl: "/cosmos.tx.v1beta1.TxBody",
|
|
121
|
-
value: signedTxBody,
|
|
122
|
-
};
|
|
123
|
-
const signedTxBodyBytes = this.registry.encode(signedTxBodyEncodeObject);
|
|
124
|
-
|
|
125
|
-
const signedGasLimit = Number(signed.fee.gas);
|
|
126
|
-
const signedSequence = Number(signed.sequence);
|
|
127
|
-
const signedAuthInfoBytes = makeAuthInfoBytes(
|
|
128
|
-
[{ pubkey, sequence: signedSequence }],
|
|
129
|
-
signed.fee.amount,
|
|
130
|
-
signedGasLimit,
|
|
131
|
-
signed.fee.granter,
|
|
132
|
-
signed.fee.payer,
|
|
133
|
-
signMode,
|
|
134
|
-
);
|
|
135
|
-
return TxRaw.fromPartial({
|
|
136
|
-
bodyBytes: signedTxBodyBytes,
|
|
137
|
-
authInfoBytes: signedAuthInfoBytes,
|
|
138
|
-
signatures: [fromBase64(signature.signature)],
|
|
139
|
-
});
|
|
140
|
-
}
|
|
141
|
-
}
|
|
1
|
+
import { fromBase64, fromBech32, toHex } from "@cosmjs/encoding";
|
|
2
|
+
import { Registry, TxBodyEncodeObject, encodePubkey, makeAuthInfoBytes, makeSignDoc } from "@cosmjs/proto-signing"
|
|
3
|
+
import { AbstractWallet, Account, WalletArgument, WalletName, keyType } from "../Wallet"
|
|
4
|
+
import { Transaction } from "../../utils/type"
|
|
5
|
+
import { TxRaw } from "cosmjs-types/cosmos/tx/v1beta1/tx";
|
|
6
|
+
import { Any } from "cosmjs-types/google/protobuf/any";
|
|
7
|
+
import { PubKey } from 'cosmjs-types/cosmos/crypto/secp256k1/keys'
|
|
8
|
+
import { SignMode } from "cosmjs-types/cosmos/tx/signing/v1beta1/signing";
|
|
9
|
+
import { AminoTypes, createDefaultAminoConverters } from "@cosmjs/stargate";
|
|
10
|
+
import { encodeSecp256k1Pubkey, makeSignDoc as makeSignDocAmino } from "@cosmjs/amino";
|
|
11
|
+
import { createWasmAminoConverters } from "@cosmjs/cosmwasm-stargate";
|
|
12
|
+
|
|
13
|
+
export class KeplerWallet implements AbstractWallet {
|
|
14
|
+
name: WalletName.Keplr
|
|
15
|
+
chainId: string
|
|
16
|
+
registry: Registry
|
|
17
|
+
conf: WalletArgument
|
|
18
|
+
aminoTypes = new AminoTypes( {...createDefaultAminoConverters(), ...createWasmAminoConverters()})
|
|
19
|
+
constructor(arg: WalletArgument, registry: Registry) {
|
|
20
|
+
this.chainId = arg.chainId || "cosmoshub"
|
|
21
|
+
// @ts-ignore
|
|
22
|
+
if (!window.getOfflineSigner || !window.keplr) {
|
|
23
|
+
throw new Error('Please install keplr extension')
|
|
24
|
+
}
|
|
25
|
+
this.registry = registry
|
|
26
|
+
this.conf = arg
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
async getAccounts(): Promise<Account[]> {
|
|
30
|
+
// const chainId = 'cosmoshub'
|
|
31
|
+
// @ts-ignore
|
|
32
|
+
await window.keplr.enable(this.chainId)
|
|
33
|
+
// @ts-ignore
|
|
34
|
+
const offlineSigner = window.getOfflineSigner(this.chainId)
|
|
35
|
+
return offlineSigner.getAccounts()
|
|
36
|
+
}
|
|
37
|
+
supportCoinType(coinType?: string | undefined): Promise<boolean> {
|
|
38
|
+
return Promise.resolve(true);
|
|
39
|
+
}
|
|
40
|
+
isEthermint() {
|
|
41
|
+
return this.conf.hdPath && this.conf.hdPath.startsWith("m/44'/60")
|
|
42
|
+
}
|
|
43
|
+
async sign(transaction: Transaction): Promise<TxRaw> {
|
|
44
|
+
// sign wasm tx with signDirect
|
|
45
|
+
if(transaction.messages.findIndex(x => x.typeUrl.startsWith("/cosmwasm.wasm")) > -1) {
|
|
46
|
+
return this.signDirect(transaction)
|
|
47
|
+
}
|
|
48
|
+
return this.signAmino(transaction)
|
|
49
|
+
}
|
|
50
|
+
// @deprecated use signAmino instead
|
|
51
|
+
// because signDirect is not supported ledger wallet
|
|
52
|
+
async signDirect(transaction: Transaction): Promise<TxRaw> {
|
|
53
|
+
const accouts = await this.getAccounts()
|
|
54
|
+
const hex = toHex(fromBech32(transaction.signerAddress).data)
|
|
55
|
+
const accountFromSigner = accouts.find((account) => toHex(fromBech32(account.address).data) === hex);
|
|
56
|
+
if (!accountFromSigner) {
|
|
57
|
+
throw new Error("Failed to retrieve account from signer");
|
|
58
|
+
}
|
|
59
|
+
const pubkey = Any.fromPartial({
|
|
60
|
+
typeUrl: keyType(transaction.chainId),
|
|
61
|
+
value: PubKey.encode({
|
|
62
|
+
key: accountFromSigner.pubkey,
|
|
63
|
+
}).finish()
|
|
64
|
+
})
|
|
65
|
+
const txBodyEncodeObject: TxBodyEncodeObject = {
|
|
66
|
+
typeUrl: "/cosmos.tx.v1beta1.TxBody",
|
|
67
|
+
value: {
|
|
68
|
+
messages: transaction.messages,
|
|
69
|
+
memo: transaction.memo,
|
|
70
|
+
},
|
|
71
|
+
};
|
|
72
|
+
const txBodyBytes = this.registry.encode(txBodyEncodeObject);
|
|
73
|
+
const gasLimit = Number(transaction.fee.gas);
|
|
74
|
+
const authInfoBytes = makeAuthInfoBytes(
|
|
75
|
+
[{ pubkey, sequence: transaction.signerData.sequence }],
|
|
76
|
+
transaction.fee.amount,
|
|
77
|
+
gasLimit,
|
|
78
|
+
transaction.fee.granter,
|
|
79
|
+
transaction.fee.payer,
|
|
80
|
+
);
|
|
81
|
+
const signDoc = makeSignDoc(txBodyBytes, authInfoBytes, transaction.chainId, transaction.signerData.accountNumber);
|
|
82
|
+
|
|
83
|
+
// @ts-ignore
|
|
84
|
+
const offlineSigner = window.getOfflineSigner(this.chainId)
|
|
85
|
+
const { signature, signed } = await offlineSigner.signDirect(transaction.signerAddress, signDoc);;
|
|
86
|
+
return TxRaw.fromPartial({
|
|
87
|
+
bodyBytes: signed.bodyBytes,
|
|
88
|
+
authInfoBytes: signed.authInfoBytes,
|
|
89
|
+
signatures: [fromBase64(signature.signature)],
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
async signAmino(tx: Transaction): Promise<TxRaw> {
|
|
94
|
+
const accouts = await this.getAccounts()
|
|
95
|
+
const hex = toHex(fromBech32(tx.signerAddress).data)
|
|
96
|
+
const accountFromSigner = accouts.find((account) => toHex(fromBech32(account.address).data) === hex);
|
|
97
|
+
if (!accountFromSigner) {
|
|
98
|
+
throw new Error("Failed to retrieve account from signer");
|
|
99
|
+
}
|
|
100
|
+
// const pubkey = encodePubkey(encodeSecp256k1Pubkey(accountFromSigner.pubkey));
|
|
101
|
+
const pubkey = Any.fromPartial({
|
|
102
|
+
typeUrl: keyType(tx.chainId),
|
|
103
|
+
value: PubKey.encode({
|
|
104
|
+
key: accountFromSigner.pubkey,
|
|
105
|
+
}).finish()
|
|
106
|
+
})
|
|
107
|
+
const signMode = SignMode.SIGN_MODE_LEGACY_AMINO_JSON;
|
|
108
|
+
const msgs = tx.messages.map((msg) => this.aminoTypes.toAmino(msg));
|
|
109
|
+
const signDoc = makeSignDocAmino(msgs, tx.fee, tx.chainId, tx.memo, tx.signerData.accountNumber, tx.signerData.sequence);
|
|
110
|
+
|
|
111
|
+
// @ts-ignore
|
|
112
|
+
const offlineSigner = window.getOfflineSigner(this.chainId)
|
|
113
|
+
const { signature, signed } = await offlineSigner.signAmino(tx.signerAddress, signDoc);
|
|
114
|
+
|
|
115
|
+
const signedTxBody = {
|
|
116
|
+
messages: signed.msgs.map((msg) => this.aminoTypes.fromAmino(msg)),
|
|
117
|
+
memo: signed.memo,
|
|
118
|
+
};
|
|
119
|
+
const signedTxBodyEncodeObject: TxBodyEncodeObject = {
|
|
120
|
+
typeUrl: "/cosmos.tx.v1beta1.TxBody",
|
|
121
|
+
value: signedTxBody,
|
|
122
|
+
};
|
|
123
|
+
const signedTxBodyBytes = this.registry.encode(signedTxBodyEncodeObject);
|
|
124
|
+
|
|
125
|
+
const signedGasLimit = Number(signed.fee.gas);
|
|
126
|
+
const signedSequence = Number(signed.sequence);
|
|
127
|
+
const signedAuthInfoBytes = makeAuthInfoBytes(
|
|
128
|
+
[{ pubkey, sequence: signedSequence }],
|
|
129
|
+
signed.fee.amount,
|
|
130
|
+
signedGasLimit,
|
|
131
|
+
signed.fee.granter,
|
|
132
|
+
signed.fee.payer,
|
|
133
|
+
signMode,
|
|
134
|
+
);
|
|
135
|
+
return TxRaw.fromPartial({
|
|
136
|
+
bodyBytes: signedTxBodyBytes,
|
|
137
|
+
authInfoBytes: signedAuthInfoBytes,
|
|
138
|
+
signatures: [fromBase64(signature.signature)],
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
}
|