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: support for optional nonce in logic calls #19

Merged
merged 9 commits into from
Jan 22, 2024
Merged
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
6 changes: 4 additions & 2 deletions docs/source/logic.rst
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@ applications on the MOI network.
const mnemonic = "mother clarify push liquid ordinary social track ...";
const provider = new JsonRpcProvider("http://localhost:1600/");
const wallet = new Wallet(provider);
return await wallet.fromMnemonic(mnemonic);
await wallet.fromMnemonic(mnemonic);
return wallet;
}

const manifest = { ... }
Expand Down Expand Up @@ -265,7 +266,8 @@ Functions
const mnemonic = "mother clarify push liquid ordinary social track ...";
const provider = new JsonRpcProvider("http://localhost:1600/");
const wallet = new Wallet(provider);
return await wallet.fromMnemonic(mnemonic);
await wallet.fromMnemonic(mnemonic);
return wallet;
}

const logicId = "0x0800007d70c34ed6ec4384c75d469894052647a078b33ac0f08db0d3751c1fce29a49a";
Expand Down
1 change: 1 addition & 0 deletions packages/js-moi-logic/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ npm install js-moi-wallet
const provider = new JsonRpcProvider("http://localhost:1600/");
const wallet = new Wallet(provider);
await wallet.fromMnemonic(mnemonic);
return wallet;
}

(async () => {
Expand Down
3 changes: 2 additions & 1 deletion packages/js-moi-logic/dist/logic-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,10 @@ class LogicBase extends element_descriptor_1.default {
params: {
sender: this.signer.getAddress(),
type: this.getIxType(),
nonce: args[1].nonce,
fuel_price: args[1].fuelPrice,
fuel_limit: args[1].fuelLimit,
payload: ixObject.createPayload()
payload: ixObject.createPayload(),
}
};
}
Expand Down
2 changes: 1 addition & 1 deletion packages/js-moi-logic/dist/tsconfig.tsbuildinfo

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion packages/js-moi-logic/src/logic-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,10 @@ export abstract class LogicBase extends ElementDescriptor {
params: {
sender: this.signer.getAddress(),
type: this.getIxType(),
nonce: args[1].nonce,
fuel_price: args[1].fuelPrice,
fuel_limit: args[1].fuelLimit,
payload: ixObject.createPayload()
payload: ixObject.createPayload(),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/js-moi-signer/dist/signer.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AbstractProvider, Options, InteractionResponse, InteractionRequest, InteractionCallResponse, InteractionObject } from "js-moi-providers";
import { AbstractProvider, InteractionCallResponse, InteractionObject, InteractionRequest, InteractionResponse, Options } from "js-moi-providers";
import { SigType, SigningAlgorithms } from "../types";
/**
* An abstract class representing a signer responsible for cryptographic
Expand Down
13 changes: 6 additions & 7 deletions packages/js-moi-signer/dist/signer.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Signer = void 0;
const js_moi_utils_1 = require("js-moi-utils");
const ecdsa_1 = __importDefault(require("./ecdsa"));
const signature_1 = __importDefault(require("./signature"));
const js_moi_utils_1 = require("js-moi-utils");
/**
* An abstract class representing a signer responsible for cryptographic
* activities like signing and verification.
Expand Down Expand Up @@ -62,7 +62,7 @@ class Signer {
* @param {number | bigint} nonce - The nonce (interaction count) for comparison.
* @throws {Error} if any of the checks fail, indicating an invalid interaction.
*/
checkInteraction(ixObject, nonce) {
async checkInteraction(ixObject) {
if (ixObject.type === undefined || ixObject.type === null) {
js_moi_utils_1.ErrorUtils.throwError("Interaction type is missing", js_moi_utils_1.ErrorCode.MISSING_ARGUMENT);
}
Expand Down Expand Up @@ -90,6 +90,7 @@ class Signer {
js_moi_utils_1.ErrorUtils.throwError("Invalid fuel limit", js_moi_utils_1.ErrorCode.INTERACTION_UNDERPRICED);
}
if (ixObject.nonce !== undefined || ixObject.nonce !== null) {
const nonce = await this.getNonce({ tesseract_number: -1 });
if (ixObject.nonce < nonce) {
js_moi_utils_1.ErrorUtils.throwError("Invalid nonce", js_moi_utils_1.ErrorCode.NONCE_EXPIRED);
}
Expand All @@ -105,14 +106,12 @@ class Signer {
* an error during preparation.
*/
async prepareInteraction(ixObject) {
const nonce = await this.getNonce();
if (!ixObject.sender) {
ixObject.sender = this.getAddress();
}
// Check the validity of the interaction object
this.checkInteraction(ixObject, nonce);
if (ixObject.nonce !== undefined || ixObject.nonce !== null) {
ixObject.nonce = nonce;
await this.checkInteraction(ixObject);
if (ixObject.nonce != null) {
ixObject.nonce = await this.getNonce();
}
}
/**
Expand Down
2 changes: 1 addition & 1 deletion packages/js-moi-signer/dist/tsconfig.tsbuildinfo

Large diffs are not rendered by default.

22 changes: 10 additions & 12 deletions packages/js-moi-signer/src/signer.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { AbstractProvider, Options, InteractionResponse, InteractionRequest, CallorEstimateIxObject, InteractionCallResponse, InteractionObject } from "js-moi-providers";
import ECDSA_S256 from "./ecdsa";
import { AbstractProvider, CallorEstimateIxObject, InteractionCallResponse, InteractionObject, InteractionRequest, InteractionResponse, Options } from "js-moi-providers";
import { ErrorCode, ErrorUtils, IxType, hexToBytes, isValidAddress } from "js-moi-utils";
import { SigType, SigningAlgorithms } from "../types";
import ECDSA_S256 from "./ecdsa";
import Signature from "./signature";
import { ErrorCode, ErrorUtils, hexToBytes, IxType, isValidAddress } from "js-moi-utils";

/**
* An abstract class representing a signer responsible for cryptographic
Expand Down Expand Up @@ -74,7 +74,7 @@ export abstract class Signer {
* @param {number | bigint} nonce - The nonce (interaction count) for comparison.
* @throws {Error} if any of the checks fail, indicating an invalid interaction.
*/
private checkInteraction(ixObject: InteractionObject, nonce: number | bigint): void {
private async checkInteraction(ixObject: InteractionObject): Promise<void> {
if(ixObject.type === undefined || ixObject.type === null) {
ErrorUtils.throwError("Interaction type is missing", ErrorCode.MISSING_ARGUMENT)
}
Expand Down Expand Up @@ -110,6 +110,7 @@ export abstract class Signer {
}

if(ixObject.nonce !== undefined || ixObject.nonce !== null) {
const nonce = await this.getNonce({ tesseract_number: -1 });
if(ixObject.nonce < nonce) {
ErrorUtils.throwError("Invalid nonce", ErrorCode.NONCE_EXPIRED);
}
Expand All @@ -126,17 +127,14 @@ export abstract class Signer {
* an error during preparation.
*/
private async prepareInteraction(ixObject: InteractionObject): Promise<void> {
const nonce = await this.getNonce();

if (!ixObject.sender) {
ixObject.sender = this.getAddress();
}

// Check the validity of the interaction object
this.checkInteraction(ixObject, nonce);

if (ixObject.nonce !== undefined || ixObject.nonce !== null) {
ixObject.nonce = nonce;

await this.checkInteraction(ixObject);

if (ixObject.nonce != null) {
ixObject.nonce = await this.getNonce();
}
}

Expand Down