-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
executable file
·142 lines (118 loc) · 4.72 KB
/
main.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
#!/usr/bin/env node
const yargs = require('yargs')
const chalk = require('chalk');
var figlet = require('figlet');
const fs = require('fs');
const os = require('os');
const { ArchethicHandler } = require('./lib/handler');
const {
checkFile,
readFile,
writeFile,
baseConfigJson
} = require("./lib/utils");
const BASE_DIR = "/.alca"
const HOME_DIR = os.homedir();
if (!fs.existsSync(HOME_DIR + BASE_DIR)) {
console.log(".alca doesn't exists!! Making Folder with path", HOME_DIR + BASE_DIR);
fs.mkdirSync(HOME_DIR + BASE_DIR);
}
const encrypted_key_plus_wallet = "0401EC530D1BBDF3B1B3E18C6E2330E5CFD1BFD88EB6D84102184CB39EC271793578B469ACBD8EB4F684C41B5DA87712A203AAA910B7964218794E3D3F343835843C44AFFE281D750E6CA526C6FC265167FE37DB9E47828BF80964DAC837E1072CA9954FF1852FF71865B9043BC117BC001C47D76A326A2A2F7CF6B16AB49E9E57F9D5E6D8E1D00D7F1B7E2F986C711DCA060005B2C8F485"
// Always send CURVE_TYPE with address to Ledger DEVICE or else BADDECODE
// receiver = "0200E48824AB31C949F422E916F2439B5505985C2B1ACC6AF9735BDDF1865B071DA7"
const mainInstance = new ArchethicHandler();
yargs.command({
command: 'about',
describe: 'Welcome',
handler: function (argv) {
console.log(chalk.green('\n', 'Welcome to Archethic Ledger CLI !', '\n'))
console.log(chalk.blue(figlet.textSync('AE Ledger CLI', { font: "Alligator2" })))
console.log(chalk.green('\n', 'Send Transactions to Archethic'))
console.log(chalk.green('\n', 'Version - 1.0.0', '\n'))
console.log(chalk.greenBright("Available Commands: \n Use it as ledger_cli `command`"))
console.log(chalk.cyan(" 1. getAppDetails \n 2. getAppVersion \n 3. getPublicKey \n 4. getArchAddress \n 5. sendTxn"))
}
})
yargs.command({
command: 'getAppDetails',
describe: 'Get the version and name for the Ledger Application',
handler: function (argv) {
mainInstance.sendGetAppAndVersion()
}
})
yargs.command({
command: 'getAppVersion',
describe: 'Get the version for the Ledger Application',
handler: function (argv) {
mainInstance.sendGetAppVersion()
}
})
yargs.command({
command: 'getPublicKey',
describe: 'Get the PublicKey of the Ledger Device',
handler: function (argv) {
mainInstance.sendGetPubKey()
}
})
yargs.command({
command: 'getArchAddress',
describe: 'Get the Archethic Address from the Ledger Device',
builder: {
endpoint: {
describe: 'Node Endpoint',
demandOption: false, // Required
type: 'string'
},
},
handler: async function (argv) {
if(await checkFile(HOME_DIR + BASE_DIR)){
const contents = await readFile(HOME_DIR + BASE_DIR);
mainInstance.setEndPoint(argv.endpoint);
if(contents.hasOwnProperty("ADDRESS_INDEX")) {
mainInstance.sendGetArchAddress(contents["ADDRESS_INDEX"], encrypted_key_plus_wallet);
} else {
console.log("Config File have no ADDRESS_INDEX. Starting from ADDRESS_INDEX = 0");
writeFile(HOME_DIR + BASE_DIR ,baseConfigJson);
mainInstance.sendGetArchAddress(null, encrypted_key_plus_wallet);
}
} else {
console.log("Failed to get config file from root Folder.")
}
}
})
yargs.command({
command: 'sendTxn',
describe: 'Send Transaction APDU Payload to the ledger.',
builder: {
endpoint: {
describe: 'Node Endpoint',
demandOption: false, // Required
type: 'string'
},
amount: {
describe: 'Amount to Send in UCOs (e.g 10.0)',
demandOption: true, // Required
type: 'string'
},
receiver: {
describe: 'Address of receiver to receive UCOs. (68 hex characters) Add 02 in front of address if you are using ARCHEthic wallet Address.',
demandOption: true, // Required
type: 'string'
}
},
handler: async function (argv) {
if(await checkFile(HOME_DIR + BASE_DIR)){
const contents = await readFile(HOME_DIR + BASE_DIR);
mainInstance.setEndPoint(argv.endpoint);
if(contents.hasOwnProperty("ADDRESS_INDEX")) {
mainInstance.sendSignTxn(contents["ADDRESS_INDEX"], argv.receiver, argv.amount, encrypted_key_plus_wallet);
} else {
console.log("Config File have no ADDRESS_INDEX. Starting from ADDRESS_INDEX = 0");
mainInstance.sendSignTxn(null, argv.receiver, argv.amount, encrypted_key_plus_wallet);
}
} else {
console.log("Failed to get config file from root Folder.")
}
}
})
yargs.parse();