Skip to content

Commit

Permalink
Update documentation & project config
Browse files Browse the repository at this point in the history
  • Loading branch information
VandeurenGlenn committed Aug 15, 2021
1 parent eb2452e commit 5052848
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 8 deletions.
11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "@arteon/network-connect",
"name": "@arteon/wallet-connect",
"version": "1.0.0",
"description": "",
"main": "dist/network-connect.js",
"browser": "dist/network-connect.browser.js",
"module": "src/network-connect.js",
"main": "dist/wallet-connect.js",
"browser": "dist/wallet-connect.browser.js",
"module": "src/wallet-connect.js",
"scripts": {
"compile": "rollup -c",
"c": "npm run compile",
Expand All @@ -14,12 +14,13 @@
"lint": "./node_modules/.bin/eslint src/**/**.js --fix",
"coverage": "nyc --reporter=lcov npm run test",
"coveralls": "cat ./coverage/lcov.info | coveralls",
"b": "browserify dist/network-connect.js -o dist/network-connect.browser.js --standalone networkConnect",
"b": "browserify dist/wallet-connect.js -o dist/wallet-connect.browser.js --standalone walletConnect",
"build": "npm run lint && npm run c && npm run b && npm run coverage && npm run coveralls npm run doc",
"test": "node test"
},
"keywords": [],
"author": "Vandeurenglenn<vandeurenglenn@gmail.com>",
"repository": "https://github.com/Artontoken/wallet-connect",
"license": "MIT",
"devDependencies": {
"browserify": "^17.0.0",
Expand Down
5 changes: 4 additions & 1 deletion rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
export default [{
input: 'src/network-connect.js',
input: 'src/wallet-connect.js',
output: [{
dir: 'dist',
format: 'cjs'
}, {
file: 'dist/wallet-connect.browser.js',
format: 'cjs'
}]
}]
4 changes: 4 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ export const DEFAULT_NETWORK = {
chainId: 1,
}

/**
* @param { String } url - rpcUrl
* @return {Object} network - { name, chainId }
*/
export const networkFromRpc = (url) => {
const network = DEFAULT_NETWORK
for (const name of Object.keys(networksByName)) {
Expand Down
25 changes: 24 additions & 1 deletion src/network-connect.js → src/wallet-connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ import { providers, Wallet, utils } from 'ethers'
const { HDNode } = utils
const { Web3Provider, JsonRpcProvider } = providers

/**
* @param { String | undefined } rpcUrl - when undefined metaMask is returned
* @param { Object } network - { name, chainId}
*
* @return { Web3Provider }
*/
const providerFor = (rpcUrl, network) => {
if (rpcUrl.includes('infura') || rpcUrl.includes('etherscan')) {
return new JsonRpcProvider(rpcUrl, network)
Expand All @@ -11,6 +17,11 @@ const providerFor = (rpcUrl, network) => {
return new Web3Provider(globalThis.ethereum)
}

/**
* @param { Object } network - { name, chainId}
*
* @return { Web3Provider, accounts }
*/
export const metaMask = async (network) => {
await globalThis.ethereum.request({
method: 'wallet_switchEthereumChain',
Expand All @@ -27,6 +38,12 @@ export const metaMask = async (network) => {
}
}

/**
* @param { Object } network - { name, chainId}
* @param { Object } params - { rpcUrl, privateKey}
*
* @return { JsonRpcProvider, Wallet, accounts }
*/
export const privateKey = (network, params) => {
const provider = providerFor(params.rpcUrl, network)
const wallet = new Wallet(params.privateKey, provider)
Expand All @@ -37,6 +54,12 @@ export const privateKey = (network, params) => {
}
}

/**
* @param { Object } network - { name, chainId}
* @param { Object } params - { rpcUrl, mnemonic}
*
* @return { JsonRpcProvider, HDNode, accounts }
*/
export const hdWallet = (network, params) => {
const provider = providerFor(params.rpcUrl, network)
const wallet = HDNode.fromMnemonic(params.mnemonic, provider)
Expand All @@ -49,7 +72,7 @@ export const hdWallet = (network, params) => {

/**
* @param {Object} params {rpcUrl, id, password, mnemonic, privateKey}
* @param {Object | Number | String} network {name, chainId} | name | chainId
* @param {Object | Number | String | undefined} network {name, chainId} | name | chainId
*/
export const connect = async (params = {}, network) => {
if (!network) {
Expand Down
2 changes: 1 addition & 1 deletion test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const test = require('tape')
const {connect} = require('./dist/network-connect.js')
const {connect} = require('./dist/wallet-connect.js')

const rpcUrl = 'https://goerli.infura.io/v3/d7acc44d359646f59bf02a00930a15e6'
const mumbaiRpcUrl = 'https://polygon-mumbai.infura.io/v3/d7acc44d359646f59bf02a00930a15e6'
Expand Down

0 comments on commit 5052848

Please sign in to comment.