From 54892ccb4ac95de977eb66838169d4b4e2ea59b0 Mon Sep 17 00:00:00 2001 From: Zach Alam Date: Sun, 13 Dec 2020 17:05:15 -0800 Subject: [PATCH] single param constructor --- BitFact.js | 23 +++++++++++++++-------- README.md | 7 ++++--- config.js | 2 +- docs/guide/library.md | 5 +++-- docs/guide/setup.md | 7 ++++--- examples/createBitFactText.js | 12 +++++++----- examples/createKey.js | 10 +++++++--- examples/keys.js | 10 ---------- examples/loadConf.js | 10 ++++++++++ examples/verifyBitFactText.js | 10 +++++++--- 10 files changed, 58 insertions(+), 38 deletions(-) delete mode 100644 examples/keys.js create mode 100644 examples/loadConf.js diff --git a/BitFact.js b/BitFact.js index 79c4a71..4fe1f6f 100644 --- a/BitFact.js +++ b/BitFact.js @@ -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) { @@ -62,7 +69,7 @@ class BitFact { txid, hash: fact.hash, meta: { - info: this.chain, + info: this.options, fact, tx, }, @@ -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(); @@ -116,7 +123,7 @@ class BitFact { async getPublicKey() { return await this.web3.eth.accounts.privateKeyToAccount( - this.options.privateKey.toString() + this.privateKey.toString() ).address; } diff --git a/README.md b/README.md index 44fc8e3..ee7d753 100644 --- a/README.md +++ b/README.md @@ -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); diff --git a/config.js b/config.js index 8311482..d586acd 100644 --- a/config.js +++ b/config.js @@ -1,5 +1,5 @@ module.exports = { BITFACT_ADDR: "0x00000000000000000000000000000000000000Bf", - DEFAULT_CHAIN: {chain: 'mainnet'}, + DEFAULT_OPTIONS: {chain: 'mainnet'}, CONFIG_FILE: 'bitfact.json' } \ No newline at end of file diff --git a/docs/guide/library.md b/docs/guide/library.md index 07df763..548c728 100644 --- a/docs/guide/library.md +++ b/docs/guide/library.md @@ -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. diff --git a/docs/guide/setup.md b/docs/guide/setup.md index e549262..3625168 100644 --- a/docs/guide/setup.md +++ b/docs/guide/setup.md @@ -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 provider here or private key here. diff --git a/examples/createBitFactText.js b/examples/createBitFactText.js index 8f908ef..85dbf64 100644 --- a/examples/createBitFactText.js +++ b/examples/createBitFactText.js @@ -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); })(); diff --git a/examples/createKey.js b/examples/createKey.js index 0e70d20..8a6f84c 100644 --- a/examples/createKey.js +++ b/examples/createKey.js @@ -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()); })(); diff --git a/examples/keys.js b/examples/keys.js deleted file mode 100644 index f674c89..0000000 --- a/examples/keys.js +++ /dev/null @@ -1,10 +0,0 @@ -// load keys. -module.exports = (secrets) => { - let secretsToUse; - try { - secretsToUse = require("./secrets.json"); - } catch (e) { - secretsToUse = secrets; - } - return secretsToUse; -}; diff --git a/examples/loadConf.js b/examples/loadConf.js new file mode 100644 index 0000000..22af504 --- /dev/null +++ b/examples/loadConf.js @@ -0,0 +1,10 @@ +// load keys. +module.exports = (otherConf) => { + let confToUse; + try { + confToUse = require("./bitfact.json"); + } catch (e) { + confToUse = otherConf; + } + return confToUse; +}; diff --git a/examples/verifyBitFactText.js b/examples/verifyBitFactText.js index be1f665..4f914de 100644 --- a/examples/verifyBitFactText.js +++ b/examples/verifyBitFactText.js @@ -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"