-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
184 lines (147 loc) · 5.68 KB
/
index.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
<!DOCTYPE html>
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"> </script>
<script src="https://cdn.jsdelivr.net/npm/web3@latest/dist/web3.min.js"></script>
<script src="https://unpkg.com/moralis/dist/moralis.js"></script>
<script src="https://cdn.ethers.io/lib/ethers-5.2.umd.min.js" type="application/javascript"></script>
<script src="https://cdn.jsdelivr.net/npm/merkletreejs@latest/merkletree.js"></script>
<script src="https://cdn.jsdelivr.net/npm/keccak256@latest/keccak256.js"></script>
</head>
<body>
<div>
<button class="generate-tree-button">Generate tree</button>
<button class="load-button">Load</button>
<button class="hex-root-button">Hex root</button>
<div id="status"></div>
<div id="nfts"></div>
</div>
<script type="text/javascript">
const tokens =
{
"1": "recipient_address",
"2": "recipient_address",
"3": "recipient_address",
}
const merkleTree = new MerkleTree(Object.entries(tokens).map(token => hashToken(...token)), keccak256, { sortPairs: true })
const collectionAddress = "smart_contract_address"
var provider;
var signer;
window.addEventListener('load', async () => {
if (window.ethereum) {
window.web3 = new Web3(ethereum);
try {
await ethereum.enable();
initApp()
} catch (err) {
console.log(err)
$('#status').html('User denied account access', err)
}
} else if (window.web3) {
window.web3 = new Web3(web3.currentProvider)
initApp()
} else {
$('#status').html('No Metamask (or other Web3 Provider) installed')
}
})
function initApp() {
initEthers()
initMoralis()
initSignButton()
initLoadNftsButton()
initHexRootButton()
}
function initEthers() {
provider = new ethers.providers.Web3Provider(web3.currentProvider);
signer = provider.getSigner()
}
function initMoralis() {
const serverUrl = "your_moralis_server_url"
const appId = "your_moralis_app_id"
Moralis.start({ serverUrl, appId })
}
async function initSignButton() {
$('.generate-tree-button').click(() => {
createMerkleDrop()
})
async function createMerkleDrop() {
await window.ethereum.request({ method: 'eth_requestAccounts' });
var content = Date.now() + " Generated tokens: \n\n"
var tokenObjects = new Array()
for ([tokenId, address] of Object.entries(tokens)) {
tokenObjects.push(await getProof(tokenId, address))
}
content += JSON.stringify(tokenObjects)
downloadFile("merkleTree.json", content)
}
async function getProof(_tokenId, _address) {
const hexProof = merkleTree.getHexProof(hashToken(_tokenId, address))
return {
tokenId: _tokenId,
address: _address,
hexProof: hexProof,
}
}
}
function initLoadNftsButton() {
$('.load-button').click(() => {
const nfts = getUserNfts()
console.log(nfts)
})
async function getUserNfts() {
const userAddress = await signer.getAddress()
const options = { chain: 'Rinkeby', address: userAddress };
return await Moralis.Web3API.account.getNFTs(options);
}
async function getNftTokenMetadata(_collectionAddress, _tokenId) {
const options = { chain: 'Rinkeby', address: _collectionAddress, token_id: _tokenId };
return await Moralis.Web3API.token.getTokenIdMetadata(options);
}
}
function initHexRootButton() {
$('.hex-root-button').click(() => {
console.log(merkleTree.getHexRoot())
})
}
function hashToken(tokenId, account) {
const leaf = ethers.utils.solidityKeccak256(['uint256', 'address'], [tokenId, account]).slice(2)
console.log(hexStringToUint8Array(leaf))
return hexStringToUint8Array(leaf)
}
function hexStringToUint8Array(hexString) {
if (hexString.length % 2 !== 0) {
throw "Invalid hexString";
}/*from w w w. j av a 2s . c o m*/
var arrayBuffer = new Uint8Array(hexString.length / 2);
for (var i = 0; i < hexString.length; i += 2) {
var byteValue = parseInt(hexString.substr(i, 2), 16);
if (isNaN(byteValue)) {
throw "Invalid hexString";
}
arrayBuffer[i / 2] = byteValue;
}
return arrayBuffer;
}
function downloadFile(filename, content) {
// It works on all HTML5 Ready browsers as it uses the download attribute of the <a> element:
const element = document.createElement('a');
//A blob is a data type that can store binary data
// "type" is a MIME type
// It can have a different value, based on a file you want to save
const formattedContent = content.replace(/\n/g, "\r\n");
const blob = new Blob([formattedContent], { type: 'plain/text', endings: 'native' });
//createObjectURL() static method creates a DOMString containing a URL representing the object given in the parameter.
const fileUrl = URL.createObjectURL(blob);
//setAttribute() Sets the value of an attribute on the specified element.
element.setAttribute('href', fileUrl); //file location
element.setAttribute('download', filename); // file name
element.style.display = 'none';
//use appendChild() method to move an element from one element to another
document.body.appendChild(element);
element.click();
//The removeChild() method of the Node interface removes a child node from the DOM and returns the removed node
document.body.removeChild(element);
};
</script>
</body>
</html>