From 6c3569179a0cafa90ba372e540e1332cbc6e2c7f Mon Sep 17 00:00:00 2001 From: Harsh Rastogi <142212174+sarvalabs-harshrastogi@users.noreply.github.com> Date: Mon, 22 Jan 2024 15:59:43 +0530 Subject: [PATCH] fix: make signer optional in getlogicdriver (#25) --- packages/js-moi-logic/dist/logic-base.d.ts | 9 ++-- packages/js-moi-logic/dist/logic-base.js | 5 +- packages/js-moi-logic/src/logic-base.ts | 15 +++--- .../__tests__/jsonrpc-provider.test.ts | 6 +-- .../js-moi-providers/src/base-provider.ts | 53 ++++--------------- packages/js-moi-signer/dist/signer.d.ts | 1 + packages/js-moi-signer/dist/signer.js | 5 +- packages/js-moi-signer/src/signer.ts | 7 ++- 8 files changed, 40 insertions(+), 61 deletions(-) diff --git a/packages/js-moi-logic/dist/logic-base.d.ts b/packages/js-moi-logic/dist/logic-base.d.ts index 24f37db5..6a4d9a9f 100644 --- a/packages/js-moi-logic/dist/logic-base.d.ts +++ b/packages/js-moi-logic/dist/logic-base.d.ts @@ -1,11 +1,10 @@ import { LogicManifest, ManifestCoder } from "js-moi-manifest"; -import { IxType } from "js-moi-utils"; -import { LogicPayload } from "js-moi-providers"; +import { InteractionCallResponse, InteractionResponse, LogicPayload } from "js-moi-providers"; import { Signer } from "js-moi-signer"; -import { InteractionResponse, InteractionCallResponse } from "js-moi-providers"; -import ElementDescriptor from "./element-descriptor"; -import { LogicIxRequest } from "../types/logic"; +import { IxType } from "js-moi-utils"; import { LogicIxArguments, LogicIxObject, LogicIxResponse, LogicIxResult } from "../types/interaction"; +import { LogicIxRequest } from "../types/logic"; +import ElementDescriptor from "./element-descriptor"; /** * This abstract class extends the ElementDescriptor class and serves as a base class for logic-related operations. diff --git a/packages/js-moi-logic/dist/logic-base.js b/packages/js-moi-logic/dist/logic-base.js index 5d3daac3..e6fd1aeb 100644 --- a/packages/js-moi-logic/dist/logic-base.js +++ b/packages/js-moi-logic/dist/logic-base.js @@ -98,10 +98,13 @@ class LogicBase extends element_descriptor_1.default { if (args.length < 2) { js_moi_utils_1.ErrorUtils.throwError("One or more required arguments are missing.", js_moi_utils_1.ErrorCode.MISSING_ARGUMENT); } + if (args[1].sender == null && this.signer.isInitialized()) { + args[1].sender = this.signer.getAddress(); + } return { type: args[0], params: { - sender: this.signer.getAddress(), + sender: args[1].sender, type: this.getIxType(), nonce: args[1].nonce, fuel_price: args[1].fuelPrice, diff --git a/packages/js-moi-logic/src/logic-base.ts b/packages/js-moi-logic/src/logic-base.ts index 823305dd..6fa5eb32 100644 --- a/packages/js-moi-logic/src/logic-base.ts +++ b/packages/js-moi-logic/src/logic-base.ts @@ -1,11 +1,10 @@ import { LogicManifest, ManifestCoder } from "js-moi-manifest"; -import { ErrorCode, ErrorUtils, IxType } from "js-moi-utils"; -import { LogicPayload } from "js-moi-providers"; +import { InteractionCallResponse, InteractionResponse, LogicPayload } from "js-moi-providers"; import { Signer } from "js-moi-signer"; -import { InteractionResponse, InteractionCallResponse } from "js-moi-providers"; -import ElementDescriptor from "./element-descriptor"; -import { LogicIxRequest } from "../types/logic"; +import { ErrorCode, ErrorUtils, IxType } from "js-moi-utils"; import { LogicIxArguments, LogicIxObject, LogicIxResponse, LogicIxResult } from "../types/interaction"; +import { LogicIxRequest } from "../types/logic"; +import ElementDescriptor from "./element-descriptor"; /** * This abstract class extends the ElementDescriptor class and serves as a base @@ -124,11 +123,15 @@ export abstract class LogicBase extends ElementDescriptor { ErrorCode.MISSING_ARGUMENT ) } + + if(args[1].sender == null && this.signer.isInitialized()) { + args[1].sender = this.signer.getAddress() + } return { type: args[0], params: { - sender: this.signer.getAddress(), + sender: args[1].sender, type: this.getIxType(), nonce: args[1].nonce, fuel_price: args[1].fuelPrice, diff --git a/packages/js-moi-providers/__tests__/jsonrpc-provider.test.ts b/packages/js-moi-providers/__tests__/jsonrpc-provider.test.ts index 09bc03c2..437ee3ad 100644 --- a/packages/js-moi-providers/__tests__/jsonrpc-provider.test.ts +++ b/packages/js-moi-providers/__tests__/jsonrpc-provider.test.ts @@ -5,10 +5,8 @@ import { Filter, InteractionReceipt } from "../types/jsonrpc"; import { initializeWallet } from "./utils/utils"; describe("Test JsonRpcProvider Query Calls", () => { - const address = - "0x2c1fe83b9d6a5c81c5e6d4da20d2d0509ac3c1eb154e5f5b1fc7d5fd4a03b9cc"; - const mnemonic = - "cushion tissue toss meadow glare math custom because inform describe vacant combine"; + const address = "0x898ca25ac7a51a36894b9c9f55ec6212500dd8e0c01f6591f0eb9f5b0bc84655"; + const mnemonic = "hockey airport rather chef nasty shrimp tragic embrace olive another own hen"; let provider: JsonRpcProvider; let ixHash: string; let signer: Signer; diff --git a/packages/js-moi-providers/src/base-provider.ts b/packages/js-moi-providers/src/base-provider.ts index f4f6b8ee..073c70b9 100644 --- a/packages/js-moi-providers/src/base-provider.ts +++ b/packages/js-moi-providers/src/base-provider.ts @@ -1,51 +1,18 @@ import { LogicManifest } from "js-moi-manifest"; import { - AssetCreationReceipt, AssetMintOrBurnReceipt, - ErrorCode, ErrorUtils, - Interaction, - IxType, - LogicDeployReceipt, LogicInvokeReceipt, Tesseract, - bytesToHex, hexDataLength, - hexToBN, - hexToBytes, - toQuantity, - unmarshal + AssetCreationReceipt, AssetMintOrBurnReceipt, ErrorCode, ErrorUtils, Interaction, + IxType, LogicDeployReceipt, LogicInvokeReceipt, Tesseract, bytesToHex, hexDataLength, + hexToBN, hexToBytes, toQuantity, unmarshal } from "js-moi-utils"; import { EventType, Listener } from "../types/event"; import { - AccountMetaInfo, - AccountMetaInfoParams, - AccountParamsBase, AccountState, - AccountStateParams, - AssetInfo, - AssetInfoParams, BalanceParams, - CallorEstimateIxObject, - CallorEstimateOptions, - ConnectionsInfo, - Content, - ContentFrom, - ContextInfo, - DBEntryParams, - Encoding, - Filter, FilterDeletionResult, - Inspect, - InteractionByTesseractParams, - InteractionCallResponse, - InteractionParams, - InteractionReceipt, - InteractionRequest, - InteractionResponse, - LogicManifestParams, - NodeInfo, - Options, - Registry, - RpcResponse, - Status, - StorageParams, - SyncStatus, SyncStatusParams, - TDU, - TDUResponse, - TesseractParams + AccountMetaInfo, AccountMetaInfoParams, AccountParamsBase, AccountState, AccountStateParams, + AssetInfo, AssetInfoParams, BalanceParams, CallorEstimateIxObject, CallorEstimateOptions, + ConnectionsInfo, Content, ContentFrom, ContextInfo, DBEntryParams, Encoding, Filter, + FilterDeletionResult, Inspect, InteractionByTesseractParams, InteractionCallResponse, + InteractionParams, InteractionReceipt, InteractionRequest, InteractionResponse, + LogicManifestParams, NodeInfo, Options, Registry, RpcResponse, Status, StorageParams, + SyncStatus, SyncStatusParams, TDU, TDUResponse, TesseractParams } from "../types/jsonrpc"; import { AbstractProvider } from "./abstract-provider"; import Event from "./event"; diff --git a/packages/js-moi-signer/dist/signer.d.ts b/packages/js-moi-signer/dist/signer.d.ts index e65c255e..8f3c1e75 100644 --- a/packages/js-moi-signer/dist/signer.d.ts +++ b/packages/js-moi-signer/dist/signer.d.ts @@ -11,6 +11,7 @@ export declare abstract class Signer { abstract getAddress(): string; abstract connect(provider: AbstractProvider): void; abstract sign(message: Uint8Array, sigAlgo: SigType): string; + abstract isInitialized(): boolean; abstract signInteraction(ixObject: InteractionObject, sigAlgo: SigType): InteractionRequest; /** * Retrieves the connected provider instance. diff --git a/packages/js-moi-signer/dist/signer.js b/packages/js-moi-signer/dist/signer.js index 7284633c..d387e3d8 100644 --- a/packages/js-moi-signer/dist/signer.js +++ b/packages/js-moi-signer/dist/signer.js @@ -69,7 +69,10 @@ class Signer { if (!(0, js_moi_utils_1.isValidAddress)(ixObject.sender)) { js_moi_utils_1.ErrorUtils.throwError("Invalid sender address", js_moi_utils_1.ErrorCode.INVALID_ARGUMENT); } - if (ixObject.sender !== this.getAddress()) { + if (ixObject.sender == null) { + js_moi_utils_1.ErrorUtils.throwError("Sender address is missing", js_moi_utils_1.ErrorCode.MISSING_ARGUMENT); + } + if (this.isInitialized() && ixObject.sender !== this.getAddress()) { js_moi_utils_1.ErrorUtils.throwError("Sender address mismatches with the signer", js_moi_utils_1.ErrorCode.UNEXPECTED_ARGUMENT); } if (ixObject.type === js_moi_utils_1.IxType.VALUE_TRANSFER) { diff --git a/packages/js-moi-signer/src/signer.ts b/packages/js-moi-signer/src/signer.ts index 5cb2e434..c77447e3 100644 --- a/packages/js-moi-signer/src/signer.ts +++ b/packages/js-moi-signer/src/signer.ts @@ -22,6 +22,7 @@ export abstract class Signer { abstract getAddress(): string; abstract connect(provider: AbstractProvider): void; abstract sign(message: Uint8Array, sigAlgo: SigType): string; + abstract isInitialized(): boolean; abstract signInteraction(ixObject: InteractionObject, sigAlgo: SigType): InteractionRequest; @@ -83,7 +84,11 @@ export abstract class Signer { ErrorUtils.throwError("Invalid sender address", ErrorCode.INVALID_ARGUMENT); } - if(ixObject.sender !== this.getAddress()) { + if(ixObject.sender == null) { + ErrorUtils.throwError("Sender address is missing", ErrorCode.MISSING_ARGUMENT); + } + + if(this.isInitialized() && ixObject.sender !== this.getAddress()) { ErrorUtils.throwError("Sender address mismatches with the signer", ErrorCode.UNEXPECTED_ARGUMENT); }