-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathexample.html
244 lines (202 loc) · 8.44 KB
/
example.html
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
<!doctype html>
<html>
<head>
<title>Filecoin.js Wallet Example</title>
<meta charset="UTF-8">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"
integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">
<script src="https://cdnjs.cloudflare.com/ajax/libs/bignumber.js/9.0.0/bignumber.min.js"
integrity="sha512-vg7OeB3i21ZSV8k2kyAXnbFL24Rwoz+nRC6xVAddaoEQIIboCtONdpuQQ+tl56RBDEf8fT0DtcVQeip2asslmg=="
crossorigin="anonymous"></script>
</head>
<body>
<div class="container">
<div class="row row-cols-12" style="padding-bottom: 10px; font-weight: bold;">
<div class="col-8">
Mnemonic Address
</div>
</div>
<div class="row row-cols-12" style="padding-bottom: 10px;">
<div class="col-7">
<div id="mnemonicAddress"></div>
</div>
<div class="col-3">
<input type="text" class="form-control" placeholder="to address" id="toMnemonic">
</div>
<div class="col-1">
<input type="text" class="form-control" placeholder="FIL" id="filMnemonic">
</div>
<div class="col">
<button type="submit" class="btn btn-primary" id="sendMnemonic">Send</button>
</div>
</div>
<div class="row row-cols-12" style="padding-bottom: 10px; font-weight: bold;">
<div class="col-8">
Default Address
</div>
</div>
<div class="row row-cols-12" style="padding-bottom: 10px;">
<div class="col-7">
<div id="rpcAddress"></div>
</div>
<div class="col-3">
<input type="text" class="form-control" placeholder="to address" id="toDefault">
</div>
<div class="col-1">
<input type="text" class="form-control" placeholder="FIL" id="filDefault">
</div>
<div class="col">
<button type="submit" class="btn btn-primary" id="sendDefault">Send</button>
</div>
</div>
<div class="row row-cols-12" style="padding-bottom: 10px; font-weight: bold;">
<div class="col-8">
Secp256k1 Address
</div>
</div>
<div class="row row-cols-12" style="padding-bottom: 10px;">
<div class="col-7">
<div id="rpcAddressSecp"></div>
</div>
<div class="col-3">
<input type="text" class="form-control" placeholder="to address" id="toSecp">
</div>
<div class="col-1">
<input type="text" class="form-control" placeholder="FIL" id="filSecp">
</div>
<div class="col">
<button type="submit" class="btn btn-primary" id="sendSecp">Send</button>
</div>
</div>
<div class="row row-cols-12" style="padding-bottom: 10px; font-weight: bold;">
<div class="col-8">
Metamask Address
</div>
</div>
<div class="row row-cols-12" style="padding-bottom: 10px;">
<div class="col-7">
<div id="metamaskAddress"></div>
</div>
<div class="col-3">
<input type="text" class="form-control" placeholder="to address" id="toMetamask">
</div>
<div class="col-1">
<input type="text" class="form-control" placeholder="FIL" id="filMetamask">
</div>
<div class="col">
<button type="submit" class="btn btn-primary" id="sendMetamask">Send</button>
</div>
</div>
</div>
<script type="text/javascript" src="../../builds/bundle/filecoin.js"></script>
<script type="text/javascript" src="../../tests/tools/testnet/credentials/credentials_web.js"></script>
<script type="text/javascript">
const main = async () => {
let mnemonicAddress = '';
let rpcAddress = '';
let rpcAddressSecp = '';
let metamaskAddress = '';
const metamaskHelper = new FilecoinJs.MetamaskSnapHelper({ url: 'http://localhost:8000/rpc/v0', token: LOTUS_AUTH_TOKEN });
const err = await metamaskHelper.installFilecoinSnap();
const httpConnector = new FilecoinJs.HttpJsonRpcConnector({ url: 'http://localhost:8000/rpc/v0', token: LOTUS_AUTH_TOKEN });
const lotusWalletProvider = new FilecoinJs.HttpJsonRpcWalletProvider(httpConnector);
const getPass = () => {
let password;
return () => {
if (password) return password;
password = prompt('Mnemonic wallet password?');
setTimeout(() => password = undefined, 100000);
return password;
}
}
const mnemonicWalletProvider = new FilecoinJs.MnemonicWalletProvider(
httpConnector,
'equip will roof matter pink blind book anxiety banner elbow sun young',
'',
`m/44'/1'/0/0/1`
);
const metamaskWalletProvider = new FilecoinJs.MetamaskWalletProvider(httpConnector, metamaskHelper.filecoinApi)
const sendLotus = async (from, toId, filId) => {
const to = document.getElementById(toId).value;
const amount = document.getElementById(filId).value;
const msg = await lotusWalletProvider.sendMessage({
From: from,
To: to,
GasLimit: 0,
GasFeeCap: new BigNumber(0),
GasPremium: new BigNumber(0),
Value: new BigNumber(amount),
Method: 0,
Params: '',
Version: 0,
Nonce: 0
});
console.log(msg);
}
const sendSigner = async (from, toId, filId) => {
try {
const to = document.getElementById(toId).value;
const amount = document.getElementById(filId).value;
const message = await mnemonicWalletProvider.createMessage({
To: to,
From: from,
Value: new BigNumber(amount),
});
const signedMessage = await mnemonicWalletProvider.signMessage(message);
const res = await mnemonicWalletProvider.sendSignedMessage(signedMessage);
} catch (e) {
alert(e.message);
}
}
const sendMetamask = async (from, toId, filId) => {
const to = document.getElementById(toId).value;
const amount = document.getElementById(filId).value;
try {
const to = document.getElementById(toId).value;
const amount = document.getElementById(filId).value;
const message = await metamaskWalletProvider.createMessage({
To: to,
From: from,
Value: new BigNumber(amount),
});
const signedMessage = await metamaskWalletProvider.signMessage(message);
const res = await metamaskWalletProvider.sendSignedMessage(signedMessage);
} catch (e) {
alert(e.message);
}
}
const updateAddresses = async () => {
const availableAccounts = await lotusWalletProvider.getAccounts();
mnemonicAddress = await mnemonicWalletProvider.getDefaultAccount();
rpcAddress = await lotusWalletProvider.getDefaultAccount();
rpcAddressSecp = availableAccounts[0];
try {
metamaskAddress = await metamaskWalletProvider.getDefaultAccount();
} catch (e) {
console.log('metamask error',e);
}
console.log(metamaskAddress);
const mnemonicBalance = await mnemonicWalletProvider.getBalance(mnemonicAddress);
const rpcBalance = await lotusWalletProvider.getBalance(rpcAddress);
const rpcSecpBalance = await lotusWalletProvider.getBalance(rpcAddressSecp);
let metamaskBalance = '';
try {
metamaskBalance = await metamaskWalletProvider.getBalance(metamaskAddress);
} catch (e) {
console.log('metamask error',e);
}
document.getElementById('mnemonicAddress').innerHTML = `${mnemonicAddress} <br/> (${mnemonicBalance})`;
document.getElementById('rpcAddress').innerHTML = `${rpcAddress} <br/> (${rpcBalance})`;
document.getElementById('rpcAddressSecp').innerHTML = `${rpcAddressSecp} <br/> (${rpcSecpBalance})`;
document.getElementById('metamaskAddress').innerHTML = `${metamaskAddress} <br/> (${metamaskBalance})`;
}
document.getElementById('sendDefault').onclick = () => sendLotus(rpcAddress, "toDefault", "filDefault");
document.getElementById('sendSecp').onclick = () => sendLotus(rpcAddressSecp, "toSecp", "filSecp");
document.getElementById('sendMnemonic').onclick = () => sendSigner(mnemonicAddress, "toMnemonic", "filMnemonic");
document.getElementById('sendMetamask').onclick = () => sendMetamask(metamaskAddress, "toMetamask", "filMetamask");
await updateAddresses();
};
main().then(console.log).catch(console.error);
</script>
</body>
</html>