Skip to content

Commit

Permalink
Merge pull request #60 from gnosis/feature/58-promisifed-api
Browse files Browse the repository at this point in the history
  • Loading branch information
mmv08 authored Oct 29, 2020
2 parents 44755ad + 1125993 commit 6f3d6ee
Show file tree
Hide file tree
Showing 17 changed files with 1,827 additions and 747 deletions.
4 changes: 4 additions & 0 deletions jest.config.js
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/'],
};
28 changes: 17 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,37 @@
"apps"
],
"scripts": {
"test": "jest",
"test": "jest --verbose",
"build": "yarn rimraf dist && yarn tsc",
"prepare": "yarn build",
"format": "prettier --write \"src/**/*.ts\"",
"lint": "tslint -p tsconfig.json"
},
"author": "Gnosis (https://gnosis.pm)",
"license": "MIT",
"dependencies": {
"web3-core": "^1.3.0"
},
"devDependencies": {
"@types/jest": "^26.0.14",
"@types/node": "^14.11.5",
"@typescript-eslint/eslint-plugin": "^4.4.0",
"@typescript-eslint/parser": "^4.4.0",
"eslint": "^7.10.0",
"eslint-config-prettier": "^6.12.0",
"@types/jest": "^26.0.15",
"@types/node": "^14.14.6",
"@typescript-eslint/eslint-plugin": "^4.6.0",
"@typescript-eslint/parser": "^4.6.0",
"eslint": "^7.12.1",
"eslint-config-prettier": "^6.15.0",
"eslint-plugin-prettier": "^3.1.4",
"husky": "^4.3.0",
"jest": "^26.5.2",
"lint-staged": "^10.4.0",
"jest": "^26.6.1",
"lint-staged": "^10.5.0",
"prettier": "^2.1.2",
"rimraf": "^3.0.2",
"ts-jest": "^26.4.1",
"ts-jest": "^26.4.3",
"tslint": "^6.1.3",
"tslint-config-prettier": "^1.18.0",
"typescript": "^4.0.3"
"typescript": "^4.0.5"
},
"peerDependencies": {
"web3-core": "^1.3.0"
},
"husky": {
"hooks": {
Expand Down
108 changes: 108 additions & 0 deletions src/communication/index.ts
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';
2 changes: 2 additions & 0 deletions src/messageIds.ts → src/communication/messageIds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ export const INTERFACE_MESSAGES = {
ON_SAFE_INFO: 'ON_SAFE_INFO',
TRANSACTION_CONFIRMED: 'TRANSACTION_CONFIRMED',
TRANSACTION_REJECTED: 'TRANSACTION_REJECTED',
RPC_CALL_RESPONSE: 'RPC_CALL_RESPONSE',
} as const;

export const SDK_MESSAGES = {
SAFE_APP_SDK_INITIALIZED: 'SAFE_APP_SDK_INITIALIZED',
SEND_TRANSACTIONS: 'SEND_TRANSACTIONS',
RPC_CALL: 'RPC_CALL',
SEND_TRANSACTIONS_V2: 'SEND_TRANSACTIONS_V2',
} as const;
11 changes: 11 additions & 0 deletions src/eth/constants.ts
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;
Loading

0 comments on commit 6f3d6ee

Please sign in to comment.