Skip to content

Commit

Permalink
feat(connect): implement ethereum rpc call
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasklim authored and dev-pvl committed Oct 21, 2024
1 parent b8c9d0a commit 0b4d00c
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 0 deletions.
63 changes: 63 additions & 0 deletions packages/connect/src/api/ethereum/api/ethereumCall.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { AbstractMethod } from '../../../core/AbstractMethod';
import { validateParams } from '../../common/paramsValidator';
import { ERRORS } from '../../../constants';
import { CoinInfo, EthereumCall as EthereumCallSchema } from '../../../types';
import { initBlockchain, isBackendSupported } from '../../../backend/BlockchainLink';
import { getCoinInfo } from '../../../data/coinInfo';

type Params = {
coinInfo: CoinInfo;
identity?: string;
request: EthereumCallSchema;
};

export default class EthereumCall extends AbstractMethod<'ethereumCall', Params> {
init() {
this.useDevice = false;
this.useUi = false;

const { payload } = this;

// validate incoming parameters
validateParams(payload, [
{ name: 'coin', type: 'string', required: true },
{ name: 'identity', type: 'string' },
{ name: 'from', type: 'string' },
{ name: 'to', type: 'string', required: true },
{ name: 'data', type: 'string', required: true },
]);

const coinInfo = getCoinInfo(payload.coin);

if (!coinInfo) {
throw ERRORS.TypedError('Method_UnknownCoin');
}
// validate backend
isBackendSupported(coinInfo);

this.params = {
coinInfo,
identity: payload.identity,
request: {
from: payload.from,
to: payload.to,
data: payload.data,
},
};
}

get info() {
return 'Ethereum call';
}

async run() {
const backend = await initBlockchain(
this.params.coinInfo,
postMessage,
this.params.identity,
);
const response = await backend.ethereumCall(this.params.request);

return response;
}
}
1 change: 1 addition & 0 deletions packages/connect/src/api/ethereum/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export { default as ethereumSignMessage } from './ethereumSignMessage';
export { default as ethereumSignTransaction } from './ethereumSignTransaction';
export { default as ethereumSignTypedData } from './ethereumSignTypedData';
export { default as ethereumVerifyMessage } from './ethereumVerifyMessage';
export { default as ethereumCall } from './ethereumCall';
4 changes: 4 additions & 0 deletions packages/connect/src/backend/Blockchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,10 @@ export class Blockchain {
return this.link.getAccountUtxo(descriptor);
}

ethereumCall(params: BlockchainLinkParams<'ethereumCall'>) {
return this.link.ethereumCall(params);
}

async estimateFee(request: Parameters<typeof this.link.estimateFee>[0]) {
const { blocks } = request;
// cache should be used if there is no specific data (ethereum case) and requested blocks are already cached/downloaded
Expand Down
2 changes: 2 additions & 0 deletions packages/connect/src/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ export const factory = ({

ethereumVerifyMessage: params => call({ ...params, method: 'ethereumVerifyMessage' }),

ethereumCall: params => call({ ...params, method: 'ethereumCall' }),

getAccountDescriptor: params => call({ ...params, method: 'getAccountDescriptor' }),

getAccountInfo: params => call({ ...params, method: 'getAccountInfo' }),
Expand Down
9 changes: 9 additions & 0 deletions packages/connect/src/types/api/ethereum/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,12 @@ export const EthereumVerifyMessage = Type.Object({
hex: Type.Optional(Type.Boolean()),
signature: Type.String(),
});

// ethereumCall

export type EthereumCall = Static<typeof EthereumCall>;
export const EthereumCall = Type.Object({
from: Type.String(),
to: Type.String(),
data: Type.String(),
});
6 changes: 6 additions & 0 deletions packages/connect/src/types/api/ethereumCall.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import type { CommonParamsWithCoin, Params, Response } from '../params';
import type { EthereumCall } from './ethereum';

export declare function ethereumCall(
params: CommonParamsWithCoin & Params<EthereumCall>,
): Response<{ data: string }>;
3 changes: 3 additions & 0 deletions packages/connect/src/types/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import { ethereumSignMessage } from './ethereumSignMessage';
import { ethereumSignTransaction } from './ethereumSignTransaction';
import { ethereumSignTypedData } from './ethereumSignTypedData';
import { ethereumVerifyMessage } from './ethereumVerifyMessage';
import { ethereumCall } from './ethereumCall';
import { firmwareUpdate } from './firmwareUpdate';
import { getAccountDescriptor } from './getAccountDescriptor';
import { getAccountInfo } from './getAccountInfo';
Expand Down Expand Up @@ -217,6 +218,8 @@ export interface TrezorConnect {
// https://connect.trezor.io/9/methods/ethereum/ethereumVerifyMessage/
ethereumVerifyMessage: typeof ethereumVerifyMessage;

ethereumCall: typeof ethereumCall;

// https://connect.trezor.io/9/methods/device/firmwareUpdate/
firmwareUpdate: typeof firmwareUpdate;

Expand Down

0 comments on commit 0b4d00c

Please sign in to comment.