-
Notifications
You must be signed in to change notification settings - Fork 2
/
metamask.js
86 lines (70 loc) · 2.5 KB
/
metamask.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import { hashMessage } from '@ethersproject/hash'
import {
computePublicKey,
recoverPublicKey,
} from '@ethersproject/signing-key'
import { altheaToEth, gravityToEth } from '@gravity-bridge/address-converter'
import { signatureToWeb3Extension } from '@gravity-bridge/transactions';
import { createTxRaw, createAnyMessage } from '@gravity-bridge/proto';
let mmAccounts;
let currPubkey;
const verifyPubKeyMsg = 'Verify Public Key';
export function metamaskInstalled() {
return typeof window.ethereum !== 'undefined';
}
export async function connectMetamask() {
mmAccounts = await window?.ethereum?.request({
method: 'eth_requestAccounts',
});
console.log("Discovered accounts: ", mmAccounts);
}
export async function verifyPubKey() {
const signature = await window?.ethereum?.request({
method: 'personal_sign',
params: [verifyPubKeyMsg, mmAccounts[0], ''],
})
// Compress the key, since the client expects
// public keys to be compressed.
const uncompressedPk = recoverPublicKey(
hashMessage(verifyPubKeyMsg),
signature,
)
const hexPk = computePublicKey(uncompressedPk, true)
const pk = Buffer.from(
hexPk.replace('0x', ''), 'hex',
).toString('base64')
currPubkey = pk
}
export function GetCurrPubkey() {
return currPubkey;
}
export function GetCurrAccount() {
if (mmAccounts && mmAccounts.length > 0) {
return mmAccounts[0];
} else {
return undefined;
}
}
export async function SignEIP712CosmosTx(context, tx) {
const { sender, chain } = context
var senderHexAddress
if (sender.accountAddress.startsWith("althea")) {
senderHexAddress = altheaToEth(sender.accountAddress)
} else if (sender.accountAddress.startsWith("gravity")) {
senderHexAddress = gravityToEth(sender.accountAddress)
}
const eip712Payload = JSON.stringify(tx.eipToSign);
console.log("EIP712 payload: ", eip712Payload);
const mmArgs = {
method: 'eth_signTypedData_v4',
params: [senderHexAddress, eip712Payload],
}
const signature = await window.ethereum.request(mmArgs);
const extension = signatureToWeb3Extension(chain, sender, signature);
const { legacyAmino } = tx;
legacyAmino.body.extensionOptions.push(createAnyMessage(extension));
const bodyBytes = legacyAmino.body.toBinary();
const authInfoBytes = legacyAmino.authInfo.toBinary();
const signedTx = createTxRaw(bodyBytes, authInfoBytes, [new Uint8Array()]);
return signedTx
}