Skip to content

Commit

Permalink
[types] Use appropriate type when necessary (#92)
Browse files Browse the repository at this point in the history
* [script] Use HexInput for bytecode in scripts

* [args] Ensure arguments use AnyNumber as inputs for BigInt

* [consistency] Name ModuleId appropriately
  • Loading branch information
gregnazario authored Oct 18, 2023
1 parent ae93ba1 commit 630148f
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 21 deletions.
16 changes: 8 additions & 8 deletions src/bcs/serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -328,8 +328,8 @@ export const outOfRangeErrorMessage = (value: AnyNumber, min: AnyNumber, max: An
`${value} is out of range: [${min}, ${max}]`;

export function validateNumberInRange<T extends AnyNumber>(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));
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/transactions/instances/transactionPayload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

/**
Expand Down Expand Up @@ -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.
*
Expand All @@ -205,12 +205,12 @@ export class EntryFunction {
* @returns EntryFunction
*/
static build(
module_name: `${string}::${string}`,
module_id: MoveModuleId,
function_name: string,
type_args: Array<TypeTag>,
args: Array<EntryFunctionArgument>,
): 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 {
Expand Down
5 changes: 2 additions & 3 deletions src/transactions/transaction_builder/transaction_builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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),
);
}

Expand Down
12 changes: 6 additions & 6 deletions src/transactions/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down Expand Up @@ -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;
};

/**
Expand Down Expand Up @@ -97,7 +97,7 @@ export type MultiSigData = {
* The data needed to generate a Script payload
*/
export type ScriptData = {
bytecode: string;
bytecode: HexInput;
typeArguments?: Array<TypeTag>;
arguments: Array<ScriptFunctionArgumentTypes>;
};
Expand Down

0 comments on commit 630148f

Please sign in to comment.