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

Add checks inside parsing methods from evm address #1741

Merged
merged 1 commit into from
Jul 27, 2023
Merged
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
32 changes: 22 additions & 10 deletions src/account/AccountId.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import Key from "../Key.js";
import PublicKey from "../PublicKey.js";
import CACHE from "../Cache.js";
import EvmAddress from "../EvmAddress.js";
import * as hex from ".././encoding/hex.js";
import { isLongZeroAddress } from "../util.js";

/**
* @typedef {import("../client/Client.js").default<*, *>} Client
Expand Down Expand Up @@ -103,15 +105,21 @@ export default class AccountId {
* @returns {AccountId}
*/
static fromEvmAddress(shard, realm, evmAddress) {
return new AccountId(
shard,
realm,
0,
undefined,
typeof evmAddress === "string"
? EvmAddress.fromString(evmAddress)
: evmAddress
);
const evmAddressObj = typeof evmAddress === "string"
? EvmAddress.fromString(evmAddress)
: evmAddress;

if (isLongZeroAddress(evmAddressObj.toBytes())) {
return this.fromSolidityAddress(evmAddressObj.toString())
} else {
return new AccountId(
shard,
realm,
0,
undefined,
evmAddressObj
);
}
}

/**
Expand Down Expand Up @@ -212,7 +220,11 @@ export default class AccountId {
* @returns {AccountId}
*/
static fromSolidityAddress(address) {
return new AccountId(...entity_id.fromSolidityAddress(address));
if (isLongZeroAddress(hex.decode(address))) {
return new AccountId(...entity_id.fromSolidityAddress(address));
} else {
return this.fromEvmAddress(0, 0, address);
}
}

/**
Expand Down
14 changes: 11 additions & 3 deletions src/contract/ContractId.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import CACHE from "../Cache.js";
import * as hex from "../encoding/hex.js";
import { arrayEqual } from "../array.js";
import Long from "long";
import { isLongZeroAddress } from "../util.js";

/**
* @typedef {import("../client/Client.js").default<*, *>} Client
Expand Down Expand Up @@ -64,7 +65,11 @@ export default class ContractId extends Key {
* @returns {ContractId}
*/
static fromEvmAddress(shard, realm, evmAddress) {
return new ContractId(shard, realm, 0, hex.decode(evmAddress));
if (isLongZeroAddress(hex.decode(evmAddress))) {
return this.fromSolidityAddress(evmAddress);
} else {
return new ContractId(shard, realm, 0, hex.decode(evmAddress));
}
}

/**
Expand Down Expand Up @@ -149,8 +154,11 @@ export default class ContractId extends Key {
* @returns {ContractId}
*/
static fromSolidityAddress(address) {
const [shard, realm, contract] = entity_id.fromSolidityAddress(address);
return new ContractId(shard, realm, contract);
if (isLongZeroAddress(hex.decode(address))) {
return new ContractId(...entity_id.fromSolidityAddress(address));
} else {
return this.fromEvmAddress(0, 0, address);
}
}

/**
Expand Down
15 changes: 15 additions & 0 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,21 @@ export function isStringOrUint8Array(variable) {
);
}

/**
* Takes an address as `Uint8Array` and returns whether or not this is a long-zero address
*
* @param {Uint8Array} address
* @returns {boolean}
*/
export function isLongZeroAddress(address) {
for (let i = 0; i < 12; i++) {
if (address[i] != 0) {
return false;
}
}
return true;
}

/**
* Takes any param and returns false if null or undefined.
*
Expand Down