@mictonode/widget 0.3.16 → 0.3.17

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.
Files changed (69) hide show
  1. package/.github/FUNDING.yml +3 -3
  2. package/.github/workflows/npm-publish.yml +34 -34
  3. package/.prettierrc.json +9 -9
  4. package/.vscode/extensions.json +3 -3
  5. package/LICENSE +674 -674
  6. package/README.md +41 -41
  7. package/dist/{main-d0e5d1e4.js → main-465ae991.js} +151 -149
  8. package/dist/ping-widget.js +2 -2
  9. package/dist/ping-widget.umd.cjs +17 -17
  10. package/dist/{query.lcd-3d4d3495.js → query.lcd-d9c6163c.js} +1 -1
  11. package/dist/{query.rpc.Query-b7807c29.js → query.rpc.Query-a479e6f9.js} +1 -1
  12. package/dist/{tx.rpc.msg-59c3408a.js → tx.rpc.msg-7444045d.js} +1 -1
  13. package/example/.vscode/extensions.json +3 -3
  14. package/example/README.md +7 -7
  15. package/example/index.html +12 -12
  16. package/example/package.json +19 -19
  17. package/example/pnpm-lock.yaml +465 -465
  18. package/example/public/cdn.html +19 -19
  19. package/example/src/App.vue +73 -73
  20. package/example/src/main.js +4 -4
  21. package/example/vite.config.js +7 -7
  22. package/index.html +12 -12
  23. package/lib/components/ConnectWallet/index.vue +262 -262
  24. package/lib/components/TokenConvert/index.vue +1033 -1033
  25. package/lib/components/TokenConvert/tokens.ts +36 -36
  26. package/lib/components/TxDialog/index.vue +8 -8
  27. package/lib/components/TxDialog/messages/Delegate.vue +174 -174
  28. package/lib/components/TxDialog/messages/Deposit.vue +96 -96
  29. package/lib/components/TxDialog/messages/Redelegate.vue +160 -160
  30. package/lib/components/TxDialog/messages/Send.vue +142 -142
  31. package/lib/components/TxDialog/messages/Transfer.vue +248 -248
  32. package/lib/components/TxDialog/messages/Unbond.vue +103 -103
  33. package/lib/components/TxDialog/messages/Vote.vue +62 -62
  34. package/lib/components/TxDialog/messages/Withdraw.vue +55 -55
  35. package/lib/components/TxDialog/messages/WithdrawCommission.vue +74 -74
  36. package/lib/components/TxDialog/wasm/ClearAdmin.vue +55 -55
  37. package/lib/components/TxDialog/wasm/ExecuteContract.vue +98 -98
  38. package/lib/components/TxDialog/wasm/InstantiateContract.vue +108 -108
  39. package/lib/components/TxDialog/wasm/MigrateContract.vue +76 -76
  40. package/lib/components/TxDialog/wasm/MigrateContract2.vue +76 -76
  41. package/lib/components/TxDialog/wasm/StoreCode.vue +100 -100
  42. package/lib/components/TxDialog/wasm/UpdateAdmin.vue +74 -74
  43. package/lib/main.css +132 -7
  44. package/lib/main.ts +23 -23
  45. package/lib/utils/TokenUnitConverter.ts +44 -44
  46. package/lib/utils/format.ts +16 -16
  47. package/lib/utils/http.ts +154 -154
  48. package/lib/utils/type.ts +56 -56
  49. package/lib/wallet/EthermintMessageAdapter.ts +135 -135
  50. package/lib/wallet/UniClient.ts +144 -144
  51. package/lib/wallet/Wallet.ts +142 -142
  52. package/lib/wallet/wallets/KeplerWallet.ts +141 -141
  53. package/lib/wallet/wallets/LeapWallet.ts +141 -141
  54. package/lib/wallet/wallets/LedgerWallet.ts +202 -202
  55. package/lib/wallet/wallets/MetamaskSnapWallet.ts +105 -105
  56. package/lib/wallet/wallets/MetamaskWallet.ts +173 -173
  57. package/lib/wallet/wallets/OKXWallet.ts +202 -202
  58. package/lib/wallet/wallets/UnisatWallet.ts +198 -198
  59. package/package.json +3 -3
  60. package/postcss.config.js +6 -6
  61. package/src/App.vue +225 -225
  62. package/src/main.ts +7 -4
  63. package/src/styles/design-system.css +150 -0
  64. package/src/vite-env.d.ts +1 -1
  65. package/tailwind.config.js +211 -34
  66. package/tsconfig.json +25 -25
  67. package/tsconfig.node.json +10 -10
  68. package/vite.config.ts +62 -62
  69. package/vue-shim.d.ts +6 -6
@@ -1,203 +1,203 @@
1
- import { fromBase64, fromBech32, toHex, toBech32, toBase64 } from "@cosmjs/encoding";
2
- import { Registry, TxBodyEncodeObject, encodePubkey, makeAuthInfoBytes } from "@cosmjs/proto-signing"
3
- import { AbstractWallet, Account, DEFAULT_HDPATH, WalletArgument, WalletName, extractChainId } from "../Wallet"
4
- import { AminoTypes, createDefaultAminoConverters } from "@cosmjs/stargate"
5
- import TransportWebUSB from "@ledgerhq/hw-transport-webusb"
6
- import TransportWebBLE from "@ledgerhq/hw-transport-web-ble"
7
- import { LedgerSigner } from "@cosmjs/ledger-amino"
8
- import { Transaction } from "../../utils/type"
9
- import { Chain, createTxRawEIP712, signatureToWeb3Extension } from "@tharsis/transactions";
10
- import { createEIP712, generateFee, generateMessageWithMultipleTransactions, generateTypes } from "@tharsis/eip712";
11
- import { defaultMessageAdapter } from "../EthermintMessageAdapter";
12
- import { createTransactionWithMultipleMessages } from "@tharsis/proto";
13
- import { encodeSecp256k1Pubkey, makeSignDoc as makeSignDocAmino } from "@cosmjs/amino";
14
- import { TxRaw } from "cosmjs-types/cosmos/tx/v1beta1/tx";
15
- import { stringToPath } from '@cosmjs/crypto'
16
- import { SignMode } from "cosmjs-types/cosmos/tx/signing/v1beta1/signing";
17
- import { ethermintToEth } from "../../utils/format";
18
- import { createWasmAminoConverters } from "@cosmjs/cosmwasm-stargate";
19
-
20
- export class LedgerWallet implements AbstractWallet {
21
- name: WalletName.Ledger
22
- transport: string
23
- hdPath: string
24
- registry: Registry
25
- aminoTypes = new AminoTypes({...createDefaultAminoConverters(), ...createWasmAminoConverters()})
26
- conf: WalletArgument
27
- constructor(arg: WalletArgument, registry: Registry) {
28
- this.transport = arg.transport || 'usb'
29
- this.hdPath = arg.hdPath || DEFAULT_HDPATH
30
- this.registry = registry
31
- this.conf = arg
32
- }
33
-
34
- async getSigner() {
35
- const transport = this.transport === 'usb' ? await TransportWebUSB.create() : await TransportWebBLE.create()
36
- // extract Cointype from from HDPath
37
- const hdPath = stringToPath(this.hdPath)
38
- const coinType = Number(hdPath[1])
39
- let ledgerAppName = 'Cosmos'
40
- switch (coinType) {
41
- case 60:
42
- throw new Error("Not support.")
43
- case 529:
44
- ledgerAppName = 'Secret' // 'Secret'
45
- break
46
- case 852:
47
- ledgerAppName = 'Desmos' // 'Desmos'
48
- break
49
- case 330:
50
- ledgerAppName = 'Terra' // 'Terra'
51
- break
52
- case 118:
53
- default:
54
- }
55
- // const path = stringToPath(this.hdPath)
56
- return new LedgerSigner(transport, { ledgerAppName, hdPaths: [hdPath] })
57
- }
58
-
59
- async getAccounts(): Promise<Account[]> {
60
- const signer = await this.getSigner();
61
- const account = (await signer.getAccounts()).map(x => ({
62
- address: x.address,
63
- algo: x.algo,
64
- pubkey: x.pubkey,
65
- }))
66
- return account
67
- }
68
- supportCoinType(coinType?: string | undefined): Promise<boolean> {
69
- return Promise.resolve(true);
70
- }
71
- isEthermint() {
72
- return this.conf.hdPath && this.conf.hdPath.startsWith("m/44'/60")
73
- }
74
-
75
- async sign(tx: Transaction): Promise<TxRaw | Uint8Array> {
76
- // if (this.isEthermint()) {
77
- // return this.sign712(tx)
78
- // }
79
- return this.signAmino(tx)
80
- }
81
-
82
- // not finished, due to the outdated dependency.
83
- async sign712(tx: Transaction): Promise<Uint8Array> {
84
- const chain: Chain = {
85
- chainId: extractChainId(tx.signerData.chainId),
86
- cosmosChainId: tx.chainId,
87
- }
88
-
89
- const signer = await this.getSigner()
90
- const ethAddr = ethermintToEth(tx.signerAddress)
91
-
92
- // this.signer.prefix = fromBech32(signerAddress).prefix
93
- const account = await signer.getAccounts()
94
-
95
- const acc = account.find(x => x.address === ethAddr)
96
- if (!acc) {
97
- throw new Error('The signer address dose not exsits in Ledger!')
98
- }
99
- const sender = {
100
- accountAddress: tx.signerAddress,
101
- sequence: tx.signerData.sequence,
102
- accountNumber: tx.signerData.accountNumber,
103
- pubkey: toBase64(account[0].pubkey),
104
- }
105
-
106
- const fees = generateFee(tx.fee.amount[0].amount, tx.fee.amount[0].denom, tx.fee.gas, tx.signerAddress)
107
-
108
- const msgs = tx.messages.map(x => this.aminoTypes.toAmino(x))
109
- const toSignTx = generateMessageWithMultipleTransactions(
110
- sender.accountNumber.toString(),
111
- sender.sequence.toString(),
112
- tx.signerData.chainId,
113
- tx.memo,
114
- fees,
115
- msgs,
116
- )
117
-
118
- const types = generateTypes(defaultMessageAdapter[tx.messages[0].typeUrl].getTypes())
119
- const eip = createEIP712(types, chain.chainId, toSignTx)
120
-
121
- // @ts-ignore
122
- const sig = await this.getSigner().sign712(eip)
123
-
124
- const rawTx = this.makeRawTxEvmos(sender, tx.messages, tx.memo, tx.fee, sig, chain)
125
- return rawTx
126
- }
127
-
128
- makeRawTxEvmos(sender, messages, memo, fee, signature, chain): Uint8Array {
129
- /// evmos style
130
- /// *
131
- const protoMsgs = messages.map(x => {
132
- const adapter = defaultMessageAdapter[x.typeUrl]
133
- return adapter.toProto(x)
134
- })
135
-
136
- const evmos = createTransactionWithMultipleMessages(
137
- protoMsgs,
138
- memo,
139
- fee.amount[0].amount,
140
- fee.amount[0].denom,
141
- Number(fee.gas),
142
- 'ethsecp256',
143
- sender.pubkey,
144
- sender.sequence,
145
- sender.accountNumber,
146
- chain.cosmosChainId,
147
- )
148
-
149
- const extension = signatureToWeb3Extension(chain, sender, signature)
150
-
151
- // Create the txRaw
152
- const prototx = createTxRawEIP712(evmos.legacyAmino.body, evmos.legacyAmino.authInfo, extension)
153
- return prototx.message.serializeBinary()
154
- /// end of EVMOS style */
155
- }
156
-
157
-
158
- async signAmino(tx: Transaction): Promise<TxRaw> {
159
- const signer = await this.getSigner();
160
- // assert(!isOfflineDirectSigner(signer));
161
-
162
- const accounts = await this.getAccounts()
163
- const {data} = fromBech32(tx.signerAddress)
164
- const hex = toHex(data)
165
- const signerAddress = toBech32("cosmos", data)
166
- const accountFromSigner = accounts.find((account) => toHex(fromBech32(account.address).data) === hex);
167
- if (!accountFromSigner) {
168
- throw new Error("Failed to retrieve account from signer");
169
- }
170
- const pubkey = encodePubkey(encodeSecp256k1Pubkey(accountFromSigner.pubkey));
171
- const signMode = SignMode.SIGN_MODE_LEGACY_AMINO_JSON;
172
- const msgs = tx.messages.map((msg) => this.aminoTypes.toAmino(msg));
173
- const signDoc = makeSignDocAmino(msgs, tx.fee, tx.chainId, tx.memo, tx.signerData.accountNumber, tx.signerData.sequence);
174
- const { signature, signed } = await signer.signAmino(signerAddress, signDoc);
175
-
176
- const signedTxBody = {
177
- messages: signed.msgs.map((msg) => this.aminoTypes.fromAmino(msg)),
178
- memo: signed.memo,
179
- };
180
- const signedTxBodyEncodeObject: TxBodyEncodeObject = {
181
- typeUrl: "/cosmos.tx.v1beta1.TxBody",
182
- value: signedTxBody,
183
- };
184
- const signedTxBodyBytes = this.registry.encode(signedTxBodyEncodeObject);
185
-
186
- const signedGasLimit = Number(signed.fee.gas);
187
- const signedSequence = Number(signed.sequence);
188
- const signedAuthInfoBytes = makeAuthInfoBytes(
189
- [{ pubkey, sequence: signedSequence }],
190
- signed.fee.amount,
191
- signedGasLimit,
192
- signed.fee.granter,
193
- signed.fee.payer,
194
- signMode,
195
- );
196
- return TxRaw.fromPartial({
197
- bodyBytes: signedTxBodyBytes,
198
- authInfoBytes: signedAuthInfoBytes,
199
- signatures: [fromBase64(signature.signature)],
200
- });
201
- }
202
-
1
+ import { fromBase64, fromBech32, toHex, toBech32, toBase64 } from "@cosmjs/encoding";
2
+ import { Registry, TxBodyEncodeObject, encodePubkey, makeAuthInfoBytes } from "@cosmjs/proto-signing"
3
+ import { AbstractWallet, Account, DEFAULT_HDPATH, WalletArgument, WalletName, extractChainId } from "../Wallet"
4
+ import { AminoTypes, createDefaultAminoConverters } from "@cosmjs/stargate"
5
+ import TransportWebUSB from "@ledgerhq/hw-transport-webusb"
6
+ import TransportWebBLE from "@ledgerhq/hw-transport-web-ble"
7
+ import { LedgerSigner } from "@cosmjs/ledger-amino"
8
+ import { Transaction } from "../../utils/type"
9
+ import { Chain, createTxRawEIP712, signatureToWeb3Extension } from "@tharsis/transactions";
10
+ import { createEIP712, generateFee, generateMessageWithMultipleTransactions, generateTypes } from "@tharsis/eip712";
11
+ import { defaultMessageAdapter } from "../EthermintMessageAdapter";
12
+ import { createTransactionWithMultipleMessages } from "@tharsis/proto";
13
+ import { encodeSecp256k1Pubkey, makeSignDoc as makeSignDocAmino } from "@cosmjs/amino";
14
+ import { TxRaw } from "cosmjs-types/cosmos/tx/v1beta1/tx";
15
+ import { stringToPath } from '@cosmjs/crypto'
16
+ import { SignMode } from "cosmjs-types/cosmos/tx/signing/v1beta1/signing";
17
+ import { ethermintToEth } from "../../utils/format";
18
+ import { createWasmAminoConverters } from "@cosmjs/cosmwasm-stargate";
19
+
20
+ export class LedgerWallet implements AbstractWallet {
21
+ name: WalletName.Ledger
22
+ transport: string
23
+ hdPath: string
24
+ registry: Registry
25
+ aminoTypes = new AminoTypes({...createDefaultAminoConverters(), ...createWasmAminoConverters()})
26
+ conf: WalletArgument
27
+ constructor(arg: WalletArgument, registry: Registry) {
28
+ this.transport = arg.transport || 'usb'
29
+ this.hdPath = arg.hdPath || DEFAULT_HDPATH
30
+ this.registry = registry
31
+ this.conf = arg
32
+ }
33
+
34
+ async getSigner() {
35
+ const transport = this.transport === 'usb' ? await TransportWebUSB.create() : await TransportWebBLE.create()
36
+ // extract Cointype from from HDPath
37
+ const hdPath = stringToPath(this.hdPath)
38
+ const coinType = Number(hdPath[1])
39
+ let ledgerAppName = 'Cosmos'
40
+ switch (coinType) {
41
+ case 60:
42
+ throw new Error("Not support.")
43
+ case 529:
44
+ ledgerAppName = 'Secret' // 'Secret'
45
+ break
46
+ case 852:
47
+ ledgerAppName = 'Desmos' // 'Desmos'
48
+ break
49
+ case 330:
50
+ ledgerAppName = 'Terra' // 'Terra'
51
+ break
52
+ case 118:
53
+ default:
54
+ }
55
+ // const path = stringToPath(this.hdPath)
56
+ return new LedgerSigner(transport, { ledgerAppName, hdPaths: [hdPath] })
57
+ }
58
+
59
+ async getAccounts(): Promise<Account[]> {
60
+ const signer = await this.getSigner();
61
+ const account = (await signer.getAccounts()).map(x => ({
62
+ address: x.address,
63
+ algo: x.algo,
64
+ pubkey: x.pubkey,
65
+ }))
66
+ return account
67
+ }
68
+ supportCoinType(coinType?: string | undefined): Promise<boolean> {
69
+ return Promise.resolve(true);
70
+ }
71
+ isEthermint() {
72
+ return this.conf.hdPath && this.conf.hdPath.startsWith("m/44'/60")
73
+ }
74
+
75
+ async sign(tx: Transaction): Promise<TxRaw | Uint8Array> {
76
+ // if (this.isEthermint()) {
77
+ // return this.sign712(tx)
78
+ // }
79
+ return this.signAmino(tx)
80
+ }
81
+
82
+ // not finished, due to the outdated dependency.
83
+ async sign712(tx: Transaction): Promise<Uint8Array> {
84
+ const chain: Chain = {
85
+ chainId: extractChainId(tx.signerData.chainId),
86
+ cosmosChainId: tx.chainId,
87
+ }
88
+
89
+ const signer = await this.getSigner()
90
+ const ethAddr = ethermintToEth(tx.signerAddress)
91
+
92
+ // this.signer.prefix = fromBech32(signerAddress).prefix
93
+ const account = await signer.getAccounts()
94
+
95
+ const acc = account.find(x => x.address === ethAddr)
96
+ if (!acc) {
97
+ throw new Error('The signer address dose not exsits in Ledger!')
98
+ }
99
+ const sender = {
100
+ accountAddress: tx.signerAddress,
101
+ sequence: tx.signerData.sequence,
102
+ accountNumber: tx.signerData.accountNumber,
103
+ pubkey: toBase64(account[0].pubkey),
104
+ }
105
+
106
+ const fees = generateFee(tx.fee.amount[0].amount, tx.fee.amount[0].denom, tx.fee.gas, tx.signerAddress)
107
+
108
+ const msgs = tx.messages.map(x => this.aminoTypes.toAmino(x))
109
+ const toSignTx = generateMessageWithMultipleTransactions(
110
+ sender.accountNumber.toString(),
111
+ sender.sequence.toString(),
112
+ tx.signerData.chainId,
113
+ tx.memo,
114
+ fees,
115
+ msgs,
116
+ )
117
+
118
+ const types = generateTypes(defaultMessageAdapter[tx.messages[0].typeUrl].getTypes())
119
+ const eip = createEIP712(types, chain.chainId, toSignTx)
120
+
121
+ // @ts-ignore
122
+ const sig = await this.getSigner().sign712(eip)
123
+
124
+ const rawTx = this.makeRawTxEvmos(sender, tx.messages, tx.memo, tx.fee, sig, chain)
125
+ return rawTx
126
+ }
127
+
128
+ makeRawTxEvmos(sender, messages, memo, fee, signature, chain): Uint8Array {
129
+ /// evmos style
130
+ /// *
131
+ const protoMsgs = messages.map(x => {
132
+ const adapter = defaultMessageAdapter[x.typeUrl]
133
+ return adapter.toProto(x)
134
+ })
135
+
136
+ const evmos = createTransactionWithMultipleMessages(
137
+ protoMsgs,
138
+ memo,
139
+ fee.amount[0].amount,
140
+ fee.amount[0].denom,
141
+ Number(fee.gas),
142
+ 'ethsecp256',
143
+ sender.pubkey,
144
+ sender.sequence,
145
+ sender.accountNumber,
146
+ chain.cosmosChainId,
147
+ )
148
+
149
+ const extension = signatureToWeb3Extension(chain, sender, signature)
150
+
151
+ // Create the txRaw
152
+ const prototx = createTxRawEIP712(evmos.legacyAmino.body, evmos.legacyAmino.authInfo, extension)
153
+ return prototx.message.serializeBinary()
154
+ /// end of EVMOS style */
155
+ }
156
+
157
+
158
+ async signAmino(tx: Transaction): Promise<TxRaw> {
159
+ const signer = await this.getSigner();
160
+ // assert(!isOfflineDirectSigner(signer));
161
+
162
+ const accounts = await this.getAccounts()
163
+ const {data} = fromBech32(tx.signerAddress)
164
+ const hex = toHex(data)
165
+ const signerAddress = toBech32("cosmos", data)
166
+ const accountFromSigner = accounts.find((account) => toHex(fromBech32(account.address).data) === hex);
167
+ if (!accountFromSigner) {
168
+ throw new Error("Failed to retrieve account from signer");
169
+ }
170
+ const pubkey = encodePubkey(encodeSecp256k1Pubkey(accountFromSigner.pubkey));
171
+ const signMode = SignMode.SIGN_MODE_LEGACY_AMINO_JSON;
172
+ const msgs = tx.messages.map((msg) => this.aminoTypes.toAmino(msg));
173
+ const signDoc = makeSignDocAmino(msgs, tx.fee, tx.chainId, tx.memo, tx.signerData.accountNumber, tx.signerData.sequence);
174
+ const { signature, signed } = await signer.signAmino(signerAddress, signDoc);
175
+
176
+ const signedTxBody = {
177
+ messages: signed.msgs.map((msg) => this.aminoTypes.fromAmino(msg)),
178
+ memo: signed.memo,
179
+ };
180
+ const signedTxBodyEncodeObject: TxBodyEncodeObject = {
181
+ typeUrl: "/cosmos.tx.v1beta1.TxBody",
182
+ value: signedTxBody,
183
+ };
184
+ const signedTxBodyBytes = this.registry.encode(signedTxBodyEncodeObject);
185
+
186
+ const signedGasLimit = Number(signed.fee.gas);
187
+ const signedSequence = Number(signed.sequence);
188
+ const signedAuthInfoBytes = makeAuthInfoBytes(
189
+ [{ pubkey, sequence: signedSequence }],
190
+ signed.fee.amount,
191
+ signedGasLimit,
192
+ signed.fee.granter,
193
+ signed.fee.payer,
194
+ signMode,
195
+ );
196
+ return TxRaw.fromPartial({
197
+ bodyBytes: signedTxBodyBytes,
198
+ authInfoBytes: signedAuthInfoBytes,
199
+ signatures: [fromBase64(signature.signature)],
200
+ });
201
+ }
202
+
203
203
  }
@@ -1,105 +1,105 @@
1
- import {
2
- AbstractWallet,
3
- Account,
4
- WalletArgument,
5
- WalletName,
6
- keyType,
7
- } from '../Wallet';
8
- import {
9
- fromBase64,
10
- fromBech32,
11
- toHex,
12
- } from '@cosmjs/encoding';
13
- import {
14
- Registry,
15
- TxBodyEncodeObject,
16
- makeAuthInfoBytes,
17
- makeSignDoc,
18
- } from '@cosmjs/proto-signing';
19
- import { Transaction } from '../../utils/type';
20
- import { TxRaw } from 'cosmjs-types/cosmos/tx/v1beta1/tx';
21
- // import { AminoTypes, createDefaultAminoConverters } from '@cosmjs/stargate';
22
- import { Any } from 'cosmjs-types/google/protobuf/any';
23
- import { PubKey } from 'cosmjs-types/cosmos/crypto/secp256k1/keys';
24
-
25
- import { connectSnap, getKey, getSnap, CosmjsOfflineSigner } from '@leapwallet/cosmos-snap-provider';
26
- // import { createWasmAminoConverters } from "@cosmjs/cosmwasm-stargate";
27
-
28
- export class MetamaskSnapWallet implements AbstractWallet {
29
- name: WalletName.MetamaskSnap;
30
- chainId: string;
31
- registry: Registry;
32
- prefix: string;
33
- // aminoTypes = new AminoTypes( {...createDefaultAminoConverters(), ...createWasmAminoConverters()});
34
-
35
- constructor(arg: WalletArgument, registry: Registry) {
36
- this.chainId = arg.chainId || 'cosmoshub';
37
- this.registry = registry;
38
- this.prefix = arg.prefix || 'cosmos';
39
- }
40
-
41
- async getAccounts(): Promise<Account[]> {
42
- const snapInstalled = await getSnap().catch(() => {
43
- throw new Error('Metamask snap not installed');
44
- });
45
- if (!snapInstalled) {
46
- await connectSnap()
47
- }
48
- const key = await getKey(this.chainId)
49
- return [key]
50
- }
51
-
52
- async supportCoinType(coinType?: string): Promise<boolean> {
53
- return true;
54
- }
55
-
56
- async sign(transaction: Transaction): Promise<TxRaw | Uint8Array> {
57
- const accouts = await this.getAccounts();
58
- const hex = toHex(fromBech32(transaction.signerAddress).data);
59
- const accountFromSigner = accouts.find(
60
- (account) => toHex(fromBech32(account.address).data) === hex
61
- );
62
- if (!accountFromSigner) {
63
- throw new Error('Failed to retrieve account from signer');
64
- }
65
-
66
- const pubkey = Any.fromPartial({
67
- typeUrl: keyType(transaction.chainId),
68
- value: PubKey.encode({
69
- key: new Uint8Array(Object.values(accountFromSigner.pubkey)),
70
- }).finish(),
71
- });
72
- const txBodyEncodeObject: TxBodyEncodeObject = {
73
- typeUrl: '/cosmos.tx.v1beta1.TxBody',
74
- value: {
75
- messages: transaction.messages,
76
- memo: transaction.memo,
77
- },
78
- };
79
- const txBodyBytes = this.registry.encode(txBodyEncodeObject);
80
- const gasLimit = Number(transaction.fee.gas);
81
-
82
- const authInfoBytes = makeAuthInfoBytes(
83
- [{ pubkey, sequence: transaction.signerData.sequence }],
84
- transaction.fee.amount,
85
- gasLimit,
86
- transaction.fee.granter,
87
- transaction.fee.payer
88
- );
89
- const signDoc = makeSignDoc(
90
- txBodyBytes,
91
- authInfoBytes,
92
- transaction.chainId,
93
- transaction.signerData.accountNumber
94
- );
95
-
96
- const offlineSigner = new CosmjsOfflineSigner(this.chainId);
97
- const { signature, signed } = await offlineSigner.signDirect(transaction.signerAddress, signDoc);
98
-
99
- return TxRaw.fromPartial({
100
- bodyBytes: signed.bodyBytes,
101
- authInfoBytes: signed.authInfoBytes,
102
- signatures: [fromBase64(signature.signature)],
103
- });
104
- }
105
- }
1
+ import {
2
+ AbstractWallet,
3
+ Account,
4
+ WalletArgument,
5
+ WalletName,
6
+ keyType,
7
+ } from '../Wallet';
8
+ import {
9
+ fromBase64,
10
+ fromBech32,
11
+ toHex,
12
+ } from '@cosmjs/encoding';
13
+ import {
14
+ Registry,
15
+ TxBodyEncodeObject,
16
+ makeAuthInfoBytes,
17
+ makeSignDoc,
18
+ } from '@cosmjs/proto-signing';
19
+ import { Transaction } from '../../utils/type';
20
+ import { TxRaw } from 'cosmjs-types/cosmos/tx/v1beta1/tx';
21
+ // import { AminoTypes, createDefaultAminoConverters } from '@cosmjs/stargate';
22
+ import { Any } from 'cosmjs-types/google/protobuf/any';
23
+ import { PubKey } from 'cosmjs-types/cosmos/crypto/secp256k1/keys';
24
+
25
+ import { connectSnap, getKey, getSnap, CosmjsOfflineSigner } from '@leapwallet/cosmos-snap-provider';
26
+ // import { createWasmAminoConverters } from "@cosmjs/cosmwasm-stargate";
27
+
28
+ export class MetamaskSnapWallet implements AbstractWallet {
29
+ name: WalletName.MetamaskSnap;
30
+ chainId: string;
31
+ registry: Registry;
32
+ prefix: string;
33
+ // aminoTypes = new AminoTypes( {...createDefaultAminoConverters(), ...createWasmAminoConverters()});
34
+
35
+ constructor(arg: WalletArgument, registry: Registry) {
36
+ this.chainId = arg.chainId || 'cosmoshub';
37
+ this.registry = registry;
38
+ this.prefix = arg.prefix || 'cosmos';
39
+ }
40
+
41
+ async getAccounts(): Promise<Account[]> {
42
+ const snapInstalled = await getSnap().catch(() => {
43
+ throw new Error('Metamask snap not installed');
44
+ });
45
+ if (!snapInstalled) {
46
+ await connectSnap()
47
+ }
48
+ const key = await getKey(this.chainId)
49
+ return [key]
50
+ }
51
+
52
+ async supportCoinType(coinType?: string): Promise<boolean> {
53
+ return true;
54
+ }
55
+
56
+ async sign(transaction: Transaction): Promise<TxRaw | Uint8Array> {
57
+ const accouts = await this.getAccounts();
58
+ const hex = toHex(fromBech32(transaction.signerAddress).data);
59
+ const accountFromSigner = accouts.find(
60
+ (account) => toHex(fromBech32(account.address).data) === hex
61
+ );
62
+ if (!accountFromSigner) {
63
+ throw new Error('Failed to retrieve account from signer');
64
+ }
65
+
66
+ const pubkey = Any.fromPartial({
67
+ typeUrl: keyType(transaction.chainId),
68
+ value: PubKey.encode({
69
+ key: new Uint8Array(Object.values(accountFromSigner.pubkey)),
70
+ }).finish(),
71
+ });
72
+ const txBodyEncodeObject: TxBodyEncodeObject = {
73
+ typeUrl: '/cosmos.tx.v1beta1.TxBody',
74
+ value: {
75
+ messages: transaction.messages,
76
+ memo: transaction.memo,
77
+ },
78
+ };
79
+ const txBodyBytes = this.registry.encode(txBodyEncodeObject);
80
+ const gasLimit = Number(transaction.fee.gas);
81
+
82
+ const authInfoBytes = makeAuthInfoBytes(
83
+ [{ pubkey, sequence: transaction.signerData.sequence }],
84
+ transaction.fee.amount,
85
+ gasLimit,
86
+ transaction.fee.granter,
87
+ transaction.fee.payer
88
+ );
89
+ const signDoc = makeSignDoc(
90
+ txBodyBytes,
91
+ authInfoBytes,
92
+ transaction.chainId,
93
+ transaction.signerData.accountNumber
94
+ );
95
+
96
+ const offlineSigner = new CosmjsOfflineSigner(this.chainId);
97
+ const { signature, signed } = await offlineSigner.signDirect(transaction.signerAddress, signDoc);
98
+
99
+ return TxRaw.fromPartial({
100
+ bodyBytes: signed.bodyBytes,
101
+ authInfoBytes: signed.authInfoBytes,
102
+ signatures: [fromBase64(signature.signature)],
103
+ });
104
+ }
105
+ }