-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmain.js
148 lines (129 loc) · 4.29 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
143
144
145
146
147
148
'use strict';
const index = require('./index.js');
const bip39 = require('bip39');
const curve = require('tiny-secp256k1');
const { PhantasmaAPI, ScriptBuilder } = require('phantasma-ts');
const bip32Factory = require('bip32').default;
const phantasmaRPC = new PhantasmaAPI('https://testnet.phantasma.io/rpc', undefined, 'testnet');
const transportNodeHid = require('@ledgerhq/hw-transport-node-hid');
const config = {};
config.debug = true;
config.transport = transportNodeHid.default;
config.rpc = phantasmaRPC;
config.bip32Factory = bip32Factory;
config.bip39 = bip39;
config.curve = curve;
config.nexusName = 'testnet';
config.chainName = 'main';
config.tokenNames = ['KCAL', 'SOUL'];
config.gasPrice = 100000;
config.gasLimit = 900;
config.verifyResponse = true;
const commands = {};
commands['linfo'] = async () => {
const response = await index.getLedgerDeviceInfo(config);
console.log('info', response);
};
commands['getpoltergeistmnemonic'] = async (mnemonic) => {
const response = await index.getPoltergeistMnemonic(config, mnemonic);
if (config.debug) {
console.log('getPoltergeistMnemonic response', response);
}
if (response.success) {
console.log('poltergeist mnemonic', response.poltergeistMnemonic);
} else {
console.log('poltergeist mnemonic error', response.message);
}
};
commands['getmbalance'] = async (mnemonic) => {
const response = await index.getBalanceFromMnemonic(config, mnemonic, 0);
if (config.debug) {
console.log('getBalanceFromMnemonic response', response);
}
if (response.success) {
console.log('address', response.address);
console.log('balances', response.balances);
} else {
console.log('balance error', response.message);
}
};
commands['getladdress'] = async (mnemonic) => {
const response = await index.getBalanceFromLedger(config, { verifyOnDevice: true });
if (config.debug) {
console.log('getAddressFromLedger response', response);
}
if (response.success) {
console.log('address', response.address);
} else {
console.log('address error', response.message);
}
};
commands['getlpublic'] = async (mnemonic) => {
const response = await index.getAddressFromLedeger(config, { verifyOnDevice: true });
if (config.debug) {
console.log('getAddressFromLedger response', response);
}
if (response.success) {
console.log('address', response);
} else {
console.log('address error', response);
}
};
commands['getlbanpm start getladdresslance'] = async (mnemonic) => {
const response = await index.getBalanceFromLedger(config, { verifyOnDevice: false });
if (config.debug) {
console.log('getBalanceFromLedger response', response);
}
if (response.success) {
console.log('address', response.address);
console.log('balances', response.balances);
} else {
console.log('balance error', response.message);
}
};
commands['msend'] = async (amount, tokenName, toAddress, mnemonic, debug) => {
if (debug !== undefined) {
config.debug = debug == 'true';
}
const response = await index.sendAmountUsingMnemonic(config, amount, tokenName, toAddress, mnemonic, 0);
if (config.debug) {
console.log('sendAmountUsingMnemonic response', JSON.stringify(response));
}
if (response.success) {
console.log('send success', response.message);
} else {
console.log('send error', response.message);
}
};
commands['lsend'] = async (amount, tokenName, toAddress) => {
console.log('sendAmountUsingLedger', amount, toAddress);
const response = await index.sendAmountUsingLedger(config, amount, tokenName, toAddress);
if (config.debug) {
console.log('sendAmountUsingLedger', 'response', response);
}
if (response.success) {
console.log('send success');
} else {
console.log('send error', response.message);
}
};
const run = async () => {
console.log('phantasma-js-hw');
if (process.argv.length < 3) {
console.log('#usage:');
console.log('https://github.com/coranos/phantasma-js-hw/blob/master/docs/cli.md');
} else {
const command = process.argv[2];
const arg0 = process.argv[3];
const arg1 = process.argv[4];
const arg2 = process.argv[5];
const arg3 = process.argv[6];
const fn = commands[command];
if (fn == undefined) {
console.log('unknown command', command);
} else {
await fn(arg0, arg1, arg2, arg3);
}
}
};
run();