-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommands.js
409 lines (400 loc) · 16.4 KB
/
commands.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
#! /usr/bin/env node
require = require('esm-wallaby')(module);
const conf = new (require('conf'))()
const chalk = require('chalk')
var fetch = require('node-fetch');
const fs = require('fs');
global.fetch = fetch;
const origWarning = process.emitWarning;
process.emitWarning = function(...args) {
if (args[2] !== 'DEP0005') {
// pass any other warnings through normally
return origWarning.apply(process, args);
} else {
// do nothing, eat the warning
}
}
const imageThumbnail = require('image-thumbnail');
const bip39 = require('bip39');
const Ed25519KeyIdentity = require("@dfinity/identity").Ed25519KeyIdentity;
const HttpAgent = require("@dfinity/agent").HttpAgent;
const Actor = require("@dfinity/agent").Actor;
const Principal = require("@dfinity/principal").Principal;
const crypto = require("crypto");
const sjcl = require("sjcl");
const prompt = require("prompt");
const yesno = require("yesno");
const mime = require('mime');
const extjs = require("./extjs/extjs").default;
global.Buffer = global.Buffer || require('buffer').Buffer;
if (typeof btoa === 'undefined') {
global.btoa = function (str) {
return Buffer.from(str, 'binary').toString('base64');
};
}
if (typeof atob === 'undefined') {
global.atob = function (b64Encoded) {
return Buffer.from(b64Encoded, 'base64').toString('binary');
};
}
const CHUNKSIZE = 1900000;
const NFTFACTORY = "bn2nh-jaaaa-aaaam-qapja-cai";
const NFTFACTORY_IDL = ({ IDL }) => {
const Factory = IDL.Service({
'createCanister' : IDL.Func([IDL.Text, IDL.Text], [IDL.Principal], []),
});
return Factory;
};
//Helpers
const removeFilenameExtension = (filename) => {
return filename.split('.').slice(0, -1).join('.');
};
const getId = (seed) => {
var seed = bip39.mnemonicToSeedSync(seed);
seed = Array.from(seed);
seed = seed.splice(0, 32);
seed = new Uint8Array(seed);
return Ed25519KeyIdentity.generate(seed);
}
const decrypt = (principal, data, password) => {
const key = crypto.pbkdf2Sync(password, principal, 30000, 512, 'sha512');
return atob(sjcl.decrypt(key.toString(), data));
};
const encrypt= (principal, seed, password) => {
const key = crypto.pbkdf2Sync(password, principal, 30000, 512, 'sha512');
return sjcl.encrypt(key.toString(), btoa(seed))
};
const getWalletSeed = (w,p) => {
if (w.encrypted) {
try {
return decrypt(w.principal, w.data, p);
} catch(e) {
return false;
};
} else {
return w.data;
};
};
const validateHandle = (h) => {
var re = /^[^\s^\x00-\x1f\\?*:"";<>|\/.][^\x00-\x1f\\?*:"";<>|\/]*[^\s^\x00-\x1f\\?*:"";<>|\/.]+$/g;
return re.test(h);
};
const uploadAsset = async (api, ah, filename, filepath) =>{
outputUpdate("Registering with canister...");
var data = fs.readFileSync(filepath);
var type = mime.getType(filepath);
await api.ext_assetAdd(ah, type, filename, {direct:[]});
var pl = [...data];
var numberOfChunks = Math.ceil(pl.length/CHUNKSIZE);
var c = 0;
var first = true;
var total = Math.ceil(pl.length/CHUNKSIZE);
while (pl.length > CHUNKSIZE) {
outputUpdate("Uploading chunk... ("+(c+1)+"/"+total+")");
c++;
await api.ext_assetStream(ah, pl.splice(0, CHUNKSIZE), first);
if (first) first = false;
};
outputUpdate("Uploading chunk... ("+(c+1)+"/"+total+")");
await api.ext_assetStream(ah, pl, first);
return true;
}
const outputUpdate = (m) => {
console.log(chalk.cyan.bold(m));
};
const output = (m) => {
console.log(chalk.green.bold(m));
};
const outputError = (m) => {
console.log(chalk.red.bold(m));
};
//Commands
var commands = {};
commands.wallet_create = async (handle, options) => {
if (!validateHandle(handle)) return outputError("Wallet handle is invalid");
if (!options.hasOwnProperty('password')) {
if (!await yesno({question: chalk.yellow.bold('Are you sure you want to create a new wallet without encrypting it with a password? If not, you can set the password using --password? (y/n)')})) return;
};
var wallets = conf.get('wallets');
if (!wallets) wallets = {};
if (wallets.hasOwnProperty(handle)) return outputError("That wallet handle is already taken");
var wallet = {};
var seed = bip39.generateMnemonic();
var id = getId(seed);
wallet.principal = id.getPrincipal().toText();
if (options.hasOwnProperty('password')) {
wallet.encrypted = true;
wallet.data = encrypt(wallet.principal, seed, options.password);
} else {
wallet.encrypted = false;
wallet.data = seed;
};
wallets[handle] = wallet;
conf.set('wallets', wallets);
conf.set('active-wallet', handle);
return output("Your new wallet was created and has been set to your active wallet!");
};
commands.wallet_import = async (handle, options) => {
if (!validateHandle(handle)) return outputError("Wallet handle is invalid");
if (!options.hasOwnProperty('password')) {
if (!await yesno({question: chalk.yellow.bold('Are you sure you want to import a wallet without encrypting it with a password? If not, you can set the password using --password? (y/n)')})) return;
};
var wallets = conf.get('wallets');
if (!wallets) wallets = {};
if (wallets.hasOwnProperty(handle)) return outputError("That wallet handle is already taken");
var wallet = {};
var seed = options.seed.join(" ");;
var id = getId(seed);
wallet.principal = id.getPrincipal().toText();
if (options.password) {
wallet.encrypted = true;
wallet.data = encrypt(wallet.principal, seed, options.password);
} else {
wallet.encrypted = false;
wallet.data = seed;
};
wallets[handle] = wallet;
conf.set('wallets', wallets);
conf.set('active-wallet', handle);
return output("Your wallet was imported and has been set to your active wallet!");
};
commands.wallet_list = async () => {
var wallets = conf.get('wallets');
if (!wallets) {
return outputError("You have no stored wallets");
};
var w = conf.get('active-wallet');
var o = 0;
for (var h of Object.keys(wallets)) {
output(h+" : "+wallets[h].principal+(h==w ? " *" : ""));
o++;
}
if (o == 0) return outputError("You have no stored wallets");
};
commands.wallet_delete = async (handle) => {
if (!await yesno({question: chalk.red.bold('If you have not backed this wallet up then you will lose access forever. This can not be reversed! Are you sure you want to delete this wallet? (y/n)')})) return;
var wallets = conf.get('wallets');
if (!wallets) wallets = {};
if (!wallets.hasOwnProperty(handle)) return outputError("No wallet using that handle");
delete wallets[handle];
conf.set('wallets', wallets);
return output("This wallet has been deleted!");
};
commands.wallet_clear = async () => {
if (!await yesno({question: chalk.red.bold('If you have not backed these wallets up then you will lose access forever. This can not be reversed! Are you sure you want to delete ALL wallets? (y/n)')})) return;
conf.set('wallets', {});
return output("All wallets have been cleared");
};
commands.wallet_use = async (handle) => {
var wallets = conf.get('wallets');
if (!wallets) wallets = {};
if (!wallets.hasOwnProperty(handle)) return outputError("No wallet using that handle");
conf.set('active-wallet', handle);
return output("You have set your active wallet!");
};
commands.wallet_export = async (options) => {
var wallets = conf.get('wallets');
if (!wallets) wallets = {};
var w = conf.get('active-wallet');
if (!w || !wallets.hasOwnProperty(w)) return outputError("You do not have an active wallet. Select an active wallet using extjs wallet use");
var seed = getWalletSeed(wallets[w], options.password);
if (!seed) return outputError("Incorrect password");
return output("Your backup seed for this wallet is: " + seed);
};
commands.wallet_whoami = async () => {
var wallets = conf.get('wallets');
if (!wallets) wallets = {};
var w = conf.get('active-wallet');
if (!w || !wallets.hasOwnProperty(w)) return outputError("You do not have an active wallet. Select an active wallet using extjs wallet use");
var principal = wallets[w].principal;
output(w+" : "+principal+" *");
};
commands.wallet_address = async () => {
var wallets = conf.get('wallets');
if (!wallets) wallets = {};
var w = conf.get('active-wallet');
if (!w || !wallets.hasOwnProperty(w)) return outputError("You do not have an active wallet. Select an active wallet using extjs wallet use");
var principal = wallets[w].principal;
var address = extjs.principalToAccountIdentifier(principal, 0);
output(address);
};
commands.wallet_principal = async () => {
var wallets = conf.get('wallets');
if (!wallets) wallets = {};
var w = conf.get('active-wallet');
if (!w || !wallets.hasOwnProperty(w)) return outputError("You do not have an active wallet. Select an active wallet using extjs wallet use");
var principal = wallets[w].principal;
output(principal);
};
commands.wallet_balance = async () => {
var wallets = conf.get('wallets');
if (!wallets) wallets = {};
var w = conf.get('active-wallet');
if (!w || !wallets.hasOwnProperty(w)) return outputError("You do not have an active wallet. Select an active wallet using extjs wallet use");
var principal = wallets[w].principal;
var address = extjs.principalToAccountIdentifier(principal, 0);
var bal = Number(await extjs.connect("https://boundary.ic0.app/").token().getBalance(address))/100000000;
output("Wallet balance: " + bal + "ICP");
};
commands.wallet_send = async (amount, address, options) => {
if (!await yesno({question: chalk.red.bold('This can not be reversed! Are you sure you want to send '+amount+'ICP to '+address+'? (y/n)')})) return;
var wallets = conf.get('wallets');
if (!wallets) wallets = {};
var w = conf.get('active-wallet');
if (!w || !wallets.hasOwnProperty(w)) return outputError("You do not have an active wallet. Select an active wallet using extjs wallet use");
var seed = getWalletSeed(wallets[w], options.password);
if (!seed) return outputError("Incorrect password");
//TODO validate amount and address
//TODO integrate extjs
output("This feature is WIP");
};
commands.nft_create = async (handle, options) => {
if (!validateHandle(handle)) return outputError("Wallet handle is invalid");
if (fs.existsSync("./"+handle)) return outputError("A directory already exists: "+handle);
var wallets = conf.get('wallets');
if (!wallets) wallets = {};
var w = conf.get('active-wallet');
if (!w || !wallets.hasOwnProperty(w)) return outputError("You do not have an active wallet. Select an active wallet using extjs wallet use");
//TODO validate input
var principal = wallets[w].principal;
var address = extjs.principalToAccountIdentifier(principal, 0);
var seed = getWalletSeed(wallets[w], options.password);
if (!seed) return outputError("Incorrect password");
var identity = getId(seed);
outputUpdate("Creating NFT canister...");
try {
var resp = await extjs.connect("https://boundary.ic0.app/", identity).canister(NFTFACTORY, NFTFACTORY_IDL).createCanister("houbaias32sgfwr2fg23ywytg2", principal);
} catch(e) {
return outputError("Something went wrong!");
};
var config = {
canister:resp.toText(),
name:(options.name ?? handle),
royaltyAddress:(options.address ?? address),
owner:(options.owner ?? principal),
admin:(options.admin ?? principal),
royaltyFee:(options.amount ?? "0.3"),
};
try {
outputUpdate("Update canister config...");
var nftCanister = extjs.connect("https://boundary.ic0.app/", identity).canister(config.canister, 'ext');
if (config.owner != principal) await nftCanister.ext_setOwner(Principal.fromText(config.owner));
if (config.admin != principal) await nftCanister.ext_setAdmin(Principal.fromText(config.admin));
await nftCanister.ext_setRoyalty(config.royaltyAddress, Math.round(Number(config.royaltyFee)*100000));
await nftCanister.ext_setCollectionMetadata(config.name, JSON.stringify({}));
outputUpdate("Creating project folder...");
fs.mkdirSync("./"+handle);
fs.mkdirSync("./"+handle+"/assets");
fs.mkdirSync("./"+handle+"/thumbnails");
fs.writeFileSync("./"+handle+"/config.json", JSON.stringify(config));
fs.writeFileSync("./"+handle+"/airdrop.txt", "");
} catch(e) {
return outputError("Could not create NFT collection folder!");
};
outputUpdate("");
outputUpdate("View on ICScan: https://icscan.io/canister/"+config.canister);
outputUpdate("");
outputUpdate("Goto your project folder using:");
outputUpdate("cd "+handle);
outputUpdate("");
return output("Your new NFT project has been created!");
};
commands.nft_mint = async (options) => {
if (!fs.existsSync("./config.json")) return outputError("You are not within a valid exponent project directory. You can create a new project using 'exponent nft create'");
var wallets = conf.get('wallets');
if (!wallets) wallets = {};
var w = conf.get('active-wallet');
if (!w || !wallets.hasOwnProperty(w)) return outputError("You do not have an active wallet. Select an active wallet using extjs wallet use");
var seed = getWalletSeed(wallets[w], options.password);
if (!seed) return outputError("Incorrect password");
var principal = wallets[w].principal;
var address = extjs.principalToAccountIdentifier(principal, 0);
var identity = getId(seed);
var assets = fs.readdirSync("./assets");
var thumbnails = fs.readdirSync("./thumbnails");
var missing = assets.filter(x => !thumbnails.includes(x)).concat(thumbnails.filter(x => !assets.includes(x)));
if (missing.length){
let options = { withMetaData: false, width: 300, height: 300, fit : 'cover' }
outputUpdate("Generating thumbnails: "+missing.length);
for(var i = 0; i < missing.length; i++){
try {
const thumbnail = await imageThumbnail("./assets/"+missing[i], options);
await fs.writeFileSync("./thumbnails/"+missing[i], thumbnail);
thumbnails.push(missing[i]);
outputUpdate("Generated thumbnail: "+missing[i]+" ("+(i+1)+"/"+missing.length+")");
} catch (err) {
outputError("Error generating thumbnail: "+missing[i]);
}
};
outputUpdate(" ");
};
var config = JSON.parse(fs.readFileSync("./config.json"));
var nftCanister = extjs.connect("https://boundary.ic0.app/", identity).canister(config.canister, 'ext');
var toUpload = [];
var toMint = [];
outputUpdate("Looking for new assets to mint...");
for(var i = 0; i < assets.length; i++){
var ah = removeFilenameExtension(assets[i]);
var exists = await nftCanister.ext_assetExists(ah);
if (!exists) {
outputUpdate("Asset found: " + assets[i]);
toUpload.push(assets[i]);
};
};
if (toUpload.length){
var end = (toUpload.length == 1 ? "" : "s");
outputUpdate("Found " + toUpload.length + " asset"+end+" to mint...");
outputUpdate(" ");
for(var i = 0; i < toUpload.length; i++){
var filename = toUpload[i];
var ah = removeFilenameExtension(filename);
var asset = "./assets/"+filename;
var thumbnail = "./thumbnails/"+filename;
outputUpdate("Uploading asset: " + filename);
await uploadAsset(nftCanister, ah, filename, asset);
var isThumbnail = fs.existsSync(thumbnail);
if (isThumbnail) {
outputUpdate("Uploading thumbnail...");
await uploadAsset(nftCanister, ah+"_thumbnail", filename, thumbnail);
};
toMint.push(
[
address,
{
nonfungible : {
name : ah,
asset : ah,
thumbnail : (isThumbnail? ah+"_thumbnail" : ""),
metadata : []
}
}
]
);
outputUpdate(" ");
};
var end = (toMint.length == 1 ? "" : "s");
outputUpdate("Minting "+toMint.length+" NFT"+end+"...");
await nftCanister.ext_mint(toMint);
outputUpdate(" ");
output("You have minted all available assets as NFTs!");
};
};
commands.nft_canister = async () => {
if (!fs.existsSync("./config.json")) return outputError("You are not within a valid exponent project directory. You can create a new project using 'exponent nft create'");
var config = JSON.parse(fs.readFileSync("./config.json"));
output("Canister ID: " + config.canister);
outputUpdate("View on ICScan: https://icscan.io/canister/"+config.canister);
};
commands.nft_airdrop = async (options) => {
if (!fs.existsSync("./config.json")) return outputError("You are not within a valid exponent project directory. You can create a new project using 'exponent nft create'");
//TODO integrate extjs
//TODO read airdrop
//TODO prompt
var wallets = conf.get('wallets');
if (!wallets) wallets = {};
var w = conf.get('active-wallet');
if (!w || !wallets.hasOwnProperty(w)) return outputError("You do not have an active wallet. Select an active wallet using extjs wallet use");
var seed = getWalletSeed(wallets[w], options.password);
if (!seed) return outputError("Incorrect password");
};
module.exports = commands;