Skip to content
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

feat: checking if the requested blueprint exists in the fullnode #16

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,18 @@
* LICENSE file in the root directory of this source tree.
*/

import { HathorWallet } from '@hathor/wallet-lib';
import { HathorWallet, ncApi } from '@hathor/wallet-lib';
import { sendNanoContractTx } from '../../src/rpcMethods/sendNanoContractTx';
import { TriggerTypes, RpcMethods, SendNanoContractRpcRequest, TriggerResponseTypes, RpcResponseTypes } from '../../src/types';
import { SendNanoContractTxError } from '../../src/errors';
import { BluePrintNotFoundError, SendNanoContractTxError } from '../../src/errors';
import { NanoRequest404Error } from '@hathor/wallet-lib/lib/errors';

jest.mock('@hathor/wallet-lib', () => ({
...jest.requireActual('@hathor/wallet-lib'),
ncApi: {
getBlueprintInformation: jest.fn(),
},
}));

describe('sendNanoContractTx', () => {
let rpcRequest: SendNanoContractRpcRequest;
Expand Down Expand Up @@ -149,4 +157,11 @@ describe('sendNanoContractTx', () => {
type: TriggerTypes.SendNanoContractTxLoadingTrigger,
}, {});
});

it('should throw BluePrintNotFoundError if the blueprint is not found in the fullnode', async () => {
const mockError = new NanoRequest404Error('Blueprint not found');
(ncApi.getBlueprintInformation as jest.Mock).mockRejectedValue(mockError);

await expect(sendNanoContractTx(rpcRequest, wallet, {}, promptHandler)).rejects.toThrow(BluePrintNotFoundError);
});
});
4 changes: 4 additions & 0 deletions packages/hathor-rpc-handler/src/errors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,7 @@ export class DifferentNetworkError extends Error {};
export class NoUtxosAvailableError extends Error {};

export class SignMessageError extends Error {};

export class ParamsValidationError extends Error {};

export class BluePrintNotFoundError extends ParamsValidationError {};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
export class BluePrintNotFoundError extends ParamsValidationError {};
export class BlueprintNotFoundError extends ParamsValidationError {};

24 changes: 22 additions & 2 deletions packages/hathor-rpc-handler/src/rpcMethods/sendNanoContractTx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/

import { HathorWallet } from '@hathor/wallet-lib';
import { HathorWallet, ncApi } from '@hathor/wallet-lib';
import {
TriggerTypes,
PinConfirmationPrompt,
Expand All @@ -19,7 +19,13 @@ import {
RpcResponseTypes,
RpcResponse,
} from '../types';
import { PromptRejectedError, SendNanoContractTxError } from '../errors';
import {
BluePrintNotFoundError,
ParamsValidationError,
PromptRejectedError,
SendNanoContractTxError,
} from '../errors';
import { NanoRequest404Error } from '@hathor/wallet-lib/lib/errors';

/**
* Sends a nano contract transaction.
Expand Down Expand Up @@ -55,6 +61,20 @@ export async function sendNanoContractTx(
throw new Error('Neither blueprint id or ncId available');
}

// Validate that the blueprintId exists
if (blueprint_id) {
try {
// Just fetch it to check if it's a valid blueprint
await ncApi.getBlueprintInformation(blueprint_id);
} catch (error) {
if (error instanceof NanoRequest404Error) {
throw new BluePrintNotFoundError();
}

throw new ParamsValidationError();
}
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should also check that the nc_id (if not executing a initialize method) is a valid nano_contract_id

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

todo: I believe it is lacking a validation on the methods existence after confirming that the blueprint exists.

const pinPrompt: PinConfirmationPrompt = {
type: TriggerTypes.PinConfirmationPrompt,
method: rpcRequest.method,
Expand Down