Skip to content

Commit 54892cc

Browse files
author
Zach Alam
committed
single param constructor
1 parent fa013c1 commit 54892cc

10 files changed

Lines changed: 58 additions & 38 deletions

File tree

BitFact.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,17 @@ const sha256f = require("sha256-file");
55
const config = require("./config");
66

77
class BitFact {
8-
constructor(options, chain = config.DEFAULT_CHAIN) {
9-
this.options = options; // ie: { provider: 'https://...' privateKey: '4109c982fa'}
10-
this.chain = chain; // ie: { chain: "ropsten" }
11-
this.web3 = new Web3(options.provider);
8+
constructor(setup) {
9+
/* setup example
10+
{
11+
"provider": "https://eth-ropsten.alchemyapi.io/v2/01GesjZxWhg-KMfDuLH_-aUOmV-bRBaf",
12+
"privateKey": "67ccc16df9e7581ec11e7483c7eba5f2ae937b7ab37db413bad46470165629cf",
13+
"options": { "chain": "ropsten" }
14+
}
15+
*/
16+
this.privateKey = setup.privateKey; // ie: 67ccc16df9e7581ec11e7483c7eba5f2ae937b7ab37db413bad46470165629cf
17+
this.options = setup.options ? setup.options : config.DEFAULT_OPTIONS; // ie: { chain: "ropsten" }
18+
this.web3 = new Web3(setup.provider);
1219
}
1320

1421
async stampText(text, memo) {
@@ -62,7 +69,7 @@ class BitFact {
6269
txid,
6370
hash: fact.hash,
6471
meta: {
65-
info: this.chain,
72+
info: this.options,
6673
fact,
6774
tx,
6875
},
@@ -94,8 +101,8 @@ class BitFact {
94101

95102
async signTx(txObj) {
96103
// Signs a TX object.
97-
const tx = new Tx.Transaction(txObj, this.chain);
98-
const pk = Buffer.from(this.options.privateKey, "hex");
104+
const tx = new Tx.Transaction(txObj, this.options);
105+
const pk = Buffer.from(this.privateKey, "hex");
99106

100107
tx.sign(pk);
101108
return tx.serialize();
@@ -116,7 +123,7 @@ class BitFact {
116123

117124
async getPublicKey() {
118125
return await this.web3.eth.accounts.privateKeyToAccount(
119-
this.options.privateKey.toString()
126+
this.privateKey.toString()
120127
).address;
121128
}
122129

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,15 @@ The code is well tested and fully documented. Pull requests to docs are welcome.
3434
- ⛽ Tiny gas fees (~21,000) - the same as sending ether.
3535
- 😎 Use with: mainnet (live), ropsten (testnet), & beaconchain (eth2).
3636

37-
## Quickstart
37+
## Use Programatically
3838
🚗 The only thing you need to drive is an Ethereum `provider` and `privateKey`.
3939
```javascript
4040
const BitFact = require("bitfact"); // load from npm or yarn
4141
const bitfact = new BitFact({
4242
provider: "https://mainnet.infura.io/v3/37a0db22401bbe211112",
43-
privateKey: "321d3fa232e55dedee2bd914273f78897f69053b61437c5"
44-
}, {chain: 'mainnet'});
43+
privateKey: "321d3fa232e55dedee2bd914273f78897f69053b61437c5",
44+
options: {chain: 'mainnet'}
45+
});
4546

4647
const receipt = await bitfact.stampText("Hello World!", "this is my memo");
4748
console.log(receipt);

config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module.exports = {
22
BITFACT_ADDR: "0x00000000000000000000000000000000000000Bf",
3-
DEFAULT_CHAIN: {chain: 'mainnet'},
3+
DEFAULT_OPTIONS: {chain: 'mainnet'},
44
CONFIG_FILE: 'bitfact.json'
55
}

docs/guide/library.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ yarn add bitfact
1515
const BitFact = require("bitfact"); // load from npm or yarn
1616
const bitfact = new BitFact({
1717
provider: "https://mainnet.infura.io/v3/37a0db22401bbe211112",
18-
privateKey: "321d3fa232e55dedee2bd914273f78897f69053b61437c5"
19-
}, {chain: 'mainnet'});
18+
privateKey: "321d3fa232e55dedee2bd914273f78897f69053b61437c5",
19+
options: {chain: 'mainnet'}
20+
});
2021
```
2122
*Optional* 2nd Parameter: **chain** can be of value `mainnet` or `ropsten`. If ignored, `mainnet` will be used.
2223

docs/guide/setup.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44
const BitFact = require("bitfact"); // load from npm or yarn
55
const bitfact = new BitFact({
66
provider: "https://mainnet.infura.io/v3/37a0db22401bbe211112",
7-
privateKey: "321d3fa232e55dedee2bd914273f78897f69053b61437c5"
8-
}, {chain: 'mainnet'});
7+
privateKey: "321d3fa232e55dedee2bd914273f78897f69053b61437c5",
8+
options: {chain: 'mainnet'}
9+
});
910
```
1011

11-
*Optional* 2nd Parameter: **chain** can be of value `mainnet` or `ropsten`. If ignored, `mainnet` will be used.
12+
*Optional* value: **chain** can be of value `mainnet` or `ropsten`. If ignored, `mainnet` will be used.
1213

1314
?> **Need help initializing the class?**
1415
Learn how to get a <a href="/#/guide/providers">provider here</a> or <a href="/#/guide/privateKeys">private key here</a>.

examples/createBitFactText.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
// NOTE: Update to USE from a package manager instead.
22
const BitFact = require("../BitFact");
3-
const keys = require("./keys");
4-
const theKeys = keys({ provider: "", privateKey: "" });
5-
6-
console.log(theKeys);
3+
const loadConf = require("./loadConf");
4+
const setup = loadConf({
5+
provider: "",
6+
privateKey: "",
7+
options: { chain: "ropsten" },
8+
});
79

810
// creates a BitFact
911
(async () => {
10-
const bitfact = new BitFact(theKeys,{chain:'ropsten'});
12+
const bitfact = new BitFact(setup);
1113
const receipt = await bitfact.stampText("Hello World!", "hello world memo");
1214
console.log(receipt);
1315
})();

examples/createKey.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
// NOTE: Update to USE from a package manager instead.
22
const BitFact = require("../BitFact");
3-
const keys = require("./keys");
4-
const theKeys = keys({ provider: "", privateKey: "" });
3+
const loadConf = require("./loadConf");
4+
const setup = loadConf({
5+
provider: "",
6+
privateKey: "",
7+
options: { chain: "ropsten" },
8+
});
59

610
// creates an Ethereum keypair
711
(async () => {
8-
console.log(await new BitFact(theKeys).createKeypair());
12+
console.log(await new BitFact(setup).createKeypair());
913
})();

examples/keys.js

Lines changed: 0 additions & 10 deletions
This file was deleted.

examples/loadConf.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// load keys.
2+
module.exports = (otherConf) => {
3+
let confToUse;
4+
try {
5+
confToUse = require("./bitfact.json");
6+
} catch (e) {
7+
confToUse = otherConf;
8+
}
9+
return confToUse;
10+
};

examples/verifyBitFactText.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
// NOTE: Update to USE from a package manager instead.
22
const BitFact = require("../BitFact");
3-
const keys = require("./keys");
4-
const theKeys = keys({ provider: "", privateKey: "" });
3+
const loadConf = require("./loadConf");
4+
const setup = loadConf({
5+
provider: "",
6+
privateKey: "",
7+
options: { chain: "ropsten" },
8+
});
59

610
// verifies a BitFact text
711
(async () => {
8-
const bitfact = new BitFact(theKeys);
12+
const bitfact = new BitFact(setup);
913
const isStampedText = await bitfact.verifyText(
1014
"Hello World!",
1115
"0xefb2678cc4eb62586184d751189357c7ee4adc10dd4be188c8f61705942a25d9"

0 commit comments

Comments
 (0)