Skip to content

Commit

Permalink
Merge pull request #131 from liquality/separate-networks
Browse files Browse the repository at this point in the history
Separate networks
  • Loading branch information
monokh authored Jan 7, 2019
2 parents 5f05b4b + 5c1d4c5 commit bea69b7
Show file tree
Hide file tree
Showing 23 changed files with 82 additions and 53 deletions.
3 changes: 2 additions & 1 deletion examples/browser/bitcoin/ledger-send-transaction.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ <h3>Pay to custom script</h3>

<script>
/* global $, ChainAbstractionLayer */
const { Client, providers, networks } = ChainAbstractionLayer
const { Client, providers } = ChainAbstractionLayer
const networks = providers.bitcoin.networks
const bitcoin = new Client()
bitcoin.addProvider(new providers.bitcoin.BitcoinRPCProvider('https://localhost:18332', 'bitcoin', 'local321'))
bitcoin.addProvider(new providers.bitcoin.BitcoinLedgerProvider({ network: networks.bitcoin_testnet }))
Expand Down
3 changes: 2 additions & 1 deletion examples/browser/bitcoin/ledger.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ <h1>CAL + Ledger: Sign a message (Bitcoin)</h1>
<p><code>For errors and logs, check console</code></p>
<script>
/* global $, ChainAbstractionLayer */
const { Client, providers, networks } = ChainAbstractionLayer
const { Client, providers } = ChainAbstractionLayer
const networks = providers.bitcoin.networks
const bitcoin = new Client()
bitcoin.addProvider(new providers.bitcoin.BitcoinLedgerProvider({ network: networks.bitcoin_testnet }))
bitcoin.getAddresses().then(addresses => {
Expand Down
4 changes: 2 additions & 2 deletions examples/node/bitcoin/test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
process.env.NODE_TLS_REJECT_UNAUTHORIZED = 0

const { Client, providers, networks } = require('../../../')
const { BitcoinLedgerProvider, BitcoreRPCProvider } = providers.bitcoin
const { Client, providers } = require('../../../')
const { BitcoinLedgerProvider, BitcoreRPCProvider, networks } = providers.bitcoin

const bitcoin = new Client()
bitcoin.addProvider(new BitcoreRPCProvider('https://btc-testnet.leep.it', 'bitcoin', 'local321'))
Expand Down
5 changes: 3 additions & 2 deletions examples/swap/cli/bitcoin.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = 0

const { Client, providers, networks, crypto } = require('../../../dist/index.cjs.js')
const { Client, providers, crypto } = require('../../../dist/index.cjs.js')
const networks = providers.bitcoin.networks
const chains = {}

chains.bitcoin = new Client()
chains.bitcoin.addProvider(new providers.bitcoin.BitcoreRPCProvider('https://btc-testnet.leep.it/', 'bitcoin', 'local321'))
chains.bitcoin.addProvider(new providers.bitcoin.BitcoreRPCProvider('http://localhost:18332', 'bitcoin', 'local321'))
chains.bitcoin.addProvider(new providers.bitcoin.BitcoinLedgerProvider({ network: networks.bitcoin_testnet, segwit: false }))
chains.bitcoin.addProvider(new providers.bitcoin.BitcoinSwapProvider({ network: networks.bitcoin_testnet }))
// chains.bitcoin.addProvider(new providers.bitcoin.BitcoinJsLibSwapProvider({ network: networks.bitcoin }))
Expand Down
3 changes: 2 additions & 1 deletion examples/swap/cli/continueclaim.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var ChainAbstractionLayer = require('../../../dist/index.cjs.js')
const { Client, providers, networks } = ChainAbstractionLayer
const { Client, providers } = ChainAbstractionLayer
const networks = providers.bitcoin.networks

var chains = {}
process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = 0
Expand Down
4 changes: 2 additions & 2 deletions examples/swap/cli/getaddressesnew.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = '0'

const { Client, providers, networks } = require('../../../')
const { BitcoinLedgerProvider, BitcoreRPCProvider } = providers.bitcoin
const { Client, providers } = require('../../../')
const { BitcoinLedgerProvider, BitcoreRPCProvider, networks } = providers.bitcoin

const bitcoin = new Client()
bitcoin.addProvider(new BitcoreRPCProvider('http://localhost:18332', 'bitcoin', 'local321'))
Expand Down
3 changes: 2 additions & 1 deletion examples/swap/cli/getsecret.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var ChainAbstractionLayer = require('../../../dist/index.cjs.js')
const { Client, providers, networks, crypto } = ChainAbstractionLayer
const { Client, providers, crypto } = ChainAbstractionLayer
const networks = providers.bitcoin.networks

var chains = {}
process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = 0
Expand Down
4 changes: 2 additions & 2 deletions examples/swap/cli/getunused.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = '0'

const { Client, providers, networks } = require('../../../')
const { BitcoinLedgerProvider, BitcoreRPCProvider } = providers.bitcoin
const { Client, providers } = require('../../../')
const { BitcoinLedgerProvider, BitcoreRPCProvider, networks } = providers.bitcoin

const bitcoin = new Client()
bitcoin.addProvider(new BitcoreRPCProvider('http://localhost:18332', 'bitcoin', 'local321'))
Expand Down
3 changes: 2 additions & 1 deletion examples/swap/cli/refund.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var ChainAbstractionLayer = require('../../../dist/index.cjs.js')
const { Client, providers, networks } = ChainAbstractionLayer
const { Client, providers } = ChainAbstractionLayer
const networks = providers.bitcoin.networks

var chains = {}
process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = 0
Expand Down
7 changes: 4 additions & 3 deletions examples/swap/swap-e2e.html
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,12 @@ <h3>Refund swap:</h3>

<script>
/* global $, ChainAbstractionLayer, web3 */
const { Client, providers, networks, crypto } = ChainAbstractionLayer
const { Client, providers, crypto } = ChainAbstractionLayer
const bitcoinNetworks = providers.bitcoin.networks
const bitcoin = new Client()
bitcoin.addProvider(new providers.bitcoin.BitcoreRPCProvider('https://localhost:18332', 'bitcoin', 'local321'))
bitcoin.addProvider(new providers.bitcoin.BitcoinLedgerProvider({ network: networks.bitcoin_testnet, segwit: false }))
bitcoin.addProvider(new providers.bitcoin.BitcoinSwapProvider({ network: networks.bitcoin_testnet }))
bitcoin.addProvider(new providers.bitcoin.BitcoinLedgerProvider({ network: bitcoinNetworks.bitcoin_testnet, segwit: false }))
bitcoin.addProvider(new providers.bitcoin.BitcoinSwapProvider({ network: bitcoinNetworks.bitcoin_testnet }))

const ethereum = new Client()
ethereum.addProvider(new providers.ethereum.EthereumRPCProvider('http://localhost:8545'))
Expand Down
2 changes: 0 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import Client from './Client'
import Provider from './Provider'
import networks from './networks'

import * as providers from './providers'
import * as crypto from './crypto'
Expand All @@ -14,7 +13,6 @@ import LedgerProvider from './providers/LedgerProvider'
export {
Client,
Provider,
networks,
providers,
crypto,
errors,
Expand Down
2 changes: 1 addition & 1 deletion src/providers/bitcoin/BitcoinBlockchainAPIProvider.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { forIn } from 'lodash'

import ApiProvider from '../ApiProvider'
import networks from '../../networks'
import networks from './networks'

export default class BitcoinBlockchainAPIProvider extends ApiProvider {
constructor (chain = { network: networks.bitcoin }) {
Expand Down
2 changes: 1 addition & 1 deletion src/providers/bitcoin/BitcoinJsLibSwapProvider.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Provider from '../../Provider'
import { addressToPubKeyHash, compressPubKey, pubKeyToAddress, reverseBuffer, scriptNumEncode } from './BitcoinUtil'
import { sha256, padHexStart } from '../../crypto'
import networks from '../../networks'
import networks from './networks'
import bitcoin from 'bitcoinjs-lib'

const sleep = ms => new Promise(resolve => setTimeout(resolve, ms))
Expand Down
2 changes: 1 addition & 1 deletion src/providers/bitcoin/BitcoinLedgerProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { BigNumber } from 'bignumber.js'
import { base58, padHexStart, sha256, ripemd160 } from '../../crypto'
import { pubKeyToAddress, addressToPubKeyHash, compressPubKey, createXPUB, encodeBase58Check, parseHexString } from './BitcoinUtil'
import Address from '../../Address'
import networks from '../../networks'
import networks from './networks'
import bip32 from 'bip32'

export default class BitcoinLedgerProvider extends LedgerProvider {
Expand Down
2 changes: 1 addition & 1 deletion src/providers/bitcoin/BitcoinSwapProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Provider from '../../Provider'
import { addressToPubKeyHash, compressPubKey, pubKeyToAddress, reverseBuffer, scriptNumEncode } from './BitcoinUtil'
import { BigNumber } from 'bignumber.js'
import { sha256, padHexStart } from '../../crypto'
import networks from '../../networks'
import networks from './networks'

const sleep = ms => new Promise(resolve => setTimeout(resolve, ms))

Expand Down
2 changes: 1 addition & 1 deletion src/providers/bitcoin/BitcoinUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import bitcoin from 'bitcoinjs-lib'
import bs58 from 'bs58'

import padStart from 'lodash/padStart'
import networks from '../../networks'
import networks from './networks'

/**
* Get compressed pubKey from pubKey.
Expand Down
2 changes: 1 addition & 1 deletion src/providers/bitcoin/BitcoreRPCProvider.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import BitcoinRPCProvider from './BitcoinRPCProvider'
import { base58 } from '../../crypto'
import { addressToPubKeyHash } from '../bitcoin/BitcoinUtil'
import networks from '../../networks'
import networks from './networks'

/**
* BitcoreRPCProvider overrides the BitcoinRPCProvider to use the address index
Expand Down
4 changes: 3 additions & 1 deletion src/providers/bitcoin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import BitcoinBlockchainAPIProvider from './BitcoinBlockchainAPIProvider'
import BitcoinSwapProvider from './BitcoinSwapProvider'
import BitcoinJsLibSwapProvider from './BitcoinJsLibSwapProvider.js'
import * as BitcoinUtil from './BitcoinUtil'
import networks from './networks'

export {
BitcoinRPCProvider,
Expand All @@ -13,5 +14,6 @@ export {
BitcoinBlockchainAPIProvider,
BitcoinSwapProvider,
BitcoinJsLibSwapProvider,
BitcoinUtil
BitcoinUtil,
networks
}
24 changes: 4 additions & 20 deletions src/networks.js → src/providers/bitcoin/networks.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ export default {
bip32: {
public: 0x0488b21e,
private: 0x0488ade4
}
},
isTestnet: false
},
bitcoin_testnet: {
name: 'bitcoin_testnet',
Expand All @@ -21,24 +22,7 @@ export default {
bip32: {
public: 0x043587cf,
private: 0x04358394
}
},
litecoin: {
name: 'litecoin',
pubKeyHash: '30',
scriptHash: '32',
coinType: '2',
bip32: {
private: 0x019d9cfe,
public: 0x019da462
}
},
ethereum: {
name: 'ethereum',
coinType: '60'
},
ethereum_classic: {
name: 'ethereum_classic',
coinType: '61'
},
isTestnet: true
}
}
4 changes: 2 additions & 2 deletions src/providers/ethereum/EthereumLedgerProvider.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import Ethereum from '@ledgerhq/hw-app-eth'

import LedgerProvider from '../LedgerProvider'
import networks from '../../networks'
import networks from './networks'
import Address from '../../Address'

export default class EthereumLedgerProvider extends LedgerProvider {
constructor (chain = { network: networks.ethereum }) {
constructor (chain = { network: networks.mainnet }) {
super(Ethereum, `44'/${chain.network.coinType}'/0'/`)
}

Expand Down
9 changes: 4 additions & 5 deletions src/providers/ethereum/EthereumMetaMaskProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import { formatEthResponse, ensureHexEthFormat, ensureHexStandardFormat } from '
import { BigNumber } from 'bignumber.js'

export default class EthereumMetaMaskProvider extends Provider {
constructor (metamaskProvider, networkId) {
constructor (metamaskProvider, network) {
super()
if (!isFunction(metamaskProvider.sendAsync)) {
throw new Error('Invalid MetaMask Provider')
}

this._metamaskProvider = metamaskProvider
this._networkId = parseInt(networkId)
this._network = network
}

_toMM (method, ...params) {
Expand Down Expand Up @@ -75,9 +75,8 @@ export default class EthereumMetaMaskProvider extends Provider {

async sendTransaction (to, value, data, from = null) {
const networkId = await this.getWalletNetworkId()

if (this._networkId) {
if (networkId !== this._networkId) {
if (this._network) {
if (networkId !== this._network.networkId) {
throw new Error('Invalid MetaMask Network')
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/providers/ethereum/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import EthereumRPCProvider from './EthereumRPCProvider'
import EthereumLedgerProvider from './EthereumLedgerProvider'
import EthereumMetaMaskProvider from './EthereumMetaMaskProvider'
import EthereumSwapProvider from './EthereumSwapProvider'
import networks from './networks'

export {
EthereumRPCProvider,
EthereumLedgerProvider,
EthereumMetaMaskProvider,
EthereumSwapProvider
EthereumSwapProvider,
networks
}
37 changes: 37 additions & 0 deletions src/providers/ethereum/networks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
export default {
mainnet: {
name: 'mainnet',
coinType: '60',
networkId: 1,
chainId: 1,
isTestnet: false
},
classic_mainnet: {
name: 'classic_mainnet',
coinType: '61',
neworkId: 1,
chainId: 61,
isTestnet: false
},
ropsten: {
name: 'ropsten',
coinType: '60',
networkId: 3,
chainId: 3,
isTestnet: true
},
rinkeby: {
name: 'rinkeby',
coinType: '60',
networkId: 4,
chainId: 4,
isTestnet: true
},
kovan: {
name: 'kovan',
coinType: '60',
networkId: 42,
chainId: 42,
isTestnet: true
}
}

0 comments on commit bea69b7

Please sign in to comment.