-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.js
197 lines (176 loc) · 5.91 KB
/
helper.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
189
190
191
192
193
194
195
196
197
require('dotenv').config();
const nem = require('nem-sdk').default;
const log4js = require('log4js');
log4js.configure({
appenders: {
stdout: { type: 'stdout' },
app: { type: 'file', filename: `log/${process.env.NODE_ENV || 'development'}.log` }
},
categories: {
default: { appenders: ['stdout', 'app'], level: 'error' }
}
});
const NETWORK = process.env.NEM_NETWORK === 'mainnet' ?
nem.model.network.data.mainnet.id :
nem.model.network.data.testnet.id
;
const ENDPOINT = nem.model.objects.create('endpoint')(
process.env.NEM_NODE || nem.model.nodes.defaultTestnet,
nem.model.nodes.defaultPort
);
let NEM_EPOCH = Date.UTC(2015, 2, 29, 0, 6, 25, 0);
function println(val) {
if (val instanceof Array) {
val.forEach(el => { println(el); })
} else {
console.log(val);
}
}
function logger(tag = 'default') {
let logger = log4js.getLogger(tag);
logger.level = process.env.NODE_ENV === 'production' ? 'info' : 'debug';
return logger;
}
function NEMTimestamp(timestamp) {
if(timestamp == null) { return null; }
return ~~((timestamp - NEM_EPOCH) / 1000);
}
function getMosaic(mosaics, fqn) {
let splitedFqn = splitNsAndMo(fqn);
let ns = splitedFqn[0];
let mo = splitedFqn[1];
if(! mosaics) { return null; }
let mosaic = null;
let len = Object.keys(mosaics).length;
for(let i = 0; i < len; i++) {
let id = mosaics[i].mosaicId;
if(id.namespaceId === ns && id.name === mo) {
mosaic = mosaics[i];
}
}
return mosaic;
}
function fetchTx(dir) {
return (address, timeStamp) => {
let now = nem.utils.helpers.createNEMTimeStamp();
timeStamp = NEMTimestamp(timeStamp) || now - 3600;
let stack = [];
function fetch_inner(dir, address, id) {
return nem.com.requests.account.transactions[dir](ENDPOINT, address, null, id).then(res => {
let txes = res.data;
let last = txes[txes.length-1];
stack = stack.concat(txes);
if(last && last.transaction.timeStamp <= timeStamp) {
return fetch_inner(dir, address, last.meta.id);
} else {
return stack;
}
})
}
return fetch_inner(dir, address);
};
}
function searchOwnedMosaic(address, fqn) {
return nem.com.requests.account.mosaics.owned(ENDPOINT, address).then(res => {
attachment = searchOwnedMoAttachmentArray(res.data, fqn);
return Promise.resolve(attachment);
});
}
function searchOwnedMoAttachmentArray(attachments, fqn) {
let splitedFqn = splitNsAndMo(fqn);
let ns = splitedFqn[0];
let mo = splitedFqn[1];
for(let k = 0; k < attachments.length; k++) {
let moId = attachments[k].mosaicId;
if(moId.namespaceId == ns && moId.name == mo){ return attachments[k]; }
}
}
function splitNsAndMo(fqn){
parts = fqn.split('.');
mo = parts.pop();
ns = parts.join('.');
return [ns, mo]
}
function sendMosaic(address, fqn, msg = '', pkey) {
let splitedFqn = splitNsAndMo(fqn);
let ns = splitedFqn[0];
let mo = splitedFqn[1];
let common = nem.model.objects.create('common')('', pkey);
let transferTx = nem.model.objects.create('transferTransaction')(address, 1, msg);
let moDefMetaDataPair = nem.model.objects.get('mosaicDefinitionMetaDataPair');
// let moAttachment1 = nem.model.objects.create('moAttachment')('nem', 'xem', 1);
// transferTx.mosaics.push(moAttachment1);
let moAttachment2 = nem.model.objects.create('mosaicAttachment')(ns, mo, 1);
transferTx.mosaics.push(moAttachment2);
return nem.com.requests.namespace.mosaicDefinitions(ENDPOINT, moAttachment2.mosaicId.namespaceId).then((res) => {
let neededDefinition = nem.utils.helpers.searchMosaicDefinitionArray(res.data, [mo]);
fullMosaicName = nem.utils.format.mosaicIdToName(moAttachment2.mosaicId);
// if(undefined === neededDefinition[fullMosaicName]) return logger.error(`${mo} Mosaic not found!`);
moDefMetaDataPair[fullMosaicName] = {};
moDefMetaDataPair[fullMosaicName].mosaicDefinition = neededDefinition[fullMosaicName];
let txEntity = nem.model.transactions.prepare('mosaicTransferTransaction')(
common,
transferTx,
moDefMetaDataPair,
NETWORK
);
// return new Promise((resolve) => { resolve(txEntity); });
// return Promise.resolve(txEntity);
return nem.model.transactions.send(common, txEntity, ENDPOINT);
// }).then((txEntity) => {
// return nem.model.transactions.send(common, txEntity, ENDPOINT);
})
}
function sendMessage(address, message, pkey) {
let common = nem.model.objects.create('common')('', pkey);
let transferTx = nem.model.objects.create('transferTransaction')(
address,
0,
message
);
return nem.com.requests.account.data(ENDPOINT, address).then(data => {
transferTx.messageType = 2;
transferTx.recipientPublicKey = data.account.publicKey;
let txEntity = nem.model.transactions.prepare('transferTransaction')(
common,
transferTx,
NETWORK
);
// return Promise.resolve(txEntity);
return nem.model.transactions.send(common, txEntity, ENDPOINT);
// }).then((txEntity) => {
// return nem.model.transactions.send(common, txEntity, ENDPOINT);
})
}
function privkey2addr(privkey, network = NETWORK) {
let kp = nem.crypto.keyPair.create(privkey);
return pubkey2addr(kp.publicKey.toString(), network);
}
function pubkey2addr(pubkey, network = NETWORK) {
return nem.model.address.toAddress(pubkey, network);
}
function addr2pubkey(address) {
return nem.com.requests.account.data(ENDPOINT, address).then(data => {
return data.account.publicKey;
})
}
function decryptMessage(pkey, pubkey, message) {
if(message.type === 1) { return message; }
let decrypted = nem.crypto.helpers.decode(pkey, pubkey, message.payload);
let decoded = nem.utils.format.hexToUtf8(decrypted);
return {type: 1, payload: decrypted, decoded: decoded};
}
module.exports = {
decryptMessage,
getMosaic,
NEMTimestamp,
fetchTx,
searchOwnedMosaic,
sendMosaic,
sendMessage,
addr2pubkey,
pubkey2addr,
privkey2addr,
logger,
println
};