Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[types] Ensure string is checked before other types #328

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ All notable changes to the Aptos TypeScript SDK will be captured in this file. T

# Unreleased

- [`Fix`] Ensure that hex input strings are loaded as strings in Javascript

# 1.10.0 (2024-03-11)

- [`Deprecate`] IIFE build support
Expand Down
2 changes: 1 addition & 1 deletion examples/javascript/simple_transfer.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ const example = async () => {
console.log("\n=== Funding accounts ===\n");

const aliceFundTxn = await sdk.fundAccount({
accountAddress: alice.accountAddress,
accountAddress: alice.accountAddress.toString(),
amount: ALICE_INITIAL_BALANCE,
});
console.log("Alice's fund transaction: ", aliceFundTxn);
Expand Down
14 changes: 7 additions & 7 deletions src/core/accountAddress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -342,13 +342,13 @@ export class AccountAddress extends Serializable implements TransactionArgument
* @param input
*/
static from(input: AccountAddressInput): AccountAddress {
if (typeof input === "string") {
return AccountAddress.fromString(input);
}
if (input instanceof AccountAddress) {
return input;
}
if (input instanceof Uint8Array) {
return new AccountAddress(input);
}
return AccountAddress.fromString(input);
return new AccountAddress(input);
}

/**
Expand All @@ -358,13 +358,13 @@ export class AccountAddress extends Serializable implements TransactionArgument
* @param input
*/
static fromStrict(input: AccountAddressInput): AccountAddress {
if (input instanceof AccountAddress) {
return input;
if (typeof input === "string") {
return AccountAddress.fromStringStrict(input);
}
if (input instanceof Uint8Array) {
return new AccountAddress(input);
}
return AccountAddress.fromStringStrict(input);
return input;
}

// ===
Expand Down
7 changes: 5 additions & 2 deletions src/core/hex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,11 @@ export class Hex {
* @returns Hex
*/
static fromHexInput(hexInput: HexInput): Hex {
if (hexInput instanceof Uint8Array) return new Hex(hexInput);
return Hex.fromString(hexInput);
// Note we have to be more defensive here, since Javascript doesn't have the concept of types
if (typeof hexInput === "string") {
gregnazario marked this conversation as resolved.
Show resolved Hide resolved
return Hex.fromString(hexInput);
}
return new Hex(hexInput);
}

// ===
Expand Down
Loading