-
Notifications
You must be signed in to change notification settings - Fork 1
/
arlog-deploy.js
199 lines (156 loc) · 5.3 KB
/
arlog-deploy.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
// deploy to ArLog
/**
APPS to ARWEAVE
DATA to IPLD, with optional follow-on to Arweave at user's discretion
PUBLISH STEP:
1. Save this version to Arweave, get Arweave Tx ID
2. Save this version Tx ID to the Arlog namespace: arlog.list(ownerAddress) arlog.name == 'this_project_name'
3. If namespace doesn't exist, make the Arweave/ArLog contract: arlog.createNewLog('use_wallet');
No need to remember the contract ID, as this is associated with the name of the ArLog :)
use an existing contract to update: await $arlog.write(keyfile, contractID, input);
*/
import ArLog from '../arlog/dist/arlog';
import config from '../arlog/dist/config';
import fs from 'fs';
import spawn from 'cross-spawn';
import path from 'path';
import { fileURLToPath } from 'url';
import { dirname } from 'path';
import CliCommands from '../arkb/bin/commands';
import { setArweaveInstance } from '../arkb/bin/utils/utils';
import Conf from 'conf';
const conf = new Conf();
const cliCommands = new CliCommands.default();
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const SLASH = path.sep;
const name = 'iframe-wallet';
const CHOSEN_NET = 'AR_LOCAL';
const arweaveKeyfileName = {
LOCAL_TEST_NET: `../test-keyfile.json`,
AR_LOCAL: `../test-keyfile.json`,
DEV_NET: `../test-keyfile.json`,
MAIN_NET: `../arweave-keyfile.json`
};
const arweaveKeyfileRel = arweaveKeyfileName[CHOSEN_NET];
const arweaveKeyfile = path.resolve(`${__dirname}${SLASH}${arweaveKeyfileRel}`);
const arlogConfigJsonName = `arlog.config.json`;
const arlogConfigJson = path.resolve(`${__dirname}${SLASH}${arlogConfigJsonName}`);
function readJsonfile(keypairfile) {
let kf = fs.readFileSync(keypairfile);
return JSON.parse(kf.toString());
}
const source = `export function handle(state, action) {
if (action.caller !== state.owner) {
return { state }; // readonly unless you'r the owner
}
if (action.input.function === 'Update') {
state.latest = action.input.latest;
}
return { state };
}
`;
(async () => {
const payerWallet = readJsonfile(arweaveKeyfile);
const arlog = new ArLog({ arweave: config.networks[CHOSEN_NET] });
const addr = await arlog.arweave.wallets.getAddress(payerWallet);
console.log({ addr });
const bal = await config.networks[CHOSEN_NET].mint(arlog, addr);
await config.networks[CHOSEN_NET].mine(arlog);
await config.networks[CHOSEN_NET].mine(arlog);
console.log('\nAirdroped mint ', bal);
// Save this app to Arweave, get a Tx ID
const args = [
'--ipfs-publish',
'--auto-confirm',
'--gateway',
`${config.networks[CHOSEN_NET].protocol}://${config.networks[CHOSEN_NET].host}:${config.networks[CHOSEN_NET].port}`,
'deploy',
`./build`,
'--wallet',
arweaveKeyfileRel
];
// console.log({ args });
// console.log('Spawning...');
var savedOutput = [];
// let result = spawn.sync('arkb', args, {
// cwd: process.cwd(),
// env: process.env,
// stdio: 'pipe',
// encoding: 'utf-8'
// });
process.on('uncaughtException', function (err) {
console.log({ err });
});
// minimist style
const argv = {
_: ['deploy', './build'],
'ipfs-publish': true,
'auto-confirm': true,
gateway: `${config.networks[CHOSEN_NET].protocol}://${config.networks[CHOSEN_NET].host}:${config.networks[CHOSEN_NET].port}`,
wallet: arweaveKeyfileRel
};
const debug = false;
const blockweave = setArweaveInstance(argv, debug);
const manifestTx = await cliCommands.cliTask({
argv,
config: conf,
debug,
blockweave
});
console.log({ manifestTx });
await config.networks[CHOSEN_NET].mine(arlog);
await config.networks[CHOSEN_NET].mine(arlog);
// result.on('data', function (data) {
// console.log('stdout: ' + data);
// data = data.toString();
// savedOutput.push(data);
// });
// result.on('close', function (data) {
// console.log('Spawn closed ');
// console.log(String(savedOutput));
// fs.writeFileSync(`./logs/${+new Date()}.json`, savedOutput);
// });
// console.log('Spawn Complete.');
// var savedOutput = result.stdout; // get the resulting TX ID
// console.log(String(savedOutput));
// fs.writeFileSync(`./logs/${+new Date()}.txt`, savedOutput);
async function createLog() {
console.log('Creating ArLog \n ', txID);
const opts = { name, ipfs: 'TBD', arweave: 'TBD', source };
const txID = await arlog.createNewLog(payerWallet, opts);
// await config.networks[CHOSEN_NET].mine(arlog);
// add txID to build
console.log('Created contract \n ', txID);
fs.writeFileSync(arlogConfigJson, JSON.stringify({ contractID: txID }));
}
async function updateLog() {
const configJson = readJsonfile(arlogConfigJson);
const contractID = configJson.contractID;
const latestIPFSCID = configJson.latestIPFSCID; // TODO: Save this app to IPFS
let latest = {};
latest['ipfs'] = latestIPFSCID;
latest['arweave'] = 'TBD'; // TODO: Save the ipfs2arweave first
let input = {
function: 'Update',
latest
};
const txid = await arlog.write(payerWallet, contractID, input);
console.log('ArLog updated \n ', txID);
console.log({ txid });
}
/**
TODO: move this to arlog library
TODO: Find all SmartWeaves for this own (ardb)
read through contracts
filter based on name
if no name exists, make a new one with the name
if name exists, update it.
*/
if (!fs.existsSync(arlogConfigJson)) {
// createLog();
} else {
// Save this app to IPFS
// updateLog();
}
})();