Advertisement
mateuspl

test-bitcoin.js

Jan 20th, 2018
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const fs        = require('fs');
  2. const Bitcoin   = require('../');
  3.  
  4.  
  5. const bitcoin = new Bitcoin('testnet');
  6. const opReturn = '12345678901234567890123456789012345678901234567890123456789012345678901234567890';
  7. const bin = new Buffer(8);
  8. bin.writeDoubleBE(Math.random());
  9.  
  10.  
  11. describe('Bitcoin', function () {
  12.     describe('#register(data)', function () {
  13.         function testRegister(data, done) {
  14.             bitcoin.register(data)
  15.                 .then(txId => {
  16.                     if (typeof txId !== 'string') throw `${txId} is not a string`;
  17.                     if (!/^[a-fA-F0-9]{64}$/.test(txId)) throw `${txId} is not a valid transaction id`;
  18.                     done();
  19.                 })
  20.                 .catch(done);
  21.         }
  22.  
  23.         it('should return the transaction id', function (done) {
  24.             testRegister('', done);
  25.         });
  26.  
  27.         it('should return the transaction id', function (done) {
  28.             testRegister(opReturn, done);
  29.         });
  30.        
  31.         it('should return the transaction id', function (done) {
  32.             testRegister(bin, done);
  33.         });
  34.  
  35.         it('should throw an error', function (done) {
  36.             bitcoin.register(opReturn + opReturn)
  37.                 .then(txId => done('returned'))
  38.                 .catch(() => done());
  39.         });
  40.     });
  41.  
  42.     describe('#verify(txId)', function () {
  43.         function testVerify(data, done) {
  44.             bitcoin.register(data)
  45.                 .then(bitcoin.verify.bind(bitcoin))
  46.                 .then(tx => {
  47.                     if (typeof tx.data !== 'object') throw `data ${tx.data} is not an object`;
  48.                     if (tx.data.toString() !== data.toString()) throw `data ${tx.data} is not equal to ${data}`;
  49.                     if (tx.time <= 0) throw `time ${time} is not greater than 0`;
  50.                     if (tx.confirmations < 0) throw `confirmations ${confirmations} is not equal to or greater than 0`;
  51.                     done();
  52.                 })
  53.                 .catch(done);
  54.         }
  55.  
  56.         it('should return the transaction data', function (done) {
  57.             testVerify(opReturn, done);
  58.         });
  59.        
  60.         it('should return the transaction data', function (done) {
  61.             testVerify(bin, done);
  62.         });
  63.     });
  64. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement