This repository has been archived by the owner on Jun 23, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
console.js
155 lines (123 loc) · 4.21 KB
/
console.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
#!/usr/bin/env node
'use strict'
const Web3 = require('web3')
const IpfsClient = require('ipfs-http-client')
const program = require('commander')
const settings = require('user-settings').file('.ethavatar')
const EthAvatar = require('./client.js')
const FileHelper = require('./helpers/file.js')
const { version } = require('../package.json')
const __getWeb3Connection = (required, optional) => {
let web3Connection
try {
if (typeof optional.web3 === 'string') {
web3Connection = new Web3(optional.web3)
} else if (typeof settings.get('web3') !== 'undefined') {
web3Connection = new Web3(settings.get('web3'))
} else {
process.stderr.write('Web3 connection not specified!')
process.exit(1)
}
} catch (error) {
process.stderr.write(error.message)
process.exit(1)
}
return web3Connection
}
const __getIpfsConnection = (required, optional) => {
let ipfsConnection
try {
if (typeof optional.ipfs === 'string') {
ipfsConnection = IpfsClient(optional.ipfs)
} else if (typeof settings.get('ipfs') !== 'undefined') {
ipfsConnection = IpfsClient(optional.ipfs)
} else {
ipfsConnection = IpfsClient('https://ipfs.infura.io:5001')
}
} catch (error) {
process.stderr.write(error.message)
process.exit(1)
}
return ipfsConnection
}
const __getFileHelper = (web3Connection, ipfsConnection) => {
const ethavatar = new EthAvatar(web3Connection, ipfsConnection)
const fileHelper = new FileHelper(ethavatar)
return fileHelper
}
const config = (optional) => {
if (typeof optional.web3 === 'string') {
settings.set('web3', optional.web3)
} else if (optional.web3 === true) {
settings.unset('web3')
}
if (typeof optional.ipfs === 'string') {
settings.set('ipfs', optional.ipfs)
} else if (optional.ipfs === true) {
settings.unset('ipfs')
}
const web3Conn = typeof settings.get('web3') !== 'undefined' ? settings.get('web3') : 'Not set'
const ipfsConn = typeof settings.get('ipfs') !== 'undefined' ? settings.get('ipfs') : 'Not set'
process.stdout.write('Current Web3 connection: ' + web3Conn + '\n')
process.stdout.write('Current IPFS connection: ' + ipfsConn)
}
const get = async (required, optional) => {
const web3Connection = __getWeb3Connection(required, optional)
const ipfsConnection = __getIpfsConnection(required, optional)
const fileHelper = __getFileHelper(web3Connection, ipfsConnection)
let address
if (typeof optional.address === 'string') {
address = optional.address
} else {
address = (await web3Connection.eth.getAccounts())[0]
}
try {
await fileHelper.toFile(required, address)
} catch (error) {
process.stderr.write(error.message)
process.exit(1)
}
process.stdout.write(`Avatar of address ${address} has been written to file ${required}`)
process.exit(0)
}
const set = async (required, optional) => {
const web3Connection = __getWeb3Connection(required, optional)
const ipfsConnection = __getIpfsConnection(required, optional)
const fileHelper = __getFileHelper(web3Connection, ipfsConnection)
try {
await fileHelper.fromFile(required)
} catch (error) {
process.stderr.write(error.message)
process.exit(1)
}
const address = (await web3Connection.eth.getAccounts())[0]
process.stdout.write(`Avatar of address ${address} from file ${required} has been uploaded to blockchain`)
process.exit(0)
}
program
.version(version)
.description('JavaScript API for EthAvatar')
program
.command('config')
.description('Set Web3 and IPFS connection')
.option('--web3 [optional]', 'Web3 Conenction')
.option('--ipfs [optional]', 'IPFS Conenction')
.action(config)
program
.command('get <filename>')
.description('Get avatar of address to file')
.option('--web3 [optional]', 'Web3 Conenction')
.option('--ipfs [optional]', 'IPFS Conenction')
.option('--address [optional]', 'Ethereum address or ENS domain')
.action(get)
program
.command('set <filename>')
.description('Set avatar of address from file')
.option('--web3 [optional]', 'Web3 Conenction')
.option('--ipfs [optional]', 'IPFS Conenction')
.action(set)
if (process.argv.length === 2) {
process.stdout.write(program.helpInformation())
} else {
program.parse(process.argv)
}