-
Notifications
You must be signed in to change notification settings - Fork 25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Format send result to work with ethers #8
Merged
Merged
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
510f893
Improve readability of client log
ryanchristo df390d3
Additional reformatting with eslint
ryanchristo a0c0ab2
Add format result method to work with ethers
ryanchristo ec285d5
Add and improve provider comments
ryanchristo 0e7943c
Use parseInt and this.constructor
ryanchristo ee60f08
Add format condition for eth_filter
ryanchristo c0f382d
Fix connector sending incorrect result
ryanchristo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,9 +3,20 @@ class RemoteMetaMaskProvider { | |
this._connector = connector; | ||
this._callbacks = new Map(); | ||
} | ||
_getAsyncMethod(method) { | ||
// Sync methods don't work with MetaMask | ||
this.syncMethods = [ | ||
|
||
// Generate a request id to track callbacks from async methods | ||
static generateRequestId() { | ||
const s4 = () => | ||
Math.floor((1 + Math.random()) * 0x10000) | ||
.toString(16) | ||
.substring(1); | ||
return `${s4()}${s4()}-${s4()}-${s4()}-${s4()}-${s4()}${s4()}${s4()}`; | ||
} | ||
|
||
// Get the associated async method for the given sync method (MetaMask does | ||
// not work with sync methods) | ||
static getAsyncMethod(method) { | ||
const syncMethods = [ | ||
'version_node', | ||
'version_network', | ||
'version_ethereum', | ||
|
@@ -20,13 +31,17 @@ class RemoteMetaMaskProvider { | |
'eth_accounts', | ||
'eth_blockNumber', | ||
]; | ||
const idx = this.syncMethods.indexOf(method); | ||
|
||
// Translate the defined sync methods | ||
const idx = syncMethods.indexOf(method); | ||
if (idx >= 0) { | ||
return this.syncMethods[idx].replace( | ||
return syncMethods[idx].replace( | ||
/(.+)_([a-z])(.+)/, | ||
(str, p1, p2, p3) => `${p1}_get${p2.toUpperCase()}${p3}`, | ||
); | ||
} | ||
|
||
// Translate other sync methods | ||
const translateMethod = { | ||
net_version: 'version_getNetwork', | ||
eth_getLogs: 'eth_filter', | ||
|
@@ -35,60 +50,78 @@ class RemoteMetaMaskProvider { | |
if (Object.prototype.hasOwnProperty.call(translateMethod, method)) { | ||
return translateMethod[method]; | ||
} | ||
|
||
return method; | ||
} | ||
_guid() { | ||
this.s4 = () => | ||
Math.floor((1 + Math.random()) * 0x10000) | ||
.toString(16) | ||
.substring(1); | ||
return ` | ||
${this.s4()} | ||
${this.s4()} | ||
- | ||
${this.s4()} | ||
- | ||
${this.s4()} | ||
- | ||
${this.s4()} | ||
- | ||
${this.s4()} | ||
${this.s4()} | ||
${this.s4()} | ||
`.replace(/[\n\r]+ */g, ''); | ||
|
||
// When connected to a remote network, the return values for "gasPrice" and | ||
// "value" are strings, so we will need to properly format them for ethers. | ||
// Ideally we would use the big number type from bn.js or bignumber.js but | ||
// ethers does not support any big number type other than it's own. | ||
static formatResult(_result) { | ||
const result = _result; | ||
|
||
// Format "gasPrice" | ||
if (result && typeof result.gasPrice === 'string') { | ||
result.gasPrice = Number(result.gasPrice); | ||
} | ||
|
||
// Format "value" | ||
if (result && typeof result.value === 'string') { | ||
result.value = Number(result.value); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
// Define send method | ||
send(_payload, _callback) { | ||
if (!this._connector.ready()) { | ||
return _callback( | ||
new Error("Can't send. Not connected to a MetaMask socket."), | ||
new Error('Unable to send. Not connected to a MetaMask socket.'), | ||
); | ||
} | ||
// Because requests are handled across a WebSocket they need to be | ||
// associated with their callback with an ID which is returned with the | ||
// response. | ||
const requestId = this._guid(); | ||
const payload = _payload; | ||
|
||
// Because requests are handled across a WebSocket, their callbacks need to | ||
// be associated with an ID which is returned with the response. | ||
const requestId = RemoteMetaMaskProvider.generateRequestId(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We like to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
|
||
// Set the callback using the requestId | ||
this._callbacks.set(requestId, _callback); | ||
payload.method = this._getAsyncMethod(payload.method); | ||
|
||
// Set the payload to allow reassignment | ||
const payload = _payload; | ||
|
||
// Get the async method (Metamask does not support sync methods) | ||
payload.method = RemoteMetaMaskProvider.getAsyncMethod(payload.method); | ||
|
||
return this._connector | ||
.send('execute', requestId, payload, 'executed') | ||
.then(({ requestId: responseRequestId, result }) => { | ||
const requestCallback = this._callbacks.get(responseRequestId); | ||
if (!this._callbacks.has(responseRequestId)) { | ||
return; // A response for this request was already handled | ||
} | ||
this._callbacks.delete(responseRequestId); | ||
|
||
// Exit if a response for this request was already handled | ||
if (!this._callbacks.has(responseRequestId)) return; | ||
|
||
// Throw error if send error | ||
if (result && result.error) { | ||
requestCallback(new Error(result.error)); | ||
} | ||
|
||
// Handle request callback | ||
requestCallback(null, { | ||
id: payload.id, | ||
jsonrpc: '2.0', | ||
result, | ||
result: RemoteMetaMaskProvider.formatResult(result), | ||
}); | ||
|
||
// Delete the callback after the request has been handled | ||
this._callbacks.delete(responseRequestId); | ||
}) | ||
.catch(err => _callback(err)); | ||
} | ||
|
||
// Define async send method | ||
sendAsync(payload, callback) { | ||
this.send(payload, callback); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
ul { | ||
list-style: none; | ||
margin: 0; | ||
padding: 2rem; | ||
} | ||
|
||
li { | ||
line-height: 1.5; | ||
padding: 1rem; | ||
word-break: break-all; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I'd rather see
parseInt(x, 10)
here as it is more explicitThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍