Skip to content

Commit

Permalink
Add default implementations for toString for Serializable classes (#544)
Browse files Browse the repository at this point in the history
* Add default implementations for toString for Serializable classes

* update
  • Loading branch information
heliuchuan authored Oct 18, 2024
1 parent 5929b32 commit e4ccc6c
Show file tree
Hide file tree
Showing 10 changed files with 24 additions and 115 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ All notable changes to the Aptos TypeScript SDK will be captured in this file. T
- Add the `isPrimitive` function to `TypeTag`.
- Add `showStdout` optional property to `Move` and `LocalNode` classes to control the output of the CLI commands
- Add support for MultiKey's in transaction simulations
- Adds default implementation for `toString` and `toStringWithoutPrefix` for `Serializable`

# 1.29.1 (2024-10-09)

Expand Down
16 changes: 16 additions & 0 deletions src/bcs/serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,22 @@ export abstract class Serializable {
const bcsBytes = this.bcsToBytes();
return Hex.fromHexInput(bcsBytes);
}

/**
* Returns the hex string representation of the `Serializable` value without the 0x prefix.
* @returns the hex format as a string without `0x` prefix.
*/
toStringWithoutPrefix(): string {
return this.bcsToHex().toStringWithoutPrefix();
}

/**
* Returns the hex string representation of the `Serializable` value with the 0x prefix.
* @returns the hex formatas a string prefixed by `0x`.
*/
toString(): string {
return `0x${this.toStringWithoutPrefix()}`;
}
}

/**
Expand Down
9 changes: 0 additions & 9 deletions src/core/authenticationKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,6 @@ export class AuthenticationKey extends Serializable {
return new AuthenticationKey({ data: bytes });
}

/**
* Converts the internal data representation to a string format.
*
* @returns {string} The string representation of the internal data.
*/
toString(): string {
return this.data.toString();
}

/**
* Convert the internal data representation to a Uint8Array.
*
Expand Down
18 changes: 0 additions & 18 deletions src/core/crypto/ephemeral.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,6 @@ export class EphemeralPublicKey extends PublicKey {
}
}

/**
* Get the public key in bytes as a Uint8Array.
*
* @returns Uint8Array representation of the public key.
*/
toUint8Array(): Uint8Array {
return this.bcsToBytes();
}

/**
* Verifies a signed message using the ephemeral public key.
*
Expand Down Expand Up @@ -129,15 +120,6 @@ export class EphemeralSignature extends Signature {
}
}

/**
* Get the public key in bytes (Uint8Array).
*
* @returns Uint8Array representation of the public key
*/
toUint8Array(): Uint8Array {
return this.bcsToBytes();
}

/**
* Deserializes an ephemeral signature from a hexadecimal input.
* This function allows you to convert a hexadecimal representation of an ephemeral signature into its deserialized form for
Expand Down
19 changes: 0 additions & 19 deletions src/core/crypto/federatedKeyless.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

import { AccountPublicKey, PublicKey } from "./publicKey";
import { Deserializer, Serializer } from "../../bcs";
import { Hex } from "../hex";
import { HexInput, AnyPublicKeyVariant, SigningScheme } from "../../types";
import { AuthenticationKey } from "../authenticationKey";
import { AccountAddress, AccountAddressInput } from "../accountAddress";
Expand Down Expand Up @@ -48,24 +47,6 @@ export class FederatedKeylessPublicKey extends AccountPublicKey {
});
}

/**
* Get the public key in bytes (Uint8Array).
*
* @returns Uint8Array representation of the public key
*/
toUint8Array(): Uint8Array {
return this.bcsToBytes();
}

/**
* Get the public key as a hex string with the 0x prefix.
*
* @returns string representation of the public key
*/
toString(): string {
return Hex.fromHexInput(this.toUint8Array()).toString();
}

/**
* Verifies a signed data with a public key
*
Expand Down
37 changes: 0 additions & 37 deletions src/core/crypto/keyless.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,25 +97,6 @@ export class KeylessPublicKey extends AccountPublicKey {
});
}

/**
* Get the public key in bytes as a Uint8Array.
*
* @returns Uint8Array representation of the public key.
*/
toUint8Array(): Uint8Array;
toUint8Array(): Uint8Array {
return this.bcsToBytes();
}

/**
* Get the public key as a hex string with the 0x prefix.
*
* @returns string representation of the public key
*/
toString(): string {
return Hex.fromHexInput(this.toUint8Array()).toString();
}

/**
* Verifies the validity of a signature for a given message.
*
Expand Down Expand Up @@ -301,15 +282,6 @@ export class KeylessSignature extends Signature {
this.ephemeralSignature = ephemeralSignature;
}

/**
* Get the signature in bytes (Uint8Array).
*
* @returns Uint8Array representation of the signature
*/
toUint8Array(): Uint8Array {
return this.bcsToBytes();
}

serialize(serializer: Serializer): void {
this.ephemeralCertificate.serialize(serializer);
serializer.serializeStr(this.jwtHeader);
Expand Down Expand Up @@ -583,15 +555,6 @@ export class ZeroKnowledgeSig extends Signature {
this.overrideAudVal = overrideAudVal;
}

/**
* Get the signature in bytes (Uint8Array).
*
* @returns Uint8Array representation of the signature
*/
toUint8Array(): Uint8Array {
return this.bcsToBytes();
}

/**
* Deserialize a ZeroKnowledgeSig object from its BCS serialization in bytes.
*
Expand Down
18 changes: 0 additions & 18 deletions src/core/crypto/multiKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,16 +108,6 @@ export class MultiKey extends AccountPublicKey {
});
}

/**
* Convert the current instance to a Uint8Array representation.
* This is useful for serializing data for transmission or storage.
*
* @returns Uint8Array representation of the instance.
*/
toUint8Array(): Uint8Array {
return this.bcsToBytes();
}

// endregion

// region Serializable
Expand Down Expand Up @@ -330,14 +320,6 @@ export class MultiKeySignature extends Signature {
return bitmap;
}

// region Signature

toUint8Array(): Uint8Array {
return this.bcsToBytes();
}

// endregion

// region Serializable

serialize(serializer: Serializer): void {
Expand Down
13 changes: 1 addition & 12 deletions src/core/crypto/proof.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,7 @@
import { Serializable } from "../../bcs";
import { Hex } from "../hex";

/**
* An abstract representation of a cryptographic proof associated with specific
* zero-knowledge proof schemes, such as Groth16 and PLONK.
*/
export abstract class Proof extends Serializable {
/**
* Get the proof as a hex string with a 0x prefix.
*
* @returns The proof represented as a hex string.
*/
toString(): string {
const bytes = this.bcsToBytes();
return Hex.fromHexInput(bytes).toString();
}
}
export abstract class Proof extends Serializable {}
4 changes: 3 additions & 1 deletion src/core/crypto/publicKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ export abstract class PublicKey extends Serializable {
/**
* Get the raw public key bytes
*/
abstract toUint8Array(): Uint8Array;
toUint8Array(): Uint8Array {
return this.bcsToBytes();
}

/**
* Get the public key as a hex string with a 0x prefix.
Expand Down
4 changes: 3 additions & 1 deletion src/core/crypto/signature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ export abstract class Signature extends Serializable {
/**
* Get the raw signature bytes
*/
abstract toUint8Array(): Uint8Array;
toUint8Array(): Uint8Array {
return this.bcsToBytes();
}

/**
* Get the signature as a hex string with a 0x prefix e.g. 0x123456...
Expand Down

0 comments on commit e4ccc6c

Please sign in to comment.