-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample.js
More file actions
124 lines (102 loc) · 3.13 KB
/
example.js
File metadata and controls
124 lines (102 loc) · 3.13 KB
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
// const moment = require('moment');
const { encode, decode } = require('./src/utils');
const Encoder = require('./src/encoder');
const Printer = require('./src');
const printer = new Printer({ path: 'COM5' });
printer.eventEmitter.on('ERROR', (error) => {
console.log('ERROR', error);
});
async function printReceipt(content) {
console.log('hex', Buffer.from(content).toString('hex'));
try {
// connect printer
if (!printer.port || !printer.port.isOpen) {
await printer.open();
}
// get sensors status
const status = await printer.command('CHECK_SENSOR_STATUS');
console.log('CHECK_SENSOR_STATUS', status);
const { paper, jamSensor } = status.data;
if (!paper) {
throw new Error('Printer is out of paper!');
}
// try to unjam printer
if (jamSensor) {
const initializeResult = await printer.command('RPU_INITIALIZE');
console.log('RPU_INITIALIZE', initializeResult);
const status2 = await printer.command('CHECK_SENSOR_STATUS');
console.log('CHECK_SENSOR_STATUS', status2);
const { jamSensor: jamed2 } = status2.data;
if (jamed2) {
throw new Error('Printer is jamed and failed to self fix');
}
}
// set page start
const iddatddResutl = await printer.command(
'IMAGE_DATA_DOWNLOAD_AND_TEXT_DATA_DOWNLOAD'
);
console.log('IMAGE_DATA_DOWNLOAD_AND_TEXT_DATA_DOWNLOAD', iddatddResutl);
const adpResutl = await printer.command(
'ASCII_DATA_PRINT',
Buffer.from(content)
);
console.log('ASCII_DATA_PRINT', adpResutl);
// print receipt and cut
const cutResult = await printer.command('CUT');
console.log('CUT', cutResult);
} catch (error) {
console.log('printReceipt', error);
}
}
function printPaperWallet() {
const CompanyName = 'CompanyName';
const CoinName = 'Bitcoin';
const PublicKey = 'IFt3cRS13tVz6mnfCnUuYpp3mtPc34Sm36';
const PrivateKey = 'L2FQvDoUUhhUArCUcimeE1lhYdnVDvK6BXLTuy09kAcScbSRr';
const encoder = new Encoder();
const content = encoder
.initialize()
.marginLeft(40)
.align('center')
.bold(true)
.line(CompanyName)
.bold(false)
.line("Here is your paper wallet's PUBLIC KEY:", 46)
.newline()
.qrcode(PublicKey, 1, 0, 'a', 12)
.newline()
.line(PublicKey)
.newline()
.align('left')
.line(
`You may ADD ${CoinName} to this wallet address by scanning the public address above.`,
46
)
.newline()
.align('center')
.line("Here is your paper wallet's PRIVATE KEY:", 46)
.newline()
.qrcode(PrivateKey, 1, 0, 'a', 12)
.newline()
.line(PrivateKey)
.newline()
.line(`Use this key to send or withdraw ${CoinName}`, 46)
.line('DO NOT LOSE OR SHARE THIS PRIVATE KEY.', 46)
.newline()
.cut()
.encode();
printReceipt(content, 'printPaperWallet');
}
// console.log(
// decode('ASCII_DATA_PRINT', Buffer.from('0206004f3030302000035a', 'hex'))
// );
// console.log(
// decode('CHECK_SENSOR_STATUS', Buffer.from('0206005330303020000346', 'hex'))
// );
(async function () {
try {
await printPaperWallet();
} catch (error) {
console.log('error', error);
}
})();