Skip to content

Commit c837b4c

Browse files
author
Zach Alam
committed
new reply & bitfact format
1 parent 0371fb7 commit c837b4c

3 files changed

Lines changed: 95 additions & 85 deletions

File tree

BitFact.js

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -15,47 +15,36 @@ class BitFact {
1515
// Exposed method:
1616
// BitFact's a piece of text.
1717
const hash = sha256(text);
18-
const fact = this.getFact("text", hash, memo);
18+
const fact = this.buildFact("text", hash, memo);
1919
const stamp = await this.stamp(fact); // stamp to chain
20-
return this.formReply(hash, fact, stamp);
20+
return this.formReply(fact, stamp);
2121
}
2222

2323
async file(filePath, memo) {
2424
// Exposed method:
2525
// BitFact's a file (path).
2626
const hash = sha256f(filePath);
27-
const fact = this.getFact("file", hash, memo);
27+
const fact = this.buildFact("file", hash, memo);
2828
const stamp = await this.stamp(fact); // stamp to chain.
29-
return this.formReply(hash, fact, stamp);
30-
}
31-
32-
parse(fact) {
33-
// parse's a bitfact string.
34-
// fact: BitFact:text2|hash:2c29..9824|memo:this is a memo
35-
let parsedFact = new Object();
36-
fact = fact.split("|");
37-
fact.forEach((aFact) => {
38-
// split the fact section. ie: BitFact:text2
39-
const factSect = aFact.split(":");
40-
parsedFact[factSect[0]] = factSect[1];
41-
});
42-
return parsedFact;
29+
return this.formReply(fact, stamp);
4330
}
4431

4532
// --------------------------
4633

47-
formReply(hash, fact, stamp) {
48-
// returns a nicely formatted response for end user.
34+
formReply(factString, tx) {
35+
// returns a nicely formatted response for lib user.
36+
const fact = this.parseFact(factString)
4937
return {
50-
info: this.chain,
51-
fact: this.parse(fact),
52-
hash,
53-
stamp,
38+
txid: tx.transactionHash,
39+
hash: fact.hash,
40+
meta: {
41+
info: this.chain, fact, tx
42+
}
5443
};
5544
}
5645

5746
async stamp(fact) {
58-
// fact: BitFact:text2|hash:2c29..9824|memo:this is a memo
47+
// fact: BitFact({"algo":"sha256","hash":"b94e2efcde9","type":"text","memo":"mymemo"})
5948
let tx = await this.buildTx(fact);
6049
tx = await this.signTx(tx);
6150
tx = await this.broadcastTx(tx);
@@ -112,14 +101,29 @@ class BitFact {
112101
return txCount;
113102
}
114103

115-
getFact(type, hash, memo) {
116-
// this method formats the bitfact hash.
117-
// memos cannot have any of the following: "bitfact", "|", or ":".
118-
memo = memo.toLowerCase();
119-
memo = memo.replace(/bitfact/g, "");
120-
memo = memo.replace(/|/g, "");
121-
memo = memo.replace(/:\s*/g, "");
122-
return "BitFact:" + type + "|sha256:" + hash + "|memo:" + memo;
104+
// --------------------------
105+
106+
parseFact(fact) {
107+
// BitFact(algo:sha256|hash:dabfac484|type:file|memo:testing..)
108+
// this method turns a "bitfact string" into an object.
109+
// algo:sha256|hash:dabfac484|type:file|memo:testing.."
110+
const factString = fact.substring(8, fact.length - 1);
111+
112+
// {algo: "sha256", hash: "b94e2efcde9", type: "text", memo: "mymemo"}
113+
return JSON.parse(factString);
114+
}
115+
116+
buildFact(type, hash, memo) {
117+
// this method turns "object data" into a "bitfact string".
118+
const input = {
119+
algo: "sha256",
120+
hash,
121+
type,
122+
memo,
123+
};
124+
125+
// BitFact({"algo":"sha256","hash":"b94e2efcde9","type":"text","memo":"mymemo"})
126+
return "BitFact(" + JSON.stringify(input) + ")";
123127
}
124128
}
125129

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "bitfact",
3-
"version": "1.0.3",
3+
"version": "1.0.4",
44
"description": "A Javascript library to fingerprint (prove) your data, text, & files on the Ethereum blockchain.",
55
"main": "BitFact.js",
66
"scripts": {

test/BitFact.js

Lines changed: 58 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
const Web3 = require('web3');
2-
const sinon = require('sinon');
1+
const Web3 = require("web3");
2+
const sinon = require("sinon");
33
const chai = require("chai");
44
const assert = chai.assert;
55
const BitFact = require("../BitFact");
66

77
// testing bitfact object (with faked data).
88
const 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

Comments
 (0)