1- const Web3 = require ( ' web3' ) ;
2- const sinon = require ( ' sinon' ) ;
1+ const Web3 = require ( " web3" ) ;
2+ const sinon = require ( " sinon" ) ;
33const chai = require ( "chai" ) ;
44const assert = chai . assert ;
55const BitFact = require ( "../BitFact" ) ;
66
77// testing bitfact object (with faked data).
88const bitfact = new BitFact ( {
99 provider : "https://mainnet.infura.io/v3/37a0db22401bbe211112" , // no http requests used in tests
10- privateKey : "67ccc16df9e7581ec11e7483c7eba5f2ae937b7ab37db413bad46470165629cf" ,
10+ privateKey :
11+ "67ccc16df9e7581ec11e7483c7eba5f2ae937b7ab37db413bad46470165629cf" ,
1112} ) ;
1213
1314// -------
@@ -19,32 +20,36 @@ describe("BitFact", () => {
1920 } ) ;
2021 } ) ;
2122 describe ( "formReply()" , ( ) => {
22- it ( "object should return 4 keys" , async ( ) => {
23- const reply = bitfact . formReply ( "hash" , "fact" , "call" ) ;
24- assert . equal ( Object . keys ( reply ) . length , 4 ) ;
25- assert . include ( Object . keys ( reply ) , 'hash' ) ;
26- assert . include ( Object . keys ( reply ) , 'fact' ) ;
27- assert . include ( Object . keys ( reply ) , 'stamp' ) ;
28- assert . include ( Object . keys ( reply ) , 'info' ) ;
23+ const bf = 'BitFact({"algo":"sha256","hash":"b94e2efcde9","type":"text","memo":"this is memo-izing"})' ;
24+ const reply = bitfact . formReply ( bf , {
25+ to : '0xface74f0d85cf2fc5a7cd4f55258493c0535f89b' ,
26+ transactionHash : '0x8978f838f6e3f10fb87478c5e6d2cdcddc3b451b39e09d1bba0974d9e4086a96' ,
27+ transactionIndex : 4
28+ } ) ;
29+
30+ it ( "object should return 3 keys" , async ( ) => {
31+ assert . equal ( Object . keys ( reply ) . length , 3 ) ;
2932 } ) ;
3033 it ( "keys should have correct names" , async ( ) => {
31- const reply = bitfact . formReply ( "hash" , "fact" , "call" ) ;
32- assert . include ( Object . keys ( reply ) , 'hash' ) ;
33- assert . include ( Object . keys ( reply ) , 'fact' ) ;
34- assert . include ( Object . keys ( reply ) , 'stamp' ) ;
35- assert . include ( Object . keys ( reply ) , 'info' ) ;
34+ assert . include ( Object . keys ( reply ) , "txid" ) ;
35+ assert . include ( Object . keys ( reply ) , "hash" ) ;
36+ assert . include ( Object . keys ( reply ) , "meta" ) ;
37+ } ) ;
38+ it ( ".txid and .hash should be strings" , async ( ) => {
39+ assert . isString ( reply . txid ) ;
40+ assert . isString ( reply . hash ) ;
3641 } ) ;
37- it ( ".info and .fact should be objects" , async ( ) => {
38- const reply = bitfact . formReply ( "hash" , "fact" , "call" ) ;
39- assert . isObject ( reply . fact ) ;
40- assert . isObject ( reply . info )
42+ it ( ".meta should be an object" , async ( ) => {
43+ assert . isObject ( reply . meta ) ;
4144 } ) ;
4245 } ) ;
4346
4447 describe ( "getPublicKey()" , ( ) => {
4548 it ( "should return public key" , async ( ) => {
4649 const testKey = "0x9BDf7a7F7FDF391b6EFD32D16c2594ADE09Ff041" ;
47- sinon . stub ( bitfact . web3 . eth . accounts , "privateKeyToAccount" ) . returns ( { address : testKey } ) ;
50+ sinon
51+ . stub ( bitfact . web3 . eth . accounts , "privateKeyToAccount" )
52+ . returns ( { address : testKey } ) ;
4853 const publicKey = await bitfact . getPublicKey ( ) ;
4954 assert . equal ( publicKey , testKey ) ;
5055 } ) ;
@@ -58,67 +63,68 @@ describe("BitFact", () => {
5863 } ) ;
5964 describe ( "getGasPrice()" , ( ) => {
6065 it ( "should return number" , async ( ) => {
61- sinon . stub ( bitfact . web3 . eth , "getGasPrice" ) . returns ( ' 100000000' ) ;
66+ sinon . stub ( bitfact . web3 . eth , "getGasPrice" ) . returns ( " 100000000" ) ;
6267 const gasPrice = await bitfact . getGasPrice ( ) ;
63- assert . isString ( gasPrice ) ; // gas price is a string.
68+ assert . isString ( gasPrice ) ; // gas price is a string.
6469 } ) . timeout ( 5000 ) ;
6570 } ) ;
66- describe ( "getFact()" , ( ) => {
67- const memoDetails = [
68- "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9" ,
69- "hash of 'hello world'" ,
70- ] ;
71- const memo = bitfact . getFact ( 'text' , memoDetails [ 0 ] , memoDetails [ 1 ] ) ;
72- it ( "should be a matching string" , ( ) => {
73- assert . isString ( memo ) ;
74- } ) ;
75- it ( "should match expected string" , ( ) => {
76- assert . equal (
77- memo ,
78- "BitFact:text|sha256:" +
79- memoDetails [ 0 ] +
80- "|memo:" +
81- memoDetails [ 1 ]
82- ) ;
83- } ) ;
84- } ) ;
8571 describe ( "buildTx()" , ( ) => {
8672 it ( "should return object" , async ( ) => {
8773 const txObject = await bitfact . buildTx ( ) ;
88- assert . isObject ( txObject ) ; // is object
74+ assert . isObject ( txObject ) ; // is object
8975 } ) ;
9076 } ) ;
9177 describe ( "signTx()" , ( ) => {
9278 it ( "should return object" , async ( ) => {
9379 const signedTx = await bitfact . signTx ( {
94- blank : true
80+ blank : true ,
9581 } ) ;
96- assert . equal ( typeof signedTx , ' object' ) ; // returns a buffer, if "object" via js but not chai.
82+ assert . equal ( typeof signedTx , " object" ) ; // returns a buffer, if "object" via js but not chai.
9783 } ) ;
9884 } ) ;
9985 describe ( "broadcastTx()" , ( ) => {
10086 it ( "should return object" , async ( ) => {
10187 const signedTx = await bitfact . signTx ( {
102- blank : true
88+ blank : true ,
10389 } ) ;
10490 sinon . stub ( bitfact . web3 . eth , "sendSignedTransaction" ) . returns ( {
105- transactionHash : ' 0x60868331cbe9ba5e2f39edccac324646ca4536d'
91+ transactionHash : " 0x60868331cbe9ba5e2f39edccac324646ca4536d" ,
10692 } ) ;
10793 const broadcastedTx = await bitfact . broadcastTx ( signedTx ) ;
10894
10995 assert . isObject ( broadcastedTx ) ;
11096 } ) ;
11197 } ) ;
112- describe ( "parse()" , ( ) => {
98+
99+ // ------------------
100+
101+ describe ( "buildFact()" , ( ) => {
102+ const info = {
103+ algo : "sha256" ,
104+ hash : "b94e2efcde9" ,
105+ type : "text" ,
106+ memo : "this is memo-izing" ,
107+ } ;
108+ const input = bitfact . buildFact ( info . type , info . hash , info . memo ) ;
109+ it ( "should be a string" , ( ) => {
110+ assert . isString ( input ) ;
111+ } ) ;
112+ it ( "should match expected string." , ( ) => {
113+ assert . equal ( input , "BitFact(" + JSON . stringify ( info ) + ")" ) ;
114+ } ) ;
115+ } ) ;
116+ describe ( "parseFact()" , ( ) => {
117+ const parsedFact = bitfact . parseFact (
118+ 'BitFact({"algo":"sha256","hash":"b94e2efcde9","type":"text","memo":"this is memo-izing"})'
119+ ) ;
113120 it ( "should return a object from string" , async ( ) => {
114- const parsedFact = bitfact . parse ( 'BitFact:file|sha256:testing123|memo:meow' ) ;
115121 assert . isObject ( parsedFact ) ;
116122 } ) ;
117123 it ( "should have expected matching values" , async ( ) => {
118- const fact = bitfact . parse ( 'BitFact:text|sha256:foobar|memo:none for now' ) ;
119- assert . equal ( fact . BitFact , 'text' ) ;
120- assert . equal ( fact . sha256 , 'foobar' ) ;
121- assert . equal ( fact . memo , 'none for now' ) ;
124+ assert . equal ( parsedFact . algo , "sha256" ) ;
125+ assert . equal ( parsedFact . hash , "b94e2efcde9" ) ;
126+ assert . equal ( parsedFact . type , "text" ) ;
127+ assert . equal ( parsedFact . memo , "this is memo-izing" ) ;
122128 } ) ;
123129 } ) ;
124130} ) ;
0 commit comments