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