-
Notifications
You must be signed in to change notification settings - Fork 5
/
bootstrap.js
74 lines (71 loc) · 2.99 KB
/
bootstrap.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
const axios = require('axios');
const dotenv = require('dotenv').config();
const fs = require('fs');
const StellarSdk = require('stellar-sdk');
const bootstrap = async () => {
const assets = JSON.parse(fs.readFileSync('./config/assets.json'));
let issuerKeypair, distKeypair, channelKeypair;
for (const asset of assets) {
const server = new StellarSdk.Server(asset.horizonUrl);
if (!asset.stellar.issuer) {
issuerKeypair = StellarSdk.Keypair.random();
asset.stellar.issuer = issuerKeypair.publicKey();
fs.appendFileSync(`./${process.env.NODE_ENV}.env`, `STELLAR_SECRET_${issuerKeypair.publicKey()}=${issuerKeypair.secret()}\n`)
} else {
issuerKeypair = StellarSdk.Keypair.fromSecret(process.env[`STELLAR_SECRET_${asset.stellar.issuer}`]);
}
if (!asset.distributor) {
distKeypair = StellarSdk.Keypair.random();
asset.distributor = distKeypair.publicKey();
fs.appendFileSync(`./${process.env.NODE_ENV}.env`, `STELLAR_SECRET_${distKeypair.publicKey()}=${distKeypair.secret()}\n`)
} else {
distKeypair = StellarSdk.Keypair.fromSecret(process.env[`STELLAR_SECRET_${asset.distributor}`]);
}
if (!asset.channels || asset.channels.length === 0) {
channelKeypair = StellarSdk.Keypair.random();
asset.channels = [channelKeypair.publicKey()];
fs.appendFileSync(`./${process.env.NODE_ENV}.env`, `STELLAR_SECRET_${channelKeypair.publicKey()}=${channelKeypair.secret()}\n`)
} else {
channelKeypair = StellarSdk.Keypair.fromSecret(process.env[`STELLAR_SECRET_${asset.channels[0]}`]);
}
try {
await axios.get(`https://friendbot.stellar.org/?addr=${asset.stellar.issuer}`);
await axios.get(`https://friendbot.stellar.org/?addr=${asset.distributor}`);
for (const channel of asset.channels) {
await axios.get(`https://friendbot.stellar.org/?addr=${channel}`);
}
} catch(err) {
// ignore
}
console.log(asset);
try {
const distributor = await server.loadAccount(asset.distributor);
const txBuilder = new StellarSdk.TransactionBuilder(distributor, {
networkPassphrase: asset.networkPassphrase,
fee: 100,
});
txBuilder.setTimeout(180);
txBuilder.addOperation(StellarSdk.Operation.changeTrust({
asset: new StellarSdk.Asset(asset.code, asset.stellar.issuer),
source: asset.distributor
}));
txBuilder.addOperation(StellarSdk.Operation.payment({
destination: asset.distributor,
asset: new StellarSdk.Asset(asset.code, asset.stellar.issuer),
amount: '1000',
source: asset.stellar.issuer
}));
const tx = txBuilder.build();
tx.sign(issuerKeypair, distKeypair);
console.log(await server.submitTransaction(tx));
} catch (err) {
console.log(err.response.data);
}
}
fs.writeFileSync('./config/assets.json', JSON.stringify(assets, null, 4));
};
if (process.env.NODE_ENV !== 'production') {
bootstrap();
} else {
console.log('THIS IS NOT MEANT FOR production!');
}