Skip to content

Commit

Permalink
feat: added helper methods to allow dapps to create rpc requests
Browse files Browse the repository at this point in the history
  • Loading branch information
andreabadesso committed Aug 13, 2024
1 parent b2176d0 commit 7ad95f7
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 14 deletions.
1 change: 1 addition & 0 deletions packages/hathor-rpc-handler/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

export * from './rpcHandler';
export * from './rpcRequest';
export * from './types';
export * from './rpcMethods';
export * from './errors';
2 changes: 2 additions & 0 deletions packages/hathor-rpc-handler/src/rpcRequest/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './sendNanoContractTx';
export * from './signWithAddress';
33 changes: 33 additions & 0 deletions packages/hathor-rpc-handler/src/rpcRequest/sendNanoContractTx.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Copyright (c) Hathor Labs and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

import type { NanoContractAction } from '@hathor/wallet-lib/lib/nano_contracts/types';
import {
RpcMethods,
SendNanoContractRpcRequest,
} from '../types';

export function sendNanoContractTxRpcRequest(
method: string,
blueprintId: string,
actions: NanoContractAction[],
args: unknown[],
pushTx: boolean,
ncId: string | null,
): SendNanoContractRpcRequest {
return {
method: RpcMethods.SendNanoContractTx,
params: {
method,
blueprint_id: blueprintId,
actions,
args,
push_tx: pushTx,
nc_id: ncId,
}
};
}
26 changes: 26 additions & 0 deletions packages/hathor-rpc-handler/src/rpcRequest/signWithAddress.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* Copyright (c) Hathor Labs and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

import {
RpcMethods,
SignWithAddressRpcRequest,
} from '../types';

export function signWithAddressRpcRequest(
network: string,
message: string,
addressIndex: number,
): SignWithAddressRpcRequest {
return {
method: RpcMethods.SignWithAddress,
params: {
network,
message,
addressIndex,
}
};
}
22 changes: 8 additions & 14 deletions packages/hathor-rpc-handler/src/types/rpcRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,7 @@ export enum RpcMethods {
SendNanoContractTx = 'htr_sendNanoContractTx',
}

export interface BaseRpcRequest {
method: string;
id: string;
jsonrpc: string;
}

export interface CreateTokenRpcRequest extends BaseRpcRequest {
export interface CreateTokenRpcRequest {
method: RpcMethods.CreateToken,
params: {
name: string;
Expand All @@ -45,7 +39,7 @@ export interface CreateTokenRpcRequest extends BaseRpcRequest {
}
}

export interface GetAddressRpcRequest extends BaseRpcRequest {
export interface GetAddressRpcRequest {
method: RpcMethods.GetAddress,
params: {
type: 'first_empty' | 'full_path' | 'index' | 'client';
Expand All @@ -55,7 +49,7 @@ export interface GetAddressRpcRequest extends BaseRpcRequest {
}
}

export interface GetBalanceRpcRequest extends BaseRpcRequest {
export interface GetBalanceRpcRequest {
method: RpcMethods.GetBalance,
params: {
network: string;
Expand All @@ -64,7 +58,7 @@ export interface GetBalanceRpcRequest extends BaseRpcRequest {
};
}

export interface GetUtxosRpcRequest extends BaseRpcRequest {
export interface GetUtxosRpcRequest {
method: RpcMethods.GetUtxos,
params: {
network: string;
Expand All @@ -79,7 +73,7 @@ export interface GetUtxosRpcRequest extends BaseRpcRequest {
};
}

export interface SignWithAddressRpcRequest extends BaseRpcRequest {
export interface SignWithAddressRpcRequest {
method: RpcMethods.SignWithAddress,
params: {
network: string;
Expand All @@ -88,7 +82,7 @@ export interface SignWithAddressRpcRequest extends BaseRpcRequest {
}
}

export interface SendNanoContractRpcRequest extends BaseRpcRequest {
export interface SendNanoContractRpcRequest {
method: RpcMethods.SendNanoContractTx,
params: {
method: string;
Expand All @@ -104,11 +98,11 @@ export type RequestMetadata = {
[key: string]: string,
};

export interface GetConnectedNetworkRpcRequest extends BaseRpcRequest {
export interface GetConnectedNetworkRpcRequest {
method: RpcMethods.GetConnectedNetwork,
}

export interface GenericRpcRequest extends BaseRpcRequest {
export interface GenericRpcRequest {
params?: unknown | null;
}

Expand Down

0 comments on commit 7ad95f7

Please sign in to comment.