Skip to content

Instantly share code, notes, and snippets.

@hewigovens
Created July 2, 2018 13:20
Show Gist options
  • Save hewigovens/a0f2ea42c84d592e6af625a32a086099 to your computer and use it in GitHub Desktop.
Save hewigovens/a0f2ea42c84d592e6af625a32a086099 to your computer and use it in GitHub Desktop.
cipher provider
__d(function(e, r, n, t) {
Object.defineProperty(t, "__esModule", {
value: !0
}), t.provider = function(e) {
function r(e) {
i.__CIPHER_BRIDGE__ ? i.__CIPHER_BRIDGE__.postMessage(JSON.stringify(e)) : i.webkit.messageHandlers.reactNative.postMessage(JSON.stringify(e))
}
function n() {
var e = crypto.getRandomValues(new Uint8Array(4)),
r = btoa((function(e) {
return Array.from ? Array.from(e) : [].slice.call(e)
})(e).map(function(e) {
return String.fromCharCode(e)
}).join('')).slice(0, 6);
return Date.now() + r
}
function t(e) {
var r = {
jsonrpc: '2.0',
id: e.id
};
switch (e.method) {
case 'eth_accounts':
r.result = c ? [c] : [];
break;
case 'eth_coinbase':
r.result = c;
break;
case 'eth_uninstallFilter':
o(e).catch(function() {}), r.result = !0;
break;
case 'net_version':
r.result = u.toString(10) || null;
break;
default:
throw new Error("Cipher does not support calling " + e.method + " synchronously without a callback. Please provide a callback parameter to call " + e.method + " asynchronously.")
}
return r
}
function o(e) {
return new Promise(function(t, o) {
var i = n();
a.set(i, function(r, n) {
r ? o(r) : n && (n.id = e.id, t(n))
}), r({
action: 'rpc',
id: i,
request: e
})
})
}
var i = window;
if (!i.__CIPHER__ || !i.__CIPHER__.provider) {
var a = new Map,
s = e.version,
c = e.address,
u = e.chainId;
c = c ? c.toLowerCase() : null;
var l = {
CipherProvider: function() {}
};
l.CipherProvider.prototype.send = function(e) {
return Array.isArray(e) ? e.map(t) : t(e)
}, l.CipherProvider.prototype.sendAsync = function(e, r) {
Array.isArray(e) ? (function(e) {
return Promise.all(e.map(o))
})(e).then(function(e) {
return r(null, e)
}).catch(function(e) {
return r(e, null)
}) : o(e).then(function(e) {
return r(null, e)
}).catch(function(e) {
return r(e, null)
})
}, l.CipherProvider.prototype.isConnected = function() {
return !0
}, l.CipherProvider.prototype.isCipher = !0;
var d = null,
f = null;
l.CipherProvider.prototype.scanQRCode = function(e) {
return d || f ? Promise.reject(new Error('Another QR code scan is currently active')) : !e || e instanceof RegExp ? new Promise(function(n, t) {
d = n, f = t, r({
action: 'scanQRCode',
match: e ? e.toString() : null
})
}) : Promise.reject(new Error('First argument must either be absent or a regular expression'))
};
var p = new l.CipherProvider;
Object.defineProperty(i, '__CIPHER__', {
value: {
__internal__: {
respond: function(e, r) {
var n = a.get(e);
n && (a.delete(e), n(null, r))
},
fail: function(e, r) {
var n = a.get(e);
n && (a.delete(e), n(new Error(r), null))
},
setAddress: function(e) {
c = e ? e.toLowerCase() : e, i.web3.eth.defaultAccount = c
},
setChainId: function(e) {
u = e
},
postMessage: r,
receiveMessage: function(e, r) {
if ('scanQRCode' === e) {
var n = d,
t = f;
f = null, d = null, n && 'string' == typeof r && n(r), t && r && 'string' == typeof r.errorMessage && t(new Error(r.errorMessage))
}
}
},
provider: p,
version: s
}
}), i.web3 = new i.Web3(p), i.web3.eth.defaultAccount = c, i.chrome = {
webstore: {}
}
}
}
}, 1255);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment