@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,173 @@
|
|
|
1
|
+
import { AbstractWallet, Account, WalletArgument, WalletName, extractChainId, keyType } from '../Wallet';
|
|
2
|
+
import { fromBase64, fromBech32, toHex, fromHex, toBase64 } from '@cosmjs/encoding';
|
|
3
|
+
import {
|
|
4
|
+
Registry,
|
|
5
|
+
encodePubkey,
|
|
6
|
+
makeAuthInfoBytes,
|
|
7
|
+
} from '@cosmjs/proto-signing';
|
|
8
|
+
import { Transaction } from '../../utils/type';
|
|
9
|
+
import { TxRaw } from 'cosmjs-types/cosmos/tx/v1beta1/tx';
|
|
10
|
+
import { hashMessage } from '@ethersproject/hash';
|
|
11
|
+
import { computePublicKey, recoverPublicKey } from '@ethersproject/signing-key';
|
|
12
|
+
import { ethToEthermint, ethermintToEth } from '../../utils/format';
|
|
13
|
+
import { AminoTypes, createDefaultAminoConverters } from "@cosmjs/stargate"
|
|
14
|
+
import { Chain, createTxRawEIP712, signatureToWeb3Extension } from "@tharsis/transactions";
|
|
15
|
+
import { createEIP712, generateFee, generateMessageWithMultipleTransactions, generateTypes } from "@tharsis/eip712";
|
|
16
|
+
import { defaultMessageAdapter } from '../EthermintMessageAdapter';
|
|
17
|
+
import { Any } from "cosmjs-types/google/protobuf/any";
|
|
18
|
+
import { PubKey } from 'cosmjs-types/cosmos/crypto/secp256k1/keys'
|
|
19
|
+
|
|
20
|
+
export class MetamaskWallet implements AbstractWallet {
|
|
21
|
+
name: WalletName.Metamask;
|
|
22
|
+
chainId: string;
|
|
23
|
+
registry: Registry;
|
|
24
|
+
prefix: string;
|
|
25
|
+
aminoTypes = new AminoTypes(createDefaultAminoConverters())
|
|
26
|
+
|
|
27
|
+
constructor(arg: WalletArgument, registry: Registry) {
|
|
28
|
+
this.chainId = arg.chainId || 'cosmoshub';
|
|
29
|
+
this.registry = registry;
|
|
30
|
+
this.prefix = arg.prefix || 'evmos'
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
async getAccounts(): Promise<Account[]> {
|
|
34
|
+
// @ts-ignore
|
|
35
|
+
if (!window.ethereum || !window.ethereum.request) {
|
|
36
|
+
throw new Error('Please install Metamask extension');
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// @ts-ignore46
|
|
40
|
+
const accounts = await window.ethereum.request({
|
|
41
|
+
method: 'eth_requestAccounts',
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
const message = 'Verify Public Key';
|
|
45
|
+
// @ts-ignore
|
|
46
|
+
const signature = await window?.ethereum?.request({
|
|
47
|
+
method: 'personal_sign',
|
|
48
|
+
params: [message, accounts[0], ''],
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
const uncompressedPk = recoverPublicKey(
|
|
52
|
+
hashMessage(message),
|
|
53
|
+
signature
|
|
54
|
+
);
|
|
55
|
+
const hexPk = computePublicKey(uncompressedPk, true);
|
|
56
|
+
const pk = toBase64(fromHex(hexPk.replace('0x', '')));
|
|
57
|
+
|
|
58
|
+
const connected = accounts.map((address) => ({
|
|
59
|
+
address: ethToEthermint(address, this.prefix),
|
|
60
|
+
algo: 'secp256k1',
|
|
61
|
+
pubkey: pk,
|
|
62
|
+
}))
|
|
63
|
+
|
|
64
|
+
localStorage.setItem("metamask-connected", JSON.stringify(connected))
|
|
65
|
+
|
|
66
|
+
return connected;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
async getConnectedAccounts() {
|
|
70
|
+
const connected = localStorage.getItem("metamask-connected")
|
|
71
|
+
if(connected) {
|
|
72
|
+
return JSON.parse(connected) as Account[]
|
|
73
|
+
}
|
|
74
|
+
return this.getAccounts()
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
async supportCoinType(coinType?: string): Promise<boolean> {
|
|
78
|
+
// Here, you can check if Metamask supports a specific type of cryptocurrency.
|
|
79
|
+
return true;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
async sign(transaction: Transaction): Promise<TxRaw | Uint8Array> {
|
|
83
|
+
const { signerAddress, messages, fee, signerData } =
|
|
84
|
+
transaction;
|
|
85
|
+
|
|
86
|
+
const senderHexAddress = ethermintToEth(signerAddress);
|
|
87
|
+
const chain: Chain = {
|
|
88
|
+
chainId: extractChainId(signerData.chainId),
|
|
89
|
+
cosmosChainId: signerData.chainId,
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// getAccounts
|
|
93
|
+
const accounts = await this.getConnectedAccounts();
|
|
94
|
+
const account = accounts.find(acc => ethermintToEth(acc.address) === senderHexAddress );
|
|
95
|
+
if (!account) {
|
|
96
|
+
throw new Error('Account not found');
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
console.log("account:", account)
|
|
100
|
+
|
|
101
|
+
const pubkeyBytes = String(account.pubkey)
|
|
102
|
+
console.log("toBase64(pubkeyBytes)", pubkeyBytes)
|
|
103
|
+
|
|
104
|
+
const sender = {
|
|
105
|
+
accountAddress: transaction.signerAddress,
|
|
106
|
+
sequence: transaction.signerData.sequence,
|
|
107
|
+
accountNumber: transaction.signerData.accountNumber,
|
|
108
|
+
pubkey: pubkeyBytes,
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const fees = generateFee(transaction.fee.amount[0].amount, transaction.fee.amount[0].denom, transaction.fee.gas, transaction.signerAddress)
|
|
112
|
+
|
|
113
|
+
const msgs = transaction.messages.map(x => this.aminoTypes.toAmino(x))
|
|
114
|
+
const toSignTx = generateMessageWithMultipleTransactions(
|
|
115
|
+
sender.accountNumber.toString(),
|
|
116
|
+
sender.sequence.toString(),
|
|
117
|
+
transaction.signerData.chainId,
|
|
118
|
+
transaction.memo,
|
|
119
|
+
fees,
|
|
120
|
+
msgs,
|
|
121
|
+
)
|
|
122
|
+
|
|
123
|
+
const types = generateTypes(defaultMessageAdapter[transaction.messages[0].typeUrl].getTypes())
|
|
124
|
+
const eip712Payload = createEIP712(types, chain.chainId, toSignTx)
|
|
125
|
+
|
|
126
|
+
// Obtaining the data required for MetaMask signature
|
|
127
|
+
// @ts-ignore
|
|
128
|
+
// const acc = await window.ethereum.request({ method: 'eth_requestAccounts' });
|
|
129
|
+
// console.log("accounts: ", acc)
|
|
130
|
+
|
|
131
|
+
// const eip712Payload = JSON.stringify(messages[0]);
|
|
132
|
+
|
|
133
|
+
// Signing an EIP-712 payload using MetaMask.
|
|
134
|
+
// @ts-ignore
|
|
135
|
+
const signature = await window.ethereum.request({
|
|
136
|
+
method: 'eth_signTypedData_v4',
|
|
137
|
+
params: [senderHexAddress, JSON.stringify(eip712Payload)],
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
// Creating a TxRaw object after signing.
|
|
141
|
+
const signatureBytes = fromHex(signature.replace('0x', ''));
|
|
142
|
+
const txBodyBytes = this.registry.encode({
|
|
143
|
+
typeUrl: '/cosmos.tx.v1beta1.TxBody',
|
|
144
|
+
value: { messages },
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
// NOTE: assume ethsecp256k1 by default because after mainnet is the only one that is going to be supported
|
|
150
|
+
const pubkey = Any.fromPartial({
|
|
151
|
+
typeUrl: keyType(signerData.chainId),
|
|
152
|
+
value: PubKey.encode({
|
|
153
|
+
key: fromBase64(pubkeyBytes),
|
|
154
|
+
}).finish()
|
|
155
|
+
})
|
|
156
|
+
const authInfoBytes = makeAuthInfoBytes(
|
|
157
|
+
[{ pubkey, sequence: signerData.sequence }],
|
|
158
|
+
fee.amount,
|
|
159
|
+
Number(fee.gas),
|
|
160
|
+
undefined, // feeGranter
|
|
161
|
+
undefined // feePayer
|
|
162
|
+
);
|
|
163
|
+
|
|
164
|
+
// Using TxRaw.fromPartial to create a TxRaw object with attribute values.
|
|
165
|
+
const signedTx: TxRaw = TxRaw.fromPartial({
|
|
166
|
+
bodyBytes: txBodyBytes,
|
|
167
|
+
authInfoBytes: authInfoBytes,
|
|
168
|
+
signatures: [signatureBytes],
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
return signedTx; // Create the txRaw
|
|
172
|
+
}
|
|
173
|
+
}
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
import { fromBase64, fromHex } from '@cosmjs/encoding';
|
|
2
|
+
import { makeAuthInfoBytes, makeSignBytes, makeSignDoc, Registry, TxBodyEncodeObject } from '@cosmjs/proto-signing';
|
|
3
|
+
|
|
4
|
+
import { serializeSignDoc } from '@cosmjs/amino/build/signdoc';
|
|
5
|
+
|
|
6
|
+
import { AbstractWallet, Account, IChain, WalletArgument, WalletName } from '../Wallet';
|
|
7
|
+
import { Transaction } from '../../utils/type';
|
|
8
|
+
import { TxRaw } from 'cosmjs-types/cosmos/tx/v1beta1/tx';
|
|
9
|
+
import { Any } from 'cosmjs-types/google/protobuf/any';
|
|
10
|
+
import { PubKey } from 'cosmjs-types/cosmos/crypto/secp256k1/keys';
|
|
11
|
+
import { SignMode } from 'cosmjs-types/cosmos/tx/signing/v1beta1/signing';
|
|
12
|
+
import { AminoTypes, createDefaultAminoConverters, createIbcAminoConverters } from '@cosmjs/stargate';
|
|
13
|
+
import { makeSignDoc as makeSignDocAmino } from '@cosmjs/amino';
|
|
14
|
+
import { createWasmAminoConverters } from '@cosmjs/cosmwasm-stargate';
|
|
15
|
+
import { Buffer } from 'buffer';
|
|
16
|
+
|
|
17
|
+
export class OKXWallet implements AbstractWallet {
|
|
18
|
+
name: WalletName.OKX = WalletName.OKX;
|
|
19
|
+
chainId: string;
|
|
20
|
+
chain: IChain;
|
|
21
|
+
registry: Registry;
|
|
22
|
+
conf: WalletArgument;
|
|
23
|
+
aminoTypes = new AminoTypes({
|
|
24
|
+
...createDefaultAminoConverters(),
|
|
25
|
+
...createIbcAminoConverters(),
|
|
26
|
+
...createWasmAminoConverters(),
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
connectEventNamesOnWindow: string[] = [];
|
|
30
|
+
|
|
31
|
+
constructor(arg: WalletArgument, chain: IChain, registry: Registry) {
|
|
32
|
+
this.chainId = arg.chainId || "cosmoshub";
|
|
33
|
+
// @ts-ignore
|
|
34
|
+
|
|
35
|
+
this.registry = registry;
|
|
36
|
+
this.conf = arg;
|
|
37
|
+
this.chain = chain;
|
|
38
|
+
}
|
|
39
|
+
async getAccounts(): Promise<Account[]> {
|
|
40
|
+
// @ts-ignore
|
|
41
|
+
|
|
42
|
+
if (!window?.okxwallet) {
|
|
43
|
+
throw new Error("Please install OKX wallet extension");
|
|
44
|
+
}
|
|
45
|
+
// @ts-ignore
|
|
46
|
+
const connectRes = await window.okxwallet.bitcoin.connect();
|
|
47
|
+
console.log("connectRes: ", connectRes);
|
|
48
|
+
|
|
49
|
+
// @ts-ignore
|
|
50
|
+
const accounts = await window.okxwallet.bitcoin.getAccounts();
|
|
51
|
+
|
|
52
|
+
// @ts-ignore
|
|
53
|
+
const pbk = await window.okxwallet.bitcoin.getPublicKey();
|
|
54
|
+
|
|
55
|
+
return [
|
|
56
|
+
{
|
|
57
|
+
address: accounts[0],
|
|
58
|
+
pubkey: fromHex(pbk),
|
|
59
|
+
algo: 'segwit',
|
|
60
|
+
},
|
|
61
|
+
];
|
|
62
|
+
}
|
|
63
|
+
supportCoinType(): Promise<boolean> {
|
|
64
|
+
return Promise.resolve(true);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
isEthermint() {
|
|
68
|
+
return this.conf.hdPath && this.conf.hdPath.startsWith("m/44'/60");
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
async sign(transaction: Transaction): Promise<TxRaw> {
|
|
72
|
+
// return this.signDirect(transaction);
|
|
73
|
+
return this.signAmino(transaction);
|
|
74
|
+
}
|
|
75
|
+
// @deprecated use signAmino instead
|
|
76
|
+
// because signDirect is not supported ledger wallet
|
|
77
|
+
async signDirect(transaction: Transaction): Promise<TxRaw> {
|
|
78
|
+
const accouts = await this.getAccounts();
|
|
79
|
+
|
|
80
|
+
const accountFromSigner = accouts[0];
|
|
81
|
+
if (!accountFromSigner) {
|
|
82
|
+
throw new Error("Failed to retrieve account from signer");
|
|
83
|
+
}
|
|
84
|
+
const pubkey = Any.fromPartial({
|
|
85
|
+
typeUrl: "/cosmos.crypto.segwit.PubKey",
|
|
86
|
+
value: PubKey.encode({
|
|
87
|
+
key: accountFromSigner.pubkey,
|
|
88
|
+
}).finish(),
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
const txBodyEncodeObject: TxBodyEncodeObject = {
|
|
92
|
+
typeUrl: "/cosmos.tx.v1beta1.TxBody",
|
|
93
|
+
value: {
|
|
94
|
+
messages: transaction.messages,
|
|
95
|
+
memo: transaction.memo,
|
|
96
|
+
},
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
const txBodyBytes = this.registry.encode(txBodyEncodeObject);
|
|
100
|
+
const gasLimit = Number(transaction.fee.gas);
|
|
101
|
+
const authInfoBytes = makeAuthInfoBytes(
|
|
102
|
+
[{ pubkey, sequence: transaction.signerData.sequence }],
|
|
103
|
+
transaction.fee.amount,
|
|
104
|
+
gasLimit,
|
|
105
|
+
transaction.fee.granter,
|
|
106
|
+
transaction.fee.payer
|
|
107
|
+
);
|
|
108
|
+
|
|
109
|
+
const signDoc = makeSignDoc(
|
|
110
|
+
txBodyBytes,
|
|
111
|
+
authInfoBytes,
|
|
112
|
+
transaction.chainId,
|
|
113
|
+
transaction.signerData.accountNumber
|
|
114
|
+
);
|
|
115
|
+
console.log("signDoc: ", signDoc);
|
|
116
|
+
|
|
117
|
+
const signDocBuffer = makeSignBytes(signDoc);
|
|
118
|
+
|
|
119
|
+
const signString = Buffer.from(signDocBuffer).toString();
|
|
120
|
+
|
|
121
|
+
// @ts-ignore
|
|
122
|
+
const signature = await window.okxwallet.bitcoin.signMessage(
|
|
123
|
+
signString,
|
|
124
|
+
"ecdsa"
|
|
125
|
+
);
|
|
126
|
+
|
|
127
|
+
const signed = signDoc;
|
|
128
|
+
|
|
129
|
+
return TxRaw.fromPartial({
|
|
130
|
+
bodyBytes: signed.bodyBytes,
|
|
131
|
+
authInfoBytes: signed.authInfoBytes,
|
|
132
|
+
signatures: [fromBase64(signature)],
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
async signAmino(tx: Transaction): Promise<TxRaw> {
|
|
137
|
+
const accouts = await this.getAccounts();
|
|
138
|
+
|
|
139
|
+
const accountFromSigner = accouts[0];
|
|
140
|
+
if (!accountFromSigner) {
|
|
141
|
+
throw new Error("Failed to retrieve account from signer");
|
|
142
|
+
}
|
|
143
|
+
const pubkey = Any.fromPartial({
|
|
144
|
+
typeUrl: "/cosmos.crypto.segwit.PubKey",
|
|
145
|
+
value: PubKey.encode({
|
|
146
|
+
key: accountFromSigner.pubkey,
|
|
147
|
+
}).finish(),
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
const signMode = SignMode.SIGN_MODE_LEGACY_AMINO_JSON;
|
|
151
|
+
const msgs = tx.messages.map((msg) => this.aminoTypes.toAmino(msg));
|
|
152
|
+
const signDoc = makeSignDocAmino(
|
|
153
|
+
msgs,
|
|
154
|
+
tx.fee,
|
|
155
|
+
tx.chainId,
|
|
156
|
+
tx.memo,
|
|
157
|
+
tx.signerData.accountNumber,
|
|
158
|
+
tx.signerData.sequence
|
|
159
|
+
);
|
|
160
|
+
|
|
161
|
+
const signed = signDoc;
|
|
162
|
+
|
|
163
|
+
const signDocBuffer = serializeSignDoc(signDoc);
|
|
164
|
+
|
|
165
|
+
const signString = Buffer.from(signDocBuffer).toString();
|
|
166
|
+
|
|
167
|
+
// @ts-ignore
|
|
168
|
+
const signature = await window.okxwallet.bitcoin.signMessage(
|
|
169
|
+
signString,
|
|
170
|
+
"ecdsa"
|
|
171
|
+
);
|
|
172
|
+
|
|
173
|
+
const signedTxBody = {
|
|
174
|
+
messages: signed.msgs.map((msg) => this.aminoTypes.fromAmino(msg)),
|
|
175
|
+
memo: signed.memo,
|
|
176
|
+
};
|
|
177
|
+
|
|
178
|
+
const signedTxBodyEncodeObject: TxBodyEncodeObject = {
|
|
179
|
+
typeUrl: "/cosmos.tx.v1beta1.TxBody",
|
|
180
|
+
value: signedTxBody,
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
const signedTxBodyBytes = this.registry.encode(signedTxBodyEncodeObject);
|
|
184
|
+
|
|
185
|
+
const signedGasLimit = Number(signed.fee.gas);
|
|
186
|
+
const signedSequence = Number(signed.sequence);
|
|
187
|
+
const signedAuthInfoBytes = makeAuthInfoBytes(
|
|
188
|
+
[{ pubkey, sequence: signedSequence }],
|
|
189
|
+
signed.fee.amount,
|
|
190
|
+
signedGasLimit,
|
|
191
|
+
signed.fee.granter,
|
|
192
|
+
signed.fee.payer,
|
|
193
|
+
signMode
|
|
194
|
+
);
|
|
195
|
+
|
|
196
|
+
return TxRaw.fromPartial({
|
|
197
|
+
bodyBytes: signedTxBodyBytes,
|
|
198
|
+
authInfoBytes: signedAuthInfoBytes,
|
|
199
|
+
signatures: [fromBase64(signature)],
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
}
|
|
@@ -0,0 +1,198 @@
|
|
|
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 } from '../Wallet';
|
|
5
|
+
import { Transaction } 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
|
+
|
|
15
|
+
import { serializeSignDoc } from '@cosmjs/amino/build/signdoc';
|
|
16
|
+
|
|
17
|
+
export class UnisatWallet implements AbstractWallet {
|
|
18
|
+
name: WalletName.Unisat = WalletName.Unisat;
|
|
19
|
+
chainId: string;
|
|
20
|
+
chain: IChain;
|
|
21
|
+
registry: Registry;
|
|
22
|
+
conf: WalletArgument;
|
|
23
|
+
aminoTypes = new AminoTypes({
|
|
24
|
+
...createDefaultAminoConverters(),
|
|
25
|
+
...createIbcAminoConverters(),
|
|
26
|
+
...createWasmAminoConverters(),
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
connectEventNamesOnWindow: string[] = [];
|
|
30
|
+
|
|
31
|
+
constructor(arg: WalletArgument, chain: IChain, registry: Registry) {
|
|
32
|
+
this.chainId = arg.chainId || "cosmoshub";
|
|
33
|
+
// @ts-ignore
|
|
34
|
+
|
|
35
|
+
this.registry = registry;
|
|
36
|
+
this.conf = arg;
|
|
37
|
+
this.chain = chain;
|
|
38
|
+
}
|
|
39
|
+
async getAccounts(): Promise<Account[]> {
|
|
40
|
+
// @ts-ignore
|
|
41
|
+
|
|
42
|
+
if (typeof window?.unisat === "undefined") {
|
|
43
|
+
throw new Error("Please install UniSat wallet extension");
|
|
44
|
+
}
|
|
45
|
+
// @ts-ignore
|
|
46
|
+
await window.unisat.requestAccounts();
|
|
47
|
+
|
|
48
|
+
// @ts-ignore
|
|
49
|
+
const accounts = await window.unisat.getAccounts();
|
|
50
|
+
|
|
51
|
+
// @ts-ignore
|
|
52
|
+
const pbk = await window.unisat.getPublicKey();
|
|
53
|
+
|
|
54
|
+
return [
|
|
55
|
+
{
|
|
56
|
+
address: accounts[0],
|
|
57
|
+
pubkey: fromHex(pbk),
|
|
58
|
+
algo: "segwit",
|
|
59
|
+
},
|
|
60
|
+
];
|
|
61
|
+
}
|
|
62
|
+
supportCoinType(): Promise<boolean> {
|
|
63
|
+
return Promise.resolve(true);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
isEthermint() {
|
|
67
|
+
return this.conf.hdPath && this.conf.hdPath.startsWith("m/44'/60");
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
async sign(transaction: Transaction): Promise<TxRaw> {
|
|
71
|
+
return this.signAmino(transaction);
|
|
72
|
+
}
|
|
73
|
+
// @deprecated use signAmino instead
|
|
74
|
+
// because signDirect is not supported ledger wallet
|
|
75
|
+
async signDirect(transaction: Transaction): Promise<TxRaw> {
|
|
76
|
+
const accouts = await this.getAccounts();
|
|
77
|
+
|
|
78
|
+
const accountFromSigner = accouts[0];
|
|
79
|
+
if (!accountFromSigner) {
|
|
80
|
+
throw new Error("Failed to retrieve account from signer");
|
|
81
|
+
}
|
|
82
|
+
const pubkey = Any.fromPartial({
|
|
83
|
+
typeUrl: "/cosmos.crypto.segwit.PubKey",
|
|
84
|
+
value: PubKey.encode({
|
|
85
|
+
key: accountFromSigner.pubkey,
|
|
86
|
+
}).finish(),
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
const txBodyEncodeObject: TxBodyEncodeObject = {
|
|
90
|
+
typeUrl: "/cosmos.tx.v1beta1.TxBody",
|
|
91
|
+
value: {
|
|
92
|
+
messages: transaction.messages,
|
|
93
|
+
memo: transaction.memo,
|
|
94
|
+
},
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
const txBodyBytes = this.registry.encode(txBodyEncodeObject);
|
|
98
|
+
console.log("txBodyBytes: ", this.registry);
|
|
99
|
+
const gasLimit = Number(transaction.fee.gas);
|
|
100
|
+
const authInfoBytes = makeAuthInfoBytes(
|
|
101
|
+
[{ pubkey, sequence: transaction.signerData.sequence }],
|
|
102
|
+
transaction.fee.amount,
|
|
103
|
+
gasLimit,
|
|
104
|
+
transaction.fee.granter,
|
|
105
|
+
transaction.fee.payer
|
|
106
|
+
);
|
|
107
|
+
|
|
108
|
+
const signDoc = makeSignDoc(
|
|
109
|
+
txBodyBytes,
|
|
110
|
+
authInfoBytes,
|
|
111
|
+
transaction.chainId,
|
|
112
|
+
transaction.signerData.accountNumber
|
|
113
|
+
);
|
|
114
|
+
console.log("signDoc: ", signDoc);
|
|
115
|
+
|
|
116
|
+
const signDocBuffer = makeSignBytes(signDoc);
|
|
117
|
+
|
|
118
|
+
const signString = new TextDecoder().decode(signDocBuffer);
|
|
119
|
+
|
|
120
|
+
// @ts-ignore
|
|
121
|
+
const signature = await window.unisat.signMessage(signString);
|
|
122
|
+
console.log("result22: ", signature);
|
|
123
|
+
|
|
124
|
+
const signed = signDoc;
|
|
125
|
+
console.log("signed: ", signed);
|
|
126
|
+
|
|
127
|
+
return TxRaw.fromPartial({
|
|
128
|
+
bodyBytes: signed.bodyBytes,
|
|
129
|
+
authInfoBytes: signed.authInfoBytes,
|
|
130
|
+
signatures: [fromBase64(signature)],
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
async signAmino(tx: Transaction): Promise<TxRaw> {
|
|
135
|
+
const accouts = await this.getAccounts();
|
|
136
|
+
|
|
137
|
+
const accountFromSigner = accouts[0];
|
|
138
|
+
if (!accountFromSigner) {
|
|
139
|
+
throw new Error("Failed to retrieve account from signer");
|
|
140
|
+
}
|
|
141
|
+
const pubkey = Any.fromPartial({
|
|
142
|
+
typeUrl: "/cosmos.crypto.segwit.PubKey",
|
|
143
|
+
value: PubKey.encode({
|
|
144
|
+
key: accountFromSigner.pubkey,
|
|
145
|
+
}).finish(),
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
const signMode = SignMode.SIGN_MODE_LEGACY_AMINO_JSON;
|
|
149
|
+
const msgs = tx.messages.map((msg) => this.aminoTypes.toAmino(msg));
|
|
150
|
+
const signDoc = makeSignDocAmino(
|
|
151
|
+
msgs,
|
|
152
|
+
tx.fee,
|
|
153
|
+
tx.chainId,
|
|
154
|
+
tx.memo,
|
|
155
|
+
tx.signerData.accountNumber,
|
|
156
|
+
tx.signerData.sequence
|
|
157
|
+
);
|
|
158
|
+
|
|
159
|
+
const signed = signDoc;
|
|
160
|
+
|
|
161
|
+
const signDocBuffer = serializeSignDoc(signDoc);
|
|
162
|
+
|
|
163
|
+
const signString = Buffer.from(signDocBuffer).toString();
|
|
164
|
+
|
|
165
|
+
// @ts-ignore
|
|
166
|
+
const signature = await window.unisat.signMessage(signString);
|
|
167
|
+
console.log("signature: ", signature);
|
|
168
|
+
|
|
169
|
+
const signedTxBody = {
|
|
170
|
+
messages: signed.msgs.map((msg) => this.aminoTypes.fromAmino(msg)),
|
|
171
|
+
memo: signed.memo,
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
const signedTxBodyEncodeObject: TxBodyEncodeObject = {
|
|
175
|
+
typeUrl: "/cosmos.tx.v1beta1.TxBody",
|
|
176
|
+
value: signedTxBody,
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
const signedTxBodyBytes = this.registry.encode(signedTxBodyEncodeObject);
|
|
180
|
+
|
|
181
|
+
const signedGasLimit = Number(signed.fee.gas);
|
|
182
|
+
const signedSequence = Number(signed.sequence);
|
|
183
|
+
const signedAuthInfoBytes = makeAuthInfoBytes(
|
|
184
|
+
[{ pubkey, sequence: signedSequence }],
|
|
185
|
+
signed.fee.amount,
|
|
186
|
+
signedGasLimit,
|
|
187
|
+
signed.fee.granter,
|
|
188
|
+
signed.fee.payer,
|
|
189
|
+
signMode
|
|
190
|
+
);
|
|
191
|
+
|
|
192
|
+
return TxRaw.fromPartial({
|
|
193
|
+
bodyBytes: signedTxBodyBytes,
|
|
194
|
+
authInfoBytes: signedAuthInfoBytes,
|
|
195
|
+
signatures: [fromBase64(signature)],
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@mictonode/widget",
|
|
3
|
+
"private": false,
|
|
4
|
+
"version": "0.3.9",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/ping-widget.js",
|
|
7
|
+
"author": {
|
|
8
|
+
"name": "Alisa | Side Protocol",
|
|
9
|
+
"email": "alisa@side.one"
|
|
10
|
+
},
|
|
11
|
+
"homepage": "https://github.com/ping-pub/ping-widget",
|
|
12
|
+
"repository": {
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "https://github.com/ping-pub/ping-widget"
|
|
15
|
+
},
|
|
16
|
+
"license": "MIT",
|
|
17
|
+
"description": "Ping widget",
|
|
18
|
+
"keywords": [
|
|
19
|
+
"vue3",
|
|
20
|
+
"webcomponent",
|
|
21
|
+
"widget",
|
|
22
|
+
"explorer",
|
|
23
|
+
"blockchain",
|
|
24
|
+
"sidechain",
|
|
25
|
+
"Side Protocol",
|
|
26
|
+
"Ping.pub"
|
|
27
|
+
],
|
|
28
|
+
"scripts": {
|
|
29
|
+
"dev": "vite",
|
|
30
|
+
"serve": "vite",
|
|
31
|
+
"build": "node --max_old_space_size=4192 ./node_modules/.bin/vite build",
|
|
32
|
+
"preview": "vite preview",
|
|
33
|
+
"trypublish": "npm publish || true"
|
|
34
|
+
},
|
|
35
|
+
"dependencies": {
|
|
36
|
+
"@cosmjs/amino": "^0.32.3",
|
|
37
|
+
"@cosmjs/cosmwasm-stargate": "^0.32.3",
|
|
38
|
+
"@cosmjs/encoding": "^0.32.3",
|
|
39
|
+
"@cosmjs/ledger-amino": "^0.32.3",
|
|
40
|
+
"@cosmjs/proto-signing": "^0.32.3",
|
|
41
|
+
"@cosmjs/stargate": "^0.32.3",
|
|
42
|
+
"@ethersproject/hash": "5.7.0",
|
|
43
|
+
"@ethersproject/signing-key": "5.7.0",
|
|
44
|
+
"@initia/initia-registry-types": "^0.0.17",
|
|
45
|
+
"@initia/initia.js": "0.2.7",
|
|
46
|
+
"@initia/initia.proto": "^0.2.0",
|
|
47
|
+
"@initia/opinit.proto": "^0.0.8",
|
|
48
|
+
"@initia/utils": "0.69.0",
|
|
49
|
+
"@leapwallet/cosmos-snap-provider": "^0.1.21",
|
|
50
|
+
"@ledgerhq/hw-transport-web-ble": "^6.27.13",
|
|
51
|
+
"@ledgerhq/hw-transport-webusb": "^6.27.14",
|
|
52
|
+
"@metamask/eth-sig-util": "^5.1.0",
|
|
53
|
+
"@msgpack/msgpack": "^3.0.0-beta2",
|
|
54
|
+
"@noble/hashes": "^1.4.0",
|
|
55
|
+
"@ping-pub/chain-registry-client": "^0.0.25",
|
|
56
|
+
"@types/bignumber.js": "^5.0.0",
|
|
57
|
+
"@types/numeral": "^2.0.5",
|
|
58
|
+
"@types/ramda": "^0.30.0",
|
|
59
|
+
"bech32": "^2.0.0",
|
|
60
|
+
"bignumber.js": "^9.1.2",
|
|
61
|
+
"buffer": "^6.0.3",
|
|
62
|
+
"change-case": "^5.4.4",
|
|
63
|
+
"cosmjs-types": "0.9.0",
|
|
64
|
+
"cross-fetch": "^3.1.5",
|
|
65
|
+
"daisyui": "^3.1.0",
|
|
66
|
+
"dayjs": "^1.11.7",
|
|
67
|
+
"ethers": "^6.13.1",
|
|
68
|
+
"fflate": "^0.8.0",
|
|
69
|
+
"ky": "^1.3.0",
|
|
70
|
+
"numeral": "^2.0.6",
|
|
71
|
+
"osmojs": "^15.2.1",
|
|
72
|
+
"ping-widget": "^0.0.33",
|
|
73
|
+
"ramda": "^0.30.1",
|
|
74
|
+
"vue": "^3.2.47",
|
|
75
|
+
"vue3-webcomponent-wrapper": "^0.2.0",
|
|
76
|
+
"zod": "^3.23.8"
|
|
77
|
+
},
|
|
78
|
+
"devDependencies": {
|
|
79
|
+
"@esbuild-plugins/node-globals-polyfill": "^0.2.3",
|
|
80
|
+
"@iconify/vue": "^4.1.1",
|
|
81
|
+
"@noble/secp256k1": "^2.1.0",
|
|
82
|
+
"@rollup/plugin-inject": "^5.0.3",
|
|
83
|
+
"@types/elliptic": "^6.4.18",
|
|
84
|
+
"@vitejs/plugin-vue": "^4.1.0",
|
|
85
|
+
"assert-browserify": "^2.0.0",
|
|
86
|
+
"autoprefixer": "^10.4.14",
|
|
87
|
+
"bitcoinjs-lib": "^6.1.5",
|
|
88
|
+
"buffer": "6.0.3",
|
|
89
|
+
"crypto-browserify": "^3.12.0",
|
|
90
|
+
"elliptic": "^6.5.5",
|
|
91
|
+
"long": "^5.2.0",
|
|
92
|
+
"path-browserify": "^1.0.1",
|
|
93
|
+
"postcss": "^8.4.23",
|
|
94
|
+
"rollup-plugin-polyfill-node": "^0.12.0",
|
|
95
|
+
"stream-browserify": "^3.0.0",
|
|
96
|
+
"tailwindcss": "^3.3.2",
|
|
97
|
+
"typescript": "^5.0.2",
|
|
98
|
+
"vite": "^4.3.2",
|
|
99
|
+
"vite-plugin-css-injected-by-js": "^3.1.0",
|
|
100
|
+
"vue-tsc": "^1.4.2"
|
|
101
|
+
}
|
|
102
|
+
}
|