diff --git a/CHANGELOG.md b/CHANGELOG.md index 5e47f3fc5..290406179 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/src/bcs/serializer.ts b/src/bcs/serializer.ts index 427a3eb22..e6ba4b6dd 100644 --- a/src/bcs/serializer.ts +++ b/src/bcs/serializer.ts @@ -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()}`; + } } /** diff --git a/src/core/authenticationKey.ts b/src/core/authenticationKey.ts index b8bb544f4..e6fcbfea9 100644 --- a/src/core/authenticationKey.ts +++ b/src/core/authenticationKey.ts @@ -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. * diff --git a/src/core/crypto/ephemeral.ts b/src/core/crypto/ephemeral.ts index efd1a080d..69e618ed5 100644 --- a/src/core/crypto/ephemeral.ts +++ b/src/core/crypto/ephemeral.ts @@ -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. * @@ -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 diff --git a/src/core/crypto/federatedKeyless.ts b/src/core/crypto/federatedKeyless.ts index 583639f08..0a2fec22e 100644 --- a/src/core/crypto/federatedKeyless.ts +++ b/src/core/crypto/federatedKeyless.ts @@ -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"; @@ -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 * diff --git a/src/core/crypto/keyless.ts b/src/core/crypto/keyless.ts index 4c27a335d..e149a2fd1 100644 --- a/src/core/crypto/keyless.ts +++ b/src/core/crypto/keyless.ts @@ -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. * @@ -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); @@ -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. * diff --git a/src/core/crypto/multiKey.ts b/src/core/crypto/multiKey.ts index 6d40ed6b3..0317233c4 100644 --- a/src/core/crypto/multiKey.ts +++ b/src/core/crypto/multiKey.ts @@ -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 @@ -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 { diff --git a/src/core/crypto/proof.ts b/src/core/crypto/proof.ts index f1cdd632b..4a2d182cc 100644 --- a/src/core/crypto/proof.ts +++ b/src/core/crypto/proof.ts @@ -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 {} diff --git a/src/core/crypto/publicKey.ts b/src/core/crypto/publicKey.ts index 16128b012..90a1036a4 100644 --- a/src/core/crypto/publicKey.ts +++ b/src/core/crypto/publicKey.ts @@ -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. diff --git a/src/core/crypto/signature.ts b/src/core/crypto/signature.ts index b2f6725cc..023ca9358 100644 --- a/src/core/crypto/signature.ts +++ b/src/core/crypto/signature.ts @@ -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...