-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
188 lines (172 loc) · 6.19 KB
/
index.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
'use strict';
// STARTED TOP nodejs/browser hack
(function() {
// FINISHED TOP nodejs/browser hack
const bananojs = require('@bananocoin/bananojs');
const index = bananojs;
const bananodeApi = bananojs.bananodeApi;
const BananoHwApp = require('hw-app-nano').Banano;
const transportNodeHid = require('@ledgerhq/hw-transport-node-hid');
const bananoConfig = {};
bananoConfig.walletPrefix = `44'/198'/`;
bananoConfig.prefix = index.BANANO_PREFIX;
bananoConfig.bananodeUrl = 'https://kaliumapi.appditto.com/api';
let config = bananoConfig;
const setConfig = (_config) => {
config = _config;
};
const getConfig = () => {
return config;
};
const getLedgerPath = (accountIndex) => {
return `${config.walletPrefix}${accountIndex}'`;
};
const getLedgerInfo = async () => {
const paths = await transportNodeHid.default.list();
const retval = {};
retval.pathCount = paths.length;
retval.found = retval.pathCount > 0;
if (retval.found) {
const path = paths[0];
const transport = await transportNodeHid.default.open(path);
try {
retval.supported = await transportNodeHid.default.isSupported();
if (retval.supported) {
const banHwAppInst = new BananoHwApp(transport);
try {
retval.config = await banHwAppInst.getAppConfiguration();
} catch (error) {
retval.error = error.message;
}
}
} finally {
await transport.close();
}
}
return retval;
};
const getLedgerAccountData = async (index) => {
// https://github.com/BananoCoin/bananovault/blob/master/src/app/services/ledger.service.ts#L128
try {
const paths = await transportNodeHid.default.list();
const path = paths[0];
const transport = await transportNodeHid.default.open(path);
try {
const banHwAppInst = new BananoHwApp(transport);
const accountData = await banHwAppInst.getAddress(getLedgerPath(index));
accountData.account = accountData.address;
delete accountData.address;
return accountData;
} finally {
await transport.close();
}
} catch (error) {
console.trace('banano getaccount error', error);
}
};
const getLedgerAccountSigner = async (accountIx) => {
/* istanbul ignore if */
if (config === undefined) {
throw Error('config is a required parameter.');
}
/* istanbul ignore if */
if (accountIx === undefined) {
throw Error('accountIx is a required parameter.');
}
// https://github.com/BananoCoin/bananovault/blob/master/src/app/services/ledger.service.ts#L379
const paths = await transportNodeHid.default.list();
const path = paths[0];
const transport = await transportNodeHid.default.open(path);
let accountData;
try {
const banHwAppInst = new BananoHwApp(transport);
const ledgerPath = getLedgerPath(accountIx);
accountData = await banHwAppInst.getAddress(ledgerPath);
} finally {
transport.close();
}
const signer = {};
signer.getPublicKey = () => {
return accountData.publicKey;
};
signer.getAccount = () => {
return accountData.address;
};
signer.signBlock = async (blockData) => {
const transport = await transportNodeHid.default.open(path);
try {
const banHwAppInst = new BananoHwApp(transport);
const ledgerPath = getLedgerPath(accountIx);
// console.log('signer.signBlock', 'blockData', blockData);
const hwBlockData = {};
if (
blockData.previous ==
'0000000000000000000000000000000000000000000000000000000000000000'
) {
hwBlockData.representative = blockData.representative;
hwBlockData.balance = blockData.balance;
hwBlockData.sourceBlock = blockData.link;
} else {
hwBlockData.previousBlock = blockData.previous;
hwBlockData.representative = blockData.representative;
hwBlockData.balance = blockData.balance;
hwBlockData.recipient = index.getBananoAccount(blockData.link);
const cacheBlockData = {};
const cacheBlocks = await bananodeApi.getBlocks(
[blockData.previous],
true,
);
// console.log('signer.signBlock', 'cacheBlocks', cacheBlocks);
const cacheBlock = cacheBlocks.blocks[blockData.previous];
// console.log('signer.signBlock', 'cacheBlock', cacheBlock);
cacheBlockData.previousBlock = cacheBlock.previous;
cacheBlockData.representative = cacheBlock.representative;
cacheBlockData.balance = cacheBlock.balance;
cacheBlockData.recipient = index.getBananoAccount(cacheBlock.link);
// console.log('signer.signBlock', 'cacheBlockData', cacheBlockData);
try {
// const cacheResponse =
await banHwAppInst.cacheBlock(
ledgerPath,
cacheBlockData,
cacheBlock.signature,
);
// console.log('signer.signBlock', 'cacheResponse', cacheResponse);
} catch (error) {
console.log('signer.signBlock', 'error', error.message);
console.trace(error);
}
}
// console.log('signer.signBlock', 'hwBlockData', hwBlockData);
return await banHwAppInst.signBlock(ledgerPath, hwBlockData);
} finally {
transport.close();
}
};
return signer;
};
// STARTED BOTTOM nodejs/browser hack
const exports = (() => {
// istanbul ignore if
if (typeof BigInt === 'undefined') {
return;
}
const exports = {};
exports.bananoConfig = bananoConfig;
exports.bananojs = bananojs;
exports.getLedgerInfo = getLedgerInfo;
exports.setConfig = setConfig;
exports.getConfig = getConfig;
exports.getLedgerPath = getLedgerPath;
exports.getLedgerAccountData = getLedgerAccountData;
exports.getLedgerAccountSigner = getLedgerAccountSigner;
return exports;
})();
// istanbul ignore else
if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
module.exports = exports;
} else {
window.bananocoinBananojsHw = exports;
}
})();
// FINISHED BOTTOM nodejs/browser hack