Skip to content

Commit

Permalink
remove viem encoding #53
Browse files Browse the repository at this point in the history
  • Loading branch information
ukorvl committed Jul 3, 2024
1 parent 08a7f90 commit 314612f
Show file tree
Hide file tree
Showing 33 changed files with 226 additions and 67 deletions.
5 changes: 5 additions & 0 deletions .changeset/fluffy-paws-float.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@nilfoundation/niljs": minor
---

Remove viem encoding and replace with local implementation
1 change: 1 addition & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ name: Release
on:
pull_request:
types: [closed]
branches: [master]
workflow_dispatch:

concurrency:
Expand Down
2 changes: 1 addition & 1 deletion examples/asyncCall.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { bytesToHex } from "viem";
import {
Faucet,
HttpTransport,
LocalECDSAKeySigner,
PublicClient,
WalletV1,
bytesToHex,
generateRandomPrivateKey,
waitTillCompleted,
} from "../src";
Expand Down
3 changes: 2 additions & 1 deletion examples/bounce.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { bytesToHex, encodeFunctionData } from "viem";
import { encodeFunctionData } from "viem";
import {
Faucet,
HttpTransport,
LocalECDSAKeySigner,
PublicClient,
WalletV1,
bytesToHex,
generateRandomPrivateKey,
waitTillCompleted,
} from "../src";
Expand Down
2 changes: 1 addition & 1 deletion examples/deployInternalMessage.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type { Abi } from "abitype";
import { bytesToHex } from "viem";
import {
Faucet,
HttpTransport,
LocalECDSAKeySigner,
PublicClient,
WalletV1,
bytesToHex,
generateRandomPrivateKey,
waitTillCompleted,
} from "../src";
Expand Down
2 changes: 1 addition & 1 deletion examples/deployWallet.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { bytesToHex } from "viem";
import {
Faucet,
HttpTransport,
LocalECDSAKeySigner,
PublicClient,
WalletV1,
bytesToHex,
generateRandomPrivateKey,
} from "../src";

Expand Down
2 changes: 1 addition & 1 deletion examples/externalContractDeployment.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { bytesToHex } from "viem";
import {
Faucet,
HttpTransport,
LocalECDSAKeySigner,
PublicClient,
WalletV1,
bytesToHex,
externalDeploymentMessage,
generateRandomPrivateKey,
} from "../src";
Expand Down
2 changes: 1 addition & 1 deletion examples/syncCall.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { bytesToHex } from "viem";
import {
Faucet,
HttpTransport,
LocalECDSAKeySigner,
PublicClient,
WalletV1,
bytesToHex,
convertEthToWei,
generateRandomPrivateKey,
} from "../src";
Expand Down
4 changes: 3 additions & 1 deletion examples/tokenMint.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { bytesToHex, encodeFunctionData, hexToBigInt } from "viem";
import { encodeFunctionData } from "viem";
import {
Faucet,
HttpTransport,
Expand All @@ -7,7 +7,9 @@ import {
MINTER_ADDRESS,
PublicClient,
WalletV1,
bytesToHex,
generateRandomPrivateKey,
hexToBigInt,
waitTillCompleted,
} from "../src";

Expand Down
12 changes: 8 additions & 4 deletions src/clients/PublicClient.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { bytesToHex } from "@noble/curves/abstract/utils";
import { hexToBytes, numberToHex } from "viem";
import { hexToBigInt, hexToNumber } from "../encoding/index.js";
import {
bytesToHex,
hexToBigInt,
hexToBytes,
hexToNumber,
toHex,
} from "../encoding/index.js";
import { BlockNotFoundError } from "../errors/block.js";
import { type Hex, assertIsValidShardId } from "../index.js";
import type { IAddress } from "../signers/types/IAddress.js";
Expand Down Expand Up @@ -410,7 +414,7 @@ class PublicClient extends BaseClient {
typeof callArgs.data === "string"
? callArgs.data
: addHexPrefix(bytesToHex(callArgs.data)),
value: numberToHex(callArgs.value || 0n),
value: toHex(callArgs.value || 0n),
gasLimit: (callArgs.gasLimit || 5_000_000n).toString(10),
};

Expand Down
10 changes: 6 additions & 4 deletions src/contracts/Faucet/Faucet.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { bytesToHex } from "viem";
import { generatePrivateKey } from "viem/accounts";
import { PublicClient } from "../../clients/index.js";
import { LocalECDSAKeySigner } from "../../signers/index.js";
import { bytesToHex } from "../../index.js";
import {
LocalECDSAKeySigner,
generateRandomPrivateKey,
} from "../../signers/index.js";
import { MockTransport } from "../../transport/MockTransport.js";
import { WalletV1 } from "../WalletV1/WalletV1.js";
import { Faucet } from "./Faucet.js";

const signer = new LocalECDSAKeySigner({
privateKey: generatePrivateKey(),
privateKey: generateRandomPrivateKey(),
});

test("Faucet with retry", async () => {
Expand Down
4 changes: 2 additions & 2 deletions src/contracts/Faucet/Faucet.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { type Hex, bytesToHex, encodeFunctionData, hexToBytes } from "viem";
import { type Hex, bytesToHex, encodeFunctionData } from "viem";
import type { PublicClient } from "../../clients/PublicClient.js";
import { ExternalMessageEnvelope } from "../../encoding/externalMessage.js";
import { hexToBytes } from "../../index.js";
import type { IReceipt } from "../../types/IReceipt.js";
import { getShardIdFromAddress } from "../../utils/address.js";
import { waitTillCompleted } from "../../utils/receipt.js";
Expand All @@ -9,7 +10,6 @@ import FaucetAbi from "./Faucet.abi.json";
/**
* Faucet is a special contract that is used to top up other contracts in the =nil; devnet.
*
* @class Faucet
* @typedef {Faucet}
*/
Expand Down
4 changes: 2 additions & 2 deletions src/contracts/WalletV1/WalletV1.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { generatePrivateKey } from "viem/accounts";
import { PublicClient } from "../../clients/index.js";
import { generateRandomPrivateKey } from "../../index.js";
import { LocalECDSAKeySigner } from "../../signers/LocalECDSAKeySigner.js";
import { MockTransport } from "../../transport/MockTransport.js";
import { HttpTransport } from "../../transport/index.js";
import { WalletV1 } from "./WalletV1.js";

const signer = new LocalECDSAKeySigner({
privateKey: generatePrivateKey(),
privateKey: generateRandomPrivateKey(),
});
const pubkey = await signer.getPublicKey();
const client = new PublicClient({
Expand Down
9 changes: 5 additions & 4 deletions src/contracts/WalletV1/WalletV1.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import type { Abi } from "abitype";
import invariant from "tiny-invariant";
import { bytesToHex, encodeFunctionData, hexToBytes } from "viem";
import { bytesToHex, encodeFunctionData } from "viem";
import type { PublicClient } from "../../clients/PublicClient.js";
import { prepareDeployPart } from "../../encoding/deployPart.js";
import { externalMessageEncode } from "../../encoding/externalMessage.js";
import { hexToBytes, toHex } from "../../index.js";
import type { ISigner } from "../../signers/index.js";
import type { IDeployData } from "../../types/IDeployData.js";
import { getShardIdFromAddress, refineAddress } from "../../utils/address.js";
Expand All @@ -19,9 +20,9 @@ import type {
} from "./types/index.js";

/**
* WalletV1 is a class used for performing operations on the cluster that require authentication.
* WalletV1 is a class used for performing operations on the cluster that require authentication.
*
* @class WalletV1
* @typedef {WalletV1}
*/
Expand Down Expand Up @@ -166,7 +167,7 @@ export class WalletV1 {
if (salt) {
this.salt = refineSalt(salt);
}
this.shardId = getShardIdFromAddress(this.address);
this.shardId = getShardIdFromAddress(toHex(this.address));
}

/**
Expand Down
4 changes: 3 additions & 1 deletion src/encoding/deployPart.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { bytesToHex, encodeDeployData, hexToBytes } from "viem";
import { encodeDeployData } from "viem";
import type { IDeployData } from "../types/IDeployData.js";
import { calculateAddress } from "../utils/address.js";
import { refineSalt } from "../utils/refiners.js";
import { bytesToHex } from "./fromBytes.js";
import { hexToBytes } from "./fromHex.js";

/**
* Refines the provided salt and generates the full bytecode for deployment. Returns the bytecode and the deployment address.
Expand Down
6 changes: 3 additions & 3 deletions src/encoding/externalMessage.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { bytesToHex } from "viem";
import type { PublicClient } from "../clients/PublicClient.js";
import type { ISigner } from "../signers/index.js";
import type { ExternalMessage } from "../types/ExternalMessage.js";
import type { IDeployData } from "../types/IDeployData.js";
import { prepareDeployPart } from "./deployPart.js";
import { bytesToHex } from "./fromBytes.js";
import { SszMessageSchema, SszSignedMessageSchema } from "./ssz.js";

/**
* The envelope for an external message (a message sent by a user, a dApp, etc.)
*
* @class ExternalMessageEnvelope
* @typedef {ExternalMessageEnvelope}
*/
Expand Down Expand Up @@ -206,7 +206,7 @@ export class ExternalMessageEnvelope {
/**
* The envelope for an internal message (a message sent by a smart contract to another smart contract).
*
* @class InternalMessageEnvelope
* @typedef {InternalMessageEnvelope}
*/
Expand Down
11 changes: 9 additions & 2 deletions src/encoding/fromBytes.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
import { type Hex, toHex } from "../index.js";

const decoder = new TextDecoder("utf8");

/**
* Converts bytes to a string.
* @param bytes - The bytes to convert.
* @returns The string representation of the input.
*/
const bytesToString = (bytes: Uint8Array): string => {
const decoder = new TextDecoder("utf8");
const str = decoder.decode(bytes);

return str;
};

export { bytesToString };
const bytesToHex = (bytes: Uint8Array): Hex => {
return toHex(bytes);
};

export { bytesToString, bytesToHex };
8 changes: 5 additions & 3 deletions src/encoding/fromHex.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { hexToBigInt, hexToNumber } from "./fromHex.js";
import { hexToBigInt, hexToBytes, hexToNumber } from "./fromHex.js";

test("hexToBigInt", () => {
expect(hexToBigInt("0x7b")).toBe(123n);
expect(hexToBigInt("7b")).toBe(123n);
});

test("hexToNumber", () => {
expect(hexToNumber("0x7b")).toBe(123);
expect(hexToNumber("7b")).toBe(123);
});

test("hexToBytes", () => {
expect(hexToBytes("0x7b")).toEqual(Uint8Array.from([123]));
});
45 changes: 44 additions & 1 deletion src/encoding/fromHex.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,25 @@
import { BaseError } from "../errors/BaseError.js";
import type { Hex } from "../index.js";
import { addHexPrefix, removeHexPrefix } from "../utils/hex.js";

/**
* Convert a character code to a base16 number.
* @param charCode - The character code to convert.
* @returns The base16 representation of the input.
*/
const charCodeToBase16 = (charCode: number): number | undefined => {
if (charCode >= 48 && charCode <= 57) {
return charCode - 48;
}
if (charCode >= 65 && charCode <= 70) {
return charCode - 55;
}
if (charCode >= 97 && charCode <= 102) {
return charCode - 87;
}
return undefined;
};

/**
* Convert a hex string to a number.
* @param hex - The hex string to convert.
Expand All @@ -19,4 +38,28 @@ const hexToBigInt = (hex: Hex): bigint => {
return BigInt(addHexPrefix(hex));
};

export { hexToNumber, hexToBigInt };
const hexToBytes = (hex: Hex): Uint8Array => {
let hexString = hex.slice(2);
if (hexString.length % 2) {
hexString = `0${hexString}`;
}

const length = hexString.length / 2;
const bytes = new Uint8Array(length);

for (let index = 0, j = 0; index < length; index++) {
const nibbleLeft = charCodeToBase16(hexString.charCodeAt(j++));
const nibbleRight = charCodeToBase16(hexString.charCodeAt(j++));
if (nibbleLeft === undefined || nibbleRight === undefined) {
throw new BaseError(
`Invalid byte sequence ("${hexString[j - 2]}${
hexString[j - 1]
}" in "${hexString}").`,
);
}
bytes[index] = nibbleLeft * 16 + nibbleRight;
}
return bytes;
};

export { hexToNumber, hexToBigInt, hexToBytes };
3 changes: 3 additions & 0 deletions src/encoding/toHex.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ import { toHex } from "./toHex.js";

test("should convert a string to hex", () => {
expect(toHex("hello")).toBe("0x68656c6c6f");
expect(toHex("")).toBe("0x");
expect(toHex("some string")).toBe("0x736f6d6520737472696e67");
});

test("should convert a number to hex", () => {
expect(toHex(123)).toBe("0x7b");
expect(toHex(0)).toBe("0x0");
});

test("should convert a bigint to hex", () => {
Expand Down
Loading

0 comments on commit 314612f

Please sign in to comment.