From 630148f5acc17581b3b4f50cbeb1c8d15ba69684 Mon Sep 17 00:00:00 2001 From: Greg Nazario Date: Wed, 18 Oct 2023 14:53:41 -0400 Subject: [PATCH] [types] Use appropriate type when necessary (#92) * [script] Use HexInput for bytecode in scripts * [args] Ensure arguments use AnyNumber as inputs for BigInt * [consistency] Name ModuleId appropriately --- src/bcs/serializer.ts | 16 ++++++++-------- src/transactions/instances/transactionPayload.ts | 8 ++++---- .../transaction_builder/transaction_builder.ts | 5 ++--- src/transactions/types.ts | 12 ++++++------ 4 files changed, 20 insertions(+), 21 deletions(-) diff --git a/src/bcs/serializer.ts b/src/bcs/serializer.ts index a7e2cd4ef..25d43135b 100644 --- a/src/bcs/serializer.ts +++ b/src/bcs/serializer.ts @@ -188,8 +188,8 @@ export class Serializer { */ @checkNumberRange(BigInt(0), MAX_U64_BIG_INT) serializeU64(value: AnyNumber) { - const low = BigInt(value.toString()) & BigInt(MAX_U32_NUMBER); - const high = BigInt(value.toString()) >> BigInt(32); + const low = BigInt(value) & BigInt(MAX_U32_NUMBER); + const high = BigInt(value) >> BigInt(32); // write little endian number this.serializeU32(Number(low)); @@ -203,8 +203,8 @@ export class Serializer { */ @checkNumberRange(BigInt(0), MAX_U128_BIG_INT) serializeU128(value: AnyNumber) { - const low = BigInt(value.toString()) & MAX_U64_BIG_INT; - const high = BigInt(value.toString()) >> BigInt(64); + const low = BigInt(value) & MAX_U64_BIG_INT; + const high = BigInt(value) >> BigInt(64); // write little endian number this.serializeU64(low); @@ -218,8 +218,8 @@ export class Serializer { */ @checkNumberRange(BigInt(0), MAX_U256_BIG_INT) serializeU256(value: AnyNumber) { - const low = BigInt(value.toString()) & MAX_U128_BIG_INT; - const high = BigInt(value.toString()) >> BigInt(128); + const low = BigInt(value) & MAX_U128_BIG_INT; + const high = BigInt(value) >> BigInt(128); // write little endian number this.serializeU128(low); @@ -328,8 +328,8 @@ export const outOfRangeErrorMessage = (value: AnyNumber, min: AnyNumber, max: An `${value} is out of range: [${min}, ${max}]`; export function validateNumberInRange(value: T, minValue: T, maxValue: T) { - const valueBigInt = BigInt(value.toString()); - if (valueBigInt > BigInt(maxValue.toString()) || valueBigInt < BigInt(minValue.toString())) { + const valueBigInt = BigInt(value); + if (valueBigInt > BigInt(maxValue) || valueBigInt < BigInt(minValue)) { throw new Error(outOfRangeErrorMessage(value, minValue, maxValue)); } } diff --git a/src/transactions/instances/transactionPayload.ts b/src/transactions/instances/transactionPayload.ts index 3bf5397d3..7e19bd290 100644 --- a/src/transactions/instances/transactionPayload.ts +++ b/src/transactions/instances/transactionPayload.ts @@ -12,7 +12,7 @@ import { AccountAddress } from "../../core"; import { Identifier } from "./identifier"; import { ModuleId } from "./moduleId"; import type { EntryFunctionArgument, ScriptFunctionArgument, TransactionArgument } from "./transactionArgument"; -import { ScriptTransactionArgumentVariants, TransactionPayloadVariants } from "../../types"; +import { MoveModuleId, ScriptTransactionArgumentVariants, TransactionPayloadVariants } from "../../types"; import { TypeTag } from "../typeTag/typeTag"; /** @@ -186,7 +186,7 @@ export class EntryFunction { /** * A helper function to build a EntryFunction payload from raw primitive values * - * @param module_name Fully qualified module name in format "AccountAddress::module_name" e.g. "0x1::coin" + * @param module_id Fully qualified module name in format "AccountAddress::module_id" e.g. "0x1::coin" * @param function_name Function name * @param type_args Type arguments that move function requires. * @@ -205,12 +205,12 @@ export class EntryFunction { * @returns EntryFunction */ static build( - module_name: `${string}::${string}`, + module_id: MoveModuleId, function_name: string, type_args: Array, args: Array, ): EntryFunction { - return new EntryFunction(ModuleId.fromStr(module_name), new Identifier(function_name), type_args, args); + return new EntryFunction(ModuleId.fromStr(module_id), new Identifier(function_name), type_args, args); } serialize(serializer: Serializer): void { diff --git a/src/transactions/transaction_builder/transaction_builder.ts b/src/transactions/transaction_builder/transaction_builder.ts index 5bfcdf300..2b5456391 100644 --- a/src/transactions/transaction_builder/transaction_builder.ts +++ b/src/transactions/transaction_builder/transaction_builder.ts @@ -7,10 +7,9 @@ * and a signed transaction that can be simulated, signed and submitted to chain. */ import { sha3_256 as sha3Hash } from "@noble/hashes/sha3"; -import { hexToBytes } from "@noble/hashes/utils"; import { AptosConfig } from "../../api/aptosConfig"; import { Deserializer } from "../../bcs/deserializer"; -import { AccountAddress, PublicKey } from "../../core"; +import { AccountAddress, Hex, PublicKey } from "../../core"; import { Account } from "../../core/account"; import { Ed25519PublicKey, Ed25519Signature } from "../../core/crypto/ed25519"; import { Secp256k1PublicKey, Secp256k1Signature } from "../../core/crypto/secp256k1"; @@ -91,7 +90,7 @@ export function generateTransactionPayload(args: GenerateTransactionPayloadData) // generate script payload if ("bytecode" in args) { return new TransactionPayloadScript( - new Script(hexToBytes(args.bytecode), args.typeArguments ?? [], args.arguments), + new Script(Hex.fromHexInput(args.bytecode).toUint8Array(), args.typeArguments ?? [], args.arguments), ); } diff --git a/src/transactions/types.ts b/src/transactions/types.ts index 06f1becd0..38154a130 100644 --- a/src/transactions/types.ts +++ b/src/transactions/types.ts @@ -15,7 +15,7 @@ import { TransactionPayloadMultisig, TransactionPayloadScript, } from "./instances"; -import { HexInput, MoveStructType } from "../types"; +import { AnyNumber, HexInput, MoveStructType } from "../types"; import { TypeTag } from "./typeTag/typeTag"; export type EntryFunctionArgumentTypes = @@ -57,10 +57,10 @@ export type AnyRawTransactionInstance = RawTransaction | MultiAgentRawTransactio * Optional options to set when generating a transaction */ export type GenerateTransactionOptions = { - maxGasAmount?: string; - gasUnitPrice?: string; - expireTimestamp?: string; - accountSequenceNumber?: string | bigint; + maxGasAmount?: AnyNumber; + gasUnitPrice?: AnyNumber; + expireTimestamp?: AnyNumber; + accountSequenceNumber?: AnyNumber; }; /** @@ -97,7 +97,7 @@ export type MultiSigData = { * The data needed to generate a Script payload */ export type ScriptData = { - bytecode: string; + bytecode: HexInput; typeArguments?: Array; arguments: Array; };