Skip to content

fix: add instanceof fallback for cases of duplicated module resolution #982

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

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: 1 addition & 1 deletion src/abi/abi_type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ export class ABIAddressType extends ABIType {
return decodedAddress.publicKey;
}

if (value instanceof Address) {
if (typeof value === 'object' && 'publicKey' in value) {
return value.publicKey;
}

Expand Down
5 changes: 1 addition & 4 deletions src/encoding/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,7 @@ export class Address {
* Check if the address is equal to another address.
*/
equals(other: Address): boolean {
return (
other instanceof Address &&
utils.arrayEqual(this.publicKey, other.publicKey)
);
return utils.arrayEqual(this.publicKey, other.publicKey);
}

/**
Expand Down
19 changes: 15 additions & 4 deletions src/encoding/schema/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,14 @@ export class AddressSchema extends Schema {
}

public prepareMsgpack(data: unknown): MsgpackEncodingData {
if (data instanceof Address) {
return data.publicKey;
if (
data instanceof Address ||
(data &&
typeof data === 'object' &&
'publicKey' in data &&
data.publicKey instanceof Uint8Array)
) {
return data.publicKey as Uint8Array;
}
throw new Error(`Invalid address: (${typeof data}) ${data}`);
}
Expand All @@ -40,8 +46,13 @@ export class AddressSchema extends Schema {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
_options: PrepareJSONOptions
): JSONEncodingData {
if (data instanceof Address) {
return data.toString();
if (
data instanceof Address ||
(data && typeof data === 'object' && 'toString' in data)
) {
// Since are checking for the toString method, we might have something that isn't an Address
// Using Address.fromString allows us to throw an error if the input is not a valid address
return Address.fromString(data.toString()).toString();
}
throw new Error(`Invalid address: (${typeof data}) ${data}`);
}
Expand Down
7 changes: 7 additions & 0 deletions src/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ function ensureAddress(input: unknown): Address {
if (input instanceof Address) {
return input;
}

if (typeof input === 'object' && 'toString' in input) {
return Address.fromString(input.toString());
}

throw new Error(`Not an address: ${input}`);
}

Expand All @@ -93,6 +98,8 @@ function optionalAddress(input: unknown): Address | undefined {
addr = input;
} else if (typeof input === 'string') {
addr = Address.fromString(input);
} else if (typeof input === 'object' && 'toString' in input) {
addr = Address.fromString(input.toString());
} else {
throw new Error(`Not an address: ${input}`);
}
Expand Down