Skip to content

Commit

Permalink
moar fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrouid committed Aug 1, 2019
1 parent 7e210d1 commit b65dfe0
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 83 deletions.
156 changes: 92 additions & 64 deletions packages/web3-provider/src/index.js
Expand Up @@ -18,7 +18,15 @@ export default function WalletConnectProvider (opts) {
throw new Error('Missing or Invalid bridge field')
}

const walletConnector = new WalletConnect({ bridge })
const wc = new WalletConnect({ bridge })

wc.on('disconnect', (error, payload) => {
if (error) {
throw error
}

engine.stop()
})

const engine = new ProviderEngine()

Expand All @@ -36,16 +44,63 @@ export default function WalletConnectProvider (opts) {
}
}

engine._walletConnector = walletConnector
async function handleRequest (payload) {
let result = null
try {
const walletConnector = await engine.getWalletConnector()

switch (payload.method) {
case 'wc_killSession':
await walletConnector.killSession()
await engine.stop()
result = null
break
case 'eth_accounts':
result = walletConnector.accounts
break

case 'eth_coinbase':
result = walletConnector.accounts[0]
break

case 'eth_chainId':
result = walletConnector.chainId
break

case 'net_version':
result = walletConnector.networkId || walletConnector.chainId
break

case 'eth_uninstallFilter':
engine.sendAsync(payload, _ => _)
result = true
break

default:
result = await walletConnector.sendCustomRequest(payload)
break
}
} catch (error) {
throw error
}

return {
id: payload.id,
jsonrpc: payload.jsonrpc,
result: result
}
}

engine.wc = wc

engine.send = (payload, callback) => {
engine.send = async (payload, callback) => {
// Web3 1.0 beta.38 (and above) calls `send` with method and parameters
if (typeof payload === 'string') {
return new Promise((resolve, reject) => {
engine.sendAsync(
{
jsonrpc: '2.0',
id: 42,
jsonrpc: '2.0',
method: payload,
params: callback || []
},
Expand All @@ -66,48 +121,18 @@ export default function WalletConnectProvider (opts) {
return
}

let result = null
switch (payload.method) {
case 'eth_accounts':
result = engine._walletConnector.accounts
break

case 'eth_coinbase':
result = engine._walletConnector.accounts[0]
break

case 'eth_chainId':
result = engine._walletConnector.chainId
break

case 'net_version':
result =
engine._walletConnector.networkId || engine._walletConnector.chainId
break

case 'eth_uninstallFilter':
engine.sendAsync(payload, _ => _)
result = true
break

default:
throw new Error(`Method ${payload.method} is not support`)
}
const res = await handleRequest(payload, callback)

return {
id: payload.id,
jsonrpc: payload.jsonrpc,
result: result
}
return res
}

engine.addProvider(
new FixtureSubprovider({
web3_clientVersion: `WalletConnect/v${pkg.version}/javascript`,
net_listening: true,
eth_hashrate: '0x00',
eth_mining: false,
eth_syncing: true
eth_syncing: true,
net_listening: true,
web3_clientVersion: `WalletConnect/v${pkg.version}/javascript`
})
)

Expand All @@ -119,53 +144,57 @@ export default function WalletConnectProvider (opts) {
engine.addProvider(
new HookedWalletSubprovider({
getAccounts: async cb => {
const walletConnector = await engine.getWalletConnector()
const accounts = walletConnector.accounts
if (accounts && accounts.length) {
cb(null, accounts)
} else {
cb(new Error('Failed to get accounts'))
try {
const walletConnector = await engine.getWalletConnector()
const accounts = walletConnector.accounts
if (accounts && accounts.length) {
cb(null, accounts)
} else {
cb(new Error('Failed to get accounts'))
}
} catch (error) {
cb(error)
}
},
processTransaction: async (txParams, cb) => {
const walletConnector = await engine.getWalletConnector()
processMessage: async (msgParams, cb) => {
try {
const result = await walletConnector.sendTransaction(txParams)
const walletConnector = await engine.getWalletConnector()
const result = await walletConnector.signMessage(msgParams)
cb(null, result)
} catch (error) {
cb(error)
}
},
processSignTransaction: async (txParams, cb) => {
const walletConnector = await engine.getWalletConnector()
processPersonalMessage: async (msgParams, cb) => {
try {
const result = await walletConnector.signTransaction(txParams)
const walletConnector = await engine.getWalletConnector()
const result = await walletConnector.signPersonalMessage(msgParams)
cb(null, result)
} catch (error) {
cb(error)
}
},
processMessage: async (msgParams, cb) => {
const walletConnector = await engine.getWalletConnector()
processSignTransaction: async (txParams, cb) => {
try {
const result = await walletConnector.signMessage(msgParams)
const walletConnector = await engine.getWalletConnector()
const result = await walletConnector.signTransaction(txParams)
cb(null, result)
} catch (error) {
cb(error)
}
},
processPersonalMessage: async (msgParams, cb) => {
const walletConnector = await engine.getWalletConnector()
processTransaction: async (txParams, cb) => {
try {
const result = await walletConnector.signPersonalMessage(msgParams)
const walletConnector = await engine.getWalletConnector()
const result = await walletConnector.sendTransaction(txParams)
cb(null, result)
} catch (error) {
cb(error)
}
},
processTypedMessage: async (msgParams, cb) => {
const walletConnector = await engine.getWalletConnector()
try {
const walletConnector = await engine.getWalletConnector()
const result = await walletConnector.signTypedData(msgParams)
cb(null, result)
} catch (error) {
Expand All @@ -176,16 +205,15 @@ export default function WalletConnectProvider (opts) {
)

engine.addProvider({
setEngine: _ => _,
handleRequest: async (payload, next, end) => {
const walletConnector = await engine.getWalletConnector()
try {
const result = await walletConnector.sendCustomRequest(payload)
const { result } = await handleRequest(payload)
end(null, result)
} catch (error) {
end(error)
}
}
},
setEngine: _ => _
})

engine.enable = () =>
Expand All @@ -201,10 +229,10 @@ export default function WalletConnectProvider (opts) {

engine.getWalletConnector = () => {
return new Promise((resolve, reject) => {
const walletConnector = engine._walletConnector
const walletConnector = engine.wc

if (engine.isConnecting) {
onConnect(_walletConnector => resolve(_walletConnector))
onConnect(x => resolve(x))
} else if (!walletConnector.connected) {
engine.isConnecting = true
walletConnector
Expand Down
42 changes: 23 additions & 19 deletions packages/web3-subprovider/src/index.js
Expand Up @@ -6,53 +6,57 @@ export default class WalletConnectSubprovider extends HookedWalletSubprovider {
constructor (opts) {
super({
getAccounts: async cb => {
const walletConnector = await this.getWalletConnector()
const accounts = walletConnector.accounts
if (accounts && accounts.length) {
cb(null, accounts)
} else {
cb(new Error('Failed to get accounts'))
try {
const walletConnector = await this.getWalletConnector()
const accounts = walletConnector.accounts
if (accounts && accounts.length) {
cb(null, accounts)
} else {
cb(new Error('Failed to get accounts'))
}
} catch (error) {
cb(error)
}
},
processTransaction: async (txParams, cb) => {
const walletConnector = await this.getWalletConnector()
processMessage: async (msgParams, cb) => {
try {
const result = await walletConnector.sendTransaction(txParams)
const walletConnector = await this.getWalletConnector()
const result = await walletConnector.signMessage(msgParams)
cb(null, result)
} catch (error) {
cb(error)
}
},
processSignTransaction: async (txParams, cb) => {
const walletConnector = await this.getWalletConnector()
processPersonalMessage: async (msgParams, cb) => {
try {
const result = await walletConnector.signTransaction(txParams)
const walletConnector = await this.getWalletConnector()
const result = await walletConnector.signPersonalMessage(msgParams)
cb(null, result)
} catch (error) {
cb(error)
}
},
processMessage: async (msgParams, cb) => {
const walletConnector = await this.getWalletConnector()
processSignTransaction: async (txParams, cb) => {
try {
const result = await walletConnector.signMessage(msgParams)
const walletConnector = await this.getWalletConnector()
const result = await walletConnector.signTransaction(txParams)
cb(null, result)
} catch (error) {
cb(error)
}
},
processPersonalMessage: async (msgParams, cb) => {
const walletConnector = await this.getWalletConnector()
processTransaction: async (txParams, cb) => {
try {
const result = await walletConnector.signPersonalMessage(msgParams)
const walletConnector = await this.getWalletConnector()
const result = await walletConnector.sendTransaction(txParams)
cb(null, result)
} catch (error) {
cb(error)
}
},
processTypedMessage: async (msgParams, cb) => {
const walletConnector = await this.getWalletConnector()
try {
const walletConnector = await this.getWalletConnector()
const result = await walletConnector.signTypedData(msgParams)
cb(null, result)
} catch (error) {
Expand Down

0 comments on commit b65dfe0

Please sign in to comment.