Skip to content

Commit

Permalink
single param constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
Zach Alam committed Dec 14, 2020
1 parent fa013c1 commit 54892cc
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 38 deletions.
23 changes: 15 additions & 8 deletions BitFact.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,17 @@ const sha256f = require("sha256-file");
const config = require("./config");

class BitFact {
constructor(options, chain = config.DEFAULT_CHAIN) {
this.options = options; // ie: { provider: 'https://...' privateKey: '4109c982fa'}
this.chain = chain; // ie: { chain: "ropsten" }
this.web3 = new Web3(options.provider);
constructor(setup) {
/* setup example
{
"provider": "https://eth-ropsten.alchemyapi.io/v2/01GesjZxWhg-KMfDuLH_-aUOmV-bRBaf",
"privateKey": "67ccc16df9e7581ec11e7483c7eba5f2ae937b7ab37db413bad46470165629cf",
"options": { "chain": "ropsten" }
}
*/
this.privateKey = setup.privateKey; // ie: 67ccc16df9e7581ec11e7483c7eba5f2ae937b7ab37db413bad46470165629cf
this.options = setup.options ? setup.options : config.DEFAULT_OPTIONS; // ie: { chain: "ropsten" }
this.web3 = new Web3(setup.provider);
}

async stampText(text, memo) {
Expand Down Expand Up @@ -62,7 +69,7 @@ class BitFact {
txid,
hash: fact.hash,
meta: {
info: this.chain,
info: this.options,
fact,
tx,
},
Expand Down Expand Up @@ -94,8 +101,8 @@ class BitFact {

async signTx(txObj) {
// Signs a TX object.
const tx = new Tx.Transaction(txObj, this.chain);
const pk = Buffer.from(this.options.privateKey, "hex");
const tx = new Tx.Transaction(txObj, this.options);
const pk = Buffer.from(this.privateKey, "hex");

tx.sign(pk);
return tx.serialize();
Expand All @@ -116,7 +123,7 @@ class BitFact {

async getPublicKey() {
return await this.web3.eth.accounts.privateKeyToAccount(
this.options.privateKey.toString()
this.privateKey.toString()
).address;
}

Expand Down
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,15 @@ The code is well tested and fully documented. Pull requests to docs are welcome.
- ⛽ Tiny gas fees (~21,000) - the same as sending ether.
- 😎 Use with: mainnet (live), ropsten (testnet), & beaconchain (eth2).

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

const receipt = await bitfact.stampText("Hello World!", "this is my memo");
console.log(receipt);
Expand Down
2 changes: 1 addition & 1 deletion config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = {
BITFACT_ADDR: "0x00000000000000000000000000000000000000Bf",
DEFAULT_CHAIN: {chain: 'mainnet'},
DEFAULT_OPTIONS: {chain: 'mainnet'},
CONFIG_FILE: 'bitfact.json'
}
5 changes: 3 additions & 2 deletions docs/guide/library.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ yarn add bitfact
const BitFact = require("bitfact"); // load from npm or yarn
const bitfact = new BitFact({
provider: "https://mainnet.infura.io/v3/37a0db22401bbe211112",
privateKey: "321d3fa232e55dedee2bd914273f78897f69053b61437c5"
}, {chain: 'mainnet'});
privateKey: "321d3fa232e55dedee2bd914273f78897f69053b61437c5",
options: {chain: 'mainnet'}
});
```
*Optional* 2nd Parameter: **chain** can be of value `mainnet` or `ropsten`. If ignored, `mainnet` will be used.

Expand Down
7 changes: 4 additions & 3 deletions docs/guide/setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
const BitFact = require("bitfact"); // load from npm or yarn
const bitfact = new BitFact({
provider: "https://mainnet.infura.io/v3/37a0db22401bbe211112",
privateKey: "321d3fa232e55dedee2bd914273f78897f69053b61437c5"
}, {chain: 'mainnet'});
privateKey: "321d3fa232e55dedee2bd914273f78897f69053b61437c5",
options: {chain: 'mainnet'}
});
```

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

?> **Need help initializing the class?**
Learn how to get a <a href="/#/guide/providers">provider here</a> or <a href="/#/guide/privateKeys">private key here</a>.
12 changes: 7 additions & 5 deletions examples/createBitFactText.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
// NOTE: Update to USE from a package manager instead.
const BitFact = require("../BitFact");
const keys = require("./keys");
const theKeys = keys({ provider: "", privateKey: "" });

console.log(theKeys);
const loadConf = require("./loadConf");
const setup = loadConf({
provider: "",
privateKey: "",
options: { chain: "ropsten" },
});

// creates a BitFact
(async () => {
const bitfact = new BitFact(theKeys,{chain:'ropsten'});
const bitfact = new BitFact(setup);
const receipt = await bitfact.stampText("Hello World!", "hello world memo");
console.log(receipt);
})();
10 changes: 7 additions & 3 deletions examples/createKey.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
// NOTE: Update to USE from a package manager instead.
const BitFact = require("../BitFact");
const keys = require("./keys");
const theKeys = keys({ provider: "", privateKey: "" });
const loadConf = require("./loadConf");
const setup = loadConf({
provider: "",
privateKey: "",
options: { chain: "ropsten" },
});

// creates an Ethereum keypair
(async () => {
console.log(await new BitFact(theKeys).createKeypair());
console.log(await new BitFact(setup).createKeypair());
})();
10 changes: 0 additions & 10 deletions examples/keys.js

This file was deleted.

10 changes: 10 additions & 0 deletions examples/loadConf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// load keys.
module.exports = (otherConf) => {
let confToUse;
try {
confToUse = require("./bitfact.json");
} catch (e) {
confToUse = otherConf;
}
return confToUse;
};
10 changes: 7 additions & 3 deletions examples/verifyBitFactText.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
// NOTE: Update to USE from a package manager instead.
const BitFact = require("../BitFact");
const keys = require("./keys");
const theKeys = keys({ provider: "", privateKey: "" });
const loadConf = require("./loadConf");
const setup = loadConf({
provider: "",
privateKey: "",
options: { chain: "ropsten" },
});

// verifies a BitFact text
(async () => {
const bitfact = new BitFact(theKeys);
const bitfact = new BitFact(setup);
const isStampedText = await bitfact.verifyText(
"Hello World!",
"0xefb2678cc4eb62586184d751189357c7ee4adc10dd4be188c8f61705942a25d9"
Expand Down

0 comments on commit 54892cc

Please sign in to comment.