-
Notifications
You must be signed in to change notification settings - Fork 5
/
server.js
233 lines (197 loc) · 7.45 KB
/
server.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
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
import express from "express";
import cors from "cors";
import { ElectrumClient } from "electrum-cash";
import { appName, mintStatus, tokenId, contractAddressMint, mintDate, nrMintingUtxos, collectionSize, network, apiDomain, bcmrJSON, chaingraphUrl, hideIPFSImages, hideIPFSIcons } from "./config.js";
import { queryMintingNFTs } from './queryChainGraph.js';
import { vmNumberToBigInt, hexToBin, bigIntToVmNumber, binToHex } from '@bitauth/libauth';
// Load express web app.
const app = express();
const port = 3000;
app.use(cors());
app.use(express.json());
// Initialize an electrum client and wait for the client to connect.
// We use this client for address updates.
const electrumClient = network == "mainnet" ? "fulcrum.greyh.at" : "chipnet.imaginary.cash";
const electrum = new ElectrumClient(appName, '1.5.1', electrumClient);
await electrum.connect();
// Declare global scope variables.
let nftsMinted = 0;
let commitmentCount = 0;
let mintingUtxos = [];
let mintedPerUtxo = [];
// This function is called when an address updates,
// then it calls Chaingraph to get updated UTXO information.
async function updateMintingUtxos() {
const oldAmountMinted = mintedPerUtxo.reduce((a, b) => a + b, 0);
const responseJson = await queryMintingNFTs(tokenId, chaingraphUrl);
const newMintingUtxos = [];
const newMintedPerUtxo = [];
if (responseJson.data) {
const outputs = responseJson.data.output;
let txidChanged = false;
for (let i = 0; i < outputs.length; i++) {
const output = outputs[i];
const txid = output.transaction_hash.slice(2);
const commitmentScriptNum = output.nonfungible_token_commitment.slice(2);
const newMintingUtxo = { txid, vout: output.output_index, commitment: commitmentScriptNum };
const commitmentNumber = Number(vmNumberToBigInt(hexToBin(commitmentScriptNum)));
const numberMintingUtxo = commitmentNumber % nrMintingUtxos;
const oldMintingUtxo = mintingUtxos[numberMintingUtxo];
const oldTxId = oldMintingUtxo?.txid;
if (oldTxId != txid) { txidChanged = true; };
newMintingUtxos[numberMintingUtxo] = newMintingUtxo;
const amountMinted = Math.floor(commitmentNumber / nrMintingUtxos);
newMintedPerUtxo[numberMintingUtxo] = amountMinted;
}
// We need to set newMintedPerUtxo to the max number of minted items for
// any fully used minting UTXO. Otherwise the reduce below will not
// be accurate near the end of the mint.
for (let i = 0; i < nrMintingUtxos; i++) {
if (newMintedPerUtxo[i] == undefined) {
newMintedPerUtxo[i] = collectionSize / nrMintingUtxos;
}
}
// Update global state.
mintingUtxos = newMintingUtxos;
mintedPerUtxo = newMintedPerUtxo;
nftsMinted = mintedPerUtxo.reduce((a, b) => a + b, 0);
const noStateUpdate = nftsMinted == oldAmountMinted && !txidChanged;
// If the Chaingraph instance has not seen the new tx, wait and re-fetch.
if (noStateUpdate && nftsMinted != 0) {
setTimeout(updateMintingUtxos, 500);
}
} else {
console.log(responseJson);
// Fire off another attempt. We errored out!
updateMintingUtxos();
}
}
// Listen for notifications and set up a subscription with callback function.
electrum.on('notification', () => updateMintingUtxos());
await electrum.subscribe('blockchain.address.subscribe', contractAddressMint);
// Home route.
app.get('/', (req, res) => {
let nftsAvailable = collectionSize - nftsMinted;
res.json({
nftsMinted,
nftsAvailable,
mintStatus
});
});
// Minting UTXO information route.
app.get('/mint', (req, res) => {
let availableMintingUtxo = "";
if (mintingUtxos.length > 0) {
// Remove all undefined minting UTXOs.
let existingMintingUtxos = [];
for (let i = 0; i < mintingUtxos.length; i++) {
if (mintingUtxos[i] != undefined) {
existingMintingUtxos.push(mintingUtxos[i]);
}
}
// If we have UTXOs left, pick a revolving commitment.
if (existingMintingUtxos.length > 0) {
let idx = commitmentCount % existingMintingUtxos.length;
availableMintingUtxo = existingMintingUtxos[idx].commitment;
commitmentCount++;
}
}
res.json({
contractAddressMint,
mintingUtxos,
mintedPerUtxo,
availableMintingUtxo
});
});
// Filters BCMR file to hide IPFS links and unminted items.
const filteredBCMR = function (bcmr) {
let bcmrCopy = JSON.parse(JSON.stringify(bcmr));
if (bcmrCopy.identities[tokenId]) {
for (let i = 0; i < collectionSize; i++) {
let vmNum = bigIntToVmNumber(BigInt(i));
let indexHex = binToHex(vmNum);
if (!nftMinted(i + 1)) {
delete bcmrCopy.identities[tokenId][mintDate]["token"]["nfts"]["parse"]["types"][indexHex];
} else {
if (hideIPFSImages) {
if (bcmrCopy.identities[tokenId][mintDate]["token"]["nfts"]["parse"]["types"][indexHex]) {
bcmrCopy.identities[tokenId][mintDate]["token"]["nfts"]["parse"]["types"][indexHex]["uris"]["image"] = apiDomain + "images/" + (i + 1);
}
}
if (hideIPFSIcons) {
if (bcmrCopy.identities[tokenId][mintDate]["token"]["nfts"]["parse"]["types"][indexHex]) {
bcmrCopy.identities[tokenId][mintDate]["token"]["nfts"]["parse"]["types"][indexHex]["uris"]["icon"] = apiDomain + "icons/" + (i + 1);
}
}
}
}
}
return bcmrCopy;
};
// BCMR route.
app.get('/.well-known/bitcoin-cash-metadata-registry.json', (req, res) => {
res.json(filteredBCMR(bcmrJSON));
});
// Checks if an NFT has been minted. Used as a gate by many endpoints.
const nftMinted = function (id) {
// If we don't have any minted, it's all false.
if (mintedPerUtxo == []) {
return false;
}
let threadID = (id - 1) % nrMintingUtxos;
let highMintId = mintedPerUtxo[threadID] * nrMintingUtxos;
return id <= highMintId;
};
// Sends an image for an NFT.
const sendImage = function (id) {
if (nftMinted(id)) {
return 'data/images/' + id + '.png';
} else {
return 'images/unknown.webp';
}
};
// NFT image route.
app.get('/images/:id', (req, res) => {
res.sendFile(sendImage(req.params.id), { root: '.' });
});
// Sends an icon for an NFT.
const sendIcon = function (id) {
if (nftMinted(id)) {
return 'data/icons/' + id + '.png';
} else {
return 'images/unknown-icon.webp';
}
};
// NFT icon route.
app.get('/icons/:id', (req, res) => {
res.sendFile(sendIcon(req.params.id), { root: '.' });
});
// Filters BCMR down to a single NFT record.
const bcmrNFT = function (id, bcmr) {
if (bcmr.identities[tokenId]) {
if (nftMinted(id)) {
let vmNum = bigIntToVmNumber(BigInt(id - 1));
let indexHex = binToHex(vmNum);
if (hideIPFSImages) {
if (bcmr.identities[tokenId][mintDate]["token"]["nfts"]["parse"]["types"][indexHex]) {
bcmr.identities[tokenId][mintDate]["token"]["nfts"]["parse"]["types"][indexHex]["uris"]["image"] = apiDomain + "images/" + (id);
}
}
if (hideIPFSIcons) {
if (bcmr.identities[tokenId][mintDate]["token"]["nfts"]["parse"]["types"][indexHex]) {
bcmr.identities[tokenId][mintDate]["token"]["nfts"]["parse"]["types"][indexHex]["uris"]["icon"] = apiDomain + "icons/" + (id);
}
}
return bcmr.identities[tokenId][mintDate]["token"]["nfts"]["parse"]["types"][indexHex];
}
}
return {};
};
// NFT metadata route.
app.get('/nfts/:id', (req, res) => {
res.json(bcmrNFT(req.params.id, bcmrJSON));
});
// Start server.
app.listen(port, () => {
console.log(`Server listening at http://localhost:${port}`);
});