Skip to content

Commit

Permalink
rename enum variants and transaction arguments (aptos-labs#10384)
Browse files Browse the repository at this point in the history
  • Loading branch information
0xmaayan authored and Zekun Wang committed Oct 13, 2023
1 parent a0797a8 commit 5852a13
Show file tree
Hide file tree
Showing 8 changed files with 263 additions and 264 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
import { Serializer, Deserializer, Serializable } from "../../bcs";
import { Ed25519PublicKey, Ed25519Signature } from "../../crypto/ed25519";
import { MultiEd25519PublicKey, MultiEd25519Signature } from "../../crypto/multi_ed25519";
import { RustEnumAccountAuthenticatorVariant } from "../../types";
import { AccountAuthenticatorVariant } from "../../types";

export abstract class AccountAuthenticator extends Serializable {
abstract serialize(serializer: Serializer): void;

static deserialize(deserializer: Deserializer): AccountAuthenticator {
const index = deserializer.deserializeUleb128AsU32();
switch (index) {
case RustEnumAccountAuthenticatorVariant.AccountAuthenticatorEd25519:
case AccountAuthenticatorVariant.AccountAuthenticatorEd25519:
return AccountAuthenticatorEd25519.load(deserializer);
case RustEnumAccountAuthenticatorVariant.AccountAuthenticatorMultiEd25519:
case AccountAuthenticatorVariant.AccountAuthenticatorMultiEd25519:
return AccountAuthenticatorMultiEd25519.load(deserializer);
default:
throw new Error(`Unknown variant index for AccountAuthenticator: ${index}`);
Expand All @@ -40,7 +40,7 @@ export class AccountAuthenticatorEd25519 extends AccountAuthenticator {
*
*/
serialize(serializer: Serializer): void {
serializer.serializeU32AsUleb128(RustEnumAccountAuthenticatorVariant.AccountAuthenticatorEd25519);
serializer.serializeU32AsUleb128(AccountAuthenticatorVariant.AccountAuthenticatorEd25519);
this.public_key.serialize(serializer);
this.signature.serialize(serializer);
}
Expand Down Expand Up @@ -71,7 +71,7 @@ export class AccountAuthenticatorMultiEd25519 extends AccountAuthenticator {
}

serialize(serializer: Serializer): void {
serializer.serializeU32AsUleb128(RustEnumAccountAuthenticatorVariant.AccountAuthenticatorMultiEd25519);
serializer.serializeU32AsUleb128(AccountAuthenticatorVariant.AccountAuthenticatorMultiEd25519);
this.public_key.serialize(serializer);
this.signature.serialize(serializer);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Deserializer, Serializer } from "../../bcs";
import { AccountAddress } from "../../core";
import { Ed25519PublicKey, Ed25519Signature } from "../../crypto/ed25519";
import { MultiEd25519PublicKey, MultiEd25519Signature } from "../../crypto/multi_ed25519";
import { RustEnumTransactionAuthenticatorVariant } from "../../types";
import { TransactionAuthenticatorVariant } from "../../types";
import { AccountAuthenticator } from "./account";

export abstract class TransactionAuthenticator {
Expand All @@ -13,13 +13,13 @@ export abstract class TransactionAuthenticator {
static deserialize(deserializer: Deserializer): TransactionAuthenticator {
const index = deserializer.deserializeUleb128AsU32();
switch (index) {
case RustEnumTransactionAuthenticatorVariant.TransactionAuthenticatorEd25519:
case TransactionAuthenticatorVariant.TransactionAuthenticatorEd25519:
return TransactionAuthenticatorEd25519.load(deserializer);
case RustEnumTransactionAuthenticatorVariant.TransactionAuthenticatorMultiEd25519:
case TransactionAuthenticatorVariant.TransactionAuthenticatorMultiEd25519:
return TransactionAuthenticatorMultiEd25519.load(deserializer);
case RustEnumTransactionAuthenticatorVariant.TransactionAuthenticatorMultiAgent:
case TransactionAuthenticatorVariant.TransactionAuthenticatorMultiAgent:
return TransactionAuthenticatorMultiAgent.load(deserializer);
case RustEnumTransactionAuthenticatorVariant.TransactionAuthenticatorFeePayer:
case TransactionAuthenticatorVariant.TransactionAuthenticatorFeePayer:
return TransactionAuthenticatorFeePayer.load(deserializer);
default:
throw new Error(`Unknown variant index for TransactionAuthenticator: ${index}`);
Expand Down Expand Up @@ -47,7 +47,7 @@ export class TransactionAuthenticatorEd25519 extends TransactionAuthenticator {
}

serialize(serializer: Serializer): void {
serializer.serializeU32AsUleb128(RustEnumTransactionAuthenticatorVariant.TransactionAuthenticatorEd25519);
serializer.serializeU32AsUleb128(TransactionAuthenticatorVariant.TransactionAuthenticatorEd25519);
this.public_key.serialize(serializer);
this.signature.serialize(serializer);
}
Expand Down Expand Up @@ -78,7 +78,7 @@ export class TransactionAuthenticatorMultiEd25519 extends TransactionAuthenticat
}

serialize(serializer: Serializer): void {
serializer.serializeU32AsUleb128(RustEnumTransactionAuthenticatorVariant.TransactionAuthenticatorMultiEd25519);
serializer.serializeU32AsUleb128(TransactionAuthenticatorVariant.TransactionAuthenticatorMultiEd25519);
this.public_key.serialize(serializer);
this.signature.serialize(serializer);
}
Expand Down Expand Up @@ -117,7 +117,7 @@ export class TransactionAuthenticatorMultiAgent extends TransactionAuthenticator
}

serialize(serializer: Serializer): void {
serializer.serializeU32AsUleb128(RustEnumTransactionAuthenticatorVariant.TransactionAuthenticatorMultiAgent);
serializer.serializeU32AsUleb128(TransactionAuthenticatorVariant.TransactionAuthenticatorMultiAgent);
this.sender.serialize(serializer);
serializer.serializeVector<AccountAddress>(this.secondary_signer_addresses);
serializer.serializeVector<AccountAuthenticator>(this.secondary_signers);
Expand Down Expand Up @@ -163,7 +163,7 @@ export class TransactionAuthenticatorFeePayer extends TransactionAuthenticator {
}

serialize(serializer: Serializer): void {
serializer.serializeU32AsUleb128(RustEnumTransactionAuthenticatorVariant.TransactionAuthenticatorFeePayer);
serializer.serializeU32AsUleb128(TransactionAuthenticatorVariant.TransactionAuthenticatorFeePayer);
this.sender.serialize(serializer);
serializer.serializeVector<AccountAddress>(this.secondary_signer_addresses);
serializer.serializeVector<AccountAuthenticator>(this.secondary_signers);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export * from "./chainId";
export * from "./rawTransaction";
export * from "./transactionArguments";
export * from "./scriptTransactionArguments";
export * from "./transactionPayload";
export * from "./moduleId";
export * from "./identifier";
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

import { Deserializer, Serializer } from "../../bcs";
import { AccountAddress } from "../../core";
import { RustEnumTransactionVariants } from "../../types";
import { TransactionVariants } from "../../types";
import { ChainId } from "./chainId";
import { TransactionArgument } from "./transactionArguments";
import { TransactionPayload } from "./transactionPayload";

/**
Expand Down Expand Up @@ -104,9 +103,9 @@ export abstract class RawTransactionWithData {
// undex enum variant
const index = deserializer.deserializeUleb128AsU32();
switch (index) {
case RustEnumTransactionVariants.MultiAgentTransaction:
case TransactionVariants.MultiAgentTransaction:
return MultiAgentRawTransaction.load(deserializer);
case RustEnumTransactionVariants.FeePayerTransaction:
case TransactionVariants.FeePayerTransaction:
return FeePayerRawTransaction.load(deserializer);
default:
throw new Error(`Unknown variant index for RawTransactionWithData: ${index}`);
Expand Down Expand Up @@ -135,9 +134,9 @@ export class MultiAgentRawTransaction extends RawTransactionWithData {
}

serialize(serializer: Serializer): void {
serializer.serializeU32AsUleb128(RustEnumTransactionVariants.MultiAgentTransaction);
serializer.serializeU32AsUleb128(TransactionVariants.MultiAgentTransaction);
this.raw_txn.serialize(serializer);
serializer.serializeVector<TransactionArgument>(this.secondary_signer_addresses);
serializer.serializeVector(this.secondary_signer_addresses);
}

static load(deserializer: Deserializer): MultiAgentRawTransaction {
Expand Down Expand Up @@ -179,9 +178,9 @@ export class FeePayerRawTransaction extends RawTransactionWithData {
}

serialize(serializer: Serializer): void {
serializer.serializeU32AsUleb128(RustEnumTransactionVariants.FeePayerTransaction);
serializer.serializeU32AsUleb128(TransactionVariants.FeePayerTransaction);
this.raw_txn.serialize(serializer);
serializer.serializeVector<TransactionArgument>(this.secondary_signer_addresses);
serializer.serializeVector(this.secondary_signer_addresses);
this.fee_payer_address.serialize(serializer);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
import { Serializer, Deserializer, Serializable } from "../../bcs";
import { AccountAddress } from "../../core";
import { ScriptTransactionArgumentVariants } from "../../types";

/**
* Representation of a Script Transaction Argument that can be serialized and deserialized
*/
export abstract class ScriptTransactionArgument extends Serializable {
/**
* Serialize a Script Transaction Argument
*/
abstract serialize(serializer: Serializer): void;

/**
* Deserialize a Script Transaction Argument
*/
static deserialize(deserializer: Deserializer): ScriptTransactionArgument {
// index enum variant
const index = deserializer.deserializeUleb128AsU32();
switch (index) {
case ScriptTransactionArgumentVariants.ScriptTransactionArgumentU8:
return ScriptTransactionArgumentU8.load(deserializer);
case ScriptTransactionArgumentVariants.ScriptTransactionArgumentU64:
return ScriptTransactionArgumentU64.load(deserializer);
case ScriptTransactionArgumentVariants.ScriptTransactionArgumentU128:
return ScriptTransactionArgumentU128.load(deserializer);
case ScriptTransactionArgumentVariants.ScriptTransactionArgumentAddress:
return ScriptTransactionArgumentAddress.load(deserializer);
case ScriptTransactionArgumentVariants.ScriptTransactionArgumentU8Vector:
return ScriptTransactionArgumentU8Vector.load(deserializer);
case ScriptTransactionArgumentVariants.ScriptTransactionArgumentBool:
return ScriptTransactionArgumentBool.load(deserializer);
case ScriptTransactionArgumentVariants.ScriptTransactionArgumentU16:
return ScriptTransactionArgumentU16.load(deserializer);
case ScriptTransactionArgumentVariants.ScriptTransactionArgumentU32:
return ScriptTransactionArgumentU32.load(deserializer);
case ScriptTransactionArgumentVariants.ScriptTransactionArgumentU256:
return ScriptTransactionArgumentU256.load(deserializer);
default:
throw new Error(`Unknown variant index for ScriptTransactionArgument: ${index}`);
}
}
}

export class ScriptTransactionArgumentU8 extends ScriptTransactionArgument {
public readonly value: number;

constructor(value: number) {
super();
this.value = value;
}

serialize(serializer: Serializer): void {
serializer.serializeU32AsUleb128(ScriptTransactionArgumentVariants.ScriptTransactionArgumentU8);
serializer.serializeU8(this.value);
}

static load(deserializer: Deserializer): ScriptTransactionArgumentU8 {
const value = deserializer.deserializeU8();
return new ScriptTransactionArgumentU8(value);
}
}

export class ScriptTransactionArgumentU16 extends ScriptTransactionArgument {
public readonly value: number;

constructor(value: number) {
super();
this.value = value;
}

serialize(serializer: Serializer): void {
serializer.serializeU32AsUleb128(ScriptTransactionArgumentVariants.ScriptTransactionArgumentU16);
serializer.serializeU16(this.value);
}

static load(deserializer: Deserializer): ScriptTransactionArgumentU16 {
const value = deserializer.deserializeU16();
return new ScriptTransactionArgumentU16(value);
}
}

export class ScriptTransactionArgumentU32 extends ScriptTransactionArgument {
public readonly value: number;

constructor(value: number) {
super();
this.value = value;
}

serialize(serializer: Serializer): void {
serializer.serializeU32AsUleb128(ScriptTransactionArgumentVariants.ScriptTransactionArgumentU32);
serializer.serializeU32(this.value);
}

static load(deserializer: Deserializer): ScriptTransactionArgumentU32 {
const value = deserializer.deserializeU32();
return new ScriptTransactionArgumentU32(value);
}
}

export class ScriptTransactionArgumentU64 extends ScriptTransactionArgument {
public readonly value: bigint;

constructor(value: bigint) {
super();
this.value = value;
}

serialize(serializer: Serializer): void {
serializer.serializeU32AsUleb128(ScriptTransactionArgumentVariants.ScriptTransactionArgumentU64);
serializer.serializeU64(this.value);
}

static load(deserializer: Deserializer): ScriptTransactionArgumentU64 {
const value = deserializer.deserializeU64();
return new ScriptTransactionArgumentU64(value);
}
}

export class ScriptTransactionArgumentU128 extends ScriptTransactionArgument {
public readonly value: bigint;

constructor(value: bigint) {
super();
this.value = value;
}

serialize(serializer: Serializer): void {
serializer.serializeU32AsUleb128(ScriptTransactionArgumentVariants.ScriptTransactionArgumentU128);
serializer.serializeU128(this.value);
}

static load(deserializer: Deserializer): ScriptTransactionArgumentU128 {
const value = deserializer.deserializeU128();
return new ScriptTransactionArgumentU128(value);
}
}

export class ScriptTransactionArgumentU256 extends ScriptTransactionArgument {
public readonly value: bigint;

constructor(value: bigint) {
super();
this.value = value;
}

serialize(serializer: Serializer): void {
serializer.serializeU32AsUleb128(ScriptTransactionArgumentVariants.ScriptTransactionArgumentU256);
serializer.serializeU256(this.value);
}

static load(deserializer: Deserializer): ScriptTransactionArgumentU256 {
const value = deserializer.deserializeU256();
return new ScriptTransactionArgumentU256(value);
}
}

export class ScriptTransactionArgumentAddress extends ScriptTransactionArgument {
public readonly value: AccountAddress;

constructor(value: AccountAddress) {
super();
this.value = value;
}

serialize(serializer: Serializer): void {
serializer.serializeU32AsUleb128(ScriptTransactionArgumentVariants.ScriptTransactionArgumentAddress);
this.value.serialize(serializer);
}

static load(deserializer: Deserializer): ScriptTransactionArgumentAddress {
const value = AccountAddress.deserialize(deserializer);
return new ScriptTransactionArgumentAddress(value);
}
}

export class ScriptTransactionArgumentU8Vector extends ScriptTransactionArgument {
public readonly value: Uint8Array;

constructor(value: Uint8Array) {
super();
this.value = value;
}

serialize(serializer: Serializer): void {
serializer.serializeU32AsUleb128(ScriptTransactionArgumentVariants.ScriptTransactionArgumentU8Vector);
serializer.serializeBytes(this.value);
}

static load(deserializer: Deserializer): ScriptTransactionArgumentU8Vector {
const value = deserializer.deserializeBytes();
return new ScriptTransactionArgumentU8Vector(value);
}
}

export class ScriptTransactionArgumentBool extends ScriptTransactionArgument {
public readonly value: boolean;

constructor(value: boolean) {
super();
this.value = value;
}

serialize(serializer: Serializer): void {
serializer.serializeU32AsUleb128(ScriptTransactionArgumentVariants.ScriptTransactionArgumentBool);
serializer.serializeBool(this.value);
}

static load(deserializer: Deserializer): ScriptTransactionArgumentBool {
const value = deserializer.deserializeBool();
return new ScriptTransactionArgumentBool(value);
}
}
Loading

0 comments on commit 5852a13

Please sign in to comment.