1 | jasmine.getEnv().defaultTimeoutInterval = 50000;
|
2 |
|
3 | var openpublish = require("../src/index");
|
4 |
|
5 | var bitcoin = require("bitcoinjs-lib");
|
6 | var File = require("file-api").File;
|
7 | var blockcast = require("blockcast");
|
8 | var fs = require('fs');
|
9 | var crypto = require("crypto");
|
10 | var request = require("request");
|
11 |
|
12 | var txHexToJSON = require('bitcoin-tx-hex-to-json');
|
13 |
|
14 | var env = require('node-env-file');
|
15 | env('./.env');
|
16 |
|
17 | var BLOCKCYPHER_TOKEN = process.env.BLOCKCYPHER_TOKEN;
|
18 |
|
19 | var commonBlockchain = require('blockcypher-unofficial')({
|
20 | key: BLOCKCYPHER_TOKEN,
|
21 | network: "testnet"
|
22 | });
|
23 |
|
24 | var testCommonWallet = require('test-common-wallet');
|
25 |
|
26 | var aliceWallet = testCommonWallet({
|
27 | seed: "test",
|
28 | network: "testnet",
|
29 | commonBlockchain: commonBlockchain
|
30 | });
|
31 |
|
32 | var bobWallet = testCommonWallet({
|
33 | seed: "test1",
|
34 | network: "testnet",
|
35 | commonBlockchain: commonBlockchain
|
36 | });
|
37 |
|
38 | var inMemoryCommonBlockchain = require("mem-common-blockchain")();
|
39 |
|
40 | var inMemoryAliceWallet = testCommonWallet({
|
41 | seed: "test",
|
42 | network: "testnet",
|
43 | commonBlockchain: inMemoryCommonBlockchain
|
44 | });
|
45 |
|
46 | var createRandomString = function(length) {
|
47 | var characters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
|
48 | var output = '';
|
49 | for (var i = 0; i < length; i++) {
|
50 | var r = Math.floor(Math.random() * characters.length);
|
51 | output += characters.substring(r, r + 1);
|
52 | }
|
53 | return output;
|
54 | };
|
55 |
|
56 | var createRandomFile = function(options, callback) {
|
57 | var fileName = options.fileName;
|
58 | var string = options.string;
|
59 | var path = "./test/" + fileName;
|
60 | fs.writeFile(path, string, function(err) {
|
61 | callback(path);
|
62 | });
|
63 | };
|
64 |
|
65 | var sha1 = "dc724af18fbdd4e59189f5fe768a5f8311527050";
|
66 |
|
67 | describe("open-publish", function() {
|
68 |
|
69 | var fileBuffer = new Buffer("testing");
|
70 | var fileName = "test.txt";
|
71 | var fileType = "text/plain";
|
72 | var fileTitle = "A text file for testing";
|
73 | var fileKeywords = "test, text, txt";
|
74 | var fileBtih = "335400c43179bb1ad0085289e4e60c0574e6252e";
|
75 | var fileSha1 = "dc724af18fbdd4e59189f5fe768a5f8311527050";
|
76 | var fileIpfs = "QmcJf1w9bVpquGdzCp86pX4K21Zcn7bJBUtrBP1cr2NFuR";
|
77 |
|
78 | var testData0 = {
|
79 | op: "r",
|
80 | btih: fileBtih,
|
81 | sha1: fileSha1,
|
82 | name: fileName,
|
83 | size: fileBuffer.length,
|
84 | type: fileType,
|
85 | title: fileTitle,
|
86 | keywords: fileKeywords
|
87 | }
|
88 |
|
89 | it("should get the number of transaction payloads", function(done) {
|
90 | var file = new File({
|
91 | name: fileName,
|
92 | type: fileType,
|
93 | buffer: fileBuffer
|
94 | });
|
95 | openpublish.getPayloadsLength({
|
96 | file: file,
|
97 | title: fileTitle,
|
98 | keywords: fileKeywords,
|
99 | commonWallet: aliceWallet,
|
100 | commonBlockchain: commonBlockchain
|
101 | }, function(err, payloadsLength) {
|
102 | expect(payloadsLength).toBe(3);
|
103 | done();
|
104 | });
|
105 | });
|
106 |
|
107 | it("should publish a small text file", function(done) {
|
108 | var file = new File({
|
109 | name: fileName,
|
110 | type: fileType,
|
111 | buffer: fileBuffer
|
112 | });
|
113 | openpublish.register({
|
114 | file: file,
|
115 | title: fileTitle,
|
116 | keywords: fileKeywords,
|
117 | commonWallet: aliceWallet,
|
118 | commonBlockchain: commonBlockchain
|
119 | }, function(err, receipt) {
|
120 | var data = receipt.data;
|
121 | expect(data.op).toBe("r");
|
122 | expect(data.btih).toBe(fileBtih);
|
123 | expect(data.sha1).toBe(fileSha1);
|
124 | expect(data.ipfs).toBe(fileIpfs);
|
125 | expect(data.name).toBe(fileName);
|
126 | expect(data.size).toBe(fileBuffer.length);
|
127 | expect(data.type).toBe(fileType);
|
128 | expect(data.title).toBe(fileTitle);
|
129 | expect(data.uri).not.toBeDefined();
|
130 | expect(data.keywords).toBe(fileKeywords);
|
131 | var blockcastTx = receipt.blockcastTx;
|
132 | expect(blockcastTx.txid).toBeDefined();
|
133 | expect(blockcastTx.transactionTotal).toBe(3);
|
134 | done();
|
135 | });
|
136 | });
|
137 |
|
138 | it("should register then transfer ownership", function(done) {
|
139 |
|
140 | var randomBufferSize = 48;
|
141 | var randomFileName = 'randomFile.txt';
|
142 | var randomString = createRandomString(randomBufferSize);
|
143 |
|
144 | var assetValue = 50000000;
|
145 | var bitcoinValue = 12345;
|
146 |
|
147 | var bobWalletSignPrimaryTxHex = function(txHex, callback) {
|
148 | var tx = txHexToJSON(txHex);
|
149 | expect(tx.vin.length).toBe(2);
|
150 | expect(tx.vout.length).toBe(4);
|
151 | expect(tx.vout[0].value).toBe(bitcoinValue);
|
152 | expect(tx.vout[2].value).toBe(0);
|
153 | expect(tx.vout[0].scriptPubKey.type).toBe("pubkeyhash");
|
154 | expect(tx.vout[1].scriptPubKey.type).toBe("pubkeyhash");
|
155 | expect(tx.vout[2].scriptPubKey.type).toBe("nonstandard");
|
156 | expect(tx.vout[3].scriptPubKey.type).toBe("pubkeyhash");
|
157 | bobWallet.signRawTransaction({txHex: txHex, input: 0}, callback);
|
158 | };
|
159 |
|
160 | createRandomFile({string: randomString, fileName: randomFileName}, function(path) {
|
161 |
|
162 | var randomFile = new File(path);
|
163 | randomFile.size = randomBufferSize;
|
164 |
|
165 | var ttl = 365;
|
166 |
|
167 | openpublish.register({
|
168 | file: randomFile,
|
169 | commonWallet: aliceWallet,
|
170 | commonBlockchain: commonBlockchain
|
171 | }, function(err, receipt) {
|
172 |
|
173 | var registerData = receipt.data;
|
174 | var sha1 = registerData.sha1;
|
175 |
|
176 | openpublish.transfer({
|
177 | assetValue: assetValue,
|
178 | bitcoinValue: bitcoinValue,
|
179 | ttl: ttl,
|
180 | sha1: sha1,
|
181 | assetWallet: aliceWallet,
|
182 | bitcoinWallet: bobWallet,
|
183 | bitcoinWalletSignPrimaryTxHex: bobWalletSignPrimaryTxHex,
|
184 | commonBlockchain: commonBlockchain
|
185 | }, function(err, receipt) {
|
186 | var transferData = receipt.data;
|
187 | expect(transferData.op).toBe("t");
|
188 | expect(transferData.sha1).toBe(sha1);
|
189 | expect(transferData.value).toBe(assetValue);
|
190 | expect(transferData.ttl).toBe(ttl);
|
191 | expect(receipt.blockcastTx.txid).toBeDefined();
|
192 | done();
|
193 | });
|
194 | });
|
195 | });
|
196 | });
|
197 |
|
198 | it("should find an open publish register transaction", function(done) {
|
199 | openpublish.scanSingle({
|
200 | txid: 'a37cd6234df77d3bbd8955336ec16bbc5ae24908c066bde29163834461b6b856',
|
201 | commonBlockchain: commonBlockchain
|
202 | }, function(err, data) {
|
203 | expect(data.op).toBe("r");
|
204 | expect(data.btih).toBe(fileBtih);
|
205 | expect(data.sha1).toBe(fileSha1);
|
206 | expect(data.name).toBe(fileName);
|
207 | expect(data.size).toBe(fileBuffer.length);
|
208 | expect(data.type).toBe(fileType);
|
209 | expect(data.title).toBe(fileTitle);
|
210 | expect(data.uri).not.toBeDefined();
|
211 | expect(data.keywords).toBe(fileKeywords);
|
212 | done();
|
213 | });
|
214 | });
|
215 |
|
216 | it("should publish and then find an open publish transaction (inMemoryCommonBlockchain)", function(done) {
|
217 | var file = new File({
|
218 | name: fileName,
|
219 | type: fileType,
|
220 | buffer: fileBuffer
|
221 | });
|
222 | openpublish.register({
|
223 | file: file,
|
224 | title: fileTitle,
|
225 | keywords: fileKeywords,
|
226 | name: fileName,
|
227 | sha1: fileSha1,
|
228 | btih: fileBtih,
|
229 | size: fileBuffer.legnth,
|
230 | type: fileType,
|
231 | commonWallet: inMemoryAliceWallet,
|
232 | commonBlockchain: inMemoryCommonBlockchain
|
233 | }, function(err, receipt) {
|
234 | var blockcastTx = receipt.blockcastTx;
|
235 | var txid = blockcastTx.txid;
|
236 | openpublish.scanSingle({
|
237 | txid: txid,
|
238 | commonBlockchain: inMemoryCommonBlockchain
|
239 | }, function(err, data) {
|
240 | expect(data.op).toBe("r");
|
241 | expect(data.btih).toBe(fileBtih);
|
242 | expect(data.sha1).toBe(fileSha1);
|
243 | expect(data.name).toBe(fileName);
|
244 | expect(data.size).toBe(fileBuffer.length);
|
245 | expect(data.type).toBe(fileType);
|
246 | expect(data.title).toBe(fileTitle);
|
247 | expect(data.uri).not.toBeDefined();
|
248 | expect(data.keywords).toBe(fileKeywords);
|
249 | done();
|
250 | });
|
251 | });
|
252 | });
|
253 |
|
254 | it("should tip an openpublish document", function(done) {
|
255 | var amount = 20000;
|
256 | var destination = "mqMsBiNtGJdwdhKr12TqyRNE7RTvEeAkaR";
|
257 | openpublish.tip({
|
258 | destination: destination,
|
259 | sha1: sha1,
|
260 | amount: amount,
|
261 | commonWallet: aliceWallet,
|
262 | commonBlockchain: commonBlockchain
|
263 | }, function(error, tipTx) {
|
264 | expect(tipTx.tipDestinationAddress).toBe(destination);
|
265 | expect(tipTx.openpublishSha1).toBe(sha1);
|
266 | expect(tipTx.tipAmount).toBe(amount);
|
267 | expect(tipTx.txid).toBeDefined();
|
268 | expect(tipTx.propagateResponse).toBe('success');
|
269 | done();
|
270 | });
|
271 | });
|
272 |
|
273 | it("should scan an opentip", function(done) {
|
274 | var txid = "b32192c9d2d75a8a28dd4034ea61eacb0dfe4f226acb502cfe108df20fbddebc";
|
275 | openpublish.scanSingle({
|
276 | txid: txid,
|
277 | commonBlockchain: commonBlockchain
|
278 | }, function(err, tip) {
|
279 | expect(tip.openpublishSha1).toBe(sha1);
|
280 | expect(tip.tipAmount).toBe(20000);
|
281 | expect(tip.tipDestinationAddresses[0]).toBe("mqMsBiNtGJdwdhKr12TqyRNE7RTvEeAkaR");
|
282 | done();
|
283 | });
|
284 | });
|
285 |
|
286 | }); |
\ | No newline at end of file |