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 method that queries the mirror node to get the actual num field #1790

Merged
merged 2 commits into from
Aug 8, 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
201 changes: 103 additions & 98 deletions common_js_test/pnpm-lock.yaml

Large diffs are not rendered by default.

983 changes: 501 additions & 482 deletions examples/pnpm-lock.yaml

Large diffs are not rendered by default.

1,172 changes: 603 additions & 569 deletions examples/simple_rest_signature_provider/pnpm-lock.yaml

Large diffs are not rendered by default.

1,084 changes: 562 additions & 522 deletions packages/proto/pnpm-lock.yaml

Large diffs are not rendered by default.

3,078 changes: 1,567 additions & 1,511 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/EntityIdHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export function constructor(props, realmOrNull, numOrNull) {
throw new Error("invalid entity ID");
}

// If the first parameter is a nubmer then we need to conver the
// If the first parameter is a number then we need to convert the
// first, second, and third parameters into numbers. Otherwise,
// we should look at the fields `shard`, `realm`, and `num` on
// `props`
Expand Down
39 changes: 38 additions & 1 deletion src/account/AccountId.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import CACHE from "../Cache.js";
import EvmAddress from "../EvmAddress.js";
import * as hex from ".././encoding/hex.js";
import { isLongZeroAddress } from "../util.js";
import axios from "axios";

/**
* @typedef {import("../client/Client.js").default<*, *>} Client
Expand Down Expand Up @@ -172,6 +173,34 @@ export default class AccountId {
return this.evmAddress;
}

/**
* @description Gets the actual `num` field of the `AccountId` from the Mirror Node.
* Should be used after generating `AccountId.fromEvmAddress()` because it sets the `num` field to `0`
* automatically since there is no connection between the `num` and the `evmAddress`
* @param {Client} client
* @returns {Promise<AccountId>}
*/
async populateAccountNum(client) {
if (this.evmAddress === null) {
throw new Error("field `evmAddress` should not be null");
}
const mirrorUrl = client.mirrorNetwork[0].slice(
0,
client.mirrorNetwork[0].indexOf(":")
);

/* eslint-disable */
const url = `https://${mirrorUrl}/api/v1/accounts/${this.evmAddress.toString()}`;
const mirrorAccountId = (await axios.get(url)).data.account;

this.num = Long.fromString(
mirrorAccountId.slice(mirrorAccountId.lastIndexOf(".") + 1)
);
/* eslint-enable */

return this;
}

/**
* @deprecated - Use `validateChecksum` instead
* @param {Client} client
Expand Down Expand Up @@ -226,7 +255,15 @@ export default class AccountId {
* @returns {string}
*/
toSolidityAddress() {
return entity_id.toSolidityAddress([this.shard, this.realm, this.num]);
if (this.evmAddress != null) {
return this.evmAddress.toString();
} else {
return entity_id.toSolidityAddress([
this.shard,
this.realm,
this.num,
]);
}
}

//TODO remove the comments after we get to HIP-631
Expand Down
31 changes: 31 additions & 0 deletions src/contract/ContractId.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import * as hex from "../encoding/hex.js";
import { arrayEqual } from "../array.js";
import Long from "long";
import { isLongZeroAddress } from "../util.js";
import axios from "axios";

/**
* @typedef {import("../client/Client.js").default<*, *>} Client
Expand Down Expand Up @@ -117,6 +118,36 @@ export default class ContractId extends Key {
return this._checksum;
}

/**
* @description Gets the actual `num` field of the `ContractId` from the Mirror Node.
* Should be used after generating `ContractId.fromEvmAddress()` because it sets the `num` field to `0`
* automatically since there is no connection between the `num` and the `evmAddress`
* @param {Client} client
* @returns {Promise<ContractId>}
*/
async populateAccountNum(client) {
if (this.evmAddress === null) {
throw new Error("field `evmAddress` should not be null");
}
const mirrorUrl = client.mirrorNetwork[0].slice(
0,
client.mirrorNetwork[0].indexOf(":")
);

/* eslint-disable */
const url = `https://${mirrorUrl}/api/v1/contracts/${hex.encode(
this.evmAddress
)}`;
const mirrorAccountId = (await axios.get(url)).data.contract_id;

this.num = Long.fromString(
mirrorAccountId.slice(mirrorAccountId.lastIndexOf(".") + 1)
);
/* eslint-enable */

return this;
}

/**
* @deprecated - Use `validateChecksum` instead
* @param {Client} client
Expand Down