-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #60 from gnosis/feature/58-promisifed-api
- Loading branch information
Showing
17 changed files
with
1,827 additions
and
747 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
module.exports = { | ||
preset: 'ts-jest', | ||
moduleNameMapper: { | ||
'src/(.*)': '<rootDir>/src/$1', | ||
}, | ||
roots: ['./src/'], | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import { | ||
InterfaceMessageIds, | ||
InterfaceMessageEvent, | ||
SentSDKMessage, | ||
SDKMessageIds, | ||
SDKMessageToPayload, | ||
RequestId, | ||
InterfaceMessageToPayload, | ||
Communicator, | ||
} from '../types'; | ||
import { INTERFACE_MESSAGES } from './messageIds'; | ||
|
||
class InterfaceCommunicator implements Communicator { | ||
private allowedOrigins: RegExp[] = []; | ||
|
||
constructor(allowedOrigins: RegExp[]) { | ||
this.allowedOrigins = allowedOrigins; | ||
|
||
window.addEventListener('message', this.onParentMessage); | ||
} | ||
|
||
private isValidMessage({ origin, data }: InterfaceMessageEvent): boolean { | ||
const emptyOrMalformed = !data || !data.messageId; | ||
const unknownOrigin = this.allowedOrigins?.find((regExp) => regExp.test(origin)) === undefined; | ||
const sameOrigin = origin === window.origin; | ||
|
||
return !emptyOrMalformed && !unknownOrigin && !sameOrigin; | ||
} | ||
|
||
private logIncomingMessage(origin: string, payload: InterfaceMessageToPayload[InterfaceMessageIds]): void { | ||
console.info(`SafeConnector: A message was received from origin ${origin}. `, payload); | ||
} | ||
|
||
private onParentMessage(msg: InterfaceMessageEvent): void { | ||
this.logIncomingMessage(msg.origin, msg.data); | ||
|
||
if (this.isValidMessage(msg)) { | ||
this.handleIncomingMessage(msg.data.messageId, msg.data.data, msg.data.requestId); | ||
} | ||
} | ||
|
||
private handleIncomingMessage( | ||
messageId: InterfaceMessageIds, | ||
payload: InterfaceMessageToPayload[InterfaceMessageIds], | ||
requestId: RequestId, | ||
): void { | ||
console.log(payload, requestId); | ||
switch (messageId) { | ||
case INTERFACE_MESSAGES.ENV_INFO: | ||
// const typedPayload = payload as InterfaceMessageToPayload[typeof INTERFACE_MESSAGES.ENV_INFO]; | ||
|
||
break; | ||
|
||
case INTERFACE_MESSAGES.ON_SAFE_INFO: { | ||
/* tslint:disable-next-line:no-shadowed-variable */ | ||
// const typedPayload = payload as InterfaceMessageToPayload[typeof INTERFACE_MESSAGES.ON_SAFE_INFO]; | ||
|
||
break; | ||
} | ||
|
||
case INTERFACE_MESSAGES.TRANSACTION_CONFIRMED: { | ||
/* tslint:disable-next-line:no-shadowed-variable */ | ||
// const typedPayload = payload as InterfaceMessageToPayload[typeof INTERFACE_MESSAGES.TRANSACTION_CONFIRMED]; | ||
|
||
break; | ||
} | ||
|
||
case INTERFACE_MESSAGES.TRANSACTION_REJECTED: { | ||
break; | ||
} | ||
|
||
default: { | ||
console.warn( | ||
`SafeConnector: A message was received from origin ${origin} with an unknown message id: ${messageId}`, | ||
); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
public send<T extends SDKMessageIds, D = SDKMessageToPayload[T]>( | ||
messageId: T, | ||
data: D, | ||
requestId?: RequestId, | ||
): SentSDKMessage<T, D> { | ||
if (!requestId) { | ||
if (typeof window !== 'undefined') { | ||
requestId = Math.trunc(window?.performance.now()); | ||
} else { | ||
requestId = Math.trunc(Date.now()); | ||
} | ||
} | ||
const message = { | ||
messageId, | ||
requestId, | ||
data, | ||
}; | ||
|
||
if (typeof window !== 'undefined') { | ||
window.parent.postMessage(message, '*'); | ||
} | ||
|
||
return message; | ||
} | ||
} | ||
|
||
export default InterfaceCommunicator; | ||
export * from './messageIds'; |
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
export const RPC_CALLS = { | ||
eth_call: 'eth_call', | ||
eth_getLogs: 'eth_getLogs', | ||
eth_getBalance: 'eth_getBalance', | ||
eth_getCode: 'eth_getCode', | ||
eth_getBlockByHash: 'eth_getBlockByHash', | ||
eth_getBlockByNumber: 'eth_getBlockByNumber', | ||
eth_getStorageAt: 'eth_getStorageAt', | ||
eth_getTransactionByHash: 'eth_getTransactionByHash', | ||
eth_getTransactionReceipt: 'eth_getTransactionReceipt', | ||
} as const; |
Oops, something went wrong.