-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlibraryd-js.js
211 lines (173 loc) · 6.86 KB
/
libraryd-js.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// Using a `wallet` from fluffy-enigma
var LibraryDJS = LibraryDJS || {};
// returns signature directly
LibraryDJS.signPublisher = function (wallet, name, address, time) {
// http://api.alexandria.io/#sign-publisher-announcement-message
var toSign = name + "-" + address + "-" + time;
return wallet.signMessage(address, toSign);
};
// returns signature directly
LibraryDJS.signArtifact = function (wallet, ipfs, address, time) {
// http://api.alexandria.io/#sign-publisher-announcement-message
var toSign = ipfs + "-" + address + "-" + time;
return wallet.signMessage(address, toSign);
};
// callback is (errorString, response) response=http://api.alexandria.io/#publish-new-artifact
LibraryDJS.publishArtifact = function (wallet, ipfs, address, alexandriaMedia, callback) {
var time = unixTime();
var test = {
"torrent": "Qmeke1CyonqgKErvGhE18WLBuhrLaScbpSAS6vGLuoSCXM",
"publisher": "F6yEsikfYQPRAEL8FfDzumLqPD9WDPmKtK",
"timestamp": 0,
"type": "music",
"payment": {},
"info": {
"title": "Lady J",
"description": "Lady J with a really long description so it goes into multiple parts and really tests stuff.",
"year": 2003,
"extra-info": {
"filename": "320bit_mp3/10%20Lady%20J.mp3",
"filetype": "album track",
"displayname": "Lady J",
"albumtrack": "10",
"runtime": 241
}
}
};
ipfs = "Qmeke1CyonqgKErvGhE18WLBuhrLaScbpSAS6vGLuoSCXM";
var signature = LibraryDJS.signArtifact(wallet, ipfs, address, time);
var data = {
"alexandria-media": test, //alexandriaMedia,
signature: signature
};
data["alexandria-media"].timestamp = parseInt(time);
data["alexandria-media"].publisher = address;
LibraryDJS.Send(wallet, JSON.stringify(data), address, 0.001, function (err, txIDs) {
if (err != null)
callback(err,
JSON.stringify({
status: "failure",
response: err
}));
else
callback(null,
JSON.stringify({
status: "success",
response: txIDs
}));
});
};
// callback is (errorString, response) response=http://api.alexandria.io/#announce-new-publisher
LibraryDJS.registerPublisher = function (wallet, name, address, bitMessage, email, signature, callback) {
LibraryDJS.announcePublisher(wallet, name, address, bitMessage, email, signature, callback);
};
// callback is (errorString, response) response=http://api.alexandria.io/#announce-new-publisher
LibraryDJS.announcePublisher = function (wallet, name, address, bitMessage, email, callback) {
var time = unixTime();
var signature = LibraryDJS.signPublisher(wallet, name, address, time);
var data = {
"alexandria-publisher": {
"name": name,
"address": address,
"timestamp": parseInt(time),
"bitmessage": bitMessage,
"email": CryptoJS.MD5(email).toString()
},
"signature": signature
};
LibraryDJS.Send(wallet, JSON.stringify(data), address, 0.001, function (err, txIDs) {
if (err != null)
callback(err,
JSON.stringify({
status: "failure",
response: err
}));
else
callback(null,
JSON.stringify({
status: "success",
response: txIDs
}));
});
};
function unixTime() {
// slice is to strip milliseconds
return Date.now().toString().slice(0, -3);
}
// callback is (errorString, txIDs Array)
LibraryDJS.Send = function (wallet, jsonData, address, amount, callback) {
LibraryDJS.sendToBlockChain(wallet, jsonData, address, amount, function (err, txIDs) {
callback(err, txIDs);
});
};
// callback is (errorString, txIDs Array)
LibraryDJS.sendToBlockChain = function (wallet, txComment, address, amount, callback) {
// set tx fee
// feature non existent in js currently
// get new address
// change returns to sender currently
// over sized?
// over sized?
if (txComment.length > (CHOP_MAX_LEN * 10)) {
callback("txComment is too large to fit within 10 multipart transactions. try making it smaller!");
}
else if (txComment.length > TXCOMMENT_MAX_LEN) {
LibraryDJS.multiPart(wallet, txComment, address, amount, callback);
}
else {
wallet.sendCoins(address, address, amount, txComment, function (err, data) {
callback(null, [data.txid]);
});
}
};
// callback is (errorString, txIDs Array)
LibraryDJS.multiPart = function (wallet, txComment, address, amount, callback) {
var txIDs = [];
var multiPartPrefix = "alexandria-media-multipart(";
var chop = LibraryDJS.chopString(txComment);
var part = 0;
var max = chop.length - 1;
// the first reference tx id is always 64 zeros
var reference = new Array(65).join("0");
var data = chop[part];
var preImage = part.toString() + "-" + max.toString() + "-" + address + "-" + reference + "-" + data;
var signature = wallet.signMessage(address, preImage);
var multiPart = multiPartPrefix + part.toString() + "," + max.toString() +
"," + address + "," + reference + "," + signature + "):" + data;
wallet.sendCoins(address, address, amount, multiPart, function (err, data) {
txIDs[txIDs.length] = data.txid;
reference = data.txid;
var count = 0;
for (var i = 1; i <= max; ++i) {
part = i;
data = chop[part];
preImage = part.toString() + "-" + max.toString() + "-" + address + "-" + reference + "-" + data;
signature = wallet.signMessage(address, preImage);
multiPart = multiPartPrefix + part.toString() + "," + max.toString() +
"," + address + "," + reference + "," + signature + "," + "):" + data;
(function (i, address, amount, multiPart) {
setTimeout(function () {
wallet.sendCoins(address, address, amount, multiPart, function (err, data) {
txIDs[txIDs.length] = data.txid;
++count;
if (count == max) {
callback(null, txIDs);
}
});
}, i * 1000);
})(i, address, amount, multiPart);
}
});
};
LibraryDJS.chopString = function (input) {
input = input.toString();
var chunks = [];
while (input.length > CHOP_MAX_LEN) {
chunks[chunks.length] = input.slice(0, CHOP_MAX_LEN);
input = input.slice(CHOP_MAX_LEN);
}
chunks[chunks.length] = input;
return chunks;
};
const CHOP_MAX_LEN = 270;
const TXCOMMENT_MAX_LEN = 528;