Skip to content

Commit

Permalink
Serialization/deserialization fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
xbtmatt committed Oct 11, 2023
1 parent c04058a commit 28194ad
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 15 deletions.
13 changes: 6 additions & 7 deletions src/api/transaction_submission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {
GenerateSingleSignerRawTransactionInput,
SingleSignerTransaction,
SimulateTransactionData,
GenerateSingleSignerRawTransactionArgs,
} from "../transactions/types";
import { UserTransactionResponse, PendingTransactionResponse } from "../types";
import {
Expand Down Expand Up @@ -89,12 +88,12 @@ export class TransactionSubmission {
const payload = await generateTransactionPayload(data);
const rawTransaction = await generateTransaction({
aptosConfig: this.config,
sender: sender,
payload: payload,
options: options,
secondarySignerAddresses: secondarySignerAddresses,
feePayerAddress: feePayerAddress,
} as GenerateSingleSignerRawTransactionArgs);
sender,
payload,
options,
secondarySignerAddresses,
feePayerAddress,
});
return rawTransaction;
}

Expand Down
10 changes: 10 additions & 0 deletions src/bcs/serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
MAX_U256_BIG_INT,
} from "./consts";
import { AnyNumber, Uint16, Uint32, Uint8 } from "../types";
import { Hex } from "../core";

// This class is intended to be used as a base class for all serializable types.
// It can be used to facilitate composable serialization of a complex type and
Expand All @@ -28,6 +29,15 @@ export abstract class Serializable {
this.serialize(serializer);
return serializer.toUint8Array();
}

/**
* Helper function to get a value's BCS-serialized bytes as a Hex instance.
* @returns a Hex instance with the BCS-serialized bytes loaded into its underlying Uint8Array
*/
bcsToHex() { //: Hex {
// const bcsBytes = this.bcsToBytes();
// return Hex.fromHexInput({ hexInput: bcsBytes });
}
}

export class Serializer {
Expand Down
14 changes: 10 additions & 4 deletions src/transactions/instances/transactionArgument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,21 @@ export class EntryFunctionBytes
this.value = new FixedBytes(value);
}

// Note that both serialize and serializeForEntryFunction are the same.
// We need them to be equivalent so that when we re-serialize this class as an entry
// function payload's arguments, we don't re-serialize the length prefix.
// Note that to see the Move, BCS-serialized representation of the underlying fixed byte vector,
// we must not serialize the length prefix.
//
// In other words, this class is only used to represent a sequence of bytes that are already
// BCS-serialized as a type. To represent those bytes accurately, the BCS-serialized form is the same exact
// representation.
serialize(serializer: Serializer): void {
serializer.serialize(this.value);
}

// This is necessary to implement the interface, so we can use this class as an EntryFunctionArgument.
// When we serialize these bytes as an entry function argument, we need to
// serialize the length prefix. This essentially converts the underlying fixed byte vector to a type-agnostic
// byte vector to an `any` type.
serializeForEntryFunction(serializer: Serializer): void {
serializer.serializeU32AsUleb128(this.value.value.length);
serializer.serialize(this);
}
/**
Expand Down
4 changes: 1 addition & 3 deletions tests/unit/script_transaction_arguments.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,7 @@ describe("Tests for the script transaction argument class", () => {
serializer = new Serializer();
input.serializeForScriptFunction(serializer);
const deserializer = new Deserializer(serializer.toUint8Array());
const asdf = ScriptTransactionArgument.deserialize(deserializer);
console.log(asdf);
return asdf;
return ScriptTransactionArgument.deserialize(deserializer);
};

expect(deserializeToScriptArg(new U8(1)) instanceof ScriptTransactionArgumentU8).toBe(true);
Expand Down
7 changes: 6 additions & 1 deletion tests/unit/transaction_builder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { Network } from "../../src/utils/apiEndpoints";
import { SignedTransaction } from "../../src/transactions/instances/signedTransaction";
import { U64 } from "../../src/bcs/serializable/move-primitives";
import { MoveObject } from "../../src/bcs/serializable/move-structs";
import { Hex } from "../../src/core";

describe("transaction builder", () => {
describe("generate transaction payload", () => {
Expand Down Expand Up @@ -443,7 +444,11 @@ describe("transaction builder", () => {
hexInput: "0x5aba8dab1c523be32bd4dafe2cc612f7f8050ce42a3322b60216ef67dc97768c",
}),
});
const bob = Account.generate({ scheme: SigningScheme.Ed25519 });
const bob = Account.fromPrivateKey({
privateKey: new Ed25519PrivateKey({
hexInput: "0x5aba8dab1c523be32bd4dafe2cc612f7f8050ce42a3322b60216ef67dc97768c",
}),
});
const payload = generateTransactionPayload({
function: "0x1::aptos_account::transfer",
type_arguments: [],
Expand Down

0 comments on commit 28194ad

Please sign in to comment.