Skip to content

Commit

Permalink
add prototype of receipts #1
Browse files Browse the repository at this point in the history
  • Loading branch information
ukorvl committed May 22, 2024
1 parent c148332 commit 0eeb564
Show file tree
Hide file tree
Showing 21 changed files with 170 additions and 75 deletions.
5 changes: 5 additions & 0 deletions .changeset/chilly-bags-battle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"niljs": patch
---

Add commitlint config in typescript
5 changes: 5 additions & 0 deletions .changeset/sharp-owls-behave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"niljs": patch
---

Add IReceipt and ILog interfaces
11 changes: 0 additions & 11 deletions .commitlintrc

This file was deleted.

44 changes: 22 additions & 22 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,25 @@ jobs:
title: "Version Packages"
commit: "version packages"

# publish:
# name: Publish
# needs: version-pull-request
# runs-on: ubuntu-latest
# permissions:
# contents: write
# id-token: write
# steps:
# - name: Clone repository
# uses: actions/checkout@v4
# - name: Install dependencies
# uses: ./.github/actions/install-dependencies
# with:
# node_version: 20
# - name: Publish to NPM
# uses: changesets/action@v1
# with:
# createGithubReleases: true
# publish: "npm run changeset:publish"
# env:
# GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
publish:
name: Publish
needs: version-pull-request
runs-on: ubuntu-latest
permissions:
contents: write
id-token: write
steps:
- name: Clone repository
uses: actions/checkout@v4
- name: Install dependencies
uses: ./.github/actions/install-dependencies
with:
node_version: 20
- name: Publish to NPM
uses: changesets/action@v1
with:
createGithubReleases: true
publish: "npm run changeset:publish"
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
17 changes: 17 additions & 0 deletions commitlint.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import type { UserConfig } from "@commitlint/types";

const config = {
extends: ["@commitlint/config-conventional"],
rules: {
"body-max-line-length": [0, "always", 100],
"footer-max-line-length": [0, "always", 100],
"footer-leading-blank": [0, "always"],
"references-empty": [2, "never"],
"type-empty": [0, "never"],
"subject-empty": [0, "never"],
},
ignores: [(message) => message.includes("version packages")],
defaultIgnores: false,
} satisfies UserConfig;

export default config;
18 changes: 13 additions & 5 deletions src/clients/WalletClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import invariant from "tiny-invariant";
import { messageToSsz, signedMessageToSsz } from "../encoding/toSsz.js";
import type { ISigner } from "../signers/index.js";
import type { IMessage } from "../types/IMessage.js";
import type { IReceipt } from "../types/IReceipt.js";
import { assertIsValidMessage } from "../utils/assert.js";
import { BaseClient } from "./BaseClient.js";
import type { IWalletClientConfig } from "./types/ClientConfigs.js";
Expand Down Expand Up @@ -29,8 +30,7 @@ class WalletClient extends BaseClient {

/**
* sendMessage sends a message to the network.
* @param message - The message to send. It can be a raw message or a base message object.
* If the message is a raw message, it will be signed with the signer, that is passed in the constructor.
* @param message - The message to send. It will be signed with the signer.
* @param options - The options to send a message.
* @returns The hash of the message.
* @example
Expand Down Expand Up @@ -127,12 +127,20 @@ class WalletClient extends BaseClient {
*/
public async deployContract(contract: Uint8Array): Promise<Uint8Array> {
const hash = await this.sendRawMessage(contract);
// there will be a method to get receipt by hash
// receipt - result of smart conract calling
// we should wait to receipts to be sure that message is included in the block
// there will be a method to get receipt by hash, but it is not implemented yet
// it will use kinda polling to get receipt asap
// mocking it for now:
const receipt: Partial<IReceipt> = {
success: true,
};

// ! compiling smart contract to the bytecode shall not be included in this library
// it can be done by hardhat
invariant(
receipt.success,
"Contract deployment failed. Please check the contract bytecode.",
);

return hash;
}
}
Expand Down
16 changes: 15 additions & 1 deletion src/encoding/fromHex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
type Hex,
hexToBytes as hexToBytesNoble,
} from "@noble/curves/abstract/utils";
import { addHexPrefix } from "../index.js";

/**
* Convert a hex string to bytes.
Expand Down Expand Up @@ -42,4 +43,17 @@ const hexToString = (hex: Hex): string => {
return Buffer.from(hex, "hex").toString("utf-8");
};

export { hexToBytes, hexToNumber, hexToString };
/**
* Convert a hex string to a bigint.
* @param hex - The hex string to convert to a bigint.
* @returns The bigint representation of the input.
*/
const hexToBigInt = (hex: Hex): bigint => {
if (typeof hex !== "string") {
return hexToBigInt(hex.toString());
}

return BigInt(addHexPrefix(hex));
};

export { hexToBytes, hexToNumber, hexToString, hexToBigInt };
2 changes: 1 addition & 1 deletion src/signers/LocalKeySigner.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { secp256k1 } from "@noble/curves/secp256k1";
import { toHex } from "../encoding/toHex.js";
import { assertIsHexString, assertIsValidPrivateKey } from "../utils/assert.js";
import { getPublicKey } from "./getPublicKey.js";
import { getPublicKey } from "./publicKey.js";
import type { ILocalKeySignerConfig } from "./types/ILocalKeySignerConfig.js";
import type { ISignature } from "./types/ISignature.js";
import type { ISigner } from "./types/ISigner.js";
Expand Down
13 changes: 0 additions & 13 deletions src/signers/generatePrivateKey.ts

This file was deleted.

14 changes: 0 additions & 14 deletions src/signers/getPublicKey.ts

This file was deleted.

5 changes: 2 additions & 3 deletions src/signers/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
export * from "./LocalKeySigner.js";
export * from "./verifySignature.js";
export * from "./getPublicKey.js";
export * from "./verifyMessage.js";
export * from "./publicKey.js";
export * from "./types/ILocalKeySignerConfig.js";
export * from "./types/IPrivateKey.js";
export * from "./types/ISigner.js";
export * from "./types/ISignature.js";
export * from "./generatePrivateKey.js";
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { accounts } from "../../test/mocks/accounts.js";
import { getPublicKey } from "./getPublicKey.js";
import { getPublicKey } from "./publicKey.js";

test("getPublicKey", async ({ expect }) => {
const account = accounts[0];
Expand Down
34 changes: 34 additions & 0 deletions src/signers/publicKey.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { type Hex, bytesToHex } from "@noble/curves/abstract/utils";
import { secp256k1 } from "@noble/curves/secp256k1";
import {
type ISignature,
addHexPrefix,
removeHexPrefix,
toHex,
} from "../index.js";
import type { IPrivateKey } from "./types/IPrivateKey.js";

/**
* Returns the public key from the private key using the secp256k1 curve.
*/
const getPublicKey = (privateKey: IPrivateKey): Hex => {
const publicKey = secp256k1.getPublicKey(removeHexPrefix(privateKey), false);
return addHexPrefix(bytesToHex(publicKey));
};

/**
* Generate a new private key.
* @returns Hex - Private key
* @example
* const privateKey = generatePrivateKey();
*/
const generatePrivateKey = (): Hex => toHex(secp256k1.utils.randomPrivateKey());

const recoverPublicKey = (
messageHash: Hex | Uint8Array,
signature: ISignature,
) => {
//
};

export { getPublicKey, generatePrivateKey, recoverPublicKey };
File renamed without changes.
10 changes: 10 additions & 0 deletions src/signers/verifyMessage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import type { Hex } from "@noble/curves/abstract/utils";
import type { ISignature } from "./index.js";

const verifyMessage = async (
signature: ISignature,
messageHash: Uint8Array,
fullPublicKey: Hex,
) => {
//
};
Empty file.
Empty file removed src/signers/verifySignature.ts
Empty file.
11 changes: 11 additions & 0 deletions src/types/ILog.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* Log interface.
*/
type ILog = {
address: string;
topics: string[];
data: string;
blockNumber: bigint;
};

export type { ILog };
18 changes: 18 additions & 0 deletions src/types/IReceipt.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import type { ILog } from "./ILog.js";

/**
* Receipt interface.
*/
type IReceipt = {
success: boolean;
gasUsed: number;
bloom: string;
logs: ILog[];
msgHash: string;
contractAddress: string;
blockHash: string;
blockNumber: bigint;
msgIndex: bigint;
};

export type { IReceipt };
7 changes: 3 additions & 4 deletions src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
export * from "../clients/types/ClientConfigs.js";
export * from "./IMessage.js";
export * from "../signers/types/ISigner.js";
export * from "../signers/types/IPrivateKey.js";
export * from "./IBlock.js";
export * from "../signers/types/ILocalKeySignerConfig.js";
export * from "./ISignedMessage.js";
export * from "./IReceipt.js";
export * from "./ILog.js";
13 changes: 13 additions & 0 deletions src/utils/assert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,22 @@ const assertIsValidMessage = (message: IMessage) => {
);
};

/**
* Checks if the address is valid. If the address is valid, it returns nothing.
* @param address - The address to check.
* @param message - The message to throw if the address is invalid.
*/
const assertIsAddress = (address: string, message?: string): void => {
invariant(
isAddress(address),
message ?? `Expected a valid address but got ${address}`,
);
};

export {
assertIsBuffer,
assertIsHexString,
assertIsValidPrivateKey,
assertIsValidMessage,
assertIsAddress,
};

0 comments on commit 0eeb564

Please sign in to comment.