Skip to content

Commit

Permalink
need to be explicit about web3.js dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
JakubNer committed Apr 24, 2019
1 parent 8758cb7 commit 51ab1bc
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 90 deletions.
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@ The *business logic* does not leverage *ledgers.js*--the *library* is intended f

## Getting Started

The *ledgers.js* library [source file](https://github.com/overhide/ledgers.js/blob/master/ledgers.js) is the distributable artifact. You can simply copy it ([or the minfied variant](https://github.com/overhide/ledgers.js/blob/master/ledgers.min.js)).
The *ledgers.js* library [source file](https://github.com/overhide/ledgers.js/blob/master/ledgers.js) is the distributable artifact.

If you're using *npm* simply: `npm install ledgers.js --save-prod`.

Keep in mind *ledgers.js* is meant to run with a DOM present--in a browser.
The library has no exports, it has a global `oh$` object. If using *webpack* simply `import "ledgers.js"` to bring in the `oh$` object.

Keep in mind *ledgers.js* is meant to run with a DOM present--in a browser (not *Node.js*).

### Quick and Dirty :: CDN Distribution

Expand All @@ -40,6 +42,11 @@ You can include *ledgers.js* via CDN:

For a specific version, e.g. version *1.0.5*: `https://cdn.jsdelivr.net/npm/ledgers.js@1.0.5/ledgers.min.js`

> *Important*
>
> The *ledgers.js* library depends on *web3.js*: if using CDN you must explicitly load *web3.js* beforehand. It is not
> sufficient to depend on wallet injection for this.
**(¬-_-)¬** create an *index.html* file with the following contents:

```
Expand Down
2 changes: 1 addition & 1 deletion demo/login.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.4.1/semantic.min.js"></script>
<script src="https://cdn.jsdelivr.net/gh/ethereum/web3.js@1.0.0-beta.37/dist/web3.min.js"></script>
<!--script src="https://cdn.jsdelivr.net/gh/ethereum/web3.js@1.0.0-beta.37/dist/web3.min.js"></script-->
<script src="ignore/logging.js"></script> <!-- helper JS functionality to support showing source code for this file in the UI -->
<script src="ignore/login.ui.js"></script> <!-- helper JS functionality to support login.ui.html -->
<script src="config.js"></script> <!-- CONFIG shared between login.html and service.html -->
Expand Down
99 changes: 56 additions & 43 deletions docs/demo/ledgers.js
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@
const OHLEDGER_IMPARTER_TAG = 'ohledger'
const OHLEDGER_WEB3_IMPARTER_TAG = 'ohledger-web3'

var imparterTags = [OHLEDGER_IMPARTER_TAG];
var imparterTags = [];

var data = {
ETH_WEB3_IMPARTER_TAG: {
Expand Down Expand Up @@ -475,57 +475,70 @@
*
* @ignore
*/
function detectWeb3Wallet() {
function detectWeb3Wallet() {
if (typeof Web3 != 'function') {
console.log('web3 library not loaded/injected :: skipping');
return;
}

imparterTags = [OHLEDGER_IMPARTER_TAG]; // at least imported web3 available for OHLEDGER
window.web3 = new Web3();

if (!window.ethereum) return;

// Modern dapp browsers...
if (window.ethereum) {
(async () => {
try {
await ethereum.enable();
window.web3 = new Web3(ethereum);
} catch (e) {/*noop*/ }

await detectWalletCb();
(async () => {
try {
await window.ethereum.enable();
window.web3 = new Web3(window.ethereum);
} catch (e) {/*noop*/ }

setInterval(async function () {
await detectWalletCb();
}, WALLET_CHECK_INTERVAL_MS);
})();
await detectWalletCb();

var detectWalletCb = async () => {
try {
var currentAddress = (await window.web3.eth.getAccounts())[0];
var currentNetwork = (await window.web3.eth.net.getNetworkType());
} catch (e) {/*noop*/ }
if (currentAddress !== data.ETH_WEB3_IMPARTER_TAG.walletAddress) {
let imparterTagIndex = imparterTags.findIndex(v => v === ETH_WEB3_IMPARTER_TAG);
if (imparterTagIndex && !currentAddress) {
imparterTags.splice(imparterTagIndex,1);
} else if (!imparterTagIndex && currentAddress) {
imparterTags.push(ETH_WEB3_IMPARTER_TAG);
imparterTags.push(OHLEDGER_WEB3_IMPARTER_TAG);
}
data.ETH_WEB3_IMPARTER_TAG.walletAddress = currentAddress;
data.OHLEDGER_WEB3_IMPARTER_TAG.walletAddress = currentAddress;
if (root.oh$.onWalletChange) {
root.oh$.onWalletChange(ETH_WEB3_IMPARTER_TAG,!!currentAddress);
root.oh$.onWalletChange(OHLEDGER_WEB3_IMPARTER_TAG, !!currentAddress);
}
if (root.oh$.onCredentialsUpdate && currentAddress) {
root.oh$.onCredentialsUpdate(ETH_WEB3_IMPARTER_TAG, {address:currentAddress});
root.oh$.onCredentialsUpdate(OHLEDGER_WEB3_IMPARTER_TAG, { address: currentAddress });
}
setInterval(async function () {
await detectWalletCb();
}, WALLET_CHECK_INTERVAL_MS);
})();

var detectWalletCb = async () => {
try {
console.log('go go go');
var currentAccounts = await window.web3.eth.getAccounts();
if (!currentAccounts || !currentAccounts.length) return;
var currentAddress = currentAccounts[0];
var currentNetwork = (await window.web3.eth.net.getNetworkType());
} catch (e) {
/*noop*/
console.log('error');
return;
}
if (currentAddress !== data.ETH_WEB3_IMPARTER_TAG.walletAddress) {
let imparterTagIndex = imparterTags.findIndex(v => v === ETH_WEB3_IMPARTER_TAG);
if (imparterTagIndex && !currentAddress) {
imparterTags.splice(imparterTagIndex,1);
} else if (!imparterTagIndex && currentAddress) {
imparterTags.push(ETH_WEB3_IMPARTER_TAG);
imparterTags.push(OHLEDGER_WEB3_IMPARTER_TAG);
}
if (currentNetwork !== data.ETH_WEB3_IMPARTER_TAG.network) {
if (root.oh$.onNetworkChange) {
root.oh$.onNetworkChange(ETH_WEB3_IMPARTER_TAG, {name:currentNetwork, uri: data.ETH_WEB3_IMPARTER_TAG.remuneration_uri[currentNetwork]});
}
data.ETH_WEB3_IMPARTER_TAG.network = currentNetwork;
data.ETH_WEB3_IMPARTER_TAG.walletAddress = currentAddress;
data.OHLEDGER_WEB3_IMPARTER_TAG.walletAddress = currentAddress;
if (root.oh$.onWalletChange) {
root.oh$.onWalletChange(ETH_WEB3_IMPARTER_TAG,!!currentAddress);
root.oh$.onWalletChange(OHLEDGER_WEB3_IMPARTER_TAG, !!currentAddress);
}
if (root.oh$.onCredentialsUpdate && currentAddress) {
root.oh$.onCredentialsUpdate(ETH_WEB3_IMPARTER_TAG, {address:currentAddress});
root.oh$.onCredentialsUpdate(OHLEDGER_WEB3_IMPARTER_TAG, { address: currentAddress });
}
}
if (currentNetwork !== data.ETH_WEB3_IMPARTER_TAG.network) {
if (root.oh$.onNetworkChange) {
root.oh$.onNetworkChange(ETH_WEB3_IMPARTER_TAG, {name:currentNetwork, uri: data.ETH_WEB3_IMPARTER_TAG.remuneration_uri[currentNetwork]});
}
data.ETH_WEB3_IMPARTER_TAG.network = currentNetwork;
}
}
}
}

function getImparterTags() {
return imparterTags;
Expand Down
2 changes: 1 addition & 1 deletion docs/demo/login.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.4.1/semantic.min.js"></script>
<script src="https://cdn.jsdelivr.net/gh/ethereum/web3.js@1.0.0-beta.37/dist/web3.min.js"></script>
<!--script src="https://cdn.jsdelivr.net/gh/ethereum/web3.js@1.0.0-beta.37/dist/web3.min.js"></script-->
<script src="ignore/logging.js"></script> <!-- helper JS functionality to support showing source code for this file in the UI -->
<script src="ignore/login.ui.js"></script> <!-- helper JS functionality to support login.ui.html -->
<script src="config.js"></script> <!-- CONFIG shared between login.html and service.html -->
Expand Down
99 changes: 56 additions & 43 deletions ledgers.js
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@
const OHLEDGER_IMPARTER_TAG = 'ohledger'
const OHLEDGER_WEB3_IMPARTER_TAG = 'ohledger-web3'

var imparterTags = [OHLEDGER_IMPARTER_TAG];
var imparterTags = [];

var data = {
ETH_WEB3_IMPARTER_TAG: {
Expand Down Expand Up @@ -475,57 +475,70 @@
*
* @ignore
*/
function detectWeb3Wallet() {
function detectWeb3Wallet() {
if (typeof Web3 != 'function') {
console.log('web3 library not loaded/injected :: skipping');
return;
}

imparterTags = [OHLEDGER_IMPARTER_TAG]; // at least imported web3 available for OHLEDGER
window.web3 = new Web3();

if (!window.ethereum) return;

// Modern dapp browsers...
if (window.ethereum) {
(async () => {
try {
await ethereum.enable();
window.web3 = new Web3(ethereum);
} catch (e) {/*noop*/ }

await detectWalletCb();
(async () => {
try {
await window.ethereum.enable();
window.web3 = new Web3(window.ethereum);
} catch (e) {/*noop*/ }

setInterval(async function () {
await detectWalletCb();
}, WALLET_CHECK_INTERVAL_MS);
})();
await detectWalletCb();

var detectWalletCb = async () => {
try {
var currentAddress = (await window.web3.eth.getAccounts())[0];
var currentNetwork = (await window.web3.eth.net.getNetworkType());
} catch (e) {/*noop*/ }
if (currentAddress !== data.ETH_WEB3_IMPARTER_TAG.walletAddress) {
let imparterTagIndex = imparterTags.findIndex(v => v === ETH_WEB3_IMPARTER_TAG);
if (imparterTagIndex && !currentAddress) {
imparterTags.splice(imparterTagIndex,1);
} else if (!imparterTagIndex && currentAddress) {
imparterTags.push(ETH_WEB3_IMPARTER_TAG);
imparterTags.push(OHLEDGER_WEB3_IMPARTER_TAG);
}
data.ETH_WEB3_IMPARTER_TAG.walletAddress = currentAddress;
data.OHLEDGER_WEB3_IMPARTER_TAG.walletAddress = currentAddress;
if (root.oh$.onWalletChange) {
root.oh$.onWalletChange(ETH_WEB3_IMPARTER_TAG,!!currentAddress);
root.oh$.onWalletChange(OHLEDGER_WEB3_IMPARTER_TAG, !!currentAddress);
}
if (root.oh$.onCredentialsUpdate && currentAddress) {
root.oh$.onCredentialsUpdate(ETH_WEB3_IMPARTER_TAG, {address:currentAddress});
root.oh$.onCredentialsUpdate(OHLEDGER_WEB3_IMPARTER_TAG, { address: currentAddress });
}
setInterval(async function () {
await detectWalletCb();
}, WALLET_CHECK_INTERVAL_MS);
})();

var detectWalletCb = async () => {
try {
console.log('go go go');
var currentAccounts = await window.web3.eth.getAccounts();
if (!currentAccounts || !currentAccounts.length) return;
var currentAddress = currentAccounts[0];
var currentNetwork = (await window.web3.eth.net.getNetworkType());
} catch (e) {
/*noop*/
console.log('error');
return;
}
if (currentAddress !== data.ETH_WEB3_IMPARTER_TAG.walletAddress) {
let imparterTagIndex = imparterTags.findIndex(v => v === ETH_WEB3_IMPARTER_TAG);
if (imparterTagIndex && !currentAddress) {
imparterTags.splice(imparterTagIndex,1);
} else if (!imparterTagIndex && currentAddress) {
imparterTags.push(ETH_WEB3_IMPARTER_TAG);
imparterTags.push(OHLEDGER_WEB3_IMPARTER_TAG);
}
if (currentNetwork !== data.ETH_WEB3_IMPARTER_TAG.network) {
if (root.oh$.onNetworkChange) {
root.oh$.onNetworkChange(ETH_WEB3_IMPARTER_TAG, {name:currentNetwork, uri: data.ETH_WEB3_IMPARTER_TAG.remuneration_uri[currentNetwork]});
}
data.ETH_WEB3_IMPARTER_TAG.network = currentNetwork;
data.ETH_WEB3_IMPARTER_TAG.walletAddress = currentAddress;
data.OHLEDGER_WEB3_IMPARTER_TAG.walletAddress = currentAddress;
if (root.oh$.onWalletChange) {
root.oh$.onWalletChange(ETH_WEB3_IMPARTER_TAG,!!currentAddress);
root.oh$.onWalletChange(OHLEDGER_WEB3_IMPARTER_TAG, !!currentAddress);
}
if (root.oh$.onCredentialsUpdate && currentAddress) {
root.oh$.onCredentialsUpdate(ETH_WEB3_IMPARTER_TAG, {address:currentAddress});
root.oh$.onCredentialsUpdate(OHLEDGER_WEB3_IMPARTER_TAG, { address: currentAddress });
}
}
if (currentNetwork !== data.ETH_WEB3_IMPARTER_TAG.network) {
if (root.oh$.onNetworkChange) {
root.oh$.onNetworkChange(ETH_WEB3_IMPARTER_TAG, {name:currentNetwork, uri: data.ETH_WEB3_IMPARTER_TAG.remuneration_uri[currentNetwork]});
}
data.ETH_WEB3_IMPARTER_TAG.network = currentNetwork;
}
}
}
}

function getImparterTags() {
return imparterTags;
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
},
"homepage": "https://github.com/overhide/ledgers.js#readme",
"dependencies": {
"web3": "1.0.0-beta.52"
},
"dev-dependencies": {
"copyfiles": "2.1.0",
"documentation": "9.3.1",
"babel-minify": "0.5.0",
Expand Down

0 comments on commit 51ab1bc

Please sign in to comment.