Skip to content

Instantly share code, notes, and snippets.

@jdkanani
Created April 25, 2018 17:37
Show Gist options
  • Save jdkanani/8d4b18ae68d3774a92d1ee637a26efde to your computer and use it in GitHub Desktop.
Save jdkanani/8d4b18ae68d3774a92d1ee637a26efde to your computer and use it in GitHub Desktop.
Walletconnect providers
import Web3 from 'web3'
function getCallback(payload, cb) {
return function(err, result) {
const obj = {}
const keys = ['id', 'jsonrpc']
keys.forEach(key => {
obj[key] = payload[key]
})
obj.result = result
cb(err, obj)
}
}
export default class WalletConnectProvider extends Web3.providers.HttpProvider {
isWalletConnect = true
constructor({host, timeout, webconnector}) {
super(host, timeout)
// set webconnector
this._webconnector = webconnector
}
send(...args) {
return super.send(...args)
}
sendAsync(payload, cb) {
if (payload.method === 'eth_sendTransaction' || payload.method === 'eth_signTypedData') {
return this._webconnector
.createTransaction(payload)
.then(({transactionId}) => {
this._webconnector.listenTransactionStatus(transactionId, getCallback(payload, cb))
})
.catch(getCallback(payload, cb))
}
return super.send(payload, cb)
}
}
@jdkanani
Copy link
Author

jdkanani commented Apr 30, 2018

Checkout improved web3-provider here - https://github.com/walletconnect/walletconnect-providers

@jdkanani
Copy link
Author

jdkanani commented Apr 30, 2018

// insert web3
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'https://cdn.jsdelivr.net/gh/ethereum/web3.js/dist/web3.min.js';
document.head.appendChild(script);

// insert web3-provider
script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'https://cdn.rawgit.com/walletconnect/walletconnect-providers/master/lib/web3-provider.min.js';
document.head.appendChild(script);

// Create web3
var WCProvider = window["web3-provider"];
var provider = new WCProvider({host: 'https://kovan.infura.io/'});
window.web3 = new Web3(provider);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment